File: audio_renderer_sink_cache.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (237 lines) | stat: -rw-r--r-- 7,835 bytes parent folder | download | duplicates (6)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/media/audio/audio_renderer_sink_cache.h"

#include <algorithm>
#include <memory>
#include <utility>

#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "media/audio/audio_device_description.h"
#include "media/base/audio_renderer_sink.h"
#include "third_party/blink/public/web/modules/media/audio/audio_device_factory.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/supplementable.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"

namespace blink {

AudioRendererSinkCache* AudioRendererSinkCache::instance_ = nullptr;

class AudioRendererSinkCache::WindowObserver final
    : public GarbageCollected<AudioRendererSinkCache::WindowObserver>,
      public Supplement<LocalDOMWindow>,
      public ExecutionContextLifecycleObserver {
 public:
  static const char kSupplementName[];

  explicit WindowObserver(LocalDOMWindow& window)
      : Supplement<LocalDOMWindow>(window),
        ExecutionContextLifecycleObserver(&window) {}

  WindowObserver(const WindowObserver&) = delete;
  WindowObserver& operator=(const WindowObserver&) = delete;

  ~WindowObserver() override = default;

  void Trace(Visitor* visitor) const final {
    Supplement<LocalDOMWindow>::Trace(visitor);
    ExecutionContextLifecycleObserver::Trace(visitor);
  }

  // ExecutionContextLifecycleObserver implementation.
  void ContextDestroyed() override {
    if (auto* cache_instance = AudioRendererSinkCache::instance_)
      cache_instance->DropSinksForFrame(DomWindow()->GetLocalFrameToken());
  }
};

const char AudioRendererSinkCache::WindowObserver::kSupplementName[] =
    "AudioRendererSinkCache::WindowObserver";

namespace {

bool SinkIsHealthy(media::AudioRendererSink* sink) {
  return sink->GetOutputDeviceInfo().device_status() ==
         media::OUTPUT_DEVICE_STATUS_OK;
}

}  // namespace

// Cached sink data.
struct AudioRendererSinkCache::CacheEntry {
  LocalFrameToken source_frame_token;
  std::string device_id;
  scoped_refptr<media::AudioRendererSink> sink;  // Sink instance
};

// static
void AudioRendererSinkCache::InstallWindowObserver(LocalDOMWindow& window) {
  if (Supplement<LocalDOMWindow>::From<WindowObserver>(window))
    return;
  Supplement<LocalDOMWindow>::ProvideTo(
      window, MakeGarbageCollected<WindowObserver>(window));
}

AudioRendererSinkCache::AudioRendererSinkCache(
    scoped_refptr<base::SequencedTaskRunner> cleanup_task_runner,
    CreateSinkCallback create_sink_cb,
    base::TimeDelta delete_timeout)
    : cleanup_task_runner_(std::move(cleanup_task_runner)),
      create_sink_cb_(std::move(create_sink_cb)),
      delete_timeout_(delete_timeout) {
  DCHECK(!instance_);
  instance_ = this;
}

AudioRendererSinkCache::~AudioRendererSinkCache() {
  {
    // Stop all of the sinks before destruction.
    base::AutoLock auto_lock(cache_lock_);
    for (auto& entry : cache_)
      entry.sink->Stop();
  }

  DCHECK(instance_ == this);
  instance_ = nullptr;
}

media::OutputDeviceInfo AudioRendererSinkCache::GetSinkInfo(
    const LocalFrameToken& source_frame_token,
    const std::string& device_id) {
  TRACE_EVENT_BEGIN2("audio", "AudioRendererSinkCache::GetSinkInfo",
                     "frame_token", source_frame_token.ToString(), "device id",
                     device_id);
  {
    base::AutoLock auto_lock(cache_lock_);
    auto cache_iter = FindCacheEntry_Locked(source_frame_token, device_id);
    if (cache_iter != cache_.end()) {
      // A matching cached sink is found.
      TRACE_EVENT_END1("audio", "AudioRendererSinkCache::GetSinkInfo", "result",
                       "Cache hit");
      return cache_iter->sink->GetOutputDeviceInfo();
    }
  }

  // No matching sink found, create a new one.
  scoped_refptr<media::AudioRendererSink> sink =
      create_sink_cb_.Run(source_frame_token, device_id);

  MaybeCacheSink(source_frame_token, device_id, sink);

  TRACE_EVENT_END1("audio", "AudioRendererSinkCache::GetSinkInfo", "result",
                   "Cache miss");
  // |sink| is ref-counted, so it's ok if it is removed from cache before we
  // get here.
  return sink->GetOutputDeviceInfo();
}

void AudioRendererSinkCache::DeleteLater(
    scoped_refptr<media::AudioRendererSink> sink) {
  PostDelayedCrossThreadTask(
      *cleanup_task_runner_, FROM_HERE,
      CrossThreadBindOnce(
          &AudioRendererSinkCache::DeleteSink,
          // Unretained is safe here since this is a process-wide
          // singleton and tests will ensure lifetime.
          CrossThreadUnretained(this), WTF::RetainedRef(std::move(sink))),
      delete_timeout_);
}

void AudioRendererSinkCache::DeleteSink(
    const media::AudioRendererSink* sink_ptr) {
  DCHECK(sink_ptr);

  scoped_refptr<media::AudioRendererSink> sink_to_stop;

  {
    base::AutoLock auto_lock(cache_lock_);

    // Looking up the sink by its pointer.
    auto cache_iter = std::ranges::find(
        cache_, sink_ptr, [](const CacheEntry& val) { return val.sink.get(); });

    if (cache_iter == cache_.end())
      return;

    sink_to_stop = cache_iter->sink;
    cache_.erase(cache_iter);
  }  // Lock scope;

  // Stop the sink out of the lock scope.
  if (sink_to_stop) {
    DCHECK_EQ(sink_ptr, sink_to_stop.get());
    sink_to_stop->Stop();
  }
}

AudioRendererSinkCache::CacheContainer::iterator
AudioRendererSinkCache::FindCacheEntry_Locked(
    const LocalFrameToken& source_frame_token,
    const std::string& device_id) {
  cache_lock_.AssertAcquired();
  return std::ranges::find_if(
      cache_, [source_frame_token, &device_id](const CacheEntry& val) {
        if (val.source_frame_token != source_frame_token)
          return false;
        if (media::AudioDeviceDescription::IsDefaultDevice(device_id) &&
            media::AudioDeviceDescription::IsDefaultDevice(val.device_id)) {
          // Both device IDs represent the same default device => do not
          // compare them;
          return true;
        }
        return val.device_id == device_id;
      });
}

void AudioRendererSinkCache::MaybeCacheSink(
    const LocalFrameToken& source_frame_token,
    const std::string& device_id,
    scoped_refptr<media::AudioRendererSink> sink) {
  if (!SinkIsHealthy(sink.get())) {
    TRACE_EVENT_INSTANT0("audio", "MaybeCacheSink: Unhealthy sink",
                         TRACE_EVENT_SCOPE_THREAD);
    // Since |sink| is not cached, we must make sure to Stop it now.
    sink->Stop();
    return;
  }

  CacheEntry cache_entry = {source_frame_token, device_id, std::move(sink)};

  {
    base::AutoLock auto_lock(cache_lock_);
    cache_.push_back(cache_entry);
  }

  DeleteLater(cache_entry.sink);
}

void AudioRendererSinkCache::DropSinksForFrame(
    const LocalFrameToken& source_frame_token) {
  base::AutoLock auto_lock(cache_lock_);
  WTF::EraseIf(cache_, [source_frame_token](const CacheEntry& val) {
    if (val.source_frame_token == source_frame_token) {
      val.sink->Stop();
      return true;
    }
    return false;
  });
}

wtf_size_t AudioRendererSinkCache::GetCacheSizeForTesting() {
  base::AutoLock auto_lock(cache_lock_);
  return cache_.size();
}

}  // namespace blink