pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 1 | /* |
| 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 Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 13 | #include <fstream> |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 14 | #include <map> |
kwiberg | 27f982b | 2016-03-01 11:52:33 -0800 | [diff] [blame] | 15 | #include <memory> |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 16 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 17 | #include "absl/flags/flag.h" |
| 18 | #include "absl/flags/parse.h" |
Danil Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 19 | #include "api/rtc_event_log/rtc_event_log.h" |
Sergey Silkin | 626f7ff | 2019-09-12 10:51:19 +0200 | [diff] [blame] | 20 | #include "api/task_queue/default_task_queue_factory.h" |
Danil Chapovalov | 99b71df | 2018-10-26 15:57:48 +0200 | [diff] [blame] | 21 | #include "api/test/video/function_video_decoder_factory.h" |
Ilya Nikolaevskiy | 33aaa35 | 2020-01-21 13:38:19 +0100 | [diff] [blame] | 22 | #include "api/transport/field_trial_based_config.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/video_codecs/video_decoder.h" |
| 24 | #include "call/call.h" |
| 25 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "media/engine/internal_decoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/string_to_number.h" |
Sam Zackrisson | b45bdb5 | 2018-10-02 16:25:59 +0200 | [diff] [blame] | 29 | #include "rtc_base/strings/json.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 30 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "system_wrappers/include/clock.h" |
| 32 | #include "system_wrappers/include/sleep.h" |
Benjamin Wright | 8efafdf | 2019-01-11 10:48:42 -0800 | [diff] [blame] | 33 | #include "test/call_config_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #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 Silkin | 626f7ff | 2019-09-12 10:51:19 +0200 | [diff] [blame] | 40 | #include "test/rtp_header_parser.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 41 | #include "test/run_loop.h" |
| 42 | #include "test/run_test.h" |
Sebastian Jansson | f1f363f | 2018-08-13 14:24:58 +0200 | [diff] [blame] | 43 | #include "test/test_video_capturer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 44 | #include "test/testsupport/frame_writer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 45 | #include "test/video_renderer.h" |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 46 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 47 | // Flag for payload type. |
| 48 | ABSL_FLAG(int, |
| 49 | media_payload_type, |
| 50 | webrtc::test::CallTest::kPayloadTypeVP8, |
| 51 | "Media payload type"); |
| 52 | |
| 53 | // Flag for RED payload type. |
| 54 | ABSL_FLAG(int, |
| 55 | red_payload_type, |
| 56 | webrtc::test::CallTest::kRedPayloadType, |
| 57 | "RED payload type"); |
| 58 | |
| 59 | // Flag for ULPFEC payload type. |
| 60 | ABSL_FLAG(int, |
| 61 | ulpfec_payload_type, |
| 62 | webrtc::test::CallTest::kUlpfecPayloadType, |
| 63 | "ULPFEC payload type"); |
| 64 | |
| 65 | ABSL_FLAG(int, |
| 66 | media_payload_type_rtx, |
| 67 | webrtc::test::CallTest::kSendRtxPayloadType, |
| 68 | "Media over RTX payload type"); |
| 69 | |
| 70 | ABSL_FLAG(int, |
| 71 | red_payload_type_rtx, |
| 72 | webrtc::test::CallTest::kRtxRedPayloadType, |
| 73 | "RED over RTX payload type"); |
| 74 | |
| 75 | // Flag for SSRC. |
| 76 | const std::string& DefaultSsrc() { |
| 77 | static const std::string ssrc = |
| 78 | std::to_string(webrtc::test::CallTest::kVideoSendSsrcs[0]); |
| 79 | return ssrc; |
| 80 | } |
| 81 | ABSL_FLAG(std::string, ssrc, DefaultSsrc().c_str(), "Incoming SSRC"); |
| 82 | |
| 83 | const std::string& DefaultSsrcRtx() { |
| 84 | static const std::string ssrc_rtx = |
| 85 | std::to_string(webrtc::test::CallTest::kSendRtxSsrcs[0]); |
| 86 | return ssrc_rtx; |
| 87 | } |
| 88 | ABSL_FLAG(std::string, ssrc_rtx, DefaultSsrcRtx().c_str(), "Incoming RTX SSRC"); |
| 89 | |
| 90 | // Flag for abs-send-time id. |
| 91 | ABSL_FLAG(int, abs_send_time_id, -1, "RTP extension ID for abs-send-time"); |
| 92 | |
| 93 | // Flag for transmission-offset id. |
| 94 | ABSL_FLAG(int, |
| 95 | transmission_offset_id, |
| 96 | -1, |
| 97 | "RTP extension ID for transmission-offset"); |
| 98 | |
| 99 | // Flag for rtpdump input file. |
| 100 | ABSL_FLAG(std::string, input_file, "", "input file"); |
| 101 | |
| 102 | ABSL_FLAG(std::string, config_file, "", "config file"); |
| 103 | |
| 104 | // Flag for raw output files. |
| 105 | ABSL_FLAG(std::string, |
| 106 | out_base, |
| 107 | "", |
| 108 | "Basename (excluding .jpg) for raw output"); |
| 109 | |
| 110 | ABSL_FLAG(std::string, |
| 111 | decoder_bitstream_filename, |
| 112 | "", |
| 113 | "Decoder bitstream output file"); |
| 114 | |
| 115 | // Flag for video codec. |
| 116 | ABSL_FLAG(std::string, codec, "VP8", "Video codec"); |
| 117 | |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 118 | namespace { |
| 119 | |
| 120 | static bool ValidatePayloadType(int32_t payload_type) { |
| 121 | return payload_type > 0 && payload_type <= 127; |
| 122 | } |
| 123 | |
| 124 | static bool ValidateSsrc(const char* ssrc_string) { |
| 125 | return rtc::StringToNumber<uint32_t>(ssrc_string).has_value(); |
| 126 | } |
| 127 | |
| 128 | static bool ValidateOptionalPayloadType(int32_t payload_type) { |
| 129 | return payload_type == -1 || ValidatePayloadType(payload_type); |
| 130 | } |
| 131 | |
| 132 | static bool ValidateRtpHeaderExtensionId(int32_t extension_id) { |
| 133 | return extension_id >= -1 && extension_id < 15; |
| 134 | } |
| 135 | |
| 136 | bool ValidateInputFilenameNotEmpty(const std::string& string) { |
| 137 | return !string.empty(); |
| 138 | } |
| 139 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 140 | static int MediaPayloadType() { |
| 141 | return absl::GetFlag(FLAGS_media_payload_type); |
| 142 | } |
| 143 | |
| 144 | static int RedPayloadType() { |
| 145 | return absl::GetFlag(FLAGS_red_payload_type); |
| 146 | } |
| 147 | |
| 148 | static int UlpfecPayloadType() { |
| 149 | return absl::GetFlag(FLAGS_ulpfec_payload_type); |
| 150 | } |
| 151 | |
| 152 | static int MediaPayloadTypeRtx() { |
| 153 | return absl::GetFlag(FLAGS_media_payload_type_rtx); |
| 154 | } |
| 155 | |
| 156 | static int RedPayloadTypeRtx() { |
| 157 | return absl::GetFlag(FLAGS_red_payload_type_rtx); |
| 158 | } |
| 159 | |
| 160 | static uint32_t Ssrc() { |
| 161 | return rtc::StringToNumber<uint32_t>(absl::GetFlag(FLAGS_ssrc)).value(); |
| 162 | } |
| 163 | |
| 164 | static uint32_t SsrcRtx() { |
| 165 | return rtc::StringToNumber<uint32_t>(absl::GetFlag(FLAGS_ssrc_rtx)).value(); |
| 166 | } |
| 167 | |
| 168 | static int AbsSendTimeId() { |
| 169 | return absl::GetFlag(FLAGS_abs_send_time_id); |
| 170 | } |
| 171 | |
| 172 | static int TransmissionOffsetId() { |
| 173 | return absl::GetFlag(FLAGS_transmission_offset_id); |
| 174 | } |
| 175 | |
| 176 | static std::string InputFile() { |
| 177 | return absl::GetFlag(FLAGS_input_file); |
| 178 | } |
| 179 | |
| 180 | static std::string ConfigFile() { |
| 181 | return absl::GetFlag(FLAGS_config_file); |
| 182 | } |
| 183 | |
| 184 | static std::string OutBase() { |
| 185 | return absl::GetFlag(FLAGS_out_base); |
| 186 | } |
| 187 | |
| 188 | static std::string DecoderBitstreamFilename() { |
| 189 | return absl::GetFlag(FLAGS_decoder_bitstream_filename); |
| 190 | } |
| 191 | |
| 192 | static std::string Codec() { |
| 193 | return absl::GetFlag(FLAGS_codec); |
| 194 | } |
| 195 | |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 196 | } // namespace |
| 197 | |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 198 | namespace webrtc { |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 199 | |
| 200 | static const uint32_t kReceiverLocalSsrc = 0x123456; |
| 201 | |
nisse | 7ade7b3 | 2016-03-23 04:48:10 -0700 | [diff] [blame] | 202 | class FileRenderPassthrough : public rtc::VideoSinkInterface<VideoFrame> { |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 203 | public: |
nisse | 7ade7b3 | 2016-03-23 04:48:10 -0700 | [diff] [blame] | 204 | FileRenderPassthrough(const std::string& basename, |
| 205 | rtc::VideoSinkInterface<VideoFrame>* renderer) |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 206 | : basename_(basename), renderer_(renderer), file_(nullptr), count_(0) {} |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 207 | |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 208 | ~FileRenderPassthrough() override { |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 209 | if (file_) |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 210 | fclose(file_); |
| 211 | } |
| 212 | |
| 213 | private: |
nisse | eb83a1a | 2016-03-21 01:27:56 -0700 | [diff] [blame] | 214 | void OnFrame(const VideoFrame& video_frame) override { |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 215 | if (renderer_) |
nisse | eb83a1a | 2016-03-21 01:27:56 -0700 | [diff] [blame] | 216 | renderer_->OnFrame(video_frame); |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 217 | |
pbos | bb36fdf | 2015-07-09 07:48:14 -0700 | [diff] [blame] | 218 | if (basename_.empty()) |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 219 | return; |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 220 | |
| 221 | std::stringstream filename; |
| 222 | filename << basename_ << count_++ << "_" << video_frame.timestamp() |
| 223 | << ".jpg"; |
| 224 | |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 225 | test::JpegFrameWriter frame_writer(filename.str()); |
| 226 | RTC_CHECK(frame_writer.WriteFrame(video_frame, 100)); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | const std::string basename_; |
nisse | 7ade7b3 | 2016-03-23 04:48:10 -0700 | [diff] [blame] | 230 | rtc::VideoSinkInterface<VideoFrame>* const renderer_; |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 231 | FILE* file_; |
| 232 | size_t count_; |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Niels Möller | f88a22c | 2018-06-19 17:05:03 +0200 | [diff] [blame] | 235 | class DecoderBitstreamFileWriter : public test::FakeDecoder { |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 236 | public: |
| 237 | explicit DecoderBitstreamFileWriter(const char* filename) |
| 238 | : file_(fopen(filename, "wb")) { |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 239 | RTC_DCHECK(file_); |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 240 | } |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 241 | ~DecoderBitstreamFileWriter() override { fclose(file_); } |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 242 | |
Niels Möller | f88a22c | 2018-06-19 17:05:03 +0200 | [diff] [blame] | 243 | int32_t Decode(const EncodedImage& encoded_frame, |
Benjamin Wright | 8efafdf | 2019-01-11 10:48:42 -0800 | [diff] [blame] | 244 | bool /* missing_frames */, |
Benjamin Wright | 8efafdf | 2019-01-11 10:48:42 -0800 | [diff] [blame] | 245 | int64_t /* render_time_ms */) override { |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 246 | if (fwrite(encoded_frame.data(), 1, encoded_frame.size(), file_) < |
| 247 | encoded_frame.size()) { |
Niels Möller | f88a22c | 2018-06-19 17:05:03 +0200 | [diff] [blame] | 248 | 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.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | private: |
| 255 | FILE* file_; |
| 256 | }; |
| 257 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 258 | // 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. |
| 261 | class 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 Silkin | 626f7ff | 2019-09-12 10:51:19 +0200 | [diff] [blame] | 266 | std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory = |
| 267 | webrtc::CreateDefaultTaskQueueFactory(); |
Ilya Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 268 | 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 Chapovalov | 83bbe91 | 2019-08-07 12:24:53 +0200 | [diff] [blame] | 272 | webrtc::RtcEventLogNull event_log; |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 273 | Call::Config call_config(&event_log); |
Sergey Silkin | 626f7ff | 2019-09-12 10:51:19 +0200 | [diff] [blame] | 274 | call_config.task_queue_factory = task_queue_factory.get(); |
Ilya Nikolaevskiy | 33aaa35 | 2020-01-21 13:38:19 +0100 | [diff] [blame] | 275 | call_config.trials = new FieldTrialBasedConfig(); |
Ilya Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 276 | std::unique_ptr<Call> call; |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 277 | std::unique_ptr<StreamState> stream_state; |
Ilya Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 278 | |
| 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 Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 303 | // Attempt to create an RtpReader from the input file. |
| 304 | std::unique_ptr<test::RtpFileReader> rtp_reader = |
| 305 | CreateRtpReader(rtp_dump_path); |
Ilya Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 306 | |
| 307 | // Wait for streams creation. |
| 308 | sync_event.Wait(/*give_up_after_ms=*/10000); |
| 309 | |
| 310 | if (stream_state == nullptr || rtp_reader == nullptr) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 311 | return; |
| 312 | } |
Ilya Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 313 | |
| 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.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 326 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 327 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 328 | 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öller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 336 | std::unique_ptr<VideoDecoderFactory> decoder_factory; |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 337 | }; |
| 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 Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 343 | auto stream_state = std::make_unique<StreamState>(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 344 | // 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 Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 357 | stream_state->decoder_factory = std::make_unique<InternalDecoderFactory>(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 358 | size_t config_count = 0; |
| 359 | for (const auto& json : json_configs) { |
| 360 | // Create the configuration and parse the JSON into the config. |
Benjamin Wright | 8efafdf | 2019-01-11 10:48:42 -0800 | [diff] [blame] | 361 | auto receive_config = |
| 362 | ParseVideoReceiveStreamJsonConfig(&(stream_state->transport), json); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 363 | // Instantiate the underlying decoder. |
| 364 | for (auto& decoder : receive_config.decoders) { |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 365 | decoder = test::CreateMatchingDecoder(decoder.payload_type, |
| 366 | decoder.video_format.name); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 367 | } |
| 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 Eliasson | 2b068ce | 2020-08-03 15:55:10 +0000 | [diff] [blame] | 375 | receive_config.decoder_factory = stream_state->decoder_factory.get(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 376 | stream_state->receive_streams.emplace_back( |
| 377 | call->CreateVideoReceiveStream(std::move(receive_config))); |
| 378 | } |
| 379 | return stream_state; |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 380 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 381 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 382 | // 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 Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 386 | auto stream_state = std::make_unique<StreamState>(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 387 | // 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 Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 393 | auto file_passthrough = std::make_unique<FileRenderPassthrough>( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 394 | OutBase(), playback_video.get()); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 395 | 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 Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 399 | receive_config.rtp.remote_ssrc = Ssrc(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 400 | receive_config.rtp.local_ssrc = kReceiverLocalSsrc; |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 401 | 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 Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 408 | receive_config.rtp.nack.rtp_history_ms = 1000; |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 409 | if (TransmissionOffsetId() != -1) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 410 | receive_config.rtp.extensions.push_back(RtpExtension( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 411 | RtpExtension::kTimestampOffsetUri, TransmissionOffsetId())); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 412 | } |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 413 | if (AbsSendTimeId() != -1) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 414 | receive_config.rtp.extensions.push_back( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 415 | RtpExtension(RtpExtension::kAbsSendTimeUri, AbsSendTimeId())); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 416 | } |
| 417 | receive_config.renderer = stream_state->sinks.back().get(); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 418 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 419 | // Setup the receiving stream |
| 420 | VideoReceiveStream::Decoder decoder; |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 421 | decoder = test::CreateMatchingDecoder(MediaPayloadType(), Codec()); |
| 422 | if (DecoderBitstreamFilename().empty()) { |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 423 | stream_state->decoder_factory = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 424 | std::make_unique<InternalDecoderFactory>(); |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 425 | } else { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 426 | // Replace decoder with file writer if we're writing the bitstream to a |
| 427 | // file instead. |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 428 | stream_state->decoder_factory = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 429 | std::make_unique<test::FunctionVideoDecoderFactory>([]() { |
| 430 | return std::make_unique<DecoderBitstreamFileWriter>( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 431 | DecoderBitstreamFilename().c_str()); |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 432 | }); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 433 | } |
Philip Eliasson | 2b068ce | 2020-08-03 15:55:10 +0000 | [diff] [blame] | 434 | receive_config.decoder_factory = stream_state->decoder_factory.get(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 435 | 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öm | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 446 | if (!rtp_reader) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 447 | rtp_reader.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap, |
| 448 | rtp_dump_path)); |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 449 | if (!rtp_reader) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 450 | 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.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 462 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 463 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 464 | return rtp_reader; |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 465 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 466 | |
Ilya Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 467 | static void ReplayPackets(Call* call, |
| 468 | test::RtpFileReader* rtp_reader, |
| 469 | TaskQueueBase* worker_thread) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 470 | int64_t replay_start_ms = -1; |
| 471 | int num_packets = 0; |
| 472 | std::map<uint32_t, int> unknown_packets; |
Ilya Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 473 | rtc::Event event(/*manual_reset=*/false, /*initially_signalled=*/false); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 474 | while (true) { |
| 475 | int64_t now_ms = rtc::TimeMillis(); |
| 476 | if (replay_start_ms == -1) { |
| 477 | replay_start_ms = now_ms; |
| 478 | } |
philipel | 02f0396 | 2018-01-11 17:28:35 +0100 | [diff] [blame] | 479 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 480 | test::RtpPacket packet; |
| 481 | if (!rtp_reader->NextPacket(&packet)) { |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 482 | break; |
| 483 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 484 | |
| 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 Nikolaevskiy | 6a64690 | 2020-12-11 11:07:01 +0100 | [diff] [blame^] | 491 | 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 Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 501 | case PacketReceiver::DELIVERY_OK: |
| 502 | break; |
| 503 | case PacketReceiver::DELIVERY_UNKNOWN_SSRC: { |
| 504 | RTPHeader header; |
Sergey Silkin | 626f7ff | 2019-09-12 10:51:19 +0200 | [diff] [blame] | 505 | std::unique_ptr<RtpHeaderParser> parser( |
| 506 | RtpHeaderParser::CreateForTest()); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 507 | 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 Silkin | 626f7ff | 2019-09-12 10:51:19 +0200 | [diff] [blame] | 517 | std::unique_ptr<RtpHeaderParser> parser( |
| 518 | RtpHeaderParser::CreateForTest()); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 519 | 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.hancke | 7b58960 | 2017-01-26 04:54:04 -0800 | [diff] [blame] | 525 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 526 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 527 | 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.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 534 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 535 | }; // class RtpReplayer |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 536 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 537 | void RtpReplay() { |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 538 | RtpReplayer::Replay(ConfigFile(), InputFile()); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 539 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 540 | |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 541 | } // namespace webrtc |
| 542 | |
| 543 | int main(int argc, char* argv[]) { |
| 544 | ::testing::InitGoogleTest(&argc, argv); |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 545 | absl::ParseCommandLine(argc, argv); |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 546 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 547 | 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))); |
philipel | 752968e | 2017-12-05 12:40:28 +0100 | [diff] [blame] | 550 | RTC_CHECK( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 551 | ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type_rtx))); |
philipel | 752968e | 2017-12-05 12:40:28 +0100 | [diff] [blame] | 552 | RTC_CHECK( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 553 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 556 | RTC_CHECK( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame] | 557 | 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.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 561 | |
Philipp Hancke | 07b7dec | 2020-07-28 12:34:48 +0200 | [diff] [blame] | 562 | rtc::ThreadManager::Instance()->WrapCurrentThread(); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 563 | webrtc::test::RunTest(webrtc::RtpReplay); |
| 564 | return 0; |
| 565 | } |