blob: b4894269e18efb55d22c2464381c27d677d78d62 [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
27RtpFrameObject::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,
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +010032 int64_t first_packet_received_time,
Chen Xingf00bf422019-06-20 10:05:55 +020033 int64_t last_packet_received_time,
34 RtpPacketInfos packet_infos)
Johannes Krona3705562019-08-26 16:37:11 +020035 : first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020036 last_seq_num_(last_seq_num),
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +010037 last_packet_received_time_(last_packet_received_time),
philipel5ceaaae2016-05-24 10:20:47 +020038 times_nacked_(times_nacked) {
Johannes Krona3705562019-08-26 16:37:11 +020039 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
Johannes Krona3705562019-08-26 16:37:11 +020042 rtp_video_header_ = first_packet->video_header;
43 rtp_generic_frame_descriptor_ = first_packet->generic_descriptor;
44
philipele7c891f2018-02-22 14:35:06 +010045 // EncodedFrame members
Niels Möllerd5e02f02019-02-20 13:12:21 +010046 codec_type_ = first_packet->codec();
philipel36928452016-11-07 10:42:36 +010047
philipele7c891f2018-02-22 14:35:06 +010048 // TODO(philipel): Remove when encoded image is replaced by EncodedFrame.
philipel266f0a42016-11-28 08:49:07 -080049 // VCMEncodedFrame members
Emircan Uysaler9bb8f052018-01-23 15:53:06 -080050 CopyCodecSpecific(&first_packet->video_header);
philipel266f0a42016-11-28 08:49:07 -080051 _completeFrame = true;
52 _payloadType = first_packet->payloadType;
Niels Möller23775882018-08-16 10:24:12 +020053 SetTimestamp(first_packet->timestamp);
philipel266f0a42016-11-28 08:49:07 -080054 ntp_time_ms_ = first_packet->ntp_time_ms_;
Niels Möllerabbc50e2019-04-24 09:41:16 +020055 _frameType = first_packet->video_header.frame_type;
philipel36928452016-11-07 10:42:36 +010056
gnishb2a318b2017-05-10 09:21:33 -070057 // Setting frame's playout delays to the same values
58 // as of the first packet's.
59 SetPlayoutDelay(first_packet->video_header.playout_delay);
60
Niels Möller4d504c72019-06-18 15:56:56 +020061 // TODO(nisse): Change GetBitstream to return the buffer?
62 SetEncodedData(EncodedImageBuffer::Create(frame_size));
Johannes Krona3705562019-08-26 16:37:11 +020063 bool bitstream_copied = packet_buffer->GetBitstream(*this, data());
philipel227f8b92017-08-04 06:39:31 -070064 RTC_DCHECK(bitstream_copied);
Niels Möllerd5e02f02019-02-20 13:12:21 +010065 _encodedWidth = first_packet->width();
66 _encodedHeight = first_packet->height();
philipel266f0a42016-11-28 08:49:07 -080067
philipele7c891f2018-02-22 14:35:06 +010068 // EncodedFrame members
Niels Möller23775882018-08-16 10:24:12 +020069 SetTimestamp(first_packet->timestamp);
Chen Xingf00bf422019-06-20 10:05:55 +020070 SetPacketInfos(std::move(packet_infos));
philipel266f0a42016-11-28 08:49:07 -080071
Johannes Krona3705562019-08-26 16:37:11 +020072 VCMPacket* last_packet = packet_buffer->GetPacket(last_seq_num);
kwibergee89e782017-08-09 17:22:01 -070073 RTC_CHECK(last_packet);
Niels Möllerd5e02f02019-02-20 13:12:21 +010074 RTC_CHECK(last_packet->is_last_packet_in_frame());
philipel266f0a42016-11-28 08:49:07 -080075 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
76 // ts_126114v120700p.pdf Section 7.4.5.
77 // The MTSI client shall add the payload bytes as defined in this clause
78 // onto the last RTP packet in each group of packets which make up a key
79 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
80 // (HEVC)).
81 rotation_ = last_packet->video_header.rotation;
Danil Chapovalovb7698942019-02-05 11:32:19 +010082 SetColorSpace(last_packet->video_header.color_space);
philipel266f0a42016-11-28 08:49:07 -080083 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -070084 content_type_ = last_packet->video_header.content_type;
sprangba050a62017-08-18 02:51:12 -070085 if (last_packet->video_header.video_timing.flags !=
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +020086 VideoSendTiming::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -070087 // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
88 // as this will be dealt with at the time of reporting.
ilnik04f4d122017-06-19 07:18:55 -070089 timing_.encode_start_ms =
90 ntp_time_ms_ +
91 last_packet->video_header.video_timing.encode_start_delta_ms;
92 timing_.encode_finish_ms =
93 ntp_time_ms_ +
94 last_packet->video_header.video_timing.encode_finish_delta_ms;
95 timing_.packetization_finish_ms =
96 ntp_time_ms_ +
97 last_packet->video_header.video_timing.packetization_finish_delta_ms;
98 timing_.pacer_exit_ms =
99 ntp_time_ms_ +
100 last_packet->video_header.video_timing.pacer_exit_delta_ms;
101 timing_.network_timestamp_ms =
102 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100103 last_packet->video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700104 timing_.network2_timestamp_ms =
105 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +0100106 last_packet->video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -0700107 }
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100108 timing_.receive_start_ms = first_packet_received_time;
109 timing_.receive_finish_ms = last_packet_received_time;
sprangba050a62017-08-18 02:51:12 -0700110 timing_.flags = last_packet->video_header.video_timing.flags;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100111 is_last_spatial_layer = last_packet->markerBit;
philipel02447bc2016-05-13 06:01:03 -0700112}
philipelc707ab72016-04-01 02:01:54 -0700113
114RtpFrameObject::~RtpFrameObject() {
philipelc707ab72016-04-01 02:01:54 -0700115}
116
philipelf4139332016-04-20 10:26:34 +0200117uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700118 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700119}
120
philipelf4139332016-04-20 10:26:34 +0200121uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700122 return last_seq_num_;
123}
124
philipel5ceaaae2016-05-24 10:20:47 +0200125int RtpFrameObject::times_nacked() const {
126 return times_nacked_;
127}
128
Niels Möller87e2d782019-03-07 10:18:23 +0100129VideoFrameType RtpFrameObject::frame_type() const {
Johannes Krona3705562019-08-26 16:37:11 +0200130 return rtp_video_header_.frame_type;
philipel02447bc2016-05-13 06:01:03 -0700131}
132
133VideoCodecType RtpFrameObject::codec_type() const {
134 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700135}
136
philipelb4d31082016-07-11 08:46:29 -0700137int64_t RtpFrameObject::ReceivedTime() const {
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100138 return last_packet_received_time_;
philipelb4d31082016-07-11 08:46:29 -0700139}
140
141int64_t RtpFrameObject::RenderTime() const {
142 return _renderTimeMs;
143}
144
philipele0754302017-01-25 08:56:23 -0800145bool RtpFrameObject::delayed_by_retransmission() const {
146 return times_nacked() > 0;
147}
148
Johannes Krona3705562019-08-26 16:37:11 +0200149const RTPVideoHeader& RtpFrameObject::GetRtpVideoHeader() const {
150 return rtp_video_header_;
philipel02447bc2016-05-13 06:01:03 -0700151}
152
Johannes Krona3705562019-08-26 16:37:11 +0200153const absl::optional<RtpGenericFrameDescriptor>&
philipel2837edc2018-10-02 13:55:47 +0200154RtpFrameObject::GetGenericFrameDescriptor() const {
Johannes Krona3705562019-08-26 16:37:11 +0200155 return rtp_generic_frame_descriptor_;
philipel2837edc2018-10-02 13:55:47 +0200156}
157
Johannes Krona3705562019-08-26 16:37:11 +0200158const FrameMarking& RtpFrameObject::GetFrameMarking() const {
159 return rtp_video_header_.frame_marking;
Johnny Leee0c8b232018-09-11 16:50:49 -0400160}
161
philipelc707ab72016-04-01 02:01:54 -0700162} // namespace video_coding
163} // namespace webrtc