| Index: content/browser/renderer_host/media/audio_renderer_host.cc
|
| diff --git a/content/browser/renderer_host/media/audio_renderer_host.cc b/content/browser/renderer_host/media/audio_renderer_host.cc
|
| index ca963294d840dc746e7cfc613f8f5e49c368de3d..b9162c05d0432090dc28ac82e503ec37c3294f45 100644
|
| --- a/content/browser/renderer_host/media/audio_renderer_host.cc
|
| +++ b/content/browser/renderer_host/media/audio_renderer_host.cc
|
| @@ -31,11 +31,14 @@
|
| #include "content/public/browser/media_observer.h"
|
| #include "content/public/browser/render_frame_host.h"
|
| #include "content/public/common/content_switches.h"
|
| +#include "content/renderer/media/audio_output_client.h"
|
| #include "media/audio/audio_device_name.h"
|
| #include "media/audio/audio_manager_base.h"
|
| #include "media/audio/audio_streams_tracker.h"
|
| #include "media/base/audio_bus.h"
|
| #include "media/base/limits.h"
|
| +#include "mojo/edk/embedder/embedder.h"
|
| +#include "mojo/public/cpp/system/handle.h"
|
|
|
| using media::AudioBus;
|
| using media::AudioManager;
|
| @@ -139,6 +142,17 @@ class AudioRendererHost::AudioEntry
|
| const std::string& output_device_id,
|
| scoped_ptr<base::SharedMemory> shared_memory,
|
| scoped_ptr<media::AudioOutputController::SyncReader> reader);
|
| +
|
| + AudioEntry(AudioRendererHost* host,
|
| + int stream_id,
|
| + int render_frame_id,
|
| + const media::AudioParameters& params,
|
| + const std::string& output_device_id,
|
| + scoped_ptr<base::SharedMemory> shared_memory,
|
| + scoped_ptr<media::AudioOutputController::SyncReader> reader,
|
| + AudioOutputStreamPtr stream,
|
| + const AudioOutput::CreateStreamCallback& callback);
|
| +
|
| ~AudioEntry() override;
|
|
|
| int stream_id() const {
|
| @@ -162,6 +176,8 @@ class AudioRendererHost::AudioEntry
|
|
|
| private:
|
| // media::AudioOutputController::EventHandler implementation.
|
| + void OnCreated(AudioOutputStreamPtr stream,
|
| + const AudioOutput::CreateStreamCallback& callback) override;
|
| void OnCreated() override;
|
| void OnPlaying() override;
|
| void OnPaused() override;
|
| @@ -207,6 +223,32 @@ AudioRendererHost::AudioEntry::AudioEntry(
|
| DCHECK(controller_.get());
|
| }
|
|
|
| +AudioRendererHost::AudioEntry::AudioEntry(
|
| + AudioRendererHost* host,
|
| + int stream_id,
|
| + int render_frame_id,
|
| + const media::AudioParameters& params,
|
| + const std::string& output_device_id,
|
| + scoped_ptr<base::SharedMemory> shared_memory,
|
| + scoped_ptr<media::AudioOutputController::SyncReader> reader,
|
| + AudioOutputStreamPtr stream,
|
| + const AudioOutput::CreateStreamCallback& callback)
|
| + : host_(host),
|
| + stream_id_(stream_id),
|
| + render_frame_id_(render_frame_id),
|
| + shared_memory_(std::move(shared_memory)),
|
| + reader_(std::move(reader)),
|
| + controller_(media::AudioOutputController::Create(host->audio_manager_,
|
| + this,
|
| + params,
|
| + output_device_id,
|
| + reader_.get(),
|
| + std::move(stream),
|
| + callback)),
|
| + playing_(false) {
|
| + DCHECK(controller_.get());
|
| +}
|
| +
|
| AudioRendererHost::AudioEntry::~AudioEntry() {}
|
|
|
| ///////////////////////////////////////////////////////////////////////////////
|
| @@ -281,6 +323,15 @@ void AudioRendererHost::AudioEntry::OnCreated() {
|
| base::Bind(&AudioRendererHost::DoCompleteCreation, host_, stream_id_));
|
| }
|
|
|
| +void AudioRendererHost::AudioEntry::OnCreated(
|
| + AudioOutputStreamPtr stream,
|
| + const AudioOutput::CreateStreamCallback& callback) {
|
| + host_->stream_ptr_ = std::move(stream);
|
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
| + base::Bind(&AudioRendererHost::DoCompleteCreationMojo,
|
| + host_, stream_id_, callback));
|
| +}
|
| +
|
| void AudioRendererHost::AudioEntry::OnPlaying() {
|
| BrowserThread::PostTask(
|
| BrowserThread::IO,
|
| @@ -350,6 +401,86 @@ void AudioRendererHost::DoCompleteCreation(int stream_id) {
|
| entry->shared_memory()->requested_size()));
|
| }
|
|
|
| +void AudioRendererHost::DoCompleteCreationMojo(
|
| + int stream_id,
|
| + const AudioOutput::CreateStreamCallback& callback) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| +
|
| + if (!PeerHandle()) {
|
| + DLOG(WARNING) << "Renderer process handle is invalid.";
|
| + ReportErrorAndClose(stream_id);
|
| + return;
|
| + }
|
| +
|
| + AudioEntry* const entry = LookupById(stream_id);
|
| + if (!entry) {
|
| + ReportErrorAndClose(stream_id);
|
| + return;
|
| + }
|
| +
|
| + // Once the audio stream is created then complete the creation process by
|
| + // mapping shared memory and sharing with the renderer process.
|
| + base::SharedMemoryHandle foreign_memory_handle;
|
| + if (!entry->shared_memory()->ShareToProcess(PeerHandle(),
|
| + &foreign_memory_handle)) {
|
| + // If we failed to map and share the shared memory then close the audio
|
| + // stream and send an error message.
|
| + ReportErrorAndClose(entry->stream_id());
|
| + return;
|
| + }
|
| +
|
| + AudioSyncReader* reader = static_cast<AudioSyncReader*>(entry->reader());
|
| +
|
| + base::SyncSocket::TransitDescriptor socket_descriptor;
|
| +
|
| + // If we failed to prepare the sync socket for the renderer then we fail
|
| + // the construction of audio stream.
|
| + if (!reader->PrepareForeignSocket(PeerHandle(), &socket_descriptor)) {
|
| + ReportErrorAndClose(entry->stream_id());
|
| + return;
|
| + }
|
| +
|
| + /*Send(new AudioMsg_NotifyStreamCreated(
|
| + entry->stream_id(), foreign_memory_handle, socket_descriptor,
|
| + entry->shared_memory()->requested_size()));*/
|
| + MojoHandle mojo_foreign_memory_handle;
|
| +
|
| + MojoResult shared_buffer_result = mojo::edk::CreateSharedBufferWrapper(
|
| + foreign_memory_handle, entry->shared_memory()->requested_size(), false,
|
| + &mojo_foreign_memory_handle);
|
| +
|
| + if (shared_buffer_result != MOJO_RESULT_OK) {
|
| + LOG(WARNING) << "Failed to wrap transit descriptor. Closing: "
|
| + << shared_buffer_result;
|
| + return;
|
| + }
|
| +
|
| + MojoHandle socket_descriptor_handle;
|
| +
|
| +#if defined(OS_WIN)
|
| + MojoResult platform_handle_result = mojo::edk::CreatePlatformHandleWrapper(
|
| + mojo::edk::ScopedPlatformHandle(
|
| + mojo::edk::PlatformHandle(socket_descriptor)),
|
| + &socket_descriptor_handle);
|
| +#else
|
| + MojoResult platform_handle_result = mojo::edk::CreatePlatformHandleWrapper(
|
| + mojo::edk::ScopedPlatformHandle(
|
| + mojo::edk::PlatformHandle(socket_descriptor.fd)),
|
| + &socket_descriptor_handle);
|
| +#endif
|
| +
|
| + if (platform_handle_result != MOJO_RESULT_OK) {
|
| + LOG(WARNING) << "Failed to wrap platform handle. Closing: "
|
| + << platform_handle_result;
|
| + return;
|
| + }
|
| +
|
| + callback.Run(mojo::ScopedSharedBufferHandle(
|
| + mojo::SharedBufferHandle(mojo_foreign_memory_handle)),
|
| + mojo::ScopedHandle(mojo::Handle(socket_descriptor_handle)),
|
| + std::move(stream_ptr_));
|
| +}
|
| +
|
| void AudioRendererHost::DoNotifyStreamStateChanged(int stream_id,
|
| bool is_playing) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| @@ -554,6 +685,31 @@ void AudioRendererHost::OnCreateStream(int stream_id,
|
| authorizations_.erase(auth_data);
|
| }
|
|
|
| +void AudioRendererHost::OnCreateStreamMojo(
|
| + int stream_id,
|
| + int render_frame_id,
|
| + const media::AudioParameters& params,
|
| + AudioOutputStreamPtr stream,
|
| + const AudioOutput::CreateStreamCallback& callback) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| + DVLOG(1) << "AudioRendererHost@" << this << "::OnCreateStream"
|
| + << "(stream_id=" << stream_id << ")";
|
| +
|
| + const auto& auth_data = authorizations_.find(stream_id);
|
| +
|
| + // If no previous authorization requested, assume default device
|
| + if (auth_data == authorizations_.end()) {
|
| + DoCreateStream(stream_id, render_frame_id, params, std::string(),
|
| + std::move(stream), callback);
|
| + return;
|
| + }
|
| +
|
| + CHECK(auth_data->second.first);
|
| + DoCreateStream(stream_id, render_frame_id, params, auth_data->second.second,
|
| + std::move(stream), callback);
|
| + authorizations_.erase(auth_data);
|
| +}
|
| +
|
| void AudioRendererHost::DoCreateStream(int stream_id,
|
| int render_frame_id,
|
| const media::AudioParameters& params,
|
| @@ -605,6 +761,61 @@ void AudioRendererHost::DoCreateStream(int stream_id,
|
| max_simultaneous_streams_ = audio_entries_.size();
|
| }
|
|
|
| +void AudioRendererHost::DoCreateStream(
|
| + int stream_id,
|
| + int render_frame_id,
|
| + const media::AudioParameters& params,
|
| + const std::string& device_unique_id,
|
| + AudioOutputStreamPtr stream,
|
| + const AudioOutput::CreateStreamCallback& callback) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| +
|
| + // media::AudioParameters is validated in the deserializer.
|
| + if (LookupById(stream_id) != NULL) {
|
| + SendErrorMessage(stream_id);
|
| + return;
|
| + }
|
| +
|
| + // Create the shared memory and share with the renderer process.
|
| + uint32_t shared_memory_size = sizeof(media::AudioOutputBufferParameters) +
|
| + AudioBus::CalculateMemorySize(params);
|
| + scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
|
| + if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) {
|
| + SendErrorMessage(stream_id);
|
| + return;
|
| + }
|
| +
|
| + scoped_ptr<AudioSyncReader> reader(
|
| + new AudioSyncReader(shared_memory.get(), params));
|
| + if (!reader->Init()) {
|
| + SendErrorMessage(stream_id);
|
| + return;
|
| + }
|
| +
|
| + MediaObserver* const media_observer =
|
| + GetContentClient()->browser()->GetMediaObserver();
|
| + if (media_observer)
|
| + media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id);
|
| +
|
| + scoped_ptr<AudioEntry> entry(
|
| + new AudioEntry(this, stream_id, render_frame_id, params, device_unique_id,
|
| + std::move(shared_memory), std::move(reader),
|
| + std::move(stream), callback));
|
| + if (mirroring_manager_) {
|
| + mirroring_manager_->AddDiverter(
|
| + render_process_id_, entry->render_frame_id(), entry->controller());
|
| + }
|
| + audio_entries_.insert(std::make_pair(stream_id, entry.release()));
|
| + g_audio_streams_tracker.Get().IncreaseStreamCount();
|
| +
|
| + audio_log_->OnCreated(stream_id, params, device_unique_id);
|
| + MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry(
|
| + stream_id, render_process_id_, render_frame_id, audio_log_.get());
|
| +
|
| + if (audio_entries_.size() > max_simultaneous_streams_)
|
| + max_simultaneous_streams_ = audio_entries_.size();
|
| +}
|
| +
|
| void AudioRendererHost::OnPlayStream(int stream_id) {
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
|
|
| @@ -653,6 +864,7 @@ void AudioRendererHost::SendErrorMessage(int stream_id) {
|
| }
|
|
|
| void AudioRendererHost::OnCloseStream(int stream_id) {
|
| + DLOG(WARNING) << "OnCloseStream " << stream_id;
|
| DCHECK_CURRENTLY_ON(BrowserThread::IO);
|
| authorizations_.erase(stream_id);
|
|
|
|
|