You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2019 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. #include "test/fuzzers/utils/rtp_replayer.h"
  11. #include <algorithm>
  12. #include <memory>
  13. #include <string>
  14. #include <utility>
  15. #include "api/task_queue/default_task_queue_factory.h"
  16. #include "rtc_base/strings/json.h"
  17. #include "system_wrappers/include/clock.h"
  18. #include "test/call_config_utils.h"
  19. #include "test/encoder_settings.h"
  20. #include "test/fake_decoder.h"
  21. #include "test/fake_videorenderer.h"
  22. #include "test/rtp_file_reader.h"
  23. #include "test/rtp_header_parser.h"
  24. namespace webrtc {
  25. namespace test {
  26. void RtpReplayer::Replay(const std::string& replay_config_filepath,
  27. const uint8_t* rtp_dump_data,
  28. size_t rtp_dump_size) {
  29. auto stream_state = std::make_unique<StreamState>();
  30. std::vector<VideoReceiveStream::Config> receive_stream_configs =
  31. ReadConfigFromFile(replay_config_filepath, &(stream_state->transport));
  32. return Replay(std::move(stream_state), std::move(receive_stream_configs),
  33. rtp_dump_data, rtp_dump_size);
  34. }
  35. void RtpReplayer::Replay(
  36. std::unique_ptr<StreamState> stream_state,
  37. std::vector<VideoReceiveStream::Config> receive_stream_configs,
  38. const uint8_t* rtp_dump_data,
  39. size_t rtp_dump_size) {
  40. rtc::ScopedBaseFakeClock fake_clock;
  41. // Work around: webrtc calls webrtc::Random(clock.TimeInMicroseconds())
  42. // everywhere and Random expects non-zero seed. Let's set the clock non-zero
  43. // to make them happy.
  44. fake_clock.SetTime(webrtc::Timestamp::ms(1));
  45. // Attempt to create an RtpReader from the input file.
  46. auto rtp_reader = CreateRtpReader(rtp_dump_data, rtp_dump_size);
  47. if (rtp_reader == nullptr) {
  48. RTC_LOG(LS_ERROR) << "Failed to create the rtp_reader";
  49. return;
  50. }
  51. // Setup the video streams based on the configuration.
  52. webrtc::RtcEventLogNull event_log;
  53. std::unique_ptr<TaskQueueFactory> task_queue_factory =
  54. CreateDefaultTaskQueueFactory();
  55. Call::Config call_config(&event_log);
  56. call_config.task_queue_factory = task_queue_factory.get();
  57. std::unique_ptr<Call> call(Call::Create(call_config));
  58. SetupVideoStreams(&receive_stream_configs, stream_state.get(), call.get());
  59. // Start replaying the provided stream now that it has been configured.
  60. for (const auto& receive_stream : stream_state->receive_streams) {
  61. receive_stream->Start();
  62. }
  63. ReplayPackets(&fake_clock, call.get(), rtp_reader.get());
  64. for (const auto& receive_stream : stream_state->receive_streams) {
  65. call->DestroyVideoReceiveStream(receive_stream);
  66. }
  67. }
  68. std::vector<VideoReceiveStream::Config> RtpReplayer::ReadConfigFromFile(
  69. const std::string& replay_config,
  70. Transport* transport) {
  71. Json::Reader json_reader;
  72. Json::Value json_configs;
  73. if (!json_reader.parse(replay_config, json_configs)) {
  74. RTC_LOG(LS_ERROR)
  75. << "Error parsing JSON replay configuration for the fuzzer"
  76. << json_reader.getFormatedErrorMessages();
  77. return {};
  78. }
  79. std::vector<VideoReceiveStream::Config> receive_stream_configs;
  80. receive_stream_configs.reserve(json_configs.size());
  81. for (const auto& json : json_configs) {
  82. receive_stream_configs.push_back(
  83. ParseVideoReceiveStreamJsonConfig(transport, json));
  84. }
  85. return receive_stream_configs;
  86. }
  87. void RtpReplayer::SetupVideoStreams(
  88. std::vector<VideoReceiveStream::Config>* receive_stream_configs,
  89. StreamState* stream_state,
  90. Call* call) {
  91. stream_state->decoder_factory = std::make_unique<InternalDecoderFactory>();
  92. for (auto& receive_config : *receive_stream_configs) {
  93. // Attach the decoder for the corresponding payload type in the config.
  94. for (auto& decoder : receive_config.decoders) {
  95. decoder = test::CreateMatchingDecoder(decoder.payload_type,
  96. decoder.video_format.name);
  97. decoder.decoder_factory = stream_state->decoder_factory.get();
  98. }
  99. stream_state->sinks.emplace_back(new test::FakeVideoRenderer());
  100. // Create a receive stream for this config.
  101. receive_config.renderer = stream_state->sinks.back().get();
  102. stream_state->receive_streams.emplace_back(
  103. call->CreateVideoReceiveStream(std::move(receive_config)));
  104. }
  105. }
  106. std::unique_ptr<test::RtpFileReader> RtpReplayer::CreateRtpReader(
  107. const uint8_t* rtp_dump_data,
  108. size_t rtp_dump_size) {
  109. std::unique_ptr<test::RtpFileReader> rtp_reader(test::RtpFileReader::Create(
  110. test::RtpFileReader::kRtpDump, rtp_dump_data, rtp_dump_size, {}));
  111. if (!rtp_reader) {
  112. RTC_LOG(LS_ERROR) << "Unable to open input file with any supported format";
  113. return nullptr;
  114. }
  115. return rtp_reader;
  116. }
  117. void RtpReplayer::ReplayPackets(rtc::FakeClock* clock,
  118. Call* call,
  119. test::RtpFileReader* rtp_reader) {
  120. int64_t replay_start_ms = -1;
  121. int num_packets = 0;
  122. std::map<uint32_t, int> unknown_packets;
  123. while (true) {
  124. int64_t now_ms = rtc::TimeMillis();
  125. if (replay_start_ms == -1) {
  126. replay_start_ms = now_ms;
  127. }
  128. test::RtpPacket packet;
  129. if (!rtp_reader->NextPacket(&packet)) {
  130. break;
  131. }
  132. int64_t deliver_in_ms = replay_start_ms + packet.time_ms - now_ms;
  133. if (deliver_in_ms > 0) {
  134. // StatsCounter::ReportMetricToAggregatedCounter is O(elapsed time).
  135. // Set an upper limit to prevent waste time.
  136. clock->AdvanceTime(webrtc::TimeDelta::ms(
  137. std::min(deliver_in_ms, static_cast<int64_t>(100))));
  138. }
  139. ++num_packets;
  140. switch (call->Receiver()->DeliverPacket(
  141. webrtc::MediaType::VIDEO,
  142. rtc::CopyOnWriteBuffer(packet.data, packet.length),
  143. /* packet_time_us */ -1)) {
  144. case PacketReceiver::DELIVERY_OK:
  145. break;
  146. case PacketReceiver::DELIVERY_UNKNOWN_SSRC: {
  147. RTPHeader header;
  148. std::unique_ptr<RtpHeaderParser> parser(
  149. RtpHeaderParser::CreateForTest());
  150. parser->Parse(packet.data, packet.length, &header);
  151. if (unknown_packets[header.ssrc] == 0) {
  152. RTC_LOG(LS_ERROR) << "Unknown SSRC: " << header.ssrc;
  153. }
  154. ++unknown_packets[header.ssrc];
  155. break;
  156. }
  157. case PacketReceiver::DELIVERY_PACKET_ERROR: {
  158. RTC_LOG(LS_ERROR)
  159. << "Packet error, corrupt packets or incorrect setup?";
  160. RTPHeader header;
  161. std::unique_ptr<RtpHeaderParser> parser(
  162. RtpHeaderParser::CreateForTest());
  163. parser->Parse(packet.data, packet.length, &header);
  164. RTC_LOG(LS_ERROR) << "Packet packet_length=" << packet.length
  165. << " payload_type=" << header.payloadType
  166. << " sequence_number=" << header.sequenceNumber
  167. << " time_stamp=" << header.timestamp
  168. << " ssrc=" << header.ssrc;
  169. break;
  170. }
  171. }
  172. }
  173. RTC_LOG(LS_INFO) << "num_packets: " << num_packets;
  174. for (const auto& unknown_packet : unknown_packets) {
  175. RTC_LOG(LS_ERROR) << "Packets for unknown ssrc " << unknown_packet.first
  176. << ":" << unknown_packet.second;
  177. }
  178. }
  179. } // namespace test
  180. } // namespace webrtc