1 /*
2  *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/video_coding/frame_object.h"
12 
13 #include "common_video/h264/h264_common.h"
14 #include "modules/video_coding/packet_buffer.h"
15 #include "rtc_base/checks.h"
16 
17 namespace webrtc {
18 namespace video_coding {
19 
FrameObject()20 FrameObject::FrameObject()
21     : picture_id(0),
22       spatial_layer(0),
23       timestamp(0),
24       num_references(0),
25       inter_layer_predicted(false) {}
26 
RtpFrameObject(PacketBuffer * packet_buffer,uint16_t first_seq_num,uint16_t last_seq_num,size_t frame_size,int times_nacked,int64_t received_time)27 RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
28                                uint16_t first_seq_num,
29                                uint16_t last_seq_num,
30                                size_t frame_size,
31                                int times_nacked,
32                                int64_t received_time)
33     : packet_buffer_(packet_buffer),
34       first_seq_num_(first_seq_num),
35       last_seq_num_(last_seq_num),
36       timestamp_(0),
37       received_time_(received_time),
38       times_nacked_(times_nacked) {
39   VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num);
40   RTC_CHECK(first_packet);
41 
42   // RtpFrameObject members
43   frame_type_ = first_packet->frameType;
44   codec_type_ = first_packet->codec;
45 
46   // TODO(philipel): Remove when encoded image is replaced by FrameObject.
47   // VCMEncodedFrame members
48   CopyCodecSpecific(&first_packet->video_header);
49   _completeFrame = true;
50   _payloadType = first_packet->payloadType;
51   _timeStamp = first_packet->timestamp;
52   ntp_time_ms_ = first_packet->ntp_time_ms_;
53   _frameType = first_packet->frameType;
54 
55   // Setting frame's playout delays to the same values
56   // as of the first packet's.
57   SetPlayoutDelay(first_packet->video_header.playout_delay);
58 
59   // Since FFmpeg use an optimized bitstream reader that reads in chunks of
60   // 32/64 bits we have to add at least that much padding to the buffer
61   // to make sure the decoder doesn't read out of bounds.
62   // NOTE! EncodedImage::_size is the size of the buffer (think capacity of
63   //       an std::vector) and EncodedImage::_length is the actual size of
64   //       the bitstream (think size of an std::vector).
65   if (codec_type_ == kVideoCodecH264)
66     _size = frame_size + EncodedImage::kBufferPaddingBytesH264;
67   else
68     _size = frame_size;
69 
70   _buffer = new uint8_t[_size];
71   _length = frame_size;
72 
73   bool bitstream_copied = GetBitstream(_buffer);
74   RTC_DCHECK(bitstream_copied);
75   _encodedWidth = first_packet->width;
76   _encodedHeight = first_packet->height;
77 
78   // FrameObject members
79   timestamp = first_packet->timestamp;
80 
81   VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
82   RTC_CHECK(last_packet);
83   RTC_CHECK(last_packet->markerBit);
84   // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
85   // ts_126114v120700p.pdf Section 7.4.5.
86   // The MTSI client shall add the payload bytes as defined in this clause
87   // onto the last RTP packet in each group of packets which make up a key
88   // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
89   // (HEVC)).
90   rotation_ = last_packet->video_header.rotation;
91   _rotation_set = true;
92   content_type_ = last_packet->video_header.content_type;
93   if (last_packet->video_header.video_timing.flags !=
94       TimingFrameFlags::kInvalid) {
95     // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
96     // as this will be dealt with at the time of reporting.
97     timing_.encode_start_ms =
98         ntp_time_ms_ +
99         last_packet->video_header.video_timing.encode_start_delta_ms;
100     timing_.encode_finish_ms =
101         ntp_time_ms_ +
102         last_packet->video_header.video_timing.encode_finish_delta_ms;
103     timing_.packetization_finish_ms =
104         ntp_time_ms_ +
105         last_packet->video_header.video_timing.packetization_finish_delta_ms;
106     timing_.pacer_exit_ms =
107         ntp_time_ms_ +
108         last_packet->video_header.video_timing.pacer_exit_delta_ms;
109     timing_.network_timestamp_ms =
110         ntp_time_ms_ +
111         last_packet->video_header.video_timing.network_timestamp_delta_ms;
112     timing_.network2_timestamp_ms =
113         ntp_time_ms_ +
114         last_packet->video_header.video_timing.network2_timestamp_delta_ms;
115 
116     timing_.receive_start_ms = first_packet->receive_time_ms;
117     timing_.receive_finish_ms = last_packet->receive_time_ms;
118   }
119   timing_.flags = last_packet->video_header.video_timing.flags;
120 }
121 
~RtpFrameObject()122 RtpFrameObject::~RtpFrameObject() {
123   packet_buffer_->ReturnFrame(this);
124 }
125 
first_seq_num() const126 uint16_t RtpFrameObject::first_seq_num() const {
127   return first_seq_num_;
128 }
129 
last_seq_num() const130 uint16_t RtpFrameObject::last_seq_num() const {
131   return last_seq_num_;
132 }
133 
times_nacked() const134 int RtpFrameObject::times_nacked() const {
135   return times_nacked_;
136 }
137 
frame_type() const138 FrameType RtpFrameObject::frame_type() const {
139   return frame_type_;
140 }
141 
codec_type() const142 VideoCodecType RtpFrameObject::codec_type() const {
143   return codec_type_;
144 }
145 
GetBitstream(uint8_t * destination) const146 bool RtpFrameObject::GetBitstream(uint8_t* destination) const {
147   return packet_buffer_->GetBitstream(*this, destination);
148 }
149 
Timestamp() const150 uint32_t RtpFrameObject::Timestamp() const {
151   return timestamp_;
152 }
153 
ReceivedTime() const154 int64_t RtpFrameObject::ReceivedTime() const {
155   return received_time_;
156 }
157 
RenderTime() const158 int64_t RtpFrameObject::RenderTime() const {
159   return _renderTimeMs;
160 }
161 
delayed_by_retransmission() const162 bool RtpFrameObject::delayed_by_retransmission() const {
163   return times_nacked() > 0;
164 }
165 
GetCodecHeader() const166 rtc::Optional<RTPVideoTypeHeader> RtpFrameObject::GetCodecHeader() const {
167   rtc::CritScope lock(&packet_buffer_->crit_);
168   VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
169   if (!packet)
170     return rtc::nullopt;
171   return packet->video_header.codecHeader;
172 }
173 
174 }  // namespace video_coding
175 }  // namespace webrtc
176