andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "video_engine/vie_external_codec_impl.h" |
| 12 | |
| 13 | #include "engine_configurations.h" // NOLINT |
| 14 | #include "system_wrappers/interface/trace.h" |
| 15 | #include "video_engine/include/vie_errors.h" |
| 16 | #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" |
| 20 | #include "video_engine/vie_shared_data.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | ViEExternalCodec* ViEExternalCodec::GetInterface(VideoEngine* video_engine) { |
| 25 | #ifdef WEBRTC_VIDEO_ENGINE_EXTERNAL_CODEC_API |
| 26 | if (video_engine == NULL) { |
| 27 | return NULL; |
| 28 | } |
| 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; |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | int ViEExternalCodecImpl::Release() { |
| 40 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_->instance_id(), |
| 41 | "ViEExternalCodec::Release()"); |
| 42 | // Decrease ref count. |
| 43 | (*this)--; |
| 44 | |
pbos@webrtc.org | 2a5d229 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 45 | int32_t ref_count = GetCount(); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 46 | if (ref_count < 0) { |
| 47 | WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_->instance_id(), |
| 48 | "ViEExternalCodec release too many times"); |
| 49 | shared_data_->SetLastError(kViEAPIDoesNotExist); |
| 50 | return -1; |
| 51 | } |
| 52 | WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_->instance_id(), |
| 53 | "ViEExternalCodec reference count: %d", ref_count); |
| 54 | return ref_count; |
| 55 | } |
| 56 | |
| 57 | ViEExternalCodecImpl::ViEExternalCodecImpl(ViESharedData* shared_data) |
| 58 | : shared_data_(shared_data) { |
| 59 | WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(), |
| 60 | "ViEExternalCodecImpl::ViEExternalCodecImpl() Ctor"); |
| 61 | } |
| 62 | |
| 63 | ViEExternalCodecImpl::~ViEExternalCodecImpl() { |
| 64 | WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(), |
| 65 | "ViEExternalCodecImpl::~ViEExternalCodecImpl() Dtor"); |
| 66 | } |
| 67 | |
| 68 | int ViEExternalCodecImpl::RegisterExternalSendCodec(const int video_channel, |
| 69 | const unsigned char pl_type, |
stefan@webrtc.org | 8be968f | 2013-01-09 08:35:40 +0000 | [diff] [blame] | 70 | VideoEncoder* encoder, |
| 71 | bool internal_source) { |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 72 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 73 | "%s channel %d pl_type %d encoder 0x%x", __FUNCTION__, |
| 74 | video_channel, pl_type, encoder); |
| 75 | |
| 76 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 77 | ViEEncoder* vie_encoder = cs.Encoder(video_channel); |
| 78 | if (!vie_encoder) { |
| 79 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 80 | ViEId(shared_data_->instance_id(), video_channel), |
| 81 | "%s: Invalid argument video_channel %u. Does it exist?", |
| 82 | __FUNCTION__, video_channel); |
| 83 | shared_data_->SetLastError(kViECodecInvalidArgument); |
| 84 | return -1; |
| 85 | } |
| 86 | if (!encoder) { |
| 87 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 88 | ViEId(shared_data_->instance_id(), video_channel), |
| 89 | "%s: Invalid argument Encoder 0x%x.", __FUNCTION__, encoder); |
| 90 | shared_data_->SetLastError(kViECodecInvalidArgument); |
| 91 | return -1; |
| 92 | } |
| 93 | |
stefan@webrtc.org | 8be968f | 2013-01-09 08:35:40 +0000 | [diff] [blame] | 94 | if (vie_encoder->RegisterExternalEncoder(encoder, pl_type, internal_source) |
| 95 | != 0) { |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 96 | shared_data_->SetLastError(kViECodecUnknownError); |
| 97 | return -1; |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int ViEExternalCodecImpl::DeRegisterExternalSendCodec( |
| 103 | const int video_channel, const unsigned char pl_type) { |
| 104 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 105 | "%s channel %d pl_type %d", __FUNCTION__, video_channel, |
| 106 | pl_type); |
| 107 | |
| 108 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 109 | ViEEncoder* vie_encoder = cs.Encoder(video_channel); |
| 110 | if (!vie_encoder) { |
| 111 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 112 | ViEId(shared_data_->instance_id(), video_channel), |
| 113 | "%s: Invalid argument video_channel %u. Does it exist?", |
| 114 | __FUNCTION__, video_channel); |
| 115 | shared_data_->SetLastError(kViECodecInvalidArgument); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | if (vie_encoder->DeRegisterExternalEncoder(pl_type) != 0) { |
| 120 | shared_data_->SetLastError(kViECodecUnknownError); |
| 121 | return -1; |
| 122 | } |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | int ViEExternalCodecImpl::RegisterExternalReceiveCodec( |
| 127 | const int video_channel, |
| 128 | const unsigned int pl_type, |
| 129 | VideoDecoder* decoder, |
| 130 | bool decoder_render, |
| 131 | int render_delay) { |
| 132 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 133 | "%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); |
| 136 | |
| 137 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 138 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 139 | if (!vie_channel) { |
| 140 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 141 | ViEId(shared_data_->instance_id(), video_channel), |
| 142 | "%s: Invalid argument video_channel %u. Does it exist?", |
| 143 | __FUNCTION__, video_channel); |
| 144 | shared_data_->SetLastError(kViECodecInvalidArgument); |
| 145 | return -1; |
| 146 | } |
| 147 | if (!decoder) { |
| 148 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 149 | ViEId(shared_data_->instance_id(), video_channel), |
| 150 | "%s: Invalid argument decoder 0x%x.", __FUNCTION__, decoder); |
| 151 | shared_data_->SetLastError(kViECodecInvalidArgument); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | if (vie_channel->RegisterExternalDecoder(pl_type, decoder, decoder_render, |
| 156 | render_delay) != 0) { |
| 157 | shared_data_->SetLastError(kViECodecUnknownError); |
| 158 | return -1; |
| 159 | } |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | int ViEExternalCodecImpl::DeRegisterExternalReceiveCodec( |
| 164 | const int video_channel, const unsigned char pl_type) { |
| 165 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 166 | "%s channel %d pl_type %u", __FUNCTION__, video_channel, |
| 167 | pl_type); |
| 168 | |
| 169 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 170 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 171 | if (!vie_channel) { |
| 172 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 173 | ViEId(shared_data_->instance_id(), video_channel), |
| 174 | "%s: Invalid argument video_channel %u. Does it exist?", |
| 175 | __FUNCTION__, video_channel); |
| 176 | shared_data_->SetLastError(kViECodecInvalidArgument); |
| 177 | return -1; |
| 178 | } |
| 179 | if (vie_channel->DeRegisterExternalDecoder(pl_type) != 0) { |
| 180 | shared_data_->SetLastError(kViECodecUnknownError); |
| 181 | return -1; |
| 182 | } |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | } // namespace webrtc |