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#ifndef WEBRTC_AUDIO_DEVICE_AUDIO_MIXER_MANAGER_PULSE_LINUX_H
12#define WEBRTC_AUDIO_DEVICE_AUDIO_MIXER_MANAGER_PULSE_LINUX_H
13
14#include "webrtc/modules/audio_device/include/audio_device.h"
15#include "webrtc/modules/audio_device/linux/pulseaudiosymboltable_linux.h"
16#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17#include "webrtc/typedefs.h"
18
19#include <pulse/pulseaudio.h>
20#include <stdint.h>
21
22#ifndef UINT32_MAX
23#define UINT32_MAX  ((uint32_t)-1)
24#endif
25
26namespace webrtc
27{
28
29class AudioMixerManagerLinuxPulse
30{
31public:
32    int32_t SetPlayStream(pa_stream* playStream);
33    int32_t SetRecStream(pa_stream* recStream);
34    int32_t OpenSpeaker(uint16_t deviceIndex);
35    int32_t OpenMicrophone(uint16_t deviceIndex);
36    int32_t SetSpeakerVolume(uint32_t volume);
37    int32_t SpeakerVolume(uint32_t& volume) const;
38    int32_t MaxSpeakerVolume(uint32_t& maxVolume) const;
39    int32_t MinSpeakerVolume(uint32_t& minVolume) const;
40    int32_t SpeakerVolumeStepSize(uint16_t& stepSize) const;
41    int32_t SpeakerVolumeIsAvailable(bool& available);
42    int32_t SpeakerMuteIsAvailable(bool& available);
43    int32_t SetSpeakerMute(bool enable);
44    int32_t StereoPlayoutIsAvailable(bool& available);
45    int32_t StereoRecordingIsAvailable(bool& available);
46    int32_t SpeakerMute(bool& enabled) const;
47    int32_t MicrophoneMuteIsAvailable(bool& available);
48    int32_t SetMicrophoneMute(bool enable);
49    int32_t MicrophoneMute(bool& enabled) const;
50    int32_t MicrophoneBoostIsAvailable(bool& available);
51    int32_t SetMicrophoneBoost(bool enable);
52    int32_t MicrophoneBoost(bool& enabled) const;
53    int32_t MicrophoneVolumeIsAvailable(bool& available);
54    int32_t SetMicrophoneVolume(uint32_t volume);
55    int32_t MicrophoneVolume(uint32_t& volume) const;
56    int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const;
57    int32_t MinMicrophoneVolume(uint32_t& minVolume) const;
58    int32_t MicrophoneVolumeStepSize(uint16_t& stepSize) const;
59    int32_t SetPulseAudioObjects(pa_threaded_mainloop* mainloop,
60                                 pa_context* context);
61    int32_t Close();
62    int32_t CloseSpeaker();
63    int32_t CloseMicrophone();
64    bool SpeakerIsInitialized() const;
65    bool MicrophoneIsInitialized() const;
66
67public:
68    AudioMixerManagerLinuxPulse(const int32_t id);
69    ~AudioMixerManagerLinuxPulse();
70
71private:
72    static void PaSinkInfoCallback(pa_context *c, const pa_sink_info *i,
73                                   int eol, void *pThis);
74    static void PaSinkInputInfoCallback(pa_context *c,
75                                        const pa_sink_input_info *i, int eol,
76                                        void *pThis);
77    static void PaSourceInfoCallback(pa_context *c, const pa_source_info *i,
78                                     int eol, void *pThis);
79    static void
80        PaSetVolumeCallback(pa_context* /*c*/, int success, void* /*pThis*/);
81    void PaSinkInfoCallbackHandler(const pa_sink_info *i, int eol);
82    void PaSinkInputInfoCallbackHandler(const pa_sink_input_info *i, int eol);
83    void PaSourceInfoCallbackHandler(const pa_source_info *i, int eol);
84
85    void ResetCallbackVariables() const;
86    void WaitForOperationCompletion(pa_operation* paOperation) const;
87    void PaLock() const;
88    void PaUnLock() const;
89
90    bool GetSinkInputInfo() const;
91    bool GetSinkInfoByIndex(int device_index)const ;
92    bool GetSourceInfoByIndex(int device_index) const;
93
94private:
95    CriticalSectionWrapper& _critSect;
96    int32_t _id;
97    int16_t _paOutputDeviceIndex;
98    int16_t _paInputDeviceIndex;
99
100    pa_stream* _paPlayStream;
101    pa_stream* _paRecStream;
102
103    pa_threaded_mainloop* _paMainloop;
104    pa_context* _paContext;
105
106    mutable uint32_t _paVolume;
107    mutable uint32_t _paMute;
108    mutable uint32_t _paVolSteps;
109    bool _paSpeakerMute;
110    mutable uint32_t _paSpeakerVolume;
111    mutable uint8_t _paChannels;
112    bool _paObjectsSet;
113    mutable bool _callbackValues;
114};
115
116}
117
118#endif  // MODULES_AUDIO_DEVICE_MAIN_SOURCE_LINUX_AUDIO_MIXER_MANAGER_PULSE_LINUX_H_
119