| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/video_coding/frame_buffer.h" | 11 #include "webrtc/modules/video_coding/frame_buffer.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 | 15 |
| 16 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/logging.h" | 17 #include "webrtc/base/logging.h" |
| 18 #include "webrtc/modules/video_coding/packet.h" | 18 #include "webrtc/modules/video_coding/packet.h" |
| 19 | 19 |
| 20 namespace webrtc { | 20 namespace webrtc { |
| 21 | 21 |
| 22 VCMFrameBuffer::VCMFrameBuffer() | 22 VCMFrameBuffer::VCMFrameBuffer() |
| 23 : | 23 : _state(kStateEmpty), _nackCount(0), _latestPacketTimeMs(-1) {} |
| 24 _state(kStateEmpty), | 24 |
| 25 _nackCount(0), | 25 VCMFrameBuffer::~VCMFrameBuffer() {} |
| 26 _latestPacketTimeMs(-1) { | 26 |
| 27 VCMFrameBuffer::VCMFrameBuffer(const VCMFrameBuffer& rhs) |
| 28 : VCMEncodedFrame(rhs), |
| 29 _state(rhs._state), |
| 30 _sessionInfo(), |
| 31 _nackCount(rhs._nackCount), |
| 32 _latestPacketTimeMs(rhs._latestPacketTimeMs) { |
| 33 _sessionInfo = rhs._sessionInfo; |
| 34 _sessionInfo.UpdateDataPointers(rhs._buffer, _buffer); |
| 27 } | 35 } |
| 28 | 36 |
| 29 VCMFrameBuffer::~VCMFrameBuffer() { | 37 webrtc::FrameType VCMFrameBuffer::FrameType() const { |
| 38 return _sessionInfo.FrameType(); |
| 30 } | 39 } |
| 31 | 40 |
| 32 VCMFrameBuffer::VCMFrameBuffer(const VCMFrameBuffer& rhs) | 41 int32_t VCMFrameBuffer::GetLowSeqNum() const { |
| 33 : | 42 return _sessionInfo.LowSequenceNumber(); |
| 34 VCMEncodedFrame(rhs), | |
| 35 _state(rhs._state), | |
| 36 _sessionInfo(), | |
| 37 _nackCount(rhs._nackCount), | |
| 38 _latestPacketTimeMs(rhs._latestPacketTimeMs) { | |
| 39 _sessionInfo = rhs._sessionInfo; | |
| 40 _sessionInfo.UpdateDataPointers(rhs._buffer, _buffer); | |
| 41 } | 43 } |
| 42 | 44 |
| 43 webrtc::FrameType | 45 int32_t VCMFrameBuffer::GetHighSeqNum() const { |
| 44 VCMFrameBuffer::FrameType() const { | 46 return _sessionInfo.HighSequenceNumber(); |
| 45 return _sessionInfo.FrameType(); | |
| 46 } | |
| 47 | |
| 48 int32_t | |
| 49 VCMFrameBuffer::GetLowSeqNum() const { | |
| 50 return _sessionInfo.LowSequenceNumber(); | |
| 51 } | |
| 52 | |
| 53 int32_t | |
| 54 VCMFrameBuffer::GetHighSeqNum() const { | |
| 55 return _sessionInfo.HighSequenceNumber(); | |
| 56 } | 47 } |
| 57 | 48 |
| 58 int VCMFrameBuffer::PictureId() const { | 49 int VCMFrameBuffer::PictureId() const { |
| 59 return _sessionInfo.PictureId(); | 50 return _sessionInfo.PictureId(); |
| 60 } | 51 } |
| 61 | 52 |
| 62 int VCMFrameBuffer::TemporalId() const { | 53 int VCMFrameBuffer::TemporalId() const { |
| 63 return _sessionInfo.TemporalId(); | 54 return _sessionInfo.TemporalId(); |
| 64 } | 55 } |
| 65 | 56 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 77 | 68 |
| 78 void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) { | 69 void VCMFrameBuffer::SetGofInfo(const GofInfoVP9& gof_info, size_t idx) { |
| 79 _sessionInfo.SetGofInfo(gof_info, idx); | 70 _sessionInfo.SetGofInfo(gof_info, idx); |
| 80 // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing. | 71 // TODO(asapersson): Consider adding hdr->VP9.ref_picture_id for testing. |
| 81 _codecSpecificInfo.codecSpecific.VP9.temporal_idx = | 72 _codecSpecificInfo.codecSpecific.VP9.temporal_idx = |
| 82 gof_info.temporal_idx[idx]; | 73 gof_info.temporal_idx[idx]; |
| 83 _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch = | 74 _codecSpecificInfo.codecSpecific.VP9.temporal_up_switch = |
| 84 gof_info.temporal_up_switch[idx]; | 75 gof_info.temporal_up_switch[idx]; |
| 85 } | 76 } |
| 86 | 77 |
| 87 bool | 78 bool VCMFrameBuffer::IsSessionComplete() const { |
| 88 VCMFrameBuffer::IsSessionComplete() const { | 79 return _sessionInfo.complete(); |
| 89 return _sessionInfo.complete(); | |
| 90 } | 80 } |
| 91 | 81 |
| 92 // Insert packet | 82 // Insert packet |
| 93 VCMFrameBufferEnum | 83 VCMFrameBufferEnum VCMFrameBuffer::InsertPacket( |
| 94 VCMFrameBuffer::InsertPacket(const VCMPacket& packet, | 84 const VCMPacket& packet, |
| 95 int64_t timeInMs, | 85 int64_t timeInMs, |
| 96 VCMDecodeErrorMode decode_error_mode, | 86 VCMDecodeErrorMode decode_error_mode, |
| 97 const FrameData& frame_data) { | 87 const FrameData& frame_data) { |
| 98 assert(!(NULL == packet.dataPtr && packet.sizeBytes > 0)); | 88 assert(!(NULL == packet.dataPtr && packet.sizeBytes > 0)); |
| 99 if (packet.dataPtr != NULL) { | 89 if (packet.dataPtr != NULL) { |
| 100 _payloadType = packet.payloadType; | 90 _payloadType = packet.payloadType; |
| 91 } |
| 92 |
| 93 if (kStateEmpty == _state) { |
| 94 // First packet (empty and/or media) inserted into this frame. |
| 95 // store some info and set some initial values. |
| 96 _timeStamp = packet.timestamp; |
| 97 // We only take the ntp timestamp of the first packet of a frame. |
| 98 ntp_time_ms_ = packet.ntp_time_ms_; |
| 99 _codec = packet.codec; |
| 100 if (packet.frameType != kEmptyFrame) { |
| 101 // first media packet |
| 102 SetState(kStateIncomplete); |
| 101 } | 103 } |
| 104 } |
| 102 | 105 |
| 103 if (kStateEmpty == _state) { | 106 uint32_t requiredSizeBytes = |
| 104 // First packet (empty and/or media) inserted into this frame. | 107 Length() + packet.sizeBytes + |
| 105 // store some info and set some initial values. | 108 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0); |
| 106 _timeStamp = packet.timestamp; | 109 if (requiredSizeBytes >= _size) { |
| 107 // We only take the ntp timestamp of the first packet of a frame. | 110 const uint8_t* prevBuffer = _buffer; |
| 108 ntp_time_ms_ = packet.ntp_time_ms_; | 111 const uint32_t increments = |
| 109 _codec = packet.codec; | 112 requiredSizeBytes / kBufferIncStepSizeBytes + |
| 110 if (packet.frameType != kEmptyFrame) { | 113 (requiredSizeBytes % kBufferIncStepSizeBytes > 0); |
| 111 // first media packet | 114 const uint32_t newSize = _size + increments * kBufferIncStepSizeBytes; |
| 112 SetState(kStateIncomplete); | 115 if (newSize > kMaxJBFrameSizeBytes) { |
| 113 } | 116 LOG(LS_ERROR) << "Failed to insert packet due to frame being too " |
| 117 "big."; |
| 118 return kSizeError; |
| 114 } | 119 } |
| 120 VerifyAndAllocate(newSize); |
| 121 _sessionInfo.UpdateDataPointers(prevBuffer, _buffer); |
| 122 } |
| 115 | 123 |
| 116 uint32_t requiredSizeBytes = Length() + packet.sizeBytes + | 124 if (packet.width > 0 && packet.height > 0) { |
| 117 (packet.insertStartCode ? kH264StartCodeLengthBytes : 0); | 125 _encodedWidth = packet.width; |
| 118 if (requiredSizeBytes >= _size) { | 126 _encodedHeight = packet.height; |
| 119 const uint8_t* prevBuffer = _buffer; | 127 } |
| 120 const uint32_t increments = requiredSizeBytes / | |
| 121 kBufferIncStepSizeBytes + | |
| 122 (requiredSizeBytes % | |
| 123 kBufferIncStepSizeBytes > 0); | |
| 124 const uint32_t newSize = _size + | |
| 125 increments * kBufferIncStepSizeBytes; | |
| 126 if (newSize > kMaxJBFrameSizeBytes) { | |
| 127 LOG(LS_ERROR) << "Failed to insert packet due to frame being too " | |
| 128 "big."; | |
| 129 return kSizeError; | |
| 130 } | |
| 131 VerifyAndAllocate(newSize); | |
| 132 _sessionInfo.UpdateDataPointers(prevBuffer, _buffer); | |
| 133 } | |
| 134 | 128 |
| 135 if (packet.width > 0 && packet.height > 0) { | 129 // Don't copy payload specific data for empty packets (e.g padding packets). |
| 136 _encodedWidth = packet.width; | 130 if (packet.sizeBytes > 0) |
| 137 _encodedHeight = packet.height; | 131 CopyCodecSpecific(&packet.codecSpecificHeader); |
| 138 } | |
| 139 | 132 |
| 140 // Don't copy payload specific data for empty packets (e.g padding packets). | 133 int retVal = |
| 141 if (packet.sizeBytes > 0) | 134 _sessionInfo.InsertPacket(packet, _buffer, decode_error_mode, frame_data); |
| 142 CopyCodecSpecific(&packet.codecSpecificHeader); | 135 if (retVal == -1) { |
| 136 return kSizeError; |
| 137 } else if (retVal == -2) { |
| 138 return kDuplicatePacket; |
| 139 } else if (retVal == -3) { |
| 140 return kOutOfBoundsPacket; |
| 141 } |
| 142 // update length |
| 143 _length = Length() + static_cast<uint32_t>(retVal); |
| 143 | 144 |
| 144 int retVal = _sessionInfo.InsertPacket(packet, _buffer, | 145 _latestPacketTimeMs = timeInMs; |
| 145 decode_error_mode, | |
| 146 frame_data); | |
| 147 if (retVal == -1) { | |
| 148 return kSizeError; | |
| 149 } else if (retVal == -2) { | |
| 150 return kDuplicatePacket; | |
| 151 } else if (retVal == -3) { | |
| 152 return kOutOfBoundsPacket; | |
| 153 } | |
| 154 // update length | |
| 155 _length = Length() + static_cast<uint32_t>(retVal); | |
| 156 | 146 |
| 157 _latestPacketTimeMs = timeInMs; | 147 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ |
| 148 // ts_126114v120700p.pdf Section 7.4.5. |
| 149 // The MTSI client shall add the payload bytes as defined in this clause |
| 150 // onto the last RTP packet in each group of packets which make up a key |
| 151 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265 |
| 152 // (HEVC)). |
| 153 if (packet.markerBit) { |
| 154 RTC_DCHECK(!_rotation_set); |
| 155 _rotation = packet.codecSpecificHeader.rotation; |
| 156 _rotation_set = true; |
| 157 } |
| 158 | 158 |
| 159 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ | 159 if (_sessionInfo.complete()) { |
| 160 // ts_126114v120700p.pdf Section 7.4.5. | 160 SetState(kStateComplete); |
| 161 // The MTSI client shall add the payload bytes as defined in this clause | 161 return kCompleteSession; |
| 162 // onto the last RTP packet in each group of packets which make up a key | 162 } else if (_sessionInfo.decodable()) { |
| 163 // frame (I-frame or IDR frame in H.264 (AVC), or an IRAP picture in H.265 | 163 SetState(kStateDecodable); |
| 164 // (HEVC)). | 164 return kDecodableSession; |
| 165 if (packet.markerBit) { | 165 } |
| 166 RTC_DCHECK(!_rotation_set); | 166 return kIncomplete; |
| 167 _rotation = packet.codecSpecificHeader.rotation; | |
| 168 _rotation_set = true; | |
| 169 } | |
| 170 | |
| 171 if (_sessionInfo.complete()) { | |
| 172 SetState(kStateComplete); | |
| 173 return kCompleteSession; | |
| 174 } else if (_sessionInfo.decodable()) { | |
| 175 SetState(kStateDecodable); | |
| 176 return kDecodableSession; | |
| 177 } | |
| 178 return kIncomplete; | |
| 179 } | 167 } |
| 180 | 168 |
| 181 int64_t | 169 int64_t VCMFrameBuffer::LatestPacketTimeMs() const { |
| 182 VCMFrameBuffer::LatestPacketTimeMs() const { | 170 return _latestPacketTimeMs; |
| 183 return _latestPacketTimeMs; | |
| 184 } | 171 } |
| 185 | 172 |
| 186 void | 173 void VCMFrameBuffer::IncrementNackCount() { |
| 187 VCMFrameBuffer::IncrementNackCount() { | 174 _nackCount++; |
| 188 _nackCount++; | |
| 189 } | 175 } |
| 190 | 176 |
| 191 int16_t | 177 int16_t VCMFrameBuffer::GetNackCount() const { |
| 192 VCMFrameBuffer::GetNackCount() const { | 178 return _nackCount; |
| 193 return _nackCount; | |
| 194 } | 179 } |
| 195 | 180 |
| 196 bool | 181 bool VCMFrameBuffer::HaveFirstPacket() const { |
| 197 VCMFrameBuffer::HaveFirstPacket() const { | 182 return _sessionInfo.HaveFirstPacket(); |
| 198 return _sessionInfo.HaveFirstPacket(); | |
| 199 } | 183 } |
| 200 | 184 |
| 201 bool | 185 bool VCMFrameBuffer::HaveLastPacket() const { |
| 202 VCMFrameBuffer::HaveLastPacket() const { | 186 return _sessionInfo.HaveLastPacket(); |
| 203 return _sessionInfo.HaveLastPacket(); | |
| 204 } | 187 } |
| 205 | 188 |
| 206 int | 189 int VCMFrameBuffer::NumPackets() const { |
| 207 VCMFrameBuffer::NumPackets() const { | 190 return _sessionInfo.NumPackets(); |
| 208 return _sessionInfo.NumPackets(); | |
| 209 } | 191 } |
| 210 | 192 |
| 211 void | 193 void VCMFrameBuffer::Reset() { |
| 212 VCMFrameBuffer::Reset() { | 194 _length = 0; |
| 213 _length = 0; | 195 _timeStamp = 0; |
| 214 _timeStamp = 0; | 196 _sessionInfo.Reset(); |
| 215 _sessionInfo.Reset(); | 197 _payloadType = 0; |
| 216 _payloadType = 0; | 198 _nackCount = 0; |
| 217 _nackCount = 0; | 199 _latestPacketTimeMs = -1; |
| 218 _latestPacketTimeMs = -1; | 200 _state = kStateEmpty; |
| 219 _state = kStateEmpty; | 201 VCMEncodedFrame::Reset(); |
| 220 VCMEncodedFrame::Reset(); | |
| 221 } | 202 } |
| 222 | 203 |
| 223 // Set state of frame | 204 // Set state of frame |
| 224 void | 205 void VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) { |
| 225 VCMFrameBuffer::SetState(VCMFrameBufferStateEnum state) { | 206 if (_state == state) { |
| 226 if (_state == state) { | 207 return; |
| 227 return; | 208 } |
| 228 } | 209 switch (state) { |
| 229 switch (state) { | |
| 230 case kStateIncomplete: | 210 case kStateIncomplete: |
| 231 // we can go to this state from state kStateEmpty | 211 // we can go to this state from state kStateEmpty |
| 232 assert(_state == kStateEmpty); | 212 assert(_state == kStateEmpty); |
| 233 | 213 |
| 234 // Do nothing, we received a packet | 214 // Do nothing, we received a packet |
| 235 break; | 215 break; |
| 236 | 216 |
| 237 case kStateComplete: | 217 case kStateComplete: |
| 238 assert(_state == kStateEmpty || | 218 assert(_state == kStateEmpty || _state == kStateIncomplete || |
| 239 _state == kStateIncomplete || | 219 _state == kStateDecodable); |
| 240 _state == kStateDecodable); | |
| 241 | 220 |
| 242 break; | 221 break; |
| 243 | 222 |
| 244 case kStateEmpty: | 223 case kStateEmpty: |
| 245 // Should only be set to empty through Reset(). | 224 // Should only be set to empty through Reset(). |
| 246 assert(false); | 225 assert(false); |
| 247 break; | 226 break; |
| 248 | 227 |
| 249 case kStateDecodable: | 228 case kStateDecodable: |
| 250 assert(_state == kStateEmpty || | 229 assert(_state == kStateEmpty || _state == kStateIncomplete); |
| 251 _state == kStateIncomplete); | 230 break; |
| 252 break; | 231 } |
| 253 } | 232 _state = state; |
| 254 _state = state; | |
| 255 } | 233 } |
| 256 | 234 |
| 257 // Get current state of frame | 235 // Get current state of frame |
| 258 VCMFrameBufferStateEnum | 236 VCMFrameBufferStateEnum VCMFrameBuffer::GetState() const { |
| 259 VCMFrameBuffer::GetState() const { | 237 return _state; |
| 260 return _state; | |
| 261 } | 238 } |
| 262 | 239 |
| 263 // Get current state of frame | 240 // Get current state of frame |
| 264 VCMFrameBufferStateEnum | 241 VCMFrameBufferStateEnum VCMFrameBuffer::GetState(uint32_t& timeStamp) const { |
| 265 VCMFrameBuffer::GetState(uint32_t& timeStamp) const { | 242 timeStamp = TimeStamp(); |
| 266 timeStamp = TimeStamp(); | 243 return GetState(); |
| 267 return GetState(); | |
| 268 } | 244 } |
| 269 | 245 |
| 270 bool | 246 bool VCMFrameBuffer::IsRetransmitted() const { |
| 271 VCMFrameBuffer::IsRetransmitted() const { | 247 return _sessionInfo.session_nack(); |
| 272 return _sessionInfo.session_nack(); | |
| 273 } | 248 } |
| 274 | 249 |
| 275 void | 250 void VCMFrameBuffer::PrepareForDecode(bool continuous) { |
| 276 VCMFrameBuffer::PrepareForDecode(bool continuous) { | |
| 277 #ifdef INDEPENDENT_PARTITIONS | 251 #ifdef INDEPENDENT_PARTITIONS |
| 278 if (_codec == kVideoCodecVP8) { | 252 if (_codec == kVideoCodecVP8) { |
| 279 _length = | 253 _length = _sessionInfo.BuildVP8FragmentationHeader(_buffer, _length, |
| 280 _sessionInfo.BuildVP8FragmentationHeader(_buffer, _length, | 254 &_fragmentation); |
| 281 &_fragmentation); | 255 } else { |
| 282 } else { | |
| 283 size_t bytes_removed = _sessionInfo.MakeDecodable(); | |
| 284 _length -= bytes_removed; | |
| 285 } | |
| 286 #else | |
| 287 size_t bytes_removed = _sessionInfo.MakeDecodable(); | 256 size_t bytes_removed = _sessionInfo.MakeDecodable(); |
| 288 _length -= bytes_removed; | 257 _length -= bytes_removed; |
| 258 } |
| 259 #else |
| 260 size_t bytes_removed = _sessionInfo.MakeDecodable(); |
| 261 _length -= bytes_removed; |
| 289 #endif | 262 #endif |
| 290 // Transfer frame information to EncodedFrame and create any codec | 263 // Transfer frame information to EncodedFrame and create any codec |
| 291 // specific information. | 264 // specific information. |
| 292 _frameType = _sessionInfo.FrameType(); | 265 _frameType = _sessionInfo.FrameType(); |
| 293 _completeFrame = _sessionInfo.complete(); | 266 _completeFrame = _sessionInfo.complete(); |
| 294 _missingFrame = !continuous; | 267 _missingFrame = !continuous; |
| 295 } | 268 } |
| 296 | 269 |
| 297 } // namespace webrtc | 270 } // namespace webrtc |
| OLD | NEW |