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 12 #ifndef MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_ 13 #define MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_ 14 15 #include <memory> 16 #include <stack> 17 #include <string> 18 #include <utility> 19 #include <vector> 20 21 #include "media/engine/webrtcvideoencoderfactory.h" 22 #include "modules/video_coding/codecs/vp8/include/vp8.h" 23 #include "rtc_base/atomicops.h" 24 #include "rtc_base/sequenced_task_checker.h" 25 26 namespace webrtc { 27 28 class SimulcastRateAllocator; 29 class VideoEncoderFactory; 30 31 // SimulcastEncoderAdapter implements simulcast support by creating multiple 32 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. 33 // The object is created and destroyed on the worker thread, but all public 34 // interfaces should be called from the encoder task queue. 35 class SimulcastEncoderAdapter : public VP8Encoder { 36 public: 37 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory); 38 // Deprecated - use webrtc::VideoEncoderFactory instead. 39 explicit SimulcastEncoderAdapter(cricket::WebRtcVideoEncoderFactory* factory); 40 virtual ~SimulcastEncoderAdapter(); 41 42 // Implements VideoEncoder. 43 int Release() override; 44 int InitEncode(const VideoCodec* inst, int number_of_cores, 45 size_t max_payload_size) override; 46 int Encode(const VideoFrame& input_image, 47 const CodecSpecificInfo* codec_specific_info, 48 const std::vector<FrameType>* frame_types) override; 49 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; 50 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; 51 int SetRateAllocation(const BitrateAllocation& bitrate, 52 uint32_t new_framerate) override; 53 54 // Eventual handler for the contained encoders' EncodedImageCallbacks, but 55 // called from an internal helper that also knows the correct stream 56 // index. 57 EncodedImageCallback::Result OnEncodedImage( 58 size_t stream_idx, const EncodedImage& encoded_image, 59 const CodecSpecificInfo* codec_specific_info, 60 const RTPFragmentationHeader* fragmentation); 61 62 VideoEncoder::ScalingSettings GetScalingSettings() const override; 63 64 bool SupportsNativeHandle() const override; 65 const char* ImplementationName() const override; 66 67 private: 68 struct StreamInfo { StreamInfoStreamInfo69 StreamInfo(std::unique_ptr<VideoEncoder> encoder, 70 std::unique_ptr<EncodedImageCallback> callback, uint16_t width, 71 uint16_t height, bool send_stream) 72 : encoder(std::move(encoder)), 73 callback(std::move(callback)), 74 width(width), 75 height(height), 76 key_frame_request(false), 77 send_stream(send_stream) {} 78 std::unique_ptr<VideoEncoder> encoder; 79 std::unique_ptr<EncodedImageCallback> callback; 80 uint16_t width; 81 uint16_t height; 82 bool key_frame_request; 83 bool send_stream; 84 }; 85 86 enum class StreamResolution { 87 OTHER, 88 HIGHEST, 89 LOWEST, 90 }; 91 92 // Populate the codec settings for each simulcast stream. 93 static void PopulateStreamCodec(const webrtc::VideoCodec& inst, 94 int stream_index, uint32_t start_bitrate_kbps, 95 StreamResolution stream_resolution, 96 webrtc::VideoCodec* stream_codec); 97 98 bool Initialized() const; 99 100 void DestroyStoredEncoders(); 101 102 volatile int inited_; // Accessed atomically. 103 VideoEncoderFactory* const factory_; 104 cricket::WebRtcVideoEncoderFactory* const cricket_factory_; 105 VideoCodec codec_; 106 std::vector<StreamInfo> streaminfos_; 107 EncodedImageCallback* encoded_complete_callback_; 108 std::string implementation_name_; 109 110 // Used for checking the single-threaded access of the encoder interface. 111 rtc::SequencedTaskChecker encoder_queue_; 112 113 // Store encoders in between calls to Release and InitEncode, so they don't 114 // have to be recreated. Remaining encoders are destroyed by the destructor. 115 std::stack<std::unique_ptr<VideoEncoder>> stored_encoders_; 116 }; 117 118 } // namespace webrtc 119 120 #endif // MEDIA_ENGINE_SIMULCAST_ENCODER_ADAPTER_H_ 121