philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/frame_object.h" |
Rasmus Brandt | 58b7291 | 2017-10-25 11:09:23 +0200 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/video_coding/packet_buffer.h" |
| 19 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "rtc_base/critical_section.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | namespace video_coding { |
| 24 | |
| 25 | RtpFrameObject::RtpFrameObject(PacketBuffer* packet_buffer, |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 26 | uint16_t first_seq_num, |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 27 | uint16_t last_seq_num, |
| 28 | size_t frame_size, |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 29 | int times_nacked, |
Ilya Nikolaevskiy | 4348ce2 | 2018-12-07 16:26:56 +0100 | [diff] [blame] | 30 | int64_t first_packet_received_time, |
| 31 | int64_t last_packet_received_time) |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 32 | : packet_buffer_(packet_buffer), |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 33 | first_seq_num_(first_seq_num), |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 34 | last_seq_num_(last_seq_num), |
Ilya Nikolaevskiy | 4348ce2 | 2018-12-07 16:26:56 +0100 | [diff] [blame] | 35 | last_packet_received_time_(last_packet_received_time), |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 36 | times_nacked_(times_nacked) { |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 37 | VCMPacket* first_packet = packet_buffer_->GetPacket(first_seq_num); |
philipel | 7d79e63 | 2017-05-23 08:19:11 -0700 | [diff] [blame] | 38 | RTC_CHECK(first_packet); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 39 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 40 | // EncodedFrame members |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 41 | frame_type_ = first_packet->frameType; |
| 42 | codec_type_ = first_packet->codec; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 43 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 44 | // TODO(philipel): Remove when encoded image is replaced by EncodedFrame. |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 45 | // VCMEncodedFrame members |
Emircan Uysaler | 9bb8f05 | 2018-01-23 15:53:06 -0800 | [diff] [blame] | 46 | CopyCodecSpecific(&first_packet->video_header); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 47 | _completeFrame = true; |
| 48 | _payloadType = first_packet->payloadType; |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 49 | SetTimestamp(first_packet->timestamp); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 50 | ntp_time_ms_ = first_packet->ntp_time_ms_; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 51 | _frameType = first_packet->frameType; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 52 | |
gnish | b2a318b | 2017-05-10 09:21:33 -0700 | [diff] [blame] | 53 | // 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 | |
philipel | 5fb2454 | 2018-10-02 17:01:45 +0200 | [diff] [blame] | 57 | AllocateBitstreamBuffer(frame_size); |
Niels Möller | 648a7ce | 2018-11-28 15:14:54 +0100 | [diff] [blame] | 58 | bool bitstream_copied = packet_buffer_->GetBitstream(*this, _buffer); |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 59 | RTC_DCHECK(bitstream_copied); |
philipel | 59e99b7 | 2017-01-12 03:26:04 -0800 | [diff] [blame] | 60 | _encodedWidth = first_packet->width; |
| 61 | _encodedHeight = first_packet->height; |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 62 | |
philipel | e7c891f | 2018-02-22 14:35:06 +0100 | [diff] [blame] | 63 | // EncodedFrame members |
Niels Möller | 2377588 | 2018-08-16 10:24:12 +0200 | [diff] [blame] | 64 | SetTimestamp(first_packet->timestamp); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 65 | |
| 66 | VCMPacket* last_packet = packet_buffer_->GetPacket(last_seq_num); |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 67 | RTC_CHECK(last_packet); |
philipel | ef615ea | 2018-09-13 11:07:48 +0200 | [diff] [blame] | 68 | RTC_CHECK(last_packet->is_last_packet_in_frame); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 69 | // 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; |
Johannes Kron | d0b69a8 | 2018-12-03 14:18:53 +0100 | [diff] [blame] | 76 | SetColorSpace(last_packet->video_header.color_space |
| 77 | ? &last_packet->video_header.color_space.value() |
| 78 | : nullptr); |
philipel | 266f0a4 | 2016-11-28 08:49:07 -0800 | [diff] [blame] | 79 | _rotation_set = true; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 80 | content_type_ = last_packet->video_header.content_type; |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 81 | if (last_packet->video_header.video_timing.flags != |
Ilya Nikolaevskiy | b6c462d | 2018-06-05 15:21:32 +0200 | [diff] [blame] | 82 | VideoSendTiming::kInvalid) { |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 83 | // ntp_time_ms_ may be -1 if not estimated yet. This is not a problem, |
| 84 | // as this will be dealt with at the time of reporting. |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 85 | timing_.encode_start_ms = |
| 86 | ntp_time_ms_ + |
| 87 | last_packet->video_header.video_timing.encode_start_delta_ms; |
| 88 | timing_.encode_finish_ms = |
| 89 | ntp_time_ms_ + |
| 90 | last_packet->video_header.video_timing.encode_finish_delta_ms; |
| 91 | timing_.packetization_finish_ms = |
| 92 | ntp_time_ms_ + |
| 93 | last_packet->video_header.video_timing.packetization_finish_delta_ms; |
| 94 | timing_.pacer_exit_ms = |
| 95 | ntp_time_ms_ + |
| 96 | last_packet->video_header.video_timing.pacer_exit_delta_ms; |
| 97 | timing_.network_timestamp_ms = |
| 98 | ntp_time_ms_ + |
Danil Chapovalov | 996eb9e | 2017-10-30 17:14:41 +0100 | [diff] [blame] | 99 | last_packet->video_header.video_timing.network_timestamp_delta_ms; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 100 | timing_.network2_timestamp_ms = |
| 101 | ntp_time_ms_ + |
Danil Chapovalov | 996eb9e | 2017-10-30 17:14:41 +0100 | [diff] [blame] | 102 | last_packet->video_header.video_timing.network2_timestamp_delta_ms; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 103 | } |
Ilya Nikolaevskiy | 4348ce2 | 2018-12-07 16:26:56 +0100 | [diff] [blame] | 104 | timing_.receive_start_ms = first_packet_received_time; |
| 105 | timing_.receive_finish_ms = last_packet_received_time; |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 106 | timing_.flags = last_packet->video_header.video_timing.flags; |
Ilya Nikolaevskiy | 5546aef | 2018-12-04 15:54:52 +0100 | [diff] [blame] | 107 | is_last_spatial_layer = last_packet->markerBit; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 108 | } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 109 | |
| 110 | RtpFrameObject::~RtpFrameObject() { |
| 111 | packet_buffer_->ReturnFrame(this); |
| 112 | } |
| 113 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 114 | uint16_t RtpFrameObject::first_seq_num() const { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 115 | return first_seq_num_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 116 | } |
| 117 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 118 | uint16_t RtpFrameObject::last_seq_num() const { |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 119 | return last_seq_num_; |
| 120 | } |
| 121 | |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 122 | int RtpFrameObject::times_nacked() const { |
| 123 | return times_nacked_; |
| 124 | } |
| 125 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 126 | FrameType RtpFrameObject::frame_type() const { |
| 127 | return frame_type_; |
| 128 | } |
| 129 | |
| 130 | VideoCodecType RtpFrameObject::codec_type() const { |
| 131 | return codec_type_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 132 | } |
| 133 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 134 | int64_t RtpFrameObject::ReceivedTime() const { |
Ilya Nikolaevskiy | 4348ce2 | 2018-12-07 16:26:56 +0100 | [diff] [blame] | 135 | return last_packet_received_time_; |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | int64_t RtpFrameObject::RenderTime() const { |
| 139 | return _renderTimeMs; |
| 140 | } |
| 141 | |
Niels Möller | f0eee00 | 2018-11-28 16:31:29 +0100 | [diff] [blame] | 142 | void RtpFrameObject::SetSize(size_t size) { |
Niels Möller | 48a7946 | 2018-12-07 16:21:18 +0100 | [diff] [blame] | 143 | RTC_DCHECK_LE(size, capacity()); |
Niels Möller | f0eee00 | 2018-11-28 16:31:29 +0100 | [diff] [blame] | 144 | _length = size; |
| 145 | } |
| 146 | |
philipel | e075430 | 2017-01-25 08:56:23 -0800 | [diff] [blame] | 147 | bool RtpFrameObject::delayed_by_retransmission() const { |
| 148 | return times_nacked() > 0; |
| 149 | } |
| 150 | |
philipel | 5470f40 | 2018-09-07 13:38:53 +0200 | [diff] [blame] | 151 | absl::optional<RTPVideoHeader> RtpFrameObject::GetRtpVideoHeader() const { |
philipel | 8848828 | 2016-11-03 08:56:54 -0700 | [diff] [blame] | 152 | rtc::CritScope lock(&packet_buffer_->crit_); |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 153 | VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_); |
| 154 | if (!packet) |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 155 | return absl::nullopt; |
Sami Kalliomäki | 9882495 | 2018-08-28 14:39:21 +0200 | [diff] [blame] | 156 | return packet->video_header; |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 157 | } |
| 158 | |
philipel | 2837edc | 2018-10-02 13:55:47 +0200 | [diff] [blame] | 159 | absl::optional<RtpGenericFrameDescriptor> |
| 160 | RtpFrameObject::GetGenericFrameDescriptor() const { |
| 161 | rtc::CritScope lock(&packet_buffer_->crit_); |
| 162 | VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_); |
| 163 | if (!packet) |
| 164 | return absl::nullopt; |
| 165 | return packet->generic_descriptor; |
| 166 | } |
| 167 | |
Johnny Lee | e0c8b23 | 2018-09-11 16:50:49 -0400 | [diff] [blame] | 168 | absl::optional<FrameMarking> RtpFrameObject::GetFrameMarking() const { |
| 169 | rtc::CritScope lock(&packet_buffer_->crit_); |
| 170 | VCMPacket* packet = packet_buffer_->GetPacket(first_seq_num_); |
| 171 | if (!packet) |
| 172 | return absl::nullopt; |
| 173 | return packet->video_header.frame_marking; |
| 174 | } |
| 175 | |
philipel | 5fb2454 | 2018-10-02 17:01:45 +0200 | [diff] [blame] | 176 | void RtpFrameObject::AllocateBitstreamBuffer(size_t frame_size) { |
| 177 | // Since FFmpeg use an optimized bitstream reader that reads in chunks of |
| 178 | // 32/64 bits we have to add at least that much padding to the buffer |
| 179 | // to make sure the decoder doesn't read out of bounds. |
| 180 | // NOTE! EncodedImage::_size is the size of the buffer (think capacity of |
| 181 | // an std::vector) and EncodedImage::_length is the actual size of |
| 182 | // the bitstream (think size of an std::vector). |
| 183 | size_t new_size = frame_size + (codec_type_ == kVideoCodecH264 |
| 184 | ? EncodedImage::kBufferPaddingBytesH264 |
| 185 | : 0); |
Niels Möller | 48a7946 | 2018-12-07 16:21:18 +0100 | [diff] [blame] | 186 | if (capacity() < new_size) { |
philipel | 5fb2454 | 2018-10-02 17:01:45 +0200 | [diff] [blame] | 187 | delete[] _buffer; |
Niels Möller | 48a7946 | 2018-12-07 16:21:18 +0100 | [diff] [blame] | 188 | set_buffer(new uint8_t[new_size], new_size); |
philipel | 5fb2454 | 2018-10-02 17:01:45 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | _length = frame_size; |
| 192 | } |
| 193 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 194 | } // namespace video_coding |
| 195 | } // namespace webrtc |