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 25 matching lines...) Expand all Loading... |
36 | 36 |
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 "content/renderer/media/audio_message_filter.h" | 47 #include "content/renderer/media/audio_message_filter.h" |
47 #include "media/audio/audio_io.h" | 48 #include "media/audio/audio_io.h" |
48 #include "media/audio/audio_manager.h" | 49 #include "media/audio/audio_manager.h" |
49 #include "media/base/filters.h" | 50 #include "media/base/filters.h" |
50 #include "media/filters/audio_renderer_base.h" | 51 #include "media/filters/audio_renderer_base.h" |
51 | 52 |
52 class AudioMessageFilter; | 53 class AudioMessageFilter; |
53 | 54 |
54 class AudioRendererImpl : public media::AudioRendererBase, | 55 class AudioRendererImpl : public media::AudioRendererBase, |
55 public AudioMessageFilter::Delegate, | 56 public AudioMessageFilter::Delegate, |
| 57 public base::DelegateSimpleThread::Delegate, |
56 public MessageLoop::DestructionObserver { | 58 public MessageLoop::DestructionObserver { |
57 public: | 59 public: |
58 // Methods called on Render thread ------------------------------------------ | 60 // Methods called on Render thread ------------------------------------------ |
59 explicit AudioRendererImpl(AudioMessageFilter* filter); | 61 explicit AudioRendererImpl(AudioMessageFilter* filter); |
60 virtual ~AudioRendererImpl(); | 62 virtual ~AudioRendererImpl(); |
61 | 63 |
62 // Methods called on IO thread ---------------------------------------------- | 64 // Methods called on IO thread ---------------------------------------------- |
63 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | 65 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. |
64 virtual void OnRequestPacket(AudioBuffersState buffers_state); | 66 virtual void OnRequestPacket(AudioBuffersState buffers_state); |
65 virtual void OnStateChanged(AudioStreamState state); | 67 virtual void OnStateChanged(AudioStreamState state); |
(...skipping 16 matching lines...) Expand all Loading... |
82 protected: | 84 protected: |
83 // Methods called on audio renderer thread ---------------------------------- | 85 // Methods called on audio renderer thread ---------------------------------- |
84 // These methods are called from AudioRendererBase. | 86 // These methods are called from AudioRendererBase. |
85 virtual bool OnInitialize(const media::AudioDecoderConfig& config); | 87 virtual bool OnInitialize(const media::AudioDecoderConfig& config); |
86 virtual void OnStop(); | 88 virtual void OnStop(); |
87 | 89 |
88 // Called when the decoder completes a Read(). | 90 // Called when the decoder completes a Read(). |
89 virtual void ConsumeAudioSamples(scoped_refptr<media::Buffer> buffer_in); | 91 virtual void ConsumeAudioSamples(scoped_refptr<media::Buffer> buffer_in); |
90 | 92 |
91 private: | 93 private: |
| 94 // We are using either low- or high-latency code path. |
| 95 enum LatencyType { |
| 96 kUninitializedLatency = 0, |
| 97 kLowLatency, |
| 98 kHighLatency |
| 99 }; |
| 100 static LatencyType latency_type_; |
| 101 |
92 // For access to constructor and IO thread methods. | 102 // For access to constructor and IO thread methods. |
93 friend class AudioRendererImplTest; | 103 friend class AudioRendererImplTest; |
94 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); | 104 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); |
95 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, | 105 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, |
96 DestroyedMessageLoop_ConsumeAudioSamples); | 106 DestroyedMessageLoop_ConsumeAudioSamples); |
97 // Helper methods. | 107 // Helper methods. |
98 // Convert number of bytes to duration of time using information about the | 108 // Convert number of bytes to duration of time using information about the |
99 // number of channels, sample rate and sample bits. | 109 // number of channels, sample rate and sample bits. |
100 base::TimeDelta ConvertToDuration(int bytes); | 110 base::TimeDelta ConvertToDuration(int bytes); |
101 | 111 |
102 // Methods call on IO thread ------------------------------------------------ | 112 // Methods call on IO thread ------------------------------------------------ |
103 // The following methods are tasks posted on the IO thread that needs to | 113 // The following methods are tasks posted on the IO thread that needs to |
104 // be executed on that thread. They interact with AudioMessageFilter and | 114 // be executed on that thread. They interact with AudioMessageFilter and |
105 // sends IPC messages on that thread. | 115 // sends IPC messages on that thread. |
106 void CreateStreamTask(const AudioParameters& params); | 116 void CreateStreamTask(const AudioParameters& params); |
107 void PlayTask(); | 117 void PlayTask(); |
108 void PauseTask(); | 118 void PauseTask(); |
109 void SeekTask(); | 119 void SeekTask(); |
110 void SetVolumeTask(double volume); | 120 void SetVolumeTask(double volume); |
111 void NotifyPacketReadyTask(); | 121 void NotifyPacketReadyTask(); |
112 void DestroyTask(); | 122 void DestroyTask(); |
113 | 123 |
114 // Called on IO thread when message loop is dying. | 124 // Called on IO thread when message loop is dying. |
115 virtual void WillDestroyCurrentMessageLoop(); | 125 virtual void WillDestroyCurrentMessageLoop(); |
116 | 126 |
| 127 // DelegateSimpleThread::Delegate implementation. |
| 128 virtual void Run(); |
| 129 |
| 130 // (Re-)starts playback. |
| 131 void NotifyDataAvailableIfNecessary(); |
| 132 |
| 133 // Creates socket. Virtual so tests can override. |
| 134 virtual void CreateSocket(base::SyncSocket::Handle socket_handle); |
| 135 |
| 136 // Launching audio thread. Virtual so tests can override. |
| 137 virtual void CreateAudioThread(); |
| 138 |
| 139 // Accessors used by tests. |
| 140 LatencyType latency_type() { |
| 141 return latency_type_; |
| 142 } |
| 143 |
| 144 // Should be called before any class instance is created. |
| 145 static void set_latency_type(LatencyType latency_type); |
| 146 |
117 // Used to calculate audio delay given bytes. | 147 // Used to calculate audio delay given bytes. |
118 uint32 bytes_per_second_; | 148 uint32 bytes_per_second_; |
119 | 149 |
120 scoped_refptr<AudioMessageFilter> filter_; | 150 scoped_refptr<AudioMessageFilter> filter_; |
121 | 151 |
122 // ID of the stream created in the browser process. | 152 // ID of the stream created in the browser process. |
123 int32 stream_id_; | 153 int32 stream_id_; |
124 | 154 |
125 // Memory shared by the browser process for audio buffer. | 155 // Memory shared by the browser process for audio buffer. |
126 scoped_ptr<base::SharedMemory> shared_memory_; | 156 scoped_ptr<base::SharedMemory> shared_memory_; |
127 uint32 shared_memory_size_; | 157 uint32 shared_memory_size_; |
128 | 158 |
| 159 // Low latency IPC stuff. |
| 160 scoped_ptr<base::SyncSocket> socket_; |
| 161 |
| 162 // That thread waits for audio input. |
| 163 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
| 164 |
129 // Message loop for the IO thread. | 165 // Message loop for the IO thread. |
130 MessageLoop* io_loop_; | 166 MessageLoop* io_loop_; |
131 | 167 |
132 // Protects: | 168 // Protects: |
133 // - |stopped_| | 169 // - |stopped_| |
134 // - |pending_request_| | 170 // - |pending_request_| |
135 // - |request_buffers_state_| | 171 // - |request_buffers_state_| |
136 base::Lock lock_; | 172 base::Lock lock_; |
137 | 173 |
138 // A flag that indicates this filter is called to stop. | 174 // A flag that indicates this filter is called to stop. |
139 bool stopped_; | 175 bool stopped_; |
140 | 176 |
141 // A flag that indicates an outstanding packet request. | 177 // A flag that indicates an outstanding packet request. |
142 bool pending_request_; | 178 bool pending_request_; |
143 | 179 |
144 // State of the audio buffers at time of the last request. | 180 // State of the audio buffers at time of the last request. |
145 AudioBuffersState request_buffers_state_; | 181 AudioBuffersState request_buffers_state_; |
146 | 182 |
147 // State variables for prerolling. | 183 // State variables for prerolling. |
148 bool prerolling_; | 184 bool prerolling_; |
149 | 185 |
150 // Remaining bytes for prerolling to complete. | 186 // Remaining bytes for prerolling to complete. |
151 uint32 preroll_bytes_; | 187 uint32 preroll_bytes_; |
152 | 188 |
153 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | 189 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); |
154 }; | 190 }; |
155 | 191 |
156 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | 192 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ |
OLD | NEW |