blob: 6a31cfd9b6e5ee490f4982d408198b385ba1d08b [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/frame_object.h"
Rasmus Brandt58b72912017-10-25 11:09:23 +020012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "common_video/h264/h264_common.h"
14#include "modules/video_coding/packet_buffer.h"
15#include "rtc_base/checks.h"
philipelc707ab72016-04-01 02:01:54 -070016
17namespace webrtc {
18namespace video_coding {
19
philipela1059872016-05-09 11:41:48 +020020FrameObject::FrameObject()
21 : picture_id(0),
22 spatial_layer(0),
philipelbe7a9e52016-05-19 12:19:35 +020023 timestamp(0),
philipela1059872016-05-09 11:41:48 +020024 num_references(0),
25 inter_layer_predicted(false) {}
26
philipelc707ab72016-04-01 02:01:54 -070027RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
philipel02447bc2016-05-13 06:01:03 -070028 uint16_t first_seq_num,
philipel5ceaaae2016-05-24 10:20:47 +020029 uint16_t last_seq_num,
30 size_t frame_size,
philipelb4d31082016-07-11 08:46:29 -070031 int times_nacked,
32 int64_t received_time)
philipelc707ab72016-04-01 02:01:54 -070033 : packet_buffer_(packet_buffer),
philipel02447bc2016-05-13 06:01:03 -070034 first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020035 last_seq_num_(last_seq_num),
sprangba050a62017-08-18 02:51:12 -070036 timestamp_(0),
philipelb4d31082016-07-11 08:46:29 -070037 received_time_(received_time),
philipel5ceaaae2016-05-24 10:20:47 +020038 times_nacked_(times_nacked) {
philipel266f0a42016-11-28 08:49:07 -080039 VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num);
philipel7d79e632017-05-23 08:19:11 -070040 RTC_CHECK(first_packet);
philipel36928452016-11-07 10:42:36 +010041
philipel266f0a42016-11-28 08:49:07 -080042 // RtpFrameObject members
43 frame_type_ = first_packet->frameType;
44 codec_type_ = first_packet->codec;
philipel36928452016-11-07 10:42:36 +010045
philipel266f0a42016-11-28 08:49:07 -080046 // TODO(philipel): Remove when encoded image is replaced by FrameObject.
47 // VCMEncodedFrame members
Philip Eliassondeb86632017-11-29 11:39:27 +000048 CopyCodecSpecific(&first_packet->video_header);
philipel266f0a42016-11-28 08:49:07 -080049 _completeFrame = true;
50 _payloadType = first_packet->payloadType;
51 _timeStamp = first_packet->timestamp;
52 ntp_time_ms_ = first_packet->ntp_time_ms_;
Rasmus Brandt88f080a2017-11-02 14:28:06 +010053 _frameType = first_packet->frameType;
philipel36928452016-11-07 10:42:36 +010054
gnishb2a318b2017-05-10 09:21:33 -070055 // 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
philipel266f0a42016-11-28 08:49:07 -080059 // 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;
philipel552866c2016-07-04 16:18:55 +020069
philipel266f0a42016-11-28 08:49:07 -080070 _buffer = new uint8_t[_size];
71 _length = frame_size;
philipele87c8762017-05-19 06:38:50 -070072
philipel227f8b92017-08-04 06:39:31 -070073 bool bitstream_copied = GetBitstream(_buffer);
74 RTC_DCHECK(bitstream_copied);
philipel59e99b72017-01-12 03:26:04 -080075 _encodedWidth = first_packet->width;
76 _encodedHeight = first_packet->height;
philipel266f0a42016-11-28 08:49:07 -080077
78 // FrameObject members
79 timestamp = first_packet->timestamp;
80
81 VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
kwibergee89e782017-08-09 17:22:01 -070082 RTC_CHECK(last_packet);
83 RTC_CHECK(last_packet->markerBit);
philipel266f0a42016-11-28 08:49:07 -080084 // 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;
ilnik00d802b2017-04-11 10:34:31 -070092 content_type_ = last_packet->video_header.content_type;
sprangba050a62017-08-18 02:51:12 -070093 if (last_packet->video_header.video_timing.flags !=
94 TimingFrameFlags::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -070095 // 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.
ilnik04f4d122017-06-19 07:18:55 -070097 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_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100111 last_packet->video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700112 timing_.network2_timestamp_ms =
113 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100114 last_packet->video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700115
116 timing_.receive_start_ms = first_packet->receive_time_ms;
117 timing_.receive_finish_ms = last_packet->receive_time_ms;
ilnik04f4d122017-06-19 07:18:55 -0700118 }
sprangba050a62017-08-18 02:51:12 -0700119 timing_.flags = last_packet->video_header.video_timing.flags;
philipel02447bc2016-05-13 06:01:03 -0700120}
philipelc707ab72016-04-01 02:01:54 -0700121
122RtpFrameObject::~RtpFrameObject() {
123 packet_buffer_->ReturnFrame(this);
124}
125
philipelf4139332016-04-20 10:26:34 +0200126uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700127 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700128}
129
philipelf4139332016-04-20 10:26:34 +0200130uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700131 return last_seq_num_;
132}
133
philipel5ceaaae2016-05-24 10:20:47 +0200134int RtpFrameObject::times_nacked() const {
135 return times_nacked_;
136}
137
philipel02447bc2016-05-13 06:01:03 -0700138FrameType RtpFrameObject::frame_type() const {
139 return frame_type_;
140}
141
142VideoCodecType RtpFrameObject::codec_type() const {
143 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700144}
145
philipelc707ab72016-04-01 02:01:54 -0700146bool RtpFrameObject::GetBitstream(uint8_t* destination) const {
147 return packet_buffer_->GetBitstream(*this, destination);
148}
149
philipelb4d31082016-07-11 08:46:29 -0700150uint32_t RtpFrameObject::Timestamp() const {
151 return timestamp_;
152}
153
154int64_t RtpFrameObject::ReceivedTime() const {
155 return received_time_;
156}
157
158int64_t RtpFrameObject::RenderTime() const {
159 return _renderTimeMs;
160}
161
philipele0754302017-01-25 08:56:23 -0800162bool RtpFrameObject::delayed_by_retransmission() const {
163 return times_nacked() > 0;
164}
165
philipel88488282016-11-03 08:56:54 -0700166rtc::Optional<RTPVideoTypeHeader> RtpFrameObject::GetCodecHeader() const {
167 rtc::CritScope lock(&packet_buffer_->crit_);
philipel02447bc2016-05-13 06:01:03 -0700168 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
169 if (!packet)
Oskar Sundbom6bd39022017-11-16 10:54:49 +0100170 return rtc::nullopt;
171 return packet->video_header.codecHeader;
philipel02447bc2016-05-13 06:01:03 -0700172}
173
philipelc707ab72016-04-01 02:01:54 -0700174} // namespace video_coding
175} // namespace webrtc