blob: 7c52692b682283de2f256db530eaaa9a1716e84e [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
23VoEExternalMedia* VoEExternalMedia::GetInterface(VoiceEngine* voiceEngine)
24{
25#ifndef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
26 return NULL;
27#else
28 if (NULL == voiceEngine)
29 {
30 return NULL;
31 }
tommi@webrtc.org0989fb72013-02-15 15:07:32 +000032 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
tommi@webrtc.orga990e122012-04-26 15:28:22 +000033 s->AddRef();
34 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000035#endif
36}
37
38#ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
39
tommi@webrtc.org851becd2012-04-04 14:57:19 +000040VoEExternalMediaImpl::VoEExternalMediaImpl(voe::SharedData* shared)
wjia@webrtc.org3f9db372013-01-12 01:09:03 +000041 :
42#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
43 playout_delay_ms_(0),
44#endif
45 shared_(shared)
niklase@google.com470e71d2011-07-07 08:21:25 +000046{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000047 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000048 "VoEExternalMediaImpl() - ctor");
49}
50
51VoEExternalMediaImpl::~VoEExternalMediaImpl()
52{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000053 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000054 "~VoEExternalMediaImpl() - dtor");
55}
56
niklase@google.com470e71d2011-07-07 08:21:25 +000057int VoEExternalMediaImpl::RegisterExternalMediaProcessing(
58 int channel,
59 ProcessingTypes type,
60 VoEMediaProcess& processObject)
61{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000062 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000063 "RegisterExternalMediaProcessing(channel=%d, type=%d, "
64 "processObject=0x%x)", channel, type, &processObject);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000065 if (!shared_->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000066 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000067 shared_->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000068 return -1;
69 }
70 switch (type)
71 {
72 case kPlaybackPerChannel:
73 case kRecordingPerChannel:
74 {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000075 voe::ChannelOwner ch =
76 shared_->channel_manager().GetChannel(channel);
77 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +000078 if (channelPtr == NULL)
79 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000080 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
81 "RegisterExternalMediaProcessing() failed to locate "
82 "channel");
niklase@google.com470e71d2011-07-07 08:21:25 +000083 return -1;
84 }
85 return channelPtr->RegisterExternalMediaProcessing(type,
86 processObject);
87 }
88 case kPlaybackAllChannelsMixed:
89 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000090 return shared_->output_mixer()->RegisterExternalMediaProcessing(
niklase@google.com470e71d2011-07-07 08:21:25 +000091 processObject);
92 }
93 case kRecordingAllChannelsMixed:
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +000094 case kRecordingPreprocessing:
niklase@google.com470e71d2011-07-07 08:21:25 +000095 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000096 return shared_->transmit_mixer()->RegisterExternalMediaProcessing(
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +000097 &processObject, type);
niklase@google.com470e71d2011-07-07 08:21:25 +000098 }
niklase@google.com470e71d2011-07-07 08:21:25 +000099 }
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +0000100 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101}
102
103int VoEExternalMediaImpl::DeRegisterExternalMediaProcessing(
104 int channel,
105 ProcessingTypes type)
106{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000107 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000108 "DeRegisterExternalMediaProcessing(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000109 if (!shared_->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000111 shared_->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 return -1;
113 }
114 switch (type)
115 {
116 case kPlaybackPerChannel:
117 case kRecordingPerChannel:
118 {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000119 voe::ChannelOwner ch =
120 shared_->channel_manager().GetChannel(channel);
121 voe::Channel* channelPtr = ch.channel();
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 if (channelPtr == NULL)
123 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000124 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000125 "RegisterExternalMediaProcessing() "
126 "failed to locate channel");
127 return -1;
128 }
129 return channelPtr->DeRegisterExternalMediaProcessing(type);
130 }
131 case kPlaybackAllChannelsMixed:
132 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000133 return shared_->output_mixer()->
134 DeRegisterExternalMediaProcessing();
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 }
136 case kRecordingAllChannelsMixed:
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +0000137 case kRecordingPreprocessing:
niklase@google.com470e71d2011-07-07 08:21:25 +0000138 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000139 return shared_->transmit_mixer()->
andrew@webrtc.org21ab3ba2012-10-19 17:30:56 +0000140 DeRegisterExternalMediaProcessing(type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000142 }
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +0000143 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144}
145
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000146int VoEExternalMediaImpl::GetAudioFrame(int channel, int desired_sample_rate_hz,
147 AudioFrame* frame) {
148 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
149 VoEId(shared_->instance_id(), channel),
150 "GetAudioFrame(channel=%d, desired_sample_rate_hz=%d)",
151 channel, desired_sample_rate_hz);
152 if (!shared_->statistics().Initialized())
153 {
154 shared_->SetLastError(VE_NOT_INITED, kTraceError);
155 return -1;
156 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000157 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
158 voe::Channel* channelPtr = ch.channel();
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000159 if (channelPtr == NULL)
160 {
161 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
162 "GetAudioFrame() failed to locate channel");
163 return -1;
164 }
165 if (!channelPtr->ExternalMixing()) {
166 shared_->SetLastError(VE_INVALID_OPERATION, kTraceError,
167 "GetAudioFrame() was called on channel that is not"
168 " externally mixed.");
169 return -1;
170 }
171 if (!channelPtr->Playing()) {
172 shared_->SetLastError(VE_INVALID_OPERATION, kTraceError,
173 "GetAudioFrame() was called on channel that is not playing.");
174 return -1;
175 }
176 if (desired_sample_rate_hz == -1) {
177 shared_->SetLastError(VE_BAD_ARGUMENT, kTraceError,
178 "GetAudioFrame() was called with bad sample rate.");
179 return -1;
180 }
181 frame->sample_rate_hz_ = desired_sample_rate_hz == 0 ? -1 :
182 desired_sample_rate_hz;
183 return channelPtr->GetAudioFrame(channel, *frame);
184}
185
186int VoEExternalMediaImpl::SetExternalMixing(int channel, bool enable) {
187 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
188 VoEId(shared_->instance_id(), channel),
189 "SetExternalMixing(channel=%d, enable=%d)", channel, enable);
190 if (!shared_->statistics().Initialized())
191 {
192 shared_->SetLastError(VE_NOT_INITED, kTraceError);
193 return -1;
194 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000195 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
196 voe::Channel* channelPtr = ch.channel();
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000197 if (channelPtr == NULL)
198 {
199 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
200 "SetExternalMixing() failed to locate channel");
201 return -1;
202 }
203 return channelPtr->SetExternalMixing(enable);
204}
205
niklase@google.com470e71d2011-07-07 08:21:25 +0000206#endif // WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
207
208} // namespace webrtc