Index: content/renderer/media/audio_renderer_sink_cache_impl.h |
diff --git a/content/renderer/media/audio_renderer_sink_cache_impl.h b/content/renderer/media/audio_renderer_sink_cache_impl.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..97eb75ef76f433f83848df8e6eb49bdaa9e9a313 |
--- /dev/null |
+++ b/content/renderer/media/audio_renderer_sink_cache_impl.h |
@@ -0,0 +1,133 @@ |
+// 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. |
+ |
+#include "content/renderer/media/audio_renderer_sink_cache.h" |
+ |
+#include <map> |
+ |
+#include "base/single_thread_task_runner.h" |
+#include "base/synchronization/lock.h" |
+#include "content/common/content_export.h" |
+#include "media/audio/audio_device_description.h" |
+#include "media/base/audio_renderer_sink.h" |
+ |
+namespace content { |
+ |
+// AudioRendererSinkCache implementation. |
+class CONTENT_EXPORT AudioRendererSinkCacheImpl |
+ : public AudioRendererSinkCache { |
+ public: |
+ // Callback to be used for AudioRendererSink creation |
+ typedef base::Callback<scoped_refptr<media::AudioRendererSink>( |
+ int render_frame_id, |
+ int session_id, |
+ const std::string& device_id, |
+ const url::Origin& security_origin)> |
+ CreateSinkCallback; |
+ |
+ AudioRendererSinkCacheImpl( |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
+ const CreateSinkCallback& create_sink_callback); |
+ |
+ ~AudioRendererSinkCacheImpl() final; |
+ |
+ media::OutputDeviceInfo GetSinkInfo(int source_render_frame_id, |
+ int session_id, |
+ const std::string& device_id, |
+ const url::Origin& security_origin) final; |
+ |
+ media::AudioRendererSink* GetSink(int source_render_frame_id, |
+ const std::string& device_id, |
+ const url::Origin& security_origin) final; |
+ |
+ void ReleaseSink(media::AudioRendererSink* sink) final; |
+ |
+ private: |
+ friend class AudioRendererSinkCacheTest; |
+ |
+ // Cached sink deletion timeout. |
+ // For example: (1) sink was create and cached in GetSinkInfo(), and then (2) |
+ // the same sink is requested in GetSink(), if time interval between (1) and |
+ // (2) is less than |kDeleteTimeoutMs|, then sink cached in (1) is reused in |
+ // (2). On the other hand, if after (1) nobody is interested in the sink |
+ // within |kDeleteTimeoutMs|, it is garbage-collected. |
+ enum { kDeleteTimeoutMs = 5000 }; |
+ |
+ // The key to be used to access a cached sink (not unique,there can be |
+ // multiple sinks per device. |
+ struct SinkKey { |
+ SinkKey(int source_render_frame_id, |
+ const std::string& device_id, |
+ const url::Origin& security_origin); |
+ SinkKey(const SinkKey& other); |
+ int source_render_frame_id; |
+ std::string device_id; |
+ url::Origin security_origin; |
+ }; |
+ |
+ // Strict weak ordering of the keys. |
+ struct SinkKeyCompare { |
+ bool operator()(const SinkKey& a, const SinkKey& b) const { |
+ if (a.source_render_frame_id != b.source_render_frame_id) |
+ return a.source_render_frame_id < b.source_render_frame_id; |
+ |
+ if (media::AudioDeviceDescription::IsDefaultDevice(a.device_id) && |
+ media::AudioDeviceDescription::IsDefaultDevice(b.device_id)) { |
+ // Both device IDs represent the same default device => do not compare |
+ // them; the default device is always authorized => ignoring security |
+ // origin. |
+ return false; |
+ } |
+ |
+ if (a.device_id != b.device_id) |
+ return a.device_id < b.device_id; |
+ |
+ return a.security_origin < b.security_origin; |
+ } |
+ }; |
+ |
+ // Cached sink data. |
+ struct AudioRendererSinkReference { |
+ AudioRendererSinkReference(scoped_refptr<media::AudioRendererSink> sink, |
+ bool used); |
+ AudioRendererSinkReference(const AudioRendererSinkReference& other); |
+ ~AudioRendererSinkReference(); |
+ scoped_refptr<media::AudioRendererSink> sink; // Sink instance |
+ bool used; // True if in used by a client. |
+ }; |
+ |
+ typedef std::multimap<SinkKey, AudioRendererSinkReference, SinkKeyCompare> |
miu
2016/05/12 21:53:06
1. If the number of sinks is small, consider using
o1ka
2016/05/17 17:17:24
1. multimap is also used for find() in GetSinkInfo
miu
2016/05/19 22:27:14
Well, okay. I'll admit I'm "bike-shedding" a littl
o1ka
2016/05/20 10:40:32
Ok. But before reverting this to the first version
|
+ AudioRendererSinkMap; |
+ |
+ // Schedules a sink for deletion. Deletion will be performed on the same |
+ // thread the cache is created on. |
+ void DeleteLaterIfUnused(media::AudioRendererSink* sink); |
+ |
+ // Deletes a sink from the cache. If |force_delete_used| is set, a sink being |
+ // deleted can (and should) be in use at the moment of deletion; otherwise the |
+ // sink is deleted only if unused. |
+ void DeleteSink(media::AudioRendererSink* sink, bool force_delete_used); |
+ |
+ // Creates a sink with a specified |key| and |used| state and inserts it into |
+ // the cache. Note: |sinks_lock_| must be held while operating on the result |
+ // of this function, otherwise the sink may be deleted and the pointer becomes |
+ // invalid. |
+ media::AudioRendererSink* InsertNewSink(const SinkKey& key, bool used); |
+ |
+ // Task runner for scheduled sink garbage collection. |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
miu
2016/05/12 21:53:06
Please make this const.
o1ka
2016/05/17 17:17:24
Done.
|
+ |
+ // Callback used for sink creation. |
+ const CreateSinkCallback create_sink_cb_; |
+ |
+ // Cached sinks, protected by lock. |
+ base::Lock sinks_lock_; |
+ AudioRendererSinkMap sinks_; |
+ |
+ base::WeakPtrFactory<AudioRendererSinkCacheImpl> weak_ptr_factory_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCacheImpl); |
+}; |
+ |
+} // namespace content |