File: media_remoter.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 (212 lines) | stat: -rw-r--r-- 7,294 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
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
// 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 "components/mirroring/service/media_remoter.h"

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/notimplemented.h"
#include "components/mirroring/service/remoting_sender.h"
#include "components/mirroring/service/rpc_dispatcher.h"
#include "media/base/media_switches.h"
#include "third_party/openscreen/src/cast/streaming/public/sender.h"

using media::cast::FrameSenderConfig;

namespace mirroring {

MediaRemoter::MediaRemoter(
    Client& client,
    const media::mojom::RemotingSinkMetadata& sink_metadata,
    RpcDispatcher& rpc_dispatcher)
    : client_(client),
      sink_metadata_(sink_metadata),
      rpc_dispatcher_(rpc_dispatcher),
      state_(MIRRORING) {
  client_->ConnectToRemotingSource(
      receiver_.BindNewPipeAndPassRemote(),
      remoting_source_.BindNewPipeAndPassReceiver());
  remoting_source_->OnSinkAvailable(sink_metadata_.Clone());
}

MediaRemoter::~MediaRemoter() {
  // Stop this remoting session if mirroring is stopped during a remoting
  // session. For example, user stops mirroring through the cast dialog or
  // closes the tab.
  Stop(media::mojom::RemotingStopReason::ROUTE_TERMINATED);
}

void MediaRemoter::OnMessageFromSink(const std::vector<uint8_t>& response) {
  remoting_source_->OnMessageFromSink(response);
}

void MediaRemoter::StartRpcMessaging(
    scoped_refptr<media::cast::CastEnvironment> cast_environment,
    std::unique_ptr<openscreen::cast::Sender> audio_sender,
    std::unique_ptr<openscreen::cast::Sender> video_sender,
    std::optional<FrameSenderConfig> audio_config,
    std::optional<FrameSenderConfig> video_config) {
  DCHECK(audio_sender || video_sender);
  DCHECK(audio_config || video_config);
  DCHECK(!openscreen_audio_sender_);
  DCHECK(!openscreen_video_sender_);
  openscreen_audio_sender_ = std::move(audio_sender);
  openscreen_video_sender_ = std::move(video_sender);
  StartRpcMessagingInternal(std::move(cast_environment),
                            std::move(audio_config), std::move(video_config));
}

void MediaRemoter::StartRpcMessagingInternal(
    scoped_refptr<media::cast::CastEnvironment> cast_environment,
    std::optional<FrameSenderConfig> audio_config,
    std::optional<FrameSenderConfig> video_config) {
  DCHECK(!cast_environment_);

  if (state_ != STARTING_REMOTING) {
    openscreen_audio_sender_ = nullptr;
    openscreen_video_sender_ = nullptr;
    return;  // Start operation was canceled.
  }

  // A remoting streaming session started. Start RPC message transport and
  // notify the remoting source to start data streaming.
  cast_environment_ = std::move(cast_environment);
  audio_config_ = std::move(audio_config);
  video_config_ = std::move(video_config);
  rpc_dispatcher_->Subscribe(base::BindRepeating(
      &MediaRemoter::OnMessageFromSink, weak_factory_.GetWeakPtr()));
  state_ = REMOTING_STARTED;
  remoting_source_->OnStarted();
}

void MediaRemoter::OnMirroringResumed(bool is_tab_switching) {
  if (state_ == REMOTING_DISABLED) {
    return;
  }
  DCHECK(state_ == STOPPING_REMOTING ||
         (state_ == MIRRORING && is_tab_switching));

  state_ = MIRRORING;

  if (is_tab_switching) {
    receiver_.reset();
    remoting_source_.reset();
    client_->ConnectToRemotingSource(
        receiver_.BindNewPipeAndPassRemote(),
        remoting_source_.BindNewPipeAndPassReceiver());
  }

  // Notify the remoting source to enable starting media remoting again.
  remoting_source_->OnSinkAvailable(sink_metadata_.Clone());
}

void MediaRemoter::OnRemotingFailed() {
  DCHECK(state_ == STARTING_REMOTING || state_ == REMOTING_STARTED);
  if (state_ == STARTING_REMOTING) {
    remoting_source_->OnStartFailed(
        media::mojom::RemotingStartFailReason::INVALID_ANSWER_MESSAGE);
  }
  state_ = REMOTING_DISABLED;
  remoting_source_->OnSinkGone();
  // Fallback to mirroring.
  client_->RestartMirroringStreaming();
}

void MediaRemoter::Stop(media::mojom::RemotingStopReason reason) {
  if (state_ == STOPPING_REMOTING || state_ == MIRRORING) {
    return;
  }

  // At this point, we are currently remoting and should tear down.
  rpc_dispatcher_->Unsubscribe();
  audio_sender_.reset();
  video_sender_.reset();
  cast_environment_ = nullptr;
  openscreen_audio_sender_ = nullptr;
  openscreen_video_sender_ = nullptr;
  audio_config_ = std::nullopt;
  video_config_ = std::nullopt;

  // Don't change `state_` if remoting is disabled so that it won't attempt to
  // start remoting again after mirroring resumed.
  if (state_ != REMOTING_DISABLED) {
    state_ = STOPPING_REMOTING;
  }
  remoting_source_->OnStopped(reason);
  // Prevent the start of remoting until switching completes.
  remoting_source_->OnSinkGone();
  // Switch to mirroring.
  client_->RestartMirroringStreaming();
}

void MediaRemoter::Start() {
  if (state_ != MIRRORING) {
    VLOG(2) << "Warning: Ignore start request. state=" << state_;
    return;
  }
  state_ = STARTING_REMOTING;
  client_->RequestRemotingStreaming();
}

void MediaRemoter::StartWithPermissionAlreadyGranted() {
  NOTIMPLEMENTED();
}

void MediaRemoter::StartDataStreams(
    mojo::ScopedDataPipeConsumerHandle audio_pipe,
    mojo::ScopedDataPipeConsumerHandle video_pipe,
    mojo::PendingReceiver<media::mojom::RemotingDataStreamSender>
        audio_sender_receiver,
    mojo::PendingReceiver<media::mojom::RemotingDataStreamSender>
        video_sender_receiver) {
  if (state_ != REMOTING_STARTED) {
    return;  // Stop() was called before.
  }

  DCHECK(cast_environment_);
  DCHECK(openscreen_audio_sender_ || openscreen_video_sender_);

  if (audio_pipe.is_valid() && audio_config_ && audio_config_->is_remoting() &&
      openscreen_audio_sender_) {
    // NOTE: use of base::Unretained is safe because we own the sender.
    audio_sender_ = std::make_unique<RemotingSender>(
        cast_environment_, std::move(openscreen_audio_sender_), *audio_config_,
        std::move(audio_pipe), std::move(audio_sender_receiver),
        base::BindOnce(&MediaRemoter::OnRemotingDataStreamError,
                       base::Unretained(this)));
  }

  if (video_pipe.is_valid() && video_config_ && video_config_->is_remoting() &&
      openscreen_video_sender_) {
    // NOTE: use of base::Unretained is safe because we own the sender.
    video_sender_ = std::make_unique<RemotingSender>(
        cast_environment_, std::move(openscreen_video_sender_), *video_config_,
        std::move(video_pipe), std::move(video_sender_receiver),
        base::BindOnce(&MediaRemoter::OnRemotingDataStreamError,
                       base::Unretained(this)));
  }
}

void MediaRemoter::SendMessageToSink(const std::vector<uint8_t>& message) {
  if (state_ != REMOTING_STARTED) {
    return;
  }
  rpc_dispatcher_->SendOutboundMessage(message);
}

void MediaRemoter::EstimateTransmissionCapacity(
    media::mojom::Remoter::EstimateTransmissionCapacityCallback callback) {
  NOTIMPLEMENTED();
  std::move(callback).Run(0);
}

void MediaRemoter::OnRemotingDataStreamError() {
  if (state_ != REMOTING_STARTED) {
    return;
  }
  Stop(media::mojom::RemotingStopReason::DATA_SEND_FAILED);
  state_ = REMOTING_DISABLED;
}

}  // namespace mirroring