blob: 5bf908cefe6b0be60aa24d1cc15cee4cb0ab6fe7 [file] [log] [blame]
sergeyu97568a8162015-02-24 18:00:551// 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
19namespace remoting {
20namespace protocol {
21
22class ClientVideoDispatcherTest : public testing::Test,
23 public VideoStub,
24 public ChannelDispatcherBase::EventHandler {
25 public:
26 ClientVideoDispatcherTest();
27
28 // VideoStub interface.
29 void ProcessVideoPacket(scoped_ptr<VideoPacket> video_packet,
30 const base::Closure& done) override;
31
32 // ChannelDispatcherBase::EventHandler interface.
33 void OnChannelInitialized(ChannelDispatcherBase* channel_dispatcher) override;
34 void OnChannelError(ChannelDispatcherBase* channel_dispatcher,
35 ErrorCode error) override;
36
37 protected:
38 void OnVideoAck(scoped_ptr<VideoAck> ack, const base::Closure& done);
sergeyua04c0462015-05-14 01:48:4039 void OnReadError(int error);
sergeyu97568a8162015-02-24 18:00:5540
41 base::MessageLoop message_loop_;
42
43 // Set to true in OnChannelInitialized().
44 bool initialized_;
45
46 // Client side.
47 ClientVideoDispatcher dispatcher_;
48 FakeSession session_;
49
50 // Host side.
51 FakeStreamSocket host_socket_;
52 MessageReader reader_;
53 ProtobufMessageParser<VideoAck> parser_;
54 BufferedSocketWriter writer_;
55
56 ScopedVector<VideoPacket> video_packets_;
57 std::vector<base::Closure> packet_done_callbacks_;
58
59 ScopedVector<VideoAck> ack_messages_;
60};
61
62ClientVideoDispatcherTest::ClientVideoDispatcherTest()
63 : initialized_(false),
64 dispatcher_(this),
65 parser_(base::Bind(&ClientVideoDispatcherTest::OnVideoAck,
66 base::Unretained(this)),
67 &reader_) {
68 dispatcher_.Init(&session_, ChannelConfig(ChannelConfig::TRANSPORT_MUX_STREAM,
69 kDefaultStreamVersion,
70 ChannelConfig::CODEC_UNDEFINED),
71 this);
72 base::RunLoop().RunUntilIdle();
73 DCHECK(initialized_);
74 host_socket_.PairWith(
75 session_.fake_channel_factory().GetFakeChannel(kVideoChannelName));
sergeyua04c0462015-05-14 01:48:4076 reader_.StartReading(&host_socket_,
77 base::Bind(&ClientVideoDispatcherTest::OnReadError,
78 base::Unretained(this)));
sergeyu97568a8162015-02-24 18:00:5579 writer_.Init(&host_socket_, BufferedSocketWriter::WriteFailedCallback());
80}
81
82void 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);
87}
88
89void ClientVideoDispatcherTest::OnChannelInitialized(
90 ChannelDispatcherBase* channel_dispatcher) {
91 initialized_ = true;
92}
93
94void ClientVideoDispatcherTest::OnChannelError(
95 ChannelDispatcherBase* channel_dispatcher,
96 ErrorCode error) {
97 // Don't expect channel creation to fail.
98 FAIL();
99}
100
101void ClientVideoDispatcherTest::OnVideoAck(scoped_ptr<VideoAck> ack,
102 const base::Closure& done) {
103 ack_messages_.push_back(ack.release());
104 done.Run();
105}
106
sergeyua04c0462015-05-14 01:48:40107void ClientVideoDispatcherTest::OnReadError(int error) {
108 LOG(FATAL) << "Unexpected read error: " << error;
109}
110
sergeyu97568a8162015-02-24 18:00:55111// Verify that the client can receive video packets and acks are not sent for
112// VideoPackets that don't have frame_id field set.
113TEST_F(ClientVideoDispatcherTest, WithoutAcks) {
114 VideoPacket packet;
115 packet.set_data(std::string());
116
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());
121
122 packet_done_callbacks_.front().Run();
123 base::RunLoop().RunUntilIdle();
124
125 // Ack should never be sent for the packet without frame_id.
126 EXPECT_TRUE(ack_messages_.empty());
127}
128
129// Verifies that the dispatcher sends Ack message with correct rendering delay.
130TEST_F(ClientVideoDispatcherTest, WithAcks) {
131 int kTestFrameId = 3;
132
133 VideoPacket packet;
134 packet.set_data(std::string());
135 packet.set_frame_id(kTestFrameId);
136
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());
141
142 // Ack should only be sent after the packet is processed.
143 EXPECT_TRUE(ack_messages_.empty());
144 base::RunLoop().RunUntilIdle();
145
146 // Fake completion of video packet decoding, to trigger the Ack.
147 packet_done_callbacks_.front().Run();
148 base::RunLoop().RunUntilIdle();
149
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());
153}
154
155// Verify that Ack messages are sent in correct order.
156TEST_F(ClientVideoDispatcherTest, AcksOrder) {
157 int kTestFrameId = 3;
158
159 VideoPacket packet;
160 packet.set_data(std::string());
161 packet.set_frame_id(kTestFrameId);
162
163 // Send two VideoPackets.
164 writer_.Write(SerializeAndFrameMessage(packet), base::Closure());
165 base::RunLoop().RunUntilIdle();
166
167 packet.set_frame_id(kTestFrameId + 1);
168 writer_.Write(SerializeAndFrameMessage(packet), base::Closure());
169 base::RunLoop().RunUntilIdle();
170
171 EXPECT_EQ(2U, video_packets_.size());
172 EXPECT_TRUE(ack_messages_.empty());
173
174 // Call completion callbacks in revers order.
175 packet_done_callbacks_[1].Run();
176 packet_done_callbacks_[0].Run();
177
178 base::RunLoop().RunUntilIdle();
179
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());
184}
185
186} // namespace protocol
187} // namespace remoting