| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 // | |
| 5 // AudioRendererHost serves audio related requests from AudioRenderer which | |
| 6 // lives inside the render process and provide access to audio hardware. | |
| 7 // | |
| 8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI | |
| 9 // thread, but all other operations and method calls happen on IO thread, so we | |
| 10 // need to be extra careful about the lifetime of this object. AudioManager is a | |
| 11 // singleton and created in IO thread, audio output streams are also created in | |
| 12 // the IO thread, so we need to destroy them also in IO thread. After this class | |
| 13 // is created, a task of OnInitialized() is posted on IO thread in which | |
| 14 // singleton of AudioManager is created and. | |
| 15 // | |
| 16 // Here's an example of a typical IPC dialog for audio: | |
| 17 // | |
| 18 // Renderer AudioRendererHost | |
| 19 // | | | |
| 20 // | CreateStream > | | |
| 21 // | < Created | | |
| 22 // | | | |
| 23 // | Play > | | |
| 24 // | < Playing | time | |
| 25 // | | | |
| 26 // | < RequestAudioPacket | | |
| 27 // | AudioPacketReady > | | |
| 28 // | ... | | |
| 29 // | < RequestAudioPacket | | |
| 30 // | AudioPacketReady > | | |
| 31 // | | | |
| 32 // | ... | | |
| 33 // | < RequestAudioPacket | | |
| 34 // | AudioPacketReady > | | |
| 35 // | ... | | |
| 36 // | Pause > | | |
| 37 // | < Paused | | |
| 38 // | ... | | |
| 39 // | Start > | | |
| 40 // | < Started | | |
| 41 // | ... | | |
| 42 // | Close > | | |
| 43 // v v | |
| 44 | |
| 45 // The above mode of operation uses relatively big buffers and has latencies | |
| 46 // of 50 ms or more. There is a second mode of operation which is low latency. | |
| 47 // For low latency audio, the picture above is modified by not having the | |
| 48 // RequestAudioPacket and the AudioPacketReady messages, instead a SyncSocket | |
| 49 // pair is used to signal buffer readiness without having to route messages | |
| 50 // using the IO thread. | |
| 51 | 4 |
| 52 #ifndef CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ | 5 #ifndef CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ |
| 53 #define CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ | 6 #define CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ |
| 54 #pragma once | 7 #pragma once |
| 55 | 8 |
| 56 #include <map> | 9 // TODO(jam): remove this file when all files have been converted. |
| 57 | 10 #include "content/browser/renderer_host/audio_renderer_host.h" |
| 58 #include "base/gtest_prod_util.h" | |
| 59 #include "base/process.h" | |
| 60 #include "base/ref_counted.h" | |
| 61 #include "base/scoped_ptr.h" | |
| 62 #include "base/shared_memory.h" | |
| 63 #include "chrome/browser/browser_message_filter.h" | |
| 64 #include "chrome/browser/browser_thread.h" | |
| 65 #include "ipc/ipc_message.h" | |
| 66 #include "media/audio/audio_io.h" | |
| 67 #include "media/audio/audio_output_controller.h" | |
| 68 #include "media/audio/simple_sources.h" | |
| 69 | |
| 70 class AudioManager; | |
| 71 struct ViewHostMsg_Audio_CreateStream_Params; | |
| 72 | |
| 73 class AudioRendererHost : public BrowserMessageFilter, | |
| 74 public media::AudioOutputController::EventHandler { | |
| 75 public: | |
| 76 typedef std::pair<int32, int> AudioEntryId; | |
| 77 | |
| 78 struct AudioEntry { | |
| 79 AudioEntry(); | |
| 80 ~AudioEntry(); | |
| 81 | |
| 82 // The AudioOutputController that manages the audio stream. | |
| 83 scoped_refptr<media::AudioOutputController> controller; | |
| 84 | |
| 85 // Render view ID that requested the audio stream. | |
| 86 int32 render_view_id; | |
| 87 | |
| 88 // The audio stream ID in the render view. | |
| 89 int stream_id; | |
| 90 | |
| 91 // Shared memory for transmission of the audio data. | |
| 92 base::SharedMemory shared_memory; | |
| 93 | |
| 94 // The synchronous reader to be used by the controller. We have the | |
| 95 // ownership of the reader. | |
| 96 scoped_ptr<media::AudioOutputController::SyncReader> reader; | |
| 97 | |
| 98 bool pending_buffer_request; | |
| 99 | |
| 100 // Set to true after we called Close() for the controller. | |
| 101 bool pending_close; | |
| 102 }; | |
| 103 | |
| 104 typedef std::map<AudioEntryId, AudioEntry*> AudioEntryMap; | |
| 105 | |
| 106 // Called from UI thread from the owner of this object. | |
| 107 AudioRendererHost(); | |
| 108 | |
| 109 | |
| 110 // BrowserMessageFilter implementation. | |
| 111 virtual void OnChannelClosing(); | |
| 112 virtual void OnDestruct() const; | |
| 113 virtual bool OnMessageReceived(const IPC::Message& message, | |
| 114 bool* message_was_ok); | |
| 115 | |
| 116 ///////////////////////////////////////////////////////////////////////////// | |
| 117 // AudioOutputController::EventHandler implementations. | |
| 118 virtual void OnCreated(media::AudioOutputController* controller); | |
| 119 virtual void OnPlaying(media::AudioOutputController* controller); | |
| 120 virtual void OnPaused(media::AudioOutputController* controller); | |
| 121 virtual void OnError(media::AudioOutputController* controller, | |
| 122 int error_code); | |
| 123 virtual void OnMoreData(media::AudioOutputController* controller, | |
| 124 AudioBuffersState buffers_state); | |
| 125 | |
| 126 private: | |
| 127 friend class AudioRendererHostTest; | |
| 128 friend class BrowserThread; | |
| 129 friend class DeleteTask<AudioRendererHost>; | |
| 130 friend class MockAudioRendererHost; | |
| 131 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | |
| 132 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | |
| 133 | |
| 134 virtual ~AudioRendererHost(); | |
| 135 | |
| 136 //////////////////////////////////////////////////////////////////////////// | |
| 137 // Methods called on IO thread. | |
| 138 // Returns true if the message is an audio related message and should be | |
| 139 // handled by this class. | |
| 140 bool IsAudioRendererHostMessage(const IPC::Message& message); | |
| 141 | |
| 142 // Audio related IPC message handlers. | |
| 143 // Creates an audio output stream with the specified format. If this call is | |
| 144 // successful this object would keep an internal entry of the stream for the | |
| 145 // required properties. | |
| 146 void OnCreateStream(const IPC::Message& msg, int stream_id, | |
| 147 const ViewHostMsg_Audio_CreateStream_Params& params, | |
| 148 bool low_latency); | |
| 149 | |
| 150 // Play the audio stream referenced by |stream_id|. | |
| 151 void OnPlayStream(const IPC::Message& msg, int stream_id); | |
| 152 | |
| 153 // Pause the audio stream referenced by |stream_id|. | |
| 154 void OnPauseStream(const IPC::Message& msg, int stream_id); | |
| 155 | |
| 156 // Discard all audio data in stream referenced by |stream_id|. | |
| 157 void OnFlushStream(const IPC::Message& msg, int stream_id); | |
| 158 | |
| 159 // Close the audio stream referenced by |stream_id|. | |
| 160 void OnCloseStream(const IPC::Message& msg, int stream_id); | |
| 161 | |
| 162 // Set the volume of the audio stream referenced by |stream_id|. | |
| 163 void OnSetVolume(const IPC::Message& msg, int stream_id, double volume); | |
| 164 | |
| 165 // Get the volume of the audio stream referenced by |stream_id|. | |
| 166 void OnGetVolume(const IPC::Message& msg, int stream_id); | |
| 167 | |
| 168 // Notify packet has been prepared for the audio stream. | |
| 169 void OnNotifyPacketReady(const IPC::Message& msg, int stream_id, | |
| 170 uint32 packet_size); | |
| 171 | |
| 172 // Complete the process of creating an audio stream. This will set up the | |
| 173 // shared memory or shared socket in low latency mode. | |
| 174 void DoCompleteCreation(media::AudioOutputController* controller); | |
| 175 | |
| 176 // Send a state change message to the renderer. | |
| 177 void DoSendPlayingMessage(media::AudioOutputController* controller); | |
| 178 void DoSendPausedMessage(media::AudioOutputController* controller); | |
| 179 | |
| 180 // Request more data from the renderer. This method is used only in normal | |
| 181 // latency mode. | |
| 182 void DoRequestMoreData(media::AudioOutputController* controller, | |
| 183 AudioBuffersState buffers_state); | |
| 184 | |
| 185 // Handle error coming from audio stream. | |
| 186 void DoHandleError(media::AudioOutputController* controller, int error_code); | |
| 187 | |
| 188 // Send an error message to the renderer. | |
| 189 void SendErrorMessage(int32 render_view_id, int32 stream_id); | |
| 190 | |
| 191 // Delete all audio entry and all audio streams | |
| 192 void DeleteEntries(); | |
| 193 | |
| 194 // Closes the stream. The stream is then deleted in DeleteEntry() after it | |
| 195 // is closed. | |
| 196 void CloseAndDeleteStream(AudioEntry* entry); | |
| 197 | |
| 198 // Called on the audio thread after the audio stream is closed. | |
| 199 void OnStreamClosed(AudioEntry* entry); | |
| 200 | |
| 201 // Delete an audio entry and close the related audio stream. | |
| 202 void DeleteEntry(AudioEntry* entry); | |
| 203 | |
| 204 // Delete audio entry and close the related audio stream due to an error, | |
| 205 // and error message is send to the renderer. | |
| 206 void DeleteEntryOnError(AudioEntry* entry); | |
| 207 | |
| 208 // A helper method to look up a AudioEntry with a tuple of render view | |
| 209 // id and stream id. Returns NULL if not found. | |
| 210 AudioEntry* LookupById(int render_view_id, int stream_id); | |
| 211 | |
| 212 // Search for a AudioEntry having the reference to |controller|. | |
| 213 // This method is used to look up an AudioEntry after a controller | |
| 214 // event is received. | |
| 215 AudioEntry* LookupByController(media::AudioOutputController* controller); | |
| 216 | |
| 217 // A map of id to audio sources. | |
| 218 AudioEntryMap audio_entries_; | |
| 219 | |
| 220 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | |
| 221 }; | |
| 222 | 11 |
| 223 #endif // CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ | 12 #endif // CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |