blob: ca475da1abcd11208c96bc17d7b30020dead18f6 [file] [log] [blame]
Henrik Lundin1391bd42017-11-24 09:28:57 +01001/*
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 <stdio.h>
12
13#ifdef WIN32
14#include <winsock2.h>
15#endif
16#ifdef WEBRTC_LINUX
17#include <netinet/in.h>
18#endif
19
20#include <iostream>
21#include <map>
22#include <string>
23
24#include "api/audio_codecs/L16/audio_encoder_L16.h"
25#include "api/audio_codecs/g711/audio_encoder_g711.h"
26#include "api/audio_codecs/g722/audio_encoder_g722.h"
27#include "api/audio_codecs/ilbc/audio_encoder_ilbc.h"
28#include "api/audio_codecs/isac/audio_encoder_isac.h"
29#include "api/audio_codecs/opus/audio_encoder_opus.h"
30#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
31#include "modules/audio_coding/include/audio_coding_module.h"
32#include "modules/audio_coding/neteq/tools/input_audio_file.h"
33#include "rtc_base/flags.h"
34#include "rtc_base/numerics/safe_conversions.h"
35#include "rtc_base/ptr_util.h"
36#include "typedefs.h" // NOLINT(build/include)
37
38namespace webrtc {
39namespace test {
40namespace {
41
42// Define command line flags.
43DEFINE_bool(list_codecs, false, "Enumerate all codecs");
44DEFINE_string(codec, "opus", "Codec to use");
45DEFINE_int(frame_len, 0, "Frame length in ms; 0 indicates codec default value");
46DEFINE_int(bitrate, 0, "Bitrate in kbps; 0 indicates codec default value");
47DEFINE_int(payload_type,
48 -1,
49 "RTP payload type; -1 indicates codec default value");
50DEFINE_int(cng_payload_type,
51 -1,
52 "RTP payload type for CNG; -1 indicates default value");
53DEFINE_int(ssrc, 0, "SSRC to write to the RTP header");
54DEFINE_bool(dtx, false, "Use DTX/CNG");
55DEFINE_int(sample_rate, 48000, "Sample rate of the input file");
56DEFINE_bool(help, false, "Print this message");
57
58// Add new codecs here, and to the map below.
59enum class CodecType {
60 kOpus,
61 kPcmU,
62 kPcmA,
63 kG722,
64 kPcm16b8,
65 kPcm16b16,
66 kPcm16b32,
67 kPcm16b48,
68 kIlbc,
69 kIsac
70};
71
72struct CodecTypeAndInfo {
73 CodecType type;
74 int default_payload_type;
75 bool internal_dtx;
76};
77
78// List all supported codecs here. This map defines the command-line parameter
79// value (the key string) for selecting each codec, together with information
80// whether it is using internal or external DTX/CNG.
81const std::map<std::string, CodecTypeAndInfo>& CodecList() {
82 static const auto* const codec_list =
83 new std::map<std::string, CodecTypeAndInfo>{
84 {"opus", {CodecType::kOpus, 111, true}},
85 {"pcmu", {CodecType::kPcmU, 0, false}},
86 {"pcma", {CodecType::kPcmA, 8, false}},
87 {"g722", {CodecType::kG722, 9, false}},
88 {"pcm16b_8", {CodecType::kPcm16b8, 93, false}},
89 {"pcm16b_16", {CodecType::kPcm16b16, 94, false}},
90 {"pcm16b_32", {CodecType::kPcm16b32, 95, false}},
91 {"pcm16b_48", {CodecType::kPcm16b48, 96, false}},
92 {"ilbc", {CodecType::kIlbc, 102, false}},
93 {"isac", {CodecType::kIsac, 103, false}}};
94 return *codec_list;
95}
96
97// This class will receive callbacks from ACM when a packet is ready, and write
98// it to the output file.
99class Packetizer : public AudioPacketizationCallback {
100 public:
101 Packetizer(FILE* out_file, uint32_t ssrc, int timestamp_rate_hz)
102 : out_file_(out_file),
103 ssrc_(ssrc),
104 timestamp_rate_hz_(timestamp_rate_hz) {}
105
106 int32_t SendData(FrameType frame_type,
107 uint8_t payload_type,
108 uint32_t timestamp,
109 const uint8_t* payload_data,
110 size_t payload_len_bytes,
111 const RTPFragmentationHeader* fragmentation) override {
112 RTC_CHECK(!fragmentation);
113 RTC_DCHECK_GT(payload_len_bytes, 0);
114
115 constexpr size_t kRtpHeaderLength = 12;
116 constexpr size_t kRtpDumpHeaderLength = 8;
117 const uint16_t length = htons(rtc::checked_cast<uint16_t>(
118 kRtpHeaderLength + kRtpDumpHeaderLength + payload_len_bytes));
119 const uint16_t plen = htons(
120 rtc::checked_cast<uint16_t>(kRtpHeaderLength + payload_len_bytes));
121 const uint32_t offset = htonl(timestamp / (timestamp_rate_hz_ / 1000));
122 RTC_CHECK_EQ(fwrite(&length, sizeof(uint16_t), 1, out_file_), 1);
123 RTC_CHECK_EQ(fwrite(&plen, sizeof(uint16_t), 1, out_file_), 1);
124 RTC_CHECK_EQ(fwrite(&offset, sizeof(uint32_t), 1, out_file_), 1);
125
126 const uint8_t rtp_header[] = {0x80,
127 static_cast<uint8_t>(payload_type & 0x7F),
128 static_cast<uint8_t>(sequence_number_ >> 8),
129 static_cast<uint8_t>(sequence_number_),
130 static_cast<uint8_t>(timestamp >> 24),
131 static_cast<uint8_t>(timestamp >> 16),
132 static_cast<uint8_t>(timestamp >> 8),
133 static_cast<uint8_t>(timestamp),
134 static_cast<uint8_t>(ssrc_ >> 24),
135 static_cast<uint8_t>(ssrc_ >> 16),
136 static_cast<uint8_t>(ssrc_ >> 8),
137 static_cast<uint8_t>(ssrc_)};
138 static_assert(sizeof(rtp_header) == kRtpHeaderLength, "");
139 RTC_CHECK_EQ(
140 fwrite(rtp_header, sizeof(uint8_t), kRtpHeaderLength, out_file_),
141 kRtpHeaderLength);
142 ++sequence_number_; // Intended to wrap on overflow.
143
144 RTC_CHECK_EQ(
145 fwrite(payload_data, sizeof(uint8_t), payload_len_bytes, out_file_),
146 payload_len_bytes);
147
148 return 0;
149 }
150
151 private:
152 FILE* const out_file_;
153 const uint32_t ssrc_;
154 const int timestamp_rate_hz_;
155 uint16_t sequence_number_ = 0;
156};
157
158void SetFrameLenIfFlagIsPositive(int* config_frame_len) {
159 if (FLAG_frame_len > 0) {
160 *config_frame_len = FLAG_frame_len;
161 }
162}
163
164AudioEncoderL16::Config Pcm16bConfig(CodecType codec_type) {
165 AudioEncoderL16::Config config;
166 SetFrameLenIfFlagIsPositive(&config.frame_size_ms);
167 switch (codec_type) {
168 case CodecType::kPcm16b8:
169 config.sample_rate_hz = 8000;
170 return config;
171 case CodecType::kPcm16b16:
172 config.sample_rate_hz = 16000;
173 return config;
174 case CodecType::kPcm16b32:
175 config.sample_rate_hz = 32000;
176 return config;
177 case CodecType::kPcm16b48:
178 config.sample_rate_hz = 48000;
179 return config;
180 default:
181 RTC_NOTREACHED();
182 return config;
183 }
184}
185
186std::unique_ptr<AudioEncoder> CreateEncoder(CodecType codec_type,
187 int payload_type) {
188 switch (codec_type) {
189 case CodecType::kOpus: {
190 AudioEncoderOpusConfig config;
191 if (FLAG_bitrate > 0) {
192 config.bitrate_bps = FLAG_bitrate;
193 }
194 config.dtx_enabled = FLAG_dtx;
195 SetFrameLenIfFlagIsPositive(&config.frame_size_ms);
196 RTC_CHECK(config.IsOk());
197 return AudioEncoderOpus::MakeAudioEncoder(config, payload_type);
198 }
199
200 case CodecType::kPcmU:
201 case CodecType::kPcmA: {
202 AudioEncoderG711::Config config;
203 SetFrameLenIfFlagIsPositive(&config.frame_size_ms);
204 config.type = codec_type == CodecType::kPcmU
205 ? AudioEncoderG711::Config::Type::kPcmU
206 : AudioEncoderG711::Config::Type::kPcmA;
207 RTC_CHECK(config.IsOk());
208 return AudioEncoderG711::MakeAudioEncoder(config, payload_type);
209 }
210
211 case CodecType::kG722: {
212 AudioEncoderG722Config config;
213 SetFrameLenIfFlagIsPositive(&config.frame_size_ms);
214 RTC_CHECK(config.IsOk());
215 return AudioEncoderG722::MakeAudioEncoder(config, payload_type);
216 }
217
218 case CodecType::kPcm16b8:
219 case CodecType::kPcm16b16:
220 case CodecType::kPcm16b32:
221 case CodecType::kPcm16b48: {
222 return AudioEncoderL16::MakeAudioEncoder(Pcm16bConfig(codec_type),
223 payload_type);
224 }
225
226 case CodecType::kIlbc: {
227 AudioEncoderIlbcConfig config;
228 SetFrameLenIfFlagIsPositive(&config.frame_size_ms);
229 RTC_CHECK(config.IsOk());
230 return AudioEncoderIlbc::MakeAudioEncoder(config, payload_type);
231 }
232
233 case CodecType::kIsac: {
234 AudioEncoderIsac::Config config;
235 SetFrameLenIfFlagIsPositive(&config.frame_size_ms);
236 RTC_CHECK(config.IsOk());
237 return AudioEncoderIsac::MakeAudioEncoder(config, payload_type);
238 }
239 }
240 RTC_NOTREACHED();
241 return nullptr;
242}
243
244AudioEncoderCng::Config GetCngConfig(int sample_rate_hz) {
245 AudioEncoderCng::Config cng_config;
246 const auto default_payload_type = [&] {
247 switch (sample_rate_hz) {
248 case 8000: return 13;
249 case 16000: return 98;
250 case 32000: return 99;
251 case 48000: return 100;
252 default: RTC_NOTREACHED();
253 }
254 return 0;
255 };
256 cng_config.payload_type = FLAG_cng_payload_type != -1
257 ? FLAG_cng_payload_type
258 : default_payload_type();
259 return cng_config;
260}
261
262int RunRtpEncode(int argc, char* argv[]) {
263 const std::string program_name = argv[0];
264 const std::string usage =
265 "Tool for generating an RTP dump file from audio input.\n"
266 "Run " +
267 program_name +
268 " --help for usage.\n"
269 "Example usage:\n" +
270 program_name + " input.pcm output.rtp --codec=[codec] " +
271 "--frame_len=[frame_len] --bitrate=[bitrate]\n\n";
272 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true) || FLAG_help ||
273 (!FLAG_list_codecs && argc != 3)) {
274 printf("%s", usage.c_str());
275 if (FLAG_help) {
276 rtc::FlagList::Print(nullptr, false);
277 return 0;
278 }
279 return 1;
280 }
281
282 if (FLAG_list_codecs) {
283 printf("The following arguments are valid --codec parameters:\n");
284 for (const auto& c : CodecList()) {
285 printf(" %s\n", c.first.c_str());
286 }
287 return 0;
288 }
289
290 const auto codec_it = CodecList().find(FLAG_codec);
291 if (codec_it == CodecList().end()) {
292 printf("%s is not a valid codec name.\n", FLAG_codec);
293 printf("Use argument --list_codecs to see all valid codec names.\n");
294 return 1;
295 }
296
297 // Create the codec.
298 const int payload_type = FLAG_payload_type == -1
299 ? codec_it->second.default_payload_type
300 : FLAG_payload_type;
301 std::unique_ptr<AudioEncoder> codec =
302 CreateEncoder(codec_it->second.type, payload_type);
303
304 // Create an external VAD/CNG encoder if needed.
305 if (FLAG_dtx && codec_it->second.internal_dtx) {
306 AudioEncoderCng::Config cng_config = GetCngConfig(codec->SampleRateHz());
307 RTC_DCHECK(codec);
308 cng_config.speech_encoder = std::move(codec);
309 codec = rtc::MakeUnique<AudioEncoderCng>(std::move(cng_config));
310 }
311 RTC_DCHECK(codec);
312
313 // Set up ACM.
314 const int timestamp_rate_hz = codec->RtpTimestampRateHz();
315 AudioCodingModule::Config config;
316 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(config));
317 acm->SetEncoder(std::move(codec));
318
319 // Open files.
320 printf("Input file: %s\n", argv[1]);
321 InputAudioFile input_file(argv[1], false); // Open input in non-looping mode.
322 FILE* out_file = fopen(argv[2], "wb");
323 RTC_CHECK(out_file) << "Could not open file " << argv[2] << " for writing";
324 printf("Output file: %s\n", argv[2]);
325 fprintf(out_file, "#!rtpplay1.0 \n"); //,
326 // Write 3 32-bit values followed by 2 16-bit values, all set to 0. This means
327 // a total of 16 bytes.
328 const uint8_t file_header[16] = {0};
329 RTC_CHECK_EQ(fwrite(file_header, sizeof(file_header), 1, out_file), 1);
330
331 // Create and register the packetizer, which will write the packets to file.
332 Packetizer packetizer(out_file, FLAG_ssrc, timestamp_rate_hz);
333 RTC_DCHECK_EQ(acm->RegisterTransportCallback(&packetizer), 0);
334
335 AudioFrame audio_frame;
336 audio_frame.samples_per_channel_ = FLAG_sample_rate / 100; // 10 ms
337 audio_frame.sample_rate_hz_ = FLAG_sample_rate;
338 audio_frame.num_channels_ = 1;
339
340 while (input_file.Read(audio_frame.samples_per_channel_,
341 audio_frame.mutable_data())) {
342 RTC_CHECK_GE(acm->Add10MsData(audio_frame), 0);
343 audio_frame.timestamp_ += audio_frame.samples_per_channel_;
344 }
345
346 return 0;
347}
348
349} // namespace
350} // namespace test
351} // namespace webrtc
352
353int main(int argc, char* argv[]) {
354 return webrtc::test::RunRtpEncode(argc, argv);
355}