Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / media / remoting / mock_receiver_controller.cc
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.
4
5 #include "media/remoting/mock_receiver_controller.h"
6
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"
12
13 namespace media {
14 namespace remoting {
15
16 MockRemotee::MockRemotee() = default;
17
18 MockRemotee::~MockRemotee() = default;
19
20 void MockRemotee::BindMojoReceiver(mojo::PendingReceiver<Remotee> receiver) {
21   DCHECK(receiver);
22   receiver_.Bind(std::move(receiver));
23 }
24
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));
30 }
31
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));
37 }
38
39 void MockRemotee::OnRemotingSinkReady(
40     mojo::PendingRemote<mojom::RemotingSink> remoting_sink) {
41   DCHECK(remoting_sink);
42   remoting_sink_.Bind(std::move(remoting_sink));
43 }
44
45 void MockRemotee::SendMessageToSource(const std::vector<uint8_t>& message) {}
46
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),
56         &audio_data_pipe);
57     audio_stream_->InitializeDataPipe(std::move(audio_data_pipe));
58   }
59
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),
66         &video_data_pipe);
67     video_stream_->InitializeDataPipe(std::move(video_data_pipe));
68   }
69 }
70
71 void MockRemotee::OnFlushUntil(uint32_t audio_count, uint32_t video_count) {
72   flush_audio_count_ = audio_count;
73   flush_video_count_ = video_count;
74
75   if (audio_stream_.is_bound()) {
76     audio_stream_->FlushUntil(audio_count);
77   }
78   if (video_stream_.is_bound()) {
79     video_stream_->FlushUntil(video_count);
80   }
81 }
82
83 void MockRemotee::OnVideoNaturalSizeChange(const gfx::Size& size) {
84   DCHECK(!size.IsEmpty());
85   changed_size_ = size;
86 }
87
88 void MockRemotee::Reset() {
89   audio_stream_.reset();
90   video_stream_.reset();
91   receiver_.reset();
92   remoting_sink_.reset();
93 }
94
95 // static
96 MockReceiverController* MockReceiverController::GetInstance() {
97   static base::NoDestructor<MockReceiverController> controller;
98   ResetForTesting(controller.get());
99   controller->mock_remotee_->Reset();
100   return controller.get();
101 }
102
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); });
108 }
109
110 MockReceiverController::~MockReceiverController() = default;
111
112 void MockReceiverController::OnSendRpc(std::vector<uint8_t> message) {
113   ReceiverController::OnMessageFromSource(message);
114 }
115
116 }  // namespace remoting
117 }  // namespace media