blob: 884204e34ed85f28c0a207bfd1d8876179def0e4 [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
14
15#include "api/video/encoded_image.h"
16#include "api/video/video_timing.h"
17#include "modules/video_coding/packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/packet_buffer.h"
19#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/critical_section.h"
philipelc707ab72016-04-01 02:01:54 -070021
22namespace webrtc {
23namespace video_coding {
24
25RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
philipel02447bc2016-05-13 06:01:03 -070026 uint16_t first_seq_num,
philipel5ceaaae2016-05-24 10:20:47 +020027 uint16_t last_seq_num,
28 size_t frame_size,
philipelb4d31082016-07-11 08:46:29 -070029 int times_nacked,
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +010030 int64_t first_packet_received_time,
31 int64_t last_packet_received_time)
philipelc707ab72016-04-01 02:01:54 -070032 : packet_buffer_(packet_buffer),
philipel02447bc2016-05-13 06:01:03 -070033 first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020034 last_seq_num_(last_seq_num),
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +010035 last_packet_received_time_(last_packet_received_time),
philipel5ceaaae2016-05-24 10:20:47 +020036 times_nacked_(times_nacked) {
philipel266f0a42016-11-28 08:49:07 -080037 VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num);
philipel7d79e632017-05-23 08:19:11 -070038 RTC_CHECK(first_packet);
philipel36928452016-11-07 10:42:36 +010039
philipele7c891f2018-02-22 14:35:06 +010040 // EncodedFrame members
philipel266f0a42016-11-28 08:49:07 -080041 frame_type_ = first_packet->frameType;
Niels Möllerd5e02f02019-02-20 13:12:21 +010042 codec_type_ = first_packet->codec();
philipel36928452016-11-07 10:42:36 +010043
philipele7c891f2018-02-22 14:35:06 +010044 // TODO(philipel): Remove when encoded image is replaced by EncodedFrame.
philipel266f0a42016-11-28 08:49:07 -080045 // VCMEncodedFrame members
Emircan Uysaler9bb8f052018-01-23 15:53:06 -080046 CopyCodecSpecific(&first_packet->video_header);
philipel266f0a42016-11-28 08:49:07 -080047 _completeFrame = true;
48 _payloadType = first_packet->payloadType;
Niels Möller23775882018-08-16 10:24:12 +020049 SetTimestamp(first_packet->timestamp);
philipel266f0a42016-11-28 08:49:07 -080050 ntp_time_ms_ = first_packet->ntp_time_ms_;
Rasmus Brandt88f080a2017-11-02 14:28:06 +010051 _frameType = first_packet->frameType;
philipel36928452016-11-07 10:42:36 +010052
gnishb2a318b2017-05-10 09:21:33 -070053 // Setting frame's playout delays to the same values
54 // as of the first packet's.
55 SetPlayoutDelay(first_packet->video_header.playout_delay);
56
philipel5fb24542018-10-02 17:01:45 +020057 AllocateBitstreamBuffer(frame_size);
Niels Möller24871e42019-01-17 11:31:13 +010058 bool bitstream_copied = packet_buffer_->GetBitstream(*this, data());
philipel227f8b92017-08-04 06:39:31 -070059 RTC_DCHECK(bitstream_copied);
Niels Möllerd5e02f02019-02-20 13:12:21 +010060 _encodedWidth = first_packet->width();
61 _encodedHeight = first_packet->height();
philipel266f0a42016-11-28 08:49:07 -080062
philipele7c891f2018-02-22 14:35:06 +010063 // EncodedFrame members
Niels Möller23775882018-08-16 10:24:12 +020064 SetTimestamp(first_packet->timestamp);
philipel266f0a42016-11-28 08:49:07 -080065
66 VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
kwibergee89e782017-08-09 17:22:01 -070067 RTC_CHECK(last_packet);
Niels Möllerd5e02f02019-02-20 13:12:21 +010068 RTC_CHECK(last_packet->is_last_packet_in_frame());
philipel266f0a42016-11-28 08:49:07 -080069 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
70 // ts_126114v120700p.pdf Section 7.4.5.
71 // The MTSI client shall add the payload bytes as defined in this clause
72 // onto the last RTP packet in each group of packets which make up a key
73 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
74 // (HEVC)).
75 rotation_ = last_packet->video_header.rotation;
Danil Chapovalovb7698942019-02-05 11:32:19 +010076 SetColorSpace(last_packet->video_header.color_space);
philipel266f0a42016-11-28 08:49:07 -080077 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -070078 content_type_ = last_packet->video_header.content_type;
sprangba050a62017-08-18 02:51:12 -070079 if (last_packet->video_header.video_timing.flags !=
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +020080 VideoSendTiming::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -070081 // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
82 // as this will be dealt with at the time of reporting.
ilnik04f4d122017-06-19 07:18:55 -070083 timing_.encode_start_ms =
84 ntp_time_ms_ +
85 last_packet->video_header.video_timing.encode_start_delta_ms;
86 timing_.encode_finish_ms =
87 ntp_time_ms_ +
88 last_packet->video_header.video_timing.encode_finish_delta_ms;
89 timing_.packetization_finish_ms =
90 ntp_time_ms_ +
91 last_packet->video_header.video_timing.packetization_finish_delta_ms;
92 timing_.pacer_exit_ms =
93 ntp_time_ms_ +
94 last_packet->video_header.video_timing.pacer_exit_delta_ms;
95 timing_.network_timestamp_ms =
96 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +010097 last_packet->video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -070098 timing_.network2_timestamp_ms =
99 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100100 last_packet->video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700101 }
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100102 timing_.receive_start_ms = first_packet_received_time;
103 timing_.receive_finish_ms = last_packet_received_time;
sprangba050a62017-08-18 02:51:12 -0700104 timing_.flags = last_packet->video_header.video_timing.flags;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100105 is_last_spatial_layer = last_packet->markerBit;
philipel02447bc2016-05-13 06:01:03 -0700106}
philipelc707ab72016-04-01 02:01:54 -0700107
108RtpFrameObject::~RtpFrameObject() {
109 packet_buffer_->ReturnFrame(this);
110}
111
philipelf4139332016-04-20 10:26:34 +0200112uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700113 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700114}
115
philipelf4139332016-04-20 10:26:34 +0200116uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700117 return last_seq_num_;
118}
119
philipel5ceaaae2016-05-24 10:20:47 +0200120int RtpFrameObject::times_nacked() const {
121 return times_nacked_;
122}
123
Niels Möller87e2d782019-03-07 10:18:23 +0100124VideoFrameType RtpFrameObject::frame_type() const {
philipel02447bc2016-05-13 06:01:03 -0700125 return frame_type_;
126}
127
128VideoCodecType RtpFrameObject::codec_type() const {
129 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700130}
131
philipelb4d31082016-07-11 08:46:29 -0700132int64_t RtpFrameObject::ReceivedTime() const {
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100133 return last_packet_received_time_;
philipelb4d31082016-07-11 08:46:29 -0700134}
135
136int64_t RtpFrameObject::RenderTime() const {
137 return _renderTimeMs;
138}
139
philipele0754302017-01-25 08:56:23 -0800140bool RtpFrameObject::delayed_by_retransmission() const {
141 return times_nacked() > 0;
142}
143
philipel5470f402018-09-07 13:38:53 +0200144absl::optional<RTPVideoHeader> RtpFrameObject::GetRtpVideoHeader() const {
philipel88488282016-11-03 08:56:54 -0700145 rtc::CritScope lock(&packet_buffer_->crit_);
philipel02447bc2016-05-13 06:01:03 -0700146 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
147 if (!packet)
Danil Chapovalov0040b662018-06-18 10:48:16 +0200148 return absl::nullopt;
Sami Kalliomäki98824952018-08-28 14:39:21 +0200149 return packet->video_header;
philipel02447bc2016-05-13 06:01:03 -0700150}
151
philipel2837edc2018-10-02 13:55:47 +0200152absl::optional<RtpGenericFrameDescriptor>
153RtpFrameObject::GetGenericFrameDescriptor() const {
154 rtc::CritScope lock(&packet_buffer_->crit_);
155 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
156 if (!packet)
157 return absl::nullopt;
158 return packet->generic_descriptor;
159}
160
Johnny Leee0c8b232018-09-11 16:50:49 -0400161absl::optional<FrameMarking> RtpFrameObject::GetFrameMarking() const {
162 rtc::CritScope lock(&packet_buffer_->crit_);
163 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
164 if (!packet)
165 return absl::nullopt;
166 return packet->video_header.frame_marking;
167}
168
philipel5fb24542018-10-02 17:01:45 +0200169void RtpFrameObject::AllocateBitstreamBuffer(size_t frame_size) {
170 // Since FFmpeg use an optimized bitstream reader that reads in chunks of
171 // 32/64 bits we have to add at least that much padding to the buffer
172 // to make sure the decoder doesn't read out of bounds.
philipel5fb24542018-10-02 17:01:45 +0200173 size_t new_size = frame_size + (codec_type_ == kVideoCodecH264
174 ? EncodedImage::kBufferPaddingBytesH264
175 : 0);
Niels Möller48a79462018-12-07 16:21:18 +0100176 if (capacity() < new_size) {
Niels Möller938dd9f2019-02-07 00:02:17 +0100177 Allocate(new_size);
philipel5fb24542018-10-02 17:01:45 +0200178 }
179
Niels Möller77536a22019-01-15 08:50:01 +0100180 set_size(frame_size);
philipel5fb24542018-10-02 17:01:45 +0200181}
182
philipelc707ab72016-04-01 02:01:54 -0700183} // namespace video_coding
184} // namespace webrtc