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
14namespace media {
15
16class AudioParameters;
17
18namespace pulse {
19
20// A helper class that acquires pa_threaded_mainloop_lock() while in scope.
21class 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.
38void StreamSuccessCallback(pa_stream* s, int error, void* mainloop);
39void ContextStateCallback(pa_context* context, void* mainloop);
40
41pa_sample_format_t BitsToPASampleFormat(int bits_per_sample);
42
43pa_channel_map ChannelLayoutToPAChannelMap(ChannelLayout channel_layout);
44
45void WaitForOperationCompletion(pa_threaded_mainloop* mainloop,
46                                pa_operation* operation);
47
48int 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|.
56bool 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|.
68bool 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