OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/modules/audio_coding/neteq/tools/neteq_delay_analyzer.h" |
| 12 |
| 13 #include <algorithm> |
| 14 #include <limits> |
| 15 #include <utility> |
| 16 |
| 17 namespace webrtc { |
| 18 namespace test { |
| 19 namespace { |
| 20 // Helper function for NetEqDelayAnalyzer::CreateGraphs. Returns the |
| 21 // interpolated value of a function at the point x. Vector x_vec contains the |
| 22 // sample points, and y_vec contains the function values at these points. The |
| 23 // return value is a linear interpolation between y_vec values. |
| 24 double LinearInterpolate(double x, |
| 25 const std::vector<int64_t>& x_vec, |
| 26 const std::vector<int64_t>& y_vec) { |
| 27 // Find first element which is larger than x. |
| 28 auto it = std::upper_bound(x_vec.begin(), x_vec.end(), x); |
| 29 if (it == x_vec.end()) { |
| 30 --it; |
| 31 } |
| 32 const size_t upper_ix = it - x_vec.begin(); |
| 33 |
| 34 size_t lower_ix; |
| 35 if (upper_ix == 0 || x_vec[upper_ix] <= x) { |
| 36 lower_ix = upper_ix; |
| 37 } else { |
| 38 lower_ix = upper_ix - 1; |
| 39 } |
| 40 double y; |
| 41 if (lower_ix == upper_ix) { |
| 42 y = y_vec[lower_ix]; |
| 43 } else { |
| 44 RTC_DCHECK_NE(x_vec[lower_ix], x_vec[upper_ix]); |
| 45 y = (x - x_vec[lower_ix]) * (y_vec[upper_ix] - y_vec[lower_ix]) / |
| 46 (x_vec[upper_ix] - x_vec[lower_ix]) + |
| 47 y_vec[lower_ix]; |
| 48 } |
| 49 return y; |
| 50 } |
| 51 } // namespace |
| 52 |
| 53 void NetEqDelayAnalyzer::AfterInsertPacket( |
| 54 const test::NetEqInput::PacketData& packet, |
| 55 NetEq* neteq) { |
| 56 data_.insert( |
| 57 std::make_pair(packet.header.timestamp, TimingData(packet.time_ms))); |
| 58 } |
| 59 |
| 60 void NetEqDelayAnalyzer::BeforeGetAudio(NetEq* neteq) { |
| 61 last_sync_buffer_ms_ = neteq->SyncBufferSizeMs(); |
| 62 } |
| 63 |
| 64 void NetEqDelayAnalyzer::AfterGetAudio(int64_t time_now_ms, |
| 65 const AudioFrame& audio_frame, |
| 66 bool /*muted*/, |
| 67 NetEq* neteq) { |
| 68 get_audio_time_ms_.push_back(time_now_ms); |
| 69 // Check what timestamps were decoded in the last GetAudio call. |
| 70 std::vector<uint32_t> dec_ts = neteq->LastDecodedTimestamps(); |
| 71 // Find those timestamps in data_, insert their decoding time and sync |
| 72 // delay. |
| 73 for (uint32_t ts : dec_ts) { |
| 74 auto it = data_.find(ts); |
| 75 if (it == data_.end()) { |
| 76 // This is a packet that was split out from another packet. Skip it. |
| 77 continue; |
| 78 } |
| 79 auto& it_timing = it->second; |
| 80 RTC_CHECK(!it_timing.decode_get_audio_count) |
| 81 << "Decode time already written"; |
| 82 it_timing.decode_get_audio_count = rtc::Optional<int64_t>(get_audio_count_); |
| 83 RTC_CHECK(!it_timing.sync_delay_ms) << "Decode time already written"; |
| 84 it_timing.sync_delay_ms = rtc::Optional<int64_t>(last_sync_buffer_ms_); |
| 85 it_timing.target_delay_ms = rtc::Optional<int>(neteq->TargetDelayMs()); |
| 86 it_timing.current_delay_ms = |
| 87 rtc::Optional<int>(neteq->FilteredCurrentDelayMs()); |
| 88 } |
| 89 last_sample_rate_hz_ = audio_frame.sample_rate_hz_; |
| 90 ++get_audio_count_; |
| 91 } |
| 92 |
| 93 void NetEqDelayAnalyzer::CreateGraphs( |
| 94 std::vector<float>* send_time_s, |
| 95 std::vector<float>* arrival_delay_ms, |
| 96 std::vector<float>* corrected_arrival_delay_ms, |
| 97 std::vector<rtc::Optional<float>>* playout_delay_ms, |
| 98 std::vector<rtc::Optional<float>>* target_delay_ms) const { |
| 99 if (get_audio_time_ms_.empty()) { |
| 100 return; |
| 101 } |
| 102 // Create nominal_get_audio_time_ms, a vector starting at |
| 103 // get_audio_time_ms_[0] and increasing by 10 for each element. |
| 104 std::vector<int64_t> nominal_get_audio_time_ms(get_audio_time_ms_.size()); |
| 105 nominal_get_audio_time_ms[0] = get_audio_time_ms_[0]; |
| 106 std::transform( |
| 107 nominal_get_audio_time_ms.begin(), nominal_get_audio_time_ms.end() - 1, |
| 108 nominal_get_audio_time_ms.begin() + 1, [](int64_t& x) { return x + 10; }); |
| 109 RTC_DCHECK( |
| 110 std::is_sorted(get_audio_time_ms_.begin(), get_audio_time_ms_.end())); |
| 111 |
| 112 std::vector<double> rtp_timestamps_ms; |
| 113 double offset = std::numeric_limits<double>::max(); |
| 114 TimestampUnwrapper unwrapper; |
| 115 // This loop traverses data_ and populates rtp_timestamps_ms as well as |
| 116 // calculates the base offset. |
| 117 for (auto& d : data_) { |
| 118 rtp_timestamps_ms.push_back(unwrapper.Unwrap(d.first) / |
| 119 (last_sample_rate_hz_ / 1000.f)); |
| 120 offset = |
| 121 std::min(offset, d.second.arrival_time_ms - rtp_timestamps_ms.back()); |
| 122 } |
| 123 |
| 124 // Calculate send times in seconds for each packet. This is the (unwrapped) |
| 125 // RTP timestamp in ms divided by 1000. |
| 126 send_time_s->resize(rtp_timestamps_ms.size()); |
| 127 std::transform(rtp_timestamps_ms.begin(), rtp_timestamps_ms.end(), |
| 128 send_time_s->begin(), [rtp_timestamps_ms](double x) { |
| 129 return (x - rtp_timestamps_ms[0]) / 1000.f; |
| 130 }); |
| 131 RTC_DCHECK_EQ(send_time_s->size(), rtp_timestamps_ms.size()); |
| 132 |
| 133 // This loop traverses the data again and populates the graph vectors. The |
| 134 // reason to have two loops and traverse twice is that the offset cannot be |
| 135 // known until the first traversal is done. Meanwhile, the final offset must |
| 136 // be known already at the start of this second loop. |
| 137 auto data_it = data_.cbegin(); |
| 138 for (size_t i = 0; i < send_time_s->size(); ++i, ++data_it) { |
| 139 RTC_DCHECK(data_it != data_.end()); |
| 140 const double offset_send_time_ms = rtp_timestamps_ms[i] + offset; |
| 141 const auto& timing = data_it->second; |
| 142 corrected_arrival_delay_ms->push_back( |
| 143 LinearInterpolate(timing.arrival_time_ms, get_audio_time_ms_, |
| 144 nominal_get_audio_time_ms) - |
| 145 offset_send_time_ms); |
| 146 arrival_delay_ms->push_back(timing.arrival_time_ms - offset_send_time_ms); |
| 147 |
| 148 if (timing.decode_get_audio_count) { |
| 149 // This packet was decoded. |
| 150 RTC_DCHECK(timing.sync_delay_ms); |
| 151 const float playout_ms = *timing.decode_get_audio_count * 10 + |
| 152 get_audio_time_ms_[0] + *timing.sync_delay_ms - |
| 153 offset_send_time_ms; |
| 154 playout_delay_ms->push_back(rtc::Optional<float>(playout_ms)); |
| 155 RTC_DCHECK(timing.target_delay_ms); |
| 156 RTC_DCHECK(timing.current_delay_ms); |
| 157 const float target = |
| 158 playout_ms - *timing.current_delay_ms + *timing.target_delay_ms; |
| 159 target_delay_ms->push_back(rtc::Optional<float>(target)); |
| 160 } else { |
| 161 // This packet was never decoded. Mark target and playout delays as empty. |
| 162 playout_delay_ms->push_back(rtc::Optional<float>()); |
| 163 target_delay_ms->push_back(rtc::Optional<float>()); |
| 164 } |
| 165 } |
| 166 RTC_DCHECK(data_it == data_.end()); |
| 167 RTC_DCHECK_EQ(send_time_s->size(), corrected_arrival_delay_ms->size()); |
| 168 RTC_DCHECK_EQ(send_time_s->size(), playout_delay_ms->size()); |
| 169 RTC_DCHECK_EQ(send_time_s->size(), target_delay_ms->size()); |
| 170 } |
| 171 |
| 172 } // namespace test |
| 173 } // namespace webrtc |
OLD | NEW |