mediapacket.h (3166B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mediapacket_h__ 8 #define mediapacket_h__ 9 10 #include <cstddef> 11 #include <cstdint> 12 13 #include "mozilla/DefineEnum.h" 14 #include "mozilla/Maybe.h" 15 #include "mozilla/UniquePtr.h" 16 17 class PickleIterator; 18 19 namespace IPC { 20 class Message; 21 class MessageReader; 22 class MessageWriter; 23 } // namespace IPC 24 25 namespace mozilla { 26 27 // TODO: It might be worthwhile to teach this class how to "borrow" a buffer. 28 // That would make it easier to misuse, however, so maybe not worth it. 29 class MediaPacket { 30 public: 31 MediaPacket() = default; 32 MediaPacket(MediaPacket&& orig) = default; 33 MediaPacket& operator=(MediaPacket&& orig) = default; 34 MediaPacket(const MediaPacket& orig); 35 36 MediaPacket Clone() const; 37 38 // Takes ownership of the passed-in data 39 void Take(UniquePtr<uint8_t[]>&& data, size_t len, size_t capacity = 0) { 40 data_ = std::move(data); 41 len_ = len; 42 if (capacity < len) { 43 capacity = len; 44 } 45 capacity_ = capacity; 46 } 47 48 void Reset() { 49 data_.reset(); 50 len_ = 0; 51 capacity_ = 0; 52 encrypted_data_.reset(); 53 encrypted_len_ = 0; 54 sdp_level_.reset(); 55 } 56 57 // Copies the passed-in data 58 void Copy(const uint8_t* data, size_t len, size_t capacity = 0); 59 60 uint8_t* data() const { return data_.get(); } 61 62 size_t len() const { return len_; } 63 64 void SetLength(size_t length) { len_ = length; } 65 66 size_t capacity() const { return capacity_; } 67 68 Maybe<size_t>& sdp_level() { return sdp_level_; } 69 70 void CopyDataToEncrypted() { 71 encrypted_data_ = std::move(data_); 72 encrypted_len_ = len_; 73 Copy(encrypted_data_.get(), len_); 74 } 75 76 const uint8_t* encrypted_data() const { return encrypted_data_.get(); } 77 78 size_t encrypted_len() const { return encrypted_len_; } 79 80 MOZ_DEFINE_ENUM_WITH_TOSTRING_AT_CLASS_SCOPE(Type, (UNCLASSIFIED, SRTP, SRTCP, 81 DTLS, RTP, RTCP, SCTP)); 82 83 void Categorize(); 84 85 void SetType(Type type) { type_ = type; } 86 87 Type type() const { return type_; } 88 89 void Serialize(IPC::MessageWriter* aWriter) const; 90 bool Deserialize(IPC::MessageReader* aReader); 91 92 private: 93 UniquePtr<uint8_t[]> data_; 94 size_t len_ = 0; 95 size_t capacity_ = 0; 96 // Encrypted form of the data, if there is one. 97 UniquePtr<uint8_t[]> encrypted_data_; 98 size_t encrypted_len_ = 0; 99 // SDP level that this packet belongs to, if known. 100 Maybe<size_t> sdp_level_; 101 Type type_ = UNCLASSIFIED; 102 }; 103 } // namespace mozilla 104 105 namespace IPC { 106 template <typename> 107 struct ParamTraits; 108 109 template <> 110 struct ParamTraits<mozilla::MediaPacket> { 111 static void Write(MessageWriter* aWriter, 112 const mozilla::MediaPacket& aParam) { 113 aParam.Serialize(aWriter); 114 } 115 116 static bool Read(MessageReader* aReader, mozilla::MediaPacket* aResult) { 117 return aResult->Deserialize(aReader); 118 } 119 }; 120 } // namespace IPC 121 #endif // mediapacket_h__