1b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*
2b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
4b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
10b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
11b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#ifndef MODULE_COMMON_TYPES_H
12b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org#define MODULE_COMMON_TYPES_H
13b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
143f45c2e0ac4cb280f941efa3a3476895795e3dd6pbos@webrtc.org#include <assert.h>
1577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org#include <string.h>  // memcpy
16d4682361fd4d284657e4e33ca5f022cc8ea8f468andrew@webrtc.org
17d4682361fd4d284657e4e33ca5f022cc8ea8f468andrew@webrtc.org#include <algorithm>
18b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
19774b3d38a4a0f1a8ec08972a3c543cb5d607ce13henrike@webrtc.org#include "webrtc/base/constructormagic.h"
20856edd527f7c49517d6d866b602defd743ca0bdbandrew@webrtc.org#include "webrtc/common_types.h"
21856edd527f7c49517d6d866b602defd743ca0bdbandrew@webrtc.org#include "webrtc/typedefs.h"
22b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
23856edd527f7c49517d6d866b602defd743ca0bdbandrew@webrtc.orgnamespace webrtc {
24856edd527f7c49517d6d866b602defd743ca0bdbandrew@webrtc.org
2577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgstruct RTPAudioHeader {
2677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t numEnergy;                  // number of valid entries in arrOfEnergy
2777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t arrOfEnergy[kRtpCsrcSize];  // one energy byte (0-9) per channel
2877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  bool isCNG;                         // is this CNG
2977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t channel;                    // number of channels 2 = stereo
30b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
31b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
329fbd3ec906560447cebb21681c7e79e37c2eed83tommi@webrtc.orgconst int16_t kNoPictureId = -1;
339fbd3ec906560447cebb21681c7e79e37c2eed83tommi@webrtc.orgconst int16_t kNoTl0PicIdx = -1;
349fbd3ec906560447cebb21681c7e79e37c2eed83tommi@webrtc.orgconst uint8_t kNoTemporalIdx = 0xFF;
359fbd3ec906560447cebb21681c7e79e37c2eed83tommi@webrtc.orgconst int kNoKeyIdx = -1;
3677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
3777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgstruct RTPVideoHeaderVP8 {
3877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void InitRTPVideoHeaderVP8() {
3977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    nonReference = false;
4077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    pictureId = kNoPictureId;
4177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    tl0PicIdx = kNoTl0PicIdx;
4277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    temporalIdx = kNoTemporalIdx;
4377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    layerSync = false;
4477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    keyIdx = kNoKeyIdx;
4577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    partitionId = 0;
4677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    beginningOfPartition = false;
4777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
48b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
4977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  bool nonReference;          // Frame is discardable.
5077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int16_t pictureId;          // Picture ID index, 15 bits;
5177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                              // kNoPictureId if PictureID does not exist.
5277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int16_t tl0PicIdx;          // TL0PIC_IDX, 8 bits;
5377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                              // kNoTl0PicIdx means no value provided.
549fbd3ec906560447cebb21681c7e79e37c2eed83tommi@webrtc.org  uint8_t temporalIdx;        // Temporal layer index, or kNoTemporalIdx.
5577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  bool layerSync;             // This frame is a layer sync frame.
5677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                              // Disabled if temporalIdx == kNoTemporalIdx.
5777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int keyIdx;                 // 5 bits; kNoKeyIdx means not used.
5877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int partitionId;            // VP8 partition ID
5977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  bool beginningOfPartition;  // True if this packet is the first
6077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                              // in a VP8 partition. Otherwise false
61b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
622d4a80c05adffe07b48dc235475852450cd90568stefan@webrtc.org
632d4a80c05adffe07b48dc235475852450cd90568stefan@webrtc.orgstruct RTPVideoHeaderH264 {
64e75b34844a4e9998475ab557001ce0cfa1fe458fstefan@webrtc.org  bool stap_a;
65e75b34844a4e9998475ab557001ce0cfa1fe458fstefan@webrtc.org  bool single_nalu;
662d4a80c05adffe07b48dc235475852450cd90568stefan@webrtc.org};
672d4a80c05adffe07b48dc235475852450cd90568stefan@webrtc.org
6877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgunion RTPVideoTypeHeader {
6977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPVideoHeaderVP8 VP8;
702d4a80c05adffe07b48dc235475852450cd90568stefan@webrtc.org  RTPVideoHeaderH264 H264;
71b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
72b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
7377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgenum RtpVideoCodecTypes {
7477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  kRtpVideoNone,
7577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  kRtpVideoGeneric,
762d4a80c05adffe07b48dc235475852450cd90568stefan@webrtc.org  kRtpVideoVp8,
772d4a80c05adffe07b48dc235475852450cd90568stefan@webrtc.org  kRtpVideoH264
78b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
7977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgstruct RTPVideoHeader {
8077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t width;  // size
8177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t height;
8277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
8377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  bool isFirstPacket;    // first packet in frame
8477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t simulcastIdx;  // Index if the simulcast encoder creating
8577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                         // this frame, 0 if not using simulcast.
8677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RtpVideoCodecTypes codec;
8777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPVideoTypeHeader codecHeader;
88b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
8977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgunion RTPTypeHeader {
9077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPAudioHeader Audio;
9177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPVideoHeader Video;
92b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
93b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
9477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgstruct WebRtcRTPHeader {
9577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPHeader header;
9677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  FrameType frameType;
9777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPTypeHeader type;
989d10769e109601915022fea44ec392645c3b0704wu@webrtc.org  // NTP time of the capture time in local timebase in milliseconds.
999d10769e109601915022fea44ec392645c3b0704wu@webrtc.org  int64_t ntp_time_ms;
100b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
101b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
10277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgclass RTPFragmentationHeader {
10377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org public:
10477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPFragmentationHeader()
10577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      : fragmentationVectorSize(0),
106b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        fragmentationOffset(NULL),
107b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        fragmentationLength(NULL),
108b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        fragmentationTimeDiff(NULL),
10977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        fragmentationPlType(NULL) {};
11077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
11177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  ~RTPFragmentationHeader() {
11277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    delete[] fragmentationOffset;
11377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    delete[] fragmentationLength;
11477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    delete[] fragmentationTimeDiff;
11577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    delete[] fragmentationPlType;
11677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
11777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
11877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void CopyFrom(const RTPFragmentationHeader& src) {
11977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (this == &src) {
12077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      return;
121b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
122b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
12377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (src.fragmentationVectorSize != fragmentationVectorSize) {
12477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // new size of vectors
12577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
12677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // delete old
12777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      delete[] fragmentationOffset;
12877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      fragmentationOffset = NULL;
12977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      delete[] fragmentationLength;
13077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      fragmentationLength = NULL;
13177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      delete[] fragmentationTimeDiff;
13277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      fragmentationTimeDiff = NULL;
13377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      delete[] fragmentationPlType;
13477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      fragmentationPlType = NULL;
13577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
13677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      if (src.fragmentationVectorSize > 0) {
13777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        // allocate new
13877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        if (src.fragmentationOffset) {
13977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org          fragmentationOffset = new uint32_t[src.fragmentationVectorSize];
140b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        }
14177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        if (src.fragmentationLength) {
14277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org          fragmentationLength = new uint32_t[src.fragmentationVectorSize];
143b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        }
14477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        if (src.fragmentationTimeDiff) {
14577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org          fragmentationTimeDiff = new uint16_t[src.fragmentationVectorSize];
14677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        }
14777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        if (src.fragmentationPlType) {
14877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org          fragmentationPlType = new uint8_t[src.fragmentationVectorSize];
149b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        }
15077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
15177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // set new size
15277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      fragmentationVectorSize = src.fragmentationVectorSize;
153b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
154856edd527f7c49517d6d866b602defd743ca0bdbandrew@webrtc.org
15577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (src.fragmentationVectorSize > 0) {
15677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // copy values
15777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      if (src.fragmentationOffset) {
15877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationOffset, src.fragmentationOffset,
15977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               src.fragmentationVectorSize * sizeof(uint32_t));
16077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
16177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      if (src.fragmentationLength) {
16277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationLength, src.fragmentationLength,
16377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               src.fragmentationVectorSize * sizeof(uint32_t));
16477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
16577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      if (src.fragmentationTimeDiff) {
16677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationTimeDiff, src.fragmentationTimeDiff,
16777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               src.fragmentationVectorSize * sizeof(uint16_t));
16877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
16977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      if (src.fragmentationPlType) {
17077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationPlType, src.fragmentationPlType,
17177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               src.fragmentationVectorSize * sizeof(uint8_t));
17277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
17377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    }
17477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
17577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
17677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void VerifyAndAllocateFragmentationHeader(const uint16_t size) {
17777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (fragmentationVectorSize < size) {
17877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      uint16_t oldVectorSize = fragmentationVectorSize;
17977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      {
18077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        // offset
18177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        uint32_t* oldOffsets = fragmentationOffset;
18277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        fragmentationOffset = new uint32_t[size];
18377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memset(fragmentationOffset + oldVectorSize, 0,
18477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint32_t) * (size - oldVectorSize));
18577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        // copy old values
18677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationOffset, oldOffsets,
18777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint32_t) * oldVectorSize);
18877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        delete[] oldOffsets;
18977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
19077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // length
19177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      {
19277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        uint32_t* oldLengths = fragmentationLength;
19377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        fragmentationLength = new uint32_t[size];
19477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memset(fragmentationLength + oldVectorSize, 0,
19577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint32_t) * (size - oldVectorSize));
19677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationLength, oldLengths,
19777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint32_t) * oldVectorSize);
19877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        delete[] oldLengths;
19977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
20077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // time diff
20177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      {
20277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        uint16_t* oldTimeDiffs = fragmentationTimeDiff;
20377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        fragmentationTimeDiff = new uint16_t[size];
20477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memset(fragmentationTimeDiff + oldVectorSize, 0,
20577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint16_t) * (size - oldVectorSize));
20677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationTimeDiff, oldTimeDiffs,
20777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint16_t) * oldVectorSize);
20877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        delete[] oldTimeDiffs;
20977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
21077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // payload type
21177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      {
21277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        uint8_t* oldTimePlTypes = fragmentationPlType;
21377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        fragmentationPlType = new uint8_t[size];
21477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memset(fragmentationPlType + oldVectorSize, 0,
21577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint8_t) * (size - oldVectorSize));
21677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        memcpy(fragmentationPlType, oldTimePlTypes,
21777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org               sizeof(uint8_t) * oldVectorSize);
21877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        delete[] oldTimePlTypes;
21977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      }
22077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      fragmentationVectorSize = size;
221b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
22277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
223b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
22477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t fragmentationVectorSize;  // Number of fragmentations
22577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t* fragmentationOffset;    // Offset of pointer to data for each fragm.
22677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t* fragmentationLength;    // Data size for each fragmentation
22777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t* fragmentationTimeDiff;  // Timestamp difference relative "now" for
22877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                    // each fragmentation
22977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t* fragmentationPlType;     // Payload type of each fragmentation
230856edd527f7c49517d6d866b602defd743ca0bdbandrew@webrtc.org
23177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org private:
23277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  DISALLOW_COPY_AND_ASSIGN(RTPFragmentationHeader);
233b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
234b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
23577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgstruct RTCPVoIPMetric {
23677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  // RFC 3611 4.7
23777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t lossRate;
23877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t discardRate;
23977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t burstDensity;
24077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t gapDensity;
24177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t burstDuration;
24277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t gapDuration;
24377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t roundTripDelay;
24477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t endSystemDelay;
24577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t signalLevel;
24677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t noiseLevel;
24777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t RERL;
24877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t Gmin;
24977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t Rfactor;
25077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t extRfactor;
25177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t MOSLQ;
25277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t MOSCQ;
25377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t RXconfig;
25477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t JBnominal;
25577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t JBmax;
25677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint16_t JBabsMax;
257b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
258b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
259b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Types for the FEC packet masks. The type |kFecMaskRandom| is based on a
260b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// random loss model. The type |kFecMaskBursty| is based on a bursty/consecutive
261b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// loss model. The packet masks are defined in
262b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// modules/rtp_rtcp/fec_private_tables_random(bursty).h
263b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgenum FecMaskType {
264b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  kFecMaskRandom,
265b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  kFecMaskBursty,
266b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
267b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
268b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// Struct containing forward error correction settings.
269b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgstruct FecProtectionParams {
270b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  int fec_rate;
271b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  bool use_uep_protection;
272b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  int max_fec_frames;
273b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  FecMaskType fec_mask_type;
274b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
275b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
2762a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org// Interface used by the CallStats class to distribute call statistics.
2772a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org// Callbacks will be triggered as soon as the class has been registered to a
2782a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org// CallStats object using RegisterStatsObserver.
2790329e591cd17957836fd295ae9b31f7d3a73e5e8fischman@webrtc.orgclass CallStatsObserver {
2802a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org public:
2812a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org  virtual void OnRttUpdate(uint32_t rtt_ms) = 0;
2822a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org
2830329e591cd17957836fd295ae9b31f7d3a73e5e8fischman@webrtc.org  virtual ~CallStatsObserver() {}
2842a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org};
2852a5dbce193b4ee594f0bb66c4f1a2bb24f31e621stefan@webrtc.org
286b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// class describing a complete, or parts of an encoded frame.
28777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgclass EncodedVideoData {
28877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org public:
28977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  EncodedVideoData()
29077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      : payloadType(0),
291b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        timeStamp(0),
292b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        renderTimeMs(0),
293b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        encodedWidth(0),
294b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        encodedHeight(0),
295b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        completeFrame(false),
296b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        missingFrame(false),
297b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        payloadData(NULL),
298b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        payloadSize(0),
299b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        bufferSize(0),
300b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        fragmentationHeader(),
301b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        frameType(kVideoFrameDelta),
30277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        codec(kVideoCodecUnknown) {};
30377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
30477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  EncodedVideoData(const EncodedVideoData& data) {
30577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    payloadType = data.payloadType;
30677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    timeStamp = data.timeStamp;
30777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    renderTimeMs = data.renderTimeMs;
30877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    encodedWidth = data.encodedWidth;
30977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    encodedHeight = data.encodedHeight;
31077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    completeFrame = data.completeFrame;
31177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    missingFrame = data.missingFrame;
31277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    payloadSize = data.payloadSize;
31377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    fragmentationHeader.CopyFrom(data.fragmentationHeader);
31477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    frameType = data.frameType;
31577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    codec = data.codec;
31677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (data.payloadSize > 0) {
31777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      payloadData = new uint8_t[data.payloadSize];
31877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      memcpy(payloadData, data.payloadData, data.payloadSize);
31977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    } else {
32077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      payloadData = NULL;
321b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
32277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
323b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
32477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  ~EncodedVideoData() {
32577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    delete[] payloadData;
32677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  };
327b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
32877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  EncodedVideoData& operator=(const EncodedVideoData& data) {
32977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (this == &data) {
33077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      return *this;
33177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    }
33277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    payloadType = data.payloadType;
33377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    timeStamp = data.timeStamp;
33477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    renderTimeMs = data.renderTimeMs;
33577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    encodedWidth = data.encodedWidth;
33677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    encodedHeight = data.encodedHeight;
33777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    completeFrame = data.completeFrame;
33877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    missingFrame = data.missingFrame;
33977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    payloadSize = data.payloadSize;
34077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    fragmentationHeader.CopyFrom(data.fragmentationHeader);
34177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    frameType = data.frameType;
34277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    codec = data.codec;
34377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (data.payloadSize > 0) {
34477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      delete[] payloadData;
34577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      payloadData = new uint8_t[data.payloadSize];
34677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      memcpy(payloadData, data.payloadData, data.payloadSize);
34777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      bufferSize = data.payloadSize;
34877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    }
34977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    return *this;
35077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  };
35177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void VerifyAndAllocate(const uint32_t size) {
35277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (bufferSize < size) {
35377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      uint8_t* oldPayload = payloadData;
35477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      payloadData = new uint8_t[size];
35577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      memcpy(payloadData, oldPayload, sizeof(uint8_t) * payloadSize);
35677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
35777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      bufferSize = size;
35877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      delete[] oldPayload;
359b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
36077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
361b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
36277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t payloadType;
36377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t timeStamp;
36477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int64_t renderTimeMs;
36577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t encodedWidth;
36677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t encodedHeight;
36777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  bool completeFrame;
36877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  bool missingFrame;
36977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t* payloadData;
37077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t payloadSize;
37177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t bufferSize;
37277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  RTPFragmentationHeader fragmentationHeader;
37377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  FrameType frameType;
37477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  VideoCodecType codec;
375b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
376b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
377b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.orgstruct VideoContentMetrics {
378b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  VideoContentMetrics()
379b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      : motion_magnitude(0.0f),
380b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        spatial_pred_err(0.0f),
381b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org        spatial_pred_err_h(0.0f),
38277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        spatial_pred_err_v(0.0f) {}
383b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
384b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  void Reset() {
385b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    motion_magnitude = 0.0f;
386b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    spatial_pred_err = 0.0f;
387b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    spatial_pred_err_h = 0.0f;
388b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    spatial_pred_err_v = 0.0f;
389b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  }
390b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  float motion_magnitude;
391b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  float spatial_pred_err;
392b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  float spatial_pred_err_h;
393b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org  float spatial_pred_err_v;
394b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
395b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
396b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/*************************************************
397b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
398b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * VideoFrame class
399b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
400b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * The VideoFrame class allows storing and
401b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * handling of video frames.
402b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
403b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
404b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *************************************************/
40577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orgclass VideoFrame {
40677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org public:
40777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  VideoFrame();
40877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  ~VideoFrame();
40977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
41077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * Verifies that current allocated buffer size is larger than or equal to the
41177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * input size.
41277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * If the current buffer size is smaller, a new allocation is made and the old
41377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * buffer data
41477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * is copied to the new buffer.
41577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * Buffer size is updated to minimumSize.
41677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
41777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int32_t VerifyAndAllocate(const uint32_t minimumSize);
41877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
41977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    Update length of data buffer in frame. Function verifies that new length
42077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * is less or
42177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    equal to allocated size.
42277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
42377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int32_t SetLength(const uint32_t newLength);
42477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /*
42577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    Swap buffer and size data
42677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
42777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int32_t Swap(uint8_t*& newMemory, uint32_t& newLength, uint32_t& newSize);
42877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /*
42977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    Swap buffer and size data
43077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
43177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int32_t SwapFrame(VideoFrame& videoFrame);
43277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
43377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    Copy buffer: If newLength is bigger than allocated size, a new buffer of
43477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * size length
43577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    is allocated.
43677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
43777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int32_t CopyFrame(const VideoFrame& videoFrame);
43877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
43977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    Copy buffer: If newLength is bigger than allocated size, a new buffer of
44077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  * size length
44177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    is allocated.
44277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
44377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int32_t CopyFrame(uint32_t length, const uint8_t* sourceBuffer);
44477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
44577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *    Delete VideoFrame and resets members to zero
44677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
44777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void Free();
44877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
44977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Set frame timestamp (90kHz)
45077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
45177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void SetTimeStamp(const uint32_t timeStamp) { _timeStamp = timeStamp; }
45277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
45377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Get pointer to frame buffer
45477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
45577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t* Buffer() const { return _buffer; }
45677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
45777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t*& Buffer() { return _buffer; }
45877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
45977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
46077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Get allocated buffer size
46177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
46277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t Size() const { return _bufferSize; }
46377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
46477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Get frame length
46577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
46677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t Length() const { return _bufferLength; }
46777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
46877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Get frame timestamp (90kHz)
46977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
47077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t TimeStamp() const { return _timeStamp; }
47177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
47277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Get frame width
47377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
47477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t Width() const { return _width; }
47577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
47677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Get frame height
47777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
47877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t Height() const { return _height; }
47977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
48077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Set frame width
48177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
48277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void SetWidth(const uint32_t width) { _width = width; }
48377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
48477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Set frame height
48577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
48677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void SetHeight(const uint32_t height) { _height = height; }
48777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
48877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *   Set render time in miliseconds
48977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
49077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void SetRenderTime(const int64_t renderTimeMs) {
49177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    _renderTimeMs = renderTimeMs;
49277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
49377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  /**
49477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  *  Get render time in miliseconds
49577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  */
49677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int64_t RenderTimeMs() const { return _renderTimeMs; }
49777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
49877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org private:
49977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void Set(uint8_t* buffer, uint32_t size, uint32_t length, uint32_t timeStamp);
50077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
50177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t* _buffer;        // Pointer to frame buffer
50277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t _bufferSize;    // Allocated buffer size
50377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t _bufferLength;  // Length (in bytes) of buffer
50477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t _timeStamp;     // Timestamp of frame (90kHz)
50577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t _width;
50677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t _height;
50777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int64_t _renderTimeMs;
50877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org};  // end of VideoFrame class declaration
509b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
510b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org// inline implementation of VideoFrame class:
51177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline VideoFrame::VideoFrame()
51277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    : _buffer(0),
51377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      _bufferSize(0),
51477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      _bufferLength(0),
51577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      _timeStamp(0),
51677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      _width(0),
51777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      _height(0),
51877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      _renderTimeMs(0) {
51977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  //
520b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
52177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline VideoFrame::~VideoFrame() {
52277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (_buffer) {
52377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    delete[] _buffer;
52477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    _buffer = NULL;
52577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
526b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
527b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
52877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline int32_t VideoFrame::VerifyAndAllocate(const uint32_t minimumSize) {
52977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (minimumSize < 1) {
53077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    return -1;
53177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
53277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (minimumSize > _bufferSize) {
53377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    // create buffer of sufficient size
53477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    uint8_t* newBufferBuffer = new uint8_t[minimumSize];
53577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (_buffer) {
53677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      // copy old data
53777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      memcpy(newBufferBuffer, _buffer, _bufferSize);
53877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      delete[] _buffer;
53977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    } else {
54077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      memset(newBufferBuffer, 0, minimumSize * sizeof(uint8_t));
541b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
54277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    _buffer = newBufferBuffer;
54377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    _bufferSize = minimumSize;
54477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
54577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return 0;
546b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
547b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
54877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline int32_t VideoFrame::SetLength(const uint32_t newLength) {
54977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (newLength > _bufferSize) {  // can't accomodate new value
55077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    return -1;
55177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
55277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _bufferLength = newLength;
55377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return 0;
554b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
555b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
55677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline int32_t VideoFrame::SwapFrame(VideoFrame& videoFrame) {
55777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t tmpTimeStamp = _timeStamp;
55877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t tmpWidth = _width;
55977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t tmpHeight = _height;
56077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  int64_t tmpRenderTime = _renderTimeMs;
56177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
56277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _timeStamp = videoFrame._timeStamp;
56377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _width = videoFrame._width;
56477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _height = videoFrame._height;
56577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _renderTimeMs = videoFrame._renderTimeMs;
566b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
56777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  videoFrame._timeStamp = tmpTimeStamp;
56877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  videoFrame._width = tmpWidth;
56977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  videoFrame._height = tmpHeight;
57077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  videoFrame._renderTimeMs = tmpRenderTime;
57177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
57277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return Swap(videoFrame._buffer, videoFrame._bufferLength,
57377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org              videoFrame._bufferSize);
574b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
575b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
57677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline int32_t VideoFrame::Swap(uint8_t*& newMemory, uint32_t& newLength,
57777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                uint32_t& newSize) {
57877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint8_t* tmpBuffer = _buffer;
57977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t tmpLength = _bufferLength;
58077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  uint32_t tmpSize = _bufferSize;
58177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _buffer = newMemory;
58277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _bufferLength = newLength;
58377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _bufferSize = newSize;
58477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  newMemory = tmpBuffer;
58577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  newLength = tmpLength;
58677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  newSize = tmpSize;
58777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return 0;
588b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
589b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
59077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline int32_t VideoFrame::CopyFrame(uint32_t length,
59177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                     const uint8_t* sourceBuffer) {
59277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (length > _bufferSize) {
59377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    int32_t ret = VerifyAndAllocate(length);
59477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (ret < 0) {
59577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      return ret;
596b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
59777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
59877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  memcpy(_buffer, sourceBuffer, length);
59977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _bufferLength = length;
60077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return 0;
601b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
602b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
60377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline int32_t VideoFrame::CopyFrame(const VideoFrame& videoFrame) {
60477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (CopyFrame(videoFrame.Length(), videoFrame.Buffer()) != 0) {
60577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    return -1;
60677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
60777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _timeStamp = videoFrame._timeStamp;
60877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _width = videoFrame._width;
60977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _height = videoFrame._height;
61077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _renderTimeMs = videoFrame._renderTimeMs;
61177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return 0;
612b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
613b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
61477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline void VideoFrame::Free() {
61577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _timeStamp = 0;
61677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _bufferLength = 0;
61777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _bufferSize = 0;
61877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _height = 0;
61977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _width = 0;
62077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  _renderTimeMs = 0;
62177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org
62277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (_buffer) {
62377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    delete[] _buffer;
62477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    _buffer = NULL;
62577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  }
62677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org}
627b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
628b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org/* This class holds up to 60 ms of super-wideband (32 kHz) stereo audio. It
629b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * allows for adding and subtracting frames while keeping track of the resulting
630b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * states.
631b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
632b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * Notes
633b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * - The total number of samples in |data_| is
634b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *   samples_per_channel_ * num_channels_
635b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
636b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * - Stereo data is interleaved starting with the left channel.
637b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *
638b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org * - The +operator assume that you would never add exactly opposite frames when
639b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org *   deciding the resulting state. To do this use the -operator.
640b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org */
6412ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.orgclass AudioFrame {
64277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org public:
6432ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  // Stereo, 32 kHz, 60 ms (2 * 32 * 60)
6442ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  static const int kMaxDataSizeSamples = 3840;
6452ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
6462ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  enum VADActivity {
64777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    kVadActive = 0,
6482ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    kVadPassive = 1,
6492ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    kVadUnknown = 2
6502ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  };
6512ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  enum SpeechType {
6522ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    kNormalSpeech = 0,
65377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    kPLC = 1,
65477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    kCNG = 2,
65577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    kPLCCNG = 3,
65677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    kUndefined = 4
6572ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  };
6582ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
6592ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  AudioFrame();
6602ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  virtual ~AudioFrame() {}
6612ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
66235af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  // Resets all members to their default state (except does not modify the
66335af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  // contents of |data_|).
66435af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  void Reset();
66535af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org
66677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  // |interleaved_| is not changed by this method.
66777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  void UpdateFrame(int id, uint32_t timestamp, const int16_t* data,
66877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                   int samples_per_channel, int sample_rate_hz,
66977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                   SpeechType speech_type, VADActivity vad_activity,
67077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                   int num_channels = 1, uint32_t energy = -1);
6712ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
6722ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  AudioFrame& Append(const AudioFrame& rhs);
6732ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
6742ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  void CopyFrom(const AudioFrame& src);
6752ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
6762ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  void Mute();
6772ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
6782ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  AudioFrame& operator>>=(const int rhs);
6792ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  AudioFrame& operator+=(const AudioFrame& rhs);
6802ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  AudioFrame& operator-=(const AudioFrame& rhs);
6812ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
6822ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  int id_;
68322f69bd27abc89979460df6d01de8685cb058aabwu@webrtc.org  // RTP timestamp of the first sample in the AudioFrame.
6842ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  uint32_t timestamp_;
68581f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  // Time since the first frame in milliseconds.
68681f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  // -1 represents an uninitialized value.
68781f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  int64_t elapsed_time_ms_;
68822f69bd27abc89979460df6d01de8685cb058aabwu@webrtc.org  // NTP time of the estimated capture time in local timebase in milliseconds.
68935af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  // -1 represents an uninitialized value.
69022f69bd27abc89979460df6d01de8685cb058aabwu@webrtc.org  int64_t ntp_time_ms_;
6912ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  int16_t data_[kMaxDataSizeSamples];
6922ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  int samples_per_channel_;
6932ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  int sample_rate_hz_;
6942ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  int num_channels_;
6952ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  SpeechType speech_type_;
6962ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  VADActivity vad_activity_;
697ad230ee4f632fcca598796451977810b97be4c5fhenrik.lundin@webrtc.org  // Note that there is no guarantee that |energy_| is correct. Any user of this
698ad230ee4f632fcca598796451977810b97be4c5fhenrik.lundin@webrtc.org  // member must verify that the value is correct.
699ad230ee4f632fcca598796451977810b97be4c5fhenrik.lundin@webrtc.org  // TODO(henrike) Remove |energy_|.
700ad230ee4f632fcca598796451977810b97be4c5fhenrik.lundin@webrtc.org  // See https://code.google.com/p/webrtc/issues/detail?id=3315.
7012ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  uint32_t energy_;
7022ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  bool interleaved_;
703d4682361fd4d284657e4e33ca5f022cc8ea8f468andrew@webrtc.org
70477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org private:
7052ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  DISALLOW_COPY_AND_ASSIGN(AudioFrame);
706b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org};
707b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
70877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline AudioFrame::AudioFrame()
70935af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org    : data_() {
71035af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  Reset();
71135af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org}
71235af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org
71335af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.orginline void AudioFrame::Reset() {
71435af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  id_ = -1;
71535af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  // TODO(wu): Zero is a valid value for |timestamp_|. We should initialize
71635af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  // to an invalid value, or add a new member to indicate invalidity.
71735af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  timestamp_ = 0;
71881f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  elapsed_time_ms_ = -1;
71935af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  ntp_time_ms_ = -1;
72035af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  samples_per_channel_ = 0;
72135af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  sample_rate_hz_ = 0;
72235af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  num_channels_ = 0;
72335af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  speech_type_ = kUndefined;
72435af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  vad_activity_ = kVadUnknown;
72535af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  energy_ = 0xffffffff;
72635af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org  interleaved_ = true;
72735af59e037fdeb66087e0c36cf70e432b371d962andrew@webrtc.org}
728b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
72977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline void AudioFrame::UpdateFrame(int id, uint32_t timestamp,
73077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                    const int16_t* data,
73177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                    int samples_per_channel, int sample_rate_hz,
73277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                    SpeechType speech_type,
73377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                    VADActivity vad_activity, int num_channels,
73477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org                                    uint32_t energy) {
7352ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  id_ = id;
7362ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  timestamp_ = timestamp;
7372ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  samples_per_channel_ = samples_per_channel;
7382ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  sample_rate_hz_ = sample_rate_hz;
7392ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  speech_type_ = speech_type;
7402ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  vad_activity_ = vad_activity;
7412ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  num_channels_ = num_channels;
7422ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  energy_ = energy;
7432ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
7442ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  const int length = samples_per_channel * num_channels;
7452ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert(length <= kMaxDataSizeSamples && length >= 0);
74677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (data != NULL) {
7472ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    memcpy(data_, data, sizeof(int16_t) * length);
7482ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  } else {
7492ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    memset(data_, 0, sizeof(int16_t) * length);
7502ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
751b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
752b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
7532ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.orginline void AudioFrame::CopyFrom(const AudioFrame& src) {
75477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (this == &src) return;
7552ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
7562ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  id_ = src.id_;
7572ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  timestamp_ = src.timestamp_;
75881f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  elapsed_time_ms_ = src.elapsed_time_ms_;
75981f8df9af96c6b4bf43234f2a0162146a5da6112wu@webrtc.org  ntp_time_ms_ = src.ntp_time_ms_;
7602ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  samples_per_channel_ = src.samples_per_channel_;
7612ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  sample_rate_hz_ = src.sample_rate_hz_;
7622ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  speech_type_ = src.speech_type_;
7632ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  vad_activity_ = src.vad_activity_;
7642ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  num_channels_ = src.num_channels_;
7652ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  energy_ = src.energy_;
7662ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  interleaved_ = src.interleaved_;
7672ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
7682ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  const int length = samples_per_channel_ * num_channels_;
7692ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert(length <= kMaxDataSizeSamples && length >= 0);
7702ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  memcpy(data_, src.data_, sizeof(int16_t) * length);
771b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
772b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
77377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline void AudioFrame::Mute() {
774d4682361fd4d284657e4e33ca5f022cc8ea8f468andrew@webrtc.org  memset(data_, 0, samples_per_channel_ * num_channels_ * sizeof(int16_t));
775b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
776b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
77777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline AudioFrame& AudioFrame::operator>>=(const int rhs) {
7782ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert((num_channels_ > 0) && (num_channels_ < 3));
77977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((num_channels_ > 2) || (num_channels_ < 1)) return *this;
7802ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
78177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  for (int i = 0; i < samples_per_channel_ * num_channels_; i++) {
7822ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    data_[i] = static_cast<int16_t>(data_[i] >> rhs);
7832ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
7842ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  return *this;
785b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
786b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
78777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline AudioFrame& AudioFrame::Append(const AudioFrame& rhs) {
7882ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  // Sanity check
7892ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert((num_channels_ > 0) && (num_channels_ < 3));
7902ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert(interleaved_ == rhs.interleaved_);
79177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((num_channels_ > 2) || (num_channels_ < 1)) return *this;
79277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (num_channels_ != rhs.num_channels_) return *this;
7932ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
79477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((vad_activity_ == kVadActive) || rhs.vad_activity_ == kVadActive) {
7952ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    vad_activity_ = kVadActive;
79677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  } else if (vad_activity_ == kVadUnknown || rhs.vad_activity_ == kVadUnknown) {
7972ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    vad_activity_ = kVadUnknown;
7982ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
79977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (speech_type_ != rhs.speech_type_) {
8002ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    speech_type_ = kUndefined;
8012ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
8022ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
8032ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  int offset = samples_per_channel_ * num_channels_;
80477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  for (int i = 0; i < rhs.samples_per_channel_ * rhs.num_channels_; i++) {
80577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    data_[offset + i] = rhs.data_[i];
8062ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
8072ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  samples_per_channel_ += rhs.samples_per_channel_;
8082ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  return *this;
809b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
810b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
81177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline AudioFrame& AudioFrame::operator+=(const AudioFrame& rhs) {
8122ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  // Sanity check
8132ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert((num_channels_ > 0) && (num_channels_ < 3));
8142ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert(interleaved_ == rhs.interleaved_);
81577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((num_channels_ > 2) || (num_channels_ < 1)) return *this;
81677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (num_channels_ != rhs.num_channels_) return *this;
817b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
8182ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  bool noPrevData = false;
81977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (samples_per_channel_ != rhs.samples_per_channel_) {
82077035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (samples_per_channel_ == 0) {
8212ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      // special case we have no data to start with
8222ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      samples_per_channel_ = rhs.samples_per_channel_;
8232ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      noPrevData = true;
8242ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    } else {
8252ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      return *this;
826b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
8272ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
828b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
82977035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((vad_activity_ == kVadActive) || rhs.vad_activity_ == kVadActive) {
8302ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    vad_activity_ = kVadActive;
83177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  } else if (vad_activity_ == kVadUnknown || rhs.vad_activity_ == kVadUnknown) {
8322ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    vad_activity_ = kVadUnknown;
8332ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
834b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
83577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (speech_type_ != rhs.speech_type_) speech_type_ = kUndefined;
8362ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
83777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if (noPrevData) {
8382ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    memcpy(data_, rhs.data_,
8392ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org           sizeof(int16_t) * rhs.samples_per_channel_ * num_channels_);
8402ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  } else {
8412ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    // IMPROVEMENT this can be done very fast in assembly
84277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    for (int i = 0; i < samples_per_channel_ * num_channels_; i++) {
84377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      int32_t wrapGuard =
84477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org          static_cast<int32_t>(data_[i]) + static_cast<int32_t>(rhs.data_[i]);
84577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      if (wrapGuard < -32768) {
8462ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org        data_[i] = -32768;
84777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org      } else if (wrapGuard > 32767) {
8482ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org        data_[i] = 32767;
8492ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      } else {
8502ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org        data_[i] = (int16_t)wrapGuard;
851b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org      }
852b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    }
8532ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
8542ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  energy_ = 0xffffffff;
8552ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  return *this;
856b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
857b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
85877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.orginline AudioFrame& AudioFrame::operator-=(const AudioFrame& rhs) {
8592ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  // Sanity check
8602ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert((num_channels_ > 0) && (num_channels_ < 3));
8612ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  assert(interleaved_ == rhs.interleaved_);
86277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((num_channels_ > 2) || (num_channels_ < 1)) return *this;
863b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
86477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((samples_per_channel_ != rhs.samples_per_channel_) ||
8652ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      (num_channels_ != rhs.num_channels_)) {
866b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org    return *this;
8672ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
86877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  if ((vad_activity_ != kVadPassive) || rhs.vad_activity_ != kVadPassive) {
8692ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    vad_activity_ = kVadUnknown;
8702ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
8712ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  speech_type_ = kUndefined;
8722ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org
87377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  for (int i = 0; i < samples_per_channel_ * num_channels_; i++) {
87477035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    int32_t wrapGuard =
87577035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org        static_cast<int32_t>(data_[i]) - static_cast<int32_t>(rhs.data_[i]);
87677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    if (wrapGuard < -32768) {
8772ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      data_[i] = -32768;
87877035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org    } else if (wrapGuard > 32767) {
8792ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      data_[i] = 32767;
8802ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    } else {
8812ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org      data_[i] = (int16_t)wrapGuard;
8822ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org    }
8832ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  }
8842ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  energy_ = 0xffffffff;
8852ea364539729b8dde2983c8dd0d8c28fbe8c1a2cxians@webrtc.org  return *this;
886b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org}
887b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
88842c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.orginline bool IsNewerSequenceNumber(uint16_t sequence_number,
88942c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org                                  uint16_t prev_sequence_number) {
89042c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org  return sequence_number != prev_sequence_number &&
89177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org         static_cast<uint16_t>(sequence_number - prev_sequence_number) < 0x8000;
89242c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org}
89342c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org
89442c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.orginline bool IsNewerTimestamp(uint32_t timestamp, uint32_t prev_timestamp) {
89542c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org  return timestamp != prev_timestamp &&
89677035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org         static_cast<uint32_t>(timestamp - prev_timestamp) < 0x80000000;
89742c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org}
89842c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org
89942c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.orginline uint16_t LatestSequenceNumber(uint16_t sequence_number1,
90042c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org                                     uint16_t sequence_number2) {
90177035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return IsNewerSequenceNumber(sequence_number1, sequence_number2)
90277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org             ? sequence_number1
90377035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org             : sequence_number2;
90442c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org}
90542c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org
90642c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.orginline uint32_t LatestTimestamp(uint32_t timestamp1, uint32_t timestamp2) {
90777035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org  return IsNewerTimestamp(timestamp1, timestamp2) ? timestamp1 : timestamp2;
90842c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org}
90942c7409229636a59e28814451bd49ada2c6803a1stefan@webrtc.org
9103b89e10f31160da35b408fd00cb8f89d2b08862dpbos@webrtc.org}  // namespace webrtc
911b015cbede88899f67a53fbbe581b02ce8e32794andrew@webrtc.org
91277035fd2addd882e450ce9ee91f290152d4c8228xians@webrtc.org#endif  // MODULE_COMMON_TYPES_H
913