blob: 0c29fd4c1f6b0f98cdc16c5d2c108485ba542dfe [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org79af7342012-01-31 12:22:14 +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.org956aa7e2013-05-21 13:52:32 +000011#include "webrtc/voice_engine/voe_external_media_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000013#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
14#include "webrtc/system_wrappers/interface/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"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21namespace webrtc {
22
Jelena Marusic0d266052015-05-04 14:15:32 +020023VoEExternalMedia* VoEExternalMedia::GetInterface(VoiceEngine* voiceEngine) {
niklase@google.com470e71d2011-07-07 08:21:25 +000024#ifndef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
Jelena Marusic0d266052015-05-04 14:15:32 +020025 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000026#else
Jelena Marusic0d266052015-05-04 14:15:32 +020027 if (NULL == voiceEngine) {
28 return NULL;
29 }
30 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
31 s->AddRef();
32 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000033#endif
34}
35
36#ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
37
tommi@webrtc.org851becd2012-04-04 14:57:19 +000038VoEExternalMediaImpl::VoEExternalMediaImpl(voe::SharedData* shared)
wjia@webrtc.org3f9db372013-01-12 01:09:03 +000039 :
40#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
Jelena Marusic0d266052015-05-04 14:15:32 +020041 playout_delay_ms_(0),
wjia@webrtc.org3f9db372013-01-12 01:09:03 +000042#endif
Jelena Marusic0d266052015-05-04 14:15:32 +020043 shared_(shared) {
44 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
45 "VoEExternalMediaImpl() - ctor");
niklase@google.com470e71d2011-07-07 08:21:25 +000046}
47
Jelena Marusic0d266052015-05-04 14:15:32 +020048VoEExternalMediaImpl::~VoEExternalMediaImpl() {
49 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
50 "~VoEExternalMediaImpl() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +000051}
52
niklase@google.com470e71d2011-07-07 08:21:25 +000053int VoEExternalMediaImpl::RegisterExternalMediaProcessing(
54 int channel,
55 ProcessingTypes type,
Jelena Marusic0d266052015-05-04 14:15:32 +020056 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);
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +000063 return -1;
Jelena Marusic0d266052015-05-04 14:15:32 +020064 }
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;
niklase@google.com470e71d2011-07-07 08:21:25 +000090}
91
92int VoEExternalMediaImpl::DeRegisterExternalMediaProcessing(
93 int channel,
Jelena Marusic0d266052015-05-04 14:15:32 +020094 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);
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +000099 return -1;
Jelena Marusic0d266052015-05-04 14:15:32 +0200100 }
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;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123}
124
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000125int VoEExternalMediaImpl::GetAudioFrame(int channel, int desired_sample_rate_hz,
126 AudioFrame* frame) {
Jelena Marusic0d266052015-05-04 14:15:32 +0200127 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
128 VoEId(shared_->instance_id(), channel),
129 "GetAudioFrame(channel=%d, desired_sample_rate_hz=%d)", channel,
130 desired_sample_rate_hz);
131 if (!shared_->statistics().Initialized()) {
132 shared_->SetLastError(VE_NOT_INITED, kTraceError);
133 return -1;
134 }
135 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
136 voe::Channel* channelPtr = ch.channel();
137 if (channelPtr == NULL) {
138 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
139 "GetAudioFrame() failed to locate channel");
140 return -1;
141 }
142 if (!channelPtr->ExternalMixing()) {
143 shared_->SetLastError(VE_INVALID_OPERATION, kTraceError,
144 "GetAudioFrame() was called on channel that is not"
145 " externally mixed.");
146 return -1;
147 }
148 if (!channelPtr->Playing()) {
149 shared_->SetLastError(
150 VE_INVALID_OPERATION, kTraceError,
151 "GetAudioFrame() was called on channel that is not playing.");
152 return -1;
153 }
154 if (desired_sample_rate_hz == -1) {
155 shared_->SetLastError(VE_BAD_ARGUMENT, kTraceError,
156 "GetAudioFrame() was called with bad sample rate.");
157 return -1;
158 }
159 frame->sample_rate_hz_ =
160 desired_sample_rate_hz == 0 ? -1 : desired_sample_rate_hz;
161 return channelPtr->GetAudioFrame(channel, *frame);
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000162}
163
164int VoEExternalMediaImpl::SetExternalMixing(int channel, bool enable) {
Jelena Marusic0d266052015-05-04 14:15:32 +0200165 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
166 VoEId(shared_->instance_id(), channel),
167 "SetExternalMixing(channel=%d, enable=%d)", channel, enable);
168 if (!shared_->statistics().Initialized()) {
169 shared_->SetLastError(VE_NOT_INITED, kTraceError);
170 return -1;
171 }
172 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
173 voe::Channel* channelPtr = ch.channel();
174 if (channelPtr == NULL) {
175 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
176 "SetExternalMixing() failed to locate channel");
177 return -1;
178 }
179 return channelPtr->SetExternalMixing(enable);
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000180}
181
niklase@google.com470e71d2011-07-07 08:21:25 +0000182#endif // WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
183
184} // namespace webrtc