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