summaryrefslogtreecommitdiff
path: root/chromium/media/remoting/receiver_controller.cc
blob: 549087cf39150fc1bb7bc87c501ff973edb726a0 (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
// Copyright 2020 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 "media/remoting/receiver_controller.h"

#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"

namespace media {
namespace remoting {

// static
ReceiverController* ReceiverController::GetInstance() {
  static base::NoDestructor<ReceiverController> controller;
  return controller.get();
}

ReceiverController::ReceiverController()
    : rpc_broker_(base::BindRepeating(&ReceiverController::OnSendRpc,
                                      base::Unretained(this))),
      main_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}

ReceiverController::~ReceiverController() = default;

void ReceiverController::Initialize(
    mojo::PendingRemote<mojom::Remotee> remotee) {
  DCHECK(main_task_runner_->BelongsToCurrentThread());
  DCHECK(!media_remotee_.is_bound());
  media_remotee_.Bind(std::move(remotee));

  // Calling NotifyRemotingSinkReady() to notify the host that RemotingSink is
  // ready.
  media_remotee_->OnRemotingSinkReady(receiver_.BindNewPipeAndPassRemote());
}

void ReceiverController::OnRendererFlush(uint32_t audio_count,
                                         uint32_t video_count) {
  if (!main_task_runner_->BelongsToCurrentThread()) {
    // |this| is a singleton per process, it would be safe to use
    // base::Unretained() here.
    main_task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&ReceiverController::OnRendererFlush,
                       base::Unretained(this), audio_count, video_count));
    return;
  }

  if (media_remotee_.is_bound())
    media_remotee_->OnFlushUntil(audio_count, video_count);
}

void ReceiverController::OnVideoNaturalSizeChange(const gfx::Size& size) {
  if (!main_task_runner_->BelongsToCurrentThread()) {
    // |this| is a singleton per process, it would be safe to use
    // base::Unretained() here.
    main_task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&ReceiverController::OnVideoNaturalSizeChange,
                                  base::Unretained(this), size));
    return;
  }

  if (media_remotee_.is_bound())
    media_remotee_->OnVideoNaturalSizeChange(size);
}

void ReceiverController::StartDataStreams(
    mojo::PendingRemote<::media::mojom::RemotingDataStreamReceiver>
        audio_stream,
    mojo::PendingRemote<::media::mojom::RemotingDataStreamReceiver>
        video_stream) {
  if (!main_task_runner_->BelongsToCurrentThread()) {
    // |this| is a singleton per process, it would be safe to use
    // base::Unretained() here.
    main_task_runner_->PostTask(
        FROM_HERE,
        base::BindOnce(&ReceiverController::StartDataStreams,
                       base::Unretained(this), std::move(audio_stream),
                       std::move(video_stream)));
    return;
  }
  if (media_remotee_.is_bound()) {
    media_remotee_->StartDataStreams(std::move(audio_stream),
                                     std::move(video_stream));
  }
}

void ReceiverController::OnMessageFromSource(
    const std::vector<uint8_t>& message) {
  DCHECK(main_task_runner_->BelongsToCurrentThread());
  auto rpc_message = std::make_unique<pb::RpcMessage>(pb::RpcMessage());
  if (!rpc_message->ParseFromArray(message.data(), message.size()))
    return;

  rpc_broker_.ProcessMessageFromRemote(std::move(rpc_message));
}

void ReceiverController::OnSendRpc(
    std::unique_ptr<std::vector<uint8_t>> message) {
  if (!main_task_runner_->BelongsToCurrentThread()) {
    // |this| is a singleton per process, it would be safe to use
    // base::Unretained() here.
    main_task_runner_->PostTask(
        FROM_HERE, base::BindOnce(&ReceiverController::OnSendRpc,
                                  base::Unretained(this), std::move(message)));
    return;
  }

  DCHECK(media_remotee_.is_bound());
  std::vector<uint8_t> binary_message = *message;
  if (media_remotee_.is_bound())
    media_remotee_->SendMessageToSource(binary_message);
}

}  // namespace remoting
}  // namespace media