diff options
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/renderer_host/audio_renderer_host.cc | 80 | ||||
-rw-r--r-- | chrome/browser/renderer_host/audio_renderer_host.h | 78 |
2 files changed, 89 insertions, 69 deletions
diff --git a/chrome/browser/renderer_host/audio_renderer_host.cc b/chrome/browser/renderer_host/audio_renderer_host.cc index 5c0d0f9..f8851d4 100644 --- a/chrome/browser/renderer_host/audio_renderer_host.cc +++ b/chrome/browser/renderer_host/audio_renderer_host.cc @@ -8,13 +8,15 @@ AudioRendererHost::IPCAudioSource::IPCAudioSource( AudioRendererHost* host, - int32 id, + int32 render_view_id, + int32 stream_id, AudioOutputStream* stream, IPC::Message::Sender* sender, base::ProcessHandle process, size_t packet_size) : host_(host), - id_(id), + render_view_id_(render_view_id), + stream_id_(stream_id), stream_(stream), sender_(sender) { // Make sure we can create and map the shared memory. @@ -48,7 +50,7 @@ void AudioRendererHost::IPCAudioSource::OnClose(AudioOutputStream* stream) { // Stuff to do here: // 1. Send an IPC to renderer about close complete. // 2. Remove this object from host. - host_->DestroySource(id_); + host_->DestroySource(render_view_id_, stream_id_); } void AudioRendererHost::IPCAudioSource::OnError(AudioOutputStream* stream, @@ -67,8 +69,7 @@ void AudioRendererHost::IPCAudioSource::NotifyPacketReady() { } AudioRendererHost::AudioRendererHost(MessageLoop* message_loop) - : next_id_(INVALID_ID+1), - io_loop_(message_loop) { + : io_loop_(message_loop) { // Make sure we perform actual initialization operations in the thread where // this object should live. io_loop_->PostTask(FROM_HERE, @@ -78,33 +79,35 @@ AudioRendererHost::AudioRendererHost(MessageLoop* message_loop) AudioRendererHost::~AudioRendererHost() { } -int AudioRendererHost::CreateStream( +bool AudioRendererHost::CreateStream( IPC::Message::Sender* sender, base::ProcessHandle handle, - AudioManager::Format format, int channels, int sample_rate, - int bits_per_sample, size_t packet_size) { + int32 render_view_id, int32 stream_id, AudioManager::Format format, + int channels, int sample_rate, int bits_per_sample, size_t packet_size) { DCHECK(MessageLoop::current() == io_loop_); + DCHECK(Lookup(render_view_id, stream_id) == NULL); // Create the stream in the first place. AudioOutputStream* stream = AudioManager::GetAudioManager()->MakeAudioStream( format, channels, sample_rate, bits_per_sample); if (!stream) - return INVALID_ID; + return false; // Try to open the stream if we can create it. if (stream->Open(packet_size)) { // Create the containing IPCAudioSource and save it to the map. IPCAudioSource* source = - new IPCAudioSource(this, next_id_++, stream, sender, handle, - packet_size); - sources_.AddWithID(source, source->id()); - return source->id(); + new IPCAudioSource(this, render_view_id, stream_id, stream, sender, + handle, packet_size); + sources_.insert(make_pair( + SourceID(source->render_view_id(), source->stream_id()), source)); + return true; } - return INVALID_ID; + return false; } -bool AudioRendererHost::Start(int stream_id) { +bool AudioRendererHost::Start(int32 render_view_id, int32 stream_id) { DCHECK(MessageLoop::current() == io_loop_); - IPCAudioSource* source = sources_.Lookup(stream_id); + IPCAudioSource* source = Lookup(render_view_id, stream_id); if (source) { source->stream()->Start(source); return true; @@ -112,9 +115,9 @@ bool AudioRendererHost::Start(int stream_id) { return false; } -bool AudioRendererHost::Stop(int stream_id) { +bool AudioRendererHost::Stop(int32 render_view_id, int32 stream_id) { DCHECK(MessageLoop::current() == io_loop_); - IPCAudioSource* source = sources_.Lookup(stream_id); + IPCAudioSource* source = Lookup(render_view_id, stream_id); if (source) { source->stream()->Stop(); return true; @@ -122,9 +125,9 @@ bool AudioRendererHost::Stop(int stream_id) { return false; } -bool AudioRendererHost::Close(int stream_id) { +bool AudioRendererHost::Close(int32 render_view_id, int32 stream_id) { DCHECK(MessageLoop::current() == io_loop_); - IPCAudioSource* source = sources_.Lookup(stream_id); + IPCAudioSource* source = Lookup(render_view_id, stream_id); if (source) { source->stream()->Close(); return true; @@ -132,20 +135,20 @@ bool AudioRendererHost::Close(int stream_id) { return false; } -bool AudioRendererHost::SetVolume( - int stream_id, double left_channel, double right_channel) { +bool AudioRendererHost::SetVolume(int32 render_view_id, int32 stream_id, + double left_channel, double right_channel) { DCHECK(MessageLoop::current() == io_loop_); - IPCAudioSource* source = sources_.Lookup(stream_id); + IPCAudioSource* source = Lookup(render_view_id, stream_id); if (source) { source->stream()->SetVolume(left_channel, right_channel); } return false; } -bool AudioRendererHost::GetVolume( - int stream_id, double* left_channel, double* right_channel) { +bool AudioRendererHost::GetVolume(int32 render_view_id, int32 stream_id, + double* left_channel, double* right_channel) { DCHECK(MessageLoop::current() == io_loop_); - IPCAudioSource* source = sources_.Lookup(stream_id); + IPCAudioSource* source = Lookup(render_view_id, stream_id); if (source) { source->stream()->GetVolume(left_channel, right_channel); return true; @@ -153,9 +156,10 @@ bool AudioRendererHost::GetVolume( return false; } -void AudioRendererHost::NotifyPacketReady(int stream_id) { +void AudioRendererHost::NotifyPacketReady(int32 render_view_id, + int32 stream_id) { DCHECK(MessageLoop::current() == io_loop_); - IPCAudioSource* source = sources_.Lookup(stream_id); + IPCAudioSource* source = Lookup(render_view_id, stream_id); if (source) { source->NotifyPacketReady(); } @@ -167,12 +171,14 @@ void AudioRendererHost::DestroyAllStreams() { // the map. } -void AudioRendererHost::DestroySource(int stream_id) { +void AudioRendererHost::DestroySource(int32 render_view_id, int32 stream_id) { DCHECK(MessageLoop::current() == io_loop_); - IPCAudioSource* source = sources_.Lookup(stream_id); - if (source) { - sources_.Remove(stream_id); - delete source; + SourceMap::iterator i = sources_.find(SourceID(render_view_id, stream_id)); + if (i != sources_.end()) { + // Remove the entry from the map. + sources_.erase(i); + // Also delete the IPCAudioSource object. + delete i->second; } } @@ -207,3 +213,11 @@ void AudioRendererHost::OnDestroyed() { // Decrease the reference to this object, which may lead to self-destruction. Release(); } + +AudioRendererHost::IPCAudioSource* AudioRendererHost::Lookup(int render_view_id, + int stream_id) { + SourceMap::iterator i = sources_.find(SourceID(render_view_id, stream_id)); + if (i != sources_.end()) + return i->second; + return NULL; +} diff --git a/chrome/browser/renderer_host/audio_renderer_host.h b/chrome/browser/renderer_host/audio_renderer_host.h index df2c14e..bcdf640 100644 --- a/chrome/browser/renderer_host/audio_renderer_host.h +++ b/chrome/browser/renderer_host/audio_renderer_host.h @@ -6,17 +6,16 @@ // lives inside the render process and provide access to audio hardware. It maps // an internal ID to AudioRendererHost::IPCAudioSource in a map, which is the // actual object providing audio packets through IPC. It creates the actual -// AudioOutputStream object when requested by the renderer and returns an -// internal ID. It then delegates calls to the AudioOutputStream object indexed -// by the internal id. +// AudioOutputStream object when requested by the renderer provided with +// render view id and stream id. // // AudioRendererHost::IPCAudioSource is a container of AudioOutputStream and // provide audio packets to the associated AudioOutputStream through IPC. It // transforms the pull data model to a push model used for IPC. When asked by // AudioOutputStream for an audio packet, it would send a message to renderer, // passing a SharedMemoryHandle for filling the buffer. -// NotifyPacketReady(|stream_id|) would be called when the buffer is filled -// and ready to be consumed. +// NotifyPacketReady(|render_view_id|, |stream_id|) would be called when the +// buffer is filled and ready to be consumed. // // This class is owned by BrowserRenderProcessHost, and instantiated on UI // thread, but all other operations and method calls (except Destroy()) happens @@ -41,7 +40,8 @@ #ifndef CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ #define CHROME_BROWSER_RENDERER_HOST_AUDIO_RENDERER_HOST_H_ -#include "base/id_map.h" +#include <map> + #include "base/ref_counted.h" #include "base/shared_memory.h" #include "chrome/common/ipc_message.h" @@ -52,55 +52,55 @@ class MessageLoop; class AudioRendererHost : public base::RefCountedThreadSafe<AudioRendererHost> { public: - static const int32 INVALID_ID = 0; - explicit AudioRendererHost(MessageLoop* message_loop); ~AudioRendererHost(); // Creates an audio output stream with the specified format, returns the - // stream id if successful, otherwise INVALID_ID. If this call is successful - // this object would keep an internal entry of the stream about the - // required properties, renderer process handle and IPC channel for sending - // buffer request messages. - int32 CreateStream(IPC::Message::Sender* sender, base::ProcessHandle handle, - AudioManager::Format format, int channels, int sample_rate, - int bits_per_sample, size_t packet_size); + // true if successful. If this call is successful this object would keep an + // internal entry of the stream about the required properties, renderer + // process handle and IPC channel for sending buffer request messages. + bool CreateStream(IPC::Message::Sender* sender, base::ProcessHandle handle, + int32 render_view_id, int32 stream_id, + AudioManager::Format format, int channels, int sample_rate, + int bits_per_sample, size_t packet_size); // Start the audio output strean, return false if stream doesn't exist or the // cannot start. - bool Start(int32 stream_id); + bool Start(int32 render_view_id, int32 stream_id); // Stop the audio output stream, return false if stream doesn't exist or // cannot stop. - bool Stop(int32 stream_id); + bool Stop(int32 render_view_id, int32 stream_id); // Close the audio output stream, return false if stream doesn't exist or // cannot close. If this call is successful, the AudioOutputStream correspond - // to |stream_id| would go unmanaged by this class, subsequent calls to - // this object with the same |stream_id| would fail. - bool Close(int32 stream_id); + // to (|render_view_id|, |stream_id|) would go unmanaged by this class, + // subsequent calls to this object with the same + // (|render_view_id|, |stream_id|) would fail. + bool Close(int32 render_view_id, int32 stream_id); // Set the volume for the stream specified, returns true if successful, false // if stream doesn't exist or cann't set volume. - bool SetVolume( - int32 stream_id, double left_channel, double right_channel); + bool SetVolume(int32 render_view_id, int32 stream_id, + double left_channel, double right_channel); // Get the volume of the stream specified, returns true if successful, false // is stream doesn't exist or can't get volume. - bool GetVolume( - int32 stream_id, double* left_channel, double* right_channel); + bool GetVolume(int32 render_view_id, int32 stream_id, + double* left_channel, double* right_channel); // Notify packet has been prepared for stream specified by |stream_id|. The - // buffer associated with |stream_id| has been filled and is ready to be - // consumed. - void NotifyPacketReady(int32 stream_id); + // buffer associated with (|render_view_id|, |stream_id|) has been filled and + // is ready to be consumed. + void NotifyPacketReady(int32 render_view_id, int32 stream_id); // Called from UI thread from the owner of this object. void Destroy(); - // Destroy the stream specified by |stream_id| and remove it from map. + // Destroy the stream specified by (|render_view_id|, |stream_id|) and remove + // it from map. // *DO NOT* call this method other than from IPCAudioSource. - void DestroySource(int32 stream_id); + void DestroySource(int32 render_view_id, int32 stream_id); private: // Methods called on IO thread. @@ -117,7 +117,8 @@ class AudioRendererHost : public base::RefCountedThreadSafe<AudioRendererHost> { class IPCAudioSource : public AudioOutputStream::AudioSourceCallback { public: IPCAudioSource(AudioRendererHost* host, // Host of this source. - int32 id, // ID of this source. + int32 render_view_id, // Routing ID to RenderView. + int32 stream_id, // ID of this source. AudioOutputStream* stream, // Stream associated. IPC::Message::Sender* sender, // IPC sender to user. base::ProcessHandle process, // Render process handle. @@ -135,23 +136,28 @@ class AudioRendererHost : public base::RefCountedThreadSafe<AudioRendererHost> { // consumed. void NotifyPacketReady(); - int32 id() { return id_; } + int32 render_view_id() { return render_view_id_; } + int32 stream_id() { return stream_id_; } AudioOutputStream* stream() { return stream_; } private: AudioRendererHost* host_; - int32 id_; + int32 render_view_id_; + int32 stream_id_; AudioOutputStream* stream_; IPC::Message::Sender* sender_; base::SharedMemory shared_memory_; base::SharedMemoryHandle foreign_memory_handle_; }; - // A map of id to audio sources. - IDMap<IPCAudioSource> sources_; + // Look up a IPCAudioSource with a tuple of render view id and stream id. + // Return NULL if not found. + IPCAudioSource* Lookup(int render_view_id, int stream_id); - // An internal id for streams. - int32 next_id_; + // A map of id to audio sources. + typedef std::pair<int32, int32> SourceID; + typedef std::map<SourceID, IPCAudioSource*> SourceMap; + SourceMap sources_; // Only used for DCHECKs to make sure all methods calls are from the same // thread as this object is created. |