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" |
Steve Anton | 40d5533 | 2019-01-07 10:21:47 -0800 | [diff] [blame] | 19 | #include "absl/memory/memory.h" |
Danil Chapovalov | 99b71df | 2018-10-26 15:57:48 +0200 | [diff] [blame] | 20 | #include "api/test/video/function_video_decoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "api/video_codecs/video_decoder.h" |
| 22 | #include "call/call.h" |
| 23 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
| 24 | #include "logging/rtc_event_log/rtc_event_log.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "media/engine/internal_decoder_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "modules/rtp_rtcp/include/rtp_header_parser.h" |
| 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" |
| 40 | #include "test/run_loop.h" |
| 41 | #include "test/run_test.h" |
Sebastian Jansson | f1f363f | 2018-08-13 14:24:58 +0200 | [diff] [blame] | 42 | #include "test/test_video_capturer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "test/testsupport/frame_writer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 44 | #include "test/video_renderer.h" |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 45 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 46 | // Flag for payload type. |
| 47 | ABSL_FLAG(int, |
| 48 | media_payload_type, |
| 49 | webrtc::test::CallTest::kPayloadTypeVP8, |
| 50 | "Media payload type"); |
| 51 | |
| 52 | // Flag for RED payload type. |
| 53 | ABSL_FLAG(int, |
| 54 | red_payload_type, |
| 55 | webrtc::test::CallTest::kRedPayloadType, |
| 56 | "RED payload type"); |
| 57 | |
| 58 | // Flag for ULPFEC payload type. |
| 59 | ABSL_FLAG(int, |
| 60 | ulpfec_payload_type, |
| 61 | webrtc::test::CallTest::kUlpfecPayloadType, |
| 62 | "ULPFEC payload type"); |
| 63 | |
| 64 | ABSL_FLAG(int, |
| 65 | media_payload_type_rtx, |
| 66 | webrtc::test::CallTest::kSendRtxPayloadType, |
| 67 | "Media over RTX payload type"); |
| 68 | |
| 69 | ABSL_FLAG(int, |
| 70 | red_payload_type_rtx, |
| 71 | webrtc::test::CallTest::kRtxRedPayloadType, |
| 72 | "RED over RTX payload type"); |
| 73 | |
| 74 | // Flag for SSRC. |
| 75 | const std::string& DefaultSsrc() { |
| 76 | static const std::string ssrc = |
| 77 | std::to_string(webrtc::test::CallTest::kVideoSendSsrcs[0]); |
| 78 | return ssrc; |
| 79 | } |
| 80 | ABSL_FLAG(std::string, ssrc, DefaultSsrc().c_str(), "Incoming SSRC"); |
| 81 | |
| 82 | const std::string& DefaultSsrcRtx() { |
| 83 | static const std::string ssrc_rtx = |
| 84 | std::to_string(webrtc::test::CallTest::kSendRtxSsrcs[0]); |
| 85 | return ssrc_rtx; |
| 86 | } |
| 87 | ABSL_FLAG(std::string, ssrc_rtx, DefaultSsrcRtx().c_str(), "Incoming RTX SSRC"); |
| 88 | |
| 89 | // Flag for abs-send-time id. |
| 90 | ABSL_FLAG(int, abs_send_time_id, -1, "RTP extension ID for abs-send-time"); |
| 91 | |
| 92 | // Flag for transmission-offset id. |
| 93 | ABSL_FLAG(int, |
| 94 | transmission_offset_id, |
| 95 | -1, |
| 96 | "RTP extension ID for transmission-offset"); |
| 97 | |
| 98 | // Flag for rtpdump input file. |
| 99 | ABSL_FLAG(std::string, input_file, "", "input file"); |
| 100 | |
| 101 | ABSL_FLAG(std::string, config_file, "", "config file"); |
| 102 | |
| 103 | // Flag for raw output files. |
| 104 | ABSL_FLAG(std::string, |
| 105 | out_base, |
| 106 | "", |
| 107 | "Basename (excluding .jpg) for raw output"); |
| 108 | |
| 109 | ABSL_FLAG(std::string, |
| 110 | decoder_bitstream_filename, |
| 111 | "", |
| 112 | "Decoder bitstream output file"); |
| 113 | |
| 114 | // Flag for video codec. |
| 115 | ABSL_FLAG(std::string, codec, "VP8", "Video codec"); |
| 116 | |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 117 | namespace { |
| 118 | |
| 119 | static bool ValidatePayloadType(int32_t payload_type) { |
| 120 | return payload_type > 0 && payload_type <= 127; |
| 121 | } |
| 122 | |
| 123 | static bool ValidateSsrc(const char* ssrc_string) { |
| 124 | return rtc::StringToNumber<uint32_t>(ssrc_string).has_value(); |
| 125 | } |
| 126 | |
| 127 | static bool ValidateOptionalPayloadType(int32_t payload_type) { |
| 128 | return payload_type == -1 || ValidatePayloadType(payload_type); |
| 129 | } |
| 130 | |
| 131 | static bool ValidateRtpHeaderExtensionId(int32_t extension_id) { |
| 132 | return extension_id >= -1 && extension_id < 15; |
| 133 | } |
| 134 | |
| 135 | bool ValidateInputFilenameNotEmpty(const std::string& string) { |
| 136 | return !string.empty(); |
| 137 | } |
| 138 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 139 | static int MediaPayloadType() { |
| 140 | return absl::GetFlag(FLAGS_media_payload_type); |
| 141 | } |
| 142 | |
| 143 | static int RedPayloadType() { |
| 144 | return absl::GetFlag(FLAGS_red_payload_type); |
| 145 | } |
| 146 | |
| 147 | static int UlpfecPayloadType() { |
| 148 | return absl::GetFlag(FLAGS_ulpfec_payload_type); |
| 149 | } |
| 150 | |
| 151 | static int MediaPayloadTypeRtx() { |
| 152 | return absl::GetFlag(FLAGS_media_payload_type_rtx); |
| 153 | } |
| 154 | |
| 155 | static int RedPayloadTypeRtx() { |
| 156 | return absl::GetFlag(FLAGS_red_payload_type_rtx); |
| 157 | } |
| 158 | |
| 159 | static uint32_t Ssrc() { |
| 160 | return rtc::StringToNumber<uint32_t>(absl::GetFlag(FLAGS_ssrc)).value(); |
| 161 | } |
| 162 | |
| 163 | static uint32_t SsrcRtx() { |
| 164 | return rtc::StringToNumber<uint32_t>(absl::GetFlag(FLAGS_ssrc_rtx)).value(); |
| 165 | } |
| 166 | |
| 167 | static int AbsSendTimeId() { |
| 168 | return absl::GetFlag(FLAGS_abs_send_time_id); |
| 169 | } |
| 170 | |
| 171 | static int TransmissionOffsetId() { |
| 172 | return absl::GetFlag(FLAGS_transmission_offset_id); |
| 173 | } |
| 174 | |
| 175 | static std::string InputFile() { |
| 176 | return absl::GetFlag(FLAGS_input_file); |
| 177 | } |
| 178 | |
| 179 | static std::string ConfigFile() { |
| 180 | return absl::GetFlag(FLAGS_config_file); |
| 181 | } |
| 182 | |
| 183 | static std::string OutBase() { |
| 184 | return absl::GetFlag(FLAGS_out_base); |
| 185 | } |
| 186 | |
| 187 | static std::string DecoderBitstreamFilename() { |
| 188 | return absl::GetFlag(FLAGS_decoder_bitstream_filename); |
| 189 | } |
| 190 | |
| 191 | static std::string Codec() { |
| 192 | return absl::GetFlag(FLAGS_codec); |
| 193 | } |
| 194 | |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 195 | } // namespace |
| 196 | |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 197 | namespace webrtc { |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 198 | |
| 199 | static const uint32_t kReceiverLocalSsrc = 0x123456; |
| 200 | |
nisse | 7ade7b3 | 2016-03-23 04:48:10 -0700 | [diff] [blame] | 201 | class FileRenderPassthrough : public rtc::VideoSinkInterface<VideoFrame> { |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 202 | public: |
nisse | 7ade7b3 | 2016-03-23 04:48:10 -0700 | [diff] [blame] | 203 | FileRenderPassthrough(const std::string& basename, |
| 204 | rtc::VideoSinkInterface<VideoFrame>* renderer) |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 205 | : basename_(basename), renderer_(renderer), file_(nullptr), count_(0) {} |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 206 | |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 207 | ~FileRenderPassthrough() override { |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 208 | if (file_) |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 209 | fclose(file_); |
| 210 | } |
| 211 | |
| 212 | private: |
nisse | eb83a1a | 2016-03-21 01:27:56 -0700 | [diff] [blame] | 213 | void OnFrame(const VideoFrame& video_frame) override { |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 214 | if (renderer_) |
nisse | eb83a1a | 2016-03-21 01:27:56 -0700 | [diff] [blame] | 215 | renderer_->OnFrame(video_frame); |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 216 | |
pbos | bb36fdf | 2015-07-09 07:48:14 -0700 | [diff] [blame] | 217 | if (basename_.empty()) |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 218 | return; |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 219 | |
| 220 | std::stringstream filename; |
| 221 | filename << basename_ << count_++ << "_" << video_frame.timestamp() |
| 222 | << ".jpg"; |
| 223 | |
philipel | 99b6345 | 2017-08-25 07:24:21 -0700 | [diff] [blame] | 224 | test::JpegFrameWriter frame_writer(filename.str()); |
| 225 | RTC_CHECK(frame_writer.WriteFrame(video_frame, 100)); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | const std::string basename_; |
nisse | 7ade7b3 | 2016-03-23 04:48:10 -0700 | [diff] [blame] | 229 | rtc::VideoSinkInterface<VideoFrame>* const renderer_; |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 230 | FILE* file_; |
| 231 | size_t count_; |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
Niels Möller | f88a22c | 2018-06-19 17:05:03 +0200 | [diff] [blame] | 234 | class DecoderBitstreamFileWriter : public test::FakeDecoder { |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 235 | public: |
| 236 | explicit DecoderBitstreamFileWriter(const char* filename) |
| 237 | : file_(fopen(filename, "wb")) { |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 238 | RTC_DCHECK(file_); |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 239 | } |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 240 | ~DecoderBitstreamFileWriter() override { fclose(file_); } |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 241 | |
Niels Möller | f88a22c | 2018-06-19 17:05:03 +0200 | [diff] [blame] | 242 | int32_t Decode(const EncodedImage& encoded_frame, |
Benjamin Wright | 8efafdf | 2019-01-11 10:48:42 -0800 | [diff] [blame] | 243 | bool /* missing_frames */, |
Benjamin Wright | 8efafdf | 2019-01-11 10:48:42 -0800 | [diff] [blame] | 244 | int64_t /* render_time_ms */) override { |
Niels Möller | 77536a2 | 2019-01-15 08:50:01 +0100 | [diff] [blame] | 245 | if (fwrite(encoded_frame.data(), 1, encoded_frame.size(), file_) < |
| 246 | encoded_frame.size()) { |
Niels Möller | f88a22c | 2018-06-19 17:05:03 +0200 | [diff] [blame] | 247 | RTC_LOG_ERR(LS_ERROR) << "fwrite of encoded frame failed."; |
| 248 | return WEBRTC_VIDEO_CODEC_ERROR; |
| 249 | } |
| 250 | return WEBRTC_VIDEO_CODEC_OK; |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | private: |
| 254 | FILE* file_; |
| 255 | }; |
| 256 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 257 | // The RtpReplayer is responsible for parsing the configuration provided by the |
| 258 | // user, setting up the windows, recieve streams and decoders and then replaying |
| 259 | // the provided RTP dump. |
| 260 | class RtpReplayer final { |
| 261 | public: |
| 262 | // Replay a rtp dump with an optional json configuration. |
| 263 | static void Replay(const std::string& replay_config_path, |
| 264 | const std::string& rtp_dump_path) { |
| 265 | webrtc::RtcEventLogNullImpl event_log; |
| 266 | Call::Config call_config(&event_log); |
Mirko Bonadei | 05cf6be | 2019-01-31 21:38:12 +0100 | [diff] [blame] | 267 | std::unique_ptr<Call> call(Call::Create(call_config)); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 268 | std::unique_ptr<StreamState> stream_state; |
| 269 | // Attempt to load the configuration |
| 270 | if (replay_config_path.empty()) { |
| 271 | stream_state = ConfigureFromFlags(rtp_dump_path, call.get()); |
| 272 | } else { |
| 273 | stream_state = ConfigureFromFile(replay_config_path, call.get()); |
| 274 | } |
| 275 | if (stream_state == nullptr) { |
| 276 | return; |
| 277 | } |
| 278 | // Attempt to create an RtpReader from the input file. |
| 279 | std::unique_ptr<test::RtpFileReader> rtp_reader = |
| 280 | CreateRtpReader(rtp_dump_path); |
| 281 | if (rtp_reader == nullptr) { |
| 282 | return; |
| 283 | } |
| 284 | // Start replaying the provided stream now that it has been configured. |
| 285 | for (const auto& receive_stream : stream_state->receive_streams) { |
| 286 | receive_stream->Start(); |
| 287 | } |
| 288 | ReplayPackets(call.get(), rtp_reader.get()); |
| 289 | for (const auto& receive_stream : stream_state->receive_streams) { |
| 290 | call->DestroyVideoReceiveStream(receive_stream); |
| 291 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 292 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 293 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 294 | private: |
| 295 | // Holds all the shared memory structures required for a recieve stream. This |
| 296 | // structure is used to prevent members being deallocated before the replay |
| 297 | // has been finished. |
| 298 | struct StreamState { |
| 299 | test::NullTransport transport; |
| 300 | std::vector<std::unique_ptr<rtc::VideoSinkInterface<VideoFrame>>> sinks; |
| 301 | std::vector<VideoReceiveStream*> receive_streams; |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 302 | std::unique_ptr<VideoDecoderFactory> decoder_factory; |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | // Loads multiple configurations from the provided configuration file. |
| 306 | static std::unique_ptr<StreamState> ConfigureFromFile( |
| 307 | const std::string& config_path, |
| 308 | Call* call) { |
| 309 | auto stream_state = absl::make_unique<StreamState>(); |
| 310 | // Parse the configuration file. |
| 311 | std::ifstream config_file(config_path); |
| 312 | std::stringstream raw_json_buffer; |
| 313 | raw_json_buffer << config_file.rdbuf(); |
| 314 | std::string raw_json = raw_json_buffer.str(); |
| 315 | Json::Reader json_reader; |
| 316 | Json::Value json_configs; |
| 317 | if (!json_reader.parse(raw_json, json_configs)) { |
| 318 | fprintf(stderr, "Error parsing JSON config\n"); |
| 319 | fprintf(stderr, "%s\n", json_reader.getFormatedErrorMessages().c_str()); |
| 320 | return nullptr; |
| 321 | } |
| 322 | |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 323 | stream_state->decoder_factory = absl::make_unique<InternalDecoderFactory>(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 324 | size_t config_count = 0; |
| 325 | for (const auto& json : json_configs) { |
| 326 | // Create the configuration and parse the JSON into the config. |
Benjamin Wright | 8efafdf | 2019-01-11 10:48:42 -0800 | [diff] [blame] | 327 | auto receive_config = |
| 328 | ParseVideoReceiveStreamJsonConfig(&(stream_state->transport), json); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 329 | // Instantiate the underlying decoder. |
| 330 | for (auto& decoder : receive_config.decoders) { |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 331 | decoder = test::CreateMatchingDecoder(decoder.payload_type, |
| 332 | decoder.video_format.name); |
| 333 | decoder.decoder_factory = stream_state->decoder_factory.get(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 334 | } |
| 335 | // Create a window for this config. |
| 336 | std::stringstream window_title; |
| 337 | window_title << "Playback Video (" << config_count++ << ")"; |
| 338 | stream_state->sinks.emplace_back( |
| 339 | test::VideoRenderer::Create(window_title.str().c_str(), 640, 480)); |
| 340 | // Create a receive stream for this config. |
| 341 | receive_config.renderer = stream_state->sinks.back().get(); |
| 342 | stream_state->receive_streams.emplace_back( |
| 343 | call->CreateVideoReceiveStream(std::move(receive_config))); |
| 344 | } |
| 345 | return stream_state; |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 346 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 347 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 348 | // Loads the base configuration from flags passed in on the commandline. |
| 349 | static std::unique_ptr<StreamState> ConfigureFromFlags( |
| 350 | const std::string& rtp_dump_path, |
| 351 | Call* call) { |
| 352 | auto stream_state = absl::make_unique<StreamState>(); |
| 353 | // Create the video renderers. We must add both to the stream state to keep |
| 354 | // them from deallocating. |
| 355 | std::stringstream window_title; |
| 356 | window_title << "Playback Video (" << rtp_dump_path << ")"; |
| 357 | std::unique_ptr<test::VideoRenderer> playback_video( |
| 358 | test::VideoRenderer::Create(window_title.str().c_str(), 640, 480)); |
| 359 | auto file_passthrough = absl::make_unique<FileRenderPassthrough>( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 360 | OutBase(), playback_video.get()); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 361 | stream_state->sinks.push_back(std::move(playback_video)); |
| 362 | stream_state->sinks.push_back(std::move(file_passthrough)); |
| 363 | // Setup the configuration from the flags. |
| 364 | VideoReceiveStream::Config receive_config(&(stream_state->transport)); |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 365 | receive_config.rtp.remote_ssrc = Ssrc(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 366 | receive_config.rtp.local_ssrc = kReceiverLocalSsrc; |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 367 | receive_config.rtp.rtx_ssrc = SsrcRtx(); |
| 368 | receive_config.rtp.rtx_associated_payload_types[MediaPayloadTypeRtx()] = |
| 369 | MediaPayloadType(); |
| 370 | receive_config.rtp.rtx_associated_payload_types[RedPayloadTypeRtx()] = |
| 371 | RedPayloadType(); |
| 372 | receive_config.rtp.ulpfec_payload_type = UlpfecPayloadType(); |
| 373 | receive_config.rtp.red_payload_type = RedPayloadType(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 374 | receive_config.rtp.nack.rtp_history_ms = 1000; |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 375 | if (TransmissionOffsetId() != -1) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 376 | receive_config.rtp.extensions.push_back(RtpExtension( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 377 | RtpExtension::kTimestampOffsetUri, TransmissionOffsetId())); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 378 | } |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 379 | if (AbsSendTimeId() != -1) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 380 | receive_config.rtp.extensions.push_back( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 381 | RtpExtension(RtpExtension::kAbsSendTimeUri, AbsSendTimeId())); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 382 | } |
| 383 | receive_config.renderer = stream_state->sinks.back().get(); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 384 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 385 | // Setup the receiving stream |
| 386 | VideoReceiveStream::Decoder decoder; |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 387 | decoder = test::CreateMatchingDecoder(MediaPayloadType(), Codec()); |
| 388 | if (DecoderBitstreamFilename().empty()) { |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 389 | stream_state->decoder_factory = |
| 390 | absl::make_unique<InternalDecoderFactory>(); |
| 391 | } else { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 392 | // Replace decoder with file writer if we're writing the bitstream to a |
| 393 | // file instead. |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 394 | stream_state->decoder_factory = |
| 395 | absl::make_unique<test::FunctionVideoDecoderFactory>([]() { |
| 396 | return absl::make_unique<DecoderBitstreamFileWriter>( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 397 | DecoderBitstreamFilename().c_str()); |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 398 | }); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 399 | } |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 400 | decoder.decoder_factory = stream_state->decoder_factory.get(); |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 401 | receive_config.decoders.push_back(decoder); |
| 402 | |
| 403 | stream_state->receive_streams.emplace_back( |
| 404 | call->CreateVideoReceiveStream(std::move(receive_config))); |
| 405 | return stream_state; |
| 406 | } |
| 407 | |
| 408 | static std::unique_ptr<test::RtpFileReader> CreateRtpReader( |
| 409 | const std::string& rtp_dump_path) { |
| 410 | std::unique_ptr<test::RtpFileReader> rtp_reader(test::RtpFileReader::Create( |
| 411 | test::RtpFileReader::kRtpDump, rtp_dump_path)); |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 412 | if (!rtp_reader) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 413 | rtp_reader.reset(test::RtpFileReader::Create(test::RtpFileReader::kPcap, |
| 414 | rtp_dump_path)); |
Peter Boström | 74f6e9e | 2016-04-04 17:56:10 +0200 | [diff] [blame] | 415 | if (!rtp_reader) { |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 416 | fprintf( |
| 417 | stderr, |
| 418 | "Couldn't open input file as either a rtpdump or .pcap. Note " |
| 419 | "that .pcapng is not supported.\nTrying to interpret the file as " |
| 420 | "length/packet interleaved.\n"); |
| 421 | rtp_reader.reset(test::RtpFileReader::Create( |
| 422 | test::RtpFileReader::kLengthPacketInterleaved, rtp_dump_path)); |
| 423 | if (!rtp_reader) { |
| 424 | fprintf(stderr, |
| 425 | "Unable to open input file with any supported format\n"); |
| 426 | return nullptr; |
| 427 | } |
stefan@webrtc.org | 48ac226 | 2015-03-02 16:18:56 +0000 | [diff] [blame] | 428 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 429 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 430 | return rtp_reader; |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 431 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 432 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 433 | static void ReplayPackets(Call* call, test::RtpFileReader* rtp_reader) { |
| 434 | int64_t replay_start_ms = -1; |
| 435 | int num_packets = 0; |
| 436 | std::map<uint32_t, int> unknown_packets; |
| 437 | while (true) { |
| 438 | int64_t now_ms = rtc::TimeMillis(); |
| 439 | if (replay_start_ms == -1) { |
| 440 | replay_start_ms = now_ms; |
| 441 | } |
philipel | 02f0396 | 2018-01-11 17:28:35 +0100 | [diff] [blame] | 442 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 443 | test::RtpPacket packet; |
| 444 | if (!rtp_reader->NextPacket(&packet)) { |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 445 | break; |
| 446 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 447 | |
| 448 | int64_t deliver_in_ms = replay_start_ms + packet.time_ms - now_ms; |
| 449 | if (deliver_in_ms > 0) { |
| 450 | SleepMs(deliver_in_ms); |
| 451 | } |
| 452 | |
| 453 | ++num_packets; |
| 454 | switch (call->Receiver()->DeliverPacket( |
| 455 | webrtc::MediaType::VIDEO, |
| 456 | rtc::CopyOnWriteBuffer(packet.data, packet.length), |
| 457 | /* packet_time_us */ -1)) { |
| 458 | case PacketReceiver::DELIVERY_OK: |
| 459 | break; |
| 460 | case PacketReceiver::DELIVERY_UNKNOWN_SSRC: { |
| 461 | RTPHeader header; |
| 462 | std::unique_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create()); |
| 463 | parser->Parse(packet.data, packet.length, &header); |
| 464 | if (unknown_packets[header.ssrc] == 0) |
| 465 | fprintf(stderr, "Unknown SSRC: %u!\n", header.ssrc); |
| 466 | ++unknown_packets[header.ssrc]; |
| 467 | break; |
| 468 | } |
| 469 | case PacketReceiver::DELIVERY_PACKET_ERROR: { |
| 470 | fprintf(stderr, |
| 471 | "Packet error, corrupt packets or incorrect setup?\n"); |
| 472 | RTPHeader header; |
| 473 | std::unique_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create()); |
| 474 | parser->Parse(packet.data, packet.length, &header); |
| 475 | fprintf(stderr, "Packet len=%zu pt=%u seq=%u ts=%u ssrc=0x%8x\n", |
| 476 | packet.length, header.payloadType, header.sequenceNumber, |
| 477 | header.timestamp, header.ssrc); |
| 478 | break; |
| 479 | } |
philipp.hancke | 7b58960 | 2017-01-26 04:54:04 -0800 | [diff] [blame] | 480 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 481 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 482 | fprintf(stderr, "num_packets: %d\n", num_packets); |
| 483 | |
| 484 | for (std::map<uint32_t, int>::const_iterator it = unknown_packets.begin(); |
| 485 | it != unknown_packets.end(); ++it) { |
| 486 | fprintf(stderr, "Packets for unknown ssrc '%u': %d\n", it->first, |
| 487 | it->second); |
| 488 | } |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 489 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 490 | }; // class RtpReplayer |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 491 | |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 492 | void RtpReplay() { |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 493 | RtpReplayer::Replay(ConfigFile(), InputFile()); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 494 | } |
Benjamin Wright | 90ab76d | 2018-08-23 11:33:29 -0700 | [diff] [blame] | 495 | |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 496 | } // namespace webrtc |
| 497 | |
| 498 | int main(int argc, char* argv[]) { |
| 499 | ::testing::InitGoogleTest(&argc, argv); |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 500 | absl::ParseCommandLine(argc, argv); |
oprypin | 6e09d87 | 2017-08-31 03:21:39 -0700 | [diff] [blame] | 501 | |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 502 | RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type))); |
| 503 | RTC_CHECK(ValidatePayloadType(absl::GetFlag(FLAGS_media_payload_type_rtx))); |
| 504 | RTC_CHECK(ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type))); |
philipel | 752968e | 2017-12-05 12:40:28 +0100 | [diff] [blame] | 505 | RTC_CHECK( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 506 | ValidateOptionalPayloadType(absl::GetFlag(FLAGS_red_payload_type_rtx))); |
philipel | 752968e | 2017-12-05 12:40:28 +0100 | [diff] [blame] | 507 | RTC_CHECK( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 508 | ValidateOptionalPayloadType(absl::GetFlag(FLAGS_ulpfec_payload_type))); |
| 509 | RTC_CHECK(ValidateSsrc(absl::GetFlag(FLAGS_ssrc).c_str())); |
| 510 | RTC_CHECK(ValidateSsrc(absl::GetFlag(FLAGS_ssrc_rtx).c_str())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 511 | RTC_CHECK( |
Mirko Bonadei | 2ab97f6 | 2019-07-18 13:44:12 +0200 | [diff] [blame^] | 512 | ValidateRtpHeaderExtensionId(absl::GetFlag(FLAGS_abs_send_time_id))); |
| 513 | RTC_CHECK(ValidateRtpHeaderExtensionId( |
| 514 | absl::GetFlag(FLAGS_transmission_offset_id))); |
| 515 | RTC_CHECK(ValidateInputFilenameNotEmpty(absl::GetFlag(FLAGS_input_file))); |
pbos@webrtc.org | 4b5625e | 2014-08-06 16:26:56 +0000 | [diff] [blame] | 516 | |
| 517 | webrtc::test::RunTest(webrtc::RtpReplay); |
| 518 | return 0; |
| 519 | } |