video_rtp_depacketizer_generic.cc (2656B)
1 /* 2 * Copyright (c) 2019 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 11 #include "modules/rtp_rtcp/source/video_rtp_depacketizer_generic.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 #include <optional> 16 #include <utility> 17 18 #include "api/video/video_codec_type.h" 19 #include "api/video/video_frame_type.h" 20 #include "modules/rtp_rtcp/source/rtp_video_header.h" 21 #include "modules/rtp_rtcp/source/video_rtp_depacketizer.h" 22 #include "rtc_base/copy_on_write_buffer.h" 23 #include "rtc_base/logging.h" 24 25 namespace webrtc { 26 namespace { 27 constexpr uint8_t kKeyFrameBit = 0b0000'0001; 28 constexpr uint8_t kFirstPacketBit = 0b0000'0010; 29 // If this bit is set, there will be an extended header contained in this 30 // packet. This was added later so old clients will not send this. 31 constexpr uint8_t kExtendedHeaderBit = 0b0000'0100; 32 33 constexpr size_t kGenericHeaderLength = 1; 34 constexpr size_t kExtendedHeaderLength = 2; 35 } // namespace 36 37 std::optional<VideoRtpDepacketizer::ParsedRtpPayload> 38 VideoRtpDepacketizerGeneric::Parse(CopyOnWriteBuffer rtp_payload) { 39 if (rtp_payload.empty()) { 40 RTC_LOG(LS_WARNING) << "Empty payload."; 41 return std::nullopt; 42 } 43 std::optional<ParsedRtpPayload> parsed(std::in_place); 44 const uint8_t* payload_data = rtp_payload.cdata(); 45 46 uint8_t generic_header = payload_data[0]; 47 size_t offset = kGenericHeaderLength; 48 49 parsed->video_header.frame_type = (generic_header & kKeyFrameBit) 50 ? VideoFrameType::kVideoFrameKey 51 : VideoFrameType::kVideoFrameDelta; 52 parsed->video_header.is_first_packet_in_frame = 53 (generic_header & kFirstPacketBit) != 0; 54 parsed->video_header.codec = kVideoCodecGeneric; 55 parsed->video_header.width = 0; 56 parsed->video_header.height = 0; 57 58 if (generic_header & kExtendedHeaderBit) { 59 if (rtp_payload.size() < offset + kExtendedHeaderLength) { 60 RTC_LOG(LS_WARNING) << "Too short payload for generic header."; 61 return std::nullopt; 62 } 63 parsed->video_header.video_type_header 64 .emplace<RTPVideoHeaderLegacyGeneric>() 65 .picture_id = ((payload_data[1] & 0x7F) << 8) | payload_data[2]; 66 offset += kExtendedHeaderLength; 67 } 68 69 parsed->video_payload = 70 rtp_payload.Slice(offset, rtp_payload.size() - offset); 71 return parsed; 72 } 73 } // namespace webrtc