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 // Audio rendering unit utilizing audio output stream provided by browser | 5 // Audio rendering unit utilizing audio output stream provided by browser |
6 // process through IPC. | 6 // process through IPC. |
7 // | 7 // |
8 // Relationship of classes. | 8 // Relationship of classes. |
9 // | 9 // |
10 // AudioRendererHost AudioRendererImpl | 10 // AudioRendererHost AudioRendererImpl |
(...skipping 26 matching lines...) Expand all Loading... |
37 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 37 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
38 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 38 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
39 #pragma once | 39 #pragma once |
40 | 40 |
41 #include "base/gtest_prod_util.h" | 41 #include "base/gtest_prod_util.h" |
42 #include "base/memory/scoped_ptr.h" | 42 #include "base/memory/scoped_ptr.h" |
43 #include "base/message_loop.h" | 43 #include "base/message_loop.h" |
44 #include "base/shared_memory.h" | 44 #include "base/shared_memory.h" |
45 #include "base/synchronization/lock.h" | 45 #include "base/synchronization/lock.h" |
46 #include "base/threading/simple_thread.h" | 46 #include "base/threading/simple_thread.h" |
47 #include "content/renderer/media/audio_message_filter.h" | 47 #include "content/renderer/media/audio_device.h" |
48 #include "media/audio/audio_io.h" | 48 #include "media/audio/audio_io.h" |
49 #include "media/audio/audio_manager.h" | 49 #include "media/audio/audio_manager.h" |
50 #include "media/base/filters.h" | 50 #include "media/base/filters.h" |
51 #include "media/filters/audio_renderer_base.h" | 51 #include "media/filters/audio_renderer_base.h" |
52 | 52 |
53 class AudioMessageFilter; | 53 class AudioMessageFilter; |
54 | 54 |
55 class AudioRendererImpl | 55 class AudioRendererImpl |
56 : public media::AudioRendererBase, | 56 : public media::AudioRendererBase, |
57 public AudioMessageFilter::Delegate, | 57 public AudioSink::RenderCallback, |
58 public base::DelegateSimpleThread::Delegate, | |
59 public MessageLoop::DestructionObserver { | 58 public MessageLoop::DestructionObserver { |
60 public: | 59 public: |
61 // Methods called on Render thread ------------------------------------------ | 60 // Methods called on Render thread ------------------------------------------ |
62 explicit AudioRendererImpl(); | 61 explicit AudioRendererImpl(MessageLoop* render_loop); |
63 virtual ~AudioRendererImpl(); | 62 virtual ~AudioRendererImpl(); |
64 | 63 |
65 // Methods called on IO thread ---------------------------------------------- | |
66 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | |
67 virtual void OnRequestPacket(AudioBuffersState buffers_state); | |
68 virtual void OnStateChanged(AudioStreamState state); | |
69 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); | |
70 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | |
71 base::SyncSocket::Handle socket_handle, | |
72 uint32 length); | |
73 virtual void OnVolume(double volume); | |
74 | |
75 // Methods called on pipeline thread ---------------------------------------- | 64 // Methods called on pipeline thread ---------------------------------------- |
76 // media::Filter implementation. | 65 // media::Filter implementation. |
77 virtual void SetPlaybackRate(float rate); | 66 virtual void SetPlaybackRate(float rate); |
78 virtual void Pause(media::FilterCallback* callback); | 67 virtual void Pause(media::FilterCallback* callback); |
79 virtual void Seek(base::TimeDelta time, const media::FilterStatusCB& cb); | 68 virtual void Seek(base::TimeDelta time, const media::FilterStatusCB& cb); |
80 virtual void Play(media::FilterCallback* callback); | 69 virtual void Play(media::FilterCallback* callback); |
81 | 70 |
82 // media::AudioRenderer implementation. | 71 // media::AudioRenderer implementation. |
83 virtual void SetVolume(float volume); | 72 virtual void SetVolume(float volume); |
84 | 73 |
| 74 void SetAudioSink(AudioSink* audio_sink); |
| 75 |
85 protected: | 76 protected: |
86 // Methods called on audio renderer thread ---------------------------------- | 77 // Methods called on audio renderer thread ---------------------------------- |
87 // These methods are called from AudioRendererBase. | 78 // These methods are called from AudioRendererBase. |
88 virtual bool OnInitialize(const media::AudioDecoderConfig& config); | 79 virtual bool OnInitialize(const media::AudioDecoderConfig& config); |
89 virtual void OnStop(); | 80 virtual void OnStop(); |
90 | 81 |
91 // Called when the decoder completes a Read(). | 82 // Called when the decoder completes a Read(). |
92 virtual void ConsumeAudioSamples(scoped_refptr<media::Buffer> buffer_in); | 83 virtual void ConsumeAudioSamples(scoped_refptr<media::Buffer> buffer_in); |
93 | 84 |
| 85 |
| 86 // AudioSink::RenderCallback implementation. |
| 87 virtual void Render(const std::vector<float*>& audio_data, |
| 88 size_t number_of_frames, |
| 89 size_t audio_delay_milliseconds); |
| 90 |
94 private: | 91 private: |
95 // We are using either low- or high-latency code path. | |
96 enum LatencyType { | |
97 kUninitializedLatency = 0, | |
98 kLowLatency, | |
99 kHighLatency | |
100 }; | |
101 static LatencyType latency_type_; | |
102 | |
103 // For access to constructor and IO thread methods. | 92 // For access to constructor and IO thread methods. |
104 friend class AudioRendererImplTest; | 93 friend class AudioRendererImplTest; |
105 friend class DelegateCaller; | 94 friend class DelegateCaller; |
106 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); | 95 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); |
107 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, | 96 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, |
108 DestroyedMessageLoop_ConsumeAudioSamples); | 97 DestroyedMessageLoop_ConsumeAudioSamples); |
109 // Helper methods. | 98 // Helper methods. |
110 // Convert number of bytes to duration of time using information about the | 99 // Convert number of bytes to duration of time using information about the |
111 // number of channels, sample rate and sample bits. | 100 // number of channels, sample rate and sample bits. |
112 base::TimeDelta ConvertToDuration(int bytes); | 101 base::TimeDelta ConvertToDuration(int bytes); |
113 | 102 |
114 // Methods call on IO thread ------------------------------------------------ | 103 // Methods call on IO thread ------------------------------------------------ |
115 // The following methods are tasks posted on the IO thread that needs to | 104 // The following methods are tasks posted on the IO thread that needs to |
116 // be executed on that thread. They interact with AudioMessageFilter and | 105 // be executed on that thread. They interact with AudioMessageFilter and |
117 // sends IPC messages on that thread. | 106 // sends IPC messages on that thread. |
118 void CreateStreamTask(const AudioParameters& params); | 107 void InitializeTask(const AudioParameters& params); |
119 void PlayTask(); | 108 void PlayTask(); |
120 void PauseTask(); | 109 void PauseTask(); |
121 void SeekTask(); | 110 void SeekTask(); |
122 void SetVolumeTask(double volume); | 111 void SetVolumeTask(double volume); |
123 void NotifyPacketReadyTask(); | 112 void NotifyPacketReadyTask(); |
124 void DestroyTask(); | 113 void DestroyTask(); |
125 | 114 |
126 // Called on IO thread when message loop is dying. | 115 // Called on IO thread when message loop is dying. |
127 virtual void WillDestroyCurrentMessageLoop(); | 116 virtual void WillDestroyCurrentMessageLoop(); |
128 | 117 |
129 // DelegateSimpleThread::Delegate implementation. | |
130 virtual void Run(); | |
131 | |
132 // (Re-)starts playback. | |
133 void NotifyDataAvailableIfNecessary(); | |
134 | |
135 // Creates socket. Virtual so tests can override. | |
136 virtual void CreateSocket(base::SyncSocket::Handle socket_handle); | |
137 | |
138 // Launching audio thread. Virtual so tests can override. | |
139 virtual void CreateAudioThread(); | |
140 | |
141 // Accessors used by tests. | |
142 LatencyType latency_type() { | |
143 return latency_type_; | |
144 } | |
145 | |
146 // Should be called before any class instance is created. | |
147 static void set_latency_type(LatencyType latency_type); | |
148 | |
149 // Helper method for IPC send calls. | |
150 void Send(IPC::Message* message); | |
151 | |
152 // Used to calculate audio delay given bytes. | 118 // Used to calculate audio delay given bytes. |
153 uint32 bytes_per_second_; | 119 uint32 bytes_per_second_; |
154 | 120 |
155 // ID of the stream created in the browser process. | |
156 int32 stream_id_; | |
157 | |
158 // Memory shared by the browser process for audio buffer. | |
159 scoped_ptr<base::SharedMemory> shared_memory_; | |
160 uint32 shared_memory_size_; | |
161 | |
162 // Cached audio message filter (lives on the main render thread). | |
163 scoped_refptr<AudioMessageFilter> filter_; | |
164 | |
165 // Low latency IPC stuff. | |
166 scoped_ptr<base::SyncSocket> socket_; | |
167 | |
168 // That thread waits for audio input. | 121 // That thread waits for audio input. |
169 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 122 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
170 | 123 |
171 // Protects: | 124 // Protects: |
172 // - |stopped_| | 125 // - |stopped_| |
173 // - |pending_request_| | 126 // - |pending_request_| |
174 // - |request_buffers_state_| | 127 // - |request_buffers_state_| |
175 base::Lock lock_; | 128 base::Lock lock_; |
176 | 129 |
177 // A flag that indicates this filter is called to stop. | 130 // A flag that indicates this filter is called to stop. |
178 bool stopped_; | 131 bool stopped_; |
179 | 132 |
180 // A flag that indicates an outstanding packet request. | 133 // A flag that indicates an outstanding packet request. |
181 bool pending_request_; | 134 bool pending_request_; |
182 | 135 |
183 // State of the audio buffers at time of the last request. | 136 // State of the audio buffers at time of the last request. |
184 AudioBuffersState request_buffers_state_; | 137 AudioBuffersState request_buffers_state_; |
185 | 138 |
186 // State variables for prerolling. | 139 // State variables for prerolling. |
187 bool prerolling_; | 140 bool prerolling_; |
188 | 141 |
189 // Remaining bytes for prerolling to complete. | 142 // Remaining bytes for prerolling to complete. |
190 uint32 preroll_bytes_; | 143 uint32 preroll_bytes_; |
191 | 144 |
| 145 scoped_refptr<AudioSink> audio_device_; |
| 146 MessageLoop* render_loop_; |
| 147 int bytes_per_frame_; |
| 148 |
192 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 149 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
193 }; | 150 }; |
194 | 151 |
195 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 152 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
OLD | NEW |