OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 * | 9 * |
10 */ | 10 */ |
11 | 11 |
12 #ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ | 12 #ifndef WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ |
13 #define WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ | 13 #define WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ |
14 | 14 |
15 #include <memory> | 15 #include <memory> |
16 #include <stack> | |
17 #include <string> | 16 #include <string> |
18 #include <utility> | |
19 #include <vector> | 17 #include <vector> |
20 | 18 |
21 #include "webrtc/base/atomicops.h" | |
22 #include "webrtc/base/sequenced_task_checker.h" | |
23 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" | 19 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" |
24 | 20 |
25 namespace webrtc { | 21 namespace webrtc { |
26 | 22 |
27 class SimulcastRateAllocator; | 23 class SimulcastRateAllocator; |
28 | 24 |
29 // TODO(brandtr): Remove this class and replace its use with a | |
30 // WebRtcVideoEncoderFactory. | |
31 class VideoEncoderFactory { | 25 class VideoEncoderFactory { |
32 public: | 26 public: |
33 virtual VideoEncoder* Create() = 0; | 27 virtual VideoEncoder* Create() = 0; |
34 virtual void Destroy(VideoEncoder* encoder) = 0; | 28 virtual void Destroy(VideoEncoder* encoder) = 0; |
35 virtual ~VideoEncoderFactory() {} | 29 virtual ~VideoEncoderFactory() {} |
36 }; | 30 }; |
37 | 31 |
38 // SimulcastEncoderAdapter implements simulcast support by creating multiple | 32 // SimulcastEncoderAdapter implements simulcast support by creating multiple |
39 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. | 33 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. |
40 // The object is created and destroyed on the worker thread, but all public | 34 // All the public interfaces are expected to be called from the same thread, |
41 // interfaces should be called from the encoder task queue. | 35 // e.g the encoder thread. |
42 class SimulcastEncoderAdapter : public VP8Encoder { | 36 class SimulcastEncoderAdapter : public VP8Encoder { |
43 public: | 37 public: |
44 // TODO(brandtr): Make it clear that the ownership of |factory| is transferred | |
45 // by only accepting a std::unique_ptr<VideoEncoderFactory> here. | |
46 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory); | 38 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory); |
47 virtual ~SimulcastEncoderAdapter(); | 39 virtual ~SimulcastEncoderAdapter(); |
48 | 40 |
49 // Implements VideoEncoder. | 41 // Implements VideoEncoder |
50 int Release() override; | 42 int Release() override; |
51 int InitEncode(const VideoCodec* inst, | 43 int InitEncode(const VideoCodec* inst, |
52 int number_of_cores, | 44 int number_of_cores, |
53 size_t max_payload_size) override; | 45 size_t max_payload_size) override; |
54 int Encode(const VideoFrame& input_image, | 46 int Encode(const VideoFrame& input_image, |
55 const CodecSpecificInfo* codec_specific_info, | 47 const CodecSpecificInfo* codec_specific_info, |
56 const std::vector<FrameType>* frame_types) override; | 48 const std::vector<FrameType>* frame_types) override; |
57 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; | 49 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; |
58 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; | 50 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
59 int SetRateAllocation(const BitrateAllocation& bitrate, | 51 int SetRateAllocation(const BitrateAllocation& bitrate, |
60 uint32_t new_framerate) override; | 52 uint32_t new_framerate) override; |
61 | 53 |
62 // Eventual handler for the contained encoders' EncodedImageCallbacks, but | 54 // Eventual handler for the contained encoders' EncodedImageCallbacks, but |
63 // called from an internal helper that also knows the correct stream | 55 // called from an internal helper that also knows the correct stream |
64 // index. | 56 // index. |
65 EncodedImageCallback::Result OnEncodedImage( | 57 EncodedImageCallback::Result OnEncodedImage( |
66 size_t stream_idx, | 58 size_t stream_idx, |
67 const EncodedImage& encoded_image, | 59 const EncodedImage& encoded_image, |
68 const CodecSpecificInfo* codec_specific_info, | 60 const CodecSpecificInfo* codec_specific_info, |
69 const RTPFragmentationHeader* fragmentation); | 61 const RTPFragmentationHeader* fragmentation); |
70 | 62 |
71 VideoEncoder::ScalingSettings GetScalingSettings() const override; | 63 VideoEncoder::ScalingSettings GetScalingSettings() const override; |
72 | 64 |
73 bool SupportsNativeHandle() const override; | 65 bool SupportsNativeHandle() const override; |
74 const char* ImplementationName() const override; | 66 const char* ImplementationName() const override; |
75 | 67 |
76 private: | 68 private: |
77 struct StreamInfo { | 69 struct StreamInfo { |
| 70 StreamInfo() |
| 71 : encoder(NULL), |
| 72 callback(NULL), |
| 73 width(0), |
| 74 height(0), |
| 75 key_frame_request(false), |
| 76 send_stream(true) {} |
78 StreamInfo(VideoEncoder* encoder, | 77 StreamInfo(VideoEncoder* encoder, |
79 std::unique_ptr<EncodedImageCallback> callback, | 78 EncodedImageCallback* callback, |
80 uint16_t width, | 79 uint16_t width, |
81 uint16_t height, | 80 uint16_t height, |
82 bool send_stream) | 81 bool send_stream) |
83 : encoder(encoder), | 82 : encoder(encoder), |
84 callback(std::move(callback)), | 83 callback(callback), |
85 width(width), | 84 width(width), |
86 height(height), | 85 height(height), |
87 key_frame_request(false), | 86 key_frame_request(false), |
88 send_stream(send_stream) {} | 87 send_stream(send_stream) {} |
89 // Deleted by SimulcastEncoderAdapter::DestroyStoredEncoders(). | 88 // Deleted by SimulcastEncoderAdapter::Release(). |
90 VideoEncoder* encoder; | 89 VideoEncoder* encoder; |
91 std::unique_ptr<EncodedImageCallback> callback; | 90 EncodedImageCallback* callback; |
92 uint16_t width; | 91 uint16_t width; |
93 uint16_t height; | 92 uint16_t height; |
94 bool key_frame_request; | 93 bool key_frame_request; |
95 bool send_stream; | 94 bool send_stream; |
96 }; | 95 }; |
97 | 96 |
98 // Populate the codec settings for each simulcast stream. | 97 // Populate the codec settings for each stream. |
99 static void PopulateStreamCodec(const webrtc::VideoCodec& inst, | 98 void PopulateStreamCodec(const webrtc::VideoCodec* inst, |
100 int stream_index, | 99 int stream_index, |
101 uint32_t start_bitrate_kbps, | 100 uint32_t start_bitrate_kbps, |
102 bool highest_resolution_stream, | 101 bool highest_resolution_stream, |
103 webrtc::VideoCodec* stream_codec); | 102 webrtc::VideoCodec* stream_codec); |
104 | 103 |
105 bool Initialized() const; | 104 bool Initialized() const; |
106 | 105 |
107 void DestroyStoredEncoders(); | 106 std::unique_ptr<VideoEncoderFactory> factory_; |
108 | |
109 volatile int inited_; // Accessed atomically. | |
110 const std::unique_ptr<VideoEncoderFactory> factory_; | |
111 VideoCodec codec_; | 107 VideoCodec codec_; |
112 std::vector<StreamInfo> streaminfos_; | 108 std::vector<StreamInfo> streaminfos_; |
113 EncodedImageCallback* encoded_complete_callback_; | 109 EncodedImageCallback* encoded_complete_callback_; |
114 std::string implementation_name_; | 110 std::string implementation_name_; |
115 | |
116 // Used for checking the single-threaded access of the encoder interface. | |
117 rtc::SequencedTaskChecker encoder_queue_; | |
118 | |
119 // Store encoders in between calls to Release and InitEncode, so they don't | |
120 // have to be recreated. Remaining encoders are destroyed by the destructor. | |
121 std::stack<VideoEncoder*> stored_encoders_; | |
122 }; | 111 }; |
123 | 112 |
124 } // namespace webrtc | 113 } // namespace webrtc |
125 | 114 |
126 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ | 115 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ |
OLD | NEW |