-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_renderer_sink_cache_impl.h
114 lines (87 loc) · 4.28 KB
/
audio_renderer_sink_cache_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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 CHROMIUM_MEDIA_LIB_AUDIO_RENDERER_SINK_CACHE_IMPL_H_
#define CHROMIUM_MEDIA_LIB_AUDIO_RENDERER_SINK_CACHE_IMPL_H_
#include "chromium_media_lib/audio_renderer_sink_cache.h"
#include <vector>
#include "base/single_thread_task_runner.h"
#include "base/synchronization/lock.h"
#include "base/time/time.h"
#include "media/base/media_export.h"
namespace media {
// AudioRendererSinkCache implementation.
class MEDIA_EXPORT AudioRendererSinkCacheImpl : public AudioRendererSinkCache {
public:
// Callback to be used for AudioRendererSink creation
using CreateSinkCallback =
base::Callback<scoped_refptr<media::AudioRendererSink>(
int render_frame_id,
int session_id,
const std::string& device_id,
const url::Origin& security_origin)>;
AudioRendererSinkCacheImpl(
scoped_refptr<base::SingleThreadTaskRunner> task_runner,
const CreateSinkCallback& create_sink_callback,
base::TimeDelta delete_timeout);
~AudioRendererSinkCacheImpl() final;
media::OutputDeviceInfo GetSinkInfo(int source_render_frame_id,
int session_id,
const std::string& device_id,
const url::Origin& security_origin) final;
scoped_refptr<media::AudioRendererSink> GetSink(
int source_render_frame_id,
const std::string& device_id,
const url::Origin& security_origin) final;
void ReleaseSink(const media::AudioRendererSink* sink_ptr) final;
private:
friend class AudioRendererSinkCacheTest;
friend class CacheEntryFinder;
struct CacheEntry;
using CacheContainer = std::vector<CacheEntry>;
// Schedules a sink for deletion. Deletion will be performed on the same
// thread the cache is created on.
void DeleteLaterIfUnused(const media::AudioRendererSink* sink_ptr);
// 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(const media::AudioRendererSink* sink_ptr,
bool force_delete_used);
CacheContainer::iterator FindCacheEntry_Locked(
int source_render_frame_id,
const std::string& device_id,
const url::Origin& security_origin,
bool unused_only);
void CacheUnusedSinkIfHealthy(int source_render_frame_id,
const std::string& device_id,
const url::Origin& security_origin,
scoped_refptr<media::AudioRendererSink> sink);
// To avoid publishing CacheEntry structure in the header.
int GetCacheSizeForTesting();
// Task runner for scheduled sink garbage collection.
const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
// Callback used for sink creation.
const CreateSinkCallback create_sink_cb_;
// Cached sink deletion timeout.
// For example: (1) sink was created 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.
const base::TimeDelta delete_timeout_;
// Cached sinks, protected by lock.
base::Lock cache_lock_;
CacheContainer cache_;
// Weak pointer to be used for delayed sink deletion on |task_runner_|.
// Pre-created in constructor and is used to post all the delayed tasks.
// A delayed task can be concurrently posted from any thread the cache is used
// on, so on-the-flight weak pointer creation with
// weak_ptr_factory_.GetWeakPtr() can't be used, because it will result in the
// racy access to the factory.
base::WeakPtr<AudioRendererSinkCacheImpl> weak_this_;
// Used to produce |weak_this_| on AudioRendererSinkCacheImpl construction.
base::WeakPtrFactory<AudioRendererSinkCacheImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(AudioRendererSinkCacheImpl);
};
} // namespace media
#endif // CHROMIUM_MEDIA_LIB_AUDIO_RENDERER_SINK_CACHE_IMPL_H_