1 // Copyright 2014 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/callback_helpers.h"
9 #include "net/socket/stream_socket.h"
10 #include "remoting/base/constants.h"
11 #include "remoting/proto/video.pb.h"
12 #include "remoting/protocol/message_serialization.h"
13 #include "remoting/protocol/video_stub.h"
18 struct ClientVideoDispatcher::PendingFrame
{
19 PendingFrame(int frame_id
)
26 ClientVideoDispatcher::ClientVideoDispatcher(VideoStub
* video_stub
)
27 : ChannelDispatcherBase(kVideoChannelName
),
28 video_stub_(video_stub
),
29 parser_(base::Bind(&ClientVideoDispatcher::ProcessVideoPacket
,
30 base::Unretained(this)),
34 ClientVideoDispatcher::~ClientVideoDispatcher() {
37 void ClientVideoDispatcher::ProcessVideoPacket(
38 scoped_ptr
<VideoPacket
> video_packet
,
39 const base::Closure
& done
) {
40 base::ScopedClosureRunner
done_runner(done
);
42 int frame_id
= video_packet
->frame_id();
44 if (!video_packet
->has_frame_id()) {
45 video_stub_
->ProcessVideoPacket(video_packet
.Pass(), done_runner
.Release());
49 PendingFramesList::iterator pending_frame
=
50 pending_frames_
.insert(pending_frames_
.end(), PendingFrame(frame_id
));
52 video_stub_
->ProcessVideoPacket(
53 video_packet
.Pass(), base::Bind(&ClientVideoDispatcher::OnPacketDone
,
54 base::Unretained(this), pending_frame
));
58 void ClientVideoDispatcher::OnPacketDone(
59 PendingFramesList::iterator pending_frame
) {
60 // Mark the frame as done.
61 DCHECK(!pending_frame
->done
);
62 pending_frame
->done
= true;
64 // Send VideoAck for all packets in the head of the queue that have finished
66 while (!pending_frames_
.empty() && pending_frames_
.front().done
) {
68 ack_message
.set_frame_id(pending_frames_
.front().frame_id
);
69 writer()->Write(SerializeAndFrameMessage(ack_message
), base::Closure());
70 pending_frames_
.pop_front();
74 } // namespace protocol
75 } // namespace remoting