1 // Copyright 2013 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/framer/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;
31 if (rtp_header.is_reference) {
32 last_referenced_frame_id_ = rtp_header.reference_frame_id;
34 last_referenced_frame_id_ = rtp_header.frame_id - 1;
37 rtp_timestamp_ = rtp_header.webrtc.header.timestamp;
39 // Is this the correct frame?
40 if (rtp_header.frame_id != frame_id_) return;
42 // Insert every packet only once.
43 if (packets_.find(rtp_header.packet_id) != packets_.end()) {
47 std::vector<uint8> data;
48 std::pair<PacketMap::iterator, bool> retval =
49 packets_.insert(make_pair(rtp_header.packet_id, data));
52 retval.first->second.resize(payload_size);
53 std::copy(payload_data, payload_data + payload_size,
54 retval.first->second.begin());
56 ++num_packets_received_;
57 total_data_size_ += payload_size;
60 bool FrameBuffer::Complete() const {
61 return num_packets_received_ - 1 == max_packet_id_;
64 bool FrameBuffer::GetEncodedAudioFrame(
65 transport::EncodedAudioFrame* audio_frame,
66 uint32* rtp_timestamp) const {
67 if (!Complete()) return false;
69 *rtp_timestamp = rtp_timestamp_;
71 // Frame is complete -> construct.
72 audio_frame->frame_id = frame_id_;
74 // Build the data vector.
75 audio_frame->data.clear();
76 audio_frame->data.reserve(total_data_size_);
77 PacketMap::const_iterator it;
78 for (it = packets_.begin(); it != packets_.end(); ++it) {
79 audio_frame->data.insert(audio_frame->data.end(),
80 it->second.begin(), it->second.end());
85 bool FrameBuffer::GetEncodedVideoFrame(
86 transport::EncodedVideoFrame* video_frame,
87 uint32* rtp_timestamp) const {
88 if (!Complete()) return false;
90 *rtp_timestamp = rtp_timestamp_;
92 // Frame is complete -> construct.
93 video_frame->key_frame = is_key_frame_;
94 video_frame->frame_id = frame_id_;
95 video_frame->last_referenced_frame_id = last_referenced_frame_id_;
97 // Build the data vector.
98 video_frame->data.clear();
99 video_frame->data.reserve(total_data_size_);
100 PacketMap::const_iterator it;
101 for (it = packets_.begin(); it != packets_.end(); ++it) {
102 video_frame->data.insert(video_frame->data.end(),
103 it->second.begin(), it->second.end());