henrik.lundin | 5c68d28 | 2017-06-14 13:09:58 | [diff] [blame] | 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> |
Henrik Lundin | 733a371 | 2017-06-20 12:48:50 | [diff] [blame] | 14 | #include <fstream> |
| 15 | #include <ios> |
| 16 | #include <iterator> |
henrik.lundin | 5c68d28 | 2017-06-14 13:09:58 | [diff] [blame] | 17 | #include <limits> |
| 18 | #include <utility> |
| 19 | |
kjellander | ae2f9a7 | 2017-06-30 21:02:00 | [diff] [blame^] | 20 | #include "webrtc/rtc_base/checks.h" |
henrik.lundin | 65f91e0 | 2017-06-14 14:02:17 | [diff] [blame] | 21 | |
henrik.lundin | 5c68d28 | 2017-06-14 13:09:58 | [diff] [blame] | 22 | namespace webrtc { |
| 23 | namespace test { |
| 24 | namespace { |
| 25 | // Helper function for NetEqDelayAnalyzer::CreateGraphs. Returns the |
| 26 | // interpolated value of a function at the point x. Vector x_vec contains the |
| 27 | // sample points, and y_vec contains the function values at these points. The |
| 28 | // return value is a linear interpolation between y_vec values. |
| 29 | double LinearInterpolate(double x, |
| 30 | const std::vector<int64_t>& x_vec, |
| 31 | const std::vector<int64_t>& y_vec) { |
| 32 | // Find first element which is larger than x. |
| 33 | auto it = std::upper_bound(x_vec.begin(), x_vec.end(), x); |
| 34 | if (it == x_vec.end()) { |
| 35 | --it; |
| 36 | } |
| 37 | const size_t upper_ix = it - x_vec.begin(); |
| 38 | |
| 39 | size_t lower_ix; |
| 40 | if (upper_ix == 0 || x_vec[upper_ix] <= x) { |
| 41 | lower_ix = upper_ix; |
| 42 | } else { |
| 43 | lower_ix = upper_ix - 1; |
| 44 | } |
| 45 | double y; |
| 46 | if (lower_ix == upper_ix) { |
| 47 | y = y_vec[lower_ix]; |
| 48 | } else { |
| 49 | RTC_DCHECK_NE(x_vec[lower_ix], x_vec[upper_ix]); |
| 50 | y = (x - x_vec[lower_ix]) * (y_vec[upper_ix] - y_vec[lower_ix]) / |
| 51 | (x_vec[upper_ix] - x_vec[lower_ix]) + |
| 52 | y_vec[lower_ix]; |
| 53 | } |
| 54 | return y; |
| 55 | } |
| 56 | } // namespace |
| 57 | |
| 58 | void NetEqDelayAnalyzer::AfterInsertPacket( |
| 59 | const test::NetEqInput::PacketData& packet, |
| 60 | NetEq* neteq) { |
| 61 | data_.insert( |
| 62 | std::make_pair(packet.header.timestamp, TimingData(packet.time_ms))); |
Henrik Lundin | 733a371 | 2017-06-20 12:48:50 | [diff] [blame] | 63 | ssrcs_.insert(packet.header.ssrc); |
| 64 | payload_types_.insert(packet.header.payloadType); |
henrik.lundin | 5c68d28 | 2017-06-14 13:09:58 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void NetEqDelayAnalyzer::BeforeGetAudio(NetEq* neteq) { |
| 68 | last_sync_buffer_ms_ = neteq->SyncBufferSizeMs(); |
| 69 | } |
| 70 | |
| 71 | void NetEqDelayAnalyzer::AfterGetAudio(int64_t time_now_ms, |
| 72 | const AudioFrame& audio_frame, |
| 73 | bool /*muted*/, |
| 74 | NetEq* neteq) { |
| 75 | get_audio_time_ms_.push_back(time_now_ms); |
| 76 | // Check what timestamps were decoded in the last GetAudio call. |
| 77 | std::vector<uint32_t> dec_ts = neteq->LastDecodedTimestamps(); |
| 78 | // Find those timestamps in data_, insert their decoding time and sync |
| 79 | // delay. |
| 80 | for (uint32_t ts : dec_ts) { |
| 81 | auto it = data_.find(ts); |
| 82 | if (it == data_.end()) { |
| 83 | // This is a packet that was split out from another packet. Skip it. |
| 84 | continue; |
| 85 | } |
| 86 | auto& it_timing = it->second; |
| 87 | RTC_CHECK(!it_timing.decode_get_audio_count) |
| 88 | << "Decode time already written"; |
| 89 | it_timing.decode_get_audio_count = rtc::Optional<int64_t>(get_audio_count_); |
| 90 | RTC_CHECK(!it_timing.sync_delay_ms) << "Decode time already written"; |
| 91 | it_timing.sync_delay_ms = rtc::Optional<int64_t>(last_sync_buffer_ms_); |
| 92 | it_timing.target_delay_ms = rtc::Optional<int>(neteq->TargetDelayMs()); |
| 93 | it_timing.current_delay_ms = |
| 94 | rtc::Optional<int>(neteq->FilteredCurrentDelayMs()); |
| 95 | } |
| 96 | last_sample_rate_hz_ = audio_frame.sample_rate_hz_; |
| 97 | ++get_audio_count_; |
| 98 | } |
| 99 | |
| 100 | void NetEqDelayAnalyzer::CreateGraphs( |
| 101 | std::vector<float>* send_time_s, |
| 102 | std::vector<float>* arrival_delay_ms, |
| 103 | std::vector<float>* corrected_arrival_delay_ms, |
| 104 | std::vector<rtc::Optional<float>>* playout_delay_ms, |
| 105 | std::vector<rtc::Optional<float>>* target_delay_ms) const { |
| 106 | if (get_audio_time_ms_.empty()) { |
| 107 | return; |
| 108 | } |
| 109 | // Create nominal_get_audio_time_ms, a vector starting at |
| 110 | // get_audio_time_ms_[0] and increasing by 10 for each element. |
| 111 | std::vector<int64_t> nominal_get_audio_time_ms(get_audio_time_ms_.size()); |
| 112 | nominal_get_audio_time_ms[0] = get_audio_time_ms_[0]; |
| 113 | std::transform( |
| 114 | nominal_get_audio_time_ms.begin(), nominal_get_audio_time_ms.end() - 1, |
| 115 | nominal_get_audio_time_ms.begin() + 1, [](int64_t& x) { return x + 10; }); |
| 116 | RTC_DCHECK( |
| 117 | std::is_sorted(get_audio_time_ms_.begin(), get_audio_time_ms_.end())); |
| 118 | |
| 119 | std::vector<double> rtp_timestamps_ms; |
| 120 | double offset = std::numeric_limits<double>::max(); |
| 121 | TimestampUnwrapper unwrapper; |
| 122 | // This loop traverses data_ and populates rtp_timestamps_ms as well as |
| 123 | // calculates the base offset. |
| 124 | for (auto& d : data_) { |
henrik.lundin | 65f91e0 | 2017-06-14 14:02:17 | [diff] [blame] | 125 | rtp_timestamps_ms.push_back( |
| 126 | unwrapper.Unwrap(d.first) / |
| 127 | rtc::CheckedDivExact(last_sample_rate_hz_, 1000)); |
henrik.lundin | 5c68d28 | 2017-06-14 13:09:58 | [diff] [blame] | 128 | offset = |
| 129 | std::min(offset, d.second.arrival_time_ms - rtp_timestamps_ms.back()); |
| 130 | } |
| 131 | |
| 132 | // Calculate send times in seconds for each packet. This is the (unwrapped) |
| 133 | // RTP timestamp in ms divided by 1000. |
| 134 | send_time_s->resize(rtp_timestamps_ms.size()); |
| 135 | std::transform(rtp_timestamps_ms.begin(), rtp_timestamps_ms.end(), |
| 136 | send_time_s->begin(), [rtp_timestamps_ms](double x) { |
| 137 | return (x - rtp_timestamps_ms[0]) / 1000.f; |
| 138 | }); |
| 139 | RTC_DCHECK_EQ(send_time_s->size(), rtp_timestamps_ms.size()); |
| 140 | |
| 141 | // This loop traverses the data again and populates the graph vectors. The |
| 142 | // reason to have two loops and traverse twice is that the offset cannot be |
| 143 | // known until the first traversal is done. Meanwhile, the final offset must |
| 144 | // be known already at the start of this second loop. |
| 145 | auto data_it = data_.cbegin(); |
| 146 | for (size_t i = 0; i < send_time_s->size(); ++i, ++data_it) { |
| 147 | RTC_DCHECK(data_it != data_.end()); |
| 148 | const double offset_send_time_ms = rtp_timestamps_ms[i] + offset; |
| 149 | const auto& timing = data_it->second; |
| 150 | corrected_arrival_delay_ms->push_back( |
| 151 | LinearInterpolate(timing.arrival_time_ms, get_audio_time_ms_, |
| 152 | nominal_get_audio_time_ms) - |
| 153 | offset_send_time_ms); |
| 154 | arrival_delay_ms->push_back(timing.arrival_time_ms - offset_send_time_ms); |
| 155 | |
| 156 | if (timing.decode_get_audio_count) { |
| 157 | // This packet was decoded. |
| 158 | RTC_DCHECK(timing.sync_delay_ms); |
| 159 | const float playout_ms = *timing.decode_get_audio_count * 10 + |
| 160 | get_audio_time_ms_[0] + *timing.sync_delay_ms - |
| 161 | offset_send_time_ms; |
| 162 | playout_delay_ms->push_back(rtc::Optional<float>(playout_ms)); |
| 163 | RTC_DCHECK(timing.target_delay_ms); |
| 164 | RTC_DCHECK(timing.current_delay_ms); |
| 165 | const float target = |
| 166 | playout_ms - *timing.current_delay_ms + *timing.target_delay_ms; |
| 167 | target_delay_ms->push_back(rtc::Optional<float>(target)); |
| 168 | } else { |
| 169 | // This packet was never decoded. Mark target and playout delays as empty. |
| 170 | playout_delay_ms->push_back(rtc::Optional<float>()); |
| 171 | target_delay_ms->push_back(rtc::Optional<float>()); |
| 172 | } |
| 173 | } |
| 174 | RTC_DCHECK(data_it == data_.end()); |
| 175 | RTC_DCHECK_EQ(send_time_s->size(), corrected_arrival_delay_ms->size()); |
| 176 | RTC_DCHECK_EQ(send_time_s->size(), playout_delay_ms->size()); |
| 177 | RTC_DCHECK_EQ(send_time_s->size(), target_delay_ms->size()); |
| 178 | } |
| 179 | |
Henrik Lundin | 733a371 | 2017-06-20 12:48:50 | [diff] [blame] | 180 | void NetEqDelayAnalyzer::CreateMatlabScript( |
| 181 | const std::string& script_name) const { |
| 182 | std::vector<float> send_time_s; |
| 183 | std::vector<float> arrival_delay_ms; |
| 184 | std::vector<float> corrected_arrival_delay_ms; |
| 185 | std::vector<rtc::Optional<float>> playout_delay_ms; |
| 186 | std::vector<rtc::Optional<float>> target_delay_ms; |
| 187 | CreateGraphs(&send_time_s, &arrival_delay_ms, &corrected_arrival_delay_ms, |
| 188 | &playout_delay_ms, &target_delay_ms); |
| 189 | |
| 190 | // Create an output file stream to Matlab script file. |
| 191 | std::ofstream output(script_name); |
| 192 | // The iterator is used to batch-output comma-separated values from vectors. |
| 193 | std::ostream_iterator<float> output_iterator(output, ","); |
| 194 | |
| 195 | output << "send_time_s = [ "; |
| 196 | std::copy(send_time_s.begin(), send_time_s.end(), output_iterator); |
| 197 | output << "];" << std::endl; |
| 198 | |
| 199 | output << "arrival_delay_ms = [ "; |
| 200 | std::copy(arrival_delay_ms.begin(), arrival_delay_ms.end(), output_iterator); |
| 201 | output << "];" << std::endl; |
| 202 | |
| 203 | output << "corrected_arrival_delay_ms = [ "; |
| 204 | std::copy(corrected_arrival_delay_ms.begin(), |
| 205 | corrected_arrival_delay_ms.end(), output_iterator); |
| 206 | output << "];" << std::endl; |
| 207 | |
| 208 | output << "playout_delay_ms = [ "; |
| 209 | for (const auto& v : playout_delay_ms) { |
| 210 | if (!v) { |
| 211 | output << "nan, "; |
| 212 | } else { |
| 213 | output << *v << ", "; |
| 214 | } |
| 215 | } |
| 216 | output << "];" << std::endl; |
| 217 | |
| 218 | output << "target_delay_ms = [ "; |
| 219 | for (const auto& v : target_delay_ms) { |
| 220 | if (!v) { |
| 221 | output << "nan, "; |
| 222 | } else { |
| 223 | output << *v << ", "; |
| 224 | } |
| 225 | } |
| 226 | output << "];" << std::endl; |
| 227 | |
| 228 | output << "h=plot(send_time_s, arrival_delay_ms, " |
| 229 | << "send_time_s, target_delay_ms, 'g.', " |
| 230 | << "send_time_s, playout_delay_ms);" << std::endl; |
| 231 | output << "set(h(1),'color',0.75*[1 1 1]);" << std::endl; |
| 232 | output << "set(h(2),'markersize',6);" << std::endl; |
| 233 | output << "set(h(3),'linew',1.5);" << std::endl; |
| 234 | output << "ax1=axis;" << std::endl; |
| 235 | output << "axis tight" << std::endl; |
| 236 | output << "ax2=axis;" << std::endl; |
| 237 | output << "axis([ax2(1:3) ax1(4)])" << std::endl; |
| 238 | output << "xlabel('send time [s]');" << std::endl; |
| 239 | output << "ylabel('relative delay [ms]');" << std::endl; |
| 240 | if (!ssrcs_.empty()) { |
| 241 | auto ssrc_it = ssrcs_.cbegin(); |
| 242 | output << "title('SSRC: 0x" << std::hex << static_cast<int64_t>(*ssrc_it++); |
| 243 | while (ssrc_it != ssrcs_.end()) { |
| 244 | output << ", 0x" << std::hex << static_cast<int64_t>(*ssrc_it++); |
| 245 | } |
| 246 | output << std::dec; |
| 247 | auto pt_it = payload_types_.cbegin(); |
| 248 | output << "; Payload Types: " << *pt_it++; |
| 249 | while (pt_it != payload_types_.end()) { |
| 250 | output << ", " << *pt_it++; |
| 251 | } |
| 252 | output << "');" << std::endl; |
| 253 | } |
| 254 | } |
| 255 | |
henrik.lundin | 5c68d28 | 2017-06-14 13:09:58 | [diff] [blame] | 256 | } // namespace test |
| 257 | } // namespace webrtc |