1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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
11 #include "webrtc/voice_engine/shared_data.h"
12
13 #include "webrtc/modules/audio_processing/include/audio_processing.h"
14 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
15 #include "webrtc/system_wrappers/include/trace.h"
16 #include "webrtc/voice_engine/channel.h"
17 #include "webrtc/voice_engine/output_mixer.h"
18 #include "webrtc/voice_engine/transmit_mixer.h"
19
20 namespace webrtc {
21
22 namespace voe {
23
24 static int32_t _gInstanceCounter = 0;
25
SharedData(const Config & config)26 SharedData::SharedData(const Config& config)
27 : _instanceId(++_gInstanceCounter),
28 _apiCritPtr(CriticalSectionWrapper::CreateCriticalSection()),
29 _channelManager(_gInstanceCounter, config),
30 _engineStatistics(_gInstanceCounter),
31 _audioDevicePtr(NULL),
32 _moduleProcessThreadPtr(ProcessThread::Create("VoiceProcessThread")) {
33 Trace::CreateTrace();
34 if (OutputMixer::Create(_outputMixerPtr, _gInstanceCounter) == 0)
35 {
36 _outputMixerPtr->SetEngineInformation(_engineStatistics);
37 }
38 if (TransmitMixer::Create(_transmitMixerPtr, _gInstanceCounter) == 0)
39 {
40 _transmitMixerPtr->SetEngineInformation(*_moduleProcessThreadPtr,
41 _engineStatistics,
42 _channelManager);
43 }
44 _audioDeviceLayer = AudioDeviceModule::kPlatformDefaultAudio;
45 }
46
~SharedData()47 SharedData::~SharedData()
48 {
49 OutputMixer::Destroy(_outputMixerPtr);
50 TransmitMixer::Destroy(_transmitMixerPtr);
51 if (_audioDevicePtr) {
52 _audioDevicePtr->Release();
53 }
54 delete _apiCritPtr;
55 _moduleProcessThreadPtr->Stop();
56 Trace::ReturnTrace();
57 }
58
set_audio_device(AudioDeviceModule * audio_device)59 void SharedData::set_audio_device(AudioDeviceModule* audio_device)
60 {
61 // AddRef first in case the pointers are equal.
62 if (audio_device)
63 audio_device->AddRef();
64 if (_audioDevicePtr)
65 _audioDevicePtr->Release();
66 _audioDevicePtr = audio_device;
67 }
68
set_audio_processing(AudioProcessing * audioproc)69 void SharedData::set_audio_processing(AudioProcessing* audioproc) {
70 audioproc_.reset(audioproc);
71 _transmitMixerPtr->SetAudioProcessingModule(audioproc);
72 _outputMixerPtr->SetAudioProcessingModule(audioproc);
73 }
74
NumOfSendingChannels()75 int SharedData::NumOfSendingChannels() {
76 ChannelManager::Iterator it(&_channelManager);
77 int sending_channels = 0;
78
79 for (ChannelManager::Iterator it(&_channelManager); it.IsValid();
80 it.Increment()) {
81 if (it.GetChannel()->Sending())
82 ++sending_channels;
83 }
84
85 return sending_channels;
86 }
87
NumOfPlayingChannels()88 int SharedData::NumOfPlayingChannels() {
89 ChannelManager::Iterator it(&_channelManager);
90 int playout_channels = 0;
91
92 for (ChannelManager::Iterator it(&_channelManager); it.IsValid();
93 it.Increment()) {
94 if (it.GetChannel()->Playing())
95 ++playout_channels;
96 }
97
98 return playout_channels;
99 }
100
SetLastError(int32_t error) const101 void SharedData::SetLastError(int32_t error) const {
102 _engineStatistics.SetLastError(error);
103 }
104
SetLastError(int32_t error,TraceLevel level) const105 void SharedData::SetLastError(int32_t error,
106 TraceLevel level) const {
107 _engineStatistics.SetLastError(error, level);
108 }
109
SetLastError(int32_t error,TraceLevel level,const char * msg) const110 void SharedData::SetLastError(int32_t error, TraceLevel level,
111 const char* msg) const {
112 _engineStatistics.SetLastError(error, level, msg);
113 }
114
115 } // namespace voe
116
117 } // namespace webrtc
118