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 const 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 int GetCacheSizeForTesting(); | |
74 | |
75 // Task runner for scheduled sink garbage collection. | |
76 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
77 | |
78 // Callback used for sink creation. | |
79 const CreateSinkCallback create_sink_cb_; | |
80 | |
81 // Cached sink deletion timeout. | |
82 // For example: (1) sink was created and cached in GetSinkInfo(), and then (2) | |
83 // the same sink is requested in GetSink(), if time interval between (1) and | |
84 // (2) is less than |kDeleteTimeoutMs|, then sink cached in (1) is reused in | |
85 // (2). On the other hand, if after (1) nobody is interested in the sink | |
86 // within |kDeleteTimeoutMs|, it is garbage-collected. | |
87 const base::TimeDelta delete_timeout_; | |
88 | |
89 // Cached sinks, protected by lock. | |
90 base::Lock cache_lock_; | |
91 CacheContainer cache_; | |
92 | |
93 // Weak pointer to be used for delayed sink deletion on |task_runner_|. | |
94 // Pre-created in constructor and is used to post all the delayed tasks. | |
95 // A delayed task can be concurrently posted from any thread the cache is used | |
96 // on, so on-the-flight weak pointer creation with | |
97 // weak_ptr_factory_.GetWeakPtr() can't be used, because it will result in the | |
98 // racy access to the factory. | |
o1ka
2016/05/26 09:00:27
Another alternative would be to post the task unde
| |
99 base::WeakPtr<AudioRendererSinkCacheImpl> weak_this_; | |
100 | |
101 // Used to produce |weak_this_| on AudioRendererSinkCacheImpl construction. | |
102 base::WeakPtrFactory<AudioRendererSinkCacheImpl> weak_ptr_factory_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCacheImpl); | |
105 }; | |
106 | |
107 } // namespace content | |
108 | |
109 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_SINK_CACHE_IMPL_H_ | |
OLD | NEW |