blob: 3ba56de54d836c45ccf789aba18444f3ab0100df [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
2 * Copyright (c) 2012 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
pbos@webrtc.org281cff82013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_codec_impl.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000012
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +000013#include <list>
14
pbos@webrtc.org281cff82013-05-17 13:44:48 +000015#include "webrtc/engine_configurations.h"
16#include "webrtc/modules/video_coding/main/interface/video_coding.h"
17#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.org281cff82013-05-17 13:44:48 +000018#include "webrtc/video_engine/include/vie_errors.h"
19#include "webrtc/video_engine/vie_capturer.h"
20#include "webrtc/video_engine/vie_channel.h"
21#include "webrtc/video_engine/vie_channel_manager.h"
22#include "webrtc/video_engine/vie_defines.h"
23#include "webrtc/video_engine/vie_encoder.h"
24#include "webrtc/video_engine/vie_impl.h"
25#include "webrtc/video_engine/vie_input_manager.h"
26#include "webrtc/video_engine/vie_shared_data.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000027
28namespace webrtc {
29
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000030static void LogCodec(const VideoCodec& codec) {
31 LOG(LS_INFO) << "CodecType " << codec.codecType
asapersson@webrtc.org1d95c5a2014-04-25 07:02:52 +000032 << ", pl_type " << static_cast<int>(codec.plType)
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000033 << ", resolution " << codec.width
34 << " x " << codec.height
35 << ", start br " << codec.startBitrate
36 << ", min br " << codec.minBitrate
37 << ", max br " << codec.maxBitrate
asapersson@webrtc.org1d95c5a2014-04-25 07:02:52 +000038 << ", max fps " << static_cast<int>(codec.maxFramerate)
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000039 << ", max qp " << codec.qpMax
40 << ", number of streams "
asapersson@webrtc.org1d95c5a2014-04-25 07:02:52 +000041 << static_cast<int>(codec.numberOfSimulcastStreams);
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000042 if (codec.codecType == kVideoCodecVP8) {
43 LOG(LS_INFO) << "VP8 specific settings";
asapersson@webrtc.org1d95c5a2014-04-25 07:02:52 +000044 LOG(LS_INFO) << "pictureLossIndicationOn "
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000045 << codec.codecSpecific.VP8.pictureLossIndicationOn
46 << ", feedbackModeOn "
47 << codec.codecSpecific.VP8.feedbackModeOn
48 << ", complexity "
49 << codec.codecSpecific.VP8.complexity
50 << ", resilience "
51 << codec.codecSpecific.VP8.resilience
asapersson@webrtc.org1d95c5a2014-04-25 07:02:52 +000052 << ", numberOfTemporalLayers "
53 << static_cast<int>(
54 codec.codecSpecific.VP8.numberOfTemporalLayers)
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000055 << ", keyFrameinterval "
56 << codec.codecSpecific.VP8.keyFrameInterval;
57 for (int idx = 0; idx < codec.numberOfSimulcastStreams; ++idx) {
58 LOG(LS_INFO) << "Stream " << codec.simulcastStream[idx].width
59 << " x " << codec.simulcastStream[idx].height;
60 LOG(LS_INFO) << "Temporal layers "
asapersson@webrtc.org1d95c5a2014-04-25 07:02:52 +000061 << static_cast<int>(
62 codec.simulcastStream[idx].numberOfTemporalLayers)
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000063 << ", min br "
64 << codec.simulcastStream[idx].minBitrate
65 << ", target br "
66 << codec.simulcastStream[idx].targetBitrate
67 << ", max br "
68 << codec.simulcastStream[idx].maxBitrate
69 << ", qp max "
70 << codec.simulcastStream[idx].qpMax;
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000071 }
72 }
73}
74
75
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000076ViECodec* ViECodec::GetInterface(VideoEngine* video_engine) {
77#ifdef WEBRTC_VIDEO_ENGINE_CODEC_API
78 if (!video_engine) {
79 return NULL;
80 }
andrew@webrtc.org7ab72682013-05-09 02:12:07 +000081 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000082 ViECodecImpl* vie_codec_impl = vie_impl;
83 // Increase ref count.
84 (*vie_codec_impl)++;
85 return vie_codec_impl;
86#else
87 return NULL;
88#endif
89}
90
91int ViECodecImpl::Release() {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000092 LOG(LS_INFO) << "ViECodec::Release.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000093 // Decrease ref count.
94 (*this)--;
95
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000096 int32_t ref_count = GetCount();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000097 if (ref_count < 0) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +000098 LOG(LS_WARNING) << "ViECodec released too many times.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000099 shared_data_->SetLastError(kViEAPIDoesNotExist);
100 return -1;
101 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000102 return ref_count;
103}
104
105ViECodecImpl::ViECodecImpl(ViESharedData* shared_data)
106 : shared_data_(shared_data) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000107}
108
109ViECodecImpl::~ViECodecImpl() {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000110}
111
112int ViECodecImpl::NumberOfCodecs() const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000113 // +2 because of FEC(RED and ULPFEC)
114 return static_cast<int>((VideoCodingModule::NumberOfCodecs() + 2));
115}
116
117int ViECodecImpl::GetCodec(const unsigned char list_number,
118 VideoCodec& video_codec) const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000119 if (list_number == VideoCodingModule::NumberOfCodecs()) {
120 memset(&video_codec, 0, sizeof(VideoCodec));
mflodman@webrtc.orgbea854a2013-04-22 12:41:57 +0000121 strcpy(video_codec.plName, "red");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000122 video_codec.codecType = kVideoCodecRED;
123 video_codec.plType = VCM_RED_PAYLOAD_TYPE;
124 } else if (list_number == VideoCodingModule::NumberOfCodecs() + 1) {
125 memset(&video_codec, 0, sizeof(VideoCodec));
mflodman@webrtc.orgbea854a2013-04-22 12:41:57 +0000126 strcpy(video_codec.plName, "ulpfec");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000127 video_codec.codecType = kVideoCodecULPFEC;
128 video_codec.plType = VCM_ULPFEC_PAYLOAD_TYPE;
129 } else if (VideoCodingModule::Codec(list_number, &video_codec) != VCM_OK) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000130 shared_data_->SetLastError(kViECodecInvalidArgument);
131 return -1;
132 }
133 return 0;
134}
135
136int ViECodecImpl::SetSendCodec(const int video_channel,
137 const VideoCodec& video_codec) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000138 LOG(LS_INFO) << "SetSendCodec for channel " << video_channel;
139 LogCodec(video_codec);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000140 if (!CodecValid(video_codec)) {
141 // Error logged.
142 shared_data_->SetLastError(kViECodecInvalidCodec);
143 return -1;
144 }
145
146 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
147 ViEChannel* vie_channel = cs.Channel(video_channel);
148 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000149 shared_data_->SetLastError(kViECodecInvalidChannelId);
150 return -1;
151 }
152
153 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
154 assert(vie_encoder);
155 if (vie_encoder->Owner() != video_channel) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000156 LOG_F(LS_ERROR) << "Receive only channel.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000157 shared_data_->SetLastError(kViECodecReceiveOnlyChannel);
158 return -1;
159 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000160 // Set a max_bitrate if the user hasn't set one.
161 VideoCodec video_codec_internal;
162 memcpy(&video_codec_internal, &video_codec, sizeof(VideoCodec));
163 if (video_codec_internal.maxBitrate == 0) {
164 // Max is one bit per pixel.
165 video_codec_internal.maxBitrate = (video_codec_internal.width *
166 video_codec_internal.height *
167 video_codec_internal.maxFramerate)
168 / 1000;
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000169 LOG(LS_INFO) << "New max bitrate set " << video_codec_internal.maxBitrate;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000170 }
171
mflodman@webrtc.org54352082014-04-29 12:38:42 +0000172 if (video_codec_internal.startBitrate < video_codec_internal.minBitrate) {
173 video_codec_internal.startBitrate = video_codec_internal.minBitrate;
174 }
mflodman@webrtc.orga9a73272013-12-20 15:07:12 +0000175 if (video_codec_internal.startBitrate > video_codec_internal.maxBitrate) {
176 video_codec_internal.startBitrate = video_codec_internal.maxBitrate;
177 }
178
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000179 VideoCodec encoder;
180 vie_encoder->GetEncoder(&encoder);
181
182 // Make sure to generate a new SSRC if the codec type and/or resolution has
183 // changed. This won't have any effect if the user has set an SSRC.
184 bool new_rtp_stream = false;
185 if (encoder.codecType != video_codec_internal.codecType) {
186 new_rtp_stream = true;
187 }
188
189 ViEInputManagerScoped is(*(shared_data_->input_manager()));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000190
191 // Stop the media flow while reconfiguring.
192 vie_encoder->Pause();
193
mflodman@webrtc.orga18c6e52013-06-07 13:57:57 +0000194 if (vie_encoder->SetEncoder(video_codec_internal) != 0) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000195 shared_data_->SetLastError(kViECodecUnknownError);
196 return -1;
197 }
198
199 // Give the channel(s) the new information.
200 ChannelList channels;
201 cs.ChannelsUsingViEEncoder(video_channel, &channels);
202 for (ChannelList::iterator it = channels.begin(); it != channels.end();
203 ++it) {
204 bool ret = true;
205 if ((*it)->SetSendCodec(video_codec_internal, new_rtp_stream) != 0) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000206 ret = false;
207 }
208 if (!ret) {
209 shared_data_->SetLastError(kViECodecUnknownError);
210 return -1;
211 }
212 }
213
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000214 // TODO(mflodman) Break out this part in GetLocalSsrcList().
215 // Update all SSRCs to ViEEncoder.
216 std::list<unsigned int> ssrcs;
217 if (video_codec_internal.numberOfSimulcastStreams == 0) {
218 unsigned int ssrc = 0;
219 if (vie_channel->GetLocalSSRC(0, &ssrc) != 0) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000220 LOG_F(LS_ERROR) << "Could not get ssrc.";
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000221 }
222 ssrcs.push_back(ssrc);
223 } else {
224 for (int idx = 0; idx < video_codec_internal.numberOfSimulcastStreams;
225 ++idx) {
226 unsigned int ssrc = 0;
227 if (vie_channel->GetLocalSSRC(idx, &ssrc) != 0) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000228 LOG_F(LS_ERROR) << "Could not get ssrc for stream " << idx;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000229 }
230 ssrcs.push_back(ssrc);
231 }
232 }
233 vie_encoder->SetSsrcs(ssrcs);
234 shared_data_->channel_manager()->UpdateSsrcs(video_channel, ssrcs);
235
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000236 // Update the protection mode, we might be switching NACK/FEC.
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000237 vie_encoder->UpdateProtectionMethod(vie_encoder->nack_enabled());
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000238
239 // Get new best format for frame provider.
pbos@webrtc.org87045952013-08-05 09:03:03 +0000240 ViEFrameProviderBase* frame_provider = is.FrameProvider(vie_encoder);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000241 if (frame_provider) {
242 frame_provider->FrameCallbackChanged();
243 }
244 // Restart the media flow
245 if (new_rtp_stream) {
246 // Stream settings changed, make sure we get a key frame.
247 vie_encoder->SendKeyFrame();
248 }
249 vie_encoder->Restart();
250 return 0;
251}
252
253int ViECodecImpl::GetSendCodec(const int video_channel,
254 VideoCodec& video_codec) const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000255 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
256 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
257 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000258 shared_data_->SetLastError(kViECodecInvalidChannelId);
259 return -1;
260 }
261 return vie_encoder->GetEncoder(&video_codec);
262}
263
264int ViECodecImpl::SetReceiveCodec(const int video_channel,
265 const VideoCodec& video_codec) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000266 LOG(LS_INFO) << "SetReceiveCodec for channel " << video_channel;
267 LOG(LS_INFO) << "Codec type " << video_codec.codecType
268 << ", payload type " << video_codec.plType;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000269
270 if (CodecValid(video_codec) == false) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000271 shared_data_->SetLastError(kViECodecInvalidCodec);
272 return -1;
273 }
274
275 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
276 ViEChannel* vie_channel = cs.Channel(video_channel);
277 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000278 shared_data_->SetLastError(kViECodecInvalidChannelId);
279 return -1;
280 }
281
282 if (vie_channel->SetReceiveCodec(video_codec) != 0) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000283 shared_data_->SetLastError(kViECodecUnknownError);
284 return -1;
285 }
286 return 0;
287}
288
289int ViECodecImpl::GetReceiveCodec(const int video_channel,
290 VideoCodec& video_codec) const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000291 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
292 ViEChannel* vie_channel = cs.Channel(video_channel);
293 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000294 shared_data_->SetLastError(kViECodecInvalidChannelId);
295 return -1;
296 }
297
298 if (vie_channel->GetReceiveCodec(&video_codec) != 0) {
299 shared_data_->SetLastError(kViECodecUnknownError);
300 return -1;
301 }
302 return 0;
303}
304
305int ViECodecImpl::GetCodecConfigParameters(
306 const int video_channel,
307 unsigned char config_parameters[kConfigParameterSize],
308 unsigned char& config_parameters_size) const {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000309 LOG(LS_INFO) << "GetCodecConfigParameters " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000310
311 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
312 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
313 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000314 shared_data_->SetLastError(kViECodecInvalidChannelId);
315 return -1;
316 }
317
318 if (vie_encoder->GetCodecConfigParameters(config_parameters,
319 config_parameters_size) != 0) {
320 shared_data_->SetLastError(kViECodecUnknownError);
321 return -1;
322 }
323 return 0;
324}
325
326int ViECodecImpl::SetImageScaleStatus(const int video_channel,
327 const bool enable) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000328 LOG(LS_INFO) << "SetImageScaleStates for channel " << video_channel
329 << ", enable: " << enable;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000330
331 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
332 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
333 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000334 shared_data_->SetLastError(kViECodecInvalidChannelId);
335 return -1;
336 }
337
338 if (vie_encoder->ScaleInputImage(enable) != 0) {
339 shared_data_->SetLastError(kViECodecUnknownError);
340 return -1;
341 }
342 return 0;
343}
344
345int ViECodecImpl::GetSendCodecStastistics(const int video_channel,
346 unsigned int& key_frames,
347 unsigned int& delta_frames) const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000348 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
349 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
350 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000351 shared_data_->SetLastError(kViECodecInvalidChannelId);
352 return -1;
353 }
354
355 if (vie_encoder->SendCodecStatistics(&key_frames, &delta_frames) != 0) {
356 shared_data_->SetLastError(kViECodecUnknownError);
357 return -1;
358 }
359 return 0;
360}
361
362int ViECodecImpl::GetReceiveCodecStastistics(const int video_channel,
363 unsigned int& key_frames,
364 unsigned int& delta_frames) const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000365 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
366 ViEChannel* vie_channel = cs.Channel(video_channel);
367 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000368 shared_data_->SetLastError(kViECodecInvalidChannelId);
369 return -1;
370 }
371 if (vie_channel->ReceiveCodecStatistics(&key_frames, &delta_frames) != 0) {
372 shared_data_->SetLastError(kViECodecUnknownError);
373 return -1;
374 }
375 return 0;
376}
377
mflodman@webrtc.orgf314c802012-12-14 14:02:10 +0000378int ViECodecImpl::GetReceiveSideDelay(const int video_channel,
379 int* delay_ms) const {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000380 assert(delay_ms != NULL);
mflodman@webrtc.orgf314c802012-12-14 14:02:10 +0000381
382 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
383 ViEChannel* vie_channel = cs.Channel(video_channel);
384 if (!vie_channel) {
mflodman@webrtc.orgf314c802012-12-14 14:02:10 +0000385 shared_data_->SetLastError(kViECodecInvalidChannelId);
386 return -1;
387 }
388 *delay_ms = vie_channel->ReceiveDelay();
389 if (*delay_ms < 0) {
390 return -1;
391 }
392 return 0;
393}
394
395
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000396int ViECodecImpl::GetCodecTargetBitrate(const int video_channel,
397 unsigned int* bitrate) const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000398 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
399 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
400 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000401 shared_data_->SetLastError(kViECodecInvalidChannelId);
402 return -1;
403 }
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000404 return vie_encoder->CodecTargetBitrate(static_cast<uint32_t*>(bitrate));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000405}
406
407unsigned int ViECodecImpl::GetDiscardedPackets(const int video_channel) const {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000408 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
409 ViEChannel* vie_channel = cs.Channel(video_channel);
410 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000411 shared_data_->SetLastError(kViECodecInvalidChannelId);
412 return -1;
413 }
414 return vie_channel->DiscardedPackets();
415}
416
417int ViECodecImpl::SetKeyFrameRequestCallbackStatus(const int video_channel,
418 const bool enable) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000419 LOG(LS_INFO) << "SetKeyFrameRequestCallbackStatus for " << video_channel
420 << ", enacle " << enable;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000421
422 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
423 ViEChannel* vie_channel = cs.Channel(video_channel);
424 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000425 shared_data_->SetLastError(kViECodecInvalidChannelId);
426 return -1;
427 }
428 if (vie_channel->EnableKeyFrameRequestCallback(enable) != 0) {
429 shared_data_->SetLastError(kViECodecUnknownError);
430 return -1;
431 }
432 return 0;
433}
434
435int ViECodecImpl::SetSignalKeyPacketLossStatus(const int video_channel,
436 const bool enable,
437 const bool only_key_frames) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000438 LOG(LS_INFO) << "SetSignalKeyPacketLossStatus for " << video_channel
439 << "enable, " << enable
440 << ", only key frames " << only_key_frames;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000441
442 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
443 ViEChannel* vie_channel = cs.Channel(video_channel);
444 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000445 shared_data_->SetLastError(kViECodecInvalidChannelId);
446 return -1;
447 }
448 if (vie_channel->SetSignalPacketLossStatus(enable, only_key_frames) != 0) {
449 shared_data_->SetLastError(kViECodecUnknownError);
450 return -1;
451 }
452 return 0;
453}
454
455int ViECodecImpl::RegisterEncoderObserver(const int video_channel,
456 ViEEncoderObserver& observer) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000457 LOG(LS_INFO) << "RegisterEncoderObserver for channel " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000458
459 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
460 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
461 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000462 shared_data_->SetLastError(kViECodecInvalidChannelId);
463 return -1;
464 }
465 if (vie_encoder->RegisterCodecObserver(&observer) != 0) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000466 shared_data_->SetLastError(kViECodecObserverAlreadyRegistered);
467 return -1;
468 }
469 return 0;
470}
471
472int ViECodecImpl::DeregisterEncoderObserver(const int video_channel) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000473 LOG(LS_INFO) << "DeregisterEncoderObserver for channel " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000474
475 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
476 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
477 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000478 shared_data_->SetLastError(kViECodecInvalidChannelId);
479 return -1;
480 }
481 if (vie_encoder->RegisterCodecObserver(NULL) != 0) {
482 shared_data_->SetLastError(kViECodecObserverNotRegistered);
483 return -1;
484 }
485 return 0;
486}
487
488int ViECodecImpl::RegisterDecoderObserver(const int video_channel,
489 ViEDecoderObserver& observer) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000490 LOG(LS_INFO) << "RegisterDecoderObserver for channel " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000491
492 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
493 ViEChannel* vie_channel = cs.Channel(video_channel);
494 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000495 shared_data_->SetLastError(kViECodecInvalidChannelId);
496 return -1;
497 }
498 if (vie_channel->RegisterCodecObserver(&observer) != 0) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000499 shared_data_->SetLastError(kViECodecObserverAlreadyRegistered);
500 return -1;
501 }
502 return 0;
503}
504
505int ViECodecImpl::DeregisterDecoderObserver(const int video_channel) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000506 LOG(LS_INFO) << "DeregisterDecodeObserver for channel " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000507
508 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
509 ViEChannel* vie_channel = cs.Channel(video_channel);
510 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000511 shared_data_->SetLastError(kViECodecInvalidChannelId);
512 return -1;
513 }
514 if (vie_channel->RegisterCodecObserver(NULL) != 0) {
515 shared_data_->SetLastError(kViECodecObserverNotRegistered);
516 return -1;
517 }
518 return 0;
519}
520
521int ViECodecImpl::SendKeyFrame(const int video_channel) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000522 LOG(LS_INFO) << "SendKeyFrame on channel " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000523
524 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
525 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
526 if (!vie_encoder) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000527 shared_data_->SetLastError(kViECodecInvalidChannelId);
528 return -1;
529 }
530 if (vie_encoder->SendKeyFrame() != 0) {
531 shared_data_->SetLastError(kViECodecUnknownError);
532 return -1;
533 }
534 return 0;
535}
536
537int ViECodecImpl::WaitForFirstKeyFrame(const int video_channel,
538 const bool wait) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000539 LOG(LS_INFO) << "WaitForFirstKeyFrame for channel " << video_channel
540 << ", wait " << wait;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000541
542 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
543 ViEChannel* vie_channel = cs.Channel(video_channel);
544 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000545 shared_data_->SetLastError(kViECodecInvalidChannelId);
546 return -1;
547 }
548 if (vie_channel->WaitForKeyFrame(wait) != 0) {
549 shared_data_->SetLastError(kViECodecUnknownError);
550 return -1;
551 }
552 return 0;
553}
554
mflodman@webrtc.org0291c802013-06-26 09:12:49 +0000555int ViECodecImpl::StartDebugRecording(int video_channel,
556 const char* file_name_utf8) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000557 LOG(LS_INFO) << "StartDebugRecording for channel " << video_channel;
mflodman@webrtc.org0291c802013-06-26 09:12:49 +0000558 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
559 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
560 if (!vie_encoder) {
mflodman@webrtc.org0291c802013-06-26 09:12:49 +0000561 return -1;
562 }
563 return vie_encoder->StartDebugRecording(file_name_utf8);
564}
565
566int ViECodecImpl::StopDebugRecording(int video_channel) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000567 LOG(LS_INFO) << "StopDebugRecording for channel " << video_channel;
mflodman@webrtc.org0291c802013-06-26 09:12:49 +0000568 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
569 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
570 if (!vie_encoder) {
mflodman@webrtc.org0291c802013-06-26 09:12:49 +0000571 return -1;
572 }
573 return vie_encoder->StopDebugRecording();
574}
575
henrik.lundin@webrtc.org45901772013-11-18 12:18:43 +0000576void ViECodecImpl::SuspendBelowMinBitrate(int video_channel) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000577 LOG(LS_INFO) << "SuspendBelowMinBitrate for channel " << video_channel;
henrik.lundin@webrtc.org39079d12013-10-02 13:34:26 +0000578 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
579 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
580 if (!vie_encoder) {
henrik.lundin@webrtc.org39079d12013-10-02 13:34:26 +0000581 return;
582 }
henrik.lundin@webrtc.orgb9f1eb82013-11-21 14:05:40 +0000583 vie_encoder->SuspendBelowMinBitrate();
584 ViEChannel* vie_channel = cs.Channel(video_channel);
585 if (!vie_channel) {
henrik.lundin@webrtc.orgb9f1eb82013-11-21 14:05:40 +0000586 return;
587 }
588 // Must enable pacing when enabling SuspendBelowMinBitrate. Otherwise, no
589 // padding will be sent when the video is suspended so the video will be
590 // unable to recover.
591 vie_channel->SetTransmissionSmoothingStatus(true);
henrik.lundin@webrtc.org39079d12013-10-02 13:34:26 +0000592}
593
stefan@webrtc.orgee234be2013-12-05 14:05:07 +0000594bool ViECodecImpl::GetSendSideDelay(int video_channel, int* avg_delay_ms,
595 int* max_delay_ms) const {
596 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
597 ViEChannel* vie_channel = cs.Channel(video_channel);
598 if (!vie_channel) {
stefan@webrtc.orgee234be2013-12-05 14:05:07 +0000599 shared_data_->SetLastError(kViECodecInvalidChannelId);
600 return false;
601 }
602 return vie_channel->GetSendSideDelay(avg_delay_ms, max_delay_ms);
603}
604
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000605bool ViECodecImpl::CodecValid(const VideoCodec& video_codec) {
606 // Check pl_name matches codec_type.
607 if (video_codec.codecType == kVideoCodecRED) {
608#if defined(WIN32)
609 if (_strnicmp(video_codec.plName, "red", 3) == 0) {
610#else
611 if (strncasecmp(video_codec.plName, "red", 3) == 0) {
612#endif
613 // We only care about the type and name for red.
614 return true;
615 }
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000616 LOG_F(LS_ERROR) << "Invalid RED configuration.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000617 return false;
618 } else if (video_codec.codecType == kVideoCodecULPFEC) {
619#if defined(WIN32)
620 if (_strnicmp(video_codec.plName, "ULPFEC", 6) == 0) {
621#else
622 if (strncasecmp(video_codec.plName, "ULPFEC", 6) == 0) {
623#endif
624 // We only care about the type and name for ULPFEC.
625 return true;
626 }
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000627 LOG_F(LS_ERROR) << "Invalid ULPFEC configuration.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000628 return false;
629 } else if ((video_codec.codecType == kVideoCodecVP8 &&
pbos@webrtc.orge3339fc2013-03-18 16:39:03 +0000630 strncmp(video_codec.plName, "VP8", 4) == 0) ||
631 (video_codec.codecType == kVideoCodecI420 &&
pbos@webrtc.org2ed1cd92013-04-05 13:27:38 +0000632 strncmp(video_codec.plName, "I420", 4) == 0)) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000633 // OK.
pbos@webrtc.org2ed1cd92013-04-05 13:27:38 +0000634 } else if (video_codec.codecType != kVideoCodecGeneric) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000635 LOG(LS_ERROR) << "Codec type and name mismatch.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000636 return false;
637 }
638
braveyao@webrtc.orge7a9bd42013-01-14 01:52:26 +0000639 if (video_codec.plType == 0 || video_codec.plType > 127) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000640 LOG(LS_ERROR) << "Invalif payload type: " << video_codec.plType;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000641 return false;
642 }
643
644 if (video_codec.width > kViEMaxCodecWidth ||
645 video_codec.height > kViEMaxCodecHeight) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000646 LOG(LS_ERROR) << "Invalid codec resolution " << video_codec.width
647 << " x " << video_codec.height;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000648 return false;
649 }
650
651 if (video_codec.startBitrate < kViEMinCodecBitrate) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000652 LOG(LS_ERROR) << "Invalid start bitrate.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000653 return false;
654 }
655 if (video_codec.minBitrate < kViEMinCodecBitrate) {
mflodman@webrtc.org022615b2014-04-07 10:56:31 +0000656 LOG(LS_ERROR) << "Invalid min bitrate.";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000657 return false;
658 }
659 return true;
660}
661
662} // namespace webrtc