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