1 // Copyright 2015 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.
5 #include "remoting/protocol/client_video_dispatcher.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "remoting/base/buffered_socket_writer.h"
12 #include "remoting/base/constants.h"
13 #include "remoting/proto/video.pb.h"
14 #include "remoting/protocol/fake_session.h"
15 #include "remoting/protocol/fake_stream_socket.h"
16 #include "remoting/protocol/message_serialization.h"
17 #include "remoting/protocol/video_stub.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 class ClientVideoDispatcherTest
: public testing::Test
,
25 public ChannelDispatcherBase::EventHandler
{
27 ClientVideoDispatcherTest();
29 // VideoStub interface.
30 void ProcessVideoPacket(scoped_ptr
<VideoPacket
> video_packet
,
31 const base::Closure
& done
) override
;
33 // ChannelDispatcherBase::EventHandler interface.
34 void OnChannelInitialized(ChannelDispatcherBase
* channel_dispatcher
) override
;
35 void OnChannelError(ChannelDispatcherBase
* channel_dispatcher
,
36 ErrorCode error
) override
;
39 void OnVideoAck(scoped_ptr
<VideoAck
> ack
, const base::Closure
& done
);
40 void OnReadError(int error
);
42 base::MessageLoop message_loop_
;
44 // Set to true in OnChannelInitialized().
48 ClientVideoDispatcher dispatcher_
;
52 FakeStreamSocket host_socket_
;
53 MessageReader reader_
;
54 ProtobufMessageParser
<VideoAck
> parser_
;
55 BufferedSocketWriter writer_
;
57 ScopedVector
<VideoPacket
> video_packets_
;
58 std::vector
<base::Closure
> packet_done_callbacks_
;
60 ScopedVector
<VideoAck
> ack_messages_
;
63 ClientVideoDispatcherTest::ClientVideoDispatcherTest()
64 : initialized_(false),
66 parser_(base::Bind(&ClientVideoDispatcherTest::OnVideoAck
,
67 base::Unretained(this)),
69 dispatcher_
.Init(&session_
, ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM
,
70 kDefaultStreamVersion
,
71 ChannelConfig::CODEC_UNDEFINED
),
73 base::RunLoop().RunUntilIdle();
75 host_socket_
.PairWith(
76 session_
.fake_channel_factory().GetFakeChannel(kVideoChannelName
));
77 reader_
.StartReading(&host_socket_
,
78 base::Bind(&ClientVideoDispatcherTest::OnReadError
,
79 base::Unretained(this)));
81 base::Bind(&P2PStreamSocket::Write
, base::Unretained(&host_socket_
)),
82 BufferedSocketWriter::WriteFailedCallback());
85 void ClientVideoDispatcherTest::ProcessVideoPacket(
86 scoped_ptr
<VideoPacket
> video_packet
,
87 const base::Closure
& done
) {
88 video_packets_
.push_back(video_packet
.release());
89 packet_done_callbacks_
.push_back(done
);
92 void ClientVideoDispatcherTest::OnChannelInitialized(
93 ChannelDispatcherBase
* channel_dispatcher
) {
97 void ClientVideoDispatcherTest::OnChannelError(
98 ChannelDispatcherBase
* channel_dispatcher
,
100 // Don't expect channel creation to fail.
104 void ClientVideoDispatcherTest::OnVideoAck(scoped_ptr
<VideoAck
> ack
,
105 const base::Closure
& done
) {
106 ack_messages_
.push_back(ack
.release());
110 void ClientVideoDispatcherTest::OnReadError(int error
) {
111 LOG(FATAL
) << "Unexpected read error: " << error
;
114 // Verify that the client can receive video packets and acks are not sent for
115 // VideoPackets that don't have frame_id field set.
116 TEST_F(ClientVideoDispatcherTest
, WithoutAcks
) {
118 packet
.set_data(std::string());
120 // Send a VideoPacket and verify that the client receives it.
121 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
122 base::RunLoop().RunUntilIdle();
123 EXPECT_EQ(1U, video_packets_
.size());
125 packet_done_callbacks_
.front().Run();
126 base::RunLoop().RunUntilIdle();
128 // Ack should never be sent for the packet without frame_id.
129 EXPECT_TRUE(ack_messages_
.empty());
132 // Verifies that the dispatcher sends Ack message with correct rendering delay.
133 TEST_F(ClientVideoDispatcherTest
, WithAcks
) {
134 int kTestFrameId
= 3;
137 packet
.set_data(std::string());
138 packet
.set_frame_id(kTestFrameId
);
140 // Send a VideoPacket and verify that the client receives it.
141 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
142 base::RunLoop().RunUntilIdle();
143 EXPECT_EQ(1U, video_packets_
.size());
145 // Ack should only be sent after the packet is processed.
146 EXPECT_TRUE(ack_messages_
.empty());
147 base::RunLoop().RunUntilIdle();
149 // Fake completion of video packet decoding, to trigger the Ack.
150 packet_done_callbacks_
.front().Run();
151 base::RunLoop().RunUntilIdle();
153 // Verify that the Ack message has been received.
154 ASSERT_EQ(1U, ack_messages_
.size());
155 EXPECT_EQ(kTestFrameId
, ack_messages_
[0]->frame_id());
158 // Verify that Ack messages are sent in correct order.
159 TEST_F(ClientVideoDispatcherTest
, AcksOrder
) {
160 int kTestFrameId
= 3;
163 packet
.set_data(std::string());
164 packet
.set_frame_id(kTestFrameId
);
166 // Send two VideoPackets.
167 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
168 base::RunLoop().RunUntilIdle();
170 packet
.set_frame_id(kTestFrameId
+ 1);
171 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
172 base::RunLoop().RunUntilIdle();
174 EXPECT_EQ(2U, video_packets_
.size());
175 EXPECT_TRUE(ack_messages_
.empty());
177 // Call completion callbacks in revers order.
178 packet_done_callbacks_
[1].Run();
179 packet_done_callbacks_
[0].Run();
181 base::RunLoop().RunUntilIdle();
183 // Verify order of Ack messages.
184 ASSERT_EQ(2U, ack_messages_
.size());
185 EXPECT_EQ(kTestFrameId
, ack_messages_
[0]->frame_id());
186 EXPECT_EQ(kTestFrameId
+ 1, ack_messages_
[1]->frame_id());
189 } // namespace protocol
190 } // namespace remoting