| 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 // AudioRendererHost serves audio related requests from AudioRenderer which | 5 // AudioRendererHost serves audio related requests from AudioRenderer which |
| 6 // lives inside the render process and provide access to audio hardware. | 6 // lives inside the render process and provide access to audio hardware. |
| 7 // | 7 // |
| 8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI | 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 | 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 | 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 | 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 | 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 | 13 // is created, a task of OnInitialized() is posted on IO thread in which |
| 14 // singleton of AudioManager is created and. | 14 // singleton of AudioManager is created. |
| 15 // | 15 // |
| 16 // Here's an example of a typical IPC dialog for audio: | 16 // Here's an example of a typical IPC dialog for audio: |
| 17 // | 17 // |
| 18 // Renderer AudioRendererHost | 18 // Renderer AudioRendererHost |
| 19 // | | | 19 // | | |
| 20 // | CreateStream > | | 20 // | CreateStream > | |
| 21 // | < Created | | 21 // | < NotifyStreamCreated | |
| 22 // | | | 22 // | | |
| 23 // | Play > | | 23 // | PlayStream > | |
| 24 // | < Playing | time | 24 // | < NotifyStreamStateChanged | kAudioStreamPlaying |
| 25 // | | | 25 // | | |
| 26 // | < RequestAudioPacket | | 26 // | PauseStream > | |
| 27 // | AudioPacketReady > | | 27 // | < NotifyStreamStateChanged | kAudioStreamPaused |
| 28 // | | |
| 29 // | PlayStream > | |
| 30 // | < NotifyStreamStateChanged | kAudioStreamPlaying |
| 28 // | ... | | 31 // | ... | |
| 29 // | < RequestAudioPacket | | 32 // | CloseStream > | |
| 30 // | AudioPacketReady > | | |
| 31 // | | | |
| 32 // | ... | | |
| 33 // | < RequestAudioPacket | | |
| 34 // | AudioPacketReady > | | |
| 35 // | ... | | |
| 36 // | Pause > | | |
| 37 // | < Paused | | |
| 38 // | ... | | |
| 39 // | Start > | | |
| 40 // | < Started | | |
| 41 // | ... | | |
| 42 // | Close > | | |
| 43 // v v | 33 // v v |
| 44 | 34 |
| 45 // The above mode of operation uses relatively big buffers and has latencies | 35 // A SyncSocket pair is used to signal buffer readiness between processes. |
| 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 | 36 |
| 52 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| 53 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| 54 #pragma once | 39 #pragma once |
| 55 | 40 |
| 56 #include <map> | 41 #include <map> |
| 57 | 42 |
| 58 #include "base/gtest_prod_util.h" | 43 #include "base/gtest_prod_util.h" |
| 59 #include "base/memory/ref_counted.h" | 44 #include "base/memory/ref_counted.h" |
| 60 #include "base/memory/scoped_ptr.h" | 45 #include "base/memory/scoped_ptr.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 90 // The audio stream ID. | 75 // The audio stream ID. |
| 91 int stream_id; | 76 int stream_id; |
| 92 | 77 |
| 93 // Shared memory for transmission of the audio data. | 78 // Shared memory for transmission of the audio data. |
| 94 base::SharedMemory shared_memory; | 79 base::SharedMemory shared_memory; |
| 95 | 80 |
| 96 // The synchronous reader to be used by the controller. We have the | 81 // The synchronous reader to be used by the controller. We have the |
| 97 // ownership of the reader. | 82 // ownership of the reader. |
| 98 scoped_ptr<media::AudioOutputController::SyncReader> reader; | 83 scoped_ptr<media::AudioOutputController::SyncReader> reader; |
| 99 | 84 |
| 100 bool pending_buffer_request; | |
| 101 | |
| 102 // Set to true after we called Close() for the controller. | 85 // Set to true after we called Close() for the controller. |
| 103 bool pending_close; | 86 bool pending_close; |
| 104 }; | 87 }; |
| 105 | 88 |
| 106 typedef std::map<int, AudioEntry*> AudioEntryMap; | 89 typedef std::map<int, AudioEntry*> AudioEntryMap; |
| 107 | 90 |
| 108 // Called from UI thread from the owner of this object. | 91 // Called from UI thread from the owner of this object. |
| 109 AudioRendererHost(const content::ResourceContext* resource_context); | 92 AudioRendererHost(const content::ResourceContext* resource_context); |
| 110 | 93 |
| 111 // content::BrowserMessageFilter implementation. | 94 // content::BrowserMessageFilter implementation. |
| 112 virtual void OnChannelClosing() OVERRIDE; | 95 virtual void OnChannelClosing() OVERRIDE; |
| 113 virtual void OnDestruct() const OVERRIDE; | 96 virtual void OnDestruct() const OVERRIDE; |
| 114 virtual bool OnMessageReceived(const IPC::Message& message, | 97 virtual bool OnMessageReceived(const IPC::Message& message, |
| 115 bool* message_was_ok) OVERRIDE; | 98 bool* message_was_ok) OVERRIDE; |
| 116 | 99 |
| 117 // AudioOutputController::EventHandler implementations. | 100 // AudioOutputController::EventHandler implementations. |
| 118 virtual void OnCreated(media::AudioOutputController* controller) OVERRIDE; | 101 virtual void OnCreated(media::AudioOutputController* controller) OVERRIDE; |
| 119 virtual void OnPlaying(media::AudioOutputController* controller) OVERRIDE; | 102 virtual void OnPlaying(media::AudioOutputController* controller) OVERRIDE; |
| 120 virtual void OnPaused(media::AudioOutputController* controller) OVERRIDE; | 103 virtual void OnPaused(media::AudioOutputController* controller) OVERRIDE; |
| 121 virtual void OnError(media::AudioOutputController* controller, | 104 virtual void OnError(media::AudioOutputController* controller, |
| 122 int error_code) OVERRIDE; | 105 int error_code) OVERRIDE; |
| 123 virtual void OnMoreData(media::AudioOutputController* controller, | |
| 124 AudioBuffersState buffers_state) OVERRIDE; | |
| 125 | 106 |
| 126 private: | 107 private: |
| 127 friend class AudioRendererHostTest; | 108 friend class AudioRendererHostTest; |
| 128 friend class content::BrowserThread; | 109 friend class content::BrowserThread; |
| 129 friend class base::DeleteHelper<AudioRendererHost>; | 110 friend class base::DeleteHelper<AudioRendererHost>; |
| 130 friend class MockAudioRendererHost; | 111 friend class MockAudioRendererHost; |
| 131 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); | 112 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream); |
| 132 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); | 113 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation); |
| 133 | 114 |
| 134 virtual ~AudioRendererHost(); | 115 virtual ~AudioRendererHost(); |
| 135 | 116 |
| 136 // Methods called on IO thread ---------------------------------------------- | 117 // Methods called on IO thread ---------------------------------------------- |
| 137 | 118 |
| 138 // Audio related IPC message handlers. | 119 // Audio related IPC message handlers. |
| 139 // Creates an audio output stream with the specified format. If this call is | 120 // Creates an audio output stream with the specified format. If this call is |
| 140 // successful this object would keep an internal entry of the stream for the | 121 // successful this object would keep an internal entry of the stream for the |
| 141 // required properties. | 122 // required properties. |
| 142 void OnCreateStream(int stream_id, | 123 void OnCreateStream(int stream_id, const AudioParameters& params); |
| 143 const AudioParameters& params, | |
| 144 bool low_latency); | |
| 145 | 124 |
| 146 // Play the audio stream referenced by |stream_id|. | 125 // Play the audio stream referenced by |stream_id|. |
| 147 void OnPlayStream(int stream_id); | 126 void OnPlayStream(int stream_id); |
| 148 | 127 |
| 149 // Pause the audio stream referenced by |stream_id|. | 128 // Pause the audio stream referenced by |stream_id|. |
| 150 void OnPauseStream(int stream_id); | 129 void OnPauseStream(int stream_id); |
| 151 | 130 |
| 152 // Discard all audio data in stream referenced by |stream_id|. | 131 // Discard all audio data in stream referenced by |stream_id|. |
| 153 void OnFlushStream(int stream_id); | 132 void OnFlushStream(int stream_id); |
| 154 | 133 |
| 155 // Close the audio stream referenced by |stream_id|. | 134 // Close the audio stream referenced by |stream_id|. |
| 156 void OnCloseStream(int stream_id); | 135 void OnCloseStream(int stream_id); |
| 157 | 136 |
| 158 // Set the volume of the audio stream referenced by |stream_id|. | 137 // Set the volume of the audio stream referenced by |stream_id|. |
| 159 void OnSetVolume(int stream_id, double volume); | 138 void OnSetVolume(int stream_id, double volume); |
| 160 | 139 |
| 161 // Get the volume of the audio stream referenced by |stream_id|. | |
| 162 void OnGetVolume(int stream_id); | |
| 163 | |
| 164 // Notify packet has been prepared for the audio stream. | |
| 165 void OnNotifyPacketReady(int stream_id, uint32 packet_size); | |
| 166 | |
| 167 // Complete the process of creating an audio stream. This will set up the | 140 // Complete the process of creating an audio stream. This will set up the |
| 168 // shared memory or shared socket in low latency mode. | 141 // shared memory or shared socket in low latency mode. |
| 169 void DoCompleteCreation(media::AudioOutputController* controller); | 142 void DoCompleteCreation(media::AudioOutputController* controller); |
| 170 | 143 |
| 171 // Send a state change message to the renderer. | 144 // Send a state change message to the renderer. |
| 172 void DoSendPlayingMessage(media::AudioOutputController* controller); | 145 void DoSendPlayingMessage(media::AudioOutputController* controller); |
| 173 void DoSendPausedMessage(media::AudioOutputController* controller); | 146 void DoSendPausedMessage(media::AudioOutputController* controller); |
| 174 | 147 |
| 175 // Request more data from the renderer. This method is used only in normal | |
| 176 // latency mode. | |
| 177 void DoRequestMoreData(media::AudioOutputController* controller, | |
| 178 AudioBuffersState buffers_state); | |
| 179 | |
| 180 // Handle error coming from audio stream. | 148 // Handle error coming from audio stream. |
| 181 void DoHandleError(media::AudioOutputController* controller, int error_code); | 149 void DoHandleError(media::AudioOutputController* controller, int error_code); |
| 182 | 150 |
| 183 // Send an error message to the renderer. | 151 // Send an error message to the renderer. |
| 184 void SendErrorMessage(int stream_id); | 152 void SendErrorMessage(int stream_id); |
| 185 | 153 |
| 186 // Delete all audio entry and all audio streams | 154 // Delete all audio entry and all audio streams |
| 187 void DeleteEntries(); | 155 void DeleteEntries(); |
| 188 | 156 |
| 189 // Closes the stream. The stream is then deleted in DeleteEntry() after it | 157 // Closes the stream. The stream is then deleted in DeleteEntry() after it |
| (...skipping 28 matching lines...) Expand all Loading... |
| 218 // A map of stream IDs to audio sources. | 186 // A map of stream IDs to audio sources. |
| 219 AudioEntryMap audio_entries_; | 187 AudioEntryMap audio_entries_; |
| 220 | 188 |
| 221 const content::ResourceContext* resource_context_; | 189 const content::ResourceContext* resource_context_; |
| 222 MediaObserver* media_observer_; | 190 MediaObserver* media_observer_; |
| 223 | 191 |
| 224 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); | 192 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost); |
| 225 }; | 193 }; |
| 226 | 194 |
| 227 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ | 195 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_ |
| OLD | NEW |