[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 1 | // Copyright (c) 2010 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/rtp_video_writer.h" |
| 6 | |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | c56acf54ff | 2010-11-19 23:44:45 | [diff] [blame] | 8 | #include "base/task.h" |
[email protected] | b3c0340 | 2010-11-16 01:27:46 | [diff] [blame] | 9 | #include "net/base/io_buffer.h" |
| 10 | #include "remoting/base/compound_buffer.h" |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 11 | #include "remoting/base/constants.h" |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 12 | #include "remoting/proto/video.pb.h" |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 13 | #include "remoting/protocol/rtp_writer.h" |
[email protected] | b3c0340 | 2010-11-16 01:27:46 | [diff] [blame] | 14 | #include "remoting/protocol/session.h" |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 15 | |
| 16 | namespace remoting { |
[email protected] | d87c404 | 2010-11-04 00:46:01 | [diff] [blame] | 17 | namespace protocol { |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 18 | |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 19 | namespace { |
| 20 | const int kMtu = 1200; |
| 21 | } // namespace |
| 22 | |
[email protected] | 60fc9600 | 2011-08-12 23:07:05 | [diff] [blame^] | 23 | RtpVideoWriter::RtpVideoWriter(base::MessageLoopProxy* message_loop) |
| 24 | : initialized_(false), |
| 25 | rtp_writer_(message_loop) { |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 26 | } |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 27 | |
[email protected] | 86dbc72 | 2011-06-30 23:23:30 | [diff] [blame] | 28 | RtpVideoWriter::~RtpVideoWriter() { |
| 29 | Close(); |
| 30 | } |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 31 | |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 32 | void RtpVideoWriter::Init(protocol::Session* session, |
| 33 | const InitializedCallback& callback) { |
| 34 | initialized_callback_ = callback; |
| 35 | session->CreateDatagramChannel( |
| 36 | kVideoRtpChannelName, |
[email protected] | dee7ab5 | 2011-08-11 02:51:34 | [diff] [blame] | 37 | base::Bind(&RtpVideoWriter::OnChannelReady, |
| 38 | base::Unretained(this), true)); |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 39 | session->CreateDatagramChannel( |
| 40 | kVideoRtcpChannelName, |
[email protected] | dee7ab5 | 2011-08-11 02:51:34 | [diff] [blame] | 41 | base::Bind(&RtpVideoWriter::OnChannelReady, |
| 42 | base::Unretained(this), false)); |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | dee7ab5 | 2011-08-11 02:51:34 | [diff] [blame] | 45 | void RtpVideoWriter::OnChannelReady(bool rtp, net::Socket* socket) { |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 46 | if (!socket) { |
| 47 | if (!initialized_) { |
| 48 | initialized_ = true; |
| 49 | initialized_callback_.Run(false); |
| 50 | } |
| 51 | return; |
| 52 | } |
| 53 | |
[email protected] | dee7ab5 | 2011-08-11 02:51:34 | [diff] [blame] | 54 | if (rtp) { |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 55 | DCHECK(!rtp_channel_.get()); |
| 56 | rtp_channel_.reset(socket); |
| 57 | rtp_writer_.Init(socket); |
[email protected] | dee7ab5 | 2011-08-11 02:51:34 | [diff] [blame] | 58 | } else { |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 59 | DCHECK(!rtcp_channel_.get()); |
| 60 | rtcp_channel_.reset(socket); |
| 61 | // TODO(sergeyu): Use RTCP channel somehow. |
| 62 | } |
| 63 | |
| 64 | if (rtp_channel_.get() && rtcp_channel_.get()) { |
| 65 | DCHECK(!initialized_); |
| 66 | initialized_ = true; |
| 67 | initialized_callback_.Run(true); |
| 68 | } |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 86dbc72 | 2011-06-30 23:23:30 | [diff] [blame] | 71 | void RtpVideoWriter::Close() { |
| 72 | rtp_writer_.Close(); |
[email protected] | 182ec8f2 | 2011-08-11 02:14:35 | [diff] [blame] | 73 | rtp_channel_.reset(); |
| 74 | rtcp_channel_.reset(); |
[email protected] | 86dbc72 | 2011-06-30 23:23:30 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | c56acf54ff | 2010-11-19 23:44:45 | [diff] [blame] | 77 | void RtpVideoWriter::ProcessVideoPacket(const VideoPacket* packet, Task* done) { |
| 78 | CHECK(packet->format().encoding() == VideoPacketFormat::ENCODING_VP8) |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 79 | << "Only VP8 is supported in RTP."; |
| 80 | |
[email protected] | b3c0340 | 2010-11-16 01:27:46 | [diff] [blame] | 81 | CompoundBuffer payload; |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 82 | // TODO(sergeyu): This copy would not be necessary CompoundBuffer was used |
| 83 | // inside of VideoPacket. |
[email protected] | c56acf54ff | 2010-11-19 23:44:45 | [diff] [blame] | 84 | payload.AppendCopyOf(packet->data().data(), packet->data().size()); |
[email protected] | b3c0340 | 2010-11-16 01:27:46 | [diff] [blame] | 85 | |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 86 | Vp8Descriptor vp8_desriptor; |
| 87 | // TODO(sergeyu): Add a flag in VideoPacket that indicates whether this is a |
| 88 | // key frame or not. |
| 89 | vp8_desriptor.non_reference_frame = false; |
| 90 | vp8_desriptor.picture_id = kuint32max; |
| 91 | |
| 92 | int position = 0; |
| 93 | while (position < payload.total_bytes()) { |
| 94 | int size = std::min(kMtu, payload.total_bytes() - position); |
| 95 | |
| 96 | // Frame beginning flag is set only for the first packet in the first |
| 97 | // partition. |
| 98 | vp8_desriptor.frame_beginning = |
[email protected] | c56acf54ff | 2010-11-19 23:44:45 | [diff] [blame] | 99 | (packet->flags() & VideoPacket::FIRST_PACKET) != 0 && position == 0; |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 100 | |
| 101 | // Marker bit is set only for the last packet in the last partition. |
| 102 | bool marker = (position + size) == payload.total_bytes() && |
[email protected] | c56acf54ff | 2010-11-19 23:44:45 | [diff] [blame] | 103 | (packet->flags() & VideoPacket::LAST_PACKET) != 0; |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 104 | |
| 105 | // Set fragmentation flag appropriately. |
| 106 | if (position == 0) { |
| 107 | if (size == payload.total_bytes()) { |
| 108 | vp8_desriptor.fragmentation_info = Vp8Descriptor::NOT_FRAGMENTED; |
| 109 | } else { |
| 110 | vp8_desriptor.fragmentation_info = Vp8Descriptor::FIRST_FRAGMENT; |
| 111 | } |
| 112 | } else { |
| 113 | if (position + size == payload.total_bytes()) { |
| 114 | vp8_desriptor.fragmentation_info = Vp8Descriptor::LAST_FRAGMENT; |
| 115 | } else { |
| 116 | vp8_desriptor.fragmentation_info = Vp8Descriptor::MIDDLE_FRAGMENT; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // Create CompoundBuffer for the chunk. |
| 121 | CompoundBuffer chunk; |
| 122 | chunk.CopyFrom(payload, position, position + size); |
| 123 | |
| 124 | // And send it. |
[email protected] | c56acf54ff | 2010-11-19 23:44:45 | [diff] [blame] | 125 | rtp_writer_.SendPacket(packet->timestamp(), marker, vp8_desriptor, chunk); |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 126 | |
| 127 | position += size; |
| 128 | } |
[email protected] | 6357e60 | 2010-11-16 01:53:07 | [diff] [blame] | 129 | DCHECK_EQ(position, payload.total_bytes()); |
[email protected] | c56acf54ff | 2010-11-19 23:44:45 | [diff] [blame] | 130 | |
| 131 | done->Run(); |
| 132 | delete done; |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int RtpVideoWriter::GetPendingPackets() { |
| 136 | return rtp_writer_.GetPendingPackets(); |
| 137 | } |
| 138 | |
[email protected] | d87c404 | 2010-11-04 00:46:01 | [diff] [blame] | 139 | } // namespace protocol |
[email protected] | 14fd1a60 | 2010-11-03 04:17:09 | [diff] [blame] | 140 | } // namespace remoting |