Chromium Code Reviews| Index: webrtc/modules/rtp_rtcp/source/rtp_packet.h |
| diff --git a/webrtc/modules/rtp_rtcp/source/rtp_packet.h b/webrtc/modules/rtp_rtcp/source/rtp_packet.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9284b974d53589a53d4d5ba1f8b4fd552bba9067 |
| --- /dev/null |
| +++ b/webrtc/modules/rtp_rtcp/source/rtp_packet.h |
| @@ -0,0 +1,210 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| +#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ |
| +#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ |
| + |
| +#include <vector> |
| + |
| +#include "webrtc/base/basictypes.h" |
| +#include "webrtc/base/buffer.h" |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/common_types.h" |
| +#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| +#include "webrtc/system_wrappers/include/ntp_time.h" |
| + |
| +namespace webrtc { |
| +class RtpHeaderExtensionMap; |
| +class Random; |
| + |
| +namespace rtp { |
| +class Packet { |
| + public: |
| + using ExtensionType = RTPExtensionType; |
| + using ExtensionManager = RtpHeaderExtensionMap; |
| + static const size_t kMaxExtensionHeaders = 14; |
| + |
| + bool Parse(const uint8_t* buffer, size_t size); |
| + bool Parse(rtc::Buffer packet); |
| + |
| + // Header. |
| + bool Marker() const; |
| + uint8_t PayloadType() const; |
| + uint16_t SequenceNumber() const; |
| + uint32_t Timestamp() const; |
| + uint32_t Ssrc() const; |
| + std::vector<uint32_t> Csrcs() const; |
| + // TODO(danilchap): Remove this function when all code update to use RtpPacket |
| + // directly. Function is there just for easier backward compatibilty. |
| + void GetHeader(RTPHeader* header) const; |
| + // Header extensions. |
| + template <typename Extension, typename... Values> |
| + bool GetExtension(Values...) const; |
| + size_t HeadersSize() const; |
| + // Payload. |
| + size_t PayloadSize() const; |
| + size_t PaddingSize() const; |
| + const uint8_t* Payload() const; |
| + // Buffer. |
| + size_t capacity() const; |
| + size_t size() const; |
| + const uint8_t* data() const; |
| + size_t FreeCapacity() const { return capacity() - size(); } |
| + size_t MaxPayloadSize() const { return capacity() - payload_offset_; } |
| + |
| + // Reset fields and buffer. |
| + void Clear(); |
| + // Header setters. |
| + void CopyHeader(const Packet& packet); |
| + void SetMarker(bool marker_bit); |
| + void SetPayloadType(uint8_t payload_type); |
| + void SetSequenceNumber(uint16_t seq_no); |
| + void SetTimestamp(uint32_t timestamp); |
| + void SetSsrc(uint32_t ssrc); |
| + // Writes csrc list. Assumes: |
| + // a) There is enough room left in buffer. |
| + // b) Extension headers, payload or padding data has not already been added. |
| + void SetCsrcs(const std::vector<uint32_t>& csrcs); |
| + // Header extensions. |
| + template <typename Extension, typename... Values> |
| + bool SetExtension(Values...); |
| + template <typename Extension> |
| + bool ReserveExtension(); |
| + // Reserve size_bytes for payload. Returns nullptr on failure. |
| + uint8_t* AllocatePayload(size_t size_bytes); |
| + void SetPayloadSize(size_t size_bytes); |
| + bool SetPadding(uint8_t size_bytes, Random* random); |
| + |
| + protected: |
| + explicit Packet(const ExtensionManager* extensions); |
|
terelius
2016/04/13 13:17:00
So you have to know what header extensions are use
danilchap
2016/04/13 16:18:34
Good point.
Adjusted. Added function SetExtensionM
|
| + Packet(const ExtensionManager* extensions, size_t capacity); |
| + virtual ~Packet(); |
| + |
| + private: |
| + struct ExtensionInfo { |
| + ExtensionType type; |
| + uint16_t offset; |
| + uint8_t length; |
| + }; |
| + // Tries to Parse data in given buffer. |
| + // Does not touch packet own buffer, leaving packet in desync state on return. |
|
terelius
2016/04/13 13:17:00
I don't understand what this comment means. Update
danilchap
2016/04/13 16:18:34
this function is private, you shouldn't call it al
|
| + bool ParseBuffer(const uint8_t* buffer, size_t size); |
| + // Find and extension, based on the type field of the parameter. |
|
philipel
2016/04/13 12:01:19
Find an extension
danilchap
2016/04/13 16:18:34
Done.
|
| + // If found, length field would be validated, the offset field will be set |
| + // and true returned, |
| + // otherwise the parameter will be unchanged and false is returned. |
| + bool FindExtension(ExtensionType type, |
| + uint8_t length, |
| + uint16_t* offset) const; |
| + // Find or allocate an extension, based on the type field of the parameter. |
| + // If found, the length field be checked against what is already registered |
| + // and the offset field will be set, then true is returned. If allocated, the |
| + // length field will be used for allocation and the offset update to indicate |
| + // position, the true is returned. |
| + // If not found and allocations fails, false is returned and paramter remains |
|
terelius
2016/04/13 13:17:00
nit: parameter
danilchap
2016/04/13 16:18:34
Done.
|
| + // unchanged. |
| + bool AllocateExtension(ExtensionType type, uint8_t length, uint16_t* offset); |
| + |
| + uint8_t* WriteAt(size_t offset) { return buffer_.data() + offset; } |
|
philipel
2016/04/13 12:01:19
Move implementation to .cc file.
danilchap
2016/04/13 16:18:34
Done.
|
| + void WriteAt(size_t offset, uint8_t byte) { buffer_.data()[offset] = byte; } |
|
philipel
2016/04/13 12:01:19
Ditto.
danilchap
2016/04/13 16:18:34
Done.
|
| + |
| + const ExtensionManager* const extensions_; |
| + // Header. |
| + bool marker_; |
| + uint8_t payload_type_; |
| + uint8_t padding_size_; |
| + uint16_t sequence_number_; |
| + uint32_t timestamp_; |
| + uint32_t ssrc_; |
| + size_t payload_offset_; // Match header size with csrcs and extensions. |
| + size_t payload_size_; |
| + |
| + uint8_t num_extensions_ = 0; |
| + ExtensionInfo extension_entries_[kMaxExtensionHeaders]; |
| + uint16_t extensions_size_ = 0; // Unaligned. |
| + rtc::Buffer buffer_; |
| + |
| + RTC_DISALLOW_COPY_AND_ASSIGN(Packet); |
| +}; |
| + |
| +template <typename Extension, typename... Values> |
| +bool Packet::GetExtension(Values... values) const { |
| + uint16_t offset = 0; |
| + if (!FindExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) |
| + return false; |
| + return Extension::Parse(data() + offset, values...); |
| +} |
| + |
| +template <typename Extension, typename... Values> |
| +bool Packet::SetExtension(Values... values) { |
| + uint16_t offset = 0; |
| + if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) |
| + return false; |
| + return Extension::Write(WriteAt(offset), values...); |
| +} |
| + |
| +template <typename Extension> |
| +bool Packet::ReserveExtension() { |
| + uint16_t offset = 0; |
| + if (!AllocateExtension(Extension::kId, Extension::kValueSizeBytes, &offset)) |
| + return false; |
| + memset(WriteAt(offset), 0, Extension::kValueSizeBytes); |
| + return true; |
| +} |
| +} // namespace rtp |
| + |
| +// Class to hold rtp packet with metadata for sender side. |
| +class RtpPacket : public rtp::Packet { |
|
philipel
2016/04/13 12:01:19
Rename to SendRtpPacket or something that indicate
danilchap
2016/04/13 16:18:34
Done.
|
| + public: |
| + explicit RtpPacket(const ExtensionManager* extensions) : Packet(extensions) {} |
| + RtpPacket(const ExtensionManager* extensions, size_t capacity) |
| + : Packet(extensions, capacity) {} |
|
philipel
2016/04/13 12:01:19
Why is the implementation for RtpPacket and Receiv
danilchap
2016/04/13 16:18:34
Definition is here for no good reason, moved to ow
philipel
2016/04/14 10:32:02
Acknowledged.
|
| + |
| + // Time in local time base as close as it can to frame capture time. |
| + int64_t capture_time_ms() const { return capture_time_ms_; } |
| + void set_capture_time_ms(int64_t time) { capture_time_ms_ = time; } |
| + |
| + private: |
| + int64_t capture_time_ms_ = 0; |
| +}; |
| + |
| +// Class to hold rtp packet with metadata for receiver side. |
| +class ReceivedRtpPacket : public rtp::Packet { |
| + public: |
| + explicit ReceivedRtpPacket(const ExtensionManager* extensions) |
| + : Packet(extensions) {} |
| + |
| + void GetHeader(RTPHeader* header) const; |
| + // Time in local time base as close as it can to packet arrived on the |
| + // network. |
| + int64_t arrival_time_ms() const { return arrival_time_ms_; } |
| + void set_arrival_time_ms(int64_t time) { arrival_time_ms_ = time; } |
| + |
| + // Estimated from Timestamp() using rtcp Sender Reports. |
| + NtpTime capture_ntp_time() const { return capture_time_; } |
| + void set_capture_ntp_time(NtpTime time) { capture_time_ = time; } |
| + |
| + // Flag if packet arrived via rtx. |
| + bool retransmit() const { return retransmit_; } |
| + void set_retransmit(bool value) { retransmit_ = value; } |
| + |
| + int payload_type_frequency() const { return payload_type_frequency_; } |
| + void set_payload_type_frequency(int value) { |
| + payload_type_frequency_ = value; |
| + } |
| + |
| + private: |
| + NtpTime capture_time_; |
| + int64_t arrival_time_ms_ = 0; |
| + int payload_type_frequency_ = 0; |
| + bool retransmit_ = false; |
| +}; |
| +} // namespace webrtc |
| + |
| +#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTP_PACKET_H_ |