summaryrefslogtreecommitdiff
path: root/chromium/media/remoting/mock_receiver_controller.cc
blob: b0e742d763524d39972ee7bddd36585a8f36b79d (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
// 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/mock_receiver_controller.h"

#include "base/check.h"
#include "media/mojo/common/mojo_decoder_buffer_converter.h"
#include "media/remoting/test_utils.h"
#include "mojo/public/cpp/system/data_pipe.h"

namespace media {
namespace remoting {

MockRemotee::MockRemotee() = default;

MockRemotee::~MockRemotee() = default;

void MockRemotee::BindMojoReceiver(mojo::PendingReceiver<Remotee> receiver) {
  DCHECK(receiver);
  receiver_.Bind(std::move(receiver));
}

void MockRemotee::SendAudioFrame(uint32_t frame_count,
                                 scoped_refptr<DecoderBuffer> buffer) {
  mojom::DecoderBufferPtr mojo_buffer =
      audio_buffer_writer_->WriteDecoderBuffer(std::move(buffer));
  audio_stream_->ReceiveFrame(frame_count, std::move(mojo_buffer));
}

void MockRemotee::SendVideoFrame(uint32_t frame_count,
                                 scoped_refptr<DecoderBuffer> buffer) {
  mojom::DecoderBufferPtr mojo_buffer =
      video_buffer_writer_->WriteDecoderBuffer(std::move(buffer));
  video_stream_->ReceiveFrame(frame_count, std::move(mojo_buffer));
}

void MockRemotee::OnRemotingSinkReady(
    mojo::PendingRemote<mojom::RemotingSink> remoting_sink) {
  DCHECK(remoting_sink);
  remoting_sink_.Bind(std::move(remoting_sink));
}

void MockRemotee::SendMessageToSource(const std::vector<uint8_t>& message) {}

void MockRemotee::StartDataStreams(
    mojo::PendingRemote<mojom::RemotingDataStreamReceiver> audio_stream,
    mojo::PendingRemote<mojom::RemotingDataStreamReceiver> video_stream) {
  if (audio_stream.is_valid()) {
    // Initialize data pipe for audio data stream receiver.
    audio_stream_.Bind(std::move(audio_stream));
    mojo::ScopedDataPipeConsumerHandle audio_data_pipe;
    audio_buffer_writer_ = MojoDecoderBufferWriter::Create(
        GetDefaultDecoderBufferConverterCapacity(DemuxerStream::AUDIO),
        &audio_data_pipe);
    audio_stream_->InitializeDataPipe(std::move(audio_data_pipe));
  }

  if (video_stream.is_valid()) {
    // Initialize data pipe for video data stream receiver.
    video_stream_.Bind(std::move(video_stream));
    mojo::ScopedDataPipeConsumerHandle video_data_pipe;
    video_buffer_writer_ = MojoDecoderBufferWriter::Create(
        GetDefaultDecoderBufferConverterCapacity(DemuxerStream::VIDEO),
        &video_data_pipe);
    video_stream_->InitializeDataPipe(std::move(video_data_pipe));
  }
}

void MockRemotee::OnFlushUntil(uint32_t audio_count, uint32_t video_count) {
  flush_audio_count_ = audio_count;
  flush_video_count_ = video_count;

  if (audio_stream_.is_bound()) {
    audio_stream_->FlushUntil(audio_count);
  }
  if (video_stream_.is_bound()) {
    video_stream_->FlushUntil(video_count);
  }
}

void MockRemotee::OnVideoNaturalSizeChange(const gfx::Size& size) {
  DCHECK(!size.IsEmpty());
  changed_size_ = size;
}

void MockRemotee::Reset() {
  audio_stream_.reset();
  video_stream_.reset();
  receiver_.reset();
  remoting_sink_.reset();
}

// static
MockReceiverController* MockReceiverController::GetInstance() {
  static base::NoDestructor<MockReceiverController> controller;
  ResetForTesting(controller.get());
  controller->mock_remotee_->Reset();
  return controller.get();
}

MockReceiverController::MockReceiverController()
    : mock_remotee_(new MockRemotee()) {
  // Overwrites |rpc_broker_|.
  rpc_broker_.SetMessageCallbackForTesting(base::BindRepeating(
      &MockReceiverController::OnSendRpc, base::Unretained(this)));
}

MockReceiverController::~MockReceiverController() = default;

void MockReceiverController::OnSendRpc(
    std::unique_ptr<std::vector<uint8_t>> message) {
  std::vector<uint8_t> binary_message = *message;
  ReceiverController::OnMessageFromSource(binary_message);
}

}  // namespace remoting
}  // namespace media