/src/mozilla-central/media/mtransport/mediapacket.h
Line | Count | Source (jump to first uncovered line) |
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 | | #include "mozilla/UniquePtr.h" |
13 | | #include "mozilla/Maybe.h" |
14 | | |
15 | | namespace mozilla { |
16 | | |
17 | | // TODO: It might be worthwhile to teach this class how to "borrow" a buffer. |
18 | | // That would make it easier to misuse, however, so maybe not worth it. |
19 | | class MediaPacket { |
20 | | public: |
21 | | MediaPacket() = default; |
22 | | MediaPacket(MediaPacket&& orig) = default; |
23 | | |
24 | | // Takes ownership of the passed-in data |
25 | | void Take(UniquePtr<uint8_t[]>&& data, size_t len, size_t capacity=0) |
26 | 0 | { |
27 | 0 | data_ = std::move(data); |
28 | 0 | len_ = len; |
29 | 0 | if (capacity < len) { |
30 | 0 | capacity = len; |
31 | 0 | } |
32 | 0 | capacity_ = capacity; |
33 | 0 | } |
34 | | |
35 | | void Reset() |
36 | 0 | { |
37 | 0 | data_.reset(); |
38 | 0 | len_ = 0; |
39 | 0 | capacity_ = 0; |
40 | 0 | } |
41 | | |
42 | | // Copies the passed-in data |
43 | | void Copy(const uint8_t* data, size_t len, size_t capacity=0); |
44 | | |
45 | | uint8_t* data() const |
46 | | { |
47 | | return data_.get(); |
48 | | } |
49 | | |
50 | | size_t len() const |
51 | | { |
52 | | return len_; |
53 | | } |
54 | | |
55 | | void SetLength(size_t length) |
56 | 0 | { |
57 | 0 | len_ = length; |
58 | 0 | } |
59 | | |
60 | | size_t capacity() const |
61 | 0 | { |
62 | 0 | return capacity_; |
63 | 0 | } |
64 | | |
65 | | Maybe<size_t>& sdp_level() |
66 | | { |
67 | | return sdp_level_; |
68 | | } |
69 | | |
70 | | void CopyDataToEncrypted() |
71 | 0 | { |
72 | 0 | encrypted_data_ = std::move(data_); |
73 | 0 | encrypted_len_ = len_; |
74 | 0 | Copy(encrypted_data_.get(), len_); |
75 | 0 | } |
76 | | |
77 | | const uint8_t* encrypted_data() const |
78 | | { |
79 | | return encrypted_data_.get(); |
80 | | } |
81 | | |
82 | | size_t encrypted_len() const |
83 | | { |
84 | | return encrypted_len_; |
85 | | } |
86 | | |
87 | | enum Type { |
88 | | UNCLASSIFIED, |
89 | | RTP, |
90 | | RTCP, |
91 | | SCTP |
92 | | }; |
93 | | |
94 | | void SetType(Type type) |
95 | | { |
96 | | type_ = type; |
97 | | } |
98 | | |
99 | | Type type() const |
100 | | { |
101 | | return type_; |
102 | | } |
103 | | |
104 | | private: |
105 | | UniquePtr<uint8_t[]> data_; |
106 | | size_t len_ = 0; |
107 | | size_t capacity_ = 0; |
108 | | // Encrypted form of the data, if there is one. |
109 | | UniquePtr<uint8_t[]> encrypted_data_; |
110 | | size_t encrypted_len_ = 0; |
111 | | // SDP level that this packet belongs to, if known. |
112 | | Maybe<size_t> sdp_level_; |
113 | | Type type_ = UNCLASSIFIED; |
114 | | }; |
115 | | } |
116 | | #endif // mediapacket_h__ |
117 | | |