blob: ce0719980ff236eb72386b9cb747739c37413a1d [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);
Henrik Lundin32f64d22017-11-28 13:05:35 +0100113 if (payload_len_bytes == 0) {
114 return 0;
115 }
Henrik Lundin1391bd42017-11-24 09:28:57 +0100116
117 constexpr size_t kRtpHeaderLength = 12;
118 constexpr size_t kRtpDumpHeaderLength = 8;
119 const uint16_t length = htons(rtc::checked_cast<uint16_t>(
120 kRtpHeaderLength + kRtpDumpHeaderLength + payload_len_bytes));
121 const uint16_t plen = htons(
122 rtc::checked_cast<uint16_t>(kRtpHeaderLength + payload_len_bytes));
123 const uint32_t offset = htonl(timestamp / (timestamp_rate_hz_ / 1000));
124 RTC_CHECK_EQ(fwrite(&length, sizeof(uint16_t), 1, out_file_), 1);
125 RTC_CHECK_EQ(fwrite(&plen, sizeof(uint16_t), 1, out_file_), 1);
126 RTC_CHECK_EQ(fwrite(&offset, sizeof(uint32_t), 1, out_file_), 1);
127
128 const uint8_t rtp_header[] = {0x80,
129 static_cast<uint8_t>(payload_type & 0x7F),
130 static_cast<uint8_t>(sequence_number_ >> 8),
131 static_cast<uint8_t>(sequence_number_),
132 static_cast<uint8_t>(timestamp >> 24),
133 static_cast<uint8_t>(timestamp >> 16),
134 static_cast<uint8_t>(timestamp >> 8),
135 static_cast<uint8_t>(timestamp),
136 static_cast<uint8_t>(ssrc_ >> 24),
137 static_cast<uint8_t>(ssrc_ >> 16),
138 static_cast<uint8_t>(ssrc_ >> 8),
139 static_cast<uint8_t>(ssrc_)};
140 static_assert(sizeof(rtp_header) == kRtpHeaderLength, "");
141 RTC_CHECK_EQ(
142 fwrite(rtp_header, sizeof(uint8_t), kRtpHeaderLength, out_file_),
143 kRtpHeaderLength);
144 ++sequence_number_; // Intended to wrap on overflow.
145
146 RTC_CHECK_EQ(
147 fwrite(payload_data, sizeof(uint8_t), payload_len_bytes, out_file_),
148 payload_len_bytes);
149
150 return 0;
151 }
152
153 private:
154 FILE* const out_file_;
155 const uint32_t ssrc_;
156 const int timestamp_rate_hz_;
157 uint16_t sequence_number_ = 0;
158};
159
160void SetFrameLenIfFlagIsPositive(int* config_frame_len) {
161 if (FLAG_frame_len > 0) {
162 *config_frame_len = FLAG_frame_len;
163 }
164}
165
Henrik Lundinf1061c22017-12-07 10:13:47 +0100166template <typename T>
167typename T::Config GetCodecConfig() {
168 typename T::Config config;
Henrik Lundin1391bd42017-11-24 09:28:57 +0100169 SetFrameLenIfFlagIsPositive(&config.frame_size_ms);
Henrik Lundinf1061c22017-12-07 10:13:47 +0100170 RTC_CHECK(config.IsOk());
171 return config;
172}
173
174AudioEncoderL16::Config Pcm16bConfig(CodecType codec_type) {
175 auto config = GetCodecConfig<AudioEncoderL16>();
Henrik Lundin1391bd42017-11-24 09:28:57 +0100176 switch (codec_type) {
177 case CodecType::kPcm16b8:
178 config.sample_rate_hz = 8000;
179 return config;
180 case CodecType::kPcm16b16:
181 config.sample_rate_hz = 16000;
182 return config;
183 case CodecType::kPcm16b32:
184 config.sample_rate_hz = 32000;
185 return config;
186 case CodecType::kPcm16b48:
187 config.sample_rate_hz = 48000;
188 return config;
189 default:
190 RTC_NOTREACHED();
191 return config;
192 }
193}
194
195std::unique_ptr<AudioEncoder> CreateEncoder(CodecType codec_type,
196 int payload_type) {
197 switch (codec_type) {
198 case CodecType::kOpus: {
Henrik Lundinf1061c22017-12-07 10:13:47 +0100199 AudioEncoderOpus::Config config = GetCodecConfig<AudioEncoderOpus>();
Henrik Lundin1391bd42017-11-24 09:28:57 +0100200 if (FLAG_bitrate > 0) {
201 config.bitrate_bps = FLAG_bitrate;
202 }
203 config.dtx_enabled = FLAG_dtx;
Henrik Lundin1391bd42017-11-24 09:28:57 +0100204 RTC_CHECK(config.IsOk());
205 return AudioEncoderOpus::MakeAudioEncoder(config, payload_type);
206 }
207
208 case CodecType::kPcmU:
209 case CodecType::kPcmA: {
Henrik Lundinf1061c22017-12-07 10:13:47 +0100210 AudioEncoderG711::Config config = GetCodecConfig<AudioEncoderG711>();
Henrik Lundin1391bd42017-11-24 09:28:57 +0100211 config.type = codec_type == CodecType::kPcmU
212 ? AudioEncoderG711::Config::Type::kPcmU
213 : AudioEncoderG711::Config::Type::kPcmA;
214 RTC_CHECK(config.IsOk());
215 return AudioEncoderG711::MakeAudioEncoder(config, payload_type);
216 }
217
218 case CodecType::kG722: {
Henrik Lundinf1061c22017-12-07 10:13:47 +0100219 return AudioEncoderG722::MakeAudioEncoder(
220 GetCodecConfig<AudioEncoderG722>(), payload_type);
Henrik Lundin1391bd42017-11-24 09:28:57 +0100221 }
222
223 case CodecType::kPcm16b8:
224 case CodecType::kPcm16b16:
225 case CodecType::kPcm16b32:
226 case CodecType::kPcm16b48: {
227 return AudioEncoderL16::MakeAudioEncoder(Pcm16bConfig(codec_type),
228 payload_type);
229 }
230
231 case CodecType::kIlbc: {
Henrik Lundinf1061c22017-12-07 10:13:47 +0100232 return AudioEncoderIlbc::MakeAudioEncoder(
233 GetCodecConfig<AudioEncoderIlbc>(), payload_type);
Henrik Lundin1391bd42017-11-24 09:28:57 +0100234 }
235
236 case CodecType::kIsac: {
Henrik Lundinf1061c22017-12-07 10:13:47 +0100237 return AudioEncoderIsac::MakeAudioEncoder(
238 GetCodecConfig<AudioEncoderIsac>(), payload_type);
Henrik Lundin1391bd42017-11-24 09:28:57 +0100239 }
240 }
241 RTC_NOTREACHED();
242 return nullptr;
243}
244
245AudioEncoderCng::Config GetCngConfig(int sample_rate_hz) {
246 AudioEncoderCng::Config cng_config;
247 const auto default_payload_type = [&] {
248 switch (sample_rate_hz) {
249 case 8000: return 13;
250 case 16000: return 98;
251 case 32000: return 99;
252 case 48000: return 100;
253 default: RTC_NOTREACHED();
254 }
255 return 0;
256 };
257 cng_config.payload_type = FLAG_cng_payload_type != -1
258 ? FLAG_cng_payload_type
259 : default_payload_type();
260 return cng_config;
261}
262
263int RunRtpEncode(int argc, char* argv[]) {
264 const std::string program_name = argv[0];
265 const std::string usage =
266 "Tool for generating an RTP dump file from audio input.\n"
267 "Run " +
268 program_name +
269 " --help for usage.\n"
270 "Example usage:\n" +
271 program_name + " input.pcm output.rtp --codec=[codec] " +
272 "--frame_len=[frame_len] --bitrate=[bitrate]\n\n";
273 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true) || FLAG_help ||
274 (!FLAG_list_codecs && argc != 3)) {
275 printf("%s", usage.c_str());
276 if (FLAG_help) {
277 rtc::FlagList::Print(nullptr, false);
278 return 0;
279 }
280 return 1;
281 }
282
283 if (FLAG_list_codecs) {
284 printf("The following arguments are valid --codec parameters:\n");
285 for (const auto& c : CodecList()) {
286 printf(" %s\n", c.first.c_str());
287 }
288 return 0;
289 }
290
291 const auto codec_it = CodecList().find(FLAG_codec);
292 if (codec_it == CodecList().end()) {
293 printf("%s is not a valid codec name.\n", FLAG_codec);
294 printf("Use argument --list_codecs to see all valid codec names.\n");
295 return 1;
296 }
297
298 // Create the codec.
299 const int payload_type = FLAG_payload_type == -1
300 ? codec_it->second.default_payload_type
301 : FLAG_payload_type;
302 std::unique_ptr<AudioEncoder> codec =
303 CreateEncoder(codec_it->second.type, payload_type);
304
305 // Create an external VAD/CNG encoder if needed.
Henrik Lundin32f64d22017-11-28 13:05:35 +0100306 if (FLAG_dtx && !codec_it->second.internal_dtx) {
Henrik Lundin1391bd42017-11-24 09:28:57 +0100307 AudioEncoderCng::Config cng_config = GetCngConfig(codec->SampleRateHz());
308 RTC_DCHECK(codec);
309 cng_config.speech_encoder = std::move(codec);
310 codec = rtc::MakeUnique<AudioEncoderCng>(std::move(cng_config));
311 }
312 RTC_DCHECK(codec);
313
314 // Set up ACM.
315 const int timestamp_rate_hz = codec->RtpTimestampRateHz();
316 AudioCodingModule::Config config;
317 std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(config));
318 acm->SetEncoder(std::move(codec));
319
320 // Open files.
321 printf("Input file: %s\n", argv[1]);
322 InputAudioFile input_file(argv[1], false); // Open input in non-looping mode.
323 FILE* out_file = fopen(argv[2], "wb");
324 RTC_CHECK(out_file) << "Could not open file " << argv[2] << " for writing";
325 printf("Output file: %s\n", argv[2]);
326 fprintf(out_file, "#!rtpplay1.0 \n"); //,
327 // Write 3 32-bit values followed by 2 16-bit values, all set to 0. This means
328 // a total of 16 bytes.
329 const uint8_t file_header[16] = {0};
330 RTC_CHECK_EQ(fwrite(file_header, sizeof(file_header), 1, out_file), 1);
331
332 // Create and register the packetizer, which will write the packets to file.
333 Packetizer packetizer(out_file, FLAG_ssrc, timestamp_rate_hz);
334 RTC_DCHECK_EQ(acm->RegisterTransportCallback(&packetizer), 0);
335
336 AudioFrame audio_frame;
337 audio_frame.samples_per_channel_ = FLAG_sample_rate / 100; // 10 ms
338 audio_frame.sample_rate_hz_ = FLAG_sample_rate;
339 audio_frame.num_channels_ = 1;
340
341 while (input_file.Read(audio_frame.samples_per_channel_,
342 audio_frame.mutable_data())) {
343 RTC_CHECK_GE(acm->Add10MsData(audio_frame), 0);
344 audio_frame.timestamp_ += audio_frame.samples_per_channel_;
345 }
346
347 return 0;
348}
349
350} // namespace
351} // namespace test
352} // namespace webrtc
353
354int main(int argc, char* argv[]) {
355 return webrtc::test::RunRtpEncode(argc, argv);
356}