| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Audio rendering unit utilizing audio output stream provided by browser | |
| 6 // process through IPC. | |
| 7 // | |
| 8 // Relationship of classes. | |
| 9 // | |
| 10 // AudioRendererHost AudioRendererImpl | |
| 11 // ^ ^ | |
| 12 // | | | |
| 13 // v IPC v | |
| 14 // RenderMessageFilter <---------> AudioMessageFilter | |
| 15 // | |
| 16 // Implementation of interface with audio device is in AudioRendererHost and | |
| 17 // it provides services and entry points in RenderMessageFilter, allowing | |
| 18 // usage of IPC calls to interact with audio device. AudioMessageFilter acts | |
| 19 // as a portal for IPC calls and does no more than delegation. | |
| 20 // | |
| 21 // Transportation of audio buffer is done by using shared memory, after | |
| 22 // OnCreateStream is executed, OnCreated would be called along with a | |
| 23 // SharedMemoryHandle upon successful creation of audio output stream in the | |
| 24 // browser process. The same piece of shared memory would be used during the | |
| 25 // lifetime of this unit. | |
| 26 // | |
| 27 // This class lives inside three threads during it's lifetime, namely: | |
| 28 // 1. IO thread. | |
| 29 // The thread within which this class receives all the IPC messages and | |
| 30 // IPC communications can only happen in this thread. | |
| 31 // 2. Pipeline thread | |
| 32 // Initialization of filter and proper stopping of filters happens here. | |
| 33 // Properties of this filter is also set in this thread. | |
| 34 // 3. Audio decoder thread (If there's one.) | |
| 35 // Responsible for decoding audio data and gives raw PCM data to this object. | |
| 36 | |
| 37 #ifndef CHROME_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | |
| 38 #define CHROME_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | |
| 39 #pragma once | |
| 40 | |
| 41 #include "base/gtest_prod_util.h" | |
| 42 #include "base/message_loop.h" | |
| 43 #include "base/scoped_ptr.h" | |
| 44 #include "base/shared_memory.h" | |
| 45 #include "base/synchronization/lock.h" | |
| 46 #include "chrome/renderer/audio_message_filter.h" | |
| 47 #include "media/audio/audio_io.h" | |
| 48 #include "media/audio/audio_manager.h" | |
| 49 #include "media/base/filters.h" | |
| 50 #include "media/filters/audio_renderer_base.h" | |
| 51 | |
| 52 class AudioMessageFilter; | |
| 53 | |
| 54 class AudioRendererImpl : public media::AudioRendererBase, | |
| 55 public AudioMessageFilter::Delegate, | |
| 56 public MessageLoop::DestructionObserver { | |
| 57 public: | |
| 58 // Methods called on Render thread ------------------------------------------ | |
| 59 explicit AudioRendererImpl(AudioMessageFilter* filter); | |
| 60 virtual ~AudioRendererImpl(); | |
| 61 | |
| 62 // Methods called on IO thread ---------------------------------------------- | |
| 63 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. | |
| 64 virtual void OnRequestPacket(AudioBuffersState buffers_state); | |
| 65 virtual void OnStateChanged(const ViewMsg_AudioStreamState_Params& state); | |
| 66 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); | |
| 67 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | |
| 68 base::SyncSocket::Handle socket_handle, | |
| 69 uint32 length); | |
| 70 virtual void OnVolume(double volume); | |
| 71 | |
| 72 // Methods called on pipeline thread ---------------------------------------- | |
| 73 // media::Filter implementation. | |
| 74 virtual void SetPlaybackRate(float rate); | |
| 75 virtual void Pause(media::FilterCallback* callback); | |
| 76 virtual void Seek(base::TimeDelta time, media::FilterCallback* callback); | |
| 77 virtual void Play(media::FilterCallback* callback); | |
| 78 | |
| 79 // media::AudioRenderer implementation. | |
| 80 virtual void SetVolume(float volume); | |
| 81 | |
| 82 protected: | |
| 83 // Methods called on audio renderer thread ---------------------------------- | |
| 84 // These methods are called from AudioRendererBase. | |
| 85 virtual bool OnInitialize(const media::MediaFormat& media_format); | |
| 86 virtual void OnStop(); | |
| 87 | |
| 88 // Called when the decoder completes a Read(). | |
| 89 virtual void ConsumeAudioSamples(scoped_refptr<media::Buffer> buffer_in); | |
| 90 | |
| 91 private: | |
| 92 // For access to constructor and IO thread methods. | |
| 93 friend class AudioRendererImplTest; | |
| 94 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, Stop); | |
| 95 FRIEND_TEST_ALL_PREFIXES(AudioRendererImplTest, | |
| 96 DestroyedMessageLoop_ConsumeAudioSamples); | |
| 97 // Helper methods. | |
| 98 // Convert number of bytes to duration of time using information about the | |
| 99 // number of channels, sample rate and sample bits. | |
| 100 base::TimeDelta ConvertToDuration(int bytes); | |
| 101 | |
| 102 // Methods call on IO thread ------------------------------------------------ | |
| 103 // The following methods are tasks posted on the IO thread that needs to | |
| 104 // be executed on that thread. They interact with AudioMessageFilter and | |
| 105 // sends IPC messages on that thread. | |
| 106 void CreateStreamTask(const AudioParameters& params); | |
| 107 void PlayTask(); | |
| 108 void PauseTask(); | |
| 109 void SeekTask(); | |
| 110 void SetVolumeTask(double volume); | |
| 111 void NotifyPacketReadyTask(); | |
| 112 void DestroyTask(); | |
| 113 | |
| 114 // Called on IO thread when message loop is dying. | |
| 115 virtual void WillDestroyCurrentMessageLoop(); | |
| 116 | |
| 117 // Information about the audio stream. | |
| 118 AudioParameters params_; | |
| 119 uint32 bytes_per_second_; | |
| 120 | |
| 121 scoped_refptr<AudioMessageFilter> filter_; | |
| 122 | |
| 123 // ID of the stream created in the browser process. | |
| 124 int32 stream_id_; | |
| 125 | |
| 126 // Memory shared by the browser process for audio buffer. | |
| 127 scoped_ptr<base::SharedMemory> shared_memory_; | |
| 128 uint32 shared_memory_size_; | |
| 129 | |
| 130 // Message loop for the IO thread. | |
| 131 MessageLoop* io_loop_; | |
| 132 | |
| 133 // Protects: | |
| 134 // - |stopped_| | |
| 135 // - |pending_request_| | |
| 136 // - |request_buffers_state_| | |
| 137 base::Lock lock_; | |
| 138 | |
| 139 // A flag that indicates this filter is called to stop. | |
| 140 bool stopped_; | |
| 141 | |
| 142 // A flag that indicates an outstanding packet request. | |
| 143 bool pending_request_; | |
| 144 | |
| 145 // State of the audio buffers at time of the last request. | |
| 146 AudioBuffersState request_buffers_state_; | |
| 147 | |
| 148 // State variables for prerolling. | |
| 149 bool prerolling_; | |
| 150 | |
| 151 // Remaining bytes for prerolling to complete. | |
| 152 uint32 preroll_bytes_; | |
| 153 | |
| 154 DISALLOW_COPY_AND_ASSIGN(AudioRendererImpl); | |
| 155 }; | |
| 156 | |
| 157 #endif // CHROME_RENDERER_MEDIA_AUDIO_RENDERER_IMPL_H_ | |
| OLD | NEW |