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.
5 #ifndef MEDIA_AUDIO_PULSE_PULSE_UTIL_H_
6 #define MEDIA_AUDIO_PULSE_PULSE_UTIL_H_
8 #include <pulse/pulseaudio.h>
10 #include "base/basictypes.h"
11 #include "media/audio/audio_device_name.h"
12 #include "media/base/channel_layout.h"
16 class AudioParameters;
20 // A helper class that acquires pa_threaded_mainloop_lock() while in scope.
23 explicit AutoPulseLock(pa_threaded_mainloop* pa_mainloop)
24 : pa_mainloop_(pa_mainloop) {
25 pa_threaded_mainloop_lock(pa_mainloop_);
29 pa_threaded_mainloop_unlock(pa_mainloop_);
33 pa_threaded_mainloop* pa_mainloop_;
34 DISALLOW_COPY_AND_ASSIGN(AutoPulseLock);
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);
41 pa_sample_format_t BitsToPASampleFormat(int bits_per_sample);
43 pa_channel_map ChannelLayoutToPAChannelMap(ChannelLayout channel_layout);
45 void WaitForOperationCompletion(pa_threaded_mainloop* mainloop,
46 pa_operation* operation);
48 int GetHardwareLatencyInBytes(pa_stream* stream,
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
56 bool CreateInputStream(pa_threaded_mainloop* mainloop,
59 const AudioParameters& params,
60 const std::string& device_id,
61 pa_stream_notify_cb_t stream_callback,
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,
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,
81 #endif // MEDIA_AUDIO_PULSE_PULSE_UTIL_H_