File: audio_playout_stats.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 (104 lines) | stat: -rw-r--r-- 3,855 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
// Copyright 2024 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/webaudio/audio_playout_stats.h"

#include "base/numerics/safe_conversions.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_object_builder.h"
#include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"

namespace blink {

AudioPlayoutStats::AudioPlayoutStats(AudioContext* context)
    : context_(context) {}

DOMHighResTimeStamp AudioPlayoutStats::fallbackFramesDuration(
    ScriptState* script_state) {
  MaybeUpdateStats(script_state);
  return stats_.glitch_frames_duration().InMillisecondsF();
}

uint32_t AudioPlayoutStats::fallbackFramesEvents(ScriptState* script_state) {
  MaybeUpdateStats(script_state);
  return base::saturated_cast<uint32_t>(stats_.glitch_event_count());
}

DOMHighResTimeStamp AudioPlayoutStats::totalFramesDuration(
    ScriptState* script_state) {
  MaybeUpdateStats(script_state);
  return (stats_.glitch_frames_duration() + stats_.observed_frames_duration())
      .InMillisecondsF();
}

DOMHighResTimeStamp AudioPlayoutStats::averageLatency(
    ScriptState* script_state) {
  MaybeUpdateStats(script_state);
  return stats_.average_latency().InMillisecondsF();
}

DOMHighResTimeStamp AudioPlayoutStats::minimumLatency(
    ScriptState* script_state) {
  MaybeUpdateStats(script_state);
  return stats_.min_latency().InMillisecondsF();
}

DOMHighResTimeStamp AudioPlayoutStats::maximumLatency(
    ScriptState* script_state) {
  MaybeUpdateStats(script_state);
  return stats_.max_latency().InMillisecondsF();
}

void AudioPlayoutStats::resetLatency(ScriptState* script_state) {
  MaybeUpdateStats(script_state);
  // Reset the latency stats correctly by having a temporary stats object absorb
  // them.
  AudioFrameStatsAccumulator temp_stats;
  temp_stats.Absorb(stats_);
}

ScriptObject AudioPlayoutStats::toJSON(ScriptState* script_state) {
  V8ObjectBuilder result(script_state);
  result.AddNumber("fallbackFramesDuration",
                   fallbackFramesDuration(script_state));
  result.AddNumber("fallbackFramesEvents", fallbackFramesEvents(script_state));
  result.AddNumber("totalFramesDuration", totalFramesDuration(script_state));
  result.AddNumber("averageLatency", averageLatency(script_state));
  result.AddNumber("minimumLatency", minimumLatency(script_state));
  result.AddNumber("maximumLatency", maximumLatency(script_state));
  return result.ToScriptObject();
}

void AudioPlayoutStats::Trace(Visitor* visitor) const {
  visitor->Trace(context_);
  ScriptWrappable::Trace(visitor);
}

void AudioPlayoutStats::MaybeUpdateStats(ScriptState* script_state) {
  // We cache the stats in |stats_| in order to preserve the JavaScript
  // run-to-completion semantics. If the cached stats were updated in the
  // current task, we should not update them again.
  if (!context_ || stats_are_from_current_task_) {
    return;
  }
  // Get the latest stats, and remember that we now have stats from the current
  // task.
  context_->TransferAudioFrameStatsTo(stats_);
  stats_are_from_current_task_ = true;

  // Queue a microtask to let us know when we are on a new task again, ensuring
  // that we get fresh stats in the next task execution cycle.
  ToEventLoop(script_state)
      .EnqueueMicrotask(WTF::BindOnce(&AudioPlayoutStats::OnMicrotask,
                                      WrapWeakPersistent(this)));
}

void AudioPlayoutStats::OnMicrotask() {
  // Since this was queued on the older task when we got the current |stats_|,
  // the stats are no longer from the current task.
  stats_are_from_current_task_ = false;
}

}  // namespace blink