diff --git a/src/engine/helper/HL_Sync.cpp b/src/engine/helper/HL_Sync.cpp index 94e62ec8..6c62326b 100644 --- a/src/engine/helper/HL_Sync.cpp +++ b/src/engine/helper/HL_Sync.cpp @@ -72,20 +72,20 @@ static uint64_t TimestampStartPoint = duration_cast(steady_clock:: static time_t TimestampBase = time(nullptr); // Returns number of milliseconds starting from 01 Jan 1970 GMT -uint64_t chronox::getTimestamp() +std::chrono::milliseconds chronox::getTimestamp() { time_point t = steady_clock::now(); uint64_t ms = duration_cast< milliseconds >(t.time_since_epoch()).count(); - return ms - TimestampStartPoint + TimestampBase * 1000; + return std::chrono::milliseconds(ms - TimestampStartPoint + TimestampBase * 1000); } -uint64_t chronox::getUptime() +std::chrono::milliseconds chronox::getUptime() { time_point t = steady_clock::now(); uint64_t ms = duration_cast< milliseconds >(t.time_since_epoch()).count(); - return ms - TimestampStartPoint; + return std::chrono::milliseconds(ms - TimestampStartPoint); } uint32_t chronox::getDelta(uint32_t later, uint32_t earlier) @@ -132,7 +132,7 @@ chronox::ExecutionTime::ExecutionTime() mStart = chronox::getTimestamp(); } -uint64_t chronox::ExecutionTime::getSpentTime() const +std::chrono::milliseconds chronox::ExecutionTime::getSpentTime() const { return chronox::getTimestamp() - mStart; } diff --git a/src/engine/helper/HL_Sync.h b/src/engine/helper/HL_Sync.h index 8cbb377e..88b3c1d5 100644 --- a/src/engine/helper/HL_Sync.h +++ b/src/engine/helper/HL_Sync.h @@ -54,10 +54,10 @@ class chronox { public: // Returns current timestamp in milliseconds - static uint64_t getTimestamp(); + static std::chrono::milliseconds getTimestamp(); // Returns uptime (of calling process) in milliseconds - static uint64_t getUptime(); + static std::chrono::milliseconds getUptime(); // Finds time delta between 'later' and 'earlier' time points. // Handles cases when clock is wrapped. @@ -75,9 +75,9 @@ public: { public: ExecutionTime(); - uint64_t getSpentTime() const; + std::chrono::milliseconds getSpentTime() const; protected: - uint64_t mStart; + std::chrono::milliseconds mStart; }; }; diff --git a/src/engine/media/MT_AudioReceiver.cpp b/src/engine/media/MT_AudioReceiver.cpp index 245038b7..eb19aa8b 100644 --- a/src/engine/media/MT_AudioReceiver.cpp +++ b/src/engine/media/MT_AudioReceiver.cpp @@ -416,8 +416,10 @@ size_t decode_packet(Codec& codec, RTPPacket& p, void* output_buffer, size_t out return result; } -bool AudioReceiver::add(const std::shared_ptr& p, Codec** detectedCodec) +Codec* AudioReceiver::add(const std::shared_ptr& p) { + Codec* codec = nullptr; + // Estimate time length int time_length = 0, samplerate = 8000, @@ -432,14 +434,13 @@ bool AudioReceiver::add(const std::shared_ptr& p, Codec** de // Check if we deal with telephone-event if (p->GetPayloadType() == mCodecSettings.mTelephoneEvent) { - *detectedCodec = nullptr; + codec = nullptr; mDtmfBuffer.add(p, 10ms, 8000); } else { // Look for codec // Check if codec can be handled - Codec* codec = nullptr; auto codecIter = mCodecMap.find(ptype); if (codecIter != mCodecMap.end()) { @@ -451,9 +452,6 @@ bool AudioReceiver::add(const std::shared_ptr& p, Codec** de codec = codecIter->second.get(); // Return pointer to codec if needed.get() - if (detectedCodec) - *detectedCodec = codec; - if (mStat.mCodecName.empty() && codec) mStat.mCodecName = codec->name(); @@ -475,26 +473,25 @@ bool AudioReceiver::add(const std::shared_ptr& p, Codec** de mStat.mJitter = static_cast(mJitterStats.get()); if (!codec) - return false; // There is no sense to add this packet into jitter buffer - we can't decode this + return nullptr; // Check if packet is CNG if (payloadLength >= 1 && payloadLength <= 6 && (ptype == 0 || ptype == 8)) time_length = mLastPacketTimeLength ? mLastPacketTimeLength : 20; else - // Check if packet is too short from time length side + // Check if packet is too short from time length side - smth strange with found codec... if (time_length < 2) { // It will cause statistics to report about bad RTP packet // I have to replay last packet payload here to avoid report about lost packet mBuffer.add(p, std::chrono::milliseconds(time_length), samplerate); - return false; + return nullptr; } // Queue packet to buffer - auto packet = mBuffer.add(p, std::chrono::milliseconds(time_length), samplerate).get(); - return packet; + mBuffer.add(p, std::chrono::milliseconds(time_length), samplerate).get(); } - return {}; + return codec; } void AudioReceiver::processDecoded(Audio::DataWindow& output, DecodeOptions options) @@ -930,43 +927,66 @@ int AudioReceiver::getSize() const return result; } -int AudioReceiver::timelengthFor(jrtplib::RTPPacket& p) +AudioReceiver::MediaInfo AudioReceiver::infoFor(jrtplib::RTPPacket& p) { CodecMap::iterator codecIter = mCodecMap.find(p.GetPayloadType()); if (codecIter == mCodecMap.end()) - return 0; - + return {}; PCodec codec = codecIter->second; - if (codec) - { - int frame_count = 0; - if (codec->rtpLength() != 0) - { - frame_count = static_cast(p.GetPayloadLength() / codec->rtpLength()); - if (p.GetPayloadType() == 9/*G729A silence*/ && p.GetPayloadLength() % codec->rtpLength()) - frame_count++; - } - else - frame_count = 1; + if (!codec) + return {}; - return frame_count * codec->frameTime(); + int frame_count = 0; + if (codec->rtpLength() != 0) + { + frame_count = static_cast(p.GetPayloadLength() / codec->rtpLength()); + if (p.GetPayloadType() == 9/*G729A silence*/ && p.GetPayloadLength() % codec->rtpLength()) + frame_count++; } else - return 0; + frame_count = 1; + + + return {std::chrono::milliseconds(frame_count * codec->frameTime()), codec->samplerate()}; } -int AudioReceiver::samplerateFor(jrtplib::RTPPacket& p) -{ - CodecMap::iterator codecIter = mCodecMap.find(p.GetPayloadType()); - if (codecIter != mCodecMap.end()) - { - PCodec codec = codecIter->second; - if (codec) - return codec->samplerate(); - } +// int AudioReceiver::timelengthFor(jrtplib::RTPPacket& p) +// { +// CodecMap::iterator codecIter = mCodecMap.find(p.GetPayloadType()); +// if (codecIter == mCodecMap.end()) +// return 0; - return 8000; -} +// PCodec codec = codecIter->second; +// if (codec) +// { +// int frame_count = 0; +// if (codec->rtpLength() != 0) +// { +// frame_count = static_cast(p.GetPayloadLength() / codec->rtpLength()); +// if (p.GetPayloadType() == 9/*G729A silence*/ && p.GetPayloadLength() % codec->rtpLength()) +// frame_count++; +// } +// else +// frame_count = 1; + +// return frame_count * codec->frameTime(); +// } +// else +// return 0; +// } + +// int AudioReceiver::samplerateFor(jrtplib::RTPPacket& p) +// { +// CodecMap::iterator codecIter = mCodecMap.find(p.GetPayloadType()); +// if (codecIter != mCodecMap.end()) +// { +// PCodec codec = codecIter->second; +// if (codec) +// return codec->samplerate(); +// } + +// return 8000; +// } // ----------------------- DtmfReceiver ------------------- DtmfReceiver::DtmfReceiver(Statistics& stat) diff --git a/src/engine/media/MT_AudioReceiver.h b/src/engine/media/MT_AudioReceiver.h index 0c567800..14b33686 100644 --- a/src/engine/media/MT_AudioReceiver.h +++ b/src/engine/media/MT_AudioReceiver.h @@ -165,7 +165,7 @@ public: // Returns false when packet is rejected as illegal. codec parameter will show codec which will be used for decoding. // Lifetime of pointer to codec is limited by lifetime of AudioReceiver (it is container). - bool add(const std::shared_ptr& p, Codec** codec = nullptr); + Codec* add(const std::shared_ptr& p); struct DecodeOptions { @@ -198,11 +198,18 @@ public: // Returns size of AudioReceiver's instance in bytes (including size of all data + codecs + etc.) int getSize() const; - // Returns timelength for given packet - int timelengthFor(jrtplib::RTPPacket& p); + struct MediaInfo + { + std::chrono::milliseconds mTimeLength = 0ms; + int mSamplerate = 0; + }; + MediaInfo infoFor(jrtplib::RTPPacket& p); - // Return samplerate for given packet - int samplerateFor(jrtplib::RTPPacket& p); + // // Returns timelength for given packet + // int timelengthFor(jrtplib::RTPPacket& p); + + // // Return samplerate for given packet + // int samplerateFor(jrtplib::RTPPacket& p); protected: RtpBuffer mBuffer; // Jitter buffer itself diff --git a/src/engine/media/MT_Statistics.h b/src/engine/media/MT_Statistics.h index 349cf03f..727ac117 100644 --- a/src/engine/media/MT_Statistics.h +++ b/src/engine/media/MT_Statistics.h @@ -84,8 +84,8 @@ public: mDecodeRequested, // Average amount of requested audio frames to play mPacketInterval; // Average interval between packet adding to jitter buffer - std::map mLoss; // Every item is number of loss of corresping length - size_t mAudioTime = 0; // Decoded/found time in milliseconds + std::map mLoss; // Every item is number of loss of corresping length + std::chrono::milliseconds mAudioTime = 0ms; // Decoded/found time in milliseconds size_t mDecodedSize = 0; // Number of decoded bytes uint16_t mSsrc = 0; // Last known SSRC ID in a RTP stream ice::NetworkAddress mRemotePeer; // Last known remote RTP address