OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
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/fake_remoting_controller.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/callback.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/threading/thread_task_runner_handle.h" | |
12 #include "media/remoting/remoting_source_impl.h" | |
13 #include "media/remoting/rpc/proto_utils.h" | |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace media { | |
18 | |
19 FakeRemotingDataStreamSender::FakeRemotingDataStreamSender( | |
20 mojom::RemotingDataStreamSenderRequest request, | |
21 mojo::ScopedDataPipeConsumerHandle consumer_handle) | |
22 : binding_(this, std::move(request)), | |
23 consumer_handle_(std::move(consumer_handle)), | |
24 consume_data_chunk_count_(0), | |
25 send_frame_count_(0), | |
26 cancel_in_flight_count_(0) {} | |
27 | |
28 FakeRemotingDataStreamSender::~FakeRemotingDataStreamSender() = default; | |
29 | |
30 void FakeRemotingDataStreamSender::ResetHistory() { | |
31 consume_data_chunk_count_ = 0; | |
32 send_frame_count_ = 0; | |
33 cancel_in_flight_count_ = 0; | |
34 next_frame_data_.resize(0); | |
35 received_frame_list.clear(); | |
36 } | |
37 | |
38 bool FakeRemotingDataStreamSender::ValidateFrameBuffer(size_t index, | |
39 size_t size, | |
40 bool key_frame, | |
41 int pts_ms) { | |
42 if (index >= received_frame_list.size()) { | |
43 VLOG(1) << "There is no such frame"; | |
44 return false; | |
45 } | |
46 | |
47 const std::vector<uint8_t>& data = received_frame_list[index]; | |
48 scoped_refptr<::media::DecoderBuffer> media_buffer = | |
49 remoting::ByteArrayToDecoderBuffer(data.data(), data.size()); | |
50 | |
51 // Checks if pts is correct or not | |
52 if (media_buffer->timestamp().InMilliseconds() != pts_ms) { | |
53 VLOG(1) << "Pts should be:" << pts_ms << "(" | |
54 << media_buffer->timestamp().InMilliseconds() << ")"; | |
55 return false; | |
56 } | |
57 | |
58 // Checks if key frame is set correct or not | |
59 if (media_buffer->is_key_frame() != key_frame) { | |
60 VLOG(1) << "Key frame should be:" << key_frame << "(" | |
61 << media_buffer->is_key_frame() << ")"; | |
62 return false; | |
63 } | |
64 | |
65 // Checks if frame buffer size is correct or not | |
66 if (media_buffer->data_size() != size) { | |
67 VLOG(1) << "Buffer size should be:" << size << "(" | |
68 << media_buffer->data_size() << ")"; | |
69 return false; | |
70 } | |
71 | |
72 // Checks if frame buffer is correct or not. | |
73 bool return_value = true; | |
74 const uint8_t* buffer = media_buffer->data(); | |
75 for (size_t i = 0; i < media_buffer->data_size(); ++i) { | |
76 uint32_t value = static_cast<uint32_t>(i & 0xFF); | |
77 if (value != static_cast<uint32_t>(buffer[i])) { | |
78 VLOG(1) << "buffer index: " << i << " should be " | |
79 << static_cast<uint32_t>(value) << " (" | |
80 << static_cast<uint32_t>(buffer[i]) << ")"; | |
81 return_value = false; | |
82 } | |
83 } | |
84 return return_value; | |
85 } | |
86 | |
87 void FakeRemotingDataStreamSender::ConsumeDataChunk( | |
88 uint32_t offset, | |
89 uint32_t size, | |
90 uint32_t total_payload_size) { | |
91 next_frame_data_.resize(total_payload_size); | |
92 MojoResult result = mojo::ReadDataRaw(consumer_handle_.get(), | |
93 next_frame_data_.data() + offset, &size, | |
94 MOJO_READ_DATA_FLAG_ALL_OR_NONE); | |
95 CHECK(result == MOJO_RESULT_OK); | |
96 ++consume_data_chunk_count_; | |
97 } | |
98 | |
99 void FakeRemotingDataStreamSender::SendFrame() { | |
100 ++send_frame_count_; | |
101 received_frame_list.push_back(std::move(next_frame_data_)); | |
102 EXPECT_EQ(send_frame_count_, received_frame_list.size()); | |
103 } | |
104 | |
105 void FakeRemotingDataStreamSender::CancelInFlightData() { | |
106 ++cancel_in_flight_count_; | |
107 } | |
108 | |
109 FakeRemoter::FakeRemoter(mojom::RemotingSourcePtr source, bool start_will_fail) | |
110 : source_(std::move(source)), | |
111 start_will_fail_(start_will_fail), | |
112 weak_factory_(this) {} | |
113 | |
114 FakeRemoter::~FakeRemoter() {} | |
115 | |
116 void FakeRemoter::Start() { | |
117 if (start_will_fail_) { | |
118 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
119 FROM_HERE, | |
120 base::Bind(&FakeRemoter::StartFailed, weak_factory_.GetWeakPtr())); | |
121 } else { | |
122 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
123 FROM_HERE, | |
124 base::Bind(&FakeRemoter::Started, weak_factory_.GetWeakPtr())); | |
125 } | |
126 } | |
127 | |
128 void FakeRemoter::StartDataStreams( | |
129 mojo::ScopedDataPipeConsumerHandle audio_pipe, | |
130 mojo::ScopedDataPipeConsumerHandle video_pipe, | |
131 mojom::RemotingDataStreamSenderRequest audio_sender_request, | |
132 mojom::RemotingDataStreamSenderRequest video_sender_request) { | |
133 if (audio_pipe.is_valid()) { | |
134 VLOG(2) << "Has audio"; | |
135 audio_stream_sender_.reset(new FakeRemotingDataStreamSender( | |
136 std::move(audio_sender_request), std::move(audio_pipe))); | |
137 } | |
138 | |
139 if (video_pipe.is_valid()) { | |
140 VLOG(2) << "Has video"; | |
141 video_stream_sender_.reset(new FakeRemotingDataStreamSender( | |
142 std::move(video_sender_request), std::move(video_pipe))); | |
143 } | |
144 } | |
145 | |
146 void FakeRemoter::Stop(mojom::RemotingStopReason reason) { | |
147 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
148 FROM_HERE, | |
149 base::Bind(&FakeRemoter::Stopped, weak_factory_.GetWeakPtr(), reason)); | |
150 } | |
151 | |
152 void FakeRemoter::SendMessageToSink(const std::vector<uint8_t>& message) {} | |
153 | |
154 void FakeRemoter::Started() { | |
155 source_->OnStarted(); | |
156 } | |
157 | |
158 void FakeRemoter::StartFailed() { | |
159 source_->OnStartFailed(mojom::RemotingStartFailReason::ROUTE_TERMINATED); | |
160 } | |
161 | |
162 void FakeRemoter::Stopped(mojom::RemotingStopReason reason) { | |
163 source_->OnStopped(reason); | |
164 } | |
165 | |
166 FakeRemoterFactory::FakeRemoterFactory(bool start_will_fail) | |
167 : start_will_fail_(start_will_fail) {} | |
168 | |
169 FakeRemoterFactory::~FakeRemoterFactory() {} | |
170 | |
171 void FakeRemoterFactory::Create(mojom::RemotingSourcePtr source, | |
172 mojom::RemoterRequest request) { | |
173 mojo::MakeStrongBinding( | |
174 base::MakeUnique<FakeRemoter>(std::move(source), start_will_fail_), | |
175 std::move(request)); | |
176 } | |
177 | |
178 scoped_refptr<RemotingSourceImpl> CreateRemotingSourceImpl( | |
179 bool start_will_fail) { | |
180 mojom::RemotingSourcePtr remoting_source; | |
181 mojom::RemotingSourceRequest remoting_source_request(&remoting_source); | |
182 mojom::RemoterPtr remoter; | |
183 std::unique_ptr<mojom::RemoterFactory> remoter_factory = | |
184 base::MakeUnique<FakeRemoterFactory>(start_will_fail); | |
185 remoter_factory->Create(std::move(remoting_source), | |
186 mojo::MakeRequest(&remoter)); | |
187 return new RemotingSourceImpl(std::move(remoting_source_request), | |
188 std::move(remoter)); | |
189 } | |
190 | |
191 } // namespace media | |
OLD | NEW |