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