Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Creates an audio output stream based on the PulseAudio asynchronous API. | |
| 6 // | |
| 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 | |
| 9 // occurs. | |
| 10 // | |
| 11 // This object is designed so that all AudioOutputStream methods will be called | |
| 12 // on the same thread that created the object. | |
| 13 // | |
| 14 // WARNING: This object blocks on internal PulseAudio calls after Start() is | |
| 15 // invoked and repeatedly throughout playback as it waits for PulseAudio write | |
| 16 // callbacks to occur. | |
| 17 | |
| 18 #ifndef MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ | |
| 19 #define MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ | |
| 20 | |
| 21 #include <pulse/pulseaudio.h> | |
| 22 | |
| 23 #include <string> | |
| 24 | |
| 25 #include "base/message_loop.h" | |
|
vrk (LEFT CHROMIUM)
2011/08/16 18:19:24
nit: includes in ABC order
Well actually, just fo
slock
2011/08/17 21:43:14
Done.
| |
| 26 #include "base/memory/scoped_ptr.h" | |
| 27 #include "media/audio/audio_io.h" | |
| 28 #include "media/audio/audio_parameters.h" | |
|
vrk (LEFT CHROMIUM)
2011/08/16 18:19:24
Forward-declare AudioParameters instead of include
slock
2011/08/17 21:43:14
Done.
| |
| 29 | |
| 30 namespace media { | |
| 31 class SeekableBuffer; | |
| 32 } | |
| 33 | |
| 34 class AudioManagerLinux; | |
| 35 | |
| 36 class PulseAudioOutputStream : public AudioOutputStream { | |
| 37 public: | |
| 38 PulseAudioOutputStream(const AudioParameters& params, | |
| 39 AudioManagerLinux* manager, | |
| 40 MessageLoop* message_loop); | |
| 41 | |
| 42 virtual ~PulseAudioOutputStream(); | |
| 43 | |
| 44 // Implementation of AudioOutputStream. | |
| 45 virtual bool Open(); | |
| 46 virtual void Close(); | |
| 47 virtual void Start(AudioSourceCallback* callback); | |
| 48 virtual void Stop(); | |
| 49 virtual void SetVolume(double volume); | |
| 50 virtual void GetVolume(double* volume); | |
| 51 | |
| 52 private: | |
| 53 // PulseAudio Callbacks. | |
| 54 static void ContextStateCallback(pa_context* context, void* userdata); | |
| 55 static void WriteCallback(pa_stream* stream, size_t length, void* userdata); | |
| 56 | |
| 57 // PulseAudio mainloop iteration loop task function; used to avoid blocking | |
| 58 // the audio thread. | |
| 59 void WaitForWriteTask(); | |
| 60 | |
| 61 // Function to get another packet from the data source and write it into the | |
| 62 // client buffer. | |
| 63 void BufferPacketInClient(); | |
| 64 | |
| 65 // API for Proxying calls to the AudioSourceCallback provided during Start(). | |
| 66 uint32 RunDataCallback(uint8* dest, uint32 max_size, | |
| 67 AudioBuffersState buffers_state); | |
| 68 | |
| 69 // Close() helper function to free internal structs. | |
| 70 void Reset(); | |
| 71 | |
| 72 // Configuration constants from the constructor. Referencable by all threads | |
| 73 // since they are constants. | |
| 74 const ChannelLayout channel_layout_; | |
| 75 const uint32 channel_count_; | |
| 76 const pa_sample_format_t sample_format_; | |
| 77 const uint32 sample_rate_; | |
| 78 const uint32 bytes_per_frame_; | |
| 79 | |
| 80 // Audio manager that created us. Used to report that we've closed. | |
| 81 AudioManagerLinux* manager_; | |
| 82 | |
| 83 // PulseAudio API structs. | |
| 84 pa_context* pa_context_; | |
| 85 pa_mainloop* pa_mainloop_; | |
| 86 | |
| 87 // Handle to the actual PulseAudio playback stream. | |
| 88 pa_stream* playback_handle_; | |
| 89 | |
| 90 // Device configuration data. Populated after Open() completes. | |
| 91 uint32 packet_size_; | |
| 92 uint32 frames_per_packet_; | |
| 93 | |
| 94 // Internal buffer. | |
| 95 scoped_ptr<media::SeekableBuffer> client_buffer_; | |
| 96 bool source_exhausted_; | |
| 97 | |
| 98 // Float representation of volume from 0.0 to 1.0. | |
| 99 float volume_; | |
| 100 | |
| 101 // Flag indicating the code should stop reading from the data source or | |
| 102 // writing to the PulseAudio server. This is set because the device has | |
| 103 // entered an unrecoverable error state, or the Close() has executed. | |
| 104 bool stream_stopped_; | |
| 105 | |
| 106 // Whether or not PulseAudio has called the WriteCallback for the most recent | |
| 107 // set of pa_mainloop iterations. | |
| 108 bool write_callback_handled_; | |
| 109 | |
| 110 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on | |
| 111 // the audio thread while waiting for PulseAudio write callbacks. | |
| 112 MessageLoop* message_loop_; | |
| 113 | |
| 114 // Allows us to run tasks on the PulseAudioOutputStream instance which are | |
| 115 // bound by its lifetime. | |
| 116 ScopedRunnableMethodFactory<PulseAudioOutputStream> method_factory_; | |
| 117 | |
| 118 // Callback to audio data source. | |
| 119 AudioSourceCallback* source_callback_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | |
| 122 }; | |
| 123 | |
| 124 #endif // MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ | |
| OLD | NEW |