OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Creates an audio output stream based on the PulseAudio asynchronous API. | 5 // Creates an audio output stream based on the PulseAudio asynchronous API; |
6 // specifically using the pa_threaded_mainloop model. | |
6 // | 7 // |
7 // If the stream is successfully opened, Close() must be called before the | 8 // If the stream is successfully opened, Close() must be called before the |
8 // stream is deleted as Close() is responsible for ensuring resource cleanup | 9 // stream is deleted as Close() is responsible for ensuring resource cleanup |
9 // occurs. | 10 // occurs. |
10 // | 11 // |
11 // This object is designed so that all AudioOutputStream methods will be called | 12 // This object is designed so that all AudioOutputStream methods will be called |
12 // on the same thread that created the object. | 13 // on the same thread that created the object. |
13 // | 14 // |
14 // WARNING: This object blocks on internal PulseAudio calls in Open() while | 15 // WARNING: This object blocks on internal PulseAudio calls in Open() while |
15 // waiting for PulseAudio's context structure to be ready. It also blocks in | 16 // waiting for PulseAudio's context structure to be ready. It also blocks in |
16 // inside PulseAudio in Start() and repeated during playback, waiting for | 17 // inside PulseAudio in Start() and repeated during playback, waiting for |
17 // PulseAudio write callbacks to occur. | 18 // PulseAudio write callbacks to occur. |
18 | 19 |
19 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 20 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
20 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 21 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
21 | 22 |
22 #include <pulse/pulseaudio.h> | 23 #include <pulse/pulseaudio.h> |
scherkus (not reviewing)
2012/10/11 20:19:31
now that you got rid of the pa_stream_state_t stuf
DaleCurtis
2012/10/12 00:09:12
Done.
| |
23 | 24 |
24 #include "base/memory/scoped_ptr.h" | 25 #include "base/memory/scoped_ptr.h" |
25 #include "base/memory/weak_ptr.h" | |
26 #include "media/audio/audio_io.h" | 26 #include "media/audio/audio_io.h" |
27 #include "media/base/channel_layout.h" | 27 #include "media/audio/audio_parameters.h" |
28 | 28 |
29 namespace media { | 29 namespace media { |
30 | 30 class AudioManagerBase; |
31 #if defined(OS_LINUX) | |
32 class AudioManagerLinux; | |
33 typedef AudioManagerLinux AudioManagerPulse; | |
34 #elif defined(OS_OPENBSD) | |
35 class AudioManagerOpenBSD; | |
36 typedef AudioManagerOpenBSD AudioManagerPulse; | |
37 #else | |
38 #error Unsupported platform | |
39 #endif | |
40 | |
41 class AudioParameters; | |
42 class SeekableBuffer; | |
43 | 31 |
44 class PulseAudioOutputStream : public AudioOutputStream { | 32 class PulseAudioOutputStream : public AudioOutputStream { |
45 public: | 33 public: |
46 PulseAudioOutputStream(const AudioParameters& params, | 34 PulseAudioOutputStream(const AudioParameters& params, |
47 AudioManagerPulse* manager); | 35 AudioManagerBase* manager); |
48 | 36 |
49 virtual ~PulseAudioOutputStream(); | 37 virtual ~PulseAudioOutputStream(); |
50 | 38 |
51 // Implementation of AudioOutputStream. | 39 // Implementation of AudioOutputStream. |
52 virtual bool Open() OVERRIDE; | 40 virtual bool Open() OVERRIDE; |
53 virtual void Close() OVERRIDE; | 41 virtual void Close() OVERRIDE; |
54 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | 42 virtual void Start(AudioSourceCallback* callback) OVERRIDE; |
55 virtual void Stop() OVERRIDE; | 43 virtual void Stop() OVERRIDE; |
56 virtual void SetVolume(double volume) OVERRIDE; | 44 virtual void SetVolume(double volume) OVERRIDE; |
57 virtual void GetVolume(double* volume) OVERRIDE; | 45 virtual void GetVolume(double* volume) OVERRIDE; |
58 | 46 |
59 private: | 47 private: |
60 // PulseAudio Callbacks. | 48 // PulseAudio Callbacks. |
61 static void ContextStateCallback(pa_context* context, void* state_addr); | |
62 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, | 49 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, |
63 void* stream_addr); | 50 void* stream_addr); |
64 | 51 |
65 // Iterate the PulseAudio mainloop to get write requests. | 52 // Retrieves a packet of audio data from |source_callback_| and writes it to |
66 void WaitForWriteRequest(); | 53 // |buffer|. Returns the number of frames written. |
54 int FillBuffer(void* buffer, size_t buffer_size); | |
67 | 55 |
68 // Get another packet from the data source and write it to the client buffer. | 56 // Fulfill a write request from the write request callback. Outputs silence |
69 bool BufferPacketFromSource(); | 57 // if the request could not be fulfilled. |
70 | |
71 // Fulfill a write request from the write request callback. If the write | |
72 // can't be finished a first, post a new attempt to the message loop. | |
73 void FulfillWriteRequest(size_t requested_bytes); | 58 void FulfillWriteRequest(size_t requested_bytes); |
74 | 59 |
75 // Write data from the client buffer to the PulseAudio stream. | |
76 void WriteToStream(size_t bytes_to_write, size_t* bytes_written); | |
77 | |
78 // API for Proxying calls to the AudioSourceCallback provided during Start(). | |
79 int RunDataCallback(AudioBus* audio_bus, AudioBuffersState buffers_state); | |
80 | |
81 // Close() helper function to free internal structs. | 60 // Close() helper function to free internal structs. |
82 void Reset(); | 61 void Reset(); |
83 | 62 |
84 // Configuration constants from the constructor. Referencable by all threads | 63 // Returns the current hardware latency value in bytes. |
85 // since they are constants. | 64 int GetHardwareLatencyInBytes(); |
86 const ChannelLayout channel_layout_; | 65 |
87 const uint32 channel_count_; | 66 // Helper method for waiting on Pulse Audio operations to complete. |
88 const pa_sample_format_t sample_format_; | 67 void WaitForPulseOperation(pa_operation* op); |
89 const uint32 sample_rate_; | 68 |
90 const uint32 bytes_per_frame_; | 69 // AudioParameters from the constructor. |
70 const AudioParameters params_; | |
91 | 71 |
92 // Audio manager that created us. Used to report that we've closed. | 72 // Audio manager that created us. Used to report that we've closed. |
93 AudioManagerPulse* manager_; | 73 AudioManagerBase* manager_; |
94 | 74 |
95 // PulseAudio API structs. | 75 // PulseAudio API structs. |
96 pa_context* pa_context_; | 76 pa_context* pa_context_; |
97 pa_mainloop* pa_mainloop_; | 77 pa_threaded_mainloop* pa_mainloop_; |
98 | |
99 // Handle to the actual PulseAudio playback stream. | |
100 pa_stream* playback_handle_; | 78 pa_stream* playback_handle_; |
101 | 79 |
102 // Device configuration data. Populated after Open() completes. | |
103 uint32 packet_size_; | |
104 uint32 frames_per_packet_; | |
105 | |
106 // Client side audio buffer feeding pulse audio's server side buffer. | |
107 scoped_ptr<media::SeekableBuffer> client_buffer_; | |
108 | |
109 // Float representation of volume from 0.0 to 1.0. | 80 // Float representation of volume from 0.0 to 1.0. |
110 float volume_; | 81 float volume_; |
111 | 82 |
112 // Flag indicating the code should stop reading from the data source or | |
113 // writing to the PulseAudio server. This is set because the device has | |
114 // entered an unrecoverable error state, or the Close() has executed. | |
115 bool stream_stopped_; | |
116 | |
117 // Whether or not PulseAudio has called the WriteCallback for the most recent | |
118 // set of pa_mainloop iterations. | |
119 bool write_callback_handled_; | |
120 | |
121 // Allows us to run tasks on the PulseAudioOutputStream instance which are | |
122 // bound by its lifetime. | |
123 base::WeakPtrFactory<PulseAudioOutputStream> weak_factory_; | |
124 | |
125 // Callback to audio data source. | 83 // Callback to audio data source. |
126 AudioSourceCallback* source_callback_; | 84 AudioSourceCallback* source_callback_; |
127 | 85 |
128 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 86 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
129 scoped_ptr<AudioBus> audio_bus_; | 87 scoped_ptr<AudioBus> audio_bus_; |
130 | 88 |
131 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | 89 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
132 }; | 90 }; |
133 | 91 |
134 } // namespace media | 92 } // namespace media |
135 | 93 |
136 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 94 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
OLD | NEW |