blob: 5a485dadaddaaa669e482eeeb2a2638a451eec5f [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>
Chen Xingf00bf422019-06-20 10:05:55 +020014#include <utility>
Yves Gerey3e707812018-11-28 16:47:49 +010015
16#include "api/video/encoded_image.h"
17#include "api/video/video_timing.h"
18#include "modules/video_coding/packet.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/video_coding/packet_buffer.h"
20#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/critical_section.h"
philipelc707ab72016-04-01 02:01:54 -070022
23namespace webrtc {
24namespace video_coding {
25
26RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
philipel02447bc2016-05-13 06:01:03 -070027 uint16_t first_seq_num,
philipel5ceaaae2016-05-24 10:20:47 +020028 uint16_t last_seq_num,
29 size_t frame_size,
philipelb4d31082016-07-11 08:46:29 -070030 int times_nacked,
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +010031 int64_t first_packet_received_time,
Chen Xingf00bf422019-06-20 10:05:55 +020032 int64_t last_packet_received_time,
33 RtpPacketInfos packet_infos)
philipelc707ab72016-04-01 02:01:54 -070034 : packet_buffer_(packet_buffer),
philipel02447bc2016-05-13 06:01:03 -070035 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) {
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
philipele7c891f2018-02-22 14:35:06 +010042 // EncodedFrame members
Niels Möllerabbc50e2019-04-24 09:41:16 +020043 frame_type_ = first_packet->video_header.frame_type;
Niels Möllerd5e02f02019-02-20 13:12:21 +010044 codec_type_ = first_packet->codec();
philipel36928452016-11-07 10:42:36 +010045
philipele7c891f2018-02-22 14:35:06 +010046 // TODO(philipel): Remove when encoded image is replaced by EncodedFrame.
philipel266f0a42016-11-28 08:49:07 -080047 // VCMEncodedFrame members
Emircan Uysaler9bb8f052018-01-23 15:53:06 -080048 CopyCodecSpecific(&first_packet->video_header);
philipel266f0a42016-11-28 08:49:07 -080049 _completeFrame = true;
50 _payloadType = first_packet->payloadType;
Niels Möller23775882018-08-16 10:24:12 +020051 SetTimestamp(first_packet->timestamp);
philipel266f0a42016-11-28 08:49:07 -080052 ntp_time_ms_ = first_packet->ntp_time_ms_;
Niels Möllerabbc50e2019-04-24 09:41:16 +020053 _frameType = first_packet->video_header.frame_type;
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
Niels Möller4d504c72019-06-18 15:56:56 +020059 // TODO(nisse): Change GetBitstream to return the buffer?
60 SetEncodedData(EncodedImageBuffer::Create(frame_size));
Niels Möller24871e42019-01-17 11:31:13 +010061 bool bitstream_copied = packet_buffer_->GetBitstream(*this, data());
philipel227f8b92017-08-04 06:39:31 -070062 RTC_DCHECK(bitstream_copied);
Niels Möllerd5e02f02019-02-20 13:12:21 +010063 _encodedWidth = first_packet->width();
64 _encodedHeight = first_packet->height();
philipel266f0a42016-11-28 08:49:07 -080065
philipele7c891f2018-02-22 14:35:06 +010066 // EncodedFrame members
Niels Möller23775882018-08-16 10:24:12 +020067 SetTimestamp(first_packet->timestamp);
Chen Xingf00bf422019-06-20 10:05:55 +020068 SetPacketInfos(std::move(packet_infos));
philipel266f0a42016-11-28 08:49:07 -080069
70 VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
kwibergee89e782017-08-09 17:22:01 -070071 RTC_CHECK(last_packet);
Niels Möllerd5e02f02019-02-20 13:12:21 +010072 RTC_CHECK(last_packet->is_last_packet_in_frame());
philipel266f0a42016-11-28 08:49:07 -080073 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
74 // ts_126114v120700p.pdf Section 7.4.5.
75 // The MTSI client shall add the payload bytes as defined in this clause
76 // onto the last RTP packet in each group of packets which make up a key
77 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
78 // (HEVC)).
79 rotation_ = last_packet->video_header.rotation;
Danil Chapovalovb7698942019-02-05 11:32:19 +010080 SetColorSpace(last_packet->video_header.color_space);
philipel266f0a42016-11-28 08:49:07 -080081 _rotation_set = true;
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
112RtpFrameObject::~RtpFrameObject() {
113 packet_buffer_->ReturnFrame(this);
114}
115
philipelf4139332016-04-20 10:26:34 +0200116uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700117 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700118}
119
philipelf4139332016-04-20 10:26:34 +0200120uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700121 return last_seq_num_;
122}
123
philipel5ceaaae2016-05-24 10:20:47 +0200124int RtpFrameObject::times_nacked() const {
125 return times_nacked_;
126}
127
Niels Möller87e2d782019-03-07 10:18:23 +0100128VideoFrameType RtpFrameObject::frame_type() const {
philipel02447bc2016-05-13 06:01:03 -0700129 return frame_type_;
130}
131
132VideoCodecType RtpFrameObject::codec_type() const {
133 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700134}
135
philipelb4d31082016-07-11 08:46:29 -0700136int64_t RtpFrameObject::ReceivedTime() const {
Ilya Nikolaevskiy4348ce22018-12-07 16:26:56 +0100137 return last_packet_received_time_;
philipelb4d31082016-07-11 08:46:29 -0700138}
139
140int64_t RtpFrameObject::RenderTime() const {
141 return _renderTimeMs;
142}
143
philipele0754302017-01-25 08:56:23 -0800144bool RtpFrameObject::delayed_by_retransmission() const {
145 return times_nacked() > 0;
146}
147
philipel5470f402018-09-07 13:38:53 +0200148absl::optional<RTPVideoHeader> RtpFrameObject::GetRtpVideoHeader() const {
philipel88488282016-11-03 08:56:54 -0700149 rtc::CritScope lock(&packet_buffer_->crit_);
philipel02447bc2016-05-13 06:01:03 -0700150 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
151 if (!packet)
Danil Chapovalov0040b662018-06-18 10:48:16 +0200152 return absl::nullopt;
Sami Kalliomäki98824952018-08-28 14:39:21 +0200153 return packet->video_header;
philipel02447bc2016-05-13 06:01:03 -0700154}
155
philipel2837edc2018-10-02 13:55:47 +0200156absl::optional<RtpGenericFrameDescriptor>
157RtpFrameObject::GetGenericFrameDescriptor() const {
158 rtc::CritScope lock(&packet_buffer_->crit_);
159 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
160 if (!packet)
161 return absl::nullopt;
162 return packet->generic_descriptor;
163}
164
Johnny Leee0c8b232018-09-11 16:50:49 -0400165absl::optional<FrameMarking> RtpFrameObject::GetFrameMarking() const {
166 rtc::CritScope lock(&packet_buffer_->crit_);
167 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
168 if (!packet)
169 return absl::nullopt;
170 return packet->video_header.frame_marking;
171}
172
philipelc707ab72016-04-01 02:01:54 -0700173} // namespace video_coding
174} // namespace webrtc