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 "media/cast/net/rtp/frame_buffer.h"
7 #include "base/logging.h"
12 FrameBuffer::FrameBuffer()
15 num_packets_received_(0),
18 last_referenced_frame_id_(0),
21 FrameBuffer::~FrameBuffer() {}
23 void FrameBuffer::InsertPacket(const uint8
* payload_data
,
25 const RtpCastHeader
& rtp_header
) {
26 // Is this the first packet in the frame?
27 if (packets_
.empty()) {
28 frame_id_
= rtp_header
.frame_id
;
29 max_packet_id_
= rtp_header
.max_packet_id
;
30 is_key_frame_
= rtp_header
.is_key_frame
;
32 DCHECK_EQ(rtp_header
.frame_id
, rtp_header
.reference_frame_id
);
33 last_referenced_frame_id_
= rtp_header
.reference_frame_id
;
34 rtp_timestamp_
= rtp_header
.rtp_timestamp
;
36 // Is this the correct frame?
37 if (rtp_header
.frame_id
!= frame_id_
)
40 // Insert every packet only once.
41 if (packets_
.find(rtp_header
.packet_id
) != packets_
.end()) {
45 std::vector
<uint8
> data
;
46 std::pair
<PacketMap::iterator
, bool> retval
=
47 packets_
.insert(make_pair(rtp_header
.packet_id
, data
));
50 retval
.first
->second
.resize(payload_size
);
52 payload_data
, payload_data
+ payload_size
, retval
.first
->second
.begin());
54 ++num_packets_received_
;
55 total_data_size_
+= payload_size
;
58 bool FrameBuffer::Complete() const {
59 return num_packets_received_
- 1 == max_packet_id_
;
62 bool FrameBuffer::AssembleEncodedFrame(EncodedFrame
* frame
) const {
66 // Frame is complete -> construct.
68 frame
->dependency
= EncodedFrame::KEY
;
69 else if (frame_id_
== last_referenced_frame_id_
)
70 frame
->dependency
= EncodedFrame::INDEPENDENT
;
72 frame
->dependency
= EncodedFrame::DEPENDENT
;
73 frame
->frame_id
= frame_id_
;
74 frame
->referenced_frame_id
= last_referenced_frame_id_
;
75 frame
->rtp_timestamp
= rtp_timestamp_
;
77 // Build the data vector.
79 frame
->data
.reserve(total_data_size_
);
80 PacketMap::const_iterator it
;
81 for (it
= packets_
.begin(); it
!= packets_
.end(); ++it
)
82 frame
->data
.insert(frame
->data
.end(), it
->second
.begin(), it
->second
.end());