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 AUDIO_DEVICE_AUDIO_MIXER_MANAGER_PULSE_LINUX_H_
12 #define AUDIO_DEVICE_AUDIO_MIXER_MANAGER_PULSE_LINUX_H_
13 
14 #include "modules/audio_device/include/audio_device.h"
15 #include "modules/audio_device/linux/pulseaudiosymboltable_linux.h"
16 #include "rtc_base/thread_checker.h"
17 #include "typedefs.h"  // NOLINT(build/include)
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 
26 namespace webrtc
27 {
28 
29 class AudioMixerManagerLinuxPulse
30 {
31 public:
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 SpeakerVolumeIsAvailable(bool& available);
41     int32_t SpeakerMuteIsAvailable(bool& available);
42     int32_t SetSpeakerMute(bool enable);
43     int32_t StereoPlayoutIsAvailable(bool& available);
44     int32_t StereoRecordingIsAvailable(bool& available);
45     int32_t SpeakerMute(bool& enabled) const;
46     int32_t MicrophoneMuteIsAvailable(bool& available);
47     int32_t SetMicrophoneMute(bool enable);
48     int32_t MicrophoneMute(bool& enabled) const;
49     int32_t MicrophoneVolumeIsAvailable(bool& available);
50     int32_t SetMicrophoneVolume(uint32_t volume);
51     int32_t MicrophoneVolume(uint32_t& volume) const;
52     int32_t MaxMicrophoneVolume(uint32_t& maxVolume) const;
53     int32_t MinMicrophoneVolume(uint32_t& minVolume) const;
54     int32_t SetPulseAudioObjects(pa_threaded_mainloop* mainloop,
55                                  pa_context* context);
56     int32_t Close();
57     int32_t CloseSpeaker();
58     int32_t CloseMicrophone();
59     bool SpeakerIsInitialized() const;
60     bool MicrophoneIsInitialized() const;
61 
62 public:
63     AudioMixerManagerLinuxPulse();
64     ~AudioMixerManagerLinuxPulse();
65 
66 private:
67     static void PaSinkInfoCallback(pa_context *c, const pa_sink_info *i,
68                                    int eol, void *pThis);
69     static void PaSinkInputInfoCallback(pa_context *c,
70                                         const pa_sink_input_info *i, int eol,
71                                         void *pThis);
72     static void PaSourceInfoCallback(pa_context *c, const pa_source_info *i,
73                                      int eol, void *pThis);
74     static void
75         PaSetVolumeCallback(pa_context* /*c*/, int success, void* /*pThis*/);
76     void PaSinkInfoCallbackHandler(const pa_sink_info *i, int eol);
77     void PaSinkInputInfoCallbackHandler(const pa_sink_input_info *i, int eol);
78     void PaSourceInfoCallbackHandler(const pa_source_info *i, int eol);
79 
80     void WaitForOperationCompletion(pa_operation* paOperation) const;
81 
82     bool GetSinkInputInfo() const;
83     bool GetSinkInfoByIndex(int device_index)const ;
84     bool GetSourceInfoByIndex(int device_index) const;
85 
86 private:
87     int16_t _paOutputDeviceIndex;
88     int16_t _paInputDeviceIndex;
89 
90     pa_stream* _paPlayStream;
91     pa_stream* _paRecStream;
92 
93     pa_threaded_mainloop* _paMainloop;
94     pa_context* _paContext;
95 
96     mutable uint32_t _paVolume;
97     mutable uint32_t _paMute;
98     mutable uint32_t _paVolSteps;
99     bool _paSpeakerMute;
100     mutable uint32_t _paSpeakerVolume;
101     mutable uint8_t _paChannels;
102     bool _paObjectsSet;
103 
104     // Stores thread ID in constructor.
105     // We can then use ThreadChecker::CalledOnValidThread() to ensure that
106     // other methods are called from the same thread.
107     // Currently only does RTC_DCHECK(thread_checker_.CalledOnValidThread()).
108     rtc::ThreadChecker thread_checker_;
109 };
110 
111 }
112 
113 #endif  // MODULES_AUDIO_DEVICE_MAIN_SOURCE_LINUX_AUDIO_MIXER_MANAGER_PULSE_LINUX_H_
114