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 WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_
13 #define WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_
14 
15 #include <vector>
16 
17 #include "webrtc/base/scoped_ptr.h"
18 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h"
19 
20 namespace webrtc {
21 
22 class VideoEncoderFactory {
23  public:
24   virtual VideoEncoder* Create() = 0;
25   virtual void Destroy(VideoEncoder* encoder) = 0;
~VideoEncoderFactory()26   virtual ~VideoEncoderFactory() {}
27 };
28 
29 // SimulcastEncoderAdapter implements simulcast support by creating multiple
30 // webrtc::VideoEncoder instances with the given VideoEncoderFactory.
31 // All the public interfaces are expected to be called from the same thread,
32 // e.g the encoder thread.
33 class SimulcastEncoderAdapter : public VP8Encoder {
34  public:
35   explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory);
36   virtual ~SimulcastEncoderAdapter();
37 
38   // Implements VideoEncoder
39   int Release() override;
40   int InitEncode(const VideoCodec* inst,
41                  int number_of_cores,
42                  size_t max_payload_size) override;
43   int Encode(const VideoFrame& input_image,
44              const CodecSpecificInfo* codec_specific_info,
45              const std::vector<FrameType>* frame_types) override;
46   int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override;
47   int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override;
48   int SetRates(uint32_t new_bitrate_kbit, uint32_t new_framerate) override;
49 
50   // Eventual handler for the contained encoders' EncodedImageCallbacks, but
51   // called from an internal helper that also knows the correct stream
52   // index.
53   int32_t Encoded(size_t stream_idx,
54                   const EncodedImage& encodedImage,
55                   const CodecSpecificInfo* codecSpecificInfo = NULL,
56                   const RTPFragmentationHeader* fragmentation = NULL);
57 
58   void OnDroppedFrame() override;
59 
60   int GetTargetFramerate() override;
61   bool SupportsNativeHandle() const override;
62   const char* ImplementationName() const override;
63 
64  private:
65   struct StreamInfo {
StreamInfoStreamInfo66     StreamInfo()
67         : encoder(NULL),
68           callback(NULL),
69           width(0),
70           height(0),
71           key_frame_request(false),
72           send_stream(true) {}
StreamInfoStreamInfo73     StreamInfo(VideoEncoder* encoder,
74                EncodedImageCallback* callback,
75                uint16_t width,
76                uint16_t height,
77                bool send_stream)
78         : encoder(encoder),
79           callback(callback),
80           width(width),
81           height(height),
82           key_frame_request(false),
83           send_stream(send_stream) {}
84     // Deleted by SimulcastEncoderAdapter::Release().
85     VideoEncoder* encoder;
86     EncodedImageCallback* callback;
87     uint16_t width;
88     uint16_t height;
89     bool key_frame_request;
90     bool send_stream;
91   };
92 
93   // Get the stream bitrate, for the stream |stream_idx|, given the bitrate
94   // |new_bitrate_kbit| and the actual configured stream count in
95   // |total_number_of_streams|. The function also returns whether there's enough
96   // bandwidth to send this stream via |send_stream|.
97   uint32_t GetStreamBitrate(int stream_idx,
98                             size_t total_number_of_streams,
99                             uint32_t new_bitrate_kbit,
100                             bool* send_stream) const;
101 
102   // Populate the codec settings for each stream.
103   void PopulateStreamCodec(const webrtc::VideoCodec* inst,
104                            int stream_index,
105                            size_t total_number_of_streams,
106                            bool highest_resolution_stream,
107                            webrtc::VideoCodec* stream_codec,
108                            bool* send_stream);
109 
110   bool Initialized() const;
111 
112   rtc::scoped_ptr<VideoEncoderFactory> factory_;
113   rtc::scoped_ptr<Config> screensharing_extra_options_;
114   VideoCodec codec_;
115   std::vector<StreamInfo> streaminfos_;
116   EncodedImageCallback* encoded_complete_callback_;
117 };
118 
119 }  // namespace webrtc
120 
121 #endif  // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_
122