blob: 8e14aa09562a96952049bb37f8b768b92825e360 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 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
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000011#include "video_engine/vie_encryption_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000013#include "system_wrappers/interface/trace.h"
mflodman@webrtc.orga4863db2011-12-22 08:51:52 +000014#include "video_engine/include/vie_errors.h"
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000015#include "video_engine/vie_channel.h"
16#include "video_engine/vie_channel_manager.h"
17#include "video_engine/vie_defines.h"
18#include "video_engine/vie_impl.h"
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000019#include "video_engine/vie_shared_data.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000021namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000023ViEEncryption* ViEEncryption::GetInterface(VideoEngine* video_engine) {
niklase@google.com470e71d2011-07-07 08:21:25 +000024#ifdef WEBRTC_VIDEO_ENGINE_ENCRYPTION_API
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000025 if (video_engine == NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000026 return NULL;
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000027 }
28 VideoEngineImpl* vie_impl = reinterpret_cast<VideoEngineImpl*>(video_engine);
29 ViEEncryptionImpl* vie_encryption_impl = vie_impl;
30 // Increase ref count.
31 (*vie_encryption_impl)++;
32 return vie_encryption_impl;
33#else
34 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000035#endif
36}
37
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000038int ViEEncryptionImpl::Release() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000039 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_->instance_id(),
niklase@google.com470e71d2011-07-07 08:21:25 +000040 "ViEEncryptionImpl::Release()");
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000041 // Decrease ref count.
42 (*this)--;
niklase@google.com470e71d2011-07-07 08:21:25 +000043
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000044 int32_t ref_count = GetCount();
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000045 if (ref_count < 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000046 WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000047 "ViEEncryptionImpl release too many times");
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000048 shared_data_->SetLastError(kViEAPIDoesNotExist);
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000049 return -1;
50 }
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000051 WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000052 "ViEEncryptionImpl reference count: %d", ref_count);
53 return ref_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000054}
55
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000056ViEEncryptionImpl::ViEEncryptionImpl(ViESharedData* shared_data)
57 : shared_data_(shared_data) {
58 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
niklase@google.com470e71d2011-07-07 08:21:25 +000059 "ViEEncryptionImpl::ViEEncryptionImpl() Ctor");
60}
61
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000062ViEEncryptionImpl::~ViEEncryptionImpl() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000063 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
niklase@google.com470e71d2011-07-07 08:21:25 +000064 "ViEEncryptionImpl::~ViEEncryptionImpl() Dtor");
65}
66
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000067int ViEEncryptionImpl::RegisterExternalEncryption(const int video_channel,
68 Encryption& encryption) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000069 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
70 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000071 "RegisterExternalEncryption(video_channel=%d)", video_channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000072
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000073 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000074 ViEChannel* vie_channel = cs.Channel(video_channel);
75 if (vie_channel == NULL) {
76 WEBRTC_TRACE(kTraceError, kTraceVideo,
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000077 ViEId(shared_data_->instance_id(), video_channel),
78 "%s: No channel %d", __FUNCTION__, video_channel);
79 shared_data_->SetLastError(kViEEncryptionInvalidChannelId);
niklase@google.com470e71d2011-07-07 08:21:25 +000080 return -1;
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000081 }
82 if (vie_channel->RegisterExternalEncryption(&encryption) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000083 shared_data_->SetLastError(kViEEncryptionUnknownError);
niklase@google.com470e71d2011-07-07 08:21:25 +000084 return -1;
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000085 }
86 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000087}
88
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000089int ViEEncryptionImpl::DeregisterExternalEncryption(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000090 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
91 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000092 "RegisterExternalEncryption(video_channel=%d)", video_channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000093
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000094 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000095 ViEChannel* vie_channel = cs.Channel(video_channel);
96 if (vie_channel == NULL) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000097 WEBRTC_TRACE(kTraceError, kTraceVideo,
98 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +000099 "%s: No channel %d", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000100 shared_data_->SetLastError(kViEEncryptionInvalidChannelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 return -1;
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +0000102 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +0000104 if (vie_channel->DeRegisterExternalEncryption() != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000105 shared_data_->SetLastError(kViEEncryptionUnknownError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000106 return -1;
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +0000107 }
108 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000109}
110
mflodman@webrtc.orgc12686c2011-12-21 09:29:28 +0000111} // namespace webrtc