| 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 #include "media/filters/audio_renderer_sink.h" |
| 52 | 53 |
| 53 class AudioMessageFilter; | 54 class AudioMessageFilter; |
| 54 | 55 |
| 55 class AudioRendererImpl | 56 class AudioRendererImpl |
| 56 : public media::AudioRendererBase, | 57 : public media::AudioRendererBase, |
| 57 public AudioMessageFilter::Delegate, | 58 public media::AudioRendererSink::RenderCallback, |
| 58 public base::DelegateSimpleThread::Delegate, | |
| 59 public MessageLoop::DestructionObserver { | 59 public MessageLoop::DestructionObserver { |
| 60 public: | 60 public: |
| 61 // Methods called on Render thread ------------------------------------------ | 61 // Methods called on Render thread ------------------------------------------ |
| 62 explicit AudioRendererImpl(); | 62 explicit AudioRendererImpl(MessageLoop* render_loop); |
| 63 virtual ~AudioRendererImpl(); | 63 virtual ~AudioRendererImpl(); |
| 64 | 64 |
| 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 ---------------------------------------- | 65 // Methods called on pipeline thread ---------------------------------------- |
| 76 // media::Filter implementation. | 66 // media::Filter implementation. |
| 77 virtual void SetPlaybackRate(float rate); | 67 virtual void SetPlaybackRate(float rate); |
| 78 virtual void Pause(media::FilterCallback* callback); | 68 virtual void Pause(media::FilterCallback* callback); |
| 79 virtual void Seek(base::TimeDelta time, const media::FilterStatusCB& cb); | 69 virtual void Seek(base::TimeDelta time, const media::FilterStatusCB& cb); |
| 80 virtual void Play(media::FilterCallback* callback); | 70 virtual void Play(media::FilterCallback* callback); |
| 81 | 71 |
| 82 // media::AudioRenderer implementation. | 72 // media::AudioRenderer implementation. |
| 83 virtual void SetVolume(float volume); | 73 virtual void SetVolume(float volume); |
| 84 | 74 |
| 75 // AudioRendererSink::RenderCallback implementation. |
| 76 virtual void Render(const std::vector<float*>& audio_data, |
| 77 size_t number_of_frames, |
| 78 size_t audio_delay_milliseconds); |
| 79 virtual void SetAudioRendererSink(media::AudioRendererSink* audio_sink); |
| 80 |
| 85 protected: | 81 protected: |
| 86 // Methods called on audio renderer thread ---------------------------------- | 82 // Methods called on audio renderer thread ---------------------------------- |
| 87 // These methods are called from AudioRendererBase. | 83 // These methods are called from AudioRendererBase. |
| 88 virtual bool OnInitialize(const media::AudioDecoderConfig& config); | 84 virtual bool OnInitialize(const media::AudioDecoderConfig& config); |
| 89 virtual void OnStop(); | 85 virtual void OnStop(); |
| 90 | 86 |
| 91 // Called when the decoder completes a Read(). | 87 // Called when the decoder completes a Read(). |
| 92 virtual void ConsumeAudioSamples(scoped_refptr<media::Buffer> buffer_in); | 88 virtual void ConsumeAudioSamples(scoped_refptr<media::Buffer> buffer_in); |
| 93 | 89 |
| 94 private: | 90 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. | 91 // For access to constructor and IO thread methods. |
| 104 friend class AudioRendererImplTest; | 92 friend class AudioRendererImplTest; |
| 105 friend class DelegateCaller; | 93 friend class DelegateCaller; |
| 106 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); | 94 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); |
| 107 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, | 95 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, |
| 108 DestroyedMessageLoop_ConsumeAudioSamples); | 96 DestroyedMessageLoop_ConsumeAudioSamples); |
| 109 // Helper methods. | 97 // Helper methods. |
| 110 // Convert number of bytes to duration of time using information about the | 98 // Convert number of bytes to duration of time using information about the |
| 111 // number of channels, sample rate and sample bits. | 99 // number of channels, sample rate and sample bits. |
| 112 base::TimeDelta ConvertToDuration(int bytes); | 100 base::TimeDelta ConvertToDuration(int bytes); |
| 113 | 101 |
| 114 // Methods call on IO thread ------------------------------------------------ | 102 // Methods call on IO thread ------------------------------------------------ |
| 115 // The following methods are tasks posted on the IO thread that needs to | 103 // The following methods are tasks posted on the IO thread that needs to |
| 116 // be executed on that thread. They interact with AudioMessageFilter and | 104 // be executed on that thread. They interact with AudioMessageFilter and |
| 117 // sends IPC messages on that thread. | 105 // sends IPC messages on that thread. |
| 118 void CreateStreamTask(const AudioParameters& params); | 106 void InitializeTask(const AudioParameters& params); |
| 119 void PlayTask(); | 107 void PlayTask(); |
| 120 void PauseTask(); | 108 void PauseTask(); |
| 121 void SeekTask(); | 109 void SeekTask(); |
| 122 void SetVolumeTask(double volume); | 110 void SetVolumeTask(double volume); |
| 123 void NotifyPacketReadyTask(); | 111 void NotifyPacketReadyTask(); |
| 124 void DestroyTask(); | 112 void DestroyTask(); |
| 125 | 113 |
| 126 // Called on IO thread when message loop is dying. | 114 // Called on IO thread when message loop is dying. |
| 127 virtual void WillDestroyCurrentMessageLoop(); | 115 virtual void WillDestroyCurrentMessageLoop(); |
| 128 | 116 |
| 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. | 117 // Used to calculate audio delay given bytes. |
| 153 uint32 bytes_per_second_; | 118 uint32 bytes_per_second_; |
| 154 | 119 |
| 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. | 120 // That thread waits for audio input. |
| 169 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 121 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
| 170 | 122 |
| 171 // Protects: | 123 // Protects: |
| 172 // - |stopped_| | 124 // - |stopped_| |
| 173 // - |pending_request_| | 125 // - |pending_request_| |
| 174 // - |request_buffers_state_| | 126 // - |request_buffers_state_| |
| 175 base::Lock lock_; | 127 base::Lock lock_; |
| 176 | 128 |
| 177 // A flag that indicates this filter is called to stop. | 129 // A flag that indicates this filter is called to stop. |
| 178 bool stopped_; | 130 bool stopped_; |
| 179 | 131 |
| 180 // A flag that indicates an outstanding packet request. | 132 // A flag that indicates an outstanding packet request. |
| 181 bool pending_request_; | 133 bool pending_request_; |
| 182 | 134 |
| 183 // State of the audio buffers at time of the last request. | 135 // State of the audio buffers at time of the last request. |
| 184 AudioBuffersState request_buffers_state_; | 136 AudioBuffersState request_buffers_state_; |
| 185 | 137 |
| 186 // State variables for prerolling. | 138 // State variables for prerolling. |
| 187 bool prerolling_; | 139 bool prerolling_; |
| 188 | 140 |
| 189 // Remaining bytes for prerolling to complete. | 141 // Remaining bytes for prerolling to complete. |
| 190 uint32 preroll_bytes_; | 142 uint32 preroll_bytes_; |
| 191 | 143 |
| 144 media::AudioRendererSink* audio_device_; |
| 145 MessageLoop* render_loop_; |
| 146 int bytes_per_frame_; |
| 147 |
| 192 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 148 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
| 193 }; | 149 }; |
| 194 | 150 |
| 195 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 151 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
| OLD | NEW |