andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 | #ifndef MODULE_COMMON_TYPES_H |
| 12 | #define MODULE_COMMON_TYPES_H |
| 13 | |
pbos@webrtc.org | 3f45c2e | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | #include <string.h> // memcpy |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 16 | |
| 17 | #include <algorithm> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 18 | |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 19 | #include "webrtc/common_types.h" |
| 20 | #include "webrtc/system_wrappers/interface/constructor_magic.h" |
| 21 | #include "webrtc/typedefs.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef _WIN32 |
| 24 | #pragma warning(disable:4351) // remove warning "new behavior: elements of array |
| 25 | // 'array' will be default initialized" |
| 26 | #endif |
| 27 | |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 28 | namespace webrtc { |
| 29 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 30 | struct RTPHeaderExtension |
| 31 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 32 | int32_t transmissionTimeOffset; |
solenberg@webrtc.org | a0b0025 | 2013-05-16 11:10:31 +0000 | [diff] [blame] | 33 | uint32_t absoluteSendTime; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
stefan@webrtc.org | 6696fba | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 36 | struct RTPHeader |
| 37 | { |
| 38 | bool markerBit; |
| 39 | uint8_t payloadType; |
| 40 | uint16_t sequenceNumber; |
| 41 | uint32_t timestamp; |
| 42 | uint32_t ssrc; |
| 43 | uint8_t numCSRCs; |
| 44 | uint32_t arrOfCSRCs[kRtpCsrcSize]; |
| 45 | uint8_t paddingLength; |
| 46 | uint16_t headerLength; |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 47 | int payload_type_frequency; |
stefan@webrtc.org | 6696fba | 2013-05-29 12:12:51 +0000 | [diff] [blame] | 48 | RTPHeaderExtension extension; |
| 49 | }; |
| 50 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 51 | struct RTPAudioHeader |
| 52 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 53 | uint8_t numEnergy; // number of valid entries in arrOfEnergy |
| 54 | uint8_t arrOfEnergy[kRtpCsrcSize]; // one energy byte (0-9) per channel |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 55 | bool isCNG; // is this CNG |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 56 | uint8_t channel; // number of channels 2 = stereo |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | enum {kNoPictureId = -1}; |
| 60 | enum {kNoTl0PicIdx = -1}; |
| 61 | enum {kNoTemporalIdx = -1}; |
| 62 | enum {kNoKeyIdx = -1}; |
| 63 | enum {kNoSimulcastIdx = 0}; |
| 64 | |
| 65 | struct RTPVideoHeaderVP8 |
| 66 | { |
| 67 | void InitRTPVideoHeaderVP8() |
| 68 | { |
| 69 | nonReference = false; |
| 70 | pictureId = kNoPictureId; |
| 71 | tl0PicIdx = kNoTl0PicIdx; |
| 72 | temporalIdx = kNoTemporalIdx; |
| 73 | layerSync = false; |
| 74 | keyIdx = kNoKeyIdx; |
| 75 | partitionId = 0; |
| 76 | beginningOfPartition = false; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | bool nonReference; // Frame is discardable. |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 80 | int16_t pictureId; // Picture ID index, 15 bits; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 81 | // kNoPictureId if PictureID does not exist. |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 82 | int16_t tl0PicIdx; // TL0PIC_IDX, 8 bits; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 83 | // kNoTl0PicIdx means no value provided. |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 84 | int8_t temporalIdx; // Temporal layer index, or kNoTemporalIdx. |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 85 | bool layerSync; // This frame is a layer sync frame. |
| 86 | // Disabled if temporalIdx == kNoTemporalIdx. |
| 87 | int keyIdx; // 5 bits; kNoKeyIdx means not used. |
| 88 | int partitionId; // VP8 partition ID |
| 89 | bool beginningOfPartition; // True if this packet is the first |
| 90 | // in a VP8 partition. Otherwise false |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 91 | }; |
| 92 | union RTPVideoTypeHeader |
| 93 | { |
| 94 | RTPVideoHeaderVP8 VP8; |
| 95 | }; |
| 96 | |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 97 | enum RtpVideoCodecTypes |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 98 | { |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 99 | kRtpVideoNone, |
| 100 | kRtpVideoGeneric, |
stefan@webrtc.org | db74c61 | 2013-09-06 13:40:11 +0000 | [diff] [blame] | 101 | kRtpVideoVp8 |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 102 | }; |
| 103 | struct RTPVideoHeader |
| 104 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 105 | uint16_t width; // size |
| 106 | uint16_t height; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 107 | |
| 108 | bool isFirstPacket; // first packet in frame |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 109 | uint8_t simulcastIdx; // Index if the simulcast encoder creating |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 110 | // this frame, 0 if not using simulcast. |
wu@webrtc.org | 7fc75bb | 2013-08-15 23:38:54 +0000 | [diff] [blame] | 111 | RtpVideoCodecTypes codec; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 112 | RTPVideoTypeHeader codecHeader; |
| 113 | }; |
| 114 | union RTPTypeHeader |
| 115 | { |
| 116 | RTPAudioHeader Audio; |
| 117 | RTPVideoHeader Video; |
| 118 | }; |
| 119 | |
| 120 | struct WebRtcRTPHeader |
| 121 | { |
| 122 | RTPHeader header; |
| 123 | FrameType frameType; |
| 124 | RTPTypeHeader type; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | class RTPFragmentationHeader |
| 128 | { |
| 129 | public: |
| 130 | RTPFragmentationHeader() : |
| 131 | fragmentationVectorSize(0), |
| 132 | fragmentationOffset(NULL), |
| 133 | fragmentationLength(NULL), |
| 134 | fragmentationTimeDiff(NULL), |
| 135 | fragmentationPlType(NULL) |
| 136 | {}; |
| 137 | |
| 138 | ~RTPFragmentationHeader() |
| 139 | { |
| 140 | delete [] fragmentationOffset; |
| 141 | delete [] fragmentationLength; |
| 142 | delete [] fragmentationTimeDiff; |
| 143 | delete [] fragmentationPlType; |
| 144 | } |
| 145 | |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 146 | void CopyFrom(const RTPFragmentationHeader& src) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 147 | { |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 148 | if(this == &src) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 149 | { |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 150 | return; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 151 | } |
| 152 | |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 153 | if(src.fragmentationVectorSize != fragmentationVectorSize) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 154 | { |
| 155 | // new size of vectors |
| 156 | |
| 157 | // delete old |
| 158 | delete [] fragmentationOffset; |
| 159 | fragmentationOffset = NULL; |
| 160 | delete [] fragmentationLength; |
| 161 | fragmentationLength = NULL; |
| 162 | delete [] fragmentationTimeDiff; |
| 163 | fragmentationTimeDiff = NULL; |
| 164 | delete [] fragmentationPlType; |
| 165 | fragmentationPlType = NULL; |
| 166 | |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 167 | if(src.fragmentationVectorSize > 0) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 168 | { |
| 169 | // allocate new |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 170 | if(src.fragmentationOffset) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 171 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 172 | fragmentationOffset = new uint32_t[src.fragmentationVectorSize]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 173 | } |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 174 | if(src.fragmentationLength) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 175 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 176 | fragmentationLength = new uint32_t[src.fragmentationVectorSize]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 177 | } |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 178 | if(src.fragmentationTimeDiff) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 179 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 180 | fragmentationTimeDiff = new uint16_t[src.fragmentationVectorSize]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 181 | } |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 182 | if(src.fragmentationPlType) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 183 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 184 | fragmentationPlType = new uint8_t[src.fragmentationVectorSize]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | // set new size |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 188 | fragmentationVectorSize = src.fragmentationVectorSize; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 189 | } |
| 190 | |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 191 | if(src.fragmentationVectorSize > 0) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 192 | { |
| 193 | // copy values |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 194 | if(src.fragmentationOffset) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 195 | { |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 196 | memcpy(fragmentationOffset, src.fragmentationOffset, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 197 | src.fragmentationVectorSize * sizeof(uint32_t)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 198 | } |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 199 | if(src.fragmentationLength) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 200 | { |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 201 | memcpy(fragmentationLength, src.fragmentationLength, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 202 | src.fragmentationVectorSize * sizeof(uint32_t)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 203 | } |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 204 | if(src.fragmentationTimeDiff) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 205 | { |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 206 | memcpy(fragmentationTimeDiff, src.fragmentationTimeDiff, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 207 | src.fragmentationVectorSize * sizeof(uint16_t)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 208 | } |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 209 | if(src.fragmentationPlType) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 210 | { |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 211 | memcpy(fragmentationPlType, src.fragmentationPlType, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 212 | src.fragmentationVectorSize * sizeof(uint8_t)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 215 | } |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 216 | |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 217 | void VerifyAndAllocateFragmentationHeader(const uint16_t size) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 218 | { |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 219 | if(fragmentationVectorSize < size) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 220 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 221 | uint16_t oldVectorSize = fragmentationVectorSize; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 222 | { |
| 223 | // offset |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 224 | uint32_t* oldOffsets = fragmentationOffset; |
| 225 | fragmentationOffset = new uint32_t[size]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 226 | memset(fragmentationOffset+oldVectorSize, 0, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 227 | sizeof(uint32_t)*(size-oldVectorSize)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 228 | // copy old values |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 229 | memcpy(fragmentationOffset,oldOffsets, sizeof(uint32_t) * oldVectorSize); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 230 | delete[] oldOffsets; |
| 231 | } |
| 232 | // length |
| 233 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 234 | uint32_t* oldLengths = fragmentationLength; |
| 235 | fragmentationLength = new uint32_t[size]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 236 | memset(fragmentationLength+oldVectorSize, 0, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 237 | sizeof(uint32_t) * (size- oldVectorSize)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 238 | memcpy(fragmentationLength, oldLengths, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 239 | sizeof(uint32_t) * oldVectorSize); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 240 | delete[] oldLengths; |
| 241 | } |
| 242 | // time diff |
| 243 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 244 | uint16_t* oldTimeDiffs = fragmentationTimeDiff; |
| 245 | fragmentationTimeDiff = new uint16_t[size]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 246 | memset(fragmentationTimeDiff+oldVectorSize, 0, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 247 | sizeof(uint16_t) * (size- oldVectorSize)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 248 | memcpy(fragmentationTimeDiff, oldTimeDiffs, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 249 | sizeof(uint16_t) * oldVectorSize); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 250 | delete[] oldTimeDiffs; |
| 251 | } |
| 252 | // payload type |
| 253 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 254 | uint8_t* oldTimePlTypes = fragmentationPlType; |
| 255 | fragmentationPlType = new uint8_t[size]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 256 | memset(fragmentationPlType+oldVectorSize, 0, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 257 | sizeof(uint8_t) * (size- oldVectorSize)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 258 | memcpy(fragmentationPlType, oldTimePlTypes, |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 259 | sizeof(uint8_t) * oldVectorSize); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 260 | delete[] oldTimePlTypes; |
| 261 | } |
| 262 | fragmentationVectorSize = size; |
| 263 | } |
| 264 | } |
| 265 | |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 266 | uint16_t fragmentationVectorSize; // Number of fragmentations |
| 267 | uint32_t* fragmentationOffset; // Offset of pointer to data for each fragm. |
| 268 | uint32_t* fragmentationLength; // Data size for each fragmentation |
| 269 | uint16_t* fragmentationTimeDiff; // Timestamp difference relative "now" for |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 270 | // each fragmentation |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 271 | uint8_t* fragmentationPlType; // Payload type of each fragmentation |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 272 | |
| 273 | private: |
| 274 | DISALLOW_COPY_AND_ASSIGN(RTPFragmentationHeader); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | struct RTCPVoIPMetric |
| 278 | { |
| 279 | // RFC 3611 4.7 |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 280 | uint8_t lossRate; |
| 281 | uint8_t discardRate; |
| 282 | uint8_t burstDensity; |
| 283 | uint8_t gapDensity; |
| 284 | uint16_t burstDuration; |
| 285 | uint16_t gapDuration; |
| 286 | uint16_t roundTripDelay; |
| 287 | uint16_t endSystemDelay; |
| 288 | uint8_t signalLevel; |
| 289 | uint8_t noiseLevel; |
| 290 | uint8_t RERL; |
| 291 | uint8_t Gmin; |
| 292 | uint8_t Rfactor; |
| 293 | uint8_t extRfactor; |
| 294 | uint8_t MOSLQ; |
| 295 | uint8_t MOSCQ; |
| 296 | uint8_t RXconfig; |
| 297 | uint16_t JBnominal; |
| 298 | uint16_t JBmax; |
| 299 | uint16_t JBabsMax; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 300 | }; |
| 301 | |
| 302 | // Types for the FEC packet masks. The type |kFecMaskRandom| is based on a |
| 303 | // random loss model. The type |kFecMaskBursty| is based on a bursty/consecutive |
| 304 | // loss model. The packet masks are defined in |
| 305 | // modules/rtp_rtcp/fec_private_tables_random(bursty).h |
| 306 | enum FecMaskType { |
| 307 | kFecMaskRandom, |
| 308 | kFecMaskBursty, |
| 309 | }; |
| 310 | |
| 311 | // Struct containing forward error correction settings. |
| 312 | struct FecProtectionParams { |
| 313 | int fec_rate; |
| 314 | bool use_uep_protection; |
| 315 | int max_fec_frames; |
| 316 | FecMaskType fec_mask_type; |
| 317 | }; |
| 318 | |
stefan@webrtc.org | 2a5dbce | 2013-02-01 14:33:42 +0000 | [diff] [blame] | 319 | // Interface used by the CallStats class to distribute call statistics. |
| 320 | // Callbacks will be triggered as soon as the class has been registered to a |
| 321 | // CallStats object using RegisterStatsObserver. |
fischman@webrtc.org | 0329e59 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 322 | class CallStatsObserver { |
stefan@webrtc.org | 2a5dbce | 2013-02-01 14:33:42 +0000 | [diff] [blame] | 323 | public: |
| 324 | virtual void OnRttUpdate(uint32_t rtt_ms) = 0; |
| 325 | |
fischman@webrtc.org | 0329e59 | 2013-02-19 22:09:36 +0000 | [diff] [blame] | 326 | virtual ~CallStatsObserver() {} |
stefan@webrtc.org | 2a5dbce | 2013-02-01 14:33:42 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 329 | // class describing a complete, or parts of an encoded frame. |
| 330 | class EncodedVideoData |
| 331 | { |
| 332 | public: |
| 333 | EncodedVideoData() : |
| 334 | payloadType(0), |
| 335 | timeStamp(0), |
| 336 | renderTimeMs(0), |
| 337 | encodedWidth(0), |
| 338 | encodedHeight(0), |
| 339 | completeFrame(false), |
| 340 | missingFrame(false), |
| 341 | payloadData(NULL), |
| 342 | payloadSize(0), |
| 343 | bufferSize(0), |
| 344 | fragmentationHeader(), |
| 345 | frameType(kVideoFrameDelta), |
| 346 | codec(kVideoCodecUnknown) |
| 347 | {}; |
| 348 | |
| 349 | EncodedVideoData(const EncodedVideoData& data) |
| 350 | { |
| 351 | payloadType = data.payloadType; |
| 352 | timeStamp = data.timeStamp; |
| 353 | renderTimeMs = data.renderTimeMs; |
| 354 | encodedWidth = data.encodedWidth; |
| 355 | encodedHeight = data.encodedHeight; |
| 356 | completeFrame = data.completeFrame; |
| 357 | missingFrame = data.missingFrame; |
| 358 | payloadSize = data.payloadSize; |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 359 | fragmentationHeader.CopyFrom(data.fragmentationHeader); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 360 | frameType = data.frameType; |
| 361 | codec = data.codec; |
| 362 | if (data.payloadSize > 0) |
| 363 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 364 | payloadData = new uint8_t[data.payloadSize]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 365 | memcpy(payloadData, data.payloadData, data.payloadSize); |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | payloadData = NULL; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | |
| 374 | ~EncodedVideoData() |
| 375 | { |
| 376 | delete [] payloadData; |
| 377 | }; |
| 378 | |
| 379 | EncodedVideoData& operator=(const EncodedVideoData& data) |
| 380 | { |
| 381 | if (this == &data) |
| 382 | { |
| 383 | return *this; |
| 384 | } |
| 385 | payloadType = data.payloadType; |
| 386 | timeStamp = data.timeStamp; |
| 387 | renderTimeMs = data.renderTimeMs; |
| 388 | encodedWidth = data.encodedWidth; |
| 389 | encodedHeight = data.encodedHeight; |
| 390 | completeFrame = data.completeFrame; |
| 391 | missingFrame = data.missingFrame; |
| 392 | payloadSize = data.payloadSize; |
andrew@webrtc.org | 856edd5 | 2012-11-23 19:17:23 +0000 | [diff] [blame] | 393 | fragmentationHeader.CopyFrom(data.fragmentationHeader); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 394 | frameType = data.frameType; |
| 395 | codec = data.codec; |
| 396 | if (data.payloadSize > 0) |
| 397 | { |
| 398 | delete [] payloadData; |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 399 | payloadData = new uint8_t[data.payloadSize]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 400 | memcpy(payloadData, data.payloadData, data.payloadSize); |
| 401 | bufferSize = data.payloadSize; |
| 402 | } |
| 403 | return *this; |
| 404 | }; |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 405 | void VerifyAndAllocate( const uint32_t size) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 406 | { |
| 407 | if (bufferSize < size) |
| 408 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 409 | uint8_t* oldPayload = payloadData; |
| 410 | payloadData = new uint8_t[size]; |
| 411 | memcpy(payloadData, oldPayload, sizeof(uint8_t) * payloadSize); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 412 | |
| 413 | bufferSize = size; |
| 414 | delete[] oldPayload; |
| 415 | } |
| 416 | } |
| 417 | |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 418 | uint8_t payloadType; |
| 419 | uint32_t timeStamp; |
| 420 | int64_t renderTimeMs; |
| 421 | uint32_t encodedWidth; |
| 422 | uint32_t encodedHeight; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 423 | bool completeFrame; |
| 424 | bool missingFrame; |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 425 | uint8_t* payloadData; |
| 426 | uint32_t payloadSize; |
| 427 | uint32_t bufferSize; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 428 | RTPFragmentationHeader fragmentationHeader; |
| 429 | FrameType frameType; |
| 430 | VideoCodecType codec; |
| 431 | }; |
| 432 | |
| 433 | struct VideoContentMetrics { |
| 434 | VideoContentMetrics() |
| 435 | : motion_magnitude(0.0f), |
| 436 | spatial_pred_err(0.0f), |
| 437 | spatial_pred_err_h(0.0f), |
| 438 | spatial_pred_err_v(0.0f) { |
| 439 | } |
| 440 | |
| 441 | void Reset() { |
| 442 | motion_magnitude = 0.0f; |
| 443 | spatial_pred_err = 0.0f; |
| 444 | spatial_pred_err_h = 0.0f; |
| 445 | spatial_pred_err_v = 0.0f; |
| 446 | } |
| 447 | float motion_magnitude; |
| 448 | float spatial_pred_err; |
| 449 | float spatial_pred_err_h; |
| 450 | float spatial_pred_err_v; |
| 451 | }; |
| 452 | |
| 453 | /************************************************* |
| 454 | * |
| 455 | * VideoFrame class |
| 456 | * |
| 457 | * The VideoFrame class allows storing and |
| 458 | * handling of video frames. |
| 459 | * |
| 460 | * |
| 461 | *************************************************/ |
| 462 | class VideoFrame |
| 463 | { |
| 464 | public: |
| 465 | VideoFrame(); |
| 466 | ~VideoFrame(); |
| 467 | /** |
| 468 | * Verifies that current allocated buffer size is larger than or equal to the input size. |
| 469 | * If the current buffer size is smaller, a new allocation is made and the old buffer data |
| 470 | * is copied to the new buffer. |
| 471 | * Buffer size is updated to minimumSize. |
| 472 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 473 | int32_t VerifyAndAllocate(const uint32_t minimumSize); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 474 | /** |
| 475 | * Update length of data buffer in frame. Function verifies that new length is less or |
| 476 | * equal to allocated size. |
| 477 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 478 | int32_t SetLength(const uint32_t newLength); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 479 | /* |
| 480 | * Swap buffer and size data |
| 481 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 482 | int32_t Swap(uint8_t*& newMemory, |
| 483 | uint32_t& newLength, |
| 484 | uint32_t& newSize); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 485 | /* |
| 486 | * Swap buffer and size data |
| 487 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 488 | int32_t SwapFrame(VideoFrame& videoFrame); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 489 | /** |
| 490 | * Copy buffer: If newLength is bigger than allocated size, a new buffer of size length |
| 491 | * is allocated. |
| 492 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 493 | int32_t CopyFrame(const VideoFrame& videoFrame); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 494 | /** |
| 495 | * Copy buffer: If newLength is bigger than allocated size, a new buffer of size length |
| 496 | * is allocated. |
| 497 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 498 | int32_t CopyFrame(uint32_t length, const uint8_t* sourceBuffer); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 499 | /** |
| 500 | * Delete VideoFrame and resets members to zero |
| 501 | */ |
| 502 | void Free(); |
| 503 | /** |
| 504 | * Set frame timestamp (90kHz) |
| 505 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 506 | void SetTimeStamp(const uint32_t timeStamp) {_timeStamp = timeStamp;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 507 | /** |
| 508 | * Get pointer to frame buffer |
| 509 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 510 | uint8_t* Buffer() const {return _buffer;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 511 | |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 512 | uint8_t*& Buffer() {return _buffer;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * Get allocated buffer size |
| 516 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 517 | uint32_t Size() const {return _bufferSize;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 518 | /** |
| 519 | * Get frame length |
| 520 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 521 | uint32_t Length() const {return _bufferLength;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 522 | /** |
| 523 | * Get frame timestamp (90kHz) |
| 524 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 525 | uint32_t TimeStamp() const {return _timeStamp;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 526 | /** |
| 527 | * Get frame width |
| 528 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 529 | uint32_t Width() const {return _width;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 530 | /** |
| 531 | * Get frame height |
| 532 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 533 | uint32_t Height() const {return _height;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 534 | /** |
| 535 | * Set frame width |
| 536 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 537 | void SetWidth(const uint32_t width) {_width = width;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 538 | /** |
| 539 | * Set frame height |
| 540 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 541 | void SetHeight(const uint32_t height) {_height = height;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 542 | /** |
| 543 | * Set render time in miliseconds |
| 544 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 545 | void SetRenderTime(const int64_t renderTimeMs) {_renderTimeMs = renderTimeMs;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 546 | /** |
| 547 | * Get render time in miliseconds |
| 548 | */ |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 549 | int64_t RenderTimeMs() const {return _renderTimeMs;} |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 550 | |
| 551 | private: |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 552 | void Set(uint8_t* buffer, |
| 553 | uint32_t size, |
| 554 | uint32_t length, |
| 555 | uint32_t timeStamp); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 556 | |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 557 | uint8_t* _buffer; // Pointer to frame buffer |
| 558 | uint32_t _bufferSize; // Allocated buffer size |
| 559 | uint32_t _bufferLength; // Length (in bytes) of buffer |
| 560 | uint32_t _timeStamp; // Timestamp of frame (90kHz) |
| 561 | uint32_t _width; |
| 562 | uint32_t _height; |
| 563 | int64_t _renderTimeMs; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 564 | }; // end of VideoFrame class declaration |
| 565 | |
| 566 | // inline implementation of VideoFrame class: |
| 567 | inline |
| 568 | VideoFrame::VideoFrame(): |
| 569 | _buffer(0), |
| 570 | _bufferSize(0), |
| 571 | _bufferLength(0), |
| 572 | _timeStamp(0), |
| 573 | _width(0), |
| 574 | _height(0), |
| 575 | _renderTimeMs(0) |
| 576 | { |
| 577 | // |
| 578 | } |
| 579 | inline |
| 580 | VideoFrame::~VideoFrame() |
| 581 | { |
| 582 | if(_buffer) |
| 583 | { |
| 584 | delete [] _buffer; |
| 585 | _buffer = NULL; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | |
| 590 | inline |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 591 | int32_t |
| 592 | VideoFrame::VerifyAndAllocate(const uint32_t minimumSize) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 593 | { |
| 594 | if (minimumSize < 1) |
| 595 | { |
| 596 | return -1; |
| 597 | } |
| 598 | if(minimumSize > _bufferSize) |
| 599 | { |
| 600 | // create buffer of sufficient size |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 601 | uint8_t* newBufferBuffer = new uint8_t[minimumSize]; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 602 | if(_buffer) |
| 603 | { |
| 604 | // copy old data |
| 605 | memcpy(newBufferBuffer, _buffer, _bufferSize); |
| 606 | delete [] _buffer; |
| 607 | } |
| 608 | else |
| 609 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 610 | memset(newBufferBuffer, 0, minimumSize * sizeof(uint8_t)); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 611 | } |
| 612 | _buffer = newBufferBuffer; |
| 613 | _bufferSize = minimumSize; |
| 614 | } |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | inline |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 619 | int32_t |
| 620 | VideoFrame::SetLength(const uint32_t newLength) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 621 | { |
| 622 | if (newLength >_bufferSize ) |
| 623 | { // can't accomodate new value |
| 624 | return -1; |
| 625 | } |
| 626 | _bufferLength = newLength; |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | inline |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 631 | int32_t |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 632 | VideoFrame::SwapFrame(VideoFrame& videoFrame) |
| 633 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 634 | uint32_t tmpTimeStamp = _timeStamp; |
| 635 | uint32_t tmpWidth = _width; |
| 636 | uint32_t tmpHeight = _height; |
| 637 | int64_t tmpRenderTime = _renderTimeMs; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 638 | |
| 639 | _timeStamp = videoFrame._timeStamp; |
| 640 | _width = videoFrame._width; |
| 641 | _height = videoFrame._height; |
| 642 | _renderTimeMs = videoFrame._renderTimeMs; |
| 643 | |
| 644 | videoFrame._timeStamp = tmpTimeStamp; |
| 645 | videoFrame._width = tmpWidth; |
| 646 | videoFrame._height = tmpHeight; |
| 647 | videoFrame._renderTimeMs = tmpRenderTime; |
| 648 | |
| 649 | return Swap(videoFrame._buffer, videoFrame._bufferLength, videoFrame._bufferSize); |
| 650 | } |
| 651 | |
| 652 | inline |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 653 | int32_t |
| 654 | VideoFrame::Swap(uint8_t*& newMemory, uint32_t& newLength, uint32_t& newSize) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 655 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 656 | uint8_t* tmpBuffer = _buffer; |
| 657 | uint32_t tmpLength = _bufferLength; |
| 658 | uint32_t tmpSize = _bufferSize; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 659 | _buffer = newMemory; |
| 660 | _bufferLength = newLength; |
| 661 | _bufferSize = newSize; |
| 662 | newMemory = tmpBuffer; |
| 663 | newLength = tmpLength; |
| 664 | newSize = tmpSize; |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | inline |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 669 | int32_t |
| 670 | VideoFrame::CopyFrame(uint32_t length, const uint8_t* sourceBuffer) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 671 | { |
| 672 | if (length > _bufferSize) |
| 673 | { |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 674 | int32_t ret = VerifyAndAllocate(length); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 675 | if (ret < 0) |
| 676 | { |
| 677 | return ret; |
| 678 | } |
| 679 | } |
| 680 | memcpy(_buffer, sourceBuffer, length); |
| 681 | _bufferLength = length; |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | inline |
pbos@webrtc.org | 0486a10 | 2013-04-10 17:59:17 +0000 | [diff] [blame] | 686 | int32_t |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 687 | VideoFrame::CopyFrame(const VideoFrame& videoFrame) |
| 688 | { |
| 689 | if(CopyFrame(videoFrame.Length(), videoFrame.Buffer()) != 0) |
| 690 | { |
| 691 | return -1; |
| 692 | } |
| 693 | _timeStamp = videoFrame._timeStamp; |
| 694 | _width = videoFrame._width; |
| 695 | _height = videoFrame._height; |
| 696 | _renderTimeMs = videoFrame._renderTimeMs; |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | inline |
| 701 | void |
| 702 | VideoFrame::Free() |
| 703 | { |
| 704 | _timeStamp = 0; |
| 705 | _bufferLength = 0; |
| 706 | _bufferSize = 0; |
| 707 | _height = 0; |
| 708 | _width = 0; |
| 709 | _renderTimeMs = 0; |
| 710 | |
| 711 | if(_buffer) |
| 712 | { |
| 713 | delete [] _buffer; |
| 714 | _buffer = NULL; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | |
| 719 | /* This class holds up to 60 ms of super-wideband (32 kHz) stereo audio. It |
| 720 | * allows for adding and subtracting frames while keeping track of the resulting |
| 721 | * states. |
| 722 | * |
| 723 | * Notes |
| 724 | * - The total number of samples in |data_| is |
| 725 | * samples_per_channel_ * num_channels_ |
| 726 | * |
| 727 | * - Stereo data is interleaved starting with the left channel. |
| 728 | * |
| 729 | * - The +operator assume that you would never add exactly opposite frames when |
| 730 | * deciding the resulting state. To do this use the -operator. |
| 731 | */ |
| 732 | class AudioFrame |
| 733 | { |
| 734 | public: |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 735 | // Stereo, 32 kHz, 60 ms (2 * 32 * 60) |
| 736 | static const int kMaxDataSizeSamples = 3840; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 737 | |
| 738 | enum VADActivity |
| 739 | { |
| 740 | kVadActive = 0, |
| 741 | kVadPassive = 1, |
| 742 | kVadUnknown = 2 |
| 743 | }; |
| 744 | enum SpeechType |
| 745 | { |
| 746 | kNormalSpeech = 0, |
| 747 | kPLC = 1, |
| 748 | kCNG = 2, |
| 749 | kPLCCNG = 3, |
| 750 | kUndefined = 4 |
| 751 | }; |
| 752 | |
| 753 | AudioFrame(); |
| 754 | virtual ~AudioFrame(); |
| 755 | |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 756 | void UpdateFrame( |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 757 | int id, |
| 758 | uint32_t timestamp, |
| 759 | const int16_t* data, |
| 760 | int samples_per_channel, |
| 761 | int sample_rate_hz, |
| 762 | SpeechType speech_type, |
| 763 | VADActivity vad_activity, |
| 764 | int num_channels = 1, |
| 765 | uint32_t energy = -1); |
| 766 | |
| 767 | AudioFrame& Append(const AudioFrame& rhs); |
| 768 | |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 769 | void CopyFrom(const AudioFrame& src); |
| 770 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 771 | void Mute(); |
| 772 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 773 | AudioFrame& operator>>=(const int rhs); |
| 774 | AudioFrame& operator+=(const AudioFrame& rhs); |
| 775 | AudioFrame& operator-=(const AudioFrame& rhs); |
| 776 | |
| 777 | int id_; |
| 778 | uint32_t timestamp_; |
| 779 | int16_t data_[kMaxDataSizeSamples]; |
| 780 | int samples_per_channel_; |
| 781 | int sample_rate_hz_; |
| 782 | int num_channels_; |
| 783 | SpeechType speech_type_; |
| 784 | VADActivity vad_activity_; |
| 785 | uint32_t energy_; |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 786 | |
| 787 | private: |
| 788 | DISALLOW_COPY_AND_ASSIGN(AudioFrame); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 789 | }; |
| 790 | |
| 791 | inline |
| 792 | AudioFrame::AudioFrame() |
| 793 | : |
| 794 | id_(-1), |
| 795 | timestamp_(0), |
| 796 | data_(), |
| 797 | samples_per_channel_(0), |
| 798 | sample_rate_hz_(0), |
| 799 | num_channels_(1), |
| 800 | speech_type_(kUndefined), |
| 801 | vad_activity_(kVadUnknown), |
| 802 | energy_(0xffffffff) |
| 803 | { |
| 804 | } |
| 805 | |
| 806 | inline |
| 807 | AudioFrame::~AudioFrame() |
| 808 | { |
| 809 | } |
| 810 | |
| 811 | inline |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 812 | void |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 813 | AudioFrame::UpdateFrame( |
| 814 | int id, |
| 815 | uint32_t timestamp, |
| 816 | const int16_t* data, |
| 817 | int samples_per_channel, |
| 818 | int sample_rate_hz, |
| 819 | SpeechType speech_type, |
| 820 | VADActivity vad_activity, |
| 821 | int num_channels, |
| 822 | uint32_t energy) |
| 823 | { |
| 824 | id_ = id; |
| 825 | timestamp_ = timestamp; |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 826 | samples_per_channel_ = samples_per_channel; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 827 | sample_rate_hz_ = sample_rate_hz; |
| 828 | speech_type_ = speech_type; |
| 829 | vad_activity_ = vad_activity; |
| 830 | num_channels_ = num_channels; |
| 831 | energy_ = energy; |
| 832 | |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 833 | const int length = samples_per_channel * num_channels; |
| 834 | assert(length <= kMaxDataSizeSamples && length >= 0); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 835 | if(data != NULL) |
| 836 | { |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 837 | memcpy(data_, data, sizeof(int16_t) * length); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 838 | } |
| 839 | else |
| 840 | { |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 841 | memset(data_, 0, sizeof(int16_t) * length); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 842 | } |
andrew@webrtc.org | d468236 | 2013-01-22 04:44:30 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | inline void AudioFrame::CopyFrom(const AudioFrame& src) |
| 846 | { |
| 847 | if(this == &src) |
| 848 | { |
| 849 | return; |
| 850 | } |
| 851 | id_ = src.id_; |
| 852 | timestamp_ = src.timestamp_; |
| 853 | samples_per_channel_ = src.samples_per_channel_; |
| 854 | sample_rate_hz_ = src.sample_rate_hz_; |
| 855 | speech_type_ = src.speech_type_; |
| 856 | vad_activity_ = src.vad_activity_; |
| 857 | num_channels_ = src.num_channels_; |
| 858 | energy_ = src.energy_; |
| 859 | |
| 860 | const int length = samples_per_channel_ * num_channels_; |
| 861 | assert(length <= kMaxDataSizeSamples && length >= 0); |
| 862 | memcpy(data_, src.data_, sizeof(int16_t) * length); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | inline |
| 866 | void |
| 867 | AudioFrame::Mute() |
| 868 | { |
| 869 | memset(data_, 0, samples_per_channel_ * num_channels_ * sizeof(int16_t)); |
| 870 | } |
| 871 | |
| 872 | inline |
| 873 | AudioFrame& |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 874 | AudioFrame::operator>>=(const int rhs) |
| 875 | { |
| 876 | assert((num_channels_ > 0) && (num_channels_ < 3)); |
| 877 | if((num_channels_ > 2) || |
| 878 | (num_channels_ < 1)) |
| 879 | { |
| 880 | return *this; |
| 881 | } |
| 882 | for(int i = 0; i < samples_per_channel_ * num_channels_; i++) |
| 883 | { |
| 884 | data_[i] = static_cast<int16_t>(data_[i] >> rhs); |
| 885 | } |
| 886 | return *this; |
| 887 | } |
| 888 | |
| 889 | inline |
| 890 | AudioFrame& |
| 891 | AudioFrame::Append(const AudioFrame& rhs) |
| 892 | { |
| 893 | // Sanity check |
| 894 | assert((num_channels_ > 0) && (num_channels_ < 3)); |
| 895 | if((num_channels_ > 2) || |
| 896 | (num_channels_ < 1)) |
| 897 | { |
| 898 | return *this; |
| 899 | } |
| 900 | if(num_channels_ != rhs.num_channels_) |
| 901 | { |
| 902 | return *this; |
| 903 | } |
| 904 | if((vad_activity_ == kVadActive) || |
| 905 | rhs.vad_activity_ == kVadActive) |
| 906 | { |
| 907 | vad_activity_ = kVadActive; |
| 908 | } |
| 909 | else if((vad_activity_ == kVadUnknown) || |
| 910 | rhs.vad_activity_ == kVadUnknown) |
| 911 | { |
| 912 | vad_activity_ = kVadUnknown; |
| 913 | } |
| 914 | if(speech_type_ != rhs.speech_type_) |
| 915 | { |
| 916 | speech_type_ = kUndefined; |
| 917 | } |
| 918 | |
| 919 | int offset = samples_per_channel_ * num_channels_; |
| 920 | for(int i = 0; |
| 921 | i < rhs.samples_per_channel_ * rhs.num_channels_; |
| 922 | i++) |
| 923 | { |
| 924 | data_[offset+i] = rhs.data_[i]; |
| 925 | } |
| 926 | samples_per_channel_ += rhs.samples_per_channel_; |
| 927 | return *this; |
| 928 | } |
| 929 | |
| 930 | // merge vectors |
| 931 | inline |
| 932 | AudioFrame& |
| 933 | AudioFrame::operator+=(const AudioFrame& rhs) |
| 934 | { |
| 935 | // Sanity check |
| 936 | assert((num_channels_ > 0) && (num_channels_ < 3)); |
| 937 | if((num_channels_ > 2) || |
| 938 | (num_channels_ < 1)) |
| 939 | { |
| 940 | return *this; |
| 941 | } |
| 942 | if(num_channels_ != rhs.num_channels_) |
| 943 | { |
| 944 | return *this; |
| 945 | } |
| 946 | bool noPrevData = false; |
| 947 | if(samples_per_channel_ != rhs.samples_per_channel_) |
| 948 | { |
| 949 | if(samples_per_channel_ == 0) |
| 950 | { |
| 951 | // special case we have no data to start with |
| 952 | samples_per_channel_ = rhs.samples_per_channel_; |
| 953 | noPrevData = true; |
| 954 | } else |
| 955 | { |
| 956 | return *this; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | if((vad_activity_ == kVadActive) || |
| 961 | rhs.vad_activity_ == kVadActive) |
| 962 | { |
| 963 | vad_activity_ = kVadActive; |
| 964 | } |
| 965 | else if((vad_activity_ == kVadUnknown) || |
| 966 | rhs.vad_activity_ == kVadUnknown) |
| 967 | { |
| 968 | vad_activity_ = kVadUnknown; |
| 969 | } |
| 970 | |
| 971 | if(speech_type_ != rhs.speech_type_) |
| 972 | { |
| 973 | speech_type_ = kUndefined; |
| 974 | } |
| 975 | |
| 976 | if(noPrevData) |
| 977 | { |
| 978 | memcpy(data_, rhs.data_, |
| 979 | sizeof(int16_t) * rhs.samples_per_channel_ * num_channels_); |
| 980 | } else |
| 981 | { |
| 982 | // IMPROVEMENT this can be done very fast in assembly |
| 983 | for(int i = 0; i < samples_per_channel_ * num_channels_; i++) |
| 984 | { |
| 985 | int32_t wrapGuard = static_cast<int32_t>(data_[i]) + |
| 986 | static_cast<int32_t>(rhs.data_[i]); |
| 987 | if(wrapGuard < -32768) |
| 988 | { |
| 989 | data_[i] = -32768; |
| 990 | }else if(wrapGuard > 32767) |
| 991 | { |
| 992 | data_[i] = 32767; |
| 993 | }else |
| 994 | { |
| 995 | data_[i] = (int16_t)wrapGuard; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | energy_ = 0xffffffff; |
| 1000 | return *this; |
| 1001 | } |
| 1002 | |
| 1003 | inline |
| 1004 | AudioFrame& |
| 1005 | AudioFrame::operator-=(const AudioFrame& rhs) |
| 1006 | { |
| 1007 | // Sanity check |
| 1008 | assert((num_channels_ > 0) && (num_channels_ < 3)); |
| 1009 | if((num_channels_ > 2)|| |
| 1010 | (num_channels_ < 1)) |
| 1011 | { |
| 1012 | return *this; |
| 1013 | } |
| 1014 | if((samples_per_channel_ != rhs.samples_per_channel_) || |
| 1015 | (num_channels_ != rhs.num_channels_)) |
| 1016 | { |
| 1017 | return *this; |
| 1018 | } |
| 1019 | if((vad_activity_ != kVadPassive) || |
| 1020 | rhs.vad_activity_ != kVadPassive) |
| 1021 | { |
| 1022 | vad_activity_ = kVadUnknown; |
| 1023 | } |
| 1024 | speech_type_ = kUndefined; |
| 1025 | |
| 1026 | for(int i = 0; i < samples_per_channel_ * num_channels_; i++) |
| 1027 | { |
| 1028 | int32_t wrapGuard = static_cast<int32_t>(data_[i]) - |
| 1029 | static_cast<int32_t>(rhs.data_[i]); |
| 1030 | if(wrapGuard < -32768) |
| 1031 | { |
| 1032 | data_[i] = -32768; |
| 1033 | } |
| 1034 | else if(wrapGuard > 32767) |
| 1035 | { |
| 1036 | data_[i] = 32767; |
| 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | data_[i] = (int16_t)wrapGuard; |
| 1041 | } |
| 1042 | } |
| 1043 | energy_ = 0xffffffff; |
| 1044 | return *this; |
| 1045 | } |
| 1046 | |
stefan@webrtc.org | 42c7409 | 2013-04-11 17:48:02 +0000 | [diff] [blame] | 1047 | inline bool IsNewerSequenceNumber(uint16_t sequence_number, |
| 1048 | uint16_t prev_sequence_number) { |
| 1049 | return sequence_number != prev_sequence_number && |
| 1050 | static_cast<uint16_t>(sequence_number - prev_sequence_number) < 0x8000; |
| 1051 | } |
| 1052 | |
| 1053 | inline bool IsNewerTimestamp(uint32_t timestamp, uint32_t prev_timestamp) { |
| 1054 | return timestamp != prev_timestamp && |
| 1055 | static_cast<uint32_t>(timestamp - prev_timestamp) < 0x80000000; |
| 1056 | } |
| 1057 | |
| 1058 | inline uint16_t LatestSequenceNumber(uint16_t sequence_number1, |
| 1059 | uint16_t sequence_number2) { |
| 1060 | return IsNewerSequenceNumber(sequence_number1, sequence_number2) ? |
| 1061 | sequence_number1 : sequence_number2; |
| 1062 | } |
| 1063 | |
| 1064 | inline uint32_t LatestTimestamp(uint32_t timestamp1, uint32_t timestamp2) { |
| 1065 | return IsNewerTimestamp(timestamp1, timestamp2) ? timestamp1 : |
| 1066 | timestamp2; |
| 1067 | } |
| 1068 | |
pbos@webrtc.org | 3b89e10 | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 1069 | } // namespace webrtc |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1070 | |
| 1071 | #endif // MODULE_COMMON_TYPES_H |