OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } // namespace | 87 } // namespace |
88 | 88 |
89 //#define DEBUG_ENCODER_BIT_STREAM | 89 //#define DEBUG_ENCODER_BIT_STREAM |
90 | 90 |
91 VCMGenericEncoder::VCMGenericEncoder(VideoEncoder* encoder, | 91 VCMGenericEncoder::VCMGenericEncoder(VideoEncoder* encoder, |
92 VideoEncoderRateObserver* rate_observer, | 92 VideoEncoderRateObserver* rate_observer, |
93 bool internalSource) | 93 bool internalSource) |
94 : encoder_(encoder), | 94 : encoder_(encoder), |
95 rate_observer_(rate_observer), | 95 rate_observer_(rate_observer), |
96 vcm_encoded_frame_callback_(nullptr), | 96 vcm_encoded_frame_callback_(nullptr), |
97 bit_rate_(0), | 97 encoder_params_({0, 0, 0, 0}), |
98 frame_rate_(0), | |
99 internal_source_(internalSource), | 98 internal_source_(internalSource), |
100 rotation_(kVideoRotation_0), | 99 rotation_(kVideoRotation_0), |
101 is_screenshare_(false) { | 100 is_screenshare_(false) {} |
102 } | |
103 | 101 |
104 VCMGenericEncoder::~VCMGenericEncoder() | 102 VCMGenericEncoder::~VCMGenericEncoder() |
105 { | 103 { |
106 } | 104 } |
107 | 105 |
108 int32_t VCMGenericEncoder::Release() | 106 int32_t VCMGenericEncoder::Release() |
109 { | 107 { |
110 { | 108 { |
111 rtc::CritScope lock(&rates_lock_); | 109 rtc::CritScope lock(¶ms_lock_); |
112 bit_rate_ = 0; | 110 encoder_params_ = {0, 0, 0, 0}; |
113 frame_rate_ = 0; | |
114 vcm_encoded_frame_callback_ = nullptr; | 111 vcm_encoded_frame_callback_ = nullptr; |
115 } | 112 } |
116 | 113 |
117 return encoder_->Release(); | 114 return encoder_->Release(); |
118 } | 115 } |
119 | 116 |
120 int32_t | 117 int32_t |
121 VCMGenericEncoder::InitEncode(const VideoCodec* settings, | 118 VCMGenericEncoder::InitEncode(const VideoCodec* settings, |
122 int32_t numberOfCores, | 119 int32_t numberOfCores, |
123 size_t maxPayloadSize) | 120 size_t maxPayloadSize) |
124 { | 121 { |
125 { | 122 { |
126 rtc::CritScope lock(&rates_lock_); | 123 rtc::CritScope lock(¶ms_lock_); |
127 bit_rate_ = settings->startBitrate * 1000; | 124 encoder_params_.target_bitrate = settings->startBitrate * 1000; |
128 frame_rate_ = settings->maxFramerate; | 125 encoder_params_.input_frame_rate = settings->maxFramerate; |
129 } | 126 } |
130 | 127 |
131 is_screenshare_ = settings->mode == VideoCodecMode::kScreensharing; | 128 is_screenshare_ = settings->mode == VideoCodecMode::kScreensharing; |
132 if (encoder_->InitEncode(settings, numberOfCores, maxPayloadSize) != 0) { | 129 if (encoder_->InitEncode(settings, numberOfCores, maxPayloadSize) != 0) { |
133 LOG(LS_ERROR) << "Failed to initialize the encoder associated with " | 130 LOG(LS_ERROR) << "Failed to initialize the encoder associated with " |
134 "payload name: " << settings->plName; | 131 "payload name: " << settings->plName; |
135 return -1; | 132 return -1; |
136 } | 133 } |
137 return 0; | 134 return 0; |
138 } | 135 } |
(...skipping 16 matching lines...) Expand all Loading... |
155 int32_t result = encoder_->Encode(inputFrame, codecSpecificInfo, &frameTypes); | 152 int32_t result = encoder_->Encode(inputFrame, codecSpecificInfo, &frameTypes); |
156 if (is_screenshare_ && | 153 if (is_screenshare_ && |
157 result == WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT) { | 154 result == WEBRTC_VIDEO_CODEC_TARGET_BITRATE_OVERSHOOT) { |
158 // Target bitrate exceeded, encoder state has been reset - try again. | 155 // Target bitrate exceeded, encoder state has been reset - try again. |
159 return encoder_->Encode(inputFrame, codecSpecificInfo, &frameTypes); | 156 return encoder_->Encode(inputFrame, codecSpecificInfo, &frameTypes); |
160 } | 157 } |
161 | 158 |
162 return result; | 159 return result; |
163 } | 160 } |
164 | 161 |
165 int32_t | 162 void VCMGenericEncoder::SetEncoderParameters(const EncoderParameters& params) { |
166 VCMGenericEncoder::SetChannelParameters(int32_t packetLoss, int64_t rtt) | 163 bool channel_parameters_have_changed; |
167 { | 164 bool rates_have_changed; |
168 return encoder_->SetChannelParameters(packetLoss, rtt); | 165 { |
| 166 rtc::CritScope lock(¶ms_lock_); |
| 167 channel_parameters_have_changed = |
| 168 params.loss_rate != encoder_params_.loss_rate || |
| 169 params.rtt != encoder_params_.rtt; |
| 170 rates_have_changed = |
| 171 params.target_bitrate != encoder_params_.target_bitrate || |
| 172 params.input_frame_rate != encoder_params_.input_frame_rate; |
| 173 encoder_params_ = params; |
| 174 } |
| 175 if (channel_parameters_have_changed) |
| 176 encoder_->SetChannelParameters(params.loss_rate, params.rtt); |
| 177 if (rates_have_changed) { |
| 178 uint32_t target_bitrate_kbps = (params.target_bitrate + 500) / 1000; |
| 179 encoder_->SetRates(target_bitrate_kbps, params.input_frame_rate); |
| 180 if (rate_observer_ != nullptr) { |
| 181 rate_observer_->OnSetRates(params.target_bitrate, |
| 182 params.input_frame_rate); |
| 183 } |
| 184 } |
| 185 } |
| 186 |
| 187 EncoderParameters VCMGenericEncoder::GetEncoderParameters() const { |
| 188 rtc::CritScope lock(¶ms_lock_); |
| 189 return encoder_params_; |
169 } | 190 } |
170 | 191 |
171 int32_t | 192 int32_t |
172 VCMGenericEncoder::SetRates(uint32_t newBitRate, uint32_t frameRate) | |
173 { | |
174 uint32_t target_bitrate_kbps = (newBitRate + 500) / 1000; | |
175 int32_t ret = encoder_->SetRates(target_bitrate_kbps, frameRate); | |
176 if (ret < 0) | |
177 { | |
178 return ret; | |
179 } | |
180 | |
181 { | |
182 rtc::CritScope lock(&rates_lock_); | |
183 bit_rate_ = newBitRate; | |
184 frame_rate_ = frameRate; | |
185 } | |
186 | |
187 if (rate_observer_ != nullptr) | |
188 rate_observer_->OnSetRates(newBitRate, frameRate); | |
189 return VCM_OK; | |
190 } | |
191 | |
192 int32_t | |
193 VCMGenericEncoder::CodecConfigParameters(uint8_t* buffer, int32_t size) | |
194 { | |
195 int32_t ret = encoder_->CodecConfigParameters(buffer, size); | |
196 if (ret < 0) | |
197 { | |
198 return ret; | |
199 } | |
200 return ret; | |
201 } | |
202 | |
203 uint32_t VCMGenericEncoder::BitRate() const | |
204 { | |
205 rtc::CritScope lock(&rates_lock_); | |
206 return bit_rate_; | |
207 } | |
208 | |
209 uint32_t VCMGenericEncoder::FrameRate() const | |
210 { | |
211 rtc::CritScope lock(&rates_lock_); | |
212 return frame_rate_; | |
213 } | |
214 | |
215 int32_t | |
216 VCMGenericEncoder::SetPeriodicKeyFrames(bool enable) | 193 VCMGenericEncoder::SetPeriodicKeyFrames(bool enable) |
217 { | 194 { |
218 return encoder_->SetPeriodicKeyFrames(enable); | 195 return encoder_->SetPeriodicKeyFrames(enable); |
219 } | 196 } |
220 | 197 |
221 int32_t VCMGenericEncoder::RequestFrame( | 198 int32_t VCMGenericEncoder::RequestFrame( |
222 const std::vector<FrameType>& frame_types) { | 199 const std::vector<FrameType>& frame_types) { |
223 VideoFrame image; | 200 VideoFrame image; |
224 return encoder_->Encode(image, NULL, &frame_types); | 201 return encoder_->Encode(image, NULL, &frame_types); |
225 } | 202 } |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 } | 304 } |
328 | 305 |
329 void | 306 void |
330 VCMEncodedFrameCallback::SetMediaOpt( | 307 VCMEncodedFrameCallback::SetMediaOpt( |
331 media_optimization::MediaOptimization *mediaOpt) | 308 media_optimization::MediaOptimization *mediaOpt) |
332 { | 309 { |
333 _mediaOpt = mediaOpt; | 310 _mediaOpt = mediaOpt; |
334 } | 311 } |
335 | 312 |
336 } // namespace webrtc | 313 } // namespace webrtc |
OLD | NEW |