blob: cae0715ced2da3af353f8784b2f4c11814da1a15 [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
Henrik Kjellander98f53512015-10-28 18:17:40 +010013#include "webrtc/system_wrappers/include/trace.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000014#include "webrtc/voice_engine/channel.h"
15#include "webrtc/voice_engine/include/voe_errors.h"
16#include "webrtc/voice_engine/output_mixer.h"
17#include "webrtc/voice_engine/transmit_mixer.h"
18#include "webrtc/voice_engine/voice_engine_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20namespace webrtc {
21
Jelena Marusic0d266052015-05-04 14:15:32 +020022VoEExternalMedia* VoEExternalMedia::GetInterface(VoiceEngine* voiceEngine) {
niklase@google.com470e71d2011-07-07 08:21:25 +000023#ifndef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
Jelena Marusic0d266052015-05-04 14:15:32 +020024 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000025#else
Jelena Marusic0d266052015-05-04 14:15:32 +020026 if (NULL == voiceEngine) {
27 return NULL;
28 }
29 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
30 s->AddRef();
31 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000032#endif
33}
34
35#ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
36
tommi@webrtc.org851becd2012-04-04 14:57:19 +000037VoEExternalMediaImpl::VoEExternalMediaImpl(voe::SharedData* shared)
wjia@webrtc.org3f9db372013-01-12 01:09:03 +000038 :
39#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
Jelena Marusic0d266052015-05-04 14:15:32 +020040 playout_delay_ms_(0),
wjia@webrtc.org3f9db372013-01-12 01:09:03 +000041#endif
Jelena Marusic0d266052015-05-04 14:15:32 +020042 shared_(shared) {
43 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
44 "VoEExternalMediaImpl() - ctor");
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
Jelena Marusic0d266052015-05-04 14:15:32 +020047VoEExternalMediaImpl::~VoEExternalMediaImpl() {
48 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1),
49 "~VoEExternalMediaImpl() - dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
niklase@google.com470e71d2011-07-07 08:21:25 +000052int VoEExternalMediaImpl::RegisterExternalMediaProcessing(
53 int channel,
54 ProcessingTypes type,
Jelena Marusic0d266052015-05-04 14:15:32 +020055 VoEMediaProcess& processObject) {
56 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1),
57 "RegisterExternalMediaProcessing(channel=%d, type=%d, "
58 "processObject=0x%x)",
59 channel, type, &processObject);
60 if (!shared_->statistics().Initialized()) {
61 shared_->SetLastError(VE_NOT_INITED, kTraceError);
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +000062 return -1;
Jelena Marusic0d266052015-05-04 14:15:32 +020063 }
64 switch (type) {
65 case kPlaybackPerChannel:
66 case kRecordingPerChannel: {
67 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
68 voe::Channel* channelPtr = ch.channel();
69 if (channelPtr == NULL) {
70 shared_->SetLastError(
71 VE_CHANNEL_NOT_VALID, kTraceError,
72 "RegisterExternalMediaProcessing() failed to locate "
73 "channel");
74 return -1;
75 }
76 return channelPtr->RegisterExternalMediaProcessing(type, processObject);
77 }
78 case kPlaybackAllChannelsMixed: {
79 return shared_->output_mixer()->RegisterExternalMediaProcessing(
80 processObject);
81 }
82 case kRecordingAllChannelsMixed:
83 case kRecordingPreprocessing: {
84 return shared_->transmit_mixer()->RegisterExternalMediaProcessing(
85 &processObject, type);
86 }
87 }
88 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000089}
90
91int VoEExternalMediaImpl::DeRegisterExternalMediaProcessing(
92 int channel,
Jelena Marusic0d266052015-05-04 14:15:32 +020093 ProcessingTypes type) {
94 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(shared_->instance_id(), -1),
95 "DeRegisterExternalMediaProcessing(channel=%d)", channel);
96 if (!shared_->statistics().Initialized()) {
97 shared_->SetLastError(VE_NOT_INITED, kTraceError);
mflodman@webrtc.orgc80d9d92012-02-06 10:11:25 +000098 return -1;
Jelena Marusic0d266052015-05-04 14:15:32 +020099 }
100 switch (type) {
101 case kPlaybackPerChannel:
102 case kRecordingPerChannel: {
103 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
104 voe::Channel* channelPtr = ch.channel();
105 if (channelPtr == NULL) {
106 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
107 "RegisterExternalMediaProcessing() "
108 "failed to locate channel");
109 return -1;
110 }
111 return channelPtr->DeRegisterExternalMediaProcessing(type);
112 }
113 case kPlaybackAllChannelsMixed: {
114 return shared_->output_mixer()->DeRegisterExternalMediaProcessing();
115 }
116 case kRecordingAllChannelsMixed:
117 case kRecordingPreprocessing: {
118 return shared_->transmit_mixer()->DeRegisterExternalMediaProcessing(type);
119 }
120 }
121 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122}
123
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000124int VoEExternalMediaImpl::GetAudioFrame(int channel, int desired_sample_rate_hz,
125 AudioFrame* frame) {
Jelena Marusic0d266052015-05-04 14:15:32 +0200126 if (!shared_->statistics().Initialized()) {
127 shared_->SetLastError(VE_NOT_INITED, kTraceError);
128 return -1;
129 }
130 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
131 voe::Channel* channelPtr = ch.channel();
132 if (channelPtr == NULL) {
133 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
134 "GetAudioFrame() failed to locate channel");
135 return -1;
136 }
137 if (!channelPtr->ExternalMixing()) {
138 shared_->SetLastError(VE_INVALID_OPERATION, kTraceError,
139 "GetAudioFrame() was called on channel that is not"
140 " externally mixed.");
141 return -1;
142 }
143 if (!channelPtr->Playing()) {
144 shared_->SetLastError(
145 VE_INVALID_OPERATION, kTraceError,
146 "GetAudioFrame() was called on channel that is not playing.");
147 return -1;
148 }
149 if (desired_sample_rate_hz == -1) {
150 shared_->SetLastError(VE_BAD_ARGUMENT, kTraceError,
151 "GetAudioFrame() was called with bad sample rate.");
152 return -1;
153 }
154 frame->sample_rate_hz_ =
155 desired_sample_rate_hz == 0 ? -1 : desired_sample_rate_hz;
henrik.lundin42dda502016-05-18 05:36:01 -0700156 auto ret = channelPtr->GetAudioFrameWithMuted(channel, frame);
157 if (ret == MixerParticipant::AudioFrameInfo::kMuted) {
158 frame->Mute();
159 }
160 return ret == MixerParticipant::AudioFrameInfo::kError ? -1 : 0;
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000161}
162
163int VoEExternalMediaImpl::SetExternalMixing(int channel, bool enable) {
Jelena Marusic0d266052015-05-04 14:15:32 +0200164 WEBRTC_TRACE(kTraceApiCall, kTraceVoice,
165 VoEId(shared_->instance_id(), channel),
166 "SetExternalMixing(channel=%d, enable=%d)", channel, enable);
167 if (!shared_->statistics().Initialized()) {
168 shared_->SetLastError(VE_NOT_INITED, kTraceError);
169 return -1;
170 }
171 voe::ChannelOwner ch = shared_->channel_manager().GetChannel(channel);
172 voe::Channel* channelPtr = ch.channel();
173 if (channelPtr == NULL) {
174 shared_->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
175 "SetExternalMixing() failed to locate channel");
176 return -1;
177 }
178 return channelPtr->SetExternalMixing(enable);
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000179}
180
niklase@google.com470e71d2011-07-07 08:21:25 +0000181#endif // WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
182
183} // namespace webrtc