summaryrefslogtreecommitdiff
path: root/chromium/content/browser/renderer_host/media/old_render_frame_audio_output_stream_factory.cc
blob: df02e54c4e6da40fd95d0d81e52bb86bfede6ded (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
// Copyright 2017 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 "content/browser/renderer_host/media/old_render_frame_audio_output_stream_factory.h"

#include <memory>
#include <utility>

#include "base/metrics/histogram_macros.h"
#include "base/task_runner_util.h"
#include "content/browser/renderer_host/media/audio_output_authorization_handler.h"
#include "content/browser/renderer_host/media/audio_output_stream_observer_impl.h"
#include "content/browser/renderer_host/media/renderer_audio_output_stream_factory_context.h"
#include "content/public/browser/render_frame_host.h"
#include "media/base/audio_parameters.h"
#include "media/mojo/services/mojo_audio_output_stream_provider.h"
#include "mojo/public/cpp/bindings/message.h"

namespace content {

// static
std::unique_ptr<RenderFrameAudioOutputStreamFactoryHandle,
                BrowserThread::DeleteOnIOThread>
RenderFrameAudioOutputStreamFactoryHandle::CreateFactory(
    RendererAudioOutputStreamFactoryContext* context,
    int render_frame_id,
    mojom::RendererAudioOutputStreamFactoryRequest request) {
  std::unique_ptr<RenderFrameAudioOutputStreamFactoryHandle,
                  BrowserThread::DeleteOnIOThread>
      handle(new RenderFrameAudioOutputStreamFactoryHandle(context,
                                                           render_frame_id));
  // Unretained is safe since |*handle| must be posted to the IO thread prior to
  // deletion.
  BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      base::BindOnce(&RenderFrameAudioOutputStreamFactoryHandle::Init,
                     base::Unretained(handle.get()), std::move(request)));
  return handle;
}

RenderFrameAudioOutputStreamFactoryHandle::
    ~RenderFrameAudioOutputStreamFactoryHandle() {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
}

RenderFrameAudioOutputStreamFactoryHandle::
    RenderFrameAudioOutputStreamFactoryHandle(
        RendererAudioOutputStreamFactoryContext* context,
        int render_frame_id)
    : impl_(render_frame_id, context), binding_(&impl_) {}

void RenderFrameAudioOutputStreamFactoryHandle::Init(
    mojom::RendererAudioOutputStreamFactoryRequest request) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  binding_.Bind(std::move(request));
}

OldRenderFrameAudioOutputStreamFactory::OldRenderFrameAudioOutputStreamFactory(
    int render_frame_id,
    RendererAudioOutputStreamFactoryContext* context)
    : render_frame_id_(render_frame_id),
      context_(context),
      weak_ptr_factory_(this) {
  DCHECK(context_);
}

OldRenderFrameAudioOutputStreamFactory::
    ~OldRenderFrameAudioOutputStreamFactory() {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  UMA_HISTOGRAM_EXACT_LINEAR("Media.Audio.OutputStreamsCanceledByBrowser",
                             stream_providers_.size(), 50);
  // Make sure to close all streams.
  stream_providers_.clear();
}

void OldRenderFrameAudioOutputStreamFactory::RequestDeviceAuthorization(
    media::mojom::AudioOutputStreamProviderRequest stream_provider_request,
    int32_t session_id,
    const std::string& device_id,
    RequestDeviceAuthorizationCallback callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  const base::TimeTicks auth_start_time = base::TimeTicks::Now();

  context_->RequestDeviceAuthorization(
      render_frame_id_, session_id, device_id,
      base::BindOnce(
          &OldRenderFrameAudioOutputStreamFactory::AuthorizationCompleted,
          weak_ptr_factory_.GetWeakPtr(), auth_start_time,
          std::move(stream_provider_request), std::move(callback)));
}

void OldRenderFrameAudioOutputStreamFactory::AuthorizationCompleted(
    base::TimeTicks auth_start_time,
    media::mojom::AudioOutputStreamProviderRequest request,
    RequestDeviceAuthorizationCallback callback,
    media::OutputDeviceStatus status,
    const media::AudioParameters& params,
    const std::string& raw_device_id,
    const std::string& device_id_for_renderer) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  AudioOutputAuthorizationHandler::UMALogDeviceAuthorizationTime(
      auth_start_time);

  if (status != media::OUTPUT_DEVICE_STATUS_OK) {
    std::move(callback).Run(media::OutputDeviceStatus(status),
                            media::AudioParameters::UnavailableDeviceParams(),
                            std::string());
    return;
  }

  int stream_id = next_stream_id_++;
  std::unique_ptr<media::mojom::AudioOutputStreamObserver> observer =
      std::make_unique<AudioOutputStreamObserverImpl>(
          context_->GetRenderProcessId(), render_frame_id_, stream_id);
  // Since |context_| outlives |this| and |this| outlives |stream_providers_|,
  // unretained is safe.
  stream_providers_.insert(
      std::make_unique<media::MojoAudioOutputStreamProvider>(
          std::move(request),
          base::BindOnce(
              &RendererAudioOutputStreamFactoryContext::CreateDelegate,
              base::Unretained(context_), raw_device_id, render_frame_id_,
              stream_id),
          base::BindOnce(&OldRenderFrameAudioOutputStreamFactory::RemoveStream,
                         base::Unretained(this)),
          std::move(observer)));

  std::move(callback).Run(media::OutputDeviceStatus(status), params,
                          device_id_for_renderer);
}

void OldRenderFrameAudioOutputStreamFactory::RemoveStream(
    media::mojom::AudioOutputStreamProvider* stream_provider) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  stream_providers_.erase(stream_provider);
}

}  // namespace content