blob: 7eb015c4b6bfa2c5db416f68c37d2029209ff749 [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
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000011#include "webrtc/video_engine/vie_external_codec_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000013#include "webrtc/engine_configurations.h"
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000014#include "webrtc/system_wrappers/interface/logging.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000015#include "webrtc/video_engine/include/vie_errors.h"
16#include "webrtc/video_engine/vie_channel.h"
17#include "webrtc/video_engine/vie_channel_manager.h"
18#include "webrtc/video_engine/vie_encoder.h"
19#include "webrtc/video_engine/vie_impl.h"
20#include "webrtc/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 }
andrew@webrtc.orgd72262d2013-05-09 02:12:07 +000029 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000030 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.org813b4ef2011-12-20 10:39:30 +000040 // Decrease ref count.
41 (*this)--;
niklase@google.com470e71d2011-07-07 08:21:25 +000042
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000043 int32_t ref_count = GetCount();
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000044 if (ref_count < 0) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000045 LOG(LS_WARNING) << "ViEExternalCodec released too many times.";
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000046 shared_data_->SetLastError(kViEAPIDoesNotExist);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000047 return -1;
48 }
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000049 return ref_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000052ViEExternalCodecImpl::ViEExternalCodecImpl(ViESharedData* shared_data)
53 : shared_data_(shared_data) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000054}
55
56ViEExternalCodecImpl::~ViEExternalCodecImpl() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000057}
58
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000059int ViEExternalCodecImpl::RegisterExternalSendCodec(const int video_channel,
60 const unsigned char pl_type,
stefan@webrtc.orgfcd85852013-01-09 08:35:40 +000061 VideoEncoder* encoder,
62 bool internal_source) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000063 assert(encoder != NULL);
64 LOG(LS_INFO) << "Register external encoder for channel " << video_channel
65 << ", pl_type " << static_cast<int>(pl_type)
66 << ", internal_source " << internal_source;
niklase@google.com470e71d2011-07-07 08:21:25 +000067
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000068 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000069 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
70 if (!vie_encoder) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000071 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000072 return -1;
73 }
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000074 if (vie_encoder->RegisterExternalEncoder(encoder, pl_type,
75 internal_source) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000076 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000077 return -1;
78 }
79 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000080}
81
82int ViEExternalCodecImpl::DeRegisterExternalSendCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000083 const int video_channel, const unsigned char pl_type) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +000084 LOG(LS_INFO) << "Deregister external encoder for channel " << video_channel;
niklase@google.com470e71d2011-07-07 08:21:25 +000085
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000086 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +000087 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
88 if (!vie_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->DeRegisterExternalEncoder(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::RegisterExternalReceiveCodec(
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000101 const int video_channel,
102 const unsigned int pl_type,
103 VideoDecoder* decoder,
104 bool decoder_render,
105 int render_delay) {
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000106 LOG(LS_INFO) << "Register exrernal decoder for channel " << video_channel
107 << ", pl_type " << pl_type
108 << ", decoder_render " << decoder_render
109 << ", render_delay " << render_delay;
110 assert(decoder != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000112 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000113 ViEChannel* vie_channel = cs.Channel(video_channel);
114 if (!vie_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_channel->RegisterExternalDecoder(pl_type, decoder, decoder_render,
120 render_delay) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000121 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000122 return -1;
123 }
124 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
127int ViEExternalCodecImpl::DeRegisterExternalReceiveCodec(
mflodman@webrtc.org5574dac2014-04-07 10:56:31 +0000128 const int video_channel, const unsigned char pl_type) {
129 LOG(LS_INFO) << "DeRegisterExternalReceiveCodec for channel " << video_channel
130 << ", pl_type " << pl_type;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000132 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000133 ViEChannel* vie_channel = cs.Channel(video_channel);
134 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000135 shared_data_->SetLastError(kViECodecInvalidArgument);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000136 return -1;
137 }
138 if (vie_channel->DeRegisterExternalDecoder(pl_type) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000139 shared_data_->SetLastError(kViECodecUnknownError);
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000140 return -1;
141 }
142 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000143}
mflodman@webrtc.org813b4ef2011-12-20 10:39:30 +0000144
145} // namespace webrtc