OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_SINK_CACHE_IMPL_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_SINK_CACHE_IMPL_H_ |
| 7 |
| 8 #include "content/renderer/media/audio_renderer_sink_cache.h" |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/synchronization/lock.h" |
| 14 #include "base/time/time.h" |
| 15 #include "content/common/content_export.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 // AudioRendererSinkCache implementation. |
| 20 class CONTENT_EXPORT AudioRendererSinkCacheImpl |
| 21 : public AudioRendererSinkCache { |
| 22 public: |
| 23 // Callback to be used for AudioRendererSink creation |
| 24 using CreateSinkCallback = |
| 25 base::Callback<scoped_refptr<media::AudioRendererSink>( |
| 26 int render_frame_id, |
| 27 int session_id, |
| 28 const std::string& device_id, |
| 29 const url::Origin& security_origin)>; |
| 30 |
| 31 AudioRendererSinkCacheImpl( |
| 32 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 33 const CreateSinkCallback& create_sink_callback, |
| 34 base::TimeDelta delete_timeout); |
| 35 |
| 36 ~AudioRendererSinkCacheImpl() final; |
| 37 |
| 38 media::OutputDeviceInfo GetSinkInfo(int source_render_frame_id, |
| 39 int session_id, |
| 40 const std::string& device_id, |
| 41 const url::Origin& security_origin) final; |
| 42 |
| 43 scoped_refptr<media::AudioRendererSink> GetSink( |
| 44 int source_render_frame_id, |
| 45 const std::string& device_id, |
| 46 const url::Origin& security_origin) final; |
| 47 |
| 48 void ReleaseSink(const media::AudioRendererSink* sink_ptr) final; |
| 49 |
| 50 private: |
| 51 friend class AudioRendererSinkCacheTest; |
| 52 friend class CacheEntryFinder; |
| 53 |
| 54 struct CacheEntry; |
| 55 using CacheContainer = std::vector<CacheEntry>; |
| 56 |
| 57 // Schedules a sink for deletion. Deletion will be performed on the same |
| 58 // thread the cache is created on. |
| 59 void DeleteLaterIfUnused(const media::AudioRendererSink* sink_ptr); |
| 60 |
| 61 // Deletes a sink from the cache. If |force_delete_used| is set, a sink being |
| 62 // deleted can (and should) be in use at the moment of deletion; otherwise the |
| 63 // sink is deleted only if unused. |
| 64 void DeleteSink(const media::AudioRendererSink* sink_ptr, |
| 65 bool force_delete_used); |
| 66 |
| 67 CacheContainer::iterator FindCacheEntry_Locked( |
| 68 int source_render_frame_id, |
| 69 const std::string& device_id, |
| 70 const url::Origin& security_origin, |
| 71 bool unused_only); |
| 72 |
| 73 // To avoid publishing CacheEntry structure in the header. |
| 74 int GetCacheSizeForTesting(); |
| 75 |
| 76 // Task runner for scheduled sink garbage collection. |
| 77 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 78 |
| 79 // Callback used for sink creation. |
| 80 const CreateSinkCallback create_sink_cb_; |
| 81 |
| 82 // Cached sink deletion timeout. |
| 83 // For example: (1) sink was created and cached in GetSinkInfo(), and then (2) |
| 84 // the same sink is requested in GetSink(), if time interval between (1) and |
| 85 // (2) is less than |kDeleteTimeoutMs|, then sink cached in (1) is reused in |
| 86 // (2). On the other hand, if after (1) nobody is interested in the sink |
| 87 // within |kDeleteTimeoutMs|, it is garbage-collected. |
| 88 const base::TimeDelta delete_timeout_; |
| 89 |
| 90 // Cached sinks, protected by lock. |
| 91 base::Lock cache_lock_; |
| 92 CacheContainer cache_; |
| 93 |
| 94 // Weak pointer to be used for delayed sink deletion on |task_runner_|. |
| 95 // Pre-created in constructor and is used to post all the delayed tasks. |
| 96 // A delayed task can be concurrently posted from any thread the cache is used |
| 97 // on, so on-the-flight weak pointer creation with |
| 98 // weak_ptr_factory_.GetWeakPtr() can't be used, because it will result in the |
| 99 // racy access to the factory. |
| 100 base::WeakPtr<AudioRendererSinkCacheImpl> weak_this_; |
| 101 |
| 102 // Used to produce |weak_this_| on AudioRendererSinkCacheImpl construction. |
| 103 base::WeakPtrFactory<AudioRendererSinkCacheImpl> weak_ptr_factory_; |
| 104 |
| 105 DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCacheImpl); |
| 106 }; |
| 107 |
| 108 } // namespace content |
| 109 |
| 110 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_SINK_CACHE_IMPL_H_ |
OLD | NEW |