Roll WebRTC 9084:9094, Libjingle 9081:9093
[chromium-blink-merge.git] / remoting / protocol / client_video_dispatcher.cc
blobd9918fb36a89f342f19c37a8a5e16c294c8a0245
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"
7 #include "base/bind.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"
15 namespace remoting {
16 namespace protocol {
18 struct ClientVideoDispatcher::PendingFrame {
19 PendingFrame(int frame_id)
20 : frame_id(frame_id),
21 done(false) {}
22 int frame_id;
23 bool done;
26 ClientVideoDispatcher::ClientVideoDispatcher(VideoStub* video_stub)
27 : ChannelDispatcherBase(kVideoChannelName),
28 video_stub_(video_stub),
29 parser_(base::Bind(&ClientVideoDispatcher::ProcessVideoPacket,
30 base::Unretained(this)),
31 reader()) {
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());
46 return;
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
65 // rendering.
66 while (!pending_frames_.empty() && pending_frames_.front().done) {
67 VideoAck ack_message;
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