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/constants.h"
12 #include "remoting/proto/video.pb.h"
13 #include "remoting/protocol/fake_session.h"
14 #include "remoting/protocol/fake_stream_socket.h"
15 #include "remoting/protocol/message_serialization.h"
16 #include "remoting/protocol/video_stub.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 class ClientVideoDispatcherTest
: public testing::Test
,
24 public ChannelDispatcherBase::EventHandler
{
26 ClientVideoDispatcherTest();
28 // VideoStub interface.
29 void ProcessVideoPacket(scoped_ptr
<VideoPacket
> video_packet
,
30 const base::Closure
& done
) override
;
32 // ChannelDispatcherBase::EventHandler interface.
33 void OnChannelInitialized(ChannelDispatcherBase
* channel_dispatcher
) override
;
34 void OnChannelError(ChannelDispatcherBase
* channel_dispatcher
,
35 ErrorCode error
) override
;
38 void OnVideoAck(scoped_ptr
<VideoAck
> ack
, const base::Closure
& done
);
39 void OnReadError(int error
);
41 base::MessageLoop message_loop_
;
43 // Set to true in OnChannelInitialized().
47 ClientVideoDispatcher dispatcher_
;
51 FakeStreamSocket host_socket_
;
52 MessageReader reader_
;
53 ProtobufMessageParser
<VideoAck
> parser_
;
54 BufferedSocketWriter writer_
;
56 ScopedVector
<VideoPacket
> video_packets_
;
57 std::vector
<base::Closure
> packet_done_callbacks_
;
59 ScopedVector
<VideoAck
> ack_messages_
;
62 ClientVideoDispatcherTest::ClientVideoDispatcherTest()
63 : initialized_(false),
65 parser_(base::Bind(&ClientVideoDispatcherTest::OnVideoAck
,
66 base::Unretained(this)),
68 dispatcher_
.Init(&session_
, ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM
,
69 kDefaultStreamVersion
,
70 ChannelConfig::CODEC_UNDEFINED
),
72 base::RunLoop().RunUntilIdle();
74 host_socket_
.PairWith(
75 session_
.fake_channel_factory().GetFakeChannel(kVideoChannelName
));
76 reader_
.StartReading(&host_socket_
,
77 base::Bind(&ClientVideoDispatcherTest::OnReadError
,
78 base::Unretained(this)));
79 writer_
.Init(&host_socket_
, BufferedSocketWriter::WriteFailedCallback());
82 void ClientVideoDispatcherTest::ProcessVideoPacket(
83 scoped_ptr
<VideoPacket
> video_packet
,
84 const base::Closure
& done
) {
85 video_packets_
.push_back(video_packet
.release());
86 packet_done_callbacks_
.push_back(done
);
89 void ClientVideoDispatcherTest::OnChannelInitialized(
90 ChannelDispatcherBase
* channel_dispatcher
) {
94 void ClientVideoDispatcherTest::OnChannelError(
95 ChannelDispatcherBase
* channel_dispatcher
,
97 // Don't expect channel creation to fail.
101 void ClientVideoDispatcherTest::OnVideoAck(scoped_ptr
<VideoAck
> ack
,
102 const base::Closure
& done
) {
103 ack_messages_
.push_back(ack
.release());
107 void ClientVideoDispatcherTest::OnReadError(int error
) {
108 LOG(FATAL
) << "Unexpected read error: " << error
;
111 // Verify that the client can receive video packets and acks are not sent for
112 // VideoPackets that don't have frame_id field set.
113 TEST_F(ClientVideoDispatcherTest
, WithoutAcks
) {
115 packet
.set_data(std::string());
117 // Send a VideoPacket and verify that the client receives it.
118 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
119 base::RunLoop().RunUntilIdle();
120 EXPECT_EQ(1U, video_packets_
.size());
122 packet_done_callbacks_
.front().Run();
123 base::RunLoop().RunUntilIdle();
125 // Ack should never be sent for the packet without frame_id.
126 EXPECT_TRUE(ack_messages_
.empty());
129 // Verifies that the dispatcher sends Ack message with correct rendering delay.
130 TEST_F(ClientVideoDispatcherTest
, WithAcks
) {
131 int kTestFrameId
= 3;
134 packet
.set_data(std::string());
135 packet
.set_frame_id(kTestFrameId
);
137 // Send a VideoPacket and verify that the client receives it.
138 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
139 base::RunLoop().RunUntilIdle();
140 EXPECT_EQ(1U, video_packets_
.size());
142 // Ack should only be sent after the packet is processed.
143 EXPECT_TRUE(ack_messages_
.empty());
144 base::RunLoop().RunUntilIdle();
146 // Fake completion of video packet decoding, to trigger the Ack.
147 packet_done_callbacks_
.front().Run();
148 base::RunLoop().RunUntilIdle();
150 // Verify that the Ack message has been received.
151 ASSERT_EQ(1U, ack_messages_
.size());
152 EXPECT_EQ(kTestFrameId
, ack_messages_
[0]->frame_id());
155 // Verify that Ack messages are sent in correct order.
156 TEST_F(ClientVideoDispatcherTest
, AcksOrder
) {
157 int kTestFrameId
= 3;
160 packet
.set_data(std::string());
161 packet
.set_frame_id(kTestFrameId
);
163 // Send two VideoPackets.
164 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
165 base::RunLoop().RunUntilIdle();
167 packet
.set_frame_id(kTestFrameId
+ 1);
168 writer_
.Write(SerializeAndFrameMessage(packet
), base::Closure());
169 base::RunLoop().RunUntilIdle();
171 EXPECT_EQ(2U, video_packets_
.size());
172 EXPECT_TRUE(ack_messages_
.empty());
174 // Call completion callbacks in revers order.
175 packet_done_callbacks_
[1].Run();
176 packet_done_callbacks_
[0].Run();
178 base::RunLoop().RunUntilIdle();
180 // Verify order of Ack messages.
181 ASSERT_EQ(2U, ack_messages_
.size());
182 EXPECT_EQ(kTestFrameId
, ack_messages_
[0]->frame_id());
183 EXPECT_EQ(kTestFrameId
+ 1, ack_messages_
[1]->frame_id());
186 } // namespace protocol
187 } // namespace remoting