Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / media / cast / framer / framer.cc
blobd510d8b7ba90c7c83730ad92dd12bb93af667a21
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/framer.h"
7 #include "base/logging.h"
9 namespace media {
10 namespace cast {
12 typedef FrameList::const_iterator ConstFrameIterator;
14 Framer::Framer(base::TickClock* clock,
15 RtpPayloadFeedback* incoming_payload_feedback,
16 uint32 ssrc,
17 bool decoder_faster_than_max_frame_rate,
18 int max_unacked_frames)
19 : decoder_faster_than_max_frame_rate_(decoder_faster_than_max_frame_rate),
20 cast_msg_builder_(
21 new CastMessageBuilder(clock,
22 incoming_payload_feedback,
23 &frame_id_map_,
24 ssrc,
25 decoder_faster_than_max_frame_rate,
26 max_unacked_frames)) {
27 DCHECK(incoming_payload_feedback) << "Invalid argument";
30 Framer::~Framer() {}
32 bool Framer::InsertPacket(const uint8* payload_data,
33 size_t payload_size,
34 const RtpCastHeader& rtp_header,
35 bool* duplicate) {
36 *duplicate = false;
37 PacketType packet_type = frame_id_map_.InsertPacket(rtp_header);
38 if (packet_type == kTooOldPacket) {
39 return false;
41 if (packet_type == kDuplicatePacket) {
42 VLOG(3) << "Packet already received, ignored: frame "
43 << static_cast<int>(rtp_header.frame_id) << ", packet "
44 << rtp_header.packet_id;
45 *duplicate = true;
46 return false;
49 // Does this packet belong to a new frame?
50 FrameList::iterator it = frames_.find(rtp_header.frame_id);
51 if (it == frames_.end()) {
52 // New frame.
53 linked_ptr<FrameBuffer> frame_buffer(new FrameBuffer());
54 frame_buffer->InsertPacket(payload_data, payload_size, rtp_header);
55 frames_.insert(std::make_pair(rtp_header.frame_id, frame_buffer));
56 } else {
57 // Insert packet to existing frame buffer.
58 it->second->InsertPacket(payload_data, payload_size, rtp_header);
61 bool complete = (packet_type == kNewPacketCompletingFrame);
62 if (complete) {
63 // ACK as soon as possible.
64 VLOG(2) << "Complete frame " << static_cast<int>(rtp_header.frame_id);
65 cast_msg_builder_->CompleteFrameReceived(rtp_header.frame_id,
66 rtp_header.is_key_frame);
68 return complete;
71 // This does not release the frame.
72 bool Framer::GetEncodedAudioFrame(transport::EncodedAudioFrame* audio_frame,
73 bool* next_frame) {
74 uint32 frame_id;
75 // Find frame id.
76 if (frame_id_map_.NextContinuousFrame(&frame_id)) {
77 // We have our next frame.
78 *next_frame = true;
79 } else {
80 if (!frame_id_map_.NextAudioFrameAllowingMissingFrames(&frame_id)) {
81 return false;
83 *next_frame = false;
86 ConstFrameIterator it = frames_.find(frame_id);
87 DCHECK(it != frames_.end());
88 if (it == frames_.end())
89 return false;
91 return it->second->GetEncodedAudioFrame(audio_frame);
94 // This does not release the frame.
95 bool Framer::GetEncodedVideoFrame(transport::EncodedVideoFrame* video_frame,
96 bool* next_frame) {
97 uint32 frame_id;
98 // Find frame id.
99 if (frame_id_map_.NextContinuousFrame(&frame_id)) {
100 // We have our next frame.
101 *next_frame = true;
102 } else {
103 // Check if we can skip frames when our decoder is too slow.
104 if (!decoder_faster_than_max_frame_rate_)
105 return false;
107 if (!frame_id_map_.NextVideoFrameAllowingSkippingFrames(&frame_id)) {
108 return false;
110 *next_frame = false;
113 ConstFrameIterator it = frames_.find(frame_id);
114 DCHECK(it != frames_.end());
115 if (it == frames_.end())
116 return false;
118 return it->second->GetEncodedVideoFrame(video_frame);
121 void Framer::Reset() {
122 frame_id_map_.Clear();
123 frames_.clear();
124 cast_msg_builder_->Reset();
127 void Framer::ReleaseFrame(uint32 frame_id) {
128 frame_id_map_.RemoveOldFrames(frame_id);
129 frames_.erase(frame_id);
131 // We have a frame - remove all frames with lower frame id.
132 bool skipped_old_frame = false;
133 FrameList::iterator it;
134 for (it = frames_.begin(); it != frames_.end();) {
135 if (IsOlderFrameId(it->first, frame_id)) {
136 frames_.erase(it++);
137 skipped_old_frame = true;
138 } else {
139 ++it;
142 if (skipped_old_frame) {
143 cast_msg_builder_->UpdateCastMessage();
147 bool Framer::TimeToSendNextCastMessage(base::TimeTicks* time_to_send) {
148 return cast_msg_builder_->TimeToSendNextCastMessage(time_to_send);
151 void Framer::SendCastMessage() { cast_msg_builder_->UpdateCastMessage(); }
153 } // namespace cast
154 } // namespace media