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> |
16 #include <string> | 17 #include <string> |
| 18 #include <utility> |
17 #include <vector> | 19 #include <vector> |
18 | 20 |
| 21 #include "webrtc/base/atomicops.h" |
| 22 #include "webrtc/base/sequenced_task_checker.h" |
19 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" | 23 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8.h" |
20 | 24 |
21 namespace webrtc { | 25 namespace webrtc { |
22 | 26 |
23 class SimulcastRateAllocator; | 27 class SimulcastRateAllocator; |
24 | 28 |
| 29 // TODO(brandtr): Remove this class and replace its use with a |
| 30 // WebRtcVideoEncoderFactory. |
25 class VideoEncoderFactory { | 31 class VideoEncoderFactory { |
26 public: | 32 public: |
27 virtual VideoEncoder* Create() = 0; | 33 virtual VideoEncoder* Create() = 0; |
28 virtual void Destroy(VideoEncoder* encoder) = 0; | 34 virtual void Destroy(VideoEncoder* encoder) = 0; |
29 virtual ~VideoEncoderFactory() {} | 35 virtual ~VideoEncoderFactory() {} |
30 }; | 36 }; |
31 | 37 |
32 // SimulcastEncoderAdapter implements simulcast support by creating multiple | 38 // SimulcastEncoderAdapter implements simulcast support by creating multiple |
33 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. | 39 // webrtc::VideoEncoder instances with the given VideoEncoderFactory. |
34 // All the public interfaces are expected to be called from the same thread, | 40 // The object is created and destroyed on the worker thread, but all public |
35 // e.g the encoder thread. | 41 // interfaces should be called from the encoder task queue. |
36 class SimulcastEncoderAdapter : public VP8Encoder { | 42 class SimulcastEncoderAdapter : public VP8Encoder { |
37 public: | 43 public: |
| 44 // TODO(brandtr): Make it clear that the ownership of |factory| is transferred |
| 45 // by only accepting a std::unique_ptr<VideoEncoderFactory> here. |
38 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory); | 46 explicit SimulcastEncoderAdapter(VideoEncoderFactory* factory); |
39 virtual ~SimulcastEncoderAdapter(); | 47 virtual ~SimulcastEncoderAdapter(); |
40 | 48 |
41 // Implements VideoEncoder | 49 // Implements VideoEncoder. |
42 int Release() override; | 50 int Release() override; |
43 int InitEncode(const VideoCodec* inst, | 51 int InitEncode(const VideoCodec* inst, |
44 int number_of_cores, | 52 int number_of_cores, |
45 size_t max_payload_size) override; | 53 size_t max_payload_size) override; |
46 int Encode(const VideoFrame& input_image, | 54 int Encode(const VideoFrame& input_image, |
47 const CodecSpecificInfo* codec_specific_info, | 55 const CodecSpecificInfo* codec_specific_info, |
48 const std::vector<FrameType>* frame_types) override; | 56 const std::vector<FrameType>* frame_types) override; |
49 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; | 57 int RegisterEncodeCompleteCallback(EncodedImageCallback* callback) override; |
50 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; | 58 int SetChannelParameters(uint32_t packet_loss, int64_t rtt) override; |
51 int SetRateAllocation(const BitrateAllocation& bitrate, | 59 int SetRateAllocation(const BitrateAllocation& bitrate, |
52 uint32_t new_framerate) override; | 60 uint32_t new_framerate) override; |
53 | 61 |
54 // Eventual handler for the contained encoders' EncodedImageCallbacks, but | 62 // Eventual handler for the contained encoders' EncodedImageCallbacks, but |
55 // called from an internal helper that also knows the correct stream | 63 // called from an internal helper that also knows the correct stream |
56 // index. | 64 // index. |
57 EncodedImageCallback::Result OnEncodedImage( | 65 EncodedImageCallback::Result OnEncodedImage( |
58 size_t stream_idx, | 66 size_t stream_idx, |
59 const EncodedImage& encoded_image, | 67 const EncodedImage& encoded_image, |
60 const CodecSpecificInfo* codec_specific_info, | 68 const CodecSpecificInfo* codec_specific_info, |
61 const RTPFragmentationHeader* fragmentation); | 69 const RTPFragmentationHeader* fragmentation); |
62 | 70 |
63 VideoEncoder::ScalingSettings GetScalingSettings() const override; | 71 VideoEncoder::ScalingSettings GetScalingSettings() const override; |
64 | 72 |
65 bool SupportsNativeHandle() const override; | 73 bool SupportsNativeHandle() const override; |
66 const char* ImplementationName() const override; | 74 const char* ImplementationName() const override; |
67 | 75 |
68 private: | 76 private: |
69 struct StreamInfo { | 77 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, | 78 StreamInfo(VideoEncoder* encoder, |
78 EncodedImageCallback* callback, | 79 std::unique_ptr<EncodedImageCallback> callback, |
79 uint16_t width, | 80 uint16_t width, |
80 uint16_t height, | 81 uint16_t height, |
81 bool send_stream) | 82 bool send_stream) |
82 : encoder(encoder), | 83 : encoder(encoder), |
83 callback(callback), | 84 callback(std::move(callback)), |
84 width(width), | 85 width(width), |
85 height(height), | 86 height(height), |
86 key_frame_request(false), | 87 key_frame_request(false), |
87 send_stream(send_stream) {} | 88 send_stream(send_stream) {} |
88 // Deleted by SimulcastEncoderAdapter::Release(). | 89 // Deleted by SimulcastEncoderAdapter::DestroyStoredEncoders(). |
89 VideoEncoder* encoder; | 90 VideoEncoder* encoder; |
90 EncodedImageCallback* callback; | 91 std::unique_ptr<EncodedImageCallback> callback; |
91 uint16_t width; | 92 uint16_t width; |
92 uint16_t height; | 93 uint16_t height; |
93 bool key_frame_request; | 94 bool key_frame_request; |
94 bool send_stream; | 95 bool send_stream; |
95 }; | 96 }; |
96 | 97 |
97 // Populate the codec settings for each stream. | 98 // Populate the codec settings for each simulcast stream. |
98 void PopulateStreamCodec(const webrtc::VideoCodec* inst, | 99 static void PopulateStreamCodec(const webrtc::VideoCodec& inst, |
99 int stream_index, | 100 int stream_index, |
100 uint32_t start_bitrate_kbps, | 101 uint32_t start_bitrate_kbps, |
101 bool highest_resolution_stream, | 102 bool highest_resolution_stream, |
102 webrtc::VideoCodec* stream_codec); | 103 webrtc::VideoCodec* stream_codec); |
103 | 104 |
104 bool Initialized() const; | 105 bool Initialized() const; |
105 | 106 |
106 std::unique_ptr<VideoEncoderFactory> factory_; | 107 void DestroyStoredEncoders(); |
| 108 |
| 109 volatile int inited_; // Accessed atomically. |
| 110 const std::unique_ptr<VideoEncoderFactory> factory_; |
107 VideoCodec codec_; | 111 VideoCodec codec_; |
108 std::vector<StreamInfo> streaminfos_; | 112 std::vector<StreamInfo> streaminfos_; |
109 EncodedImageCallback* encoded_complete_callback_; | 113 EncodedImageCallback* encoded_complete_callback_; |
110 std::string implementation_name_; | 114 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_; |
111 }; | 122 }; |
112 | 123 |
113 } // namespace webrtc | 124 } // namespace webrtc |
114 | 125 |
115 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ | 126 #endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_VP8_SIMULCAST_ENCODER_ADAPTER_H_ |
OLD | NEW |