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