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