summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/mediastream/audio_service_audio_processor_proxy.cc
blob: aa8c19862a3db9b0c243e7821084d0dcbaa19995 (plain)
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
// Copyright 2018 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.

#include "third_party/blink/renderer/platform/mediastream/audio_service_audio_processor_proxy.h"

#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <limits>
#include <string>
#include <utility>
#include <vector>

#include "base/single_thread_task_runner.h"
#include "base/task/post_task.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/mediastream/aec_dump_agent_impl.h"
#include "third_party/blink/renderer/platform/scheduler/public/worker_pool.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"

namespace blink {

namespace {
constexpr base::TimeDelta kMaxStatsInterval = base::TimeDelta::FromSeconds(5);
constexpr base::TimeDelta kMinStatsInterval =
    base::TimeDelta::FromMilliseconds(100);
}  // namespace

AudioServiceAudioProcessorProxy::AudioServiceAudioProcessorProxy(
    scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner)
    : main_thread_runner_(std::move(main_thread_task_runner)),
      target_stats_interval_(kMaxStatsInterval) {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
}

AudioServiceAudioProcessorProxy::~AudioServiceAudioProcessorProxy() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  Stop();
}

void AudioServiceAudioProcessorProxy::Stop() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());

  aec_dump_agent_impl_.reset();

  if (processor_controls_) {
    processor_controls_->StopEchoCancellationDump();
    processor_controls_ = nullptr;
  }

  stats_update_timer_.Stop();
}

void AudioServiceAudioProcessorProxy::OnStartDump(base::File dump_file) {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  DCHECK(dump_file.IsValid());
  if (processor_controls_) {
    processor_controls_->StartEchoCancellationDump(std::move(dump_file));
  } else {
    // Post the file close to avoid blocking the main thread.
    worker_pool::PostTask(
        FROM_HERE, {base::TaskPriority::LOWEST, base::MayBlock()},
        CrossThreadBindOnce([](base::File) {}, std::move(dump_file)));
  }
}

void AudioServiceAudioProcessorProxy::OnStopDump() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  if (processor_controls_)
    processor_controls_->StopEchoCancellationDump();
}

void AudioServiceAudioProcessorProxy::SetControls(
    media::AudioProcessorControls* controls) {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  DCHECK(!processor_controls_);
  DCHECK(controls);
  processor_controls_ = controls;

  // Initialize the stats interval request timer with the current time ticks,
  // so it makes any sort of sense.
  last_stats_request_time_ = base::TimeTicks::Now();
  stats_update_timer_.SetTaskRunner(main_thread_runner_);
  RescheduleStatsUpdateTimer(target_stats_interval_);

  // Can be null in unit tests. That's okay.
  aec_dump_agent_impl_ = AecDumpAgentImpl::Create(this);
}

webrtc::AudioProcessorInterface::AudioProcessorStatistics
AudioServiceAudioProcessorProxy::GetStats(bool has_remote_tracks) {
  base::AutoLock lock(stats_lock_);
  // Find some reasonable update interval, rounding down to the nearest one
  // tenth of a second. The update interval is chosen so that the rate of
  // updates we get from the audio service is near the interval at which the
  // client calls GetStats.
  const auto rounded = [](base::TimeDelta d) {
    return d - (d % base::TimeDelta::FromMilliseconds(100));
  };
  const auto now = base::TimeTicks::Now();
  const auto request_interval = rounded(now - last_stats_request_time_);
  target_stats_interval_ = std::max(
      kMinStatsInterval, std::min(request_interval, kMaxStatsInterval));

  last_stats_request_time_ = now;

  // |has_remote_tracks| is ignored, since the remote AudioProcessingModule gets
  // this information more directly.
  return latest_stats_;
}

void AudioServiceAudioProcessorProxy::RescheduleStatsUpdateTimer(
    base::TimeDelta new_interval) {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  // Unretained is safe since |this| owns |stats_update_timer_|.
  stats_update_timer_.Start(
      FROM_HERE, new_interval,
      WTF::BindRepeating(&AudioServiceAudioProcessorProxy::RequestStats,
                         WTF::Unretained(this)));
}

void AudioServiceAudioProcessorProxy::RequestStats() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  if (processor_controls_) {
    processor_controls_->GetStats(
        WTF::Bind(&AudioServiceAudioProcessorProxy::UpdateStats,
                  weak_ptr_factory_.GetWeakPtr()));
  }
}

void AudioServiceAudioProcessorProxy::UpdateStats(
    const AudioProcessorStatistics& new_stats) {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  base::TimeDelta target_interval;
  {
    base::AutoLock lock(stats_lock_);
    latest_stats_ = new_stats;
    target_interval = target_stats_interval_;
  }

  if (target_interval != stats_update_timer_.GetCurrentDelay()) {
    RescheduleStatsUpdateTimer(target_interval);
  }
}

}  // namespace blink