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