OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // | 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. |
16 // inside PulseAudio in Start() and repeated during playback, waiting for | |
17 // PulseAudio write callbacks to occur. | |
18 | 16 |
19 #ifndef MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ | 17 #ifndef MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ |
20 #define MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ | 18 #define MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ |
21 | 19 |
20 #include <pulse/glib-mainloop.h> | |
22 #include <pulse/pulseaudio.h> | 21 #include <pulse/pulseaudio.h> |
23 | 22 |
24 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
25 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
26 #include "base/task.h" | 25 #include "base/task.h" |
27 #include "media/audio/audio_io.h" | 26 #include "media/audio/audio_io.h" |
28 #include "media/base/channel_layout.h" | 27 #include "media/base/channel_layout.h" |
29 | 28 |
30 namespace media { | 29 namespace media { |
31 class SeekableBuffer; | 30 class SeekableBuffer; |
(...skipping 14 matching lines...) Expand all Loading... | |
46 // Implementation of AudioOutputStream. | 45 // Implementation of AudioOutputStream. |
47 virtual bool Open(); | 46 virtual bool Open(); |
48 virtual void Close(); | 47 virtual void Close(); |
49 virtual void Start(AudioSourceCallback* callback); | 48 virtual void Start(AudioSourceCallback* callback); |
50 virtual void Stop(); | 49 virtual void Stop(); |
51 virtual void SetVolume(double volume); | 50 virtual void SetVolume(double volume); |
52 virtual void GetVolume(double* volume); | 51 virtual void GetVolume(double* volume); |
53 | 52 |
54 private: | 53 private: |
55 // PulseAudio Callbacks. | 54 // PulseAudio Callbacks. |
56 static void ContextStateCallback(pa_context* context, void* state_addr); | 55 static void ContextStateCallback(pa_context* context, void* p_this); |
tommi (sloooow) - chröme
2011/11/08 10:51:36
p_this -> this_ptr
Actually, the documentation fo
no longer working on chromium
2011/11/09 12:57:33
Done.
| |
57 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, | 56 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, |
58 void* stream_addr); | 57 void* p_this); |
tommi (sloooow) - chröme
2011/11/08 10:51:36
same here
no longer working on chromium
2011/11/09 12:57:33
Done.
| |
59 | 58 |
60 // Iterate the PulseAudio mainloop to get write requests. | 59 // Fulfills a write request from the write request callback. If the write |
61 void WaitForWriteRequest(); | |
62 | |
63 // Get another packet from the data source and write it to the client buffer. | |
64 bool BufferPacketFromSource(); | |
65 | |
66 // Fulfill a write request from the write request callback. If the write | |
67 // can't be finished a first, post a new attempt to the message loop. | 60 // can't be finished a first, post a new attempt to the message loop. |
68 void FulfillWriteRequest(size_t requested_bytes); | 61 void FulfillWriteRequest(size_t requested_bytes); |
69 | 62 |
70 // Write data from the client buffer to the PulseAudio stream. | |
71 void WriteToStream(size_t bytes_to_write, size_t* bytes_written); | |
72 | |
73 // API for Proxying calls to the AudioSourceCallback provided during Start(). | |
74 uint32 RunDataCallback(uint8* dest, uint32 max_size, | |
75 AudioBuffersState buffers_state); | |
76 | |
77 // Close() helper function to free internal structs. | 63 // Close() helper function to free internal structs. |
78 void Reset(); | 64 void Reset(); |
79 | 65 |
80 // Configuration constants from the constructor. Referencable by all threads | 66 // Configuration constants from the constructor. Referencable by all threads |
81 // since they are constants. | 67 // since they are constants. |
82 const ChannelLayout channel_layout_; | 68 const uint32 channels_; |
83 const uint32 channel_count_; | |
84 const pa_sample_format_t sample_format_; | 69 const pa_sample_format_t sample_format_; |
85 const uint32 sample_rate_; | 70 const uint32 sample_rate_; |
86 const uint32 bytes_per_frame_; | 71 const uint32 bytes_per_frame_; |
72 const uint32 packet_size_; | |
73 const uint32 frames_per_packet_; | |
87 | 74 |
88 // Audio manager that created us. Used to report that we've closed. | 75 // Audio manager that created us. Used to report that we've closed. |
89 AudioManagerLinux* manager_; | 76 AudioManagerLinux* manager_; |
90 | 77 |
91 // PulseAudio API structs. | 78 // PulseAudio API structs. |
92 pa_context* pa_context_; | 79 pa_context* pa_context_; |
93 pa_mainloop* pa_mainloop_; | 80 pa_glib_mainloop* pa_glib_mainloop_; |
94 | 81 |
95 // Handle to the actual PulseAudio playback stream. | 82 // Handle to the actual PulseAudio playback stream. |
96 pa_stream* playback_handle_; | 83 pa_stream* playback_handle_; |
97 | 84 |
98 // Device configuration data. Populated after Open() completes. | 85 // Device configuration data. Populated after Open() completes. |
99 uint32 packet_size_; | 86 uint32 pa_buffer_size_; |
100 uint32 frames_per_packet_; | |
101 | 87 |
102 // Client side audio buffer feeding pulse audio's server side buffer. | 88 // Client side audio buffer feeding pulse audio's server side buffer. |
103 scoped_ptr<media::SeekableBuffer> client_buffer_; | 89 scoped_ptr<media::SeekableBuffer> buffer_; |
104 | 90 |
105 // Float representation of volume from 0.0 to 1.0. | 91 // Float representation of volume from 0.0 to 1.0. |
106 float volume_; | 92 float volume_; |
107 | 93 |
108 // Flag indicating the code should stop reading from the data source or | 94 // Flag indicating the code should stop reading from the data source or |
109 // writing to the PulseAudio server. This is set because the device has | 95 // writing to the PulseAudio server. This is set because the device has |
110 // entered an unrecoverable error state, or the Close() has executed. | 96 // entered an unrecoverable error state, or the Close() has executed. |
111 bool stream_stopped_; | 97 bool stream_stopped_; |
112 | 98 |
113 // Whether or not PulseAudio has called the WriteCallback for the most recent | 99 // Flag indicating the state of the context has been changed. |
114 // set of pa_mainloop iterations. | 100 bool context_state_changed_; |
115 bool write_callback_handled_; | |
116 | 101 |
117 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on | 102 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on |
118 // the audio thread while waiting for PulseAudio write callbacks. | 103 // the audio thread while waiting for PulseAudio write callbacks. |
119 MessageLoop* message_loop_; | 104 MessageLoop* message_loop_; |
120 | 105 |
121 // Allows us to run tasks on the PulseAudioOutputStream instance which are | 106 // Allows us to run tasks on the PulseAudioOutputStream instance which are |
122 // bound by its lifetime. | 107 // bound by its lifetime. |
123 base::WeakPtrFactory<PulseAudioOutputStream> weak_factory_; | 108 base::WeakPtrFactory<PulseAudioOutputStream> weak_factory_; |
124 | 109 |
125 // Callback to audio data source. | 110 // Callback to audio data source. |
126 AudioSourceCallback* source_callback_; | 111 AudioSourceCallback* source_callback_; |
127 | 112 |
128 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | 113 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
129 }; | 114 }; |
130 | 115 |
131 #endif // MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ | 116 #endif // MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ |
OLD | NEW |