Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / audio / pulse / pulse_output.h
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 <string>
24
25 #include "base/memory/scoped_ptr.h"
26 #include "media/audio/audio_io.h"
27 #include "media/audio/audio_parameters.h"
28
29 struct pa_context;
30 struct pa_operation;
31 struct pa_stream;
32 struct pa_threaded_mainloop;
33
34 namespace media {
35 class AudioManagerBase;
36
37 class PulseAudioOutputStream : public AudioOutputStream {
38  public:
39   PulseAudioOutputStream(const AudioParameters& params,
40                          const std::string& device_id,
41                          AudioManagerBase* manager);
42
43   virtual ~PulseAudioOutputStream();
44
45   // Implementation of AudioOutputStream.
46   virtual bool Open() OVERRIDE;
47   virtual void Close() OVERRIDE;
48   virtual void Start(AudioSourceCallback* callback) OVERRIDE;
49   virtual void Stop() OVERRIDE;
50   virtual void SetVolume(double volume) OVERRIDE;
51   virtual void GetVolume(double* volume) OVERRIDE;
52
53  private:
54   // Called by PulseAudio when |pa_stream_| change state.  If an unexpected
55   // failure state change happens and |source_callback_| is set
56   // this method will forward the error via OnError().
57   static void StreamNotifyCallback(pa_stream* s, void* p_this);
58
59   // Called by PulseAudio when it needs more audio data.
60   static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this);
61
62   // Fulfill a write request from the write request callback.  Outputs silence
63   // if the request could not be fulfilled.
64   void FulfillWriteRequest(size_t requested_bytes);
65
66   // Close() helper function to free internal structs.
67   void Reset();
68
69   // AudioParameters from the constructor.
70   const AudioParameters params_;
71
72   // The device ID for the device to open.
73   const std::string device_id_;
74
75   // Audio manager that created us.  Used to report that we've closed.
76   AudioManagerBase* manager_;
77
78   // PulseAudio API structs.
79   pa_context* pa_context_;
80   pa_threaded_mainloop* pa_mainloop_;
81   pa_stream* pa_stream_;
82
83   // Float representation of volume from 0.0 to 1.0.
84   float volume_;
85
86   // Callback to audio data source.  Must only be modified while holding a lock
87   // on |pa_mainloop_| via pa_threaded_mainloop_lock().
88   AudioSourceCallback* source_callback_;
89
90   // Container for retrieving data from AudioSourceCallback::OnMoreData().
91   scoped_ptr<AudioBus> audio_bus_;
92
93   DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream);
94 };
95
96 }  // namespace media
97
98 #endif  // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_