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