blob: 93e7ed808fac379162f05ed6558d6928e16e9387 [file] [log] [blame]
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +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
11#include "video_engine/vie_encryption_impl.h"
12
13#include "system_wrappers/interface/trace.h"
14#include "video_engine/include/vie_errors.h"
15#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"
19#include "video_engine/vie_shared_data.h"
20
21namespace webrtc {
22
23ViEEncryption* ViEEncryption::GetInterface(VideoEngine* video_engine) {
24#ifdef WEBRTC_VIDEO_ENGINE_ENCRYPTION_API
25 if (video_engine == NULL) {
26 return NULL;
27 }
andrew@webrtc.orgd3d364e2013-05-09 02:12:07 +000028 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000029 ViEEncryptionImpl* vie_encryption_impl = vie_impl;
30 // Increase ref count.
31 (*vie_encryption_impl)++;
32 return vie_encryption_impl;
33#else
34 return NULL;
35#endif
36}
37
38int ViEEncryptionImpl::Release() {
39 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_->instance_id(),
40 "ViEEncryptionImpl::Release()");
41 // Decrease ref count.
42 (*this)--;
43
pbos@webrtc.org2a5d2292013-04-09 13:41:51 +000044 int32_t ref_count = GetCount();
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000045 if (ref_count < 0) {
46 WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_->instance_id(),
47 "ViEEncryptionImpl release too many times");
48 shared_data_->SetLastError(kViEAPIDoesNotExist);
49 return -1;
50 }
51 WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_->instance_id(),
52 "ViEEncryptionImpl reference count: %d", ref_count);
53 return ref_count;
54}
55
56ViEEncryptionImpl::ViEEncryptionImpl(ViESharedData* shared_data)
57 : shared_data_(shared_data) {
58 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
59 "ViEEncryptionImpl::ViEEncryptionImpl() Ctor");
60}
61
62ViEEncryptionImpl::~ViEEncryptionImpl() {
63 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
64 "ViEEncryptionImpl::~ViEEncryptionImpl() Dtor");
65}
66
67int ViEEncryptionImpl::RegisterExternalEncryption(const int video_channel,
68 Encryption& encryption) {
69 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
70 ViEId(shared_data_->instance_id(), video_channel),
71 "RegisterExternalEncryption(video_channel=%d)", video_channel);
72
73 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
74 ViEChannel* vie_channel = cs.Channel(video_channel);
75 if (vie_channel == NULL) {
76 WEBRTC_TRACE(kTraceError, kTraceVideo,
77 ViEId(shared_data_->instance_id(), video_channel),
78 "%s: No channel %d", __FUNCTION__, video_channel);
79 shared_data_->SetLastError(kViEEncryptionInvalidChannelId);
80 return -1;
81 }
82 if (vie_channel->RegisterExternalEncryption(&encryption) != 0) {
83 shared_data_->SetLastError(kViEEncryptionUnknownError);
84 return -1;
85 }
86 return 0;
87}
88
89int ViEEncryptionImpl::DeregisterExternalEncryption(const int video_channel) {
90 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
91 ViEId(shared_data_->instance_id(), video_channel),
92 "RegisterExternalEncryption(video_channel=%d)", video_channel);
93
94 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
95 ViEChannel* vie_channel = cs.Channel(video_channel);
96 if (vie_channel == NULL) {
97 WEBRTC_TRACE(kTraceError, kTraceVideo,
98 ViEId(shared_data_->instance_id(), video_channel),
99 "%s: No channel %d", __FUNCTION__, video_channel);
100 shared_data_->SetLastError(kViEEncryptionInvalidChannelId);
101 return -1;
102 }
103
104 if (vie_channel->DeRegisterExternalEncryption() != 0) {
105 shared_data_->SetLastError(kViEEncryptionUnknownError);
106 return -1;
107 }
108 return 0;
109}
110
111} // namespace webrtc