niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
xians@webrtc.org | 79af734 | 2012-01-31 12:22:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/voe_external_media_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 13 | #include "webrtc/system_wrappers/include/trace.h" |
pbos@webrtc.org | 956aa7e | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 14 | #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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 22 | VoEExternalMedia* VoEExternalMedia::GetInterface(VoiceEngine* voiceEngine) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | #ifndef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 24 | return NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | #else |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 26 | if (NULL == voiceEngine) { |
| 27 | return NULL; |
| 28 | } |
| 29 | VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine); |
| 30 | s->AddRef(); |
| 31 | return s; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | #endif |
| 33 | } |
| 34 | |
| 35 | #ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API |
| 36 | |
tommi@webrtc.org | 851becd | 2012-04-04 14:57:19 +0000 | [diff] [blame] | 37 | VoEExternalMediaImpl::VoEExternalMediaImpl(voe::SharedData* shared) |
wjia@webrtc.org | 3f9db37 | 2013-01-12 01:09:03 +0000 | [diff] [blame] | 38 | : |
| 39 | #ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 40 | playout_delay_ms_(0), |
wjia@webrtc.org | 3f9db37 | 2013-01-12 01:09:03 +0000 | [diff] [blame] | 41 | #endif |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 42 | shared_(shared) { |
| 43 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1), |
| 44 | "VoEExternalMediaImpl() - ctor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 47 | VoEExternalMediaImpl::~VoEExternalMediaImpl() { |
| 48 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(shared_->instance_id(), -1), |
| 49 | "~VoEExternalMediaImpl() - dtor"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | int VoEExternalMediaImpl::RegisterExternalMediaProcessing( |
| 53 | int channel, |
| 54 | ProcessingTypes type, |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 55 | 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.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 62 | return -1; |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 63 | } |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int VoEExternalMediaImpl::DeRegisterExternalMediaProcessing( |
| 92 | int channel, |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 93 | 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.org | c80d9d9 | 2012-02-06 10:11:25 +0000 | [diff] [blame] | 98 | return -1; |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 99 | } |
| 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
roosa@google.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 124 | int VoEExternalMediaImpl::GetAudioFrame(int channel, int desired_sample_rate_hz, |
| 125 | AudioFrame* frame) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 126 | 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.lundin | 42dda50 | 2016-05-18 05:36:01 -0700 | [diff] [blame^] | 156 | 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.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int VoEExternalMediaImpl::SetExternalMixing(int channel, bool enable) { |
Jelena Marusic | 0d26605 | 2015-05-04 14:15:32 +0200 | [diff] [blame] | 164 | 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.com | 1b60ceb | 2012-12-12 23:00:29 +0000 | [diff] [blame] | 179 | } |
| 180 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 181 | #endif // WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API |
| 182 | |
| 183 | } // namespace webrtc |