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