summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_coding/neteq/tools/neteq_delay_analyzer.cc
blob: 9e7745777569aee67860769a770d69c39c82d269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"

#include <algorithm>
#include <fstream>
#include <ios>
#include <iterator>
#include <limits>
#include <utility>

#include "absl/strings/string_view.h"
#include "rtc_base/checks.h"
#include "rtc_base/numerics/sequence_number_unwrapper.h"

namespace webrtc {
namespace test {
namespace {
constexpr char kArrivalDelayX[] = "arrival_delay_x";
constexpr char kArrivalDelayY[] = "arrival_delay_y";
constexpr char kTargetDelayX[] = "target_delay_x";
constexpr char kTargetDelayY[] = "target_delay_y";
constexpr char kPlayoutDelayX[] = "playout_delay_x";
constexpr char kPlayoutDelayY[] = "playout_delay_y";

// Helper function for NetEqDelayAnalyzer::CreateGraphs. Returns the
// interpolated value of a function at the point x. Vector x_vec contains the
// sample points, and y_vec contains the function values at these points. The
// return value is a linear interpolation between y_vec values.
double LinearInterpolate(double x,
                         const std::vector<int64_t>& x_vec,
                         const std::vector<int64_t>& y_vec) {
  // Find first element which is larger than x.
  auto it = std::upper_bound(x_vec.begin(), x_vec.end(), x);
  if (it == x_vec.end()) {
    --it;
  }
  const size_t upper_ix = it - x_vec.begin();

  size_t lower_ix;
  if (upper_ix == 0 || x_vec[upper_ix] <= x) {
    lower_ix = upper_ix;
  } else {
    lower_ix = upper_ix - 1;
  }
  double y;
  if (lower_ix == upper_ix) {
    y = y_vec[lower_ix];
  } else {
    RTC_DCHECK_NE(x_vec[lower_ix], x_vec[upper_ix]);
    y = (x - x_vec[lower_ix]) * (y_vec[upper_ix] - y_vec[lower_ix]) /
            (x_vec[upper_ix] - x_vec[lower_ix]) +
        y_vec[lower_ix];
  }
  return y;
}

void PrintDelays(const NetEqDelayAnalyzer::Delays& delays,
                 int64_t ref_time_ms,
                 absl::string_view var_name_x,
                 absl::string_view var_name_y,
                 std::ofstream& output,
                 absl::string_view terminator = "") {
  output << var_name_x << " = [ ";
  for (const std::pair<int64_t, float>& delay : delays) {
    output << (delay.first - ref_time_ms) / 1000.f << ", ";
  }
  output << "]" << terminator << std::endl;

  output << var_name_y << " = [ ";
  for (const std::pair<int64_t, float>& delay : delays) {
    output << delay.second << ", ";
  }
  output << "]" << terminator << std::endl;
}

}  // namespace

void NetEqDelayAnalyzer::AfterInsertPacket(
    const test::NetEqInput::PacketData& packet,
    NetEq* neteq) {
  data_.insert(
      std::make_pair(packet.header.timestamp, TimingData(packet.time_ms)));
  ssrcs_.insert(packet.header.ssrc);
  payload_types_.insert(packet.header.payloadType);
}

void NetEqDelayAnalyzer::BeforeGetAudio(NetEq* neteq) {
  last_sync_buffer_ms_ = neteq->SyncBufferSizeMs();
}

void NetEqDelayAnalyzer::AfterGetAudio(int64_t time_now_ms,
                                       const AudioFrame& audio_frame,
                                       bool /*muted*/,
                                       NetEq* neteq) {
  get_audio_time_ms_.push_back(time_now_ms);
  for (const RtpPacketInfo& info : audio_frame.packet_infos_) {
    auto it = data_.find(info.rtp_timestamp());
    if (it == data_.end()) {
      // This is a packet that was split out from another packet. Skip it.
      continue;
    }
    auto& it_timing = it->second;
    RTC_CHECK(!it_timing.decode_get_audio_count)
        << "Decode time already written";
    it_timing.decode_get_audio_count = get_audio_count_;
    RTC_CHECK(!it_timing.sync_delay_ms) << "Decode time already written";
    it_timing.sync_delay_ms = last_sync_buffer_ms_;
    it_timing.target_delay_ms = neteq->TargetDelayMs();
    it_timing.current_delay_ms = neteq->FilteredCurrentDelayMs();
  }
  last_sample_rate_hz_ = audio_frame.sample_rate_hz_;
  ++get_audio_count_;
}

void NetEqDelayAnalyzer::CreateGraphs(Delays* arrival_delay_ms,
                                      Delays* corrected_arrival_delay_ms,
                                      Delays* playout_delay_ms,
                                      Delays* target_delay_ms) const {
  if (get_audio_time_ms_.empty()) {
    return;
  }
  // Create nominal_get_audio_time_ms, a vector starting at
  // get_audio_time_ms_[0] and increasing by 10 for each element.
  std::vector<int64_t> nominal_get_audio_time_ms(get_audio_time_ms_.size());
  nominal_get_audio_time_ms[0] = get_audio_time_ms_[0];
  std::transform(
      nominal_get_audio_time_ms.begin(), nominal_get_audio_time_ms.end() - 1,
      nominal_get_audio_time_ms.begin() + 1, [](int64_t& x) { return x + 10; });
  RTC_DCHECK(
      std::is_sorted(get_audio_time_ms_.begin(), get_audio_time_ms_.end()));

  std::vector<double> rtp_timestamps_ms;
  double offset = std::numeric_limits<double>::max();
  RtpTimestampUnwrapper unwrapper;
  // This loop traverses data_ and populates rtp_timestamps_ms as well as
  // calculates the base offset.
  for (auto& d : data_) {
    rtp_timestamps_ms.push_back(
        static_cast<double>(unwrapper.Unwrap(d.first)) /
        rtc::CheckedDivExact(last_sample_rate_hz_, 1000));
    offset =
        std::min(offset, d.second.arrival_time_ms - rtp_timestamps_ms.back());
  }

  // This loop traverses the data again and populates the graph vectors. The
  // reason to have two loops and traverse twice is that the offset cannot be
  // known until the first traversal is done. Meanwhile, the final offset must
  // be known already at the start of this second loop.
  size_t i = 0;
  for (const auto& data : data_) {
    const double offset_send_time_ms = rtp_timestamps_ms[i++] + offset;
    const auto& timing = data.second;
    corrected_arrival_delay_ms->push_back(std::make_pair(
        timing.arrival_time_ms,
        LinearInterpolate(timing.arrival_time_ms, get_audio_time_ms_,
                          nominal_get_audio_time_ms) -
            offset_send_time_ms));
    arrival_delay_ms->push_back(std::make_pair(
        timing.arrival_time_ms, timing.arrival_time_ms - offset_send_time_ms));

    if (timing.decode_get_audio_count) {
      // This packet was decoded.
      RTC_DCHECK(timing.sync_delay_ms);
      const int64_t get_audio_time =
          *timing.decode_get_audio_count * 10 + get_audio_time_ms_[0];
      const float playout_ms =
          get_audio_time + *timing.sync_delay_ms - offset_send_time_ms;
      playout_delay_ms->push_back(std::make_pair(get_audio_time, playout_ms));
      RTC_DCHECK(timing.target_delay_ms);
      RTC_DCHECK(timing.current_delay_ms);
      const float target =
          playout_ms - *timing.current_delay_ms + *timing.target_delay_ms;
      target_delay_ms->push_back(std::make_pair(get_audio_time, target));
    }
  }
}

void NetEqDelayAnalyzer::CreateMatlabScript(
    absl::string_view script_name) const {
  Delays arrival_delay_ms;
  Delays corrected_arrival_delay_ms;
  Delays playout_delay_ms;
  Delays target_delay_ms;
  CreateGraphs(&arrival_delay_ms, &corrected_arrival_delay_ms,
               &playout_delay_ms, &target_delay_ms);

  // Maybe better to find the actually smallest timestamp, to surely avoid
  // x-axis starting from negative.
  const int64_t ref_time_ms = arrival_delay_ms.front().first;

  // Create an output file stream to Matlab script file.
  std::ofstream output(std::string{script_name});

  PrintDelays(corrected_arrival_delay_ms, ref_time_ms, kArrivalDelayX,
              kArrivalDelayY, output, ";");

  // PrintDelays(corrected_arrival_delay_x, kCorrectedArrivalDelayX,
  // kCorrectedArrivalDelayY, output);

  PrintDelays(playout_delay_ms, ref_time_ms, kPlayoutDelayX, kPlayoutDelayY,
              output, ";");

  PrintDelays(target_delay_ms, ref_time_ms, kTargetDelayX, kTargetDelayY,
              output, ";");

  output << "h=plot(" << kArrivalDelayX << ", " << kArrivalDelayY << ", "
         << kTargetDelayX << ", " << kTargetDelayY << ", 'g.', "
         << kPlayoutDelayX << ", " << kPlayoutDelayY << ");" << std::endl;
  output << "set(h(1),'color',0.75*[1 1 1]);" << std::endl;
  output << "set(h(2),'markersize',6);" << std::endl;
  output << "set(h(3),'linew',1.5);" << std::endl;
  output << "ax1=axis;" << std::endl;
  output << "axis tight" << std::endl;
  output << "ax2=axis;" << std::endl;
  output << "axis([ax2(1:3) ax1(4)])" << std::endl;
  output << "xlabel('time [s]');" << std::endl;
  output << "ylabel('relative delay [ms]');" << std::endl;
  if (!ssrcs_.empty()) {
    auto ssrc_it = ssrcs_.cbegin();
    output << "title('SSRC: 0x" << std::hex << static_cast<int64_t>(*ssrc_it++);
    while (ssrc_it != ssrcs_.end()) {
      output << ", 0x" << std::hex << static_cast<int64_t>(*ssrc_it++);
    }
    output << std::dec;
    auto pt_it = payload_types_.cbegin();
    output << "; Payload Types: " << *pt_it++;
    while (pt_it != payload_types_.end()) {
      output << ", " << *pt_it++;
    }
    output << "');" << std::endl;
  }
}

void NetEqDelayAnalyzer::CreatePythonScript(
    absl::string_view script_name) const {
  Delays arrival_delay_ms;
  Delays corrected_arrival_delay_ms;
  Delays playout_delay_ms;
  Delays target_delay_ms;
  CreateGraphs(&arrival_delay_ms, &corrected_arrival_delay_ms,
               &playout_delay_ms, &target_delay_ms);

  // Maybe better to find the actually smallest timestamp, to surely avoid
  // x-axis starting from negative.
  const int64_t ref_time_ms = arrival_delay_ms.front().first;

  // Create an output file stream to the python script file.
  std::ofstream output(std::string{script_name});

  // Necessary includes
  output << "import numpy as np" << std::endl;
  output << "import matplotlib.pyplot as plt" << std::endl;

  PrintDelays(corrected_arrival_delay_ms, ref_time_ms, kArrivalDelayX,
              kArrivalDelayY, output);

  // PrintDelays(corrected_arrival_delay_x, kCorrectedArrivalDelayX,
  // kCorrectedArrivalDelayY, output);

  PrintDelays(playout_delay_ms, ref_time_ms, kPlayoutDelayX, kPlayoutDelayY,
              output);

  PrintDelays(target_delay_ms, ref_time_ms, kTargetDelayX, kTargetDelayY,
              output);

  output << "if __name__ == '__main__':" << std::endl;
  output << "  h=plt.plot(" << kArrivalDelayX << ", " << kArrivalDelayY << ", "
         << kTargetDelayX << ", " << kTargetDelayY << ", 'g.', "
         << kPlayoutDelayX << ", " << kPlayoutDelayY << ")" << std::endl;
  output << "  plt.setp(h[0],'color',[.75, .75, .75])" << std::endl;
  output << "  plt.setp(h[1],'markersize',6)" << std::endl;
  output << "  plt.setp(h[2],'linewidth',1.5)" << std::endl;
  output << "  plt.axis('tight')" << std::endl;
  output << "  plt.xlabel('time [s]')" << std::endl;
  output << "  plt.ylabel('relative delay [ms]')" << std::endl;
  if (!ssrcs_.empty()) {
    auto ssrc_it = ssrcs_.cbegin();
    output << "  plt.legend((\"arrival delay\", \"target delay\", \"playout "
              "delay\"))"
           << std::endl;
    output << "  plt.title('SSRC: 0x" << std::hex
           << static_cast<int64_t>(*ssrc_it++);
    while (ssrc_it != ssrcs_.end()) {
      output << ", 0x" << std::hex << static_cast<int64_t>(*ssrc_it++);
    }
    output << std::dec;
    auto pt_it = payload_types_.cbegin();
    output << "; Payload Types: " << *pt_it++;
    while (pt_it != payload_types_.end()) {
      output << ", " << *pt_it++;
    }
    output << "')" << std::endl;
  }
  output << "  plt.show()" << std::endl;
}

}  // namespace test
}  // namespace webrtc