blob: 90989db7d6e48d8d94fc7127f6ea2c1d50de7eb0 [file] [log] [blame]
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +00001/*
2 * Copyright (c) 2014 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
Benjamin Wright90ab76d2018-08-23 11:33:29 -070013#include <fstream>
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +000014#include <map>
kwiberg27f982b2016-03-01 11:52:33 -080015#include <memory>
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +000016
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020017#include "absl/flags/flag.h"
18#include "absl/flags/parse.h"
Danil Chapovalov83bbe912019-08-07 12:24:53 +020019#include "api/rtc_event_log/rtc_event_log.h"
Sergey Silkin626f7ff2019-09-12 10:51:19 +020020#include "api/task_queue/default_task_queue_factory.h"
Danil Chapovalov99b71df2018-10-26 15:57:48 +020021#include "api/test/video/function_video_decoder_factory.h"
Ilya Nikolaevskiy33aaa352020-01-21 13:38:19 +010022#include "api/transport/field_trial_based_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/video_codecs/video_decoder.h"
24#include "call/call.h"
25#include "common_video/libyuv/include/webrtc_libyuv.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "media/engine/internal_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/string_to_number.h"
Sam Zackrissonb45bdb52018-10-02 16:25:59 +020029#include "rtc_base/strings/json.h"
Steve Anton10542f22019-01-11 09:11:00 -080030#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "system_wrappers/include/clock.h"
32#include "system_wrappers/include/sleep.h"
Benjamin Wright8efafdf2019-01-11 10:48:42 -080033#include "test/call_config_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "test/call_test.h"
35#include "test/encoder_settings.h"
36#include "test/fake_decoder.h"
37#include "test/gtest.h"
38#include "test/null_transport.h"
39#include "test/rtp_file_reader.h"
Sergey Silkin626f7ff2019-09-12 10:51:19 +020040#include "test/rtp_header_parser.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020041#include "test/run_loop.h"
42#include "test/run_test.h"
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020043#include "test/test_video_capturer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "test/testsupport/frame_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "test/video_renderer.h"
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +000046
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020047// Flag for payload type.
48ABSL_FLAG(int,
49 media_payload_type,
50 webrtc::test::CallTest::kPayloadTypeVP8,
51 "Media payload type");
52
53// Flag for RED payload type.
54ABSL_FLAG(int,
55 red_payload_type,
56 webrtc::test::CallTest::kRedPayloadType,
57 "RED payload type");
58
59// Flag for ULPFEC payload type.
60ABSL_FLAG(int,
61 ulpfec_payload_type,
62 webrtc::test::CallTest::kUlpfecPayloadType,
63 "ULPFEC payload type");
64
65ABSL_FLAG(int,
66 media_payload_type_rtx,
67 webrtc::test::CallTest::kSendRtxPayloadType,
68 "Media over RTX payload type");
69
70ABSL_FLAG(int,
71 red_payload_type_rtx,
72 webrtc::test::CallTest::kRtxRedPayloadType,
73 "RED over RTX payload type");
74
75// Flag for SSRC.
76const std::string& DefaultSsrc() {
77 static const std::string ssrc =
78 std::to_string(webrtc::test::CallTest::kVideoSendSsrcs[0]);
79 return ssrc;
80}
81ABSL_FLAG(std::string, ssrc, DefaultSsrc().c_str(), "Incoming SSRC");
82
83const std::string& DefaultSsrcRtx() {
84 static const std::string ssrc_rtx =
85 std::to_string(webrtc::test::CallTest::kSendRtxSsrcs[0]);
86 return ssrc_rtx;
87}
88ABSL_FLAG(std::string, ssrc_rtx, DefaultSsrcRtx().c_str(), "Incoming RTX SSRC");
89
90// Flag for abs-send-time id.
91ABSL_FLAG(int, abs_send_time_id, -1, "RTP extension ID for abs-send-time");
92
93// Flag for transmission-offset id.
94ABSL_FLAG(int,
95 transmission_offset_id,
96 -1,
97 "RTP extension ID for transmission-offset");
98
99// Flag for rtpdump input file.
100ABSL_FLAG(std::string, input_file, "", "input file");
101
102ABSL_FLAG(std::string, config_file, "", "config file");
103
104// Flag for raw output files.
105ABSL_FLAG(std::string,
106 out_base,
107 "",
108 "Basename (excluding .jpg) for raw output");
109
110ABSL_FLAG(std::string,
111 decoder_bitstream_filename,
112 "",
113 "Decoder bitstream output file");
114
115// Flag for video codec.
116ABSL_FLAG(std::string, codec, "VP8", "Video codec");
117
oprypin6e09d872017-08-31 03:21:39 -0700118namespace {
119
120static bool ValidatePayloadType(int32_t payload_type) {
121 return payload_type > 0 && payload_type <= 127;
122}
123
124static bool ValidateSsrc(const char* ssrc_string) {
125 return rtc::StringToNumber<uint32_t>(ssrc_string).has_value();
126}
127
128static bool ValidateOptionalPayloadType(int32_t payload_type) {
129 return payload_type == -1 || ValidatePayloadType(payload_type);
130}
131
132static bool ValidateRtpHeaderExtensionId(int32_t extension_id) {
133 return extension_id >= -1 && extension_id < 15;
134}
135
136bool ValidateInputFilenameNotEmpty(const std::string& string) {
137 return !string.empty();
138}
139
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200140static int MediaPayloadType() {
141 return absl::GetFlag(FLAGS_media_payload_type);
142}
143
144static int RedPayloadType() {
145 return absl::GetFlag(FLAGS_red_payload_type);
146}
147
148static int UlpfecPayloadType() {
149 return absl::GetFlag(FLAGS_ulpfec_payload_type);
150}
151
152static int MediaPayloadTypeRtx() {
153 return absl::GetFlag(FLAGS_media_payload_type_rtx);
154}
155
156static int RedPayloadTypeRtx() {
157 return absl::GetFlag(FLAGS_red_payload_type_rtx);
158}
159
160static uint32_t Ssrc() {
161 return rtc::StringToNumber<uint32_t>(absl::GetFlag(FLAGS_ssrc)).value();
162}
163
164static uint32_t SsrcRtx() {
165 return rtc::StringToNumber<uint32_t>(absl::GetFlag(FLAGS_ssrc_rtx)).value();
166}
167
168static int AbsSendTimeId() {
169 return absl::GetFlag(FLAGS_abs_send_time_id);
170}
171
172static int TransmissionOffsetId() {
173 return absl::GetFlag(FLAGS_transmission_offset_id);
174}
175
176static std::string InputFile() {
177 return absl::GetFlag(FLAGS_input_file);
178}
179
180static std::string ConfigFile() {
181 return absl::GetFlag(FLAGS_config_file);
182}
183
184static std::string OutBase() {
185 return absl::GetFlag(FLAGS_out_base);
186}
187
188static std::string DecoderBitstreamFilename() {
189 return absl::GetFlag(FLAGS_decoder_bitstream_filename);
190}
191
192static std::string Codec() {
193 return absl::GetFlag(FLAGS_codec);
194}
195
oprypin6e09d872017-08-31 03:21:39 -0700196} // namespace
197
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000198namespace webrtc {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000199
200static const uint32_t kReceiverLocalSsrc = 0x123456;
201
nisse7ade7b32016-03-23 04:48:10 -0700202class FileRenderPassthrough : public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000203 public:
nisse7ade7b32016-03-23 04:48:10 -0700204 FileRenderPassthrough(const std::string& basename,
205 rtc::VideoSinkInterface<VideoFrame>* renderer)
philipel99b63452017-08-25 07:24:21 -0700206 : basename_(basename), renderer_(renderer), file_(nullptr), count_(0) {}
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000207
Mirko Bonadeife055c12019-01-29 22:53:28 +0100208 ~FileRenderPassthrough() override {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200209 if (file_)
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000210 fclose(file_);
211 }
212
213 private:
nisseeb83a1a2016-03-21 01:27:56 -0700214 void OnFrame(const VideoFrame& video_frame) override {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200215 if (renderer_)
nisseeb83a1a2016-03-21 01:27:56 -0700216 renderer_->OnFrame(video_frame);
philipel99b63452017-08-25 07:24:21 -0700217
pbosbb36fdf2015-07-09 07:48:14 -0700218 if (basename_.empty())
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000219 return;
philipel99b63452017-08-25 07:24:21 -0700220
221 std::stringstream filename;
222 filename << basename_ << count_++ << "_" << video_frame.timestamp()
223 << ".jpg";
224
philipel99b63452017-08-25 07:24:21 -0700225 test::JpegFrameWriter frame_writer(filename.str());
226 RTC_CHECK(frame_writer.WriteFrame(video_frame, 100));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000227 }
228
229 const std::string basename_;
nisse7ade7b32016-03-23 04:48:10 -0700230 rtc::VideoSinkInterface<VideoFrame>* const renderer_;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000231 FILE* file_;
232 size_t count_;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000233};
234
Niels Möllerf88a22c2018-06-19 17:05:03 +0200235class DecoderBitstreamFileWriter : public test::FakeDecoder {
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000236 public:
237 explicit DecoderBitstreamFileWriter(const char* filename)
238 : file_(fopen(filename, "wb")) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200239 RTC_DCHECK(file_);
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000240 }
Mirko Bonadeife055c12019-01-29 22:53:28 +0100241 ~DecoderBitstreamFileWriter() override { fclose(file_); }
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000242
Niels Möllerf88a22c2018-06-19 17:05:03 +0200243 int32_t Decode(const EncodedImage& encoded_frame,
Benjamin Wright8efafdf2019-01-11 10:48:42 -0800244 bool /* missing_frames */,
Benjamin Wright8efafdf2019-01-11 10:48:42 -0800245 int64_t /* render_time_ms */) override {
Niels Möller77536a22019-01-15 08:50:01 +0100246 if (fwrite(encoded_frame.data(), 1, encoded_frame.size(), file_) <
247 encoded_frame.size()) {
Niels Möllerf88a22c2018-06-19 17:05:03 +0200248 RTC_LOG_ERR(LS_ERROR) << "fwrite of encoded frame failed.";
249 return WEBRTC_VIDEO_CODEC_ERROR;
250 }
251 return WEBRTC_VIDEO_CODEC_OK;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000252 }
253
254 private:
255 FILE* file_;
256};
257
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700258// The RtpReplayer is responsible for parsing the configuration provided by the
259// user, setting up the windows, recieve streams and decoders and then replaying
260// the provided RTP dump.
261class RtpReplayer final {
262 public:
263 // Replay a rtp dump with an optional json configuration.
264 static void Replay(const std::string& replay_config_path,
265 const std::string& rtp_dump_path) {
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200266 std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory =
267 webrtc::CreateDefaultTaskQueueFactory();
Danil Chapovalov83bbe912019-08-07 12:24:53 +0200268 webrtc::RtcEventLogNull event_log;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700269 Call::Config call_config(&event_log);
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200270 call_config.task_queue_factory = task_queue_factory.get();
Ilya Nikolaevskiy33aaa352020-01-21 13:38:19 +0100271 call_config.trials = new FieldTrialBasedConfig();
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100272 std::unique_ptr<Call> call(Call::Create(call_config));
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700273 std::unique_ptr<StreamState> stream_state;
274 // Attempt to load the configuration
275 if (replay_config_path.empty()) {
276 stream_state = ConfigureFromFlags(rtp_dump_path, call.get());
277 } else {
278 stream_state = ConfigureFromFile(replay_config_path, call.get());
279 }
280 if (stream_state == nullptr) {
281 return;
282 }
283 // Attempt to create an RtpReader from the input file.
284 std::unique_ptr<test::RtpFileReader> rtp_reader =
285 CreateRtpReader(rtp_dump_path);
286 if (rtp_reader == nullptr) {
287 return;
288 }
289 // Start replaying the provided stream now that it has been configured.
290 for (const auto& receive_stream : stream_state->receive_streams) {
291 receive_stream->Start();
292 }
293 ReplayPackets(call.get(), rtp_reader.get());
294 for (const auto& receive_stream : stream_state->receive_streams) {
295 call->DestroyVideoReceiveStream(receive_stream);
296 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000297 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000298
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700299 private:
300 // Holds all the shared memory structures required for a recieve stream. This
301 // structure is used to prevent members being deallocated before the replay
302 // has been finished.
303 struct StreamState {
304 test::NullTransport transport;
305 std::vector<std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>>> sinks;
306 std::vector<VideoReceiveStream*> receive_streams;
Niels Möllercbcbc222018-09-28 09:07:24 +0200307 std::unique_ptr<VideoDecoderFactory> decoder_factory;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700308 };
309
310 // Loads multiple configurations from the provided configuration file.
311 static std::unique_ptr<StreamState> ConfigureFromFile(
312 const std::string& config_path,
313 Call* call) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200314 auto stream_state = std::make_unique<StreamState>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700315 // Parse the configuration file.
316 std::ifstream config_file(config_path);
317 std::stringstream raw_json_buffer;
318 raw_json_buffer << config_file.rdbuf();
319 std::string raw_json = raw_json_buffer.str();
320 Json::Reader json_reader;
321 Json::Value json_configs;
322 if (!json_reader.parse(raw_json, json_configs)) {
323 fprintf(stderr, "Error parsing JSON config\n");
324 fprintf(stderr, "%s\n", json_reader.getFormatedErrorMessages().c_str());
325 return nullptr;
326 }
327
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200328 stream_state->decoder_factory = std::make_unique<InternalDecoderFactory>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700329 size_t config_count = 0;
330 for (const auto& json : json_configs) {
331 // Create the configuration and parse the JSON into the config.
Benjamin Wright8efafdf2019-01-11 10:48:42 -0800332 auto receive_config =
333 ParseVideoReceiveStreamJsonConfig(&(stream_state->transport), json);
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700334 // Instantiate the underlying decoder.
335 for (auto& decoder : receive_config.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200336 decoder = test::CreateMatchingDecoder(decoder.payload_type,
337 decoder.video_format.name);
Philip Eliassona4f23ad2020-08-03 15:44:56 +0000338 decoder.decoder_factory = stream_state->decoder_factory.get();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700339 }
340 // Create a window for this config.
341 std::stringstream window_title;
342 window_title << "Playback Video (" << config_count++ << ")";
343 stream_state->sinks.emplace_back(
344 test::VideoRenderer::Create(window_title.str().c_str(), 640, 480));
345 // Create a receive stream for this config.
346 receive_config.renderer = stream_state->sinks.back().get();
347 stream_state->receive_streams.emplace_back(
348 call->CreateVideoReceiveStream(std::move(receive_config)));
349 }
350 return stream_state;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000351 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000352
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700353 // Loads the base configuration from flags passed in on the commandline.
354 static std::unique_ptr<StreamState> ConfigureFromFlags(
355 const std::string& rtp_dump_path,
356 Call* call) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200357 auto stream_state = std::make_unique<StreamState>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700358 // Create the video renderers. We must add both to the stream state to keep
359 // them from deallocating.
360 std::stringstream window_title;
361 window_title << "Playback Video (" << rtp_dump_path << ")";
362 std::unique_ptr<test::VideoRenderer> playback_video(
363 test::VideoRenderer::Create(window_title.str().c_str(), 640, 480));
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200364 auto file_passthrough = std::make_unique<FileRenderPassthrough>(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200365 OutBase(), playback_video.get());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700366 stream_state->sinks.push_back(std::move(playback_video));
367 stream_state->sinks.push_back(std::move(file_passthrough));
368 // Setup the configuration from the flags.
369 VideoReceiveStream::Config receive_config(&(stream_state->transport));
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200370 receive_config.rtp.remote_ssrc = Ssrc();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700371 receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200372 receive_config.rtp.rtx_ssrc = SsrcRtx();
373 receive_config.rtp.rtx_associated_payload_types[MediaPayloadTypeRtx()] =
374 MediaPayloadType();
375 receive_config.rtp.rtx_associated_payload_types[RedPayloadTypeRtx()] =
376 RedPayloadType();
377 receive_config.rtp.ulpfec_payload_type = UlpfecPayloadType();
378 receive_config.rtp.red_payload_type = RedPayloadType();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700379 receive_config.rtp.nack.rtp_history_ms = 1000;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200380 if (TransmissionOffsetId() != -1) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700381 receive_config.rtp.extensions.push_back(RtpExtension(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200382 RtpExtension::kTimestampOffsetUri, TransmissionOffsetId()));
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700383 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200384 if (AbsSendTimeId() != -1) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700385 receive_config.rtp.extensions.push_back(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200386 RtpExtension(RtpExtension::kAbsSendTimeUri, AbsSendTimeId()));
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700387 }
388 receive_config.renderer = stream_state->sinks.back().get();
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000389
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700390 // Setup the receiving stream
391 VideoReceiveStream::Decoder decoder;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200392 decoder = test::CreateMatchingDecoder(MediaPayloadType(), Codec());
393 if (DecoderBitstreamFilename().empty()) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200394 stream_state->decoder_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200395 std::make_unique<InternalDecoderFactory>();
Niels Möllercbcbc222018-09-28 09:07:24 +0200396 } else {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700397 // Replace decoder with file writer if we're writing the bitstream to a
398 // file instead.
Niels Möllercbcbc222018-09-28 09:07:24 +0200399 stream_state->decoder_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200400 std::make_unique<test::FunctionVideoDecoderFactory>([]() {
401 return std::make_unique<DecoderBitstreamFileWriter>(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200402 DecoderBitstreamFilename().c_str());
Niels Möllercbcbc222018-09-28 09:07:24 +0200403 });
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700404 }
Philip Eliassona4f23ad2020-08-03 15:44:56 +0000405 decoder.decoder_factory = stream_state->decoder_factory.get();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700406 receive_config.decoders.push_back(decoder);
407
408 stream_state->receive_streams.emplace_back(
409 call->CreateVideoReceiveStream(std::move(receive_config)));
410 return stream_state;
411 }
412
413 static std::unique_ptr<test::RtpFileReader> CreateRtpReader(
414 const std::string& rtp_dump_path) {
415 std::unique_ptr<test::RtpFileReader> rtp_reader(test::RtpFileReader::Create(
416 test::RtpFileReader::kRtpDump, rtp_dump_path));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200417 if (!rtp_reader) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700418 rtp_reader.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap,
419 rtp_dump_path));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200420 if (!rtp_reader) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700421 fprintf(
422 stderr,
423 "Couldn't open input file as either a rtpdump or .pcap. Note "
424 "that .pcapng is not supported.\nTrying to interpret the file as "
425 "length/packet interleaved.\n");
426 rtp_reader.reset(test::RtpFileReader::Create(
427 test::RtpFileReader::kLengthPacketInterleaved, rtp_dump_path));
428 if (!rtp_reader) {
429 fprintf(stderr,
430 "Unable to open input file with any supported format\n");
431 return nullptr;
432 }
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000433 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000434 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700435 return rtp_reader;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000436 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000437
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700438 static void ReplayPackets(Call* call, test::RtpFileReader* rtp_reader) {
439 int64_t replay_start_ms = -1;
440 int num_packets = 0;
441 std::map<uint32_t, int> unknown_packets;
442 while (true) {
443 int64_t now_ms = rtc::TimeMillis();
444 if (replay_start_ms == -1) {
445 replay_start_ms = now_ms;
446 }
philipel02f03962018-01-11 17:28:35 +0100447
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700448 test::RtpPacket packet;
449 if (!rtp_reader->NextPacket(&packet)) {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000450 break;
451 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700452
453 int64_t deliver_in_ms = replay_start_ms + packet.time_ms - now_ms;
454 if (deliver_in_ms > 0) {
455 SleepMs(deliver_in_ms);
456 }
457
458 ++num_packets;
459 switch (call->Receiver()->DeliverPacket(
460 webrtc::MediaType::VIDEO,
461 rtc::CopyOnWriteBuffer(packet.data, packet.length),
462 /* packet_time_us */ -1)) {
463 case PacketReceiver::DELIVERY_OK:
464 break;
465 case PacketReceiver::DELIVERY_UNKNOWN_SSRC: {
466 RTPHeader header;
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200467 std::unique_ptr<RtpHeaderParser> parser(
468 RtpHeaderParser::CreateForTest());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700469 parser->Parse(packet.data, packet.length, &header);
470 if (unknown_packets[header.ssrc] == 0)
471 fprintf(stderr, "Unknown SSRC: %u!\n", header.ssrc);
472 ++unknown_packets[header.ssrc];
473 break;
474 }
475 case PacketReceiver::DELIVERY_PACKET_ERROR: {
476 fprintf(stderr,
477 "Packet error, corrupt packets or incorrect setup?\n");
478 RTPHeader header;
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200479 std::unique_ptr<RtpHeaderParser> parser(
480 RtpHeaderParser::CreateForTest());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700481 parser->Parse(packet.data, packet.length, &header);
482 fprintf(stderr, "Packet len=%zu pt=%u seq=%u ts=%u ssrc=0x%8x\n",
483 packet.length, header.payloadType, header.sequenceNumber,
484 header.timestamp, header.ssrc);
485 break;
486 }
philipp.hancke7b589602017-01-26 04:54:04 -0800487 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000488 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700489 fprintf(stderr, "num_packets: %d\n", num_packets);
490
491 for (std::map<uint32_t, int>::const_iterator it = unknown_packets.begin();
492 it != unknown_packets.end(); ++it) {
493 fprintf(stderr, "Packets for unknown ssrc '%u': %d\n", it->first,
494 it->second);
495 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000496 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700497}; // class RtpReplayer
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000498
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700499void RtpReplay() {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200500 RtpReplayer::Replay(ConfigFile(), InputFile());
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000501}
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700502
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000503} // namespace webrtc
504
505int main(int argc, char* argv[]) {
506 ::testing::InitGoogleTest(&argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200507 absl::ParseCommandLine(argc, argv);
oprypin6e09d872017-08-31 03:21:39 -0700508
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200509 RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type)));
510 RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type_rtx)));
511 RTC_CHECK(ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type)));
philipel752968e2017-12-05 12:40:28 +0100512 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200513 ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type_rtx)));
philipel752968e2017-12-05 12:40:28 +0100514 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200515 ValidateOptionalPayloadType(absl::GetFlag(FLAGS_ulpfec_payload_type)));
516 RTC_CHECK(ValidateSsrc(absl::GetFlag(FLAGS_ssrc).c_str()));
517 RTC_CHECK(ValidateSsrc(absl::GetFlag(FLAGS_ssrc_rtx).c_str()));
Yves Gerey665174f2018-06-19 15:03:05 +0200518 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200519 ValidateRtpHeaderExtensionId(absl::GetFlag(FLAGS_abs_send_time_id)));
520 RTC_CHECK(ValidateRtpHeaderExtensionId(
521 absl::GetFlag(FLAGS_transmission_offset_id)));
522 RTC_CHECK(ValidateInputFilenameNotEmpty(absl::GetFlag(FLAGS_input_file)));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000523
524 webrtc::test::RunTest(webrtc::RtpReplay);
525 return 0;
526}