1 /* 2 * Copyright 2020 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 MODULES_VIDEO_CODING_CODECS_TEST_ENCODED_VIDEO_FRAME_PRODUCER_H_ 12 #define MODULES_VIDEO_CODING_CODECS_TEST_ENCODED_VIDEO_FRAME_PRODUCER_H_ 13 14 #include <stdint.h> 15 16 #include <vector> 17 18 #include "api/transport/rtp/dependency_descriptor.h" 19 #include "api/video/encoded_image.h" 20 #include "api/video_codecs/video_encoder.h" 21 #include "modules/video_coding/include/video_codec_interface.h" 22 23 namespace webrtc { 24 25 // Wrapper around VideoEncoder::Encode for convenient input (generates frames) 26 // and output (returns encoded frames instead of passing them to callback) 27 class EncodedVideoFrameProducer { 28 public: 29 struct EncodedFrame { 30 EncodedImage encoded_image; 31 CodecSpecificInfo codec_specific_info; 32 }; 33 34 // `encoder` should be initialized, but shouldn't have `EncoderCallback` set. EncodedVideoFrameProducer(VideoEncoder & encoder)35 explicit EncodedVideoFrameProducer(VideoEncoder& encoder) 36 : encoder_(encoder) {} 37 EncodedVideoFrameProducer(const EncodedVideoFrameProducer&) = delete; 38 EncodedVideoFrameProducer& operator=(const EncodedVideoFrameProducer&) = 39 delete; 40 41 // Number of the input frames to pass to the encoder. 42 EncodedVideoFrameProducer& SetNumInputFrames(int value); 43 // Encode next frame as key frame. 44 EncodedVideoFrameProducer& ForceKeyFrame(); 45 // Resolution of the input frames. 46 EncodedVideoFrameProducer& SetResolution(RenderResolution value); 47 48 EncodedVideoFrameProducer& SetFramerateFps(int value); 49 50 EncodedVideoFrameProducer& SetRtpTimestamp(uint32_t value); 51 52 // Generates input video frames and encodes them with `encoder` provided in 53 // the constructor. Returns frame passed to the `OnEncodedImage` by wraping 54 // `EncodedImageCallback` underneath. 55 std::vector<EncodedFrame> Encode(); 56 57 private: 58 VideoEncoder& encoder_; 59 60 uint32_t rtp_timestamp_ = 1000; 61 int num_input_frames_ = 1; 62 int framerate_fps_ = 30; 63 RenderResolution resolution_ = {320, 180}; 64 std::vector<VideoFrameType> next_frame_type_ = { 65 VideoFrameType::kVideoFrameKey}; 66 }; 67 SetNumInputFrames(int value)68inline EncodedVideoFrameProducer& EncodedVideoFrameProducer::SetNumInputFrames( 69 int value) { 70 RTC_DCHECK_GT(value, 0); 71 num_input_frames_ = value; 72 return *this; 73 } 74 ForceKeyFrame()75inline EncodedVideoFrameProducer& EncodedVideoFrameProducer::ForceKeyFrame() { 76 next_frame_type_ = {VideoFrameType::kVideoFrameKey}; 77 return *this; 78 } 79 SetResolution(RenderResolution value)80inline EncodedVideoFrameProducer& EncodedVideoFrameProducer::SetResolution( 81 RenderResolution value) { 82 resolution_ = value; 83 return *this; 84 } 85 SetFramerateFps(int value)86inline EncodedVideoFrameProducer& EncodedVideoFrameProducer::SetFramerateFps( 87 int value) { 88 RTC_DCHECK_GT(value, 0); 89 framerate_fps_ = value; 90 return *this; 91 } 92 SetRtpTimestamp(uint32_t value)93inline EncodedVideoFrameProducer& EncodedVideoFrameProducer::SetRtpTimestamp( 94 uint32_t value) { 95 rtp_timestamp_ = value; 96 return *this; 97 } 98 99 } // namespace webrtc 100 #endif // MODULES_VIDEO_CODING_CODECS_TEST_ENCODED_VIDEO_FRAME_PRODUCER_H_ 101