OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 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_test.h" | |
12 | |
13 #include <iostream> | |
14 | |
15 #include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h" | |
16 | |
17 namespace webrtc { | |
18 namespace test { | |
19 | |
20 void DefaultNetEqTestErrorCallback::OnInsertPacketError( | |
21 int error_code, | |
22 const NetEqInput::PacketData& packet) { | |
23 if (error_code == NetEq::kUnknownRtpPayloadType) { | |
24 std::cerr << "RTP Payload type " | |
25 << static_cast<int>(packet.header.header.payloadType) | |
26 << " is unknown." << std::endl; | |
27 } else { | |
28 std::cerr << "InsertPacket returned error code " << error_code << std::endl; | |
29 std::cerr << "Header data:" << std::endl; | |
30 std::cerr << " PT = " << static_cast<int>(packet.header.header.payloadType) | |
31 << std::endl; | |
32 std::cerr << " SN = " << packet.header.header.sequenceNumber << std::endl; | |
33 std::cerr << " TS = " << packet.header.header.timestamp << std::endl; | |
34 } | |
35 FATAL(); | |
36 } | |
37 | |
38 void DefaultNetEqTestErrorCallback::OnGetAudioError(int error_code) { | |
39 std::cerr << "GetAudio returned error code " << error_code << std::endl; | |
40 FATAL(); | |
41 } | |
42 | |
43 NetEqTest::NetEqTest(const NetEq::Config& config, | |
44 const DecoderMap& codecs, | |
45 const ExtDecoderMap& ext_codecs, | |
46 std::unique_ptr<NetEqInput> input, | |
47 std::unique_ptr<AudioSink> output, | |
48 NetEqTestErrorCallback* error_callback) | |
49 : neteq_(NetEq::Create(config, CreateBuiltinAudioDecoderFactory())), | |
50 input_(std::move(input)), | |
51 output_(std::move(output)), | |
52 error_callback_(error_callback), | |
53 sample_rate_hz_(config.sample_rate_hz) { | |
54 RTC_CHECK(!config.enable_muted_state) | |
55 << "The code does not handle enable_muted_state"; | |
56 RegisterDecoders(codecs); | |
57 RegisterExternalDecoders(ext_codecs); | |
58 } | |
59 | |
60 int64_t NetEqTest::Run() { | |
61 const int64_t start_time_ms = *input_->NextEventTime(); | |
62 int64_t time_now_ms = start_time_ms; | |
63 | |
64 while (!input_->ended()) { | |
65 // Advance time to next event. | |
66 RTC_DCHECK(input_->NextEventTime()); | |
67 time_now_ms = *input_->NextEventTime(); | |
68 // Check if it is time to insert packet. | |
69 while (input_->NextPacketTime() && | |
ivoc
2016/06/14 16:39:57
I think this can be an if instead of a while now,
hlundin-webrtc
2016/06/17 10:30:09
Done.
| |
70 time_now_ms >= *input_->NextPacketTime()) { | |
71 std::unique_ptr<NetEqInput::PacketData> packet_data = input_->GetPacket(); | |
72 RTC_CHECK(packet_data); | |
73 int error = neteq_->InsertPacket( | |
74 packet_data->header, | |
75 rtc::ArrayView<const uint8_t>(packet_data->payload), | |
76 static_cast<uint32_t>(packet_data->time_ms * sample_rate_hz_ / 1000)); | |
77 if (error != NetEq::kOK && error_callback_) { | |
78 error_callback_->OnInsertPacketError(neteq_->LastError(), *packet_data); | |
79 } | |
80 } | |
81 | |
82 // Check if it is time to get output audio. | |
83 while (input_->NextOutputEventTime() && | |
ivoc
2016/06/14 16:39:57
Same here, I think.
hlundin-webrtc
2016/06/17 10:30:09
Done.
| |
84 time_now_ms >= *input_->NextOutputEventTime()) { | |
85 AudioFrame out_frame; | |
86 bool muted; | |
87 int error = neteq_->GetAudio(&out_frame, &muted); | |
88 RTC_CHECK(!muted) << "The code does not handle enable_muted_state"; | |
89 if (error != NetEq::kOK) { | |
90 if (error_callback_) { | |
91 error_callback_->OnGetAudioError(neteq_->LastError()); | |
92 } | |
93 } else { | |
94 sample_rate_hz_ = out_frame.sample_rate_hz_; | |
95 } | |
96 | |
97 if (output_) { | |
98 RTC_CHECK(output_->WriteArray( | |
99 out_frame.data_, | |
100 out_frame.samples_per_channel_ * out_frame.num_channels_)); | |
101 } | |
102 | |
103 input_->AdvanceOutputEvent(); | |
ivoc
2016/06/14 16:39:57
Perhaps there should be a corresponding function f
hlundin-webrtc
2016/06/17 10:30:09
No, I don't think so. The problem is that PopPacke
| |
104 } | |
105 } | |
106 return time_now_ms - start_time_ms; | |
107 } | |
108 | |
109 NetEqNetworkStatistics NetEqTest::SimulationStats() { | |
110 NetEqNetworkStatistics stats; | |
111 RTC_CHECK_EQ(neteq_->NetworkStatistics(&stats), 0); | |
112 return stats; | |
113 } | |
114 | |
115 void NetEqTest::RegisterDecoders(const DecoderMap& codecs) { | |
116 for (const auto& c : codecs) { | |
117 RTC_CHECK_EQ( | |
118 neteq_->RegisterPayloadType(c.second.first, c.second.second, c.first), | |
119 NetEq::kOK) | |
120 << "Cannot register " << c.second.second << " to payload type " | |
121 << c.first; | |
122 } | |
123 } | |
124 | |
125 void NetEqTest::RegisterExternalDecoders(const ExtDecoderMap& codecs) { | |
126 for (const auto& c : codecs) { | |
127 RTC_CHECK_EQ(neteq_->RegisterExternalDecoder( | |
128 c.second.decoder, c.second.codec, c.second.codec_name, | |
129 c.first, c.second.sample_rate_hz), | |
130 NetEq::kOK) | |
131 << "Cannot register " << c.second.codec_name << " to payload type " | |
132 << c.first; | |
133 } | |
134 } | |
135 | |
136 } // namespace test | |
137 } // namespace webrtc | |
OLD | NEW |