blob: 60edcf1d1861b0b5d26a49eaa0af803933d9e3f9 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000011#include "video_engine/vie_external_codec_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.org9ba151b2012-06-21 10:02:13 +000013#include "engine_configurations.h" // NOLINT
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000014#include "system_wrappers/interface/trace.h"
mflodman@webrtc.orga4863db2011-12-22 08:51:52 +000015#include "video_engine/include/vie_errors.h"
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000016#include "video_engine/vie_channel.h"
17#include "video_engine/vie_channel_manager.h"
18#include "video_engine/vie_encoder.h"
19#include "video_engine/vie_impl.h"
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000020#include "video_engine/vie_shared_data.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000024ViEExternalCodec* ViEExternalCodec::GetInterface(VideoEngine* video_engine) {
niklase@google.com470e71d2011-07-07 08:21:25 +000025#ifdef WEBRTC_VIDEO_ENGINE_EXTERNAL_CODEC_API
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000026 if (video_engine == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000027 return NULL;
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000028 }
29 VideoEngineImpl* vie_impl = reinterpret_cast<VideoEngineImpl*>(video_engine);
30 ViEExternalCodecImpl* vie_external_codec_impl = vie_impl;
31 // Increase ref count.
32 (*vie_external_codec_impl)++;
33 return vie_external_codec_impl;
34#else
35 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000036#endif
37}
38
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000039int ViEExternalCodecImpl::Release() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000040 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_->instance_id(),
niklase@google.com470e71d2011-07-07 08:21:25 +000041 "ViEExternalCodec::Release()");
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000042 // Decrease ref count.
43 (*this)--;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000045 WebRtc_Word32 ref_count = GetCount();
46 if (ref_count < 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000047 WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000048 "ViEExternalCodec release too many times");
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000049 shared_data_->SetLastError(kViEAPIDoesNotExist);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000050 return -1;
51 }
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000052 WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000053 "ViEExternalCodec reference count: %d", ref_count);
54 return ref_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000055}
56
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000057ViEExternalCodecImpl::ViEExternalCodecImpl(ViESharedData* shared_data)
58 : shared_data_(shared_data) {
59 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
60 "ViEExternalCodecImpl::ViEExternalCodecImpl() Ctor");
61}
62
63ViEExternalCodecImpl::~ViEExternalCodecImpl() {
64 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
65 "ViEExternalCodecImpl::~ViEExternalCodecImpl() Dtor");
66}
67
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000068int ViEExternalCodecImpl::RegisterExternalSendCodec(const int video_channel,
69 const unsigned char pl_type,
stefan@webrtc.orgfcd85852013-01-09 08:35:40 +000070 VideoEncoder* encoder,
71 bool internal_source) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000072 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000073 "%s channel %d pl_type %d encoder 0x%x", __FUNCTION__,
74 video_channel, pl_type, encoder);
niklase@google.com470e71d2011-07-07 08:21:25 +000075
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000076 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000077 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
78 if (!vie_encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000079 WEBRTC_TRACE(kTraceError, kTraceVideo,
80 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000081 "%s: Invalid argument video_channel %u. Does it exist?",
82 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000083 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000084 return -1;
85 }
86 if (!encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000087 WEBRTC_TRACE(kTraceError, kTraceVideo,
88 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000089 "%s: Invalid argument Encoder 0x%x.", __FUNCTION__, encoder);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000090 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000091 return -1;
92 }
niklase@google.com470e71d2011-07-07 08:21:25 +000093
stefan@webrtc.orgfcd85852013-01-09 08:35:40 +000094 if (vie_encoder->RegisterExternalEncoder(encoder, pl_type, internal_source)
95 != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000096 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000097 return -1;
98 }
99 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100}
101
102int ViEExternalCodecImpl::DeRegisterExternalSendCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000103 const int video_channel, const unsigned char pl_type) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000104 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000105 "%s channel %d pl_type %d", __FUNCTION__, video_channel,
106 pl_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000108 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000109 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
110 if (!vie_encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000111 WEBRTC_TRACE(kTraceError, kTraceVideo,
112 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000113 "%s: Invalid argument video_channel %u. Does it exist?",
114 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000115 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000116 return -1;
117 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000119 if (vie_encoder->DeRegisterExternalEncoder(pl_type) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000120 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000121 return -1;
122 }
123 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124}
125
126int ViEExternalCodecImpl::RegisterExternalReceiveCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000127 const int video_channel,
128 const unsigned int pl_type,
129 VideoDecoder* decoder,
130 bool decoder_render,
131 int render_delay) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000132 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000133 "%s channel %d pl_type %d decoder 0x%x, decoder_render %d, "
134 "renderDelay %d", __FUNCTION__, video_channel, pl_type, decoder,
135 decoder_render, render_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000137 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000138 ViEChannel* vie_channel = cs.Channel(video_channel);
139 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000140 WEBRTC_TRACE(kTraceError, kTraceVideo,
141 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000142 "%s: Invalid argument video_channel %u. Does it exist?",
143 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000144 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000145 return -1;
146 }
147 if (!decoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000148 WEBRTC_TRACE(kTraceError, kTraceVideo,
149 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000150 "%s: Invalid argument decoder 0x%x.", __FUNCTION__, decoder);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000151 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000152 return -1;
153 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000155 if (vie_channel->RegisterExternalDecoder(pl_type, decoder, decoder_render,
156 render_delay) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000157 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000158 return -1;
159 }
160 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
163int ViEExternalCodecImpl::DeRegisterExternalReceiveCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000164const int video_channel, const unsigned char pl_type) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000165 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000166 "%s channel %d pl_type %u", __FUNCTION__, video_channel,
167 pl_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000169 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000170 ViEChannel* vie_channel = cs.Channel(video_channel);
171 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000172 WEBRTC_TRACE(kTraceError, kTraceVideo,
173 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000174 "%s: Invalid argument video_channel %u. Does it exist?",
175 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000176 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000177 return -1;
178 }
179 if (vie_channel->DeRegisterExternalDecoder(pl_type) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000180 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000181 return -1;
182 }
183 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000184}
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000185
186} // namespace webrtc