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 | |
|
vrk (LEFT CHROMIUM)
2011/08/18 18:22:22
Also blocks on Open(), waiting for PulseAudio to b
slock
2011/08/18 18:54:16
Done.
| |
| 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 "base/memory/scoped_ptr.h" | |
| 24 #include "base/task.h" | |
| 25 #include "media/audio/audio_io.h" | |
| 26 #include "media/base/channel_layout.h" | |
| 27 | |
| 28 namespace media { | |
| 29 class SeekableBuffer; | |
| 30 } | |
| 31 | |
| 32 class AudioManagerLinux; | |
| 33 struct AudioParameters; | |
| 34 class MessageLoop; | |
| 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* state_addr); | |
| 55 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, | |
| 56 void* stream_addr); | |
| 57 | |
| 58 // Iterate the PulseAudio mainloop to get write requests. | |
| 59 void WaitForWriteRequest(); | |
| 60 | |
| 61 // Get another packet from the data source and write it to the client buffer. | |
| 62 bool BufferPacketFromSource(); | |
| 63 | |
| 64 // Fulfill a write request from the write request callback. If the write | |
| 65 // can't be finished a first, post a new attempt to the message loop. | |
| 66 void FulfillWriteRequest(size_t requested_bytes); | |
| 67 | |
| 68 // Write data from the client buffer to the PulseAudio stream. | |
| 69 void WriteToStream(size_t bytes_to_write, size_t* bytes_written); | |
| 70 | |
| 71 // API for Proxying calls to the AudioSourceCallback provided during Start(). | |
| 72 uint32 RunDataCallback(uint8* dest, uint32 max_size, | |
| 73 AudioBuffersState buffers_state); | |
| 74 | |
| 75 // Close() helper function to free internal structs. | |
| 76 void Reset(); | |
| 77 | |
| 78 // Configuration constants from the constructor. Referencable by all threads | |
| 79 // since they are constants. | |
| 80 const ChannelLayout channel_layout_; | |
| 81 const uint32 channel_count_; | |
| 82 const pa_sample_format_t sample_format_; | |
| 83 const uint32 sample_rate_; | |
| 84 const uint32 bytes_per_frame_; | |
| 85 | |
| 86 // Audio manager that created us. Used to report that we've closed. | |
| 87 AudioManagerLinux* manager_; | |
| 88 | |
| 89 // PulseAudio API structs. | |
| 90 pa_context* pa_context_; | |
| 91 pa_mainloop* pa_mainloop_; | |
| 92 | |
| 93 // Handle to the actual PulseAudio playback stream. | |
| 94 pa_stream* playback_handle_; | |
| 95 | |
| 96 // Device configuration data. Populated after Open() completes. | |
| 97 uint32 packet_size_; | |
| 98 uint32 frames_per_packet_; | |
| 99 | |
| 100 // Client side audio buffer feeding pulse audio's server side buffer. | |
| 101 scoped_ptr<media::SeekableBuffer> client_buffer_; | |
| 102 | |
| 103 // Float representation of volume from 0.0 to 1.0. | |
| 104 float volume_; | |
| 105 | |
| 106 // Flag indicating the code should stop reading from the data source or | |
| 107 // writing to the PulseAudio server. This is set because the device has | |
| 108 // entered an unrecoverable error state, or the Close() has executed. | |
| 109 bool stream_stopped_; | |
| 110 | |
| 111 // Whether or not PulseAudio has called the WriteCallback for the most recent | |
| 112 // set of pa_mainloop iterations. | |
| 113 bool write_callback_handled_; | |
| 114 | |
| 115 // Message loop used to post WaitForWriteTasks. Used to prevent blocking on | |
| 116 // the audio thread while waiting for PulseAudio write callbacks. | |
| 117 MessageLoop* message_loop_; | |
| 118 | |
| 119 // Allows us to run tasks on the PulseAudioOutputStream instance which are | |
| 120 // bound by its lifetime. | |
| 121 ScopedRunnableMethodFactory<PulseAudioOutputStream> method_factory_; | |
| 122 | |
| 123 // Callback to audio data source. | |
| 124 AudioSourceCallback* source_callback_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | |
| 127 }; | |
| 128 | |
| 129 #endif // MEDIA_AUDIO_LINUX_PULSE_OUTPUT_H_ | |
| OLD | NEW |