pulse_output.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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// Creates an audio output stream based on the PulseAudio asynchronous API;
6// specifically using the pa_threaded_mainloop model.
7//
8// If the stream is successfully opened, Close() must be called before the
9// stream is deleted as Close() is responsible for ensuring resource cleanup
10// occurs.
11//
12// This object is designed so that all AudioOutputStream methods will be called
13// on the same thread that created the object.
14//
15// WARNING: This object blocks on internal PulseAudio calls in Open() while
16// waiting for PulseAudio's context structure to be ready.  It also blocks in
17// inside PulseAudio in Start() and repeated during playback, waiting for
18// PulseAudio write callbacks to occur.
19
20#ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_
21#define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_
22
23#include "base/memory/scoped_ptr.h"
24#include "media/audio/audio_io.h"
25#include "media/audio/audio_parameters.h"
26
27struct pa_context;
28struct pa_operation;
29struct pa_stream;
30struct pa_threaded_mainloop;
31
32namespace media {
33class AudioManagerBase;
34
35class PulseAudioOutputStream : public AudioOutputStream {
36 public:
37  PulseAudioOutputStream(const AudioParameters& params,
38                         AudioManagerBase* manager);
39
40  virtual ~PulseAudioOutputStream();
41
42  // Implementation of AudioOutputStream.
43  virtual bool Open() OVERRIDE;
44  virtual void Close() OVERRIDE;
45  virtual void Start(AudioSourceCallback* callback) OVERRIDE;
46  virtual void Stop() OVERRIDE;
47  virtual void SetVolume(double volume) OVERRIDE;
48  virtual void GetVolume(double* volume) OVERRIDE;
49
50 private:
51  // Called by PulseAudio when |pa_context_| and |pa_stream_| change state.  If
52  // an unexpected failure state change happens and |source_callback_| is set
53  // these methods will forward the error via OnError().
54  static void ContextNotifyCallback(pa_context* c, void* p_this);
55  static void StreamNotifyCallback(pa_stream* s, void* p_this);
56
57  // Called by PulseAudio when it needs more audio data.
58  static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this);
59
60  // Fulfill a write request from the write request callback.  Outputs silence
61  // if the request could not be fulfilled.
62  void FulfillWriteRequest(size_t requested_bytes);
63
64  // Close() helper function to free internal structs.
65  void Reset();
66
67  // AudioParameters from the constructor.
68  const AudioParameters params_;
69
70  // Audio manager that created us.  Used to report that we've closed.
71  AudioManagerBase* manager_;
72
73  // PulseAudio API structs.
74  pa_context* pa_context_;
75  pa_threaded_mainloop* pa_mainloop_;
76  pa_stream* pa_stream_;
77
78  // Float representation of volume from 0.0 to 1.0.
79  float volume_;
80
81  // Callback to audio data source.  Must only be modified while holding a lock
82  // on |pa_mainloop_| via pa_threaded_mainloop_lock().
83  AudioSourceCallback* source_callback_;
84
85  // Container for retrieving data from AudioSourceCallback::OnMoreData().
86  scoped_ptr<AudioBus> audio_bus_;
87
88  DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream);
89};
90
91}  // namespace media
92
93#endif  // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_
94