blob: d17a57e826b16cb405fd83b9e6a8d041c9178825 [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();
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100268 auto worker_thread = task_queue_factory->CreateTaskQueue(
269 "worker_thread", TaskQueueFactory::Priority::NORMAL);
270 rtc::Event sync_event(/*manual_reset=*/false,
271 /*initially_signalled=*/false);
Danil Chapovalov83bbe912019-08-07 12:24:53 +0200272 webrtc::RtcEventLogNull event_log;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700273 Call::Config call_config(&event_log);
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200274 call_config.task_queue_factory = task_queue_factory.get();
Ilya Nikolaevskiy33aaa352020-01-21 13:38:19 +0100275 call_config.trials = new FieldTrialBasedConfig();
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100276 std::unique_ptr<Call> call;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700277 std::unique_ptr<StreamState> stream_state;
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100278
279 // Creation of the streams must happen inside a task queue because it is
280 // resued as a worker thread.
281 worker_thread->PostTask(ToQueuedTask([&]() {
282 call.reset(Call::Create(call_config));
283
284 // Attempt to load the configuration
285 if (replay_config_path.empty()) {
286 stream_state = ConfigureFromFlags(rtp_dump_path, call.get());
287 } else {
288 stream_state = ConfigureFromFile(replay_config_path, call.get());
289 }
290
291 if (stream_state == nullptr) {
292 return;
293 }
294 // Start replaying the provided stream now that it has been configured.
295 // VideoReceiveStreams must be started on the same thread as they were
296 // created on.
297 for (const auto& receive_stream : stream_state->receive_streams) {
298 receive_stream->Start();
299 }
300 sync_event.Set();
301 }));
302
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700303 // Attempt to create an RtpReader from the input file.
304 std::unique_ptr<test::RtpFileReader> rtp_reader =
305 CreateRtpReader(rtp_dump_path);
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100306
307 // Wait for streams creation.
308 sync_event.Wait(/*give_up_after_ms=*/10000);
309
310 if (stream_state == nullptr || rtp_reader == nullptr) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700311 return;
312 }
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100313
314 ReplayPackets(call.get(), rtp_reader.get(), worker_thread.get());
315
316 // Destruction of streams and the call must happen on the same thread as
317 // their creation.
318 worker_thread->PostTask(ToQueuedTask([&]() {
319 for (const auto& receive_stream : stream_state->receive_streams) {
320 call->DestroyVideoReceiveStream(receive_stream);
321 }
322 call.reset();
323 sync_event.Set();
324 }));
325 sync_event.Wait(/*give_up_after_ms=*/10000);
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000326 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000327
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700328 private:
329 // Holds all the shared memory structures required for a recieve stream. This
330 // structure is used to prevent members being deallocated before the replay
331 // has been finished.
332 struct StreamState {
333 test::NullTransport transport;
334 std::vector<std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>>> sinks;
335 std::vector<VideoReceiveStream*> receive_streams;
Niels Möllercbcbc222018-09-28 09:07:24 +0200336 std::unique_ptr<VideoDecoderFactory> decoder_factory;
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700337 };
338
339 // Loads multiple configurations from the provided configuration file.
340 static std::unique_ptr<StreamState> ConfigureFromFile(
341 const std::string& config_path,
342 Call* call) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200343 auto stream_state = std::make_unique<StreamState>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700344 // Parse the configuration file.
345 std::ifstream config_file(config_path);
346 std::stringstream raw_json_buffer;
347 raw_json_buffer << config_file.rdbuf();
348 std::string raw_json = raw_json_buffer.str();
349 Json::Reader json_reader;
350 Json::Value json_configs;
351 if (!json_reader.parse(raw_json, json_configs)) {
352 fprintf(stderr, "Error parsing JSON config\n");
353 fprintf(stderr, "%s\n", json_reader.getFormatedErrorMessages().c_str());
354 return nullptr;
355 }
356
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200357 stream_state->decoder_factory = std::make_unique<InternalDecoderFactory>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700358 size_t config_count = 0;
359 for (const auto& json : json_configs) {
360 // Create the configuration and parse the JSON into the config.
Benjamin Wright8efafdf2019-01-11 10:48:42 -0800361 auto receive_config =
362 ParseVideoReceiveStreamJsonConfig(&(stream_state->transport), json);
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700363 // Instantiate the underlying decoder.
364 for (auto& decoder : receive_config.decoders) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200365 decoder = test::CreateMatchingDecoder(decoder.payload_type,
366 decoder.video_format.name);
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700367 }
368 // Create a window for this config.
369 std::stringstream window_title;
370 window_title << "Playback Video (" << config_count++ << ")";
371 stream_state->sinks.emplace_back(
372 test::VideoRenderer::Create(window_title.str().c_str(), 640, 480));
373 // Create a receive stream for this config.
374 receive_config.renderer = stream_state->sinks.back().get();
Philip Eliasson2b068ce2020-08-03 15:55:10 +0000375 receive_config.decoder_factory = stream_state->decoder_factory.get();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700376 stream_state->receive_streams.emplace_back(
377 call->CreateVideoReceiveStream(std::move(receive_config)));
378 }
379 return stream_state;
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000380 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000381
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700382 // Loads the base configuration from flags passed in on the commandline.
383 static std::unique_ptr<StreamState> ConfigureFromFlags(
384 const std::string& rtp_dump_path,
385 Call* call) {
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200386 auto stream_state = std::make_unique<StreamState>();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700387 // Create the video renderers. We must add both to the stream state to keep
388 // them from deallocating.
389 std::stringstream window_title;
390 window_title << "Playback Video (" << rtp_dump_path << ")";
391 std::unique_ptr<test::VideoRenderer> playback_video(
392 test::VideoRenderer::Create(window_title.str().c_str(), 640, 480));
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200393 auto file_passthrough = std::make_unique<FileRenderPassthrough>(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200394 OutBase(), playback_video.get());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700395 stream_state->sinks.push_back(std::move(playback_video));
396 stream_state->sinks.push_back(std::move(file_passthrough));
397 // Setup the configuration from the flags.
398 VideoReceiveStream::Config receive_config(&(stream_state->transport));
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200399 receive_config.rtp.remote_ssrc = Ssrc();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700400 receive_config.rtp.local_ssrc = kReceiverLocalSsrc;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200401 receive_config.rtp.rtx_ssrc = SsrcRtx();
402 receive_config.rtp.rtx_associated_payload_types[MediaPayloadTypeRtx()] =
403 MediaPayloadType();
404 receive_config.rtp.rtx_associated_payload_types[RedPayloadTypeRtx()] =
405 RedPayloadType();
406 receive_config.rtp.ulpfec_payload_type = UlpfecPayloadType();
407 receive_config.rtp.red_payload_type = RedPayloadType();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700408 receive_config.rtp.nack.rtp_history_ms = 1000;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200409 if (TransmissionOffsetId() != -1) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700410 receive_config.rtp.extensions.push_back(RtpExtension(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200411 RtpExtension::kTimestampOffsetUri, TransmissionOffsetId()));
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700412 }
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200413 if (AbsSendTimeId() != -1) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700414 receive_config.rtp.extensions.push_back(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200415 RtpExtension(RtpExtension::kAbsSendTimeUri, AbsSendTimeId()));
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700416 }
417 receive_config.renderer = stream_state->sinks.back().get();
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000418
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700419 // Setup the receiving stream
420 VideoReceiveStream::Decoder decoder;
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200421 decoder = test::CreateMatchingDecoder(MediaPayloadType(), Codec());
422 if (DecoderBitstreamFilename().empty()) {
Niels Möllercbcbc222018-09-28 09:07:24 +0200423 stream_state->decoder_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200424 std::make_unique<InternalDecoderFactory>();
Niels Möllercbcbc222018-09-28 09:07:24 +0200425 } else {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700426 // Replace decoder with file writer if we're writing the bitstream to a
427 // file instead.
Niels Möllercbcbc222018-09-28 09:07:24 +0200428 stream_state->decoder_factory =
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200429 std::make_unique<test::FunctionVideoDecoderFactory>([]() {
430 return std::make_unique<DecoderBitstreamFileWriter>(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200431 DecoderBitstreamFilename().c_str());
Niels Möllercbcbc222018-09-28 09:07:24 +0200432 });
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700433 }
Philip Eliasson2b068ce2020-08-03 15:55:10 +0000434 receive_config.decoder_factory = stream_state->decoder_factory.get();
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700435 receive_config.decoders.push_back(decoder);
436
437 stream_state->receive_streams.emplace_back(
438 call->CreateVideoReceiveStream(std::move(receive_config)));
439 return stream_state;
440 }
441
442 static std::unique_ptr<test::RtpFileReader> CreateRtpReader(
443 const std::string& rtp_dump_path) {
444 std::unique_ptr<test::RtpFileReader> rtp_reader(test::RtpFileReader::Create(
445 test::RtpFileReader::kRtpDump, rtp_dump_path));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200446 if (!rtp_reader) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700447 rtp_reader.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap,
448 rtp_dump_path));
Peter Boström74f6e9e2016-04-04 17:56:10 +0200449 if (!rtp_reader) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700450 fprintf(
451 stderr,
452 "Couldn't open input file as either a rtpdump or .pcap. Note "
453 "that .pcapng is not supported.\nTrying to interpret the file as "
454 "length/packet interleaved.\n");
455 rtp_reader.reset(test::RtpFileReader::Create(
456 test::RtpFileReader::kLengthPacketInterleaved, rtp_dump_path));
457 if (!rtp_reader) {
458 fprintf(stderr,
459 "Unable to open input file with any supported format\n");
460 return nullptr;
461 }
stefan@webrtc.org48ac2262015-03-02 16:18:56 +0000462 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000463 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700464 return rtp_reader;
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000465 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000466
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100467 static void ReplayPackets(Call* call,
468 test::RtpFileReader* rtp_reader,
469 TaskQueueBase* worker_thread) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700470 int64_t replay_start_ms = -1;
471 int num_packets = 0;
472 std::map<uint32_t, int> unknown_packets;
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100473 rtc::Event event(/*manual_reset=*/false, /*initially_signalled=*/false);
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700474 while (true) {
475 int64_t now_ms = rtc::TimeMillis();
476 if (replay_start_ms == -1) {
477 replay_start_ms = now_ms;
478 }
philipel02f03962018-01-11 17:28:35 +0100479
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700480 test::RtpPacket packet;
481 if (!rtp_reader->NextPacket(&packet)) {
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000482 break;
483 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700484
485 int64_t deliver_in_ms = replay_start_ms + packet.time_ms - now_ms;
486 if (deliver_in_ms > 0) {
487 SleepMs(deliver_in_ms);
488 }
489
490 ++num_packets;
Ilya Nikolaevskiy6a646902020-12-11 11:07:01 +0100491 PacketReceiver::DeliveryStatus result = PacketReceiver::DELIVERY_OK;
492 worker_thread->PostTask(ToQueuedTask([&]() {
493 result = call->Receiver()->DeliverPacket(
494 webrtc::MediaType::VIDEO,
495 rtc::CopyOnWriteBuffer(packet.data, packet.length),
496 /* packet_time_us */ -1);
497 event.Set();
498 }));
499 event.Wait(/*give_up_after_ms=*/10000);
500 switch (result) {
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700501 case PacketReceiver::DELIVERY_OK:
502 break;
503 case PacketReceiver::DELIVERY_UNKNOWN_SSRC: {
504 RTPHeader header;
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200505 std::unique_ptr<RtpHeaderParser> parser(
506 RtpHeaderParser::CreateForTest());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700507 parser->Parse(packet.data, packet.length, &header);
508 if (unknown_packets[header.ssrc] == 0)
509 fprintf(stderr, "Unknown SSRC: %u!\n", header.ssrc);
510 ++unknown_packets[header.ssrc];
511 break;
512 }
513 case PacketReceiver::DELIVERY_PACKET_ERROR: {
514 fprintf(stderr,
515 "Packet error, corrupt packets or incorrect setup?\n");
516 RTPHeader header;
Sergey Silkin626f7ff2019-09-12 10:51:19 +0200517 std::unique_ptr<RtpHeaderParser> parser(
518 RtpHeaderParser::CreateForTest());
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700519 parser->Parse(packet.data, packet.length, &header);
520 fprintf(stderr, "Packet len=%zu pt=%u seq=%u ts=%u ssrc=0x%8x\n",
521 packet.length, header.payloadType, header.sequenceNumber,
522 header.timestamp, header.ssrc);
523 break;
524 }
philipp.hancke7b589602017-01-26 04:54:04 -0800525 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000526 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700527 fprintf(stderr, "num_packets: %d\n", num_packets);
528
529 for (std::map<uint32_t, int>::const_iterator it = unknown_packets.begin();
530 it != unknown_packets.end(); ++it) {
531 fprintf(stderr, "Packets for unknown ssrc '%u': %d\n", it->first,
532 it->second);
533 }
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000534 }
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700535}; // class RtpReplayer
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000536
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700537void RtpReplay() {
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200538 RtpReplayer::Replay(ConfigFile(), InputFile());
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000539}
Benjamin Wright90ab76d2018-08-23 11:33:29 -0700540
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000541} // namespace webrtc
542
543int main(int argc, char* argv[]) {
544 ::testing::InitGoogleTest(&argc, argv);
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200545 absl::ParseCommandLine(argc, argv);
oprypin6e09d872017-08-31 03:21:39 -0700546
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200547 RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type)));
548 RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type_rtx)));
549 RTC_CHECK(ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type)));
philipel752968e2017-12-05 12:40:28 +0100550 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200551 ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type_rtx)));
philipel752968e2017-12-05 12:40:28 +0100552 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200553 ValidateOptionalPayloadType(absl::GetFlag(FLAGS_ulpfec_payload_type)));
554 RTC_CHECK(ValidateSsrc(absl::GetFlag(FLAGS_ssrc).c_str()));
555 RTC_CHECK(ValidateSsrc(absl::GetFlag(FLAGS_ssrc_rtx).c_str()));
Yves Gerey665174f2018-06-19 15:03:05 +0200556 RTC_CHECK(
Mirko Bonadei2ab97f62019-07-18 13:44:12 +0200557 ValidateRtpHeaderExtensionId(absl::GetFlag(FLAGS_abs_send_time_id)));
558 RTC_CHECK(ValidateRtpHeaderExtensionId(
559 absl::GetFlag(FLAGS_transmission_offset_id)));
560 RTC_CHECK(ValidateInputFilenameNotEmpty(absl::GetFlag(FLAGS_input_file)));
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000561
Philipp Hancke07b7dec2020-07-28 12:34:48 +0200562 rtc::ThreadManager::Instance()->WrapCurrentThread();
pbos@webrtc.org4b5625e2014-08-06 16:26:56 +0000563 webrtc::test::RunTest(webrtc::RtpReplay);
564 return 0;
565}