• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef RTC_TOOLS_RTP_GENERATOR_RTP_GENERATOR_H_
12 #define RTC_TOOLS_RTP_GENERATOR_RTP_GENERATOR_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "api/call/transport.h"
19 #include "api/media_types.h"
20 #include "api/rtc_event_log/rtc_event_log.h"
21 #include "api/task_queue/task_queue_factory.h"
22 #include "api/video/builtin_video_bitrate_allocator_factory.h"
23 #include "api/video_codecs/video_decoder_factory.h"
24 #include "api/video_codecs/video_encoder_config.h"
25 #include "api/video_codecs/video_encoder_factory.h"
26 #include "call/call.h"
27 #include "call/rtp_config.h"
28 #include "call/video_send_stream.h"
29 #include "media/engine/webrtc_video_engine.h"
30 #include "rtc_base/constructor_magic.h"
31 #include "test/frame_generator_capturer.h"
32 #include "test/rtp_file_reader.h"
33 #include "test/rtp_file_writer.h"
34 
35 namespace webrtc {
36 
37 // Specifies all the configurable options to pass to the corpus generator.
38 // If modified please update the JSON parser as well as all.
39 struct RtpGeneratorOptions {
40   struct VideoSendStreamConfig {
41     // The time to record the RtpDump for.
42     int duration_ms = 10000;
43     // The video resolution width.
44     int video_width = 640;
45     // The video resolution height.
46     int video_height = 480;
47     // The video fps.
48     int video_fps = 24;
49     // The number of squares to render.
50     int num_squares = 128;
51     // The individual RTP configuration.
52     RtpConfig rtp;
53   };
54   // Multiple senders can be active at once on an rtp channel.
55   std::vector<VideoSendStreamConfig> video_streams;
56 };
57 
58 // Attempts to parse RtpGeneratorOptions from a JSON file. Any failures
59 // will result in absl::nullopt.
60 absl::optional<RtpGeneratorOptions> ParseRtpGeneratorOptionsFromFile(
61     const std::string& options_file);
62 
63 // The RtpGenerator allows generating of corpus material intended to be
64 // used by fuzzers. It accepts a simple Json configuration file that allows the
65 // user to configure the codec, extensions and error correction mechanisms. It
66 // will then proceed to generate an rtpdump for the specified duration using
67 // that configuration that can be replayed by the video_replayer. The receiver
68 // configuration JSON will also be output and can be replayed as follows:
69 // ./rtp_generator --config_file sender_config --output_rtpdump my.rtpdump
70 //                 --output_config receiver_config.json
71 // ./video_replay --config_file receiver_config.json --output_file my.rtpdump
72 //
73 // It achieves this by creating a VideoStreamSender, configuring it as requested
74 // by the user and then intercepting all outgoing RTP packets and writing them
75 // to a file instead of out of the network. It then uses this sender
76 // configuration to generate a mirror receiver configuration that can be read by
77 // the video_replay program.
78 class RtpGenerator final : public webrtc::Transport {
79  public:
80   // Construct a new RtpGenerator using the specified options.
81   explicit RtpGenerator(const RtpGeneratorOptions& options);
82   // Cleans up the VideoSendStream.
83   ~RtpGenerator() override;
84   // Generates an rtp_dump that is written out to
85   void GenerateRtpDump(const std::string& rtp_dump_path);
86 
87  private:
88   // webrtc::Transport implementation
89   // Captured RTP packets are written to the RTPDump file instead of over the
90   // network.
91   bool SendRtp(const uint8_t* packet,
92                size_t length,
93                const webrtc::PacketOptions& options) override;
94   // RTCP packets are ignored for now.
95   bool SendRtcp(const uint8_t* packet, size_t length) override;
96   // Returns the maximum duration
97   int GetMaxDuration() const;
98   // Waits until all video streams have finished.
99   void WaitUntilAllVideoStreamsFinish();
100   // Converts packet data into an RtpPacket.
101   test::RtpPacket DataToRtpPacket(const uint8_t* packet, size_t packet_len);
102 
103   const RtpGeneratorOptions options_;
104   std::unique_ptr<VideoEncoderFactory> video_encoder_factory_;
105   std::unique_ptr<VideoDecoderFactory> video_decoder_factory_;
106   std::unique_ptr<VideoBitrateAllocatorFactory>
107       video_bitrate_allocator_factory_;
108   std::unique_ptr<RtcEventLog> event_log_;
109   std::unique_ptr<Call> call_;
110   std::unique_ptr<test::RtpFileWriter> rtp_dump_writer_;
111   std::vector<std::unique_ptr<test::FrameGeneratorCapturer>> frame_generators_;
112   std::vector<VideoSendStream*> video_send_streams_;
113   std::vector<uint32_t> durations_ms_;
114   uint32_t start_ms_ = 0;
115   std::unique_ptr<TaskQueueFactory> task_queue_;
116 
117   // This object cannot be copied.
118   RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(RtpGenerator);
119 };
120 
121 }  // namespace webrtc
122 
123 #endif  // RTC_TOOLS_RTP_GENERATOR_RTP_GENERATOR_H_
124