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 "webrtc/voice_engine/voe_external_media_impl.h"
12
13 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
14 #include "webrtc/system_wrappers/include/trace.h"
15 #include "webrtc/voice_engine/channel.h"
16 #include "webrtc/voice_engine/include/voe_errors.h"
17 #include "webrtc/voice_engine/output_mixer.h"
18 #include "webrtc/voice_engine/transmit_mixer.h"
19 #include "webrtc/voice_engine/voice_engine_impl.h"
20
21 namespace webrtc {
22
GetInterface(VoiceEngine * voiceEngine)23 VoEExternalMedia* VoEExternalMedia::GetInterface(VoiceEngine* voiceEngine) {
24 #ifndef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
25 return NULL;
26 #else
27 if (NULL == voiceEngine) {
28 return NULL;
29 }
30 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
31 s->AddRef();
32 return s;
33 #endif
34 }
35
36 #ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
37
VoEExternalMediaImpl(voe::SharedData * shared)38 VoEExternalMediaImpl::VoEExternalMediaImpl(voe::SharedData* shared)
39 :
40 #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
41 playout_delay_ms_(0),
42 #endif
43 shared_(shared) {
44 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
45 "VoEExternalMediaImpl() - ctor");
46 }
47
~VoEExternalMediaImpl()48 VoEExternalMediaImpl::~VoEExternalMediaImpl() {
49 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
50 "~VoEExternalMediaImpl() - dtor");
51 }
52
RegisterExternalMediaProcessing(int channel,ProcessingTypes type,VoEMediaProcess & processObject)53 int VoEExternalMediaImpl::RegisterExternalMediaProcessing(
54 int channel,
55 ProcessingTypes type,
56 VoEMediaProcess& processObject) {
57 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1),
58 "RegisterExternalMediaProcessing(channel=%d, type=%d, "
59 "processObject=0x%x)",
60 channel, type, &processObject);
61 if (!shared_->statistics().Initialized()) {
62 shared_->SetLastError(VE_NOT_INITED, kTraceError);
63 return -1;
64 }
65 switch (type) {
66 case kPlaybackPerChannel:
67 case kRecordingPerChannel: {
68 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
69 voe::Channel* channelPtr = ch.channel();
70 if (channelPtr == NULL) {
71 shared_->SetLastError(
72 VE_CHANNEL_NOT_VALID, kTraceError,
73 "RegisterExternalMediaProcessing() failed to locate "
74 "channel");
75 return -1;
76 }
77 return channelPtr->RegisterExternalMediaProcessing(type, processObject);
78 }
79 case kPlaybackAllChannelsMixed: {
80 return shared_->output_mixer()->RegisterExternalMediaProcessing(
81 processObject);
82 }
83 case kRecordingAllChannelsMixed:
84 case kRecordingPreprocessing: {
85 return shared_->transmit_mixer()->RegisterExternalMediaProcessing(
86 &processObject, type);
87 }
88 }
89 return -1;
90 }
91
DeRegisterExternalMediaProcessing(int channel,ProcessingTypes type)92 int VoEExternalMediaImpl::DeRegisterExternalMediaProcessing(
93 int channel,
94 ProcessingTypes type) {
95 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1),
96 "DeRegisterExternalMediaProcessing(channel=%d)", channel);
97 if (!shared_->statistics().Initialized()) {
98 shared_->SetLastError(VE_NOT_INITED, kTraceError);
99 return -1;
100 }
101 switch (type) {
102 case kPlaybackPerChannel:
103 case kRecordingPerChannel: {
104 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
105 voe::Channel* channelPtr = ch.channel();
106 if (channelPtr == NULL) {
107 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
108 "RegisterExternalMediaProcessing() "
109 "failed to locate channel");
110 return -1;
111 }
112 return channelPtr->DeRegisterExternalMediaProcessing(type);
113 }
114 case kPlaybackAllChannelsMixed: {
115 return shared_->output_mixer()->DeRegisterExternalMediaProcessing();
116 }
117 case kRecordingAllChannelsMixed:
118 case kRecordingPreprocessing: {
119 return shared_->transmit_mixer()->DeRegisterExternalMediaProcessing(type);
120 }
121 }
122 return -1;
123 }
124
GetAudioFrame(int channel,int desired_sample_rate_hz,AudioFrame * frame)125 int VoEExternalMediaImpl::GetAudioFrame(int channel, int desired_sample_rate_hz,
126 AudioFrame* frame) {
127 if (!shared_->statistics().Initialized()) {
128 shared_->SetLastError(VE_NOT_INITED, kTraceError);
129 return -1;
130 }
131 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
132 voe::Channel* channelPtr = ch.channel();
133 if (channelPtr == NULL) {
134 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
135 "GetAudioFrame() failed to locate channel");
136 return -1;
137 }
138 if (!channelPtr->ExternalMixing()) {
139 shared_->SetLastError(VE_INVALID_OPERATION, kTraceError,
140 "GetAudioFrame() was called on channel that is not"
141 " externally mixed.");
142 return -1;
143 }
144 if (!channelPtr->Playing()) {
145 shared_->SetLastError(
146 VE_INVALID_OPERATION, kTraceError,
147 "GetAudioFrame() was called on channel that is not playing.");
148 return -1;
149 }
150 if (desired_sample_rate_hz == -1) {
151 shared_->SetLastError(VE_BAD_ARGUMENT, kTraceError,
152 "GetAudioFrame() was called with bad sample rate.");
153 return -1;
154 }
155 frame->sample_rate_hz_ =
156 desired_sample_rate_hz == 0 ? -1 : desired_sample_rate_hz;
157 return channelPtr->GetAudioFrame(channel, frame);
158 }
159
SetExternalMixing(int channel,bool enable)160 int VoEExternalMediaImpl::SetExternalMixing(int channel, bool enable) {
161 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
162 VoEId(shared_->instance_id(), channel),
163 "SetExternalMixing(channel=%d, enable=%d)", channel, enable);
164 if (!shared_->statistics().Initialized()) {
165 shared_->SetLastError(VE_NOT_INITED, kTraceError);
166 return -1;
167 }
168 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
169 voe::Channel* channelPtr = ch.channel();
170 if (channelPtr == NULL) {
171 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
172 "SetExternalMixing() failed to locate channel");
173 return -1;
174 }
175 return channelPtr->SetExternalMixing(enable);
176 }
177
178 #endif // WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
179
180 } // namespace webrtc
181