1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_AUDIO_PULSE_PULSE_UTIL_H_ 6 #define MEDIA_AUDIO_PULSE_PULSE_UTIL_H_ 7 8 #include <pulse/pulseaudio.h> 9 10 #include "base/basictypes.h" 11 #include "media/audio/audio_device_name.h" 12 #include "media/base/channel_layout.h" 13 14 namespace media { 15 16 class AudioParameters; 17 18 namespace pulse { 19 20 // A helper class that acquires pa_threaded_mainloop_lock() while in scope. 21 class AutoPulseLock { 22 public: 23 explicit AutoPulseLock(pa_threaded_mainloop* pa_mainloop) 24 : pa_mainloop_(pa_mainloop) { 25 pa_threaded_mainloop_lock(pa_mainloop_); 26 } 27 28 ~AutoPulseLock() { 29 pa_threaded_mainloop_unlock(pa_mainloop_); 30 } 31 32 private: 33 pa_threaded_mainloop* pa_mainloop_; 34 DISALLOW_COPY_AND_ASSIGN(AutoPulseLock); 35 }; 36 37 // Triggers pa_threaded_mainloop_signal() to avoid deadlocks. 38 void StreamSuccessCallback(pa_stream* s, int error, void* mainloop); 39 void ContextStateCallback(pa_context* context, void* mainloop); 40 41 pa_sample_format_t BitsToPASampleFormat(int bits_per_sample); 42 43 pa_channel_map ChannelLayoutToPAChannelMap(ChannelLayout channel_layout); 44 45 void WaitForOperationCompletion(pa_threaded_mainloop* mainloop, 46 pa_operation* operation); 47 48 int GetHardwareLatencyInBytes(pa_stream* stream, 49 int sample_rate, 50 int bytes_per_frame); 51 52 // Create a recording stream for the threaded mainloop, return true if success, 53 // otherwise false. |mainloop| and |context| have to be from a valid Pulse 54 // threaded mainloop and the handle of the created stream will be returned by 55 // |stream|. 56 bool CreateInputStream(pa_threaded_mainloop* mainloop, 57 pa_context* context, 58 pa_stream** stream, 59 const AudioParameters& params, 60 const std::string& device_id, 61 pa_stream_notify_cb_t stream_callback, 62 void* user_data); 63 64 // Create a playback stream for the threaded mainloop, return true if success, 65 // otherwise false. This function will create a new Pulse threaded mainloop, 66 // and the handles of the mainloop, context and stream will be returned by 67 // |mainloop|, |context| and |stream|. 68 bool CreateOutputStream(pa_threaded_mainloop** mainloop, 69 pa_context** context, 70 pa_stream** stream, 71 const AudioParameters& params, 72 const std::string& device_id, 73 pa_stream_notify_cb_t stream_callback, 74 pa_stream_request_cb_t write_callback, 75 void* user_data); 76 77 } // namespace pulse 78 79 } // namespace media 80 81 #endif // MEDIA_AUDIO_PULSE_PULSE_UTIL_H_ 82