OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/rtp_writer.h" | 5 #include "remoting/protocol/rtp_writer.h" |
6 | 6 |
7 #include "net/base/io_buffer.h" | 7 #include "net/base/io_buffer.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 9 #include "remoting/base/compound_buffer.h" |
9 #include "remoting/protocol/rtp_utils.h" | 10 #include "remoting/protocol/rtp_utils.h" |
10 | 11 |
11 namespace remoting { | 12 namespace remoting { |
12 namespace protocol { | 13 namespace protocol { |
13 | 14 |
14 namespace { | 15 namespace { |
15 const int kMtu = 1200; | 16 const int kMtu = 1200; |
16 const uint8 kRtpPayloadTypePrivate = 96; | 17 const uint8 kRtpPayloadTypePrivate = 96; |
17 } | 18 } |
18 | 19 |
19 RtpWriter::RtpWriter() | 20 RtpWriter::RtpWriter() |
20 : rtp_socket_(NULL), | 21 : rtp_socket_(NULL), |
21 rtcp_socket_(NULL), | 22 rtcp_socket_(NULL), |
22 last_packet_number_(0) { | 23 last_packet_number_(0) { |
23 } | 24 } |
24 | 25 |
25 RtpWriter::~RtpWriter() { } | 26 RtpWriter::~RtpWriter() { } |
26 | 27 |
27 // Initializes the writer. Must be called on the thread the sockets belong | 28 // Initializes the writer. Must be called on the thread the sockets belong |
28 // to. | 29 // to. |
29 void RtpWriter::Init(net::Socket* rtp_socket, net::Socket* rtcp_socket) { | 30 void RtpWriter::Init(net::Socket* rtp_socket, net::Socket* rtcp_socket) { |
30 buffered_rtp_writer_ = new BufferedDatagramWriter(); | 31 buffered_rtp_writer_ = new BufferedDatagramWriter(); |
31 buffered_rtp_writer_->Init(rtp_socket, NULL); | 32 buffered_rtp_writer_->Init(rtp_socket, NULL); |
32 rtp_socket_ = rtp_socket; | 33 rtp_socket_ = rtp_socket; |
33 rtcp_socket_ = rtcp_socket; | 34 rtcp_socket_ = rtcp_socket; |
34 } | 35 } |
35 | 36 |
36 void RtpWriter::SendPacket(const char* payload, int payload_size, | 37 void RtpWriter::SendPacket(const CompoundBuffer& payload, uint32 timestamp) { |
37 uint32 timestamp) { | |
38 RtpHeader header; | 38 RtpHeader header; |
39 header.padding = false; | 39 header.padding = false; |
40 header.extension = false; | 40 header.extension = false; |
41 header.sources = 0; | 41 header.sources = 0; |
42 header.payload_type = kRtpPayloadTypePrivate; | 42 header.payload_type = kRtpPayloadTypePrivate; |
43 header.timestamp = timestamp; | 43 header.timestamp = timestamp; |
44 | 44 |
45 // TODO(sergeyu): RTP requires that SSRC is chosen randomly by each | 45 // TODO(sergeyu): RTP requires that SSRC is chosen randomly by each |
46 // participant. There are only two participants in chromoting session, | 46 // participant. There are only two participants in chromoting session, |
47 // so SSRC isn't useful. Implement it in future if neccessary. | 47 // so SSRC isn't useful. Implement it in future if neccessary. |
48 header.sync_source_id = 0; | 48 header.sync_source_id = 0; |
49 | 49 |
50 // TODO(sergeyu): Add VP8 payload header. | 50 // TODO(sergeyu): Add VP8 payload header. |
51 | 51 |
52 int position = 0; | 52 int position = 0; |
53 while (position < payload_size) { | 53 while (position < payload.total_bytes()) { |
54 // Allocate buffer. | 54 // Allocate buffer. |
55 int size = std::min(kMtu, payload_size - position); | 55 int size = std::min(kMtu, payload.total_bytes() - position); |
56 int header_size = GetRtpHeaderSize(header.sources); | 56 int header_size = GetRtpHeaderSize(header.sources); |
57 int total_size = size + header_size; | 57 int total_size = size + header_size; |
58 net::IOBufferWithSize* buffer = new net::IOBufferWithSize(total_size); | 58 net::IOBufferWithSize* buffer = new net::IOBufferWithSize(total_size); |
59 | 59 |
60 // Set marker if this is the last frame. | 60 // Set marker if this is the last frame. |
61 header.marker = (position + size) == payload_size; | 61 header.marker = (position + size) == payload.total_bytes(); |
62 | 62 |
63 // TODO(sergeyu): Handle sequence number wrapping. | 63 // TODO(sergeyu): Handle sequence number wrapping. |
64 header.sequence_number = last_packet_number_; | 64 header.sequence_number = last_packet_number_; |
65 ++last_packet_number_; | 65 ++last_packet_number_; |
66 | 66 |
67 // Generate header. | 67 // Generate header. |
68 PackRtpHeader(reinterpret_cast<uint8*>(buffer->data()), buffer->size(), | 68 PackRtpHeader(reinterpret_cast<uint8*>(buffer->data()), buffer->size(), |
69 header); | 69 header); |
70 | 70 |
71 // Copy data to the buffer. | 71 // Copy data to the buffer. |
72 memcpy(buffer->data() + header_size, payload + position, size); | 72 CompoundBuffer chunk; |
| 73 chunk.CopyFrom(payload, position, position + size); |
| 74 chunk.CopyTo(buffer->data() + header_size, size); |
73 | 75 |
74 // Send it. | 76 // Send it. |
75 buffered_rtp_writer_->Write(buffer); | 77 buffered_rtp_writer_->Write(buffer); |
76 | 78 |
77 position += size; | 79 position += size; |
78 } | 80 } |
79 | 81 |
80 DCHECK_EQ(position, payload_size); | 82 DCHECK_EQ(position, payload.total_bytes()); |
81 } | 83 } |
82 | 84 |
83 int RtpWriter::GetPendingPackets() { | 85 int RtpWriter::GetPendingPackets() { |
84 return buffered_rtp_writer_->GetBufferChunks(); | 86 return buffered_rtp_writer_->GetBufferChunks(); |
85 } | 87 } |
86 | 88 |
87 // Stop writing and drop pending data. Must be called from the same thread as | 89 // Stop writing and drop pending data. Must be called from the same thread as |
88 // Init(). | 90 // Init(). |
89 void RtpWriter::Close() { | 91 void RtpWriter::Close() { |
90 buffered_rtp_writer_->Close(); | 92 buffered_rtp_writer_->Close(); |
91 } | 93 } |
92 | 94 |
93 } // namespace protocol | 95 } // namespace protocol |
94 } // namespace remoting | 96 } // namespace remoting |
OLD | NEW |