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"
12 typedef FrameList::const_iterator ConstFrameIterator
;
14 Framer::Framer(base::TickClock
* clock
,
15 RtpPayloadFeedback
* incoming_payload_feedback
,
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
),
21 new CastMessageBuilder(clock
,
22 incoming_payload_feedback
,
25 decoder_faster_than_max_frame_rate
,
26 max_unacked_frames
)) {
27 DCHECK(incoming_payload_feedback
) << "Invalid argument";
32 bool Framer::InsertPacket(const uint8
* payload_data
,
34 const RtpCastHeader
& rtp_header
,
37 PacketType packet_type
= frame_id_map_
.InsertPacket(rtp_header
);
38 if (packet_type
== kTooOldPacket
) {
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
;
49 // Does this packet belong to a new frame?
50 FrameList::iterator it
= frames_
.find(rtp_header
.frame_id
);
51 if (it
== frames_
.end()) {
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
));
57 // Insert packet to existing frame buffer.
58 it
->second
->InsertPacket(payload_data
, payload_size
, rtp_header
);
61 bool complete
= (packet_type
== kNewPacketCompletingFrame
);
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
);
71 // This does not release the frame.
72 bool Framer::GetEncodedAudioFrame(transport::EncodedAudioFrame
* audio_frame
,
76 if (frame_id_map_
.NextContinuousFrame(&frame_id
)) {
77 // We have our next frame.
80 if (!frame_id_map_
.NextAudioFrameAllowingMissingFrames(&frame_id
)) {
86 ConstFrameIterator it
= frames_
.find(frame_id
);
87 DCHECK(it
!= frames_
.end());
88 if (it
== frames_
.end())
91 return it
->second
->GetEncodedAudioFrame(audio_frame
);
94 // This does not release the frame.
95 bool Framer::GetEncodedVideoFrame(transport::EncodedVideoFrame
* video_frame
,
99 if (frame_id_map_
.NextContinuousFrame(&frame_id
)) {
100 // We have our next frame.
103 // Check if we can skip frames when our decoder is too slow.
104 if (!decoder_faster_than_max_frame_rate_
)
107 if (!frame_id_map_
.NextVideoFrameAllowingSkippingFrames(&frame_id
)) {
113 ConstFrameIterator it
= frames_
.find(frame_id
);
114 DCHECK(it
!= frames_
.end());
115 if (it
== frames_
.end())
118 return it
->second
->GetEncodedVideoFrame(video_frame
);
121 void Framer::Reset() {
122 frame_id_map_
.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
)) {
137 skipped_old_frame
= true;
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(); }