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. |
scherkus (not reviewing)
2012/10/10 17:56:29
does this comment need to be updated?
DaleCurtis
2012/10/10 18:19:05
It's still accurate, but I'll add some details on
| |
6 // | 6 // |
7 // If the stream is successfully opened, Close() must be called before the | 7 // If the stream is successfully opened, Close() must be called before the |
8 // stream is deleted as Close() is responsible for ensuring resource cleanup | 8 // stream is deleted as Close() is responsible for ensuring resource cleanup |
9 // occurs. | 9 // occurs. |
10 // | 10 // |
11 // This object is designed so that all AudioOutputStream methods will be called | 11 // This object is designed so that all AudioOutputStream methods will be called |
12 // on the same thread that created the object. | 12 // on the same thread that created the object. |
13 // | 13 // |
14 // WARNING: This object blocks on internal PulseAudio calls in Open() while | 14 // 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 | 15 // waiting for PulseAudio's context structure to be ready. It also blocks in |
16 // inside PulseAudio in Start() and repeated during playback, waiting for | 16 // inside PulseAudio in Start() and repeated during playback, waiting for |
17 // PulseAudio write callbacks to occur. | 17 // PulseAudio write callbacks to occur. |
18 | 18 |
19 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 19 #ifndef MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
20 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 20 #define MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
21 | 21 |
22 #include <pulse/pulseaudio.h> | 22 #include <pulse/pulseaudio.h> |
23 | 23 |
24 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
25 #include "base/memory/weak_ptr.h" | |
26 #include "media/audio/audio_io.h" | 25 #include "media/audio/audio_io.h" |
27 #include "media/base/channel_layout.h" | 26 #include "media/audio/audio_parameters.h" |
28 | 27 |
29 namespace media { | 28 namespace media { |
30 | 29 |
31 #if defined(OS_LINUX) | 30 #if defined(OS_LINUX) |
32 class AudioManagerLinux; | 31 class AudioManagerLinux; |
33 typedef AudioManagerLinux AudioManagerPulse; | 32 typedef AudioManagerLinux AudioManagerPulse; |
34 #elif defined(OS_OPENBSD) | 33 #elif defined(OS_OPENBSD) |
35 class AudioManagerOpenBSD; | 34 class AudioManagerOpenBSD; |
36 typedef AudioManagerOpenBSD AudioManagerPulse; | 35 typedef AudioManagerOpenBSD AudioManagerPulse; |
37 #else | 36 #else |
38 #error Unsupported platform | 37 #error Unsupported platform |
39 #endif | 38 #endif |
40 | 39 |
41 class AudioParameters; | |
42 class SeekableBuffer; | |
43 | |
44 class PulseAudioOutputStream : public AudioOutputStream { | 40 class PulseAudioOutputStream : public AudioOutputStream { |
45 public: | 41 public: |
46 PulseAudioOutputStream(const AudioParameters& params, | 42 PulseAudioOutputStream(const AudioParameters& params, |
47 AudioManagerPulse* manager); | 43 AudioManagerPulse* manager); |
48 | 44 |
49 virtual ~PulseAudioOutputStream(); | 45 virtual ~PulseAudioOutputStream(); |
50 | 46 |
51 // Implementation of AudioOutputStream. | 47 // Implementation of AudioOutputStream. |
52 virtual bool Open() OVERRIDE; | 48 virtual bool Open() OVERRIDE; |
53 virtual void Close() OVERRIDE; | 49 virtual void Close() OVERRIDE; |
54 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | 50 virtual void Start(AudioSourceCallback* callback) OVERRIDE; |
55 virtual void Stop() OVERRIDE; | 51 virtual void Stop() OVERRIDE; |
56 virtual void SetVolume(double volume) OVERRIDE; | 52 virtual void SetVolume(double volume) OVERRIDE; |
57 virtual void GetVolume(double* volume) OVERRIDE; | 53 virtual void GetVolume(double* volume) OVERRIDE; |
58 | 54 |
59 private: | 55 private: |
60 // PulseAudio Callbacks. | 56 // PulseAudio Callbacks. |
61 static void ContextStateCallback(pa_context* context, void* state_addr); | 57 static void ContextStateCallback(pa_context* context, void* p_this); |
58 static void StreamStateCallback(pa_stream* stream, void* p_this); | |
62 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, | 59 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, |
63 void* stream_addr); | 60 void* stream_addr); |
64 | 61 |
65 // Iterate the PulseAudio mainloop to get write requests. | 62 // Retrieve a packet of audio data from |source_callback_| and write it to |
66 void WaitForWriteRequest(); | 63 // |interleaved_audio_data_|. Returns the number of frames written. |
64 int FillBuffer(); | |
67 | 65 |
68 // Get another packet from the data source and write it to the client buffer. | 66 // Fulfill a write request from the write request callback. Outputs silence |
69 bool BufferPacketFromSource(); | 67 // 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); | 68 void FulfillWriteRequest(size_t requested_bytes); |
74 | 69 |
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(). | 70 // API for Proxying calls to the AudioSourceCallback provided during Start(). |
79 int RunDataCallback(AudioBus* audio_bus, AudioBuffersState buffers_state); | 71 int RunDataCallback(AudioBus* audio_bus, AudioBuffersState buffers_state); |
80 | 72 |
81 // Close() helper function to free internal structs. | 73 // Close() helper function to free internal structs. |
82 void Reset(); | 74 void Reset(); |
83 | 75 |
84 // Configuration constants from the constructor. Referencable by all threads | 76 // AudioParameters from the constructor. |
85 // since they are constants. | 77 const AudioParameters params_; |
86 const ChannelLayout channel_layout_; | |
87 const uint32 channel_count_; | |
88 const pa_sample_format_t sample_format_; | |
89 const uint32 sample_rate_; | |
90 const uint32 bytes_per_frame_; | |
91 | 78 |
92 // Audio manager that created us. Used to report that we've closed. | 79 // Audio manager that created us. Used to report that we've closed. |
93 AudioManagerPulse* manager_; | 80 AudioManagerPulse* manager_; |
94 | 81 |
95 // PulseAudio API structs. | 82 // PulseAudio API structs. |
96 pa_context* pa_context_; | 83 pa_context* pa_context_; |
97 pa_mainloop* pa_mainloop_; | 84 pa_threaded_mainloop* pa_mainloop_; |
98 | 85 |
99 // Handle to the actual PulseAudio playback stream. | 86 // Handle to the actual PulseAudio playback stream. |
100 pa_stream* playback_handle_; | 87 pa_stream* playback_handle_; |
101 | 88 |
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. | 89 // Float representation of volume from 0.0 to 1.0. |
110 float volume_; | 90 float volume_; |
111 | 91 |
112 // Flag indicating the code should stop reading from the data source or | 92 // 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 | 93 // writing to the PulseAudio server. This is set because the device has |
114 // entered an unrecoverable error state, or the Close() has executed. | 94 // entered an unrecoverable error state, or the Close() has executed. |
115 bool stream_stopped_; | 95 bool stream_stopped_; |
116 | 96 |
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. | 97 // Callback to audio data source. |
126 AudioSourceCallback* source_callback_; | 98 AudioSourceCallback* source_callback_; |
127 | 99 |
128 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 100 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
129 scoped_ptr<AudioBus> audio_bus_; | 101 scoped_ptr<AudioBus> audio_bus_; |
102 scoped_array<uint8> interleaved_audio_data_; | |
DaleCurtis
2012/10/10 18:19:05
Looks like I can remove this too by using pa_strea
| |
103 | |
104 volatile pa_stream_state_t stream_state_; | |
105 volatile pa_context_state_t context_state_; | |
130 | 106 |
131 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | 107 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
132 }; | 108 }; |
133 | 109 |
134 } // namespace media | 110 } // namespace media |
135 | 111 |
136 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 112 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
OLD | NEW |