blob: dc98e4ed4e08307fc20f92b51ed3e682abafccff [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"
Philipp Hanckefae4fb12021-01-26 18:05:56 +010023#include "api/video/video_codec_type.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "api/video_codecs/video_decoder.h"
25#include "call/call.h"
26#include "common_video/libyuv/include/webrtc_libyuv.h"
Steve Anton10542f22019-01-11 09:11:00 -080027#include "media/engine/internal_decoder_factory.h"
Danil Chapovalov76a35d92021-06-08 12:30:46 +020028#include "modules/rtp_rtcp/source/rtp_packet.h"
Philipp Hanckefae4fb12021-01-26 18:05:56 +010029#include "modules/video_coding/utility/ivf_file_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/string_to_number.h"
Sam Zackrissonb45bdb52018-10-02 16:25:59 +020032#include "rtc_base/strings/json.h"
Steve Anton10542f22019-01-11 09:11:00 -080033#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#include "system_wrappers/include/clock.h"
35#include "system_wrappers/include/sleep.h"
Benjamin Wright8efafdf2019-01-11 10:48:42 -080036#include "test/call_config_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020037#include "test/call_test.h"
38#include "test/encoder_settings.h"
39#include "test/fake_decoder.h"
40#include "test/gtest.h"
41#include "test/null_transport.h"
42#include "test/rtp_file_reader.h"
43#include "test/run_loop.h"
44#include "test/run_test.h"
Sebastian Janssonf1f363f2018-08-13 14:24:58 +020045#include "test/test_video_capturer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "test/testsupport/frame_writer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "test/video_renderer.h"
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +000048
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020049// Flag for payload type.
50ABSL_FLAG(int,
51 media_payload_type,
52 webrtc::test::CallTest::kPayloadTypeVP8,
53 "Media payload type");
54
55// Flag for RED payload type.
56ABSL_FLAG(int,
57 red_payload_type,
58 webrtc::test::CallTest::kRedPayloadType,
59 "RED payload type");
60
61// Flag for ULPFEC payload type.
62ABSL_FLAG(int,
63 ulpfec_payload_type,
64 webrtc::test::CallTest::kUlpfecPayloadType,
65 "ULPFEC payload type");
66
67ABSL_FLAG(int,
68 media_payload_type_rtx,
69 webrtc::test::CallTest::kSendRtxPayloadType,
70 "Media over RTX payload type");
71
72ABSL_FLAG(int,
73 red_payload_type_rtx,
74 webrtc::test::CallTest::kRtxRedPayloadType,
75 "RED over RTX payload type");
76
Philipp Hanckec060ce42021-03-15 13:15:59 +010077// Flag for SSRC and RTX SSRC.
78ABSL_FLAG(uint32_t,
79 ssrc,
80 webrtc::test::CallTest::kVideoSendSsrcs[0],
81 "Incoming SSRC");
82ABSL_FLAG(uint32_t,
83 ssrc_rtx,
84 webrtc::test::CallTest::kSendRtxSsrcs[0],
85 "Incoming RTX SSRC");
Mirko Bonadei2ab97f62019-07-18 13:44:12 +020086
87// Flag for abs-send-time id.
88ABSL_FLAG(int, abs_send_time_id, -1, "RTP extension ID for abs-send-time");
89
90// Flag for transmission-offset id.
91ABSL_FLAG(int,
92 transmission_offset_id,
93 -1,
94 "RTP extension ID for transmission-offset");
95
96// Flag for rtpdump input file.
97ABSL_FLAG(std::string, input_file, "", "input file");
98
99ABSL_FLAG(std::string, config_file, "", "config file");
100
101// Flag for raw output files.
102ABSL_FLAG(std::string,
103 out_base,
104 "",
105 "Basename (excluding .jpg) for raw output");
106
107ABSL_FLAG(std::string,
108 decoder_bitstream_filename,
109 "",
110 "Decoder bitstream output file");
111
Philipp Hanckefae4fb12021-01-26 18:05:56 +0100112ABSL_FLAG(std::string, decoder_ivf_filename, "", "Decoder ivf output file");
113
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200114// Flag for video codec.
115ABSL_FLAG(std::string, codec, "VP8", "Video codec");
116
Philipp Hanckef3a687a2021-03-15 12:04:39 +0100117// Flags for rtp start and stop timestamp.
118ABSL_FLAG(uint32_t,
119 start_timestamp,
120 0,
121 "RTP start timestamp, packets with smaller timestamp will be ignored "
122 "(no wraparound)");
123ABSL_FLAG(uint32_t,
124 stop_timestamp,
125 4294967295,
126 "RTP stop timestamp, packets with larger timestamp will be ignored "
127 "(no wraparound)");
128
oprypin6e09d872017-08-31 03:21:39 -0700129namespace {
130
131static bool ValidatePayloadType(int32_t payload_type) {
132 return payload_type > 0 && payload_type <= 127;
133}
134
oprypin6e09d872017-08-31 03:21:39 -0700135static bool ValidateOptionalPayloadType(int32_t payload_type) {
136 return payload_type == -1 || ValidatePayloadType(payload_type);
137}
138
139static bool ValidateRtpHeaderExtensionId(int32_t extension_id) {
140 return extension_id >= -1 && extension_id < 15;
141}
142
143bool ValidateInputFilenameNotEmpty(const std::string& string) {
144 return !string.empty();
145}
146
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200147static int MediaPayloadType() {
148 return absl::GetFlag(FLAGS_media_payload_type);
149}
150
151static int RedPayloadType() {
152 return absl::GetFlag(FLAGS_red_payload_type);
153}
154
155static int UlpfecPayloadType() {
156 return absl::GetFlag(FLAGS_ulpfec_payload_type);
157}
158
159static int MediaPayloadTypeRtx() {
160 return absl::GetFlag(FLAGS_media_payload_type_rtx);
161}
162
163static int RedPayloadTypeRtx() {
164 return absl::GetFlag(FLAGS_red_payload_type_rtx);
165}
166
167static uint32_t Ssrc() {
Philipp Hanckec060ce42021-03-15 13:15:59 +0100168 return absl::GetFlag(FLAGS_ssrc);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200169}
170
171static uint32_t SsrcRtx() {
Philipp Hanckec060ce42021-03-15 13:15:59 +0100172 return absl::GetFlag(FLAGS_ssrc_rtx);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200173}
174
175static int AbsSendTimeId() {
176 return absl::GetFlag(FLAGS_abs_send_time_id);
177}
178
179static int TransmissionOffsetId() {
180 return absl::GetFlag(FLAGS_transmission_offset_id);
181}
182
183static std::string InputFile() {
184 return absl::GetFlag(FLAGS_input_file);
185}
186
187static std::string ConfigFile() {
188 return absl::GetFlag(FLAGS_config_file);
189}
190
191static std::string OutBase() {
192 return absl::GetFlag(FLAGS_out_base);
193}
194
195static std::string DecoderBitstreamFilename() {
196 return absl::GetFlag(FLAGS_decoder_bitstream_filename);
197}
198
Philipp Hanckefae4fb12021-01-26 18:05:56 +0100199static std::string IVFFilename() {
200 return absl::GetFlag(FLAGS_decoder_ivf_filename);
201}
202
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200203static std::string Codec() {
204 return absl::GetFlag(FLAGS_codec);
205}
206
oprypin6e09d872017-08-31 03:21:39 -0700207} // namespace
208
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000209namespace webrtc {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000210
211static const uint32_t kReceiverLocalSsrc = 0x123456;
212
nisse7ade7b32016-03-23 04:48:10 -0700213class FileRenderPassthrough : public rtc::VideoSinkInterface<VideoFrame> {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000214 public:
nisse7ade7b32016-03-23 04:48:10 -0700215 FileRenderPassthrough(const std::string& basename,
216 rtc::VideoSinkInterface<VideoFrame>* renderer)
philipel99b63452017-08-25 07:24:21 -0700217 : basename_(basename), renderer_(renderer), file_(nullptr), count_(0) {}
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000218
Mirko Bonadeife055c12019-01-29 22:53:28 +0100219 ~FileRenderPassthrough() override {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200220 if (file_)
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000221 fclose(file_);
222 }
223
224 private:
nisseeb83a1a2016-03-21 01:27:56 -0700225 void OnFrame(const VideoFrame& video_frame) override {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200226 if (renderer_)
nisseeb83a1a2016-03-21 01:27:56 -0700227 renderer_->OnFrame(video_frame);
philipel99b63452017-08-25 07:24:21 -0700228
pbosbb36fdf2015-07-09 07:48:14 -0700229 if (basename_.empty())
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000230 return;
philipel99b63452017-08-25 07:24:21 -0700231
232 std::stringstream filename;
233 filename << basename_ << count_++ << "_" << video_frame.timestamp()
234 << ".jpg";
235
philipel99b63452017-08-25 07:24:21 -0700236 test::JpegFrameWriter frame_writer(filename.str());
237 RTC_CHECK(frame_writer.WriteFrame(video_frame, 100));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000238 }
239
240 const std::string basename_;
nisse7ade7b32016-03-23 04:48:10 -0700241 rtc::VideoSinkInterface<VideoFrame>* const renderer_;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000242 FILE* file_;
243 size_t count_;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000244};
245
Niels Möllerf88a22c2018-06-19 17:05:03 +0200246class DecoderBitstreamFileWriter : public test::FakeDecoder {
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000247 public:
248 explicit DecoderBitstreamFileWriter(const char* filename)
249 : file_(fopen(filename, "wb")) {
Peter Boström74f6e9e2016-04-04 17:56:10 +0200250 RTC_DCHECK(file_);
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000251 }
Mirko Bonadeife055c12019-01-29 22:53:28 +0100252 ~DecoderBitstreamFileWriter() override { fclose(file_); }
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000253
Niels Möllerf88a22c2018-06-19 17:05:03 +0200254 int32_t Decode(const EncodedImage& encoded_frame,
Benjamin Wright8efafdf2019-01-11 10:48:42 -0800255 bool /* missing_frames */,
Benjamin Wright8efafdf2019-01-11 10:48:42 -0800256 int64_t /* render_time_ms */) override {
Niels Möller77536a22019-01-15 08:50:01 +0100257 if (fwrite(encoded_frame.data(), 1, encoded_frame.size(), file_) <
258 encoded_frame.size()) {
Niels Möllerf88a22c2018-06-19 17:05:03 +0200259 RTC_LOG_ERR(LS_ERROR) << "fwrite of encoded frame failed.";
260 return WEBRTC_VIDEO_CODEC_ERROR;
261 }
262 return WEBRTC_VIDEO_CODEC_OK;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000263 }
264
265 private:
266 FILE* file_;
267};
268
Philipp Hanckefae4fb12021-01-26 18:05:56 +0100269class DecoderIvfFileWriter : public test::FakeDecoder {
270 public:
271 explicit DecoderIvfFileWriter(const char* filename, const std::string& codec)
272 : file_writer_(
273 IvfFileWriter::Wrap(FileWrapper::OpenWriteOnly(filename), 0)) {
274 RTC_DCHECK(file_writer_.get());
275 if (codec == "VP8") {
276 video_codec_type_ = VideoCodecType::kVideoCodecVP8;
277 } else if (codec == "VP9") {
278 video_codec_type_ = VideoCodecType::kVideoCodecVP9;
279 } else if (codec == "H264") {
280 video_codec_type_ = VideoCodecType::kVideoCodecH264;
281 } else {
282 RTC_LOG(LS_ERROR) << "Unsupported video codec " << codec;
283 RTC_DCHECK(false);
284 }
285 }
286 ~DecoderIvfFileWriter() override { file_writer_->Close(); }
287
288 int32_t Decode(const EncodedImage& encoded_frame,
289 bool /* missing_frames */,
290 int64_t render_time_ms) override {
291 if (!file_writer_->WriteFrame(encoded_frame, video_codec_type_)) {
292 return WEBRTC_VIDEO_CODEC_ERROR;
293 }
294 return WEBRTC_VIDEO_CODEC_OK;
295 }
296
297 private:
298 std::unique_ptr<IvfFileWriter> file_writer_;
299 VideoCodecType video_codec_type_;
300};
301
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700302// The RtpReplayer is responsible for parsing the configuration provided by the
303// user, setting up the windows, recieve streams and decoders and then replaying
304// the provided RTP dump.
305class RtpReplayer final {
306 public:
307 // Replay a rtp dump with an optional json configuration.
308 static void Replay(const std::string& replay_config_path,
309 const std::string& rtp_dump_path) {
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200310 std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory =
311 webrtc::CreateDefaultTaskQueueFactory();
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100312 auto worker_thread = task_queue_factory->CreateTaskQueue(
313 "worker_thread", TaskQueueFactory::Priority::NORMAL);
314 rtc::Event sync_event(/*manual_reset=*/false,
315 /*initially_signalled=*/false);
Danil Chapovalov83bbe912019-08-07 12:24:53 +0200316 webrtc::RtcEventLogNull event_log;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700317 Call::Config call_config(&event_log);
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200318 call_config.task_queue_factory = task_queue_factory.get();
Ilya Nikolaevskiy33aaa352020-01-21 13:38:19 +0100319 call_config.trials = new FieldTrialBasedConfig();
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100320 std::unique_ptr<Call> call;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700321 std::unique_ptr<StreamState> stream_state;
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100322
323 // Creation of the streams must happen inside a task queue because it is
324 // resued as a worker thread.
325 worker_thread->PostTask(ToQueuedTask([&]() {
326 call.reset(Call::Create(call_config));
327
328 // Attempt to load the configuration
329 if (replay_config_path.empty()) {
330 stream_state = ConfigureFromFlags(rtp_dump_path, call.get());
331 } else {
332 stream_state = ConfigureFromFile(replay_config_path, call.get());
333 }
334
335 if (stream_state == nullptr) {
336 return;
337 }
338 // Start replaying the provided stream now that it has been configured.
339 // VideoReceiveStreams must be started on the same thread as they were
340 // created on.
341 for (const auto& receive_stream : stream_state->receive_streams) {
342 receive_stream->Start();
343 }
344 sync_event.Set();
345 }));
346
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700347 // Attempt to create an RtpReader from the input file.
348 std::unique_ptr<test::RtpFileReader> rtp_reader =
349 CreateRtpReader(rtp_dump_path);
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100350
351 // Wait for streams creation.
352 sync_event.Wait(/*give_up_after_ms=*/10000);
353
354 if (stream_state == nullptr || rtp_reader == nullptr) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700355 return;
356 }
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100357
358 ReplayPackets(call.get(), rtp_reader.get(), worker_thread.get());
359
360 // Destruction of streams and the call must happen on the same thread as
361 // their creation.
362 worker_thread->PostTask(ToQueuedTask([&]() {
363 for (const auto& receive_stream : stream_state->receive_streams) {
364 call->DestroyVideoReceiveStream(receive_stream);
365 }
366 call.reset();
367 sync_event.Set();
368 }));
369 sync_event.Wait(/*give_up_after_ms=*/10000);
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000370 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000371
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700372 private:
373 // Holds all the shared memory structures required for a recieve stream. This
374 // structure is used to prevent members being deallocated before the replay
375 // has been finished.
376 struct StreamState {
377 test::NullTransport transport;
378 std::vector<std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>>> sinks;
379 std::vector<VideoReceiveStream*> receive_streams;
Niels Möllercbcbc222018-09-28 09:07:24 +0200380 std::unique_ptr<VideoDecoderFactory> decoder_factory;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700381 };
382
383 // Loads multiple configurations from the provided configuration file.
384 static std::unique_ptr<StreamState> ConfigureFromFile(
385 const std::string& config_path,
386 Call* call) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200387 auto stream_state = std::make_unique<StreamState>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700388 // Parse the configuration file.
389 std::ifstream config_file(config_path);
390 std::stringstream raw_json_buffer;
391 raw_json_buffer << config_file.rdbuf();
392 std::string raw_json = raw_json_buffer.str();
Mirko Bonadeie99f6872021-06-24 15:24:59 +0200393 Json::CharReaderBuilder builder;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700394 Json::Value json_configs;
Mirko Bonadeie99f6872021-06-24 15:24:59 +0200395 std::string error_message;
396 std::unique_ptr<Json::CharReader> json_reader(builder.newCharReader());
397 if (!json_reader->parse(raw_json.data(), raw_json.data() + raw_json.size(),
398 &json_configs, &error_message)) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700399 fprintf(stderr, "Error parsing JSON config\n");
Mirko Bonadeie99f6872021-06-24 15:24:59 +0200400 fprintf(stderr, "%s\n", error_message.c_str());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700401 return nullptr;
402 }
403
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200404 stream_state->decoder_factory = std::make_unique<InternalDecoderFactory>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700405 size_t config_count = 0;
406 for (const auto& json : json_configs) {
407 // Create the configuration and parse the JSON into the config.
Benjamin Wright8efafdf2019-01-11 10:48:42 -0800408 auto receive_config =
409 ParseVideoReceiveStreamJsonConfig(&(stream_state->transport), json);
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700410 // Instantiate the underlying decoder.
411 for (auto& decoder : receive_config.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200412 decoder = test::CreateMatchingDecoder(decoder.payload_type,
413 decoder.video_format.name);
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700414 }
415 // Create a window for this config.
416 std::stringstream window_title;
417 window_title << "Playback Video (" << config_count++ << ")";
418 stream_state->sinks.emplace_back(
419 test::VideoRenderer::Create(window_title.str().c_str(), 640, 480));
420 // Create a receive stream for this config.
421 receive_config.renderer = stream_state->sinks.back().get();
Philip Eliasson2b068ce2020-08-03 15:55:10 +0000422 receive_config.decoder_factory = stream_state->decoder_factory.get();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700423 stream_state->receive_streams.emplace_back(
424 call->CreateVideoReceiveStream(std::move(receive_config)));
425 }
426 return stream_state;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000427 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000428
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700429 // Loads the base configuration from flags passed in on the commandline.
430 static std::unique_ptr<StreamState> ConfigureFromFlags(
431 const std::string& rtp_dump_path,
432 Call* call) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200433 auto stream_state = std::make_unique<StreamState>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700434 // Create the video renderers. We must add both to the stream state to keep
435 // them from deallocating.
436 std::stringstream window_title;
437 window_title << "Playback Video (" << rtp_dump_path << ")";
438 std::unique_ptr<test::VideoRenderer> playback_video(
439 test::VideoRenderer::Create(window_title.str().c_str(), 640, 480));
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200440 auto file_passthrough = std::make_unique<FileRenderPassthrough>(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200441 OutBase(), playback_video.get());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700442 stream_state->sinks.push_back(std::move(playback_video));
443 stream_state->sinks.push_back(std::move(file_passthrough));
444 // Setup the configuration from the flags.
445 VideoReceiveStream::Config receive_config(&(stream_state->transport));
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200446 receive_config.rtp.remote_ssrc = Ssrc();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700447 receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200448 receive_config.rtp.rtx_ssrc = SsrcRtx();
449 receive_config.rtp.rtx_associated_payload_types[MediaPayloadTypeRtx()] =
450 MediaPayloadType();
451 receive_config.rtp.rtx_associated_payload_types[RedPayloadTypeRtx()] =
452 RedPayloadType();
453 receive_config.rtp.ulpfec_payload_type = UlpfecPayloadType();
454 receive_config.rtp.red_payload_type = RedPayloadType();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700455 receive_config.rtp.nack.rtp_history_ms = 1000;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200456 if (TransmissionOffsetId() != -1) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700457 receive_config.rtp.extensions.push_back(RtpExtension(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200458 RtpExtension::kTimestampOffsetUri, TransmissionOffsetId()));
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700459 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200460 if (AbsSendTimeId() != -1) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700461 receive_config.rtp.extensions.push_back(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200462 RtpExtension(RtpExtension::kAbsSendTimeUri, AbsSendTimeId()));
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700463 }
464 receive_config.renderer = stream_state->sinks.back().get();
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000465
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700466 // Setup the receiving stream
467 VideoReceiveStream::Decoder decoder;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200468 decoder = test::CreateMatchingDecoder(MediaPayloadType(), Codec());
Philipp Hanckefae4fb12021-01-26 18:05:56 +0100469 if (!DecoderBitstreamFilename().empty()) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700470 // Replace decoder with file writer if we're writing the bitstream to a
471 // file instead.
Niels Möllercbcbc222018-09-28 09:07:24 +0200472 stream_state->decoder_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200473 std::make_unique<test::FunctionVideoDecoderFactory>([]() {
474 return std::make_unique<DecoderBitstreamFileWriter>(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200475 DecoderBitstreamFilename().c_str());
Niels Möllercbcbc222018-09-28 09:07:24 +0200476 });
Philipp Hanckefae4fb12021-01-26 18:05:56 +0100477 } else if (!IVFFilename().empty()) {
478 // Replace decoder with file writer if we're writing the ivf to a
479 // file instead.
480 stream_state->decoder_factory =
481 std::make_unique<test::FunctionVideoDecoderFactory>([]() {
482 return std::make_unique<DecoderIvfFileWriter>(IVFFilename().c_str(),
483 Codec());
484 });
485 } else {
486 stream_state->decoder_factory =
487 std::make_unique<InternalDecoderFactory>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700488 }
Philip Eliasson2b068ce2020-08-03 15:55:10 +0000489 receive_config.decoder_factory = stream_state->decoder_factory.get();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700490 receive_config.decoders.push_back(decoder);
491
492 stream_state->receive_streams.emplace_back(
493 call->CreateVideoReceiveStream(std::move(receive_config)));
494 return stream_state;
495 }
496
497 static std::unique_ptr<test::RtpFileReader> CreateRtpReader(
498 const std::string& rtp_dump_path) {
499 std::unique_ptr<test::RtpFileReader> rtp_reader(test::RtpFileReader::Create(
500 test::RtpFileReader::kRtpDump, rtp_dump_path));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200501 if (!rtp_reader) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700502 rtp_reader.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap,
503 rtp_dump_path));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200504 if (!rtp_reader) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700505 fprintf(
506 stderr,
507 "Couldn't open input file as either a rtpdump or .pcap. Note "
508 "that .pcapng is not supported.\nTrying to interpret the file as "
509 "length/packet interleaved.\n");
510 rtp_reader.reset(test::RtpFileReader::Create(
511 test::RtpFileReader::kLengthPacketInterleaved, rtp_dump_path));
512 if (!rtp_reader) {
513 fprintf(stderr,
514 "Unable to open input file with any supported format\n");
515 return nullptr;
516 }
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000517 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000518 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700519 return rtp_reader;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000520 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000521
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100522 static void ReplayPackets(Call* call,
523 test::RtpFileReader* rtp_reader,
524 TaskQueueBase* worker_thread) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700525 int64_t replay_start_ms = -1;
526 int num_packets = 0;
527 std::map<uint32_t, int> unknown_packets;
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100528 rtc::Event event(/*manual_reset=*/false, /*initially_signalled=*/false);
Philipp Hanckef3a687a2021-03-15 12:04:39 +0100529 uint32_t start_timestamp = absl::GetFlag(FLAGS_start_timestamp);
530 uint32_t stop_timestamp = absl::GetFlag(FLAGS_stop_timestamp);
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700531 while (true) {
532 int64_t now_ms = rtc::TimeMillis();
533 if (replay_start_ms == -1) {
534 replay_start_ms = now_ms;
535 }
philipel02f03962018-01-11 17:28:35 +0100536
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700537 test::RtpPacket packet;
538 if (!rtp_reader->NextPacket(&packet)) {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000539 break;
540 }
Danil Chapovalov76a35d92021-06-08 12:30:46 +0200541 rtc::CopyOnWriteBuffer packet_buffer(packet.data, packet.length);
542 RtpPacket header;
543 header.Parse(packet_buffer);
544 if (header.Timestamp() < start_timestamp ||
545 header.Timestamp() > stop_timestamp) {
Philipp Hanckef3a687a2021-03-15 12:04:39 +0100546 continue;
547 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700548
549 int64_t deliver_in_ms = replay_start_ms + packet.time_ms - now_ms;
550 if (deliver_in_ms > 0) {
551 SleepMs(deliver_in_ms);
552 }
553
554 ++num_packets;
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100555 PacketReceiver::DeliveryStatus result = PacketReceiver::DELIVERY_OK;
556 worker_thread->PostTask(ToQueuedTask([&]() {
Danil Chapovalov76a35d92021-06-08 12:30:46 +0200557 result = call->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO,
558 std::move(packet_buffer),
559 /* packet_time_us */ -1);
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100560 event.Set();
561 }));
562 event.Wait(/*give_up_after_ms=*/10000);
563 switch (result) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700564 case PacketReceiver::DELIVERY_OK:
565 break;
566 case PacketReceiver::DELIVERY_UNKNOWN_SSRC: {
Danil Chapovalov76a35d92021-06-08 12:30:46 +0200567 if (unknown_packets[header.Ssrc()] == 0)
568 fprintf(stderr, "Unknown SSRC: %u!\n", header.Ssrc());
569 ++unknown_packets[header.Ssrc()];
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700570 break;
571 }
572 case PacketReceiver::DELIVERY_PACKET_ERROR: {
573 fprintf(stderr,
574 "Packet error, corrupt packets or incorrect setup?\n");
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700575 fprintf(stderr, "Packet len=%zu pt=%u seq=%u ts=%u ssrc=0x%8x\n",
Danil Chapovalov76a35d92021-06-08 12:30:46 +0200576 packet.length, header.PayloadType(), header.SequenceNumber(),
577 header.Timestamp(), header.Ssrc());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700578 break;
579 }
philipp.hancke7b589602017-01-26 04:54:04 -0800580 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000581 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700582 fprintf(stderr, "num_packets: %d\n", num_packets);
583
584 for (std::map<uint32_t, int>::const_iterator it = unknown_packets.begin();
585 it != unknown_packets.end(); ++it) {
586 fprintf(stderr, "Packets for unknown ssrc '%u': %d\n", it->first,
587 it->second);
588 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000589 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700590}; // class RtpReplayer
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000591
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700592void RtpReplay() {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200593 RtpReplayer::Replay(ConfigFile(), InputFile());
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000594}
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700595
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000596} // namespace webrtc
597
598int main(int argc, char* argv[]) {
599 ::testing::InitGoogleTest(&argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200600 absl::ParseCommandLine(argc, argv);
oprypin6e09d872017-08-31 03:21:39 -0700601
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200602 RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type)));
603 RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type_rtx)));
604 RTC_CHECK(ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type)));
philipel752968e2017-12-05 12:40:28 +0100605 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200606 ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type_rtx)));
philipel752968e2017-12-05 12:40:28 +0100607 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200608 ValidateOptionalPayloadType(absl::GetFlag(FLAGS_ulpfec_payload_type)));
Yves Gerey665174f2018-06-19 15:03:05 +0200609 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200610 ValidateRtpHeaderExtensionId(absl::GetFlag(FLAGS_abs_send_time_id)));
611 RTC_CHECK(ValidateRtpHeaderExtensionId(
612 absl::GetFlag(FLAGS_transmission_offset_id)));
613 RTC_CHECK(ValidateInputFilenameNotEmpty(absl::GetFlag(FLAGS_input_file)));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000614
Philipp Hancke07b7dec2020-07-28 12:34:48 +0200615 rtc::ThreadManager::Instance()->WrapCurrentThread();
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000616 webrtc::test::RunTest(webrtc::RtpReplay);
617 return 0;
618}