Home | History | Annotate | Download | only in framer
      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.
      4 
      5 #include "media/cast/framer/framer.h"
      6 
      7 #include "base/logging.h"
      8 
      9 namespace media {
     10 namespace cast {
     11 
     12 typedef FrameList::const_iterator ConstFrameIterator;
     13 
     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_(new CastMessageBuilder(clock, incoming_payload_feedback,
     21           &frame_id_map_, ssrc, decoder_faster_than_max_frame_rate,
     22           max_unacked_frames)) {
     23   DCHECK(incoming_payload_feedback) << "Invalid argument";
     24 }
     25 
     26 Framer::~Framer() {}
     27 
     28 bool Framer::InsertPacket(const uint8* payload_data,
     29                           size_t payload_size,
     30                           const RtpCastHeader& rtp_header) {
     31   bool complete = false;
     32   if (!frame_id_map_.InsertPacket(rtp_header, &complete)) return false;
     33 
     34   // Does this packet belong to a new frame?
     35   FrameList::iterator it = frames_.find(rtp_header.frame_id);
     36   if (it == frames_.end()) {
     37     // New frame.
     38     linked_ptr<FrameBuffer> frame_buffer(new FrameBuffer());
     39     frame_buffer->InsertPacket(payload_data, payload_size, rtp_header);
     40     frames_.insert(std::make_pair(rtp_header.frame_id, frame_buffer));
     41   } else {
     42     // Insert packet to existing frame buffer.
     43     it->second->InsertPacket(payload_data, payload_size, rtp_header);
     44   }
     45 
     46   if (complete) {
     47     // ACK as soon as possible.
     48     VLOG(1) << "Complete frame " << static_cast<int>(rtp_header.frame_id);
     49     cast_msg_builder_->CompleteFrameReceived(rtp_header.frame_id,
     50                                              rtp_header.is_key_frame);
     51   }
     52   return complete;
     53 }
     54 
     55 // This does not release the frame.
     56 bool Framer::GetEncodedAudioFrame(EncodedAudioFrame* audio_frame,
     57                                   uint32* rtp_timestamp,
     58                                   bool* next_frame) {
     59   uint32 frame_id;
     60   // Find frame id.
     61   if (frame_id_map_.NextContinuousFrame(&frame_id)) {
     62     // We have our next frame.
     63     *next_frame = true;
     64   } else {
     65     if (!frame_id_map_.NextAudioFrameAllowingMissingFrames(&frame_id)) {
     66       return false;
     67     }
     68     *next_frame = false;
     69   }
     70 
     71   ConstFrameIterator it = frames_.find(frame_id);
     72   DCHECK(it != frames_.end());
     73   if (it == frames_.end()) return false;
     74 
     75   return it->second->GetEncodedAudioFrame(audio_frame, rtp_timestamp);
     76 }
     77 
     78 // This does not release the frame.
     79 bool Framer::GetEncodedVideoFrame(EncodedVideoFrame* video_frame,
     80                                   uint32* rtp_timestamp,
     81                                   bool* next_frame) {
     82   uint32 frame_id;
     83   // Find frame id.
     84   if (frame_id_map_.NextContinuousFrame(&frame_id)) {
     85     // We have our next frame.
     86     *next_frame = true;
     87   } else {
     88     // Check if we can skip frames when our decoder is too slow.
     89     if (!decoder_faster_than_max_frame_rate_) return false;
     90 
     91     if (!frame_id_map_.NextVideoFrameAllowingSkippingFrames(&frame_id)) {
     92       return false;
     93     }
     94     *next_frame = false;
     95   }
     96 
     97   ConstFrameIterator it = frames_.find(frame_id);
     98   DCHECK(it != frames_.end());
     99   if (it == frames_.end())  return false;
    100 
    101   return it->second->GetEncodedVideoFrame(video_frame, rtp_timestamp);
    102 }
    103 
    104 void Framer::Reset() {
    105   frame_id_map_.Clear();
    106   frames_.clear();
    107   cast_msg_builder_->Reset();
    108 }
    109 
    110 void Framer::ReleaseFrame(uint32 frame_id) {
    111   frame_id_map_.RemoveOldFrames(frame_id);
    112   frames_.erase(frame_id);
    113 
    114   // We have a frame - remove all frames with lower frame id.
    115   bool skipped_old_frame = false;
    116   FrameList::iterator it;
    117   for (it = frames_.begin(); it != frames_.end(); ) {
    118     if (IsOlderFrameId(it->first, frame_id)) {
    119       frames_.erase(it++);
    120       skipped_old_frame = true;
    121     } else {
    122       ++it;
    123     }
    124   }
    125   if (skipped_old_frame) {
    126     cast_msg_builder_->UpdateCastMessage();
    127   }
    128 }
    129 
    130 bool Framer::TimeToSendNextCastMessage(base::TimeTicks* time_to_send) {
    131   return cast_msg_builder_->TimeToSendNextCastMessage(time_to_send);
    132 }
    133 
    134 void Framer::SendCastMessage() {
    135   cast_msg_builder_->UpdateCastMessage();
    136 }
    137 
    138 }  // namespace cast
    139 }  // namespace media
    140