File: audio_context_manager_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 4,726 bytes parent folder | download | duplicates (3)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/media/webaudio/audio_context_manager_impl.h"

#include <utility>

#include "base/metrics/histogram_macros.h"
#include "base/time/default_tick_clock.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"

namespace content {

namespace {

// Returns the time in milleseconds following these rules:
//  - if the time is below 10 seconds, return the raw value;
//  - otherwise, return the value rounded to the closes second.
int64_t GetBucketedTimeInMilliseconds(const base::TimeDelta& time) {
  if (time.InMilliseconds() < 10 * base::Time::kMillisecondsPerSecond)
    return time.InMilliseconds();
  return time.InSeconds() * base::Time::kMillisecondsPerSecond;
}

}  // namespace

void AudioContextManagerImpl::Create(
    RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<blink::mojom::AudioContextManager> receiver) {
  CHECK(render_frame_host);

  // The object is bound to the lifetime of |render_frame_host| and the mojo
  // connection. See DocumentService for details.
  new AudioContextManagerImpl(*render_frame_host, std::move(receiver));
}

AudioContextManagerImpl& AudioContextManagerImpl::CreateForTesting(
    RenderFrameHost& render_frame_host,
    mojo::PendingReceiver<blink::mojom::AudioContextManager> receiver) {
  return *new AudioContextManagerImpl(render_frame_host, std::move(receiver));
}

AudioContextManagerImpl::AudioContextManagerImpl(
    RenderFrameHost& render_frame_host,
    mojo::PendingReceiver<blink::mojom::AudioContextManager> receiver)
    : DocumentService(render_frame_host, std::move(receiver)),
      clock_(base::DefaultTickClock::GetInstance()) {}

AudioContextManagerImpl::~AudioContextManagerImpl() {
  // Takes care pending "audible start" times.
  base::TimeTicks now = clock_->NowTicks();
  for (const auto& entry : pending_audible_durations_) {
    if (!entry.second.is_null())
      RecordAudibleTime(now - entry.second);
  }
  pending_audible_durations_.clear();
  UMA_HISTOGRAM_EXACT_LINEAR("WebAudio.AudioContext.ConcurrentAudioContexts",
                             max_concurrent_audio_contexts_,
                             /*exclusive_max=*/101);
}

void AudioContextManagerImpl::AudioContextAudiblePlaybackStarted(
    uint32_t audio_context_id) {
  DCHECK(pending_audible_durations_[audio_context_id].is_null());

  // Keeps track of the start audible time for this context.
  pending_audible_durations_[audio_context_id] = clock_->NowTicks();

  static_cast<RenderFrameHostImpl&>(render_frame_host())
      .AudioContextPlaybackStarted(audio_context_id);
}

void AudioContextManagerImpl::AudioContextAudiblePlaybackStopped(
    uint32_t audio_context_id) {
  base::TimeTicks then = pending_audible_durations_[audio_context_id];
  DCHECK(!then.is_null());

  RecordAudibleTime(clock_->NowTicks() - then);

  // Resets the context slot because the context is not audible.
  pending_audible_durations_[audio_context_id] = base::TimeTicks();

  static_cast<RenderFrameHostImpl&>(render_frame_host())
      .AudioContextPlaybackStopped(audio_context_id);
}

void AudioContextManagerImpl::RecordAudibleTime(base::TimeDelta audible_time) {
  DCHECK(!audible_time.is_zero());

  ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get();
  DCHECK(ukm_recorder);

  // AudioContextManagerImpl is created when the AudioContext starts running.
  // As the AudioContext is suspended during prerendering even if the autoplay
  // is permitted, it is ensured that the lifecycle state could not be
  // kPrerendering here. This assumption is needed to record UKMs below.
  CHECK(!render_frame_host().IsInLifecycleState(
      RenderFrameHost::LifecycleState::kPrerendering));

  ukm::builders::Media_WebAudio_AudioContext_AudibleTime(
      render_frame_host().GetPageUkmSourceId())
      .SetIsMainFrame(render_frame_host().IsInPrimaryMainFrame())
      .SetAudibleTime(GetBucketedTimeInMilliseconds(audible_time))
      .Record(ukm_recorder);
}

void AudioContextManagerImpl::AudioContextCreated(uint32_t audio_context_id) {
  concurrent_audio_context_ids_.insert(audio_context_id);
  max_concurrent_audio_contexts_ = std::max(
      max_concurrent_audio_contexts_, concurrent_audio_context_ids_.size());
}

void AudioContextManagerImpl::AudioContextClosed(uint32_t audio_context_id) {
  concurrent_audio_context_ids_.erase(audio_context_id);
}

}  // namespace content