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