public void addSR() { enqueueCommonReportPrefix(RTCPConstants.RTCP_PT_SR, _stream.SSRC(), 5 /* extra words in a SR */); // Now, add the 'sender info' for our sink // Insert the NTP and RTP timestamps for the 'wallclock time': TimeVal timeNow = new TimeVal(); RTPTime.GetTimestamp(ref timeNow); _fOutBuf.WriteWord((uint)(timeNow.tv_sec + 0x83AA7E80)); // NTP timestamp most-significant word (1970 epoch -> 1900 epoch) double fractionalPart = (timeNow.tv_usec / 15625.0) * 0x04000000; // 2^32/10^6 _fOutBuf.WriteWord((uint)(fractionalPart + 0.5)); // NTP timestamp least-significant word uint rtpTimestamp = _stream.ConvertToRTPTimestamp(timeNow); _fOutBuf.WriteWord(rtpTimestamp); // RTP ts // Insert the packet and byte counts: uint packetCount = _stream.PacketCount() - _session.PacketsCount; uint octetCount = _stream.OctetCount() - _session.OctetCount; _fOutBuf.WriteWord(packetCount); _fOutBuf.WriteWord(octetCount); if (_logger.IsDebugEnabled) { _logger.Debug("Sending report : {0}.{1} packet {2} octet {3} Sequence {4}", timeNow.tv_sec + 0x83AA7E80, (uint)(fractionalPart + 0.5), packetCount, octetCount, _stream.CurrentSeqNo()); } }
/// <summary> /// 描画スレッドで呼ぶ。 /// </summary> public void UpdateLatestValue(double timeSec, double val) { // 最大値、最小値更新。 if (mYMax < val) { mYMax = val; } if (val < mYMin) { mYMin = val; } if (mYMax == mYMin) { mYMax = mYMin + 0.001; } if (mXMax < timeSec) { mXMax = timeSec; } var timeVal = new TimeVal(timeSec, val); mTimeValList.Add(timeVal); Update(); }
public uint CurrentTimeStamp() { TimeVal timeNow = new TimeVal(); RTPTime.GetTimestamp(ref timeNow); return(ConvertToRTPTimestamp(timeNow)); }
public override string GenerateSDPDescription() { StringBuilder body = new StringBuilder(); TimeVal timeVal = new TimeVal(); RTPTime.GetTimestamp(ref timeVal); body.Append("v=0\r\n"). AppendFormat("o=- {0}{1:06} 1 IN IP4 {2}\r\n", timeVal.tv_sec, timeVal.tv_usec, SocketExtensions.GetLocalIPV4Address()). AppendFormat("s=Session streamed by \"{0}\"\r\n", RTSPServer.ServerVersion). AppendFormat("i={0}\r\n", Name). Append("t=0 0\r\n"). AppendFormat("a=tool:{0}\r\n", RTSPServer.ServerVersion). Append("a=type:broadcast\r\n"). Append("a=control:*\r\n"). Append("a=range:npt=0-\r\n"). AppendFormat("a=x-qt-text-nam:Session streamed by \"{0}\"\r\n", RTSPServer.ServerVersion). AppendFormat("a=x-qt-text-inf:{0}\r\n", Name). Append("m=video 0 RTP/AVP 96\r\n"). Append("c=IN IP4 0.0.0.0\r\n"). Append("b=AS:500\r\n"). Append("a=rtpmap:96 H264/90000\r\n"). Append("a=fmtp:96 packetization-mode=1;profile-level-id=640028;sprop-parameter-sets=J2QAKKwrQCgC3QDxImo=,KO4Pyw==\r\n"). Append("a=control:track1\r\n"); return(body.ToString()); }
// Start is called bef ore the first frame update void Start() { timeval = GameObject.Find("Tomato"); tval = timeval.GetComponent <TimeVal>(); seconds = countTime = tval.TVal + 0.02;// = 5.02;//初期化 }
protected void SetTimestamp(TimeVal framePresentationTime) { // First, convert the presentation time to a 32-bit RTP timestamp: uint currentTimestamp = ConvertToRTPTimestamp(framePresentationTime); // Then, insert it into the RTP packet: _outputBuffer.InsertWord(currentTimestamp, _timestampPosition); }
/// <summary> /// /// </summary> /// <param name="blockname"></param> public static void Begin(string blockname) { s_blockname += "." + blockname; if (!s_dict.ContainsKey(s_blockname)) { s_dict[s_blockname] = new TimeVal(); } TimeVal tv = s_dict[s_blockname]; tv.start = Environment.TickCount; }
public static bool TimeValGreater(ref TimeVal tv1, ref TimeVal tv2) { if (tv1.tv_sec > tv2.tv_sec) { return(true); } if ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec > tv2.tv_usec)) { return(true); } return(false); }
public static bool TimeValGreater(ref TimeVal tv1, ref TimeVal tv2) { //Debug.Log("VRPNManager: TimeValGreater"); if (tv1.tv_sec > tv2.tv_sec) { return(true); } if ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec > tv2.tv_usec)) { return(true); } return(false); }
public uint PresetNextTimestamp() { TimeVal timeNow = new TimeVal(); RTPTime.GetTimestamp(ref timeNow); uint tsNow = ConvertToRTPTimestamp(timeNow); if (_nextTimestampHasBeenPreset == false) { // Ideally, we shouldn't preset the timestamp if there _timestampBase = tsNow; _nextTimestampHasBeenPreset = true; } return(tsNow); }
/// <summary> /// /// </summary> /// <param name="blockname"></param> public static void End(string blockname) { int end = Environment.TickCount; // exclude lookup from time. if (blockname != s_blockname.Substring(s_blockname.Length - blockname.Length, blockname.Length)) { Debug.WriteLine("unmatched end for block " + blockname); } if (s_dict.ContainsKey(s_blockname)) { TimeVal tv = s_dict[s_blockname]; tv.duration += end - tv.start; } else { Debug.WriteLine("missing begin for block " + blockname); } s_blockname = s_blockname.Substring(0, s_blockname.Length - blockname.Length - 1); }
public uint ConvertToRTPTimestamp(TimeVal tv) { // Begin by converting from TimeVal units to RTP timestamp units: uint timestampIncrement = (uint)(_timestampFrequency * tv.tv_sec); timestampIncrement += (uint)(_timestampFrequency * (tv.tv_usec / 1000000.0) + 0.5); // note: rounding // Then add this to our 'timestamp base': if (_nextTimestampHasBeenPreset) { _nextTimestampHasBeenPreset = false; // Make the returned timestamp the same as the current "fTimestampBase", // so that timestamps begin with the value that was previously preset: _timestampBase -= timestampIncrement; } uint rtpTimestamp = _timestampBase + timestampIncrement; return(rtpTimestamp); }
public void ReapOldMembers() { TimeVal now = new TimeVal(); RTPTime.GetTimestamp(ref now); List <uint> oldMembers = new List <uint>(); foreach (var stat in _receivers.Values) { var timeReceived = stat.fTimeReceived; if (now.tv_sec - timeReceived.tv_sec > 60) { oldMembers.Add(stat.SSRC); } } foreach (var ssrc in oldMembers) { Console.WriteLine("Removing SSRC {0} from TransmissionStats", ssrc); _receivers.Remove(ssrc); } }
public RTPTransmissionStat(RTPStream stream, uint ssrc) { _stream = stream; SSRC = ssrc; fLastPacketNumReceived = 0; fPacketLossRatio = 0; fTotNumPacketsLost = 0; fJitter = 0; fLastSRTime = 0; fDiffSR_RRTime = 0; fAtLeastTwoRRsHaveBeenReceived = false; fFirstPacket = true; fTotalOctetCount_hi = 0; fTotalOctetCount_lo = 0; fTotalPacketCount_hi = 0; fTotalPacketCount_lo = 0; fTimeCreated = new TimeVal(); RTPTime.GetTimestamp(ref fTimeCreated); fLastOctetCount = _stream.OctetCount(); fLastPacketCount = _stream.PacketCount(); }
public async Task OnNewFrame(bool lastFragmentCompletedNalUnit, bool pictureEndMarker, byte[] buffer, TimeVal presentationTime) { Debug.Assert(buffer.Length + rtpHeaderSize <= RTP_PAYLOAD_MAX_SIZE); BuildPacket(buffer); uint frameSize = (uint)buffer.Length; uint numFrameBytesToUse = (uint)buffer.Length; // Here's where any payload format specific processing gets done: // Set the RTP 'M' (marker) bit if // 1/ The most recently delivered fragment was the end of (or the only fragment of) an NAL unit, and // 2/ This NAL unit was the last NAL unit of an 'access unit' (i.e. video frame). if (lastFragmentCompletedNalUnit && pictureEndMarker) { if (_logger.IsDebugEnabled) { _logger.Debug("Setting the M bit"); } setMarkerBit(); } SetTimestamp(presentationTime); // The packet is ready to be sent now await SendPacket(); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TimeVal obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public static bool TimeValGreater(ref TimeVal tv1, ref TimeVal tv2) { //Debug.Log("VRPNManager: TimeValGreater"); if (tv1.tv_sec > tv2.tv_sec) return true; if ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec > tv2.tv_usec)) return true; return false; }
private static extern int select(int nsock, [In, Out] FdSet read, [In, Out] FdSet write, [In, Out] FdSet error, TimeVal tv);
public void OnNewNalUnit(byte[] frame, bool completedNalUnit, bool pictureEndMarker) { if (_logger.IsDebugEnabled) { _logger.Debug("Buffer {0}, {1}", frame.Length, _inputBuffer.Length); } int frameSize; // out Array.Copy(frame, 0, _inputBuffer, 1, frame.Length); int numValidDataBytes = frame.Length + 1; int numDelivered = 0; byte[] fragment; //Dump(buffer, (uint)buffer.Length, nalCompletedNalUnit, fPictureEndMarker); // We have NAL unit data in the buffer. There are two cases to consider: // 1. There is a new NAL unit in the buffer, and it's small enough to deliver // to the RTP sink (as is). // 2. 1) There is a new NAL unit in the buffer, but it's too large to deliver to // the RTP sink in its entirety. Deliver the first fragment of this data, // as a FU packet, with one extra preceding header byte (for the "FU header"). // then // 2) Deliver the next fragment of this data, // as a FU packet, with two (H.264) extra preceding header bytes // (for the "NAL header" and the "FU header"). if (_maxSize < _maxOutputPacketSize) { // shouldn't happen _logger.Error("MaxSize ({0}) is smaller than expected", _maxSize); throw new Exception(string.Format("MaxSize ({0}) is smaller than expected", _maxSize)); } else { _maxSize = _maxOutputPacketSize; } TimeVal tv = new TimeVal(); RTPTime.GetTimestamp(ref tv); bool lastFragmentCompletedNALUnit = true; // by default if (_currentDataOffset == 1) { // case 1 or 2 if (numValidDataBytes - 1 <= _maxSize) { // case 1 fragment = new byte[numValidDataBytes - 1]; for (int i = 0; i < numValidDataBytes - 1; i++) { fragment[i] = _inputBuffer[1 + i]; } frameSize = numValidDataBytes - 1; _currentDataOffset = numValidDataBytes; numDelivered = fragment.Length; _broadcaster.OnNewFragment(lastFragmentCompletedNALUnit, pictureEndMarker, fragment, tv); } else { // case 2 // We need to send the NAL unit data as FU packets. Deliver the first // packet now. Note that we add "NAL header" and "FU header" bytes to the front // of the packet (overwriting the existing "NAL header"). fragment = new byte[_maxSize]; _inputBuffer[0] = (byte)((_inputBuffer[1] & 0xE0) | 28); // FU indicator _inputBuffer[1] = (byte)(0x80 | (_inputBuffer[1] & 0x1F)); // FU header (with S bit) for (int i = 0; i < _maxSize; i++) { fragment[i] = _inputBuffer[i]; } frameSize = _maxSize; _currentDataOffset += _maxSize - 1; lastFragmentCompletedNALUnit = false; numDelivered += fragment.Length; _broadcaster.OnNewFragment(lastFragmentCompletedNALUnit, pictureEndMarker, fragment, tv); bool last = false; do { // case 3 // We are sending this NAL unit data as FU packets. We've already sent the // first packet (fragment). Now, send the next fragment. Note that we add // "NAL header" and "FU header" bytes to the front. (We reuse these bytes that // we already sent for the first fragment, but clear the S bit, and add the E // bit if this is the last fragment.) int numExtraHeaderBytes; _inputBuffer[_currentDataOffset - 2] = _inputBuffer[0]; // FU indicator _inputBuffer[_currentDataOffset - 1] = (byte)(_inputBuffer[1] & ~0x80); // FU header (no S bit) numExtraHeaderBytes = 2; int numBytesToSend = numExtraHeaderBytes + (numValidDataBytes - _currentDataOffset); if (numBytesToSend > _maxSize) { // We can't send all of the remaining data this time: numBytesToSend = _maxSize; lastFragmentCompletedNALUnit = false; } else { // This is the last fragment: _inputBuffer[_currentDataOffset - 1] |= 0x40; // set the E bit in the FU header lastFragmentCompletedNALUnit = true; last = true; } fragment = new byte[numBytesToSend]; numDelivered += fragment.Length; Array.Copy(_inputBuffer, _currentDataOffset - numExtraHeaderBytes, fragment, 0, numBytesToSend); if (last) { lastFragmentCompletedNALUnit = completedNalUnit; } _broadcaster.OnNewFragment(lastFragmentCompletedNALUnit, pictureEndMarker, fragment, tv); frameSize = numBytesToSend; _currentDataOffset += numBytesToSend - numExtraHeaderBytes; } while (!last); } if (_currentDataOffset >= numValidDataBytes) { // We're done with this data. Reset the pointers for receiving new data: if (_logger.IsDebugEnabled) { _logger.Debug("Done with NalUnit {0} {1}. NumDelivered {2}", sequenceNumber++, frame.Length, numDelivered); } numDelivered = 0; numValidDataBytes = _currentDataOffset = 1; } } }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TimeVal obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }
public static int GetTimestamp(ref TimeVal tp) { if (!_isInitialized) { if (1 == Interlocked.Increment(ref _initializeLock)) { // // La valeur de cette propriété représente le nombre d’intervalles de 100 nanosecondes qui se sont écoulées depuis 12:00:00 minuit, // // le 1er janvier 0001(0 : 00:00 UTC 1er janvier 0001, dans le calendrier grégorien), qui représente DateTime.MinValue. // // Il n’inclut pas le nombre de graduations qui sont attribuables aux secondes intercalaires. _origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); _originTicks = _origin.Ticks; DateTimeGenerator myDTG; DateTime TheNetworkTime; DateTime TheLocalTime; TimeSpan elapsedSpan; myDTG = DateTimeGenerator.Instance; TheLocalTime = DateTime.UtcNow; TheNetworkTime = myDTG.GetNTPTime(); elapsedSpan = TheNetworkTime.Subtract(TheLocalTime); Console.WriteLine("LocalTime {0}", TheLocalTime.ToString()); Console.WriteLine("NetworkTime {0}", TheNetworkTime.ToString()); Console.WriteLine("Origin Time {0} ", _origin.ToString()); _originTicks = _origin.Ticks - elapsedSpan.Ticks; Console.WriteLine(" {0:N0} nanoseconds (10^9s)", elapsedSpan.Ticks * 100); Console.WriteLine(" {0:N0} origin ticks", _originTicks); Console.WriteLine(" {0:N0} delta ticks", elapsedSpan.Ticks); Console.WriteLine(" {0:N2} seconds", elapsedSpan.TotalSeconds); Console.WriteLine(" {0:N2} minutes", elapsedSpan.TotalMinutes); Console.WriteLine(" {0:N0} days, {1} hours, {2} minutes, {3} seconds, millisecond {4}", elapsedSpan.Days, elapsedSpan.Hours, elapsedSpan.Minutes, elapsedSpan.Seconds, elapsedSpan.TotalMilliseconds); Console.WriteLine("Corrected Time {0} ", _origin.ToString()); _originTicks += elapsedSpan.Ticks; Console.WriteLine(" Corrected ticks {0:N0} ticks", _originTicks); // next caller can use ticks for time calculation _isInitialized = true; } else { Interlocked.Decrement(ref _initializeLock); // wait until first caller has initialized static values while (!_isInitialized) { Thread.Sleep(1); } } } //DateTime now = DateTime.Now; DateTime now = DateTime.UtcNow; TimeSpan span = new TimeSpan(now.Ticks - _originTicks); tp.tv_sec = (uint)(span.TotalSeconds); tp.tv_usec = (uint)((span.TotalMilliseconds % 1000) * 1000); return(0); }