blob: 8c9be012d0f95b39f2fff6d5489447afd49913fb [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,
70 VideoEncoder* encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000071 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000072 "%s channel %d pl_type %d encoder 0x%x", __FUNCTION__,
73 video_channel, pl_type, encoder);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000075 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000076 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
77 if (!vie_encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000078 WEBRTC_TRACE(kTraceError, kTraceVideo,
79 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000080 "%s: Invalid argument video_channel %u. Does it exist?",
81 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000082 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000083 return -1;
84 }
85 if (!encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000086 WEBRTC_TRACE(kTraceError, kTraceVideo,
87 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000088 "%s: Invalid argument Encoder 0x%x.", __FUNCTION__, encoder);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000089 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000090 return -1;
91 }
niklase@google.com470e71d2011-07-07 08:21:25 +000092
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000093 if (vie_encoder->RegisterExternalEncoder(encoder, pl_type) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000094 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000095 return -1;
96 }
97 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000098}
99
100int ViEExternalCodecImpl::DeRegisterExternalSendCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000101 const int video_channel, const unsigned char pl_type) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000102 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000103 "%s channel %d pl_type %d", __FUNCTION__, video_channel,
104 pl_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000106 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000107 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
108 if (!vie_encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000109 WEBRTC_TRACE(kTraceError, kTraceVideo,
110 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000111 "%s: Invalid argument video_channel %u. Does it exist?",
112 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000113 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000114 return -1;
115 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000117 if (vie_encoder->DeRegisterExternalEncoder(pl_type) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000118 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000119 return -1;
120 }
121 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122}
123
124int ViEExternalCodecImpl::RegisterExternalReceiveCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000125 const int video_channel,
126 const unsigned int pl_type,
127 VideoDecoder* decoder,
128 bool decoder_render,
129 int render_delay) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000130 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000131 "%s channel %d pl_type %d decoder 0x%x, decoder_render %d, "
132 "renderDelay %d", __FUNCTION__, video_channel, pl_type, decoder,
133 decoder_render, render_delay);
niklase@google.com470e71d2011-07-07 08:21:25 +0000134
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000135 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000136 ViEChannel* vie_channel = cs.Channel(video_channel);
137 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000138 WEBRTC_TRACE(kTraceError, kTraceVideo,
139 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000140 "%s: Invalid argument video_channel %u. Does it exist?",
141 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000142 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000143 return -1;
144 }
145 if (!decoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000146 WEBRTC_TRACE(kTraceError, kTraceVideo,
147 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000148 "%s: Invalid argument decoder 0x%x.", __FUNCTION__, decoder);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000149 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000150 return -1;
151 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000153 if (vie_channel->RegisterExternalDecoder(pl_type, decoder, decoder_render,
154 render_delay) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000155 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000156 return -1;
157 }
158 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000159}
160
161int ViEExternalCodecImpl::DeRegisterExternalReceiveCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000162const int video_channel, const unsigned char pl_type) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000163 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000164 "%s channel %d pl_type %u", __FUNCTION__, video_channel,
165 pl_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000167 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000168 ViEChannel* vie_channel = cs.Channel(video_channel);
169 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000170 WEBRTC_TRACE(kTraceError, kTraceVideo,
171 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000172 "%s: Invalid argument video_channel %u. Does it exist?",
173 __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000174 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000175 return -1;
176 }
177 if (vie_channel->DeRegisterExternalDecoder(pl_type) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000178 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000179 return -1;
180 }
181 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000182}
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000183
184} // namespace webrtc