blob: 4e124bb83c99e5f8e79e707cbf468b8efe9c71ba [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Chen Xingf00bf422019-06-20 10:05:55 +020015#include <utility>
Yves Gerey3e707812018-11-28 16:47:49 +010016
17#include "api/video/encoded_image.h"
18#include "api/video/video_timing.h"
19#include "modules/video_coding/packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/packet_buffer.h"
21#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/critical_section.h"
philipelc707ab72016-04-01 02:01:54 -070023
24namespace webrtc {
25namespace video_coding {
26
philipel85d5c192019-09-25 17:15:37 +020027// TODO(philipel): Remove this ctor.
philipelb5e47852019-09-20 11:30:12 +020028RtpFrameObject::RtpFrameObject(
29 PacketBuffer* packet_buffer,
30 uint16_t first_seq_num,
31 uint16_t last_seq_num,
philipelb5e47852019-09-20 11:30:12 +020032 int times_nacked,
33 int64_t first_packet_received_time,
34 int64_t last_packet_received_time,
35 RtpPacketInfos packet_infos,
36 rtc::scoped_refptr<EncodedImageBuffer> image_buffer)
Johannes Krona3705562019-08-26 16:37:11 +020037 : first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020038 last_seq_num_(last_seq_num),
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +010039 last_packet_received_time_(last_packet_received_time),
philipel5ceaaae2016-05-24 10:20:47 +020040 times_nacked_(times_nacked) {
Johannes Krona3705562019-08-26 16:37:11 +020041 VCMPacket* first_packet = packet_buffer->GetPacket(first_seq_num);
philipel7d79e632017-05-23 08:19:11 -070042 RTC_CHECK(first_packet);
philipel36928452016-11-07 10:42:36 +010043
Johannes Krona3705562019-08-26 16:37:11 +020044 rtp_video_header_ = first_packet->video_header;
45 rtp_generic_frame_descriptor_ = first_packet->generic_descriptor;
46
philipele7c891f2018-02-22 14:35:06 +010047 // EncodedFrame members
Niels Möllerd5e02f02019-02-20 13:12:21 +010048 codec_type_ = first_packet->codec();
philipel36928452016-11-07 10:42:36 +010049
philipele7c891f2018-02-22 14:35:06 +010050 // TODO(philipel): Remove when encoded image is replaced by EncodedFrame.
philipel266f0a42016-11-28 08:49:07 -080051 // VCMEncodedFrame members
Emircan Uysaler9bb8f052018-01-23 15:53:06 -080052 CopyCodecSpecific(&first_packet->video_header);
philipel266f0a42016-11-28 08:49:07 -080053 _completeFrame = true;
54 _payloadType = first_packet->payloadType;
Niels Möller23775882018-08-16 10:24:12 +020055 SetTimestamp(first_packet->timestamp);
philipel266f0a42016-11-28 08:49:07 -080056 ntp_time_ms_ = first_packet->ntp_time_ms_;
Niels Möllerabbc50e2019-04-24 09:41:16 +020057 _frameType = first_packet->video_header.frame_type;
philipel36928452016-11-07 10:42:36 +010058
gnishb2a318b2017-05-10 09:21:33 -070059 // Setting frame's playout delays to the same values
60 // as of the first packet's.
61 SetPlayoutDelay(first_packet->video_header.playout_delay);
62
philipelb5e47852019-09-20 11:30:12 +020063 SetEncodedData(std::move(image_buffer));
Niels Möllerd5e02f02019-02-20 13:12:21 +010064 _encodedWidth = first_packet->width();
65 _encodedHeight = first_packet->height();
philipel266f0a42016-11-28 08:49:07 -080066
philipele7c891f2018-02-22 14:35:06 +010067 // EncodedFrame members
Niels Möller23775882018-08-16 10:24:12 +020068 SetTimestamp(first_packet->timestamp);
Chen Xingf00bf422019-06-20 10:05:55 +020069 SetPacketInfos(std::move(packet_infos));
philipel266f0a42016-11-28 08:49:07 -080070
Johannes Krona3705562019-08-26 16:37:11 +020071 VCMPacket* last_packet = packet_buffer->GetPacket(last_seq_num);
kwibergee89e782017-08-09 17:22:01 -070072 RTC_CHECK(last_packet);
Niels Möllerd5e02f02019-02-20 13:12:21 +010073 RTC_CHECK(last_packet->is_last_packet_in_frame());
philipel266f0a42016-11-28 08:49:07 -080074 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
75 // ts_126114v120700p.pdf Section 7.4.5.
76 // The MTSI client shall add the payload bytes as defined in this clause
77 // onto the last RTP packet in each group of packets which make up a key
78 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
79 // (HEVC)).
80 rotation_ = last_packet->video_header.rotation;
Danil Chapovalovb7698942019-02-05 11:32:19 +010081 SetColorSpace(last_packet->video_header.color_space);
ilnik00d802b2017-04-11 10:34:31 -070082 content_type_ = last_packet->video_header.content_type;
sprangba050a62017-08-18 02:51:12 -070083 if (last_packet->video_header.video_timing.flags !=
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +020084 VideoSendTiming::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -070085 // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
86 // as this will be dealt with at the time of reporting.
ilnik04f4d122017-06-19 07:18:55 -070087 timing_.encode_start_ms =
88 ntp_time_ms_ +
89 last_packet->video_header.video_timing.encode_start_delta_ms;
90 timing_.encode_finish_ms =
91 ntp_time_ms_ +
92 last_packet->video_header.video_timing.encode_finish_delta_ms;
93 timing_.packetization_finish_ms =
94 ntp_time_ms_ +
95 last_packet->video_header.video_timing.packetization_finish_delta_ms;
96 timing_.pacer_exit_ms =
97 ntp_time_ms_ +
98 last_packet->video_header.video_timing.pacer_exit_delta_ms;
99 timing_.network_timestamp_ms =
100 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100101 last_packet->video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700102 timing_.network2_timestamp_ms =
103 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100104 last_packet->video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700105 }
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100106 timing_.receive_start_ms = first_packet_received_time;
107 timing_.receive_finish_ms = last_packet_received_time;
sprangba050a62017-08-18 02:51:12 -0700108 timing_.flags = last_packet->video_header.video_timing.flags;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100109 is_last_spatial_layer = last_packet->markerBit;
philipel02447bc2016-05-13 06:01:03 -0700110}
philipelc707ab72016-04-01 02:01:54 -0700111
philipel85d5c192019-09-25 17:15:37 +0200112RtpFrameObject::RtpFrameObject(
113 uint16_t first_seq_num,
114 uint16_t last_seq_num,
115 bool markerBit,
116 int times_nacked,
117 int64_t first_packet_received_time,
118 int64_t last_packet_received_time,
119 uint32_t rtp_timestamp,
120 int64_t ntp_time_ms,
121 const VideoSendTiming& timing,
122 uint8_t payload_type,
123 VideoCodecType codec,
124 VideoRotation rotation,
125 VideoContentType content_type,
126 const RTPVideoHeader& video_header,
127 const absl::optional<webrtc::ColorSpace>& color_space,
128 const absl::optional<RtpGenericFrameDescriptor>& generic_descriptor,
129 RtpPacketInfos packet_infos,
130 rtc::scoped_refptr<EncodedImageBuffer> image_buffer)
131 : first_seq_num_(first_seq_num),
132 last_seq_num_(last_seq_num),
133 last_packet_received_time_(last_packet_received_time),
134 times_nacked_(times_nacked) {
135 rtp_video_header_ = video_header;
136 rtp_generic_frame_descriptor_ = generic_descriptor;
137
138 // EncodedFrame members
139 codec_type_ = codec;
140
141 // TODO(philipel): Remove when encoded image is replaced by EncodedFrame.
142 // VCMEncodedFrame members
143 CopyCodecSpecific(&rtp_video_header_);
144 _completeFrame = true;
145 _payloadType = payload_type;
146 SetTimestamp(rtp_timestamp);
147 ntp_time_ms_ = ntp_time_ms;
148 _frameType = rtp_video_header_.frame_type;
149
150 // Setting frame's playout delays to the same values
151 // as of the first packet's.
152 SetPlayoutDelay(rtp_video_header_.playout_delay);
153
154 SetEncodedData(std::move(image_buffer));
155 _encodedWidth = rtp_video_header_.width;
156 _encodedHeight = rtp_video_header_.height;
157
158 // EncodedFrame members
159 SetPacketInfos(std::move(packet_infos));
160
161 rotation_ = rotation;
162 SetColorSpace(color_space);
philipel85d5c192019-09-25 17:15:37 +0200163 content_type_ = content_type;
164 if (timing.flags != VideoSendTiming::kInvalid) {
165 // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
166 // as this will be dealt with at the time of reporting.
167 timing_.encode_start_ms = ntp_time_ms_ + timing.encode_start_delta_ms;
168 timing_.encode_finish_ms = ntp_time_ms_ + timing.encode_finish_delta_ms;
169 timing_.packetization_finish_ms =
170 ntp_time_ms_ + timing.packetization_finish_delta_ms;
171 timing_.pacer_exit_ms = ntp_time_ms_ + timing.pacer_exit_delta_ms;
172 timing_.network_timestamp_ms =
173 ntp_time_ms_ + timing.network_timestamp_delta_ms;
174 timing_.network2_timestamp_ms =
175 ntp_time_ms_ + timing.network2_timestamp_delta_ms;
176 }
177 timing_.receive_start_ms = first_packet_received_time;
178 timing_.receive_finish_ms = last_packet_received_time;
179 timing_.flags = timing.flags;
180 is_last_spatial_layer = markerBit;
181}
182
philipelc707ab72016-04-01 02:01:54 -0700183RtpFrameObject::~RtpFrameObject() {
philipelc707ab72016-04-01 02:01:54 -0700184}
185
philipelf4139332016-04-20 10:26:34 +0200186uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700187 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700188}
189
philipelf4139332016-04-20 10:26:34 +0200190uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700191 return last_seq_num_;
192}
193
philipel5ceaaae2016-05-24 10:20:47 +0200194int RtpFrameObject::times_nacked() const {
195 return times_nacked_;
196}
197
Niels Möller87e2d782019-03-07 10:18:23 +0100198VideoFrameType RtpFrameObject::frame_type() const {
Johannes Krona3705562019-08-26 16:37:11 +0200199 return rtp_video_header_.frame_type;
philipel02447bc2016-05-13 06:01:03 -0700200}
201
202VideoCodecType RtpFrameObject::codec_type() const {
203 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700204}
205
philipelb4d31082016-07-11 08:46:29 -0700206int64_t RtpFrameObject::ReceivedTime() const {
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100207 return last_packet_received_time_;
philipelb4d31082016-07-11 08:46:29 -0700208}
209
210int64_t RtpFrameObject::RenderTime() const {
211 return _renderTimeMs;
212}
213
philipele0754302017-01-25 08:56:23 -0800214bool RtpFrameObject::delayed_by_retransmission() const {
215 return times_nacked() > 0;
216}
217
Johannes Krona3705562019-08-26 16:37:11 +0200218const RTPVideoHeader& RtpFrameObject::GetRtpVideoHeader() const {
219 return rtp_video_header_;
philipel02447bc2016-05-13 06:01:03 -0700220}
221
Johannes Krona3705562019-08-26 16:37:11 +0200222const absl::optional<RtpGenericFrameDescriptor>&
philipel2837edc2018-10-02 13:55:47 +0200223RtpFrameObject::GetGenericFrameDescriptor() const {
Johannes Krona3705562019-08-26 16:37:11 +0200224 return rtp_generic_frame_descriptor_;
philipel2837edc2018-10-02 13:55:47 +0200225}
226
Johannes Krona3705562019-08-26 16:37:11 +0200227const FrameMarking& RtpFrameObject::GetFrameMarking() const {
228 return rtp_video_header_.frame_marking;
Johnny Leee0c8b232018-09-11 16:50:49 -0400229}
230
philipelc707ab72016-04-01 02:01:54 -0700231} // namespace video_coding
232} // namespace webrtc