Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: media/audio/pulse/pulse_output.h

Issue 11098031: Get PulseAudio implementation working. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/audio/audio_io.h ('k') | media/audio/pulse/pulse_output.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/memory/scoped_ptr.h"
24 #include "media/audio/audio_io.h"
25 #include "media/audio/audio_parameters.h"
23 26
24 #include "base/memory/scoped_ptr.h" 27 struct pa_context;
25 #include "base/memory/weak_ptr.h" 28 struct pa_operation;
26 #include "media/audio/audio_io.h" 29 struct pa_stream;
27 #include "media/base/channel_layout.h" 30 struct pa_threaded_mainloop;
28 31
29 namespace media { 32 namespace media {
30 33 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 34
44 class PulseAudioOutputStream : public AudioOutputStream { 35 class PulseAudioOutputStream : public AudioOutputStream {
45 public: 36 public:
46 PulseAudioOutputStream(const AudioParameters& params, 37 PulseAudioOutputStream(const AudioParameters& params,
47 AudioManagerPulse* manager); 38 AudioManagerBase* manager);
48 39
49 virtual ~PulseAudioOutputStream(); 40 virtual ~PulseAudioOutputStream();
50 41
51 // Implementation of AudioOutputStream. 42 // Implementation of AudioOutputStream.
52 virtual bool Open() OVERRIDE; 43 virtual bool Open() OVERRIDE;
53 virtual void Close() OVERRIDE; 44 virtual void Close() OVERRIDE;
54 virtual void Start(AudioSourceCallback* callback) OVERRIDE; 45 virtual void Start(AudioSourceCallback* callback) OVERRIDE;
55 virtual void Stop() OVERRIDE; 46 virtual void Stop() OVERRIDE;
56 virtual void SetVolume(double volume) OVERRIDE; 47 virtual void SetVolume(double volume) OVERRIDE;
57 virtual void GetVolume(double* volume) OVERRIDE; 48 virtual void GetVolume(double* volume) OVERRIDE;
58 49
59 private: 50 private:
60 // PulseAudio Callbacks. 51 // Called by PulseAudio when |pa_context_| and |pa_stream_| change state. If
61 static void ContextStateCallback(pa_context* context, void* state_addr); 52 // an unexpected failure state change happens and |source_callback_| is set
62 static void WriteRequestCallback(pa_stream* playback_handle, size_t length, 53 // these methods will forward the error via OnError().
63 void* stream_addr); 54 static void ContextNotifyCallback(pa_context* c, void* p_this);
55 static void StreamNotifyCallback(pa_stream* s, void* p_this);
64 56
65 // Iterate the PulseAudio mainloop to get write requests. 57 // Triggers pa_threaded_mainloop_signal() to avoid deadlocks.
66 void WaitForWriteRequest(); 58 static void StreamSuccessCallback(pa_stream* s, int success, void* p_this);
67 59
68 // Get another packet from the data source and write it to the client buffer. 60 // Called by PulseAudio when it needs more audio data.
69 bool BufferPacketFromSource(); 61 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this);
70 62
71 // Fulfill a write request from the write request callback. If the write 63 // Fulfill a write request from the write request callback. Outputs silence
72 // can't be finished a first, post a new attempt to the message loop. 64 // if the request could not be fulfilled.
73 void FulfillWriteRequest(size_t requested_bytes); 65 void FulfillWriteRequest(size_t requested_bytes);
74 66
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. 67 // Close() helper function to free internal structs.
82 void Reset(); 68 void Reset();
83 69
84 // Configuration constants from the constructor. Referencable by all threads 70 // Returns the current hardware latency value in bytes.
85 // since they are constants. 71 int GetHardwareLatencyInBytes();
86 const ChannelLayout channel_layout_; 72
87 const uint32 channel_count_; 73 // Helper method for waiting on Pulse Audio operations to complete.
88 const pa_sample_format_t sample_format_; 74 void WaitForPulseOperation(pa_operation* op);
89 const uint32 sample_rate_; 75
90 const uint32 bytes_per_frame_; 76 // AudioParameters from the constructor.
77 const AudioParameters params_;
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 AudioManagerBase* 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 pa_stream* pa_stream_;
99 // Handle to the actual PulseAudio playback stream.
100 pa_stream* playback_handle_;
101
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 86
109 // Float representation of volume from 0.0 to 1.0. 87 // Float representation of volume from 0.0 to 1.0.
110 float volume_; 88 float volume_;
111 89
112 // Flag indicating the code should stop reading from the data source or 90 // Callback to audio data source. Must only be modified while holding a lock
113 // writing to the PulseAudio server. This is set because the device has 91 // on |pa_mainloop_| via pa_threaded_mainloop_lock().
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.
126 AudioSourceCallback* source_callback_; 92 AudioSourceCallback* source_callback_;
127 93
128 // Container for retrieving data from AudioSourceCallback::OnMoreData(). 94 // Container for retrieving data from AudioSourceCallback::OnMoreData().
129 scoped_ptr<AudioBus> audio_bus_; 95 scoped_ptr<AudioBus> audio_bus_;
130 96
131 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); 97 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream);
132 }; 98 };
133 99
134 } // namespace media 100 } // namespace media
135 101
136 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ 102 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_
OLDNEW
« no previous file with comments | « media/audio/audio_io.h ('k') | media/audio/pulse/pulse_output.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698