blob: 0172c55493bba9058f2997c894b50d7990af7e8a [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
20RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer,
philipel02447bc2016-05-13 06:01:03 -070021 uint16_t first_seq_num,
philipel5ceaaae2016-05-24 10:20:47 +020022 uint16_t last_seq_num,
23 size_t frame_size,
philipelb4d31082016-07-11 08:46:29 -070024 int times_nacked,
25 int64_t received_time)
philipelc707ab72016-04-01 02:01:54 -070026 : packet_buffer_(packet_buffer),
philipel02447bc2016-05-13 06:01:03 -070027 first_seq_num_(first_seq_num),
philipel5ceaaae2016-05-24 10:20:47 +020028 last_seq_num_(last_seq_num),
philipelb4d31082016-07-11 08:46:29 -070029 received_time_(received_time),
philipel5ceaaae2016-05-24 10:20:47 +020030 times_nacked_(times_nacked) {
philipel266f0a42016-11-28 08:49:07 -080031 VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num);
philipel7d79e632017-05-23 08:19:11 -070032 RTC_CHECK(first_packet);
philipel36928452016-11-07 10:42:36 +010033
philipele7c891f2018-02-22 14:35:06 +010034 // EncodedFrame members
philipel266f0a42016-11-28 08:49:07 -080035 frame_type_ = first_packet->frameType;
36 codec_type_ = first_packet->codec;
philipel36928452016-11-07 10:42:36 +010037
philipele7c891f2018-02-22 14:35:06 +010038 // TODO(philipel): Remove when encoded image is replaced by EncodedFrame.
philipel266f0a42016-11-28 08:49:07 -080039 // VCMEncodedFrame members
Emircan Uysaler9bb8f052018-01-23 15:53:06 -080040 CopyCodecSpecific(&first_packet->video_header);
philipel266f0a42016-11-28 08:49:07 -080041 _completeFrame = true;
42 _payloadType = first_packet->payloadType;
Niels Möller23775882018-08-16 10:24:12 +020043 SetTimestamp(first_packet->timestamp);
philipel266f0a42016-11-28 08:49:07 -080044 ntp_time_ms_ = first_packet->ntp_time_ms_;
Rasmus Brandt88f080a2017-11-02 14:28:06 +010045 _frameType = first_packet->frameType;
philipel36928452016-11-07 10:42:36 +010046
gnishb2a318b2017-05-10 09:21:33 -070047 // Setting frame's playout delays to the same values
48 // as of the first packet's.
49 SetPlayoutDelay(first_packet->video_header.playout_delay);
50
philipel5fb24542018-10-02 17:01:45 +020051 AllocateBitstreamBuffer(frame_size);
philipel227f8b92017-08-04 06:39:31 -070052 bool bitstream_copied = GetBitstream(_buffer);
53 RTC_DCHECK(bitstream_copied);
philipel59e99b72017-01-12 03:26:04 -080054 _encodedWidth = first_packet->width;
55 _encodedHeight = first_packet->height;
philipel266f0a42016-11-28 08:49:07 -080056
philipele7c891f2018-02-22 14:35:06 +010057 // EncodedFrame members
Niels Möller23775882018-08-16 10:24:12 +020058 SetTimestamp(first_packet->timestamp);
philipel266f0a42016-11-28 08:49:07 -080059
60 VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num);
kwibergee89e782017-08-09 17:22:01 -070061 RTC_CHECK(last_packet);
philipelef615ea2018-09-13 11:07:48 +020062 RTC_CHECK(last_packet->is_last_packet_in_frame);
philipel266f0a42016-11-28 08:49:07 -080063 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/
64 // ts_126114v120700p.pdf Section 7.4.5.
65 // The MTSI client shall add the payload bytes as defined in this clause
66 // onto the last RTP packet in each group of packets which make up a key
67 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265
68 // (HEVC)).
69 rotation_ = last_packet->video_header.rotation;
70 _rotation_set = true;
ilnik00d802b2017-04-11 10:34:31 -070071 content_type_ = last_packet->video_header.content_type;
sprangba050a62017-08-18 02:51:12 -070072 if (last_packet->video_header.video_timing.flags !=
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +020073 VideoSendTiming::kInvalid) {
ilnik04f4d122017-06-19 07:18:55 -070074 // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem,
75 // as this will be dealt with at the time of reporting.
ilnik04f4d122017-06-19 07:18:55 -070076 timing_.encode_start_ms =
77 ntp_time_ms_ +
78 last_packet->video_header.video_timing.encode_start_delta_ms;
79 timing_.encode_finish_ms =
80 ntp_time_ms_ +
81 last_packet->video_header.video_timing.encode_finish_delta_ms;
82 timing_.packetization_finish_ms =
83 ntp_time_ms_ +
84 last_packet->video_header.video_timing.packetization_finish_delta_ms;
85 timing_.pacer_exit_ms =
86 ntp_time_ms_ +
87 last_packet->video_header.video_timing.pacer_exit_delta_ms;
88 timing_.network_timestamp_ms =
89 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +010090 last_packet->video_header.video_timing.network_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -070091 timing_.network2_timestamp_ms =
92 ntp_time_ms_ +
Danil Chapovalov996eb9e2017-10-30 17:14:41 +010093 last_packet->video_header.video_timing.network2_timestamp_delta_ms;
ilnik04f4d122017-06-19 07:18:55 -070094
95 timing_.receive_start_ms = first_packet->receive_time_ms;
96 timing_.receive_finish_ms = last_packet->receive_time_ms;
ilnik04f4d122017-06-19 07:18:55 -070097 }
sprangba050a62017-08-18 02:51:12 -070098 timing_.flags = last_packet->video_header.video_timing.flags;
philipel02447bc2016-05-13 06:01:03 -070099}
philipelc707ab72016-04-01 02:01:54 -0700100
101RtpFrameObject::~RtpFrameObject() {
102 packet_buffer_->ReturnFrame(this);
103}
104
philipelf4139332016-04-20 10:26:34 +0200105uint16_t RtpFrameObject::first_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700106 return first_seq_num_;
philipelc707ab72016-04-01 02:01:54 -0700107}
108
philipelf4139332016-04-20 10:26:34 +0200109uint16_t RtpFrameObject::last_seq_num() const {
philipel02447bc2016-05-13 06:01:03 -0700110 return last_seq_num_;
111}
112
philipel5ceaaae2016-05-24 10:20:47 +0200113int RtpFrameObject::times_nacked() const {
114 return times_nacked_;
115}
116
philipel02447bc2016-05-13 06:01:03 -0700117FrameType RtpFrameObject::frame_type() const {
118 return frame_type_;
119}
120
121VideoCodecType RtpFrameObject::codec_type() const {
122 return codec_type_;
philipelc707ab72016-04-01 02:01:54 -0700123}
124
philipel5fb24542018-10-02 17:01:45 +0200125void RtpFrameObject::SetBitstream(rtc::ArrayView<const uint8_t> bitstream) {
126 AllocateBitstreamBuffer(bitstream.size());
127 memcpy(_buffer, bitstream.data(), _length);
128}
129
philipelc707ab72016-04-01 02:01:54 -0700130bool RtpFrameObject::GetBitstream(uint8_t* destination) const {
131 return packet_buffer_->GetBitstream(*this, destination);
132}
133
philipelb4d31082016-07-11 08:46:29 -0700134int64_t RtpFrameObject::ReceivedTime() const {
135 return received_time_;
136}
137
138int64_t RtpFrameObject::RenderTime() const {
139 return _renderTimeMs;
140}
141
philipele0754302017-01-25 08:56:23 -0800142bool RtpFrameObject::delayed_by_retransmission() const {
143 return times_nacked() > 0;
144}
145
philipel5470f402018-09-07 13:38:53 +0200146absl::optional<RTPVideoHeader> RtpFrameObject::GetRtpVideoHeader() const {
philipel88488282016-11-03 08:56:54 -0700147 rtc::CritScope lock(&packet_buffer_->crit_);
philipel02447bc2016-05-13 06:01:03 -0700148 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
149 if (!packet)
Danil Chapovalov0040b662018-06-18 10:48:16 +0200150 return absl::nullopt;
Sami Kalliomäki98824952018-08-28 14:39:21 +0200151 return packet->video_header;
philipel02447bc2016-05-13 06:01:03 -0700152}
153
philipel2837edc2018-10-02 13:55:47 +0200154absl::optional<RtpGenericFrameDescriptor>
155RtpFrameObject::GetGenericFrameDescriptor() const {
156 rtc::CritScope lock(&packet_buffer_->crit_);
157 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
158 if (!packet)
159 return absl::nullopt;
160 return packet->generic_descriptor;
161}
162
Johnny Leee0c8b232018-09-11 16:50:49 -0400163absl::optional<FrameMarking> RtpFrameObject::GetFrameMarking() const {
164 rtc::CritScope lock(&packet_buffer_->crit_);
165 VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_);
166 if (!packet)
167 return absl::nullopt;
168 return packet->video_header.frame_marking;
169}
170
philipel5fb24542018-10-02 17:01:45 +0200171void RtpFrameObject::AllocateBitstreamBuffer(size_t frame_size) {
172 // Since FFmpeg use an optimized bitstream reader that reads in chunks of
173 // 32/64 bits we have to add at least that much padding to the buffer
174 // to make sure the decoder doesn't read out of bounds.
175 // NOTE! EncodedImage::_size is the size of the buffer (think capacity of
176 // an std::vector) and EncodedImage::_length is the actual size of
177 // the bitstream (think size of an std::vector).
178 size_t new_size = frame_size + (codec_type_ == kVideoCodecH264
179 ? EncodedImage::kBufferPaddingBytesH264
180 : 0);
181 if (_size < new_size) {
182 delete[] _buffer;
183 _buffer = new uint8_t[new_size];
184 _size = new_size;
185 }
186
187 _length = frame_size;
188}
189
philipelc707ab72016-04-01 02:01:54 -0700190} // namespace video_coding
191} // namespace webrtc