Commit fcb624a2 authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

Merge AudioRendererSinkCache and AudioRendererSinkCacheImpl

This CL is a follow up of https://crrev.com/c/2414551, where
AudioRendererSinkCacheImpl was moved to Blink core.

BUG=787252
R=guidou@chromium.org, haraken@chromium.org

Change-Id: Ifdaa4a5b68c4be2585307522629edc3e25f9a046
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418911
Commit-Queue: Antonio Gomes (GMT-4) <tonikitoo@igalia.com>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarGuido Urdaneta <guidou@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809267}
parent a20f05be
...@@ -278,7 +278,6 @@ source_set("blink_headers") { ...@@ -278,7 +278,6 @@ source_set("blink_headers") {
"web/blink.h", "web/blink.h",
"web/modules/autofill/web_form_element_observer.h", "web/modules/autofill/web_form_element_observer.h",
"web/modules/media/audio/audio_device_factory.h", "web/modules/media/audio/audio_device_factory.h",
"web/modules/media/audio/audio_renderer_sink_cache.h",
"web/modules/media/audio/web_audio_input_ipc_factory.h", "web/modules/media/audio/web_audio_input_ipc_factory.h",
"web/modules/media/audio/web_audio_output_ipc_factory.h", "web/modules/media/audio/web_audio_output_ipc_factory.h",
"web/modules/media/webmediaplayer_util.h", "web/modules/media/webmediaplayer_util.h",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_PUBLIC_WEB_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_H_
#define THIRD_PARTY_BLINK_PUBLIC_WEB_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_H_
#include <memory>
#include <string>
#include "base/memory/scoped_refptr.h"
#include "base/unguessable_token.h"
#include "media/base/output_device_info.h"
#include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/public/platform/web_common.h"
namespace media {
class AudioRendererSink;
}
namespace blink {
// Caches AudioRendererSink instances, provides them to the clients for usage,
// tracks their used/unused state, reuses them to obtain output device
// information, garbage-collects unused sinks.
// Must live on the main render thread. Thread safe.
//
// TODO(https://crrev.com/787252): Move this header out of the Blink public API
// layer.
class BLINK_MODULES_EXPORT AudioRendererSinkCache {
public:
virtual ~AudioRendererSinkCache() {}
// Returns output device information for a specified sink.
virtual media::OutputDeviceInfo GetSinkInfo(
const LocalFrameToken& source_frame_token,
const base::UnguessableToken& session_id,
const std::string& device_id) = 0;
// Provides a sink for usage. The sink must be returned to the cache by
// calling ReleaseSink(). The sink must be stopped by the user before
// deletion, but after releasing it from the cache.
virtual scoped_refptr<media::AudioRendererSink> GetSink(
const LocalFrameToken& source_frame_token,
const std::string& device_id) = 0;
// Notifies the cache that the sink is not in use any more. Must be
// called by the client, so that the cache can garbage-collect the sink
// reference.
virtual void ReleaseSink(const media::AudioRendererSink* sink_ptr) = 0;
protected:
AudioRendererSinkCache() {}
private:
DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCache);
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_PUBLIC_WEB_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_H_
...@@ -9,8 +9,8 @@ blink_modules_sources("media") { ...@@ -9,8 +9,8 @@ blink_modules_sources("media") {
"audio/audio_device_factory.cc", "audio/audio_device_factory.cc",
"audio/audio_renderer_mixer_manager.cc", "audio/audio_renderer_mixer_manager.cc",
"audio/audio_renderer_mixer_manager.h", "audio/audio_renderer_mixer_manager.h",
"audio/audio_renderer_sink_cache_impl.cc", "audio/audio_renderer_sink_cache.cc",
"audio/audio_renderer_sink_cache_impl.h", "audio/audio_renderer_sink_cache.h",
"audio/mojo_audio_input_ipc.cc", "audio/mojo_audio_input_ipc.cc",
"audio/mojo_audio_input_ipc.h", "audio/mojo_audio_input_ipc.h",
"audio/mojo_audio_output_ipc.cc", "audio/mojo_audio_output_ipc.cc",
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "third_party/blink/public/web/modules/media/audio/web_audio_input_ipc_factory.h" #include "third_party/blink/public/web/modules/media/audio/web_audio_input_ipc_factory.h"
#include "third_party/blink/public/web/modules/media/audio/web_audio_output_ipc_factory.h" #include "third_party/blink/public/web/modules/media/audio/web_audio_output_ipc_factory.h"
#include "third_party/blink/renderer/modules/media/audio/audio_renderer_mixer_manager.h" #include "third_party/blink/renderer/modules/media/audio/audio_renderer_mixer_manager.h"
#include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache_impl.h" #include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h" #include "third_party/blink/renderer/platform/wtf/wtf.h"
namespace blink { namespace blink {
...@@ -196,7 +196,7 @@ media::OutputDeviceInfo AudioDeviceFactory::GetOutputDeviceInfo( ...@@ -196,7 +196,7 @@ media::OutputDeviceInfo AudioDeviceFactory::GetOutputDeviceInfo(
// //
// TODO(crbug.com/787252): Replace the use of base::ThreadPool below by // TODO(crbug.com/787252): Replace the use of base::ThreadPool below by
// worker_pool::PostTask(). // worker_pool::PostTask().
static base::NoDestructor<AudioRendererSinkCacheImpl> cache( static base::NoDestructor<AudioRendererSinkCache> cache(
base::ThreadPool::CreateSequencedTaskRunner( base::ThreadPool::CreateSequencedTaskRunner(
{base::TaskPriority::BEST_EFFORT, {base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}),
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache_impl.h" #include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache.h"
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
...@@ -27,10 +27,10 @@ ...@@ -27,10 +27,10 @@
namespace blink { namespace blink {
AudioRendererSinkCacheImpl* AudioRendererSinkCacheImpl::instance_ = nullptr; AudioRendererSinkCache* AudioRendererSinkCache::instance_ = nullptr;
class AudioRendererSinkCacheImpl::FrameObserver final class AudioRendererSinkCache::FrameObserver final
: public GarbageCollected<AudioRendererSinkCacheImpl::FrameObserver>, : public GarbageCollected<AudioRendererSinkCache::FrameObserver>,
public Supplement<LocalFrame>, public Supplement<LocalFrame>,
public ExecutionContextLifecycleObserver { public ExecutionContextLifecycleObserver {
public: public:
...@@ -56,21 +56,21 @@ class AudioRendererSinkCacheImpl::FrameObserver final ...@@ -56,21 +56,21 @@ class AudioRendererSinkCacheImpl::FrameObserver final
void DropFrameCache() { void DropFrameCache() {
dropped_frame_cached_ = true; dropped_frame_cached_ = true;
if (!AudioRendererSinkCacheImpl::instance_) if (!AudioRendererSinkCache::instance_)
return; return;
if (!GetSupplementable()) if (!GetSupplementable())
return; return;
LocalFrameToken frame_token = GetSupplementable()->GetLocalFrameToken(); LocalFrameToken frame_token = GetSupplementable()->GetLocalFrameToken();
AudioRendererSinkCacheImpl::instance_->DropSinksForFrame(frame_token); AudioRendererSinkCache::instance_->DropSinksForFrame(frame_token);
} }
bool dropped_frame_cached_ = false; bool dropped_frame_cached_ = false;
DISALLOW_COPY_AND_ASSIGN(FrameObserver); DISALLOW_COPY_AND_ASSIGN(FrameObserver);
}; };
const char AudioRendererSinkCacheImpl::FrameObserver::kSupplementName[] = const char AudioRendererSinkCache::FrameObserver::kSupplementName[] =
"AudioRendererSinkCacheImpl::FrameObserver"; "AudioRendererSinkCache::FrameObserver";
namespace { namespace {
...@@ -97,7 +97,7 @@ bool SinkIsHealthy(media::AudioRendererSink* sink) { ...@@ -97,7 +97,7 @@ bool SinkIsHealthy(media::AudioRendererSink* sink) {
} // namespace } // namespace
// Cached sink data. // Cached sink data.
struct AudioRendererSinkCacheImpl::CacheEntry { struct AudioRendererSinkCache::CacheEntry {
LocalFrameToken source_frame_token; LocalFrameToken source_frame_token;
std::string device_id; std::string device_id;
scoped_refptr<media::AudioRendererSink> sink; // Sink instance scoped_refptr<media::AudioRendererSink> sink; // Sink instance
...@@ -105,15 +105,15 @@ struct AudioRendererSinkCacheImpl::CacheEntry { ...@@ -105,15 +105,15 @@ struct AudioRendererSinkCacheImpl::CacheEntry {
}; };
// static // static
void AudioRendererSinkCacheImpl::InstallFrameObserver(LocalFrame& frame) { void AudioRendererSinkCache::InstallFrameObserver(LocalFrame& frame) {
if (AudioRendererSinkCacheImpl::FrameObserver::From(frame)) if (AudioRendererSinkCache::FrameObserver::From(frame))
return; return;
Supplement<LocalFrame>::ProvideTo( Supplement<LocalFrame>::ProvideTo(
frame, frame,
MakeGarbageCollected<AudioRendererSinkCacheImpl::FrameObserver>(frame)); MakeGarbageCollected<AudioRendererSinkCache::FrameObserver>(frame));
} }
AudioRendererSinkCacheImpl::AudioRendererSinkCacheImpl( AudioRendererSinkCache::AudioRendererSinkCache(
scoped_refptr<base::SequencedTaskRunner> cleanup_task_runner, scoped_refptr<base::SequencedTaskRunner> cleanup_task_runner,
CreateSinkCallback create_sink_cb, CreateSinkCallback create_sink_cb,
base::TimeDelta delete_timeout) base::TimeDelta delete_timeout)
...@@ -124,7 +124,7 @@ AudioRendererSinkCacheImpl::AudioRendererSinkCacheImpl( ...@@ -124,7 +124,7 @@ AudioRendererSinkCacheImpl::AudioRendererSinkCacheImpl(
instance_ = this; instance_ = this;
} }
AudioRendererSinkCacheImpl::~AudioRendererSinkCacheImpl() { AudioRendererSinkCache::~AudioRendererSinkCache() {
// We just release all the cached sinks here. Stop them first. // We just release all the cached sinks here. Stop them first.
// We can stop all the sinks, no matter they are used or not, since // We can stop all the sinks, no matter they are used or not, since
// everything is being destroyed anyways. // everything is being destroyed anyways.
...@@ -135,11 +135,11 @@ AudioRendererSinkCacheImpl::~AudioRendererSinkCacheImpl() { ...@@ -135,11 +135,11 @@ AudioRendererSinkCacheImpl::~AudioRendererSinkCacheImpl() {
instance_ = nullptr; instance_ = nullptr;
} }
media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo( media::OutputDeviceInfo AudioRendererSinkCache::GetSinkInfo(
const LocalFrameToken& source_frame_token, const LocalFrameToken& source_frame_token,
const base::UnguessableToken& session_id, const base::UnguessableToken& session_id,
const std::string& device_id) { const std::string& device_id) {
TRACE_EVENT_BEGIN2("audio", "AudioRendererSinkCacheImpl::GetSinkInfo", TRACE_EVENT_BEGIN2("audio", "AudioRendererSinkCache::GetSinkInfo",
"frame_token", source_frame_token.ToString(), "device id", "frame_token", source_frame_token.ToString(), "device id",
device_id); device_id);
...@@ -156,8 +156,8 @@ media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo( ...@@ -156,8 +156,8 @@ media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo(
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization", "Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization",
SINK_CACHE_MISS_CANNOT_LOOKUP_BY_SESSION_ID, SINK_CACHE_LAST_ENTRY); SINK_CACHE_MISS_CANNOT_LOOKUP_BY_SESSION_ID, SINK_CACHE_LAST_ENTRY);
TRACE_EVENT_END1("audio", "AudioRendererSinkCacheImpl::GetSinkInfo", TRACE_EVENT_END1("audio", "AudioRendererSinkCache::GetSinkInfo", "result",
"result", "Cache not used due to using |session_id|"); "Cache not used due to using |session_id|");
return sink->GetOutputDeviceInfo(); return sink->GetOutputDeviceInfo();
} }
...@@ -171,8 +171,8 @@ media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo( ...@@ -171,8 +171,8 @@ media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo(
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization", "Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization",
SINK_CACHE_HIT, SINK_CACHE_LAST_ENTRY); SINK_CACHE_HIT, SINK_CACHE_LAST_ENTRY);
TRACE_EVENT_END1("audio", "AudioRendererSinkCacheImpl::GetSinkInfo", TRACE_EVENT_END1("audio", "AudioRendererSinkCache::GetSinkInfo", "result",
"result", "Cache hit"); "Cache hit");
return cache_iter->sink->GetOutputDeviceInfo(); return cache_iter->sink->GetOutputDeviceInfo();
} }
} }
...@@ -188,21 +188,20 @@ media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo( ...@@ -188,21 +188,20 @@ media::OutputDeviceInfo AudioRendererSinkCacheImpl::GetSinkInfo(
"Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization", "Media.Audio.Render.SinkCache.GetOutputDeviceInfoCacheUtilization",
SINK_CACHE_MISS_NO_SINK, SINK_CACHE_LAST_ENTRY); SINK_CACHE_MISS_NO_SINK, SINK_CACHE_LAST_ENTRY);
TRACE_EVENT_END1("audio", "AudioRendererSinkCacheImpl::GetSinkInfo", "result", TRACE_EVENT_END1("audio", "AudioRendererSinkCache::GetSinkInfo", "result",
"Cache miss"); "Cache miss");
// |sink| is ref-counted, so it's ok if it is removed from cache before we // |sink| is ref-counted, so it's ok if it is removed from cache before we
// get here. // get here.
return sink->GetOutputDeviceInfo(); return sink->GetOutputDeviceInfo();
} }
scoped_refptr<media::AudioRendererSink> AudioRendererSinkCacheImpl::GetSink( scoped_refptr<media::AudioRendererSink> AudioRendererSinkCache::GetSink(
const LocalFrameToken& source_frame_token, const LocalFrameToken& source_frame_token,
const std::string& device_id) { const std::string& device_id) {
UMA_HISTOGRAM_BOOLEAN("Media.Audio.Render.SinkCache.UsedForSinkCreation", UMA_HISTOGRAM_BOOLEAN("Media.Audio.Render.SinkCache.UsedForSinkCreation",
true); true);
TRACE_EVENT_BEGIN2("audio", "AudioRendererSinkCacheImpl::GetSink", TRACE_EVENT_BEGIN2("audio", "AudioRendererSinkCache::GetSink", "frame_token",
"frame_token", source_frame_token.ToString(), "device id", source_frame_token.ToString(), "device id", device_id);
device_id);
base::AutoLock auto_lock(cache_lock_); base::AutoLock auto_lock(cache_lock_);
...@@ -214,7 +213,7 @@ scoped_refptr<media::AudioRendererSink> AudioRendererSinkCacheImpl::GetSink( ...@@ -214,7 +213,7 @@ scoped_refptr<media::AudioRendererSink> AudioRendererSinkCacheImpl::GetSink(
cache_iter->used = true; cache_iter->used = true;
UMA_HISTOGRAM_BOOLEAN( UMA_HISTOGRAM_BOOLEAN(
"Media.Audio.Render.SinkCache.InfoSinkReusedForOutput", true); "Media.Audio.Render.SinkCache.InfoSinkReusedForOutput", true);
TRACE_EVENT_END1("audio", "AudioRendererSinkCacheImpl::GetSink", "result", TRACE_EVENT_END1("audio", "AudioRendererSinkCache::GetSink", "result",
"Cache hit"); "Cache hit");
return cache_iter->sink; return cache_iter->sink;
} }
...@@ -228,28 +227,28 @@ scoped_refptr<media::AudioRendererSink> AudioRendererSinkCacheImpl::GetSink( ...@@ -228,28 +227,28 @@ scoped_refptr<media::AudioRendererSink> AudioRendererSinkCacheImpl::GetSink(
true /* used */}; true /* used */};
if (SinkIsHealthy(cache_entry.sink.get())) { if (SinkIsHealthy(cache_entry.sink.get())) {
TRACE_EVENT_INSTANT0( TRACE_EVENT_INSTANT0("audio",
"audio", "AudioRendererSinkCacheImpl::GetSink: caching new sink", "AudioRendererSinkCache::GetSink: caching new sink",
TRACE_EVENT_SCOPE_THREAD); TRACE_EVENT_SCOPE_THREAD);
cache_.push_back(cache_entry); cache_.push_back(cache_entry);
} }
TRACE_EVENT_END1("audio", "AudioRendererSinkCacheImpl::GetSink", "result", TRACE_EVENT_END1("audio", "AudioRendererSinkCache::GetSink", "result",
"Cache miss"); "Cache miss");
return cache_entry.sink; return cache_entry.sink;
} }
void AudioRendererSinkCacheImpl::ReleaseSink( void AudioRendererSinkCache::ReleaseSink(
const media::AudioRendererSink* sink_ptr) { const media::AudioRendererSink* sink_ptr) {
// We don't know the sink state, so won't reuse it. Delete it immediately. // We don't know the sink state, so won't reuse it. Delete it immediately.
DeleteSink(sink_ptr, true); DeleteSink(sink_ptr, true);
} }
void AudioRendererSinkCacheImpl::DeleteLaterIfUnused( void AudioRendererSinkCache::DeleteLaterIfUnused(
const media::AudioRendererSink* sink_ptr) { const media::AudioRendererSink* sink_ptr) {
cleanup_task_runner_->PostDelayedTask( cleanup_task_runner_->PostDelayedTask(
FROM_HERE, FROM_HERE,
base::BindOnce(&AudioRendererSinkCacheImpl::DeleteSink, base::BindOnce(&AudioRendererSinkCache::DeleteSink,
// Unretained is safe here since this is a process-wide // Unretained is safe here since this is a process-wide
// singleton and tests will ensure lifetime. // singleton and tests will ensure lifetime.
base::Unretained(this), base::RetainedRef(sink_ptr), base::Unretained(this), base::RetainedRef(sink_ptr),
...@@ -257,7 +256,7 @@ void AudioRendererSinkCacheImpl::DeleteLaterIfUnused( ...@@ -257,7 +256,7 @@ void AudioRendererSinkCacheImpl::DeleteLaterIfUnused(
delete_timeout_); delete_timeout_);
} }
void AudioRendererSinkCacheImpl::DeleteSink( void AudioRendererSinkCache::DeleteSink(
const media::AudioRendererSink* sink_ptr, const media::AudioRendererSink* sink_ptr,
bool force_delete_used) { bool force_delete_used) {
DCHECK(sink_ptr); DCHECK(sink_ptr);
...@@ -302,8 +301,8 @@ void AudioRendererSinkCacheImpl::DeleteSink( ...@@ -302,8 +301,8 @@ void AudioRendererSinkCacheImpl::DeleteSink(
} }
} }
AudioRendererSinkCacheImpl::CacheContainer::iterator AudioRendererSinkCache::CacheContainer::iterator
AudioRendererSinkCacheImpl::FindCacheEntry_Locked( AudioRendererSinkCache::FindCacheEntry_Locked(
const LocalFrameToken& source_frame_token, const LocalFrameToken& source_frame_token,
const std::string& device_id, const std::string& device_id,
bool unused_only) { bool unused_only) {
...@@ -324,7 +323,7 @@ AudioRendererSinkCacheImpl::FindCacheEntry_Locked( ...@@ -324,7 +323,7 @@ AudioRendererSinkCacheImpl::FindCacheEntry_Locked(
}); });
} }
void AudioRendererSinkCacheImpl::CacheOrStopUnusedSink( void AudioRendererSinkCache::CacheOrStopUnusedSink(
const LocalFrameToken& source_frame_token, const LocalFrameToken& source_frame_token,
const std::string& device_id, const std::string& device_id,
scoped_refptr<media::AudioRendererSink> sink) { scoped_refptr<media::AudioRendererSink> sink) {
...@@ -347,7 +346,7 @@ void AudioRendererSinkCacheImpl::CacheOrStopUnusedSink( ...@@ -347,7 +346,7 @@ void AudioRendererSinkCacheImpl::CacheOrStopUnusedSink(
DeleteLaterIfUnused(cache_entry.sink.get()); DeleteLaterIfUnused(cache_entry.sink.get());
} }
void AudioRendererSinkCacheImpl::DropSinksForFrame( void AudioRendererSinkCache::DropSinksForFrame(
const LocalFrameToken& source_frame_token) { const LocalFrameToken& source_frame_token) {
base::AutoLock auto_lock(cache_lock_); base::AutoLock auto_lock(cache_lock_);
base::EraseIf(cache_, [source_frame_token](const CacheEntry& val) { base::EraseIf(cache_, [source_frame_token](const CacheEntry& val) {
...@@ -359,7 +358,7 @@ void AudioRendererSinkCacheImpl::DropSinksForFrame( ...@@ -359,7 +358,7 @@ void AudioRendererSinkCacheImpl::DropSinksForFrame(
}); });
} }
size_t AudioRendererSinkCacheImpl::GetCacheSizeForTesting() { size_t AudioRendererSinkCache::GetCacheSizeForTesting() {
return cache_.size(); return cache_.size();
} }
......
...@@ -2,29 +2,37 @@ ...@@ -2,29 +2,37 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_IMPL_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_IMPL_H_ #define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_H_
#include "third_party/blink/public/web/modules/media/audio/audio_renderer_sink_cache.h"
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "base/unguessable_token.h"
#include "media/audio/audio_sink_parameters.h" #include "media/audio/audio_sink_parameters.h"
#include "media/base/output_device_info.h"
#include "third_party/blink/public/common/tokens/tokens.h" #include "third_party/blink/public/common/tokens/tokens.h"
#include "third_party/blink/renderer/modules/modules_export.h" #include "third_party/blink/renderer/modules/modules_export.h"
namespace media {
class AudioRendererSink;
}
namespace blink { namespace blink {
class LocalFrame; class LocalFrame;
// AudioRendererSinkCache implementation. // Caches AudioRendererSink instances, provides them to the clients for usage,
class MODULES_EXPORT AudioRendererSinkCacheImpl // tracks their used/unused state, reuses them to obtain output device
: public AudioRendererSinkCache { // information, garbage-collects unused sinks.
// Must live on the main render thread. Thread safe.
class MODULES_EXPORT AudioRendererSinkCache {
public: public:
class FrameObserver; class FrameObserver;
...@@ -39,28 +47,27 @@ class MODULES_EXPORT AudioRendererSinkCacheImpl ...@@ -39,28 +47,27 @@ class MODULES_EXPORT AudioRendererSinkCacheImpl
static void InstallFrameObserver(LocalFrame& frame); static void InstallFrameObserver(LocalFrame& frame);
// |cleanup_task_runner| will be used to delete sinks when they are unused, // |cleanup_task_runner| will be used to delete sinks when they are unused,
// AudioRendererSinkCacheImpl must outlive any tasks posted to it. Since // AudioRendererSinkCache must outlive any tasks posted to it. Since
// the sink cache is normally a process-wide singleton, this isn't a problem. // the sink cache is normally a process-wide singleton, this isn't a problem.
AudioRendererSinkCacheImpl( AudioRendererSinkCache(
scoped_refptr<base::SequencedTaskRunner> cleanup_task_runner, scoped_refptr<base::SequencedTaskRunner> cleanup_task_runner,
CreateSinkCallback create_sink_callback, CreateSinkCallback create_sink_callback,
base::TimeDelta delete_timeout); base::TimeDelta delete_timeout);
~AudioRendererSinkCache();
~AudioRendererSinkCacheImpl() final;
// AudioRendererSinkCache implementation: // AudioRendererSinkCache implementation:
media::OutputDeviceInfo GetSinkInfo(const LocalFrameToken& source_frame_token, media::OutputDeviceInfo GetSinkInfo(const LocalFrameToken& source_frame_token,
const base::UnguessableToken& session_id, const base::UnguessableToken& session_id,
const std::string& device_id) final; const std::string& device_id);
scoped_refptr<media::AudioRendererSink> GetSink( scoped_refptr<media::AudioRendererSink> GetSink(
const LocalFrameToken& source_frame_token, const LocalFrameToken& source_frame_token,
const std::string& device_id) final; const std::string& device_id);
void ReleaseSink(const media::AudioRendererSink* sink_ptr) final; void ReleaseSink(const media::AudioRendererSink* sink_ptr);
private: private:
friend class AudioRendererSinkCacheTest; friend class AudioRendererSinkCacheTest;
friend class CacheEntryFinder; friend class CacheEntryFinder;
friend class AudioRendererSinkCacheImpl::FrameObserver; friend class AudioRendererSinkCache::FrameObserver;
struct CacheEntry; struct CacheEntry;
using CacheContainer = std::vector<CacheEntry>; using CacheContainer = std::vector<CacheEntry>;
...@@ -90,7 +97,7 @@ class MODULES_EXPORT AudioRendererSinkCacheImpl ...@@ -90,7 +97,7 @@ class MODULES_EXPORT AudioRendererSinkCacheImpl
size_t GetCacheSizeForTesting(); size_t GetCacheSizeForTesting();
// Global instance, set in constructor and unset in destructor. // Global instance, set in constructor and unset in destructor.
static AudioRendererSinkCacheImpl* instance_; static AudioRendererSinkCache* instance_;
// Renderer main task runner. // Renderer main task runner.
const scoped_refptr<base::SequencedTaskRunner> cleanup_task_runner_; const scoped_refptr<base::SequencedTaskRunner> cleanup_task_runner_;
...@@ -110,9 +117,9 @@ class MODULES_EXPORT AudioRendererSinkCacheImpl ...@@ -110,9 +117,9 @@ class MODULES_EXPORT AudioRendererSinkCacheImpl
base::Lock cache_lock_; base::Lock cache_lock_;
CacheContainer cache_; CacheContainer cache_;
DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCacheImpl); DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCache);
}; };
} // namespace blink } // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_IMPL_H_ #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_AUDIO_AUDIO_RENDERER_SINK_CACHE_H_
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache_impl.h" #include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache.h"
#include <utility> #include <utility>
...@@ -38,7 +38,7 @@ class AudioRendererSinkCacheTest : public testing::Test { ...@@ -38,7 +38,7 @@ class AudioRendererSinkCacheTest : public testing::Test {
task_runner_context_( task_runner_context_(
std::make_unique<base::TestMockTimeTaskRunner::ScopedContext>( std::make_unique<base::TestMockTimeTaskRunner::ScopedContext>(
task_runner_)), task_runner_)),
cache_(std::make_unique<AudioRendererSinkCacheImpl>( cache_(std::make_unique<AudioRendererSinkCache>(
task_runner_, task_runner_,
base::BindRepeating(&AudioRendererSinkCacheTest::CreateSink, base::BindRepeating(&AudioRendererSinkCacheTest::CreateSink,
base::Unretained(this)), base::Unretained(this)),
...@@ -99,7 +99,7 @@ class AudioRendererSinkCacheTest : public testing::Test { ...@@ -99,7 +99,7 @@ class AudioRendererSinkCacheTest : public testing::Test {
std::unique_ptr<base::TestMockTimeTaskRunner::ScopedContext> std::unique_ptr<base::TestMockTimeTaskRunner::ScopedContext>
task_runner_context_; task_runner_context_;
std::unique_ptr<AudioRendererSinkCacheImpl> cache_; std::unique_ptr<AudioRendererSinkCache> cache_;
private: private:
DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCacheTest); DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCacheTest);
...@@ -259,7 +259,7 @@ TEST_F(AudioRendererSinkCacheTest, UnhealthySinkIsStopped) { ...@@ -259,7 +259,7 @@ TEST_F(AudioRendererSinkCacheTest, UnhealthySinkIsStopped) {
kUnhealthyDeviceId, media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); kUnhealthyDeviceId, media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
cache_.reset(); // Destruct first so there's only one cache at a time. cache_.reset(); // Destruct first so there's only one cache at a time.
cache_ = std::make_unique<AudioRendererSinkCacheImpl>( cache_ = std::make_unique<AudioRendererSinkCache>(
task_runner_, task_runner_,
base::BindRepeating( base::BindRepeating(
[](scoped_refptr<media::AudioRendererSink> sink, [](scoped_refptr<media::AudioRendererSink> sink,
...@@ -287,7 +287,7 @@ TEST_F(AudioRendererSinkCacheTest, UnhealthySinkUsingSessionIdIsStopped) { ...@@ -287,7 +287,7 @@ TEST_F(AudioRendererSinkCacheTest, UnhealthySinkUsingSessionIdIsStopped) {
kUnhealthyDeviceId, media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL); kUnhealthyDeviceId, media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL);
cache_.reset(); // Destruct first so there's only one cache at a time. cache_.reset(); // Destruct first so there's only one cache at a time.
cache_ = std::make_unique<AudioRendererSinkCacheImpl>( cache_ = std::make_unique<AudioRendererSinkCache>(
task_runner_, task_runner_,
base::BindRepeating( base::BindRepeating(
[](scoped_refptr<media::AudioRendererSink> sink, [](scoped_refptr<media::AudioRendererSink> sink,
...@@ -352,10 +352,10 @@ TEST_F(AudioRendererSinkCacheTest, MultithreadedAccess) { ...@@ -352,10 +352,10 @@ TEST_F(AudioRendererSinkCacheTest, MultithreadedAccess) {
// Request device information on the first thread. // Request device information on the first thread.
PostAndWaitUntilDone( PostAndWaitUntilDone(
thread1, base::BindOnce( thread1,
base::IgnoreResult(&AudioRendererSinkCacheImpl::GetSinkInfo), base::BindOnce(base::IgnoreResult(&AudioRendererSinkCache::GetSinkInfo),
base::Unretained(cache_.get()), kFrameToken, base::Unretained(cache_.get()), kFrameToken,
base::UnguessableToken(), kDefaultDeviceId)); base::UnguessableToken(), kDefaultDeviceId));
EXPECT_EQ(1, sink_count()); EXPECT_EQ(1, sink_count());
...@@ -372,10 +372,10 @@ TEST_F(AudioRendererSinkCacheTest, MultithreadedAccess) { ...@@ -372,10 +372,10 @@ TEST_F(AudioRendererSinkCacheTest, MultithreadedAccess) {
// Request device information on the first thread again. // Request device information on the first thread again.
PostAndWaitUntilDone( PostAndWaitUntilDone(
thread1, base::BindOnce( thread1,
base::IgnoreResult(&AudioRendererSinkCacheImpl::GetSinkInfo), base::BindOnce(base::IgnoreResult(&AudioRendererSinkCache::GetSinkInfo),
base::Unretained(cache_.get()), kFrameToken, base::Unretained(cache_.get()), kFrameToken,
base::UnguessableToken(), kDefaultDeviceId)); base::UnguessableToken(), kDefaultDeviceId));
EXPECT_EQ(1, sink_count()); EXPECT_EQ(1, sink_count());
// Release the sink on the second thread. // Release the sink on the second thread.
......
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
#include "third_party/blink/renderer/modules/launch/file_handling_expiry_impl.h" #include "third_party/blink/renderer/modules/launch/file_handling_expiry_impl.h"
#include "third_party/blink/renderer/modules/launch/web_launch_service_impl.h" #include "third_party/blink/renderer/modules/launch/web_launch_service_impl.h"
#include "third_party/blink/renderer/modules/manifest/manifest_manager.h" #include "third_party/blink/renderer/modules/manifest/manifest_manager.h"
#include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache_impl.h" #include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache.h"
#include "third_party/blink/renderer/modules/media_controls/media_controls_impl.h" #include "third_party/blink/renderer/modules/media_controls/media_controls_impl.h"
#include "third_party/blink/renderer/modules/mediasource/media_source_registry_impl.h" #include "third_party/blink/renderer/modules/mediasource/media_source_registry_impl.h"
#include "third_party/blink/renderer/modules/mediastream/user_media_client.h" #include "third_party/blink/renderer/modules/mediastream/user_media_client.h"
...@@ -188,7 +188,7 @@ void ModulesInitializer::InstallSupplements(LocalFrame& frame) const { ...@@ -188,7 +188,7 @@ void ModulesInitializer::InstallSupplements(LocalFrame& frame) const {
DCHECK(WebLocalFrameImpl::FromFrame(&frame)->Client()); DCHECK(WebLocalFrameImpl::FromFrame(&frame)->Client());
InspectorAccessibilityAgent::ProvideTo(&frame); InspectorAccessibilityAgent::ProvideTo(&frame);
ImageDownloaderImpl::ProvideTo(frame); ImageDownloaderImpl::ProvideTo(frame);
AudioRendererSinkCacheImpl::InstallFrameObserver(frame); AudioRendererSinkCache::InstallFrameObserver(frame);
} }
MediaControls* ModulesInitializer::CreateMediaControls( MediaControls* ModulesInitializer::CreateMediaControls(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment