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; |
6 // specifically using the pa_threaded_mainloop model. | 6 // specifically using the pa_threaded_mainloop model. |
7 // | 7 // |
8 // 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 |
9 // stream is deleted as Close() is responsible for ensuring resource cleanup | 9 // stream is deleted as Close() is responsible for ensuring resource cleanup |
10 // occurs. | 10 // occurs. |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "media/audio/audio_io.h" | 24 #include "media/audio/audio_io.h" |
25 #include "media/audio/audio_parameters.h" | 25 #include "media/audio/audio_parameters.h" |
26 | 26 |
27 struct pa_context; | 27 struct pa_context; |
28 struct pa_operation; | 28 struct pa_operation; |
29 struct pa_stream; | 29 struct pa_stream; |
30 struct pa_threaded_mainloop; | 30 struct pa_threaded_mainloop; |
31 | 31 |
32 namespace media { | 32 namespace media { |
33 class AudioManagerBase; | 33 class AudioManagerBase; |
| 34 class PulseWrapper; |
34 | 35 |
35 class PulseAudioOutputStream : public AudioOutputStream { | 36 class PulseAudioOutputStream : public AudioOutputStream { |
36 public: | 37 public: |
37 PulseAudioOutputStream(const AudioParameters& params, | 38 PulseAudioOutputStream(PulseWrapper* wrapper, |
| 39 const AudioParameters& params, |
38 AudioManagerBase* manager); | 40 AudioManagerBase* manager); |
39 | 41 |
40 virtual ~PulseAudioOutputStream(); | 42 virtual ~PulseAudioOutputStream(); |
41 | 43 |
42 // Implementation of AudioOutputStream. | 44 // Implementation of AudioOutputStream. |
43 virtual bool Open() OVERRIDE; | 45 virtual bool Open() OVERRIDE; |
44 virtual void Close() OVERRIDE; | 46 virtual void Close() OVERRIDE; |
45 virtual void Start(AudioSourceCallback* callback) OVERRIDE; | 47 virtual void Start(AudioSourceCallback* callback) OVERRIDE; |
46 virtual void Stop() OVERRIDE; | 48 virtual void Stop() OVERRIDE; |
47 virtual void SetVolume(double volume) OVERRIDE; | 49 virtual void SetVolume(double volume) OVERRIDE; |
48 virtual void GetVolume(double* volume) OVERRIDE; | 50 virtual void GetVolume(double* volume) OVERRIDE; |
49 | 51 |
50 private: | 52 private: |
51 // Called by PulseAudio when |pa_context_| and |pa_stream_| change state. If | 53 // Called by PulseAudio when |pa_context_| and |pa_stream_| change state. If |
52 // an unexpected failure state change happens and |source_callback_| is set | 54 // an unexpected failure state change happens and |source_callback_| is set |
53 // these methods will forward the error via OnError(). | 55 // these methods will forward the error via OnError(). |
54 static void ContextNotifyCallback(pa_context* c, void* p_this); | 56 static void ContextNotifyCallback(pa_context* c, void* p_this); |
55 static void StreamNotifyCallback(pa_stream* s, void* p_this); | 57 static void StreamNotifyCallback(pa_stream* s, void* p_this); |
56 | 58 |
57 // Triggers pa_threaded_mainloop_signal() to avoid deadlocks. | 59 // Triggers pa_threaded_mainloop_signal() to avoid deadlocks. |
58 static void StreamSuccessCallback(pa_stream* s, int success, void* p_this); | 60 static void StreamSuccessCallback(pa_stream* s, int error, void* p_this); |
59 | 61 |
60 // Called by PulseAudio when it needs more audio data. | 62 // Called by PulseAudio when it needs more audio data. |
61 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this); | 63 static void StreamRequestCallback(pa_stream* s, size_t len, void* p_this); |
62 | 64 |
63 // Fulfill a write request from the write request callback. Outputs silence | 65 // Fulfill a write request from the write request callback. Outputs silence |
64 // if the request could not be fulfilled. | 66 // if the request could not be fulfilled. |
65 void FulfillWriteRequest(size_t requested_bytes); | 67 void FulfillWriteRequest(size_t requested_bytes); |
66 | 68 |
67 // Close() helper function to free internal structs. | 69 // Close() helper function to free internal structs. |
68 void Reset(); | 70 void Reset(); |
69 | 71 |
70 // Returns the current hardware latency value in bytes. | 72 // Wrapper class to invoke all the Pulse functions. |
71 int GetHardwareLatencyInBytes(); | 73 PulseWrapper* wrapper_; |
72 | |
73 // Helper method for waiting on Pulse Audio operations to complete. | |
74 void WaitForPulseOperation(pa_operation* op); | |
75 | 74 |
76 // AudioParameters from the constructor. | 75 // AudioParameters from the constructor. |
77 const AudioParameters params_; | 76 const AudioParameters params_; |
78 | 77 |
79 // Audio manager that created us. Used to report that we've closed. | 78 // Audio manager that created us. Used to report that we've closed. |
80 AudioManagerBase* manager_; | 79 AudioManagerBase* manager_; |
81 | 80 |
82 // PulseAudio API structs. | 81 // PulseAudio API structs. |
83 pa_context* pa_context_; | 82 pa_context* pa_context_; |
84 pa_threaded_mainloop* pa_mainloop_; | 83 pa_threaded_mainloop* pa_mainloop_; |
85 pa_stream* pa_stream_; | 84 pa_stream* pa_stream_; |
86 | 85 |
87 // Float representation of volume from 0.0 to 1.0. | 86 // Float representation of volume from 0.0 to 1.0. |
88 float volume_; | 87 float volume_; |
89 | 88 |
90 // Callback to audio data source. Must only be modified while holding a lock | 89 // Callback to audio data source. Must only be modified while holding a lock |
91 // on |pa_mainloop_| via pa_threaded_mainloop_lock(). | 90 // on |pa_mainloop_| via pa_threaded_mainloop_lock(). |
92 AudioSourceCallback* source_callback_; | 91 AudioSourceCallback* source_callback_; |
93 | 92 |
94 // Container for retrieving data from AudioSourceCallback::OnMoreData(). | 93 // Container for retrieving data from AudioSourceCallback::OnMoreData(). |
95 scoped_ptr<AudioBus> audio_bus_; | 94 scoped_ptr<AudioBus> audio_bus_; |
96 | 95 |
97 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); | 96 DISALLOW_COPY_AND_ASSIGN(PulseAudioOutputStream); |
98 }; | 97 }; |
99 | 98 |
100 } // namespace media | 99 } // namespace media |
101 | 100 |
102 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ | 101 #endif // MEDIA_AUDIO_PULSE_PULSE_OUTPUT_H_ |
OLD | NEW |