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 <string> | 16 #include <string> |
17 #include <utility> | |
17 #include <vector> | 18 #include <vector> |
18 | 19 |
20 #include "webrtc/base/criticalsection.h" | |
21 #include "webrtc/base/sequenced_task_checker.h" | |
19 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" | 22 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" |
20 | 23 |
21 namespace webrtc { | 24 namespace webrtc { |
22 | 25 |
23 class SimulcastRateAllocator; | 26 class SimulcastRateAllocator; |
24 | 27 |
25 class VideoEncoderFactory { | 28 class VideoEncoderFactory { |
26 public: | 29 public: |
27 virtual VideoEncoder* Create() = 0; | 30 virtual VideoEncoder* Create() = 0; |
28 virtual void Destroy(VideoEncoder* encoder) = 0; | 31 virtual void Destroy(VideoEncoder* encoder) = 0; |
29 virtual ~VideoEncoderFactory() {} | 32 virtual ~VideoEncoderFactory() {} |
30 }; | 33 }; |
31 | 34 |
32 // SimulcastEncoderAdapter implements simulcast support by creating multiple | 35 // SimulcastEncoderAdapter implements simulcast support by creating multiple |
33 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. | 36 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. |
34 // All the public interfaces are expected to be called from the same thread, | 37 // The object is created and destroyed on the worker thread, but all public |
35 // e.g the encoder thread. | 38 // interfaces should be called from the encoder task queue. |
36 class SimulcastEncoderAdapter : public VP8Encoder { | 39 class SimulcastEncoderAdapter : public VP8Encoder { |
37 public: | 40 public: |
38 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory); | 41 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory); |
39 virtual ~SimulcastEncoderAdapter(); | 42 virtual ~SimulcastEncoderAdapter(); |
40 | 43 |
41 // Implements VideoEncoder | 44 // Implements VideoEncoder. |
42 int Release() override; | 45 int Release() override; |
43 int InitEncode(const VideoCodec* inst, | 46 int InitEncode(const VideoCodec* inst, |
44 int number_of_cores, | 47 int number_of_cores, |
45 size_t max_payload_size) override; | 48 size_t max_payload_size) override; |
46 int Encode(const VideoFrame& input_image, | 49 int Encode(const VideoFrame& input_image, |
47 const CodecSpecificInfo* codec_specific_info, | 50 const CodecSpecificInfo* codec_specific_info, |
48 const std::vector<FrameType>* frame_types) override; | 51 const std::vector<FrameType>* frame_types) override; |
49 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; | 52 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; |
50 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; | 53 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
51 int SetRateAllocation(const BitrateAllocation& bitrate, | 54 int SetRateAllocation(const BitrateAllocation& bitrate, |
52 uint32_t new_framerate) override; | 55 uint32_t new_framerate) override; |
53 | 56 |
54 // Eventual handler for the contained encoders' EncodedImageCallbacks, but | 57 // Eventual handler for the contained encoders' EncodedImageCallbacks, but |
55 // called from an internal helper that also knows the correct stream | 58 // called from an internal helper that also knows the correct stream |
56 // index. | 59 // index. |
57 EncodedImageCallback::Result OnEncodedImage( | 60 EncodedImageCallback::Result OnEncodedImage( |
58 size_t stream_idx, | 61 size_t stream_idx, |
59 const EncodedImage& encoded_image, | 62 const EncodedImage& encoded_image, |
60 const CodecSpecificInfo* codec_specific_info, | 63 const CodecSpecificInfo* codec_specific_info, |
61 const RTPFragmentationHeader* fragmentation); | 64 const RTPFragmentationHeader* fragmentation); |
62 | 65 |
63 VideoEncoder::ScalingSettings GetScalingSettings() const override; | 66 VideoEncoder::ScalingSettings GetScalingSettings() const override; |
64 | 67 |
65 bool SupportsNativeHandle() const override; | 68 bool SupportsNativeHandle() const override; |
66 const char* ImplementationName() const override; | 69 const char* ImplementationName() const override; |
67 | 70 |
68 private: | 71 private: |
69 struct StreamInfo { | 72 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) {} | |
77 StreamInfo(VideoEncoder* encoder, | 73 StreamInfo(VideoEncoder* encoder, |
78 EncodedImageCallback* callback, | 74 std::unique_ptr<EncodedImageCallback> callback, |
79 uint16_t width, | 75 uint16_t width, |
80 uint16_t height, | 76 uint16_t height, |
81 bool send_stream) | 77 bool send_stream) |
82 : encoder(encoder), | 78 : encoder(encoder), |
83 callback(callback), | 79 callback(std::move(callback)), |
84 width(width), | 80 width(width), |
85 height(height), | 81 height(height), |
86 key_frame_request(false), | 82 key_frame_request(false), |
87 send_stream(send_stream) {} | 83 send_stream(send_stream) {} |
88 // Deleted by SimulcastEncoderAdapter::Release(). | 84 // Deleted by SimulcastEncoderAdapter::DestroyStoredEncoders(). |
89 VideoEncoder* encoder; | 85 VideoEncoder* encoder; |
90 EncodedImageCallback* callback; | 86 std::unique_ptr<EncodedImageCallback> callback; |
91 uint16_t width; | 87 uint16_t width; |
92 uint16_t height; | 88 uint16_t height; |
93 bool key_frame_request; | 89 bool key_frame_request; |
94 bool send_stream; | 90 bool send_stream; |
95 }; | 91 }; |
96 | 92 |
97 // Populate the codec settings for each stream. | 93 // Populate the codec settings for each simulcast stream. |
98 void PopulateStreamCodec(const webrtc::VideoCodec* inst, | 94 static void PopulateStreamCodec(const webrtc::VideoCodec& inst, |
99 int stream_index, | 95 int stream_index, |
100 uint32_t start_bitrate_kbps, | 96 uint32_t start_bitrate_kbps, |
101 bool highest_resolution_stream, | 97 bool highest_resolution_stream, |
102 webrtc::VideoCodec* stream_codec); | 98 webrtc::VideoCodec* stream_codec); |
103 | 99 |
104 bool Initialized() const; | 100 bool Initialized() const; |
105 | 101 |
106 std::unique_ptr<VideoEncoderFactory> factory_; | 102 void DestroyStoredEncoders(); |
103 | |
104 #if RTC_DCHECK_IS_ON | |
105 // Used for protecting |inited_| in debug builds. This is necessary for the | |
106 // DHECK in the destructor, as the object is created/destroyed on a different | |
107 // thread that it is being operated on. | |
108 rtc::CriticalSection inited_crit_; | |
109 #endif | |
110 | |
111 bool inited_; | |
112 const std::unique_ptr<VideoEncoderFactory> factory_; | |
107 VideoCodec codec_; | 113 VideoCodec codec_; |
108 std::vector<StreamInfo> streaminfos_; | 114 std::vector<StreamInfo> streaminfos_; |
109 EncodedImageCallback* encoded_complete_callback_; | 115 EncodedImageCallback* encoded_complete_callback_; |
110 std::string implementation_name_; | 116 std::string implementation_name_; |
117 | |
118 // Used for checking the single-threaded access of the encoder interface. | |
119 rtc::SequencedTaskChecker encoder_queue_; | |
120 | |
121 // Store encoders in between calls to Release and InitEncode, so they don't | |
122 // have to be recreated. Remaining encoders are destroyed by the destructor. | |
123 std::vector<VideoEncoder*> stored_encoders_; | |
stefan-webrtc
2017/05/02 08:08:34
std::stack instead?
brandtr
2017/05/02 09:17:02
Sure. Done.
| |
111 }; | 124 }; |
112 | 125 |
113 } // namespace webrtc | 126 } // namespace webrtc |
114 | 127 |
115 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ | 128 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ |
OLD | NEW |