OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "remoting/protocol/client_video_dispatcher.h" | |
6 | |
7 #include "base/bind.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" | |
18 | |
19 namespace remoting { | |
20 namespace protocol { | |
21 | |
22 class ClientVideoDispatcherTest : public testing::Test, | |
23 public VideoStub, | |
24 public ChannelDispatcherBase::EventHandler { | |
25 public: | |
26 ClientVideoDispatcherTest() | |
Wez
2015/02/11 02:22:56
nit: Move the function bodies out of the class dec
Sergey Ulanov
2015/02/17 19:37:07
Done.
| |
27 : initialized_(false), | |
28 dispatcher_(this), | |
29 parser_(base::Bind(&ClientVideoDispatcherTest::OnVideoAck, | |
30 base::Unretained(this)), | |
31 &reader_) { | |
32 } | |
33 | |
34 void Initialize() { | |
35 dispatcher_.Init( | |
36 &session_, | |
37 ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM, | |
38 kDefaultStreamVersion, ChannelConfig::CODEC_UNDEFINED), | |
39 this); | |
40 base::RunLoop().RunUntilIdle(); | |
41 DCHECK(initialized_); | |
42 host_socket_.PairWith( | |
43 session_.fake_channel_factory().GetFakeChannel(kVideoChannelName)); | |
44 reader_.StartReading(&host_socket_); | |
45 writer_.Init(&host_socket_, BufferedSocketWriter::WriteFailedCallback()); | |
46 } | |
47 | |
48 // VideoStub interface. | |
49 void ProcessVideoPacket(scoped_ptr<VideoPacket> video_packet, | |
50 const base::Closure& done) { | |
51 video_packets_.push_back(video_packet.release()); | |
52 packet_done_callbacks_.push_back(done); | |
53 } | |
54 | |
55 // ChannelDispatcherBase::EventHandler interface. | |
56 void OnChannelInitialized(ChannelDispatcherBase* channel_dispatcher) { | |
57 initialized_ = true; | |
58 } | |
59 void OnChannelError(ChannelDispatcherBase* channel_dispatcher, | |
60 ErrorCode error) { | |
61 // Don't expect channel creation to fail. | |
62 FAIL(); | |
63 } | |
64 | |
65 protected: | |
66 void OnVideoAck(scoped_ptr<VideoAck> ack, const base::Closure& done) { | |
67 ack_messages_.push_back(ack.release()); | |
68 done.Run(); | |
69 } | |
70 | |
71 base::MessageLoop message_loop_; | |
72 | |
73 // Set to true in OnChannelInitialized(). | |
74 bool initialized_; | |
75 | |
76 // Client side. | |
77 ClientVideoDispatcher dispatcher_; | |
78 FakeSession session_; | |
79 | |
80 // Host side. | |
81 FakeStreamSocket host_socket_; | |
82 MessageReader reader_; | |
83 ProtobufMessageParser<VideoAck> parser_; | |
84 BufferedSocketWriter writer_; | |
85 | |
86 ScopedVector<VideoPacket> video_packets_; | |
87 std::vector<base::Closure> packet_done_callbacks_; | |
88 | |
89 ScopedVector<VideoAck> ack_messages_; | |
90 }; | |
91 | |
92 // Verify that the client can receive video packets and acks are not sent for | |
93 // VideoPackets that don't have frame_id field set. | |
94 TEST_F(ClientVideoDispatcherTest, WithoutAcks) { | |
95 Initialize(); | |
96 | |
97 VideoPacket packet; | |
98 packet.set_data(std::string()); | |
99 | |
100 // Send a VideoPacket and verify that the client receives it. | |
101 writer_.Write(SerializeAndFrameMessage(packet), base::Closure()); | |
102 base::RunLoop().RunUntilIdle(); | |
103 EXPECT_EQ(1U, video_packets_.size()); | |
104 | |
105 packet_done_callbacks_.front().Run(); | |
106 base::RunLoop().RunUntilIdle(); | |
107 | |
108 // Ack should never be sent for the packet without frame_id. | |
109 EXPECT_TRUE(ack_messages_.empty()); | |
110 } | |
111 | |
112 // Verifies that the dispatcher sends Ack message with correct rendering delay. | |
113 TEST_F(ClientVideoDispatcherTest, WithAcks) { | |
114 int kTestFrameId = 3; | |
115 | |
116 Initialize(); | |
117 | |
118 VideoPacket packet; | |
119 packet.set_data(std::string()); | |
120 packet.set_frame_id(kTestFrameId); | |
121 | |
122 // Send a VideoPacket and verify that the client receives it. | |
123 writer_.Write(SerializeAndFrameMessage(packet), base::Closure()); | |
124 base::RunLoop().RunUntilIdle(); | |
125 EXPECT_EQ(1U, video_packets_.size()); | |
126 | |
127 // Ack should only be sent after the packet is processed. | |
128 EXPECT_TRUE(ack_messages_.empty()); | |
129 base::RunLoop().RunUntilIdle(); | |
130 | |
131 // Fake completion of video packet decoding, to trigger the Ack. | |
132 packet_done_callbacks_.front().Run(); | |
133 base::RunLoop().RunUntilIdle(); | |
134 | |
135 // Verify that the Ack message has been received. | |
136 ASSERT_EQ(1U, ack_messages_.size()); | |
137 EXPECT_EQ(kTestFrameId, ack_messages_[0]->frame_id()); | |
138 } | |
139 | |
140 // Verify that Ack messages are sent in correct order. | |
141 TEST_F(ClientVideoDispatcherTest, AcksOrder) { | |
142 int kTestFrameId = 3; | |
143 | |
144 Initialize(); | |
145 | |
146 VideoPacket packet; | |
147 packet.set_data(std::string()); | |
148 packet.set_frame_id(kTestFrameId); | |
149 | |
150 // Send two VideoPackets. | |
151 writer_.Write(SerializeAndFrameMessage(packet), base::Closure()); | |
152 base::RunLoop().RunUntilIdle(); | |
153 | |
154 packet.set_frame_id(kTestFrameId + 1); | |
155 writer_.Write(SerializeAndFrameMessage(packet), base::Closure()); | |
156 base::RunLoop().RunUntilIdle(); | |
157 | |
158 EXPECT_EQ(2U, video_packets_.size()); | |
159 EXPECT_TRUE(ack_messages_.empty()); | |
160 | |
161 // Call completion callbacks in revers order. | |
162 packet_done_callbacks_[1].Run(); | |
163 packet_done_callbacks_[0].Run(); | |
164 | |
165 base::RunLoop().RunUntilIdle(); | |
166 | |
167 // Verify order of Ack messages. | |
168 ASSERT_EQ(2U, ack_messages_.size()); | |
169 EXPECT_EQ(kTestFrameId, ack_messages_[0]->frame_id()); | |
170 EXPECT_EQ(kTestFrameId + 1, ack_messages_[1]->frame_id()); | |
171 } | |
172 | |
173 } // namespace protocol | |
174 } // namespace remoting | |
OLD | NEW |