1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/remoting/mock_receiver_controller.h"
7 #include "base/check.h"
8 #include "base/no_destructor.h"
9 #include "media/mojo/common/mojo_decoder_buffer_converter.h"
10 #include "media/remoting/test_utils.h"
11 #include "mojo/public/cpp/system/data_pipe.h"
16 MockRemotee::MockRemotee() = default;
18 MockRemotee::~MockRemotee() = default;
20 void MockRemotee::BindMojoReceiver(mojo::PendingReceiver<Remotee> receiver) {
22 receiver_.Bind(std::move(receiver));
25 void MockRemotee::SendAudioFrame(uint32_t frame_count,
26 scoped_refptr<DecoderBuffer> buffer) {
27 mojom::DecoderBufferPtr mojo_buffer =
28 audio_buffer_writer_->WriteDecoderBuffer(std::move(buffer));
29 audio_stream_->ReceiveFrame(frame_count, std::move(mojo_buffer));
32 void MockRemotee::SendVideoFrame(uint32_t frame_count,
33 scoped_refptr<DecoderBuffer> buffer) {
34 mojom::DecoderBufferPtr mojo_buffer =
35 video_buffer_writer_->WriteDecoderBuffer(std::move(buffer));
36 video_stream_->ReceiveFrame(frame_count, std::move(mojo_buffer));
39 void MockRemotee::OnRemotingSinkReady(
40 mojo::PendingRemote<mojom::RemotingSink> remoting_sink) {
41 DCHECK(remoting_sink);
42 remoting_sink_.Bind(std::move(remoting_sink));
45 void MockRemotee::SendMessageToSource(const std::vector<uint8_t>& message) {}
47 void MockRemotee::StartDataStreams(
48 mojo::PendingRemote<mojom::RemotingDataStreamReceiver> audio_stream,
49 mojo::PendingRemote<mojom::RemotingDataStreamReceiver> video_stream) {
50 if (audio_stream.is_valid()) {
51 // Initialize data pipe for audio data stream receiver.
52 audio_stream_.Bind(std::move(audio_stream));
53 mojo::ScopedDataPipeConsumerHandle audio_data_pipe;
54 audio_buffer_writer_ = MojoDecoderBufferWriter::Create(
55 GetDefaultDecoderBufferConverterCapacity(DemuxerStream::AUDIO),
57 audio_stream_->InitializeDataPipe(std::move(audio_data_pipe));
60 if (video_stream.is_valid()) {
61 // Initialize data pipe for video data stream receiver.
62 video_stream_.Bind(std::move(video_stream));
63 mojo::ScopedDataPipeConsumerHandle video_data_pipe;
64 video_buffer_writer_ = MojoDecoderBufferWriter::Create(
65 GetDefaultDecoderBufferConverterCapacity(DemuxerStream::VIDEO),
67 video_stream_->InitializeDataPipe(std::move(video_data_pipe));
71 void MockRemotee::OnFlushUntil(uint32_t audio_count, uint32_t video_count) {
72 flush_audio_count_ = audio_count;
73 flush_video_count_ = video_count;
75 if (audio_stream_.is_bound()) {
76 audio_stream_->FlushUntil(audio_count);
78 if (video_stream_.is_bound()) {
79 video_stream_->FlushUntil(video_count);
83 void MockRemotee::OnVideoNaturalSizeChange(const gfx::Size& size) {
84 DCHECK(!size.IsEmpty());
88 void MockRemotee::Reset() {
89 audio_stream_.reset();
90 video_stream_.reset();
92 remoting_sink_.reset();
96 MockReceiverController* MockReceiverController::GetInstance() {
97 static base::NoDestructor<MockReceiverController> controller;
98 ResetForTesting(controller.get());
99 controller->mock_remotee_->Reset();
100 return controller.get();
103 MockReceiverController::MockReceiverController()
104 : mock_remotee_(new MockRemotee()) {
105 // Overwrites |rpc_messenger_|.
106 rpc_messenger_.set_send_message_cb_for_testing(
107 [this](std::vector<uint8_t> message) { OnSendRpc(message); });
110 MockReceiverController::~MockReceiverController() = default;
112 void MockReceiverController::OnSendRpc(std::vector<uint8_t> message) {
113 ReceiverController::OnMessageFromSource(message);
116 } // namespace remoting