Miscellaneous fixups for VT contribution update.
fixed misc. spacing and style issues
AAVCAssembler/AHEVCAssembler: fix OOB reads in checkIFrameProvided,
checkSpsUpdated and dropFramesUntilIFrame
ARTPWriter: fix OOB read in SpsPpsParser, VpsSpsPpsParser and sendVPSSPSPPSIfIFrame
ARTPWriter: use KMP algorithm for VpsSpsPpsParser
ARTPSource/ARTPConnection/RTPSource: rename jbTIme to jbTimeMs
Bug: 165061754
Change-Id: Iceeb3851aed0ba9b1c2c50b1a2378df06faa29d6
diff --git a/media/libmediaplayerservice/StagefrightRecorder.cpp b/media/libmediaplayerservice/StagefrightRecorder.cpp
index 97db25d..02ae456 100644
--- a/media/libmediaplayerservice/StagefrightRecorder.cpp
+++ b/media/libmediaplayerservice/StagefrightRecorder.cpp
@@ -17,6 +17,9 @@
//#define LOG_NDEBUG 0
#define LOG_TAG "StagefrightRecorder"
#include <inttypes.h>
+// TODO/workaround: including base logging now as it conflicts with ADebug.h
+// and it must be included first.
+#include <android-base/logging.h>
#include <utils/Log.h>
#include "WebmWriter.h"
@@ -582,7 +585,7 @@
const float coefficient = 0.8f;
mVideoBitRate = (bitRate * coefficient) / 1000 * 1000;
}
- if (mOutputFormat == OUTPUT_FORMAT_RTP_AVP && mStarted && mPauseStartTimeUs == 0) {
+ if (mOutputFormat == OUTPUT_FORMAT_RTP_AVP && mStarted && mPauseStartTimeUs == 0) {
mVideoEncoderSource->setEncodingBitrate(mVideoBitRate);
ARTPWriter* rtpWriter = static_cast<ARTPWriter*>(mWriter.get());
rtpWriter->setTMMBNInfo(mOpponentID, bitRate);
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
index 738e42a..47362ef 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.cpp
@@ -1703,8 +1703,9 @@
}
void NuPlayer::setTargetBitrate(int bitrate) {
- if (mSource != NULL)
+ if (mSource != NULL) {
mSource->setTargetBitrate(bitrate);
+ }
}
void NuPlayer::onPause() {
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayer.h b/media/libmediaplayerservice/nuplayer/NuPlayer.h
index 2f83224..adb7075 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayer.h
+++ b/media/libmediaplayerservice/nuplayer/NuPlayer.h
@@ -102,7 +102,7 @@
void updateInternalTimers();
- void setTargetBitrate(int bitrate);
+ void setTargetBitrate(int bitrate /* bps */);
protected:
virtual ~NuPlayer();
diff --git a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
index 857e095..2a50fc2 100644
--- a/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
+++ b/media/libmediaplayerservice/nuplayer/NuPlayerDriver.cpp
@@ -817,7 +817,7 @@
}
status_t NuPlayerDriver::setParameter(
- int key, const Parcel& request ) {
+ int key, const Parcel &request ) {
if (key == KEY_PARAMETER_RTP_ATTRIBUTES) {
mPlayer->setTargetBitrate(request.readInt32());
return OK;
diff --git a/media/libmediaplayerservice/nuplayer/RTPSource.cpp b/media/libmediaplayerservice/nuplayer/RTPSource.cpp
index 5388bfd..b1901e8 100644
--- a/media/libmediaplayerservice/nuplayer/RTPSource.cpp
+++ b/media/libmediaplayerservice/nuplayer/RTPSource.cpp
@@ -114,7 +114,8 @@
// index(i) should be started from 1. 0 is reserved for [root]
mRTPConn->addStream(sockRtp, sockRtcp, desc, i + 1, notify, false);
mRTPConn->setSelfID(info->mSelfID);
- mRTPConn->setJbTime((info->mJbTime <= 3000 && info->mJbTime >= 40) ? info->mJbTime : 300);
+ mRTPConn->setJbTime(
+ (info->mJbTimeMs <= 3000 && info->mJbTimeMs >= 40) ? info->mJbTimeMs : 300);
info->mRTPSocket = sockRtp;
info->mRTCPSocket = sockRtcp;
@@ -669,7 +670,7 @@
newTrackInfo.mIsAudio = isAudioKey;
mTracks.push(newTrackInfo);
info = &mTracks.editTop();
- info->mJbTime = 300;
+ info->mJbTimeMs = 300;
}
if (key == "rtp-param-mime-type") {
@@ -713,7 +714,7 @@
int64_t networkHandle = atoll(value);
setSocketNetwork(networkHandle);
} else if (key == "rtp-param-jitter-buffer-time") {
- info->mJbTime = atoi(value);
+ info->mJbTimeMs = atoi(value);
}
return OK;
diff --git a/media/libmediaplayerservice/nuplayer/RTPSource.h b/media/libmediaplayerservice/nuplayer/RTPSource.h
index 5d70142..fb2d3b9 100644
--- a/media/libmediaplayerservice/nuplayer/RTPSource.h
+++ b/media/libmediaplayerservice/nuplayer/RTPSource.h
@@ -129,8 +129,8 @@
int32_t mTimeScale;
int32_t mAS;
- /* RTP jitter buffer time in millsecond */
- uint32_t mJbTime;
+ /* RTP jitter buffer time in milliseconds */
+ uint32_t mJbTimeMs;
/* Unique ID indicates itself */
uint32_t mSelfID;
/* extmap:<value> for CVO will be set to here */
diff --git a/media/libstagefright/include/HevcUtils.h b/media/libstagefright/include/HevcUtils.h
index 8955bc1..6a4a168 100644
--- a/media/libstagefright/include/HevcUtils.h
+++ b/media/libstagefright/include/HevcUtils.h
@@ -94,8 +94,8 @@
// Note that this method does not write the start code.
bool write(size_t index, uint8_t* dest, size_t size);
status_t makeHvcc(uint8_t *hvcc, size_t *hvccSize, size_t nalSizeLength);
- void FindHEVCDimensions(const sp<ABuffer> &SpsBuffer, int32_t *width,
- int32_t *height);
+ void FindHEVCDimensions(
+ const sp<ABuffer> &SpsBuffer, int32_t *width, int32_t *height);
Info getInfo() const { return mInfo; }
static bool IsHevcIDR(const uint8_t *data, size_t size);
diff --git a/media/libstagefright/rtsp/AAVCAssembler.cpp b/media/libstagefright/rtsp/AAVCAssembler.cpp
index efbde54..a0b66a7 100644
--- a/media/libstagefright/rtsp/AAVCAssembler.cpp
+++ b/media/libstagefright/rtsp/AAVCAssembler.cpp
@@ -39,7 +39,7 @@
mNextExpectedSeqNo(0),
mAccessUnitDamaged(false),
mFirstIFrameProvided(false),
- mLastIFrameProvidedAt(0) {
+ mLastIFrameProvidedAtMs(0) {
}
AAVCAssembler::~AAVCAssembler() {
@@ -52,17 +52,18 @@
List<sp<ABuffer> >::iterator it = queue->begin();
- uint16_t queueHeadSeqNum;
- if (it != queue->end())
- queueHeadSeqNum = (*it)->int32Data();
+ if (it == queue->end()) {
+ return nackCount /* 0 */;
+ }
- // move to the packet after that RTCP:NACK sent.
- while (it != queue->end()) {
+ uint16_t queueHeadSeqNum = (*it)->int32Data();
+
+ // move to the packet after which RTCP:NACK was sent.
+ for (; it != queue->end(); ++it) {
int32_t seqNum = (*it)->int32Data();
- if (seqNum < source->mHighestNackNumber)
- it++;
- else
+ if (seqNum >= source->mHighestNackNumber) {
break;
+ }
}
int32_t nackStartAt = -1;
@@ -70,20 +71,21 @@
while (it != queue->end()) {
int32_t seqBeforeLast = (*it)->int32Data();
// increase iterator.
- if ((++it) == queue->end())
+ if ((++it) == queue->end()) {
break;
+ }
int32_t seqLast = (*it)->int32Data();
if ((seqLast - seqBeforeLast) < 0) {
- ALOGD("addNack : found end of seqNum from(%d) to(%d)", seqBeforeLast, seqLast);
+ ALOGD("addNack: found end of seqNum from(%d) to(%d)", seqBeforeLast, seqLast);
source->mHighestNackNumber = 0;
}
// missed packet found
if (seqLast > (seqBeforeLast + 1) &&
- // we didn't send RTCP:NACK for this packet yet.
- (seqLast - 1) > source->mHighestNackNumber) {
- source->mHighestNackNumber = seqLast -1;
+ // we didn't send RTCP:NACK for this packet yet.
+ (seqLast - 1) > source->mHighestNackNumber) {
+ source->mHighestNackNumber = seqLast - 1;
nackStartAt = seqBeforeLast + 1;
break;
}
@@ -92,10 +94,10 @@
if (nackStartAt != -1) {
nackCount = source->mHighestNackNumber - nackStartAt + 1;
- ALOGD("addNack : nackCount=%d, nackFrom=%d, nackTo=%d", nackCount,
- nackStartAt, source->mHighestNackNumber);
+ ALOGD("addNack: nackCount=%d, nackFrom=%d, nackTo=%d", nackCount,
+ nackStartAt, source->mHighestNackNumber);
- uint16_t mask = (uint16_t)(0xffff) >> (16-nackCount+1);
+ uint16_t mask = (uint16_t)(0xffff) >> (16 - nackCount + 1);
source->setSeqNumToNACK(nackStartAt, mask, queueHeadSeqNum);
}
@@ -116,9 +118,10 @@
int64_t startTime = source->mFirstSysTime / 1000;
int64_t nowTime = ALooper::GetNowUs() / 1000;
int64_t playedTime = nowTime - startTime;
- int64_t playedTimeRtp = source->mFirstRtpTime +
- (((uint32_t)playedTime) * (source->mClockRate / 1000));
- const uint32_t jitterTime = (uint32_t)(source->mClockRate / ((float)1000 / (source->mJbTime)));
+ int64_t playedTimeRtp =
+ source->mFirstRtpTime + (((uint32_t)playedTime) * (source->mClockRate / 1000));
+ const uint32_t jitterTime =
+ (uint32_t)(source->mClockRate / ((float)1000 / (source->mJbTimeMs)));
uint32_t expiredTimeInJb = rtpTime + jitterTime;
bool isExpired = expiredTimeInJb <= (playedTimeRtp);
bool isTooLate200 = expiredTimeInJb < (playedTimeRtp - jitterTime);
@@ -138,8 +141,9 @@
return NOT_ENOUGH_DATA;
}
- if (isTooLate200)
+ if (isTooLate200) {
ALOGW("=== WARNING === buffer arrived 200ms late. === WARNING === ");
+ }
if (isTooLate300) {
ALOGW("buffer arrived after 300ms ... \t Diff in Jb=%lld \t Seq# %d",
@@ -221,15 +225,18 @@
}
void AAVCAssembler::checkIFrameProvided(const sp<ABuffer> &buffer) {
+ if (buffer->size() == 0) {
+ return;
+ }
const uint8_t *data = buffer->data();
unsigned nalType = data[0] & 0x1f;
if (nalType == 0x5) {
mFirstIFrameProvided = true;
- mLastIFrameProvidedAt = ALooper::GetNowUs() / 1000;
+ mLastIFrameProvidedAtMs = ALooper::GetNowUs() / 1000;
uint32_t rtpTime;
CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
- ALOGD("got First I-frame to be decoded. rtpTime=%d, size=%zu", rtpTime, buffer->size());
+ ALOGD("got First I-frame to be decoded. rtpTime=%u, size=%zu", rtpTime, buffer->size());
}
}
@@ -352,16 +359,18 @@
size_t size = buffer->size();
if ((uint32_t)buffer->int32Data() != expectedSeqNo) {
- ALOGV("sequence not complete, expected seqNo %d, got %d, nalType %d",
- expectedSeqNo, (uint32_t)buffer->int32Data(), nalType);
+ ALOGD("sequence not complete, expected seqNo %u, got %u, nalType %u",
+ expectedSeqNo, (unsigned)buffer->int32Data(), nalType);
snapped = true;
- if (!pFrame)
+ if (!pFrame) {
return WRONG_SEQUENCE_NUMBER;
+ }
}
- if (!snapped)
+ if (!snapped) {
connected++;
+ }
uint32_t rtpTime;
CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
@@ -383,7 +392,7 @@
totalSize += size - 2;
++totalCount;
- expectedSeqNo = buffer->int32Data() + 1;
+ expectedSeqNo = (uint32_t)buffer->int32Data() + 1;
if (data[1] & 0x40) {
if (pFrame && !recycleUnit(startSeqNo, expectedSeqNo,
@@ -500,13 +509,13 @@
msg->post();
}
-int32_t AAVCAssembler::pickProperSeq(const Q *q, uint32_t jit, int64_t play) {
- sp<ABuffer> buffer = *(q->begin());
+int32_t AAVCAssembler::pickProperSeq(const Queue *queue, uint32_t jit, int64_t play) {
+ sp<ABuffer> buffer = *(queue->begin());
uint32_t rtpTime;
int32_t nextSeqNo = buffer->int32Data();
- Q::const_iterator it = q->begin();
- while (it != q->end()) {
+ Queue::const_iterator it = queue->begin();
+ while (it != queue->end()) {
CHECK((*it)->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
// if pkt in time exists, that should be the next pivot
if (rtpTime + jit >= play) {
@@ -531,17 +540,17 @@
return isRecycle;
}
-int32_t AAVCAssembler::deleteUnitUnderSeq(Q *q, uint32_t seq) {
- int32_t initSize = q->size();
- Q::iterator it = q->begin();
- while (it != q->end()) {
+int32_t AAVCAssembler::deleteUnitUnderSeq(Queue *queue, uint32_t seq) {
+ int32_t initSize = queue->size();
+ Queue::iterator it = queue->begin();
+ while (it != queue->end()) {
if ((uint32_t)(*it)->int32Data() >= seq) {
break;
}
it++;
}
- q->erase(q->begin(), it);
- return initSize - q->size();
+ queue->erase(queue->begin(), it);
+ return initSize - queue->size();
}
inline void AAVCAssembler::printNowTimeUs(int64_t start, int64_t now, int64_t play) {
@@ -558,10 +567,10 @@
const sp<ARTPSource> &source) {
AssemblyStatus status = addNALUnit(source);
if (status == MALFORMED_PACKET) {
- uint64_t timeAfterLastIFrame = (ALooper::GetNowUs() / 1000) - mLastIFrameProvidedAt;
- if (timeAfterLastIFrame > 1000) {
- ALOGV("request FIR to get a new I-Frame, time after "
- "last I-Frame in miils %llu", (unsigned long long)timeAfterLastIFrame);
+ uint64_t msecsSinceLastIFrame = (ALooper::GetNowUs() / 1000) - mLastIFrameProvidedAtMs;
+ if (msecsSinceLastIFrame > 1000) {
+ ALOGV("request FIR to get a new I-Frame, time since "
+ "last I-Frame %llu ms", (unsigned long long)msecsSinceLastIFrame);
source->onIssueFIRByAssembler();
}
}
@@ -570,7 +579,7 @@
void AAVCAssembler::packetLost() {
CHECK(mNextExpectedSeqNoValid);
- ALOGD("packetLost (expected %d)", mNextExpectedSeqNo);
+ ALOGD("packetLost (expected %u)", mNextExpectedSeqNo);
++mNextExpectedSeqNo;
}
diff --git a/media/libstagefright/rtsp/AAVCAssembler.h b/media/libstagefright/rtsp/AAVCAssembler.h
index ac2c9a0..913a868 100644
--- a/media/libstagefright/rtsp/AAVCAssembler.h
+++ b/media/libstagefright/rtsp/AAVCAssembler.h
@@ -25,14 +25,13 @@
namespace android {
-using Q = List<sp<ABuffer> >;
-
struct ABuffer;
struct AMessage;
struct AAVCAssembler : public ARTPAssembler {
explicit AAVCAssembler(const sp<AMessage> ¬ify);
+ typedef List<sp<ABuffer> > Queue;
protected:
virtual ~AAVCAssembler();
@@ -48,7 +47,7 @@
uint32_t mNextExpectedSeqNo;
bool mAccessUnitDamaged;
bool mFirstIFrameProvided;
- uint64_t mLastIFrameProvidedAt;
+ uint64_t mLastIFrameProvidedAtMs;
List<sp<ABuffer> > mNALUnits;
int32_t addNack(const sp<ARTPSource> &source);
@@ -60,10 +59,10 @@
void submitAccessUnit();
- int32_t pickProperSeq(const Q *q, uint32_t jit, int64_t play);
- bool recycleUnit(uint32_t start, uint32_t end, uint32_t conneceted,
+ int32_t pickProperSeq(const Queue *q, uint32_t jit, int64_t play);
+ bool recycleUnit(uint32_t start, uint32_t end, uint32_t connected,
size_t avail, float goodRatio);
- int32_t deleteUnitUnderSeq(Q *q, uint32_t seq);
+ int32_t deleteUnitUnderSeq(Queue *q, uint32_t seq);
void printNowTimeUs(int64_t start, int64_t now, int64_t play);
void printRTPTime(uint32_t rtp, int64_t play, uint32_t exp, bool isExp);
diff --git a/media/libstagefright/rtsp/AHEVCAssembler.cpp b/media/libstagefright/rtsp/AHEVCAssembler.cpp
index f652cc1..148a0ba 100644
--- a/media/libstagefright/rtsp/AHEVCAssembler.cpp
+++ b/media/libstagefright/rtsp/AHEVCAssembler.cpp
@@ -49,7 +49,7 @@
mNextExpectedSeqNo(0),
mAccessUnitDamaged(false),
mFirstIFrameProvided(false),
- mLastIFrameProvidedAt(0),
+ mLastIFrameProvidedAtMs(0),
mWidth(0),
mHeight(0) {
@@ -66,17 +66,18 @@
List<sp<ABuffer> >::iterator it = queue->begin();
- uint16_t queueHeadSeqNum;
- if (it != queue->end())
- queueHeadSeqNum = (*it)->int32Data();
+ if (it == queue->end()) {
+ return nackCount /* 0 */;
+ }
- // move to the packet after that RTCP:NACK sent.
- while (it != queue->end()) {
+ uint16_t queueHeadSeqNum = (*it)->int32Data();
+
+ // move to the packet after which RTCP:NACK was sent.
+ for (; it != queue->end(); ++it) {
int32_t seqNum = (*it)->int32Data();
- if (seqNum < source->mHighestNackNumber)
- it++;
- else
+ if (seqNum >= source->mHighestNackNumber) {
break;
+ }
}
int32_t nackStartAt = -1;
@@ -84,12 +85,14 @@
while (it != queue->end()) {
int32_t seqBeforeLast = (*it)->int32Data();
// increase iterator.
- if ((++it) == queue->end())
+ if ((++it) == queue->end()) {
break;
+ }
+
int32_t seqLast = (*it)->int32Data();
if ((seqLast - seqBeforeLast) < 0) {
- ALOGD("addNack : found end of seqNum from(%d) to(%d)", seqBeforeLast, seqLast);
+ ALOGD("addNack: found end of seqNum from(%d) to(%d)", seqBeforeLast, seqLast);
source->mHighestNackNumber = 0;
}
@@ -106,10 +109,10 @@
if (nackStartAt != -1) {
nackCount = source->mHighestNackNumber - nackStartAt + 1;
- ALOGD("addNack : nackCount=%d, nackFrom=%d, nackTo=%d", nackCount,
+ ALOGD("addNack: nackCount=%d, nackFrom=%d, nackTo=%d", nackCount,
nackStartAt, source->mHighestNackNumber);
- uint16_t mask = (uint16_t)(0xffff) >> (16-nackCount+1);
+ uint16_t mask = (uint16_t)(0xffff) >> (16 - nackCount + 1);
source->setSeqNumToNACK(nackStartAt, mask, queueHeadSeqNum);
}
@@ -133,7 +136,7 @@
int64_t playedTime = nowTime - startTime;
int64_t playedTimeRtp = source->mFirstRtpTime +
(((uint32_t)playedTime) * (source->mClockRate / 1000));
- const uint32_t jitterTime = (uint32_t)(source->mClockRate / ((float)1000 / (source->mJbTime)));
+ const uint32_t jitterTime = (uint32_t)(source->mClockRate / ((float)1000 / (source->mJbTimeMs)));
uint32_t expiredTimeInJb = rtpTime + jitterTime;
bool isExpired = expiredTimeInJb <= (playedTimeRtp);
bool isTooLate200 = expiredTimeInJb < (playedTimeRtp - jitterTime);
@@ -153,8 +156,9 @@
return NOT_ENOUGH_DATA;
}
- if (isTooLate200)
+ if (isTooLate200) {
ALOGW("=== WARNING === buffer arrived 200ms late. === WARNING === ");
+ }
if (isTooLate300) {
ALOGW("buffer arrived after 300ms ... \t Diff in Jb=%lld \t Seq# %d",
@@ -237,6 +241,9 @@
}
void AHEVCAssembler::checkSpsUpdated(const sp<ABuffer> &buffer) {
+ if (buffer->size() == 0) {
+ return;
+ }
const uint8_t *data = buffer->data();
HevcParameterSets paramSets;
unsigned nalType = (data[0] >> 1) & H265_NALU_MASK;
@@ -254,10 +261,13 @@
}
void AHEVCAssembler::checkIFrameProvided(const sp<ABuffer> &buffer) {
+ if (buffer->size() == 0) {
+ return;
+ }
const uint8_t *data = buffer->data();
unsigned nalType = (data[0] >> 1) & H265_NALU_MASK;
if (nalType > 0x0F && nalType < 0x18) {
- mLastIFrameProvidedAt = ALooper::GetNowUs() / 1000;
+ mLastIFrameProvidedAtMs = ALooper::GetNowUs() / 1000;
if (!mFirstIFrameProvided) {
mFirstIFrameProvided = true;
uint32_t rtpTime;
@@ -268,12 +278,12 @@
}
bool AHEVCAssembler::dropFramesUntilIframe(const sp<ABuffer> &buffer) {
+ if (buffer->size() == 0) {
+ return false;
+ }
const uint8_t *data = buffer->data();
unsigned nalType = (data[0] >> 1) & H265_NALU_MASK;
- if (!mFirstIFrameProvided && nalType < 0x10)
- return true;
-
- return false;
+ return !mFirstIFrameProvided && nalType < 0x10;
}
void AHEVCAssembler::addSingleNALUnit(const sp<ABuffer> &buffer) {
@@ -419,16 +429,18 @@
size_t size = buffer->size();
if ((uint32_t)buffer->int32Data() != expectedSeqNo) {
- ALOGV("sequence not complete, expected seqNo %d, got %d, nalType %d",
+ ALOGV("sequence not complete, expected seqNo %u, got %u, nalType %u",
expectedSeqNo, (uint32_t)buffer->int32Data(), nalType);
snapped = true;
- if (!pFrame)
+ if (!pFrame) {
return WRONG_SEQUENCE_NUMBER;
+ }
}
- if (!snapped)
+ if (!snapped) {
connected++;
+ }
uint32_t rtpTime;
CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
@@ -450,7 +462,7 @@
totalSize += size - 3;
++totalCount;
- expectedSeqNo = buffer->int32Data() + 1;
+ expectedSeqNo = (uint32_t)buffer->int32Data() + 1;
if (data[2] & 0x40) {
if (pFrame && !recycleUnit(startSeqNo, expectedSeqNo,
@@ -565,13 +577,13 @@
msg->post();
}
-int32_t AHEVCAssembler::pickProperSeq(const Q *q, uint32_t jit, int64_t play) {
- sp<ABuffer> buffer = *(q->begin());
+int32_t AHEVCAssembler::pickProperSeq(const Queue *queue, uint32_t jit, int64_t play) {
+ sp<ABuffer> buffer = *(queue->begin());
uint32_t rtpTime;
int32_t nextSeqNo = buffer->int32Data();
- Q::const_iterator it = q->begin();
- while (it != q->end()) {
+ Queue::const_iterator it = queue->begin();
+ while (it != queue->end()) {
CHECK((*it)->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
// if pkt in time exists, that should be the next pivot
if (rtpTime + jit >= play) {
@@ -596,17 +608,17 @@
return isRecycle;
}
-int32_t AHEVCAssembler::deleteUnitUnderSeq(Q *q, uint32_t seq) {
- int32_t initSize = q->size();
- Q::iterator it = q->begin();
- while (it != q->end()) {
+int32_t AHEVCAssembler::deleteUnitUnderSeq(Queue *queue, uint32_t seq) {
+ int32_t initSize = queue->size();
+ Queue::iterator it = queue->begin();
+ while (it != queue->end()) {
if ((uint32_t)(*it)->int32Data() >= seq) {
break;
}
it++;
}
- q->erase(q->begin(), it);
- return initSize - q->size();
+ queue->erase(queue->begin(), it);
+ return initSize - queue->size();
}
inline void AHEVCAssembler::printNowTimeUs(int64_t start, int64_t now, int64_t play) {
@@ -624,10 +636,10 @@
const sp<ARTPSource> &source) {
AssemblyStatus status = addNALUnit(source);
if (status == MALFORMED_PACKET) {
- uint64_t timeAfterLastIFrame = (ALooper::GetNowUs() / 1000) - mLastIFrameProvidedAt;
- if (timeAfterLastIFrame > 1000) {
+ uint64_t msecsSinceLastIFrame = (ALooper::GetNowUs() / 1000) - mLastIFrameProvidedAtMs;
+ if (msecsSinceLastIFrame > 1000) {
ALOGV("request FIR to get a new I-Frame, time after "
- "last I-Frame in miils %llu", (unsigned long long)timeAfterLastIFrame);
+ "last I-Frame in %llu ms", (unsigned long long)msecsSinceLastIFrame);
source->onIssueFIRByAssembler();
}
}
@@ -636,7 +648,7 @@
void AHEVCAssembler::packetLost() {
CHECK(mNextExpectedSeqNoValid);
- ALOGD("packetLost (expected %d)", mNextExpectedSeqNo);
+ ALOGD("packetLost (expected %u)", mNextExpectedSeqNo);
++mNextExpectedSeqNo;
}
diff --git a/media/libstagefright/rtsp/AHEVCAssembler.h b/media/libstagefright/rtsp/AHEVCAssembler.h
index 25e45b6..16fc1c8 100644
--- a/media/libstagefright/rtsp/AHEVCAssembler.h
+++ b/media/libstagefright/rtsp/AHEVCAssembler.h
@@ -25,14 +25,14 @@
namespace android {
-using Q = List<sp<ABuffer> >;
-
struct ABuffer;
struct AMessage;
struct AHEVCAssembler : public ARTPAssembler {
AHEVCAssembler(const sp<AMessage> ¬ify);
+ typedef List<sp<ABuffer> > Queue;
+
protected:
virtual ~AHEVCAssembler();
@@ -48,7 +48,7 @@
uint32_t mNextExpectedSeqNo;
bool mAccessUnitDamaged;
bool mFirstIFrameProvided;
- uint64_t mLastIFrameProvidedAt;
+ uint64_t mLastIFrameProvidedAtMs;
int32_t mWidth;
int32_t mHeight;
List<sp<ABuffer> > mNALUnits;
@@ -64,10 +64,10 @@
void submitAccessUnit();
- int32_t pickProperSeq(const Q *q, uint32_t jit, int64_t play);
+ int32_t pickProperSeq(const Queue *queue, uint32_t jit, int64_t play);
bool recycleUnit(uint32_t start, uint32_t end, uint32_t conneceted,
size_t avail, float goodRatio);
- int32_t deleteUnitUnderSeq(Q *q, uint32_t seq);
+ int32_t deleteUnitUnderSeq(Queue *queue, uint32_t seq);
void printNowTimeUs(int64_t start, int64_t now, int64_t play);
void printRTPTime(uint32_t rtp, int64_t play, uint32_t exp, bool isExp);
diff --git a/media/libstagefright/rtsp/ARTPConnection.cpp b/media/libstagefright/rtsp/ARTPConnection.cpp
index 3837b71..f57077c 100644
--- a/media/libstagefright/rtsp/ARTPConnection.cpp
+++ b/media/libstagefright/rtsp/ARTPConnection.cpp
@@ -80,7 +80,7 @@
mLastReceiverReportTimeUs(-1),
mLastBitrateReportTimeUs(-1),
mTargetBitrate(-1),
- mJbTime(300) {
+ mJbTimeMs(300) {
}
ARTPConnection::~ARTPConnection() {
@@ -544,8 +544,9 @@
(!receiveRTP && s->mNumRTCPPacketsReceived == 0)
? sizeSockSt : 0;
- if (mFlags & kViLTEConnection)
+ if (mFlags & kViLTEConnection) {
remoteAddrLen = 0;
+ }
ssize_t nbytes;
do {
@@ -1032,11 +1033,12 @@
source = new ARTPSource(
srcId, info->mSessionDesc, info->mIndex, info->mNotifyMsg);
- if (mFlags & kViLTEConnection)
+ if (mFlags & kViLTEConnection) {
source->setPeriodicFIR(false);
+ }
source->setSelfID(mSelfID);
- source->setJbTime(mJbTime > 0 ? mJbTime : 300);
+ source->setJbTime(mJbTimeMs > 0 ? mJbTimeMs : 300);
info->mSources.add(srcId, source);
} else {
source = info->mSources.valueAt(index);
@@ -1056,8 +1058,8 @@
mSelfID = selfID;
}
-void ARTPConnection::setJbTime(const uint32_t jbTime) {
- mJbTime = jbTime;
+void ARTPConnection::setJbTime(const uint32_t jbTimeMs) {
+ mJbTimeMs = jbTimeMs;
}
void ARTPConnection::setTargetBitrate(int32_t targetBitrate) {
diff --git a/media/libstagefright/rtsp/ARTPConnection.h b/media/libstagefright/rtsp/ARTPConnection.h
index 25d03f5..7c8218f 100644
--- a/media/libstagefright/rtsp/ARTPConnection.h
+++ b/media/libstagefright/rtsp/ARTPConnection.h
@@ -46,7 +46,7 @@
void injectPacket(int index, const sp<ABuffer> &buffer);
void setSelfID(const uint32_t selfID);
- void setJbTime(const uint32_t jbTime);
+ void setJbTime(const uint32_t jbTimeMs);
void setTargetBitrate(int32_t targetBitrate);
// Creates a pair of UDP datagram sockets bound to adjacent ports
@@ -88,7 +88,7 @@
int32_t mSelfID;
int32_t mTargetBitrate;
- uint32_t mJbTime;
+ uint32_t mJbTimeMs;
int32_t mCumulativeBytes;
diff --git a/media/libstagefright/rtsp/ARTPSource.cpp b/media/libstagefright/rtsp/ARTPSource.cpp
index 361773e..6303fc4 100644
--- a/media/libstagefright/rtsp/ARTPSource.cpp
+++ b/media/libstagefright/rtsp/ARTPSource.cpp
@@ -46,7 +46,7 @@
mFirstRtpTime(0),
mFirstSysTime(0),
mClockRate(0),
- mJbTime(300), // default jitter buffer time is 300ms.
+ mJbTimeMs(300), // default jitter buffer time is 300ms.
mFirstSsrc(0),
mHighestNackNumber(0),
mID(id),
@@ -217,22 +217,23 @@
bool send = false;
int64_t nowUs = ALooper::GetNowUs();
- int64_t timeAfterLastFIR = nowUs - mLastFIRRequestUs;
+ int64_t usecsSinceLastFIR = nowUs - mLastFIRRequestUs;
if (mLastFIRRequestUs < 0) {
// A first FIR, just send it.
send = true;
- } else if (mIssueFIRByAssembler && (timeAfterLastFIR > 1000000)) {
+ } else if (mIssueFIRByAssembler && (usecsSinceLastFIR > 1000000)) {
// A FIR issued by Assembler.
// Send it if last FIR is not sent within a sec.
send = true;
- } else if (mIssueFIRRequests && (timeAfterLastFIR > 5000000)) {
+ } else if (mIssueFIRRequests && (usecsSinceLastFIR > 5000000)) {
// A FIR issued periodically reagardless packet loss.
// Send it if last FIR is not sent within 5 secs.
send = true;
}
- if (!send)
+ if (!send) {
return;
+ }
mLastFIRRequestUs = nowUs;
@@ -246,7 +247,7 @@
data[0] = 0x80 | 4;
data[1] = 206; // PSFB
data[2] = 0;
- data[3] = 4;
+ data[3] = 4; // total (4+1) * sizeof(int32_t) = 20 bytes
data[4] = kSourceID >> 24;
data[5] = (kSourceID >> 16) & 0xff;
data[6] = (kSourceID >> 8) & 0xff;
@@ -270,8 +271,7 @@
buffer->setRange(buffer->offset(), buffer->size() + (data[3] + 1) * sizeof(int32_t));
- if (mIssueFIRByAssembler)
- mIssueFIRByAssembler = false;
+ mIssueFIRByAssembler = false;
ALOGV("Added FIR request.");
}
@@ -303,7 +303,7 @@
data[0] = 0x80 | 1;
data[1] = 201; // RR
data[2] = 0;
- data[3] = 7;
+ data[3] = 7; // total (7+1) * sizeof(int32_t) = 32 bytes
data[4] = kSourceID >> 24;
data[5] = (kSourceID >> 16) & 0xff;
data[6] = (kSourceID >> 8) & 0xff;
@@ -358,8 +358,9 @@
return;
}
- if (targetBitrate <= 0)
+ if (targetBitrate <= 0) {
return;
+ }
uint8_t *data = buffer->data() + buffer->size();
@@ -397,8 +398,9 @@
}
int ARTPSource::addNACK(const sp<ABuffer> &buffer) {
- if (buffer->size() + 52 > buffer->capacity()) {
- ALOGW("RTCP buffer too small to accomodate NACK.");
+ constexpr size_t kMaxFCIs = 10; // max number of FCIs
+ if (buffer->size() + (3 + kMaxFCIs) * sizeof(int32_t) > buffer->capacity()) {
+ ALOGW("RTCP buffer too small to accommodate NACK.");
return -1;
}
@@ -420,11 +422,11 @@
List<int> list;
List<int>::iterator it;
- getSeqNumToNACK(list, 10);
- int cnt = 0;
+ getSeqNumToNACK(list, kMaxFCIs);
+ size_t cnt = 0;
- int* FCI = (int*)(data + 12);
- for (it = list.begin() ; it != list.end() ; it++) {
+ int *FCI = (int *)(data + 12);
+ for (it = list.begin(); it != list.end() && cnt < kMaxFCIs; it++) {
*(FCI + cnt) = *it;
cnt++;
}
@@ -441,16 +443,13 @@
int cnt = 0;
std::map<uint16_t, infoNACK>::iterator it;
- for(it = mNACKMap.begin() ; it != mNACKMap.end() ; it++) {
- infoNACK& info_it = it->second;
- // list full
- if (cnt == size)
- break;
+ for(it = mNACKMap.begin(); it != mNACKMap.end() && cnt < size; it++) {
+ infoNACK &info_it = it->second;
if (info_it.needToNACK) {
info_it.needToNACK = false;
// switch LSB to MSB for sending N/W
uint32_t FCI;
- uint8_t* temp = (uint8_t*)&FCI;
+ uint8_t *temp = (uint8_t *)&FCI;
temp[0] = (info_it.seqNum >> 8) & 0xff;
temp[1] = (info_it.seqNum) & 0xff;
temp[2] = (info_it.mask >> 8) & 0xff;
@@ -471,9 +470,9 @@
it = mNACKMap.find(seqNum);
if (it != mNACKMap.end()) {
- infoNACK& info_it = it->second;
+ infoNACK &info_it = it->second;
// renew if (mask or head seq) is changed
- if((info_it.mask != mask) || (info_it.nowJitterHeadSeqNum != nowJitterHeadSeqNum)) {
+ if ((info_it.mask != mask) || (info_it.nowJitterHeadSeqNum != nowJitterHeadSeqNum)) {
info_it = info;
}
} else {
@@ -482,16 +481,16 @@
// delete all NACK far from current Jitter's first sequence number
it = mNACKMap.begin();
- while(it != mNACKMap.end()) {
- infoNACK& info_it = it->second;
+ while (it != mNACKMap.end()) {
+ infoNACK &info_it = it->second;
int diff = nowJitterHeadSeqNum - info_it.nowJitterHeadSeqNum;
if (diff > 100) {
ALOGV("Delete %d pkt from NACK map ", info_it.seqNum);
it = mNACKMap.erase(it);
- }
- else
+ } else {
it++;
+ }
}
}
@@ -499,12 +498,13 @@
uint32_t ARTPSource::getSelfID() {
return kSourceID;
}
+
void ARTPSource::setSelfID(const uint32_t selfID) {
kSourceID = selfID;
}
-void ARTPSource::setJbTime(const uint32_t jbTime) {
- mJbTime = jbTime;
+void ARTPSource::setJbTime(const uint32_t jbTimeMs) {
+ mJbTimeMs = jbTimeMs;
}
void ARTPSource::setPeriodicFIR(bool enable) {
diff --git a/media/libstagefright/rtsp/ARTPSource.h b/media/libstagefright/rtsp/ARTPSource.h
index 9d4ccf2..ea683a0 100644
--- a/media/libstagefright/rtsp/ARTPSource.h
+++ b/media/libstagefright/rtsp/ARTPSource.h
@@ -53,7 +53,7 @@
void setSeqNumToNACK(uint16_t seqNum, uint16_t mask, uint16_t nowJitterHeadSeqNum);
uint32_t getSelfID();
void setSelfID(const uint32_t selfID);
- void setJbTime(const uint32_t jbTime);
+ void setJbTime(const uint32_t jbTimeMs);
void setPeriodicFIR(bool enable);
void notifyPktInfo(int32_t bitrate, int64_t time);
// FIR needs to be sent by missing packet or broken video image.
@@ -66,7 +66,7 @@
int64_t mFirstSysTime;
int32_t mClockRate;
- uint32_t mJbTime;
+ uint32_t mJbTimeMs;
int32_t mFirstSsrc;
int32_t mHighestNackNumber;
diff --git a/media/libstagefright/rtsp/ARTPWriter.cpp b/media/libstagefright/rtsp/ARTPWriter.cpp
index 02c9d48..76afb04 100644
--- a/media/libstagefright/rtsp/ARTPWriter.cpp
+++ b/media/libstagefright/rtsp/ARTPWriter.cpp
@@ -303,15 +303,15 @@
}
static const uint8_t SPCSize = 4; // Start Prefix Code Size
-static const uint8_t startPrefixCode[SPCSize] = {0,0,0,1};
-static const uint8_t spcKMPidx[SPCSize] = {0,0,2,0};
+static const uint8_t startPrefixCode[SPCSize] = {0, 0, 0, 1};
+static const uint8_t spcKMPidx[SPCSize] = {0, 0, 2, 0};
static void SpsPpsParser(MediaBufferBase *buffer,
- MediaBufferBase **spsBuffer, MediaBufferBase **ppsBuffer) {
+ MediaBufferBase **spsBuffer, MediaBufferBase **ppsBuffer) {
- while (true) {
- const uint8_t* NALPtr = (const uint8_t *)buffer->data() + buffer->range_offset();
+ while (buffer->range_length() > 0) {
+ const uint8_t *NALPtr = (const uint8_t *)buffer->data() + buffer->range_offset();
- MediaBufferBase** targetPtr = NULL;
+ MediaBufferBase **targetPtr = NULL;
if ((*NALPtr & H264_NALU_MASK) == H264_NALU_SPS) {
targetPtr = spsBuffer;
} else if ((*NALPtr & H264_NALU_MASK) == H264_NALU_PPS) {
@@ -322,12 +322,13 @@
ALOGV("SPS(7) or PPS(8) found. Type %d", *NALPtr & H264_NALU_MASK);
uint32_t bufferSize = buffer->range_length();
- MediaBufferBase*& target = *targetPtr;
+ MediaBufferBase *&target = *targetPtr;
uint32_t i = 0, j = 0;
bool isBoundFound = false;
- for (i = 0; i < bufferSize ; i++) {
- while (j > 0 && NALPtr[i] != startPrefixCode[j])
- j = spcKMPidx[j-1];
+ for (i = 0; i < bufferSize; i++) {
+ while (j > 0 && NALPtr[i] != startPrefixCode[j]) {
+ j = spcKMPidx[j - 1];
+ }
if (NALPtr[i] == startPrefixCode[j]) {
j++;
if (j == SPCSize) {
@@ -338,8 +339,11 @@
}
uint32_t targetSize;
- if (target != NULL)
+ if (target != NULL) {
target->release();
+ }
+ // note that targetSize is never 0 as the first byte is never part
+ // of a start prefix
if (isBoundFound) {
targetSize = i - SPCSize + 1;
target = MediaBufferBase::Create(targetSize);
@@ -361,13 +365,13 @@
}
static void VpsSpsPpsParser(MediaBufferBase *buffer,
- MediaBufferBase **vpsBuffer, MediaBufferBase **spsBuffer, MediaBufferBase **ppsBuffer) {
+ MediaBufferBase **vpsBuffer, MediaBufferBase **spsBuffer, MediaBufferBase **ppsBuffer) {
- while (true) {
- const uint8_t* NALPtr = (const uint8_t *)buffer->data() + buffer->range_offset();
+ while (buffer->range_length() > 0) {
+ const uint8_t *NALPtr = (const uint8_t *)buffer->data() + buffer->range_offset();
uint8_t nalType = ((*NALPtr) >> 1) & H265_NALU_MASK;
- MediaBufferBase** targetPtr = NULL;
+ MediaBufferBase **targetPtr = NULL;
if (nalType == H265_NALU_VPS) {
targetPtr = vpsBuffer;
} else if (nalType == H265_NALU_SPS) {
@@ -379,32 +383,44 @@
}
ALOGV("VPS(32) SPS(33) or PPS(34) found. Type %d", nalType);
- uint32_t targetSize = buffer->range_length();
- MediaBufferBase*& target = *targetPtr;
- uint32_t j;
+ uint32_t bufferSize = buffer->range_length();
+ MediaBufferBase *&target = *targetPtr;
+ uint32_t i = 0, j = 0;
bool isBoundFound = false;
- for (j = 0; j < targetSize - SPCSize ; j++) {
- if (!memcmp(NALPtr + j, startPrefixCode, SPCSize)) {
- isBoundFound = true;
- break;
+ for (i = 0; i < bufferSize; i++) {
+ while (j > 0 && NALPtr[i] != startPrefixCode[j]) {
+ j = spcKMPidx[j - 1];
+ }
+ if (NALPtr[i] == startPrefixCode[j]) {
+ j++;
+ if (j == SPCSize) {
+ isBoundFound = true;
+ break;
+ }
}
}
- if (target != NULL)
+ if (target != NULL) {
target->release();
+ }
+ uint32_t targetSize;
+ // note that targetSize is never 0 as the first byte is never part
+ // of a start prefix
if (isBoundFound) {
+ targetSize = i - SPCSize + 1;
target = MediaBufferBase::Create(j);
memcpy(target->data(),
(const uint8_t *)buffer->data() + buffer->range_offset(),
j);
- buffer->set_range(buffer->range_offset() + j + SPCSize,
- buffer->range_length() - j - SPCSize);
+ buffer->set_range(buffer->range_offset() + targetSize + SPCSize,
+ buffer->range_length() - targetSize - SPCSize);
} else {
+ targetSize = bufferSize;
target = MediaBufferBase::Create(targetSize);
memcpy(target->data(),
(const uint8_t *)buffer->data() + buffer->range_offset(),
targetSize);
- buffer->set_range(buffer->range_offset() + targetSize, 0);
+ buffer->set_range(buffer->range_offset() + bufferSize, 0);
return;
}
}
@@ -519,13 +535,15 @@
if (mMode == H264) {
StripStartcode(mediaBuf);
SpsPpsParser(mediaBuf, &mSPSBuf, &mPPSBuf);
- if (mediaBuf->range_length() > 0)
+ if (mediaBuf->range_length() > 0) {
sendAVCData(mediaBuf);
+ }
} else if (mMode == H265) {
StripStartcode(mediaBuf);
VpsSpsPpsParser(mediaBuf, &mVPSBuf, &mSPSBuf, &mPPSBuf);
- if (mediaBuf->range_length() > 0)
+ if (mediaBuf->range_length() > 0) {
sendHEVCData(mediaBuf);
+ }
} else if (mMode == H263) {
sendH263Data(mediaBuf);
} else if (mMode == AMR_NB || mMode == AMR_WB) {
@@ -882,12 +900,13 @@
}
void ARTPWriter::sendSPSPPSIfIFrame(MediaBufferBase *mediaBuf, int64_t timeUs) {
+ CHECK(mediaBuf->range_length() > 0);
const uint8_t *mediaData =
(const uint8_t *)mediaBuf->data() + mediaBuf->range_offset();
- if (mediaBuf->range_length() == 0
- || (mediaData[0] & H264_NALU_MASK) != H264_NALU_IFRAME)
+ if ((mediaData[0] & H264_NALU_MASK) != H264_NALU_IFRAME) {
return;
+ }
if (mSPSBuf != NULL) {
mSPSBuf->meta_data().setInt64(kKeyTime, timeUs);
@@ -903,11 +922,14 @@
}
void ARTPWriter::sendVPSSPSPPSIfIFrame(MediaBufferBase *mediaBuf, int64_t timeUs) {
+ CHECK(mediaBuf->range_length() > 0);
const uint8_t *mediaData =
(const uint8_t *)mediaBuf->data() + mediaBuf->range_offset();
+
int nalType = ((mediaData[0] >> 1) & H265_NALU_MASK);
- if (!(nalType >= 16 && nalType <= 21)/*H265_NALU_IFRAME*/)
+ if (!(nalType >= 16 && nalType <= 21) /*H265_NALU_IFRAME*/) {
return;
+ }
if (mVPSBuf != NULL) {
mVPSBuf->meta_data().setInt64(kKeyTime, timeUs);
@@ -939,6 +961,7 @@
uint32_t rtpTime = mRTPTimeBase + (timeUs * 9 / 100ll);
+ CHECK(mediaBuf->range_length() > 0);
const uint8_t *mediaData =
(const uint8_t *)mediaBuf->data() + mediaBuf->range_offset();
@@ -956,10 +979,11 @@
// The data fits into a single packet
uint8_t *data = buffer->data();
data[0] = 0x80;
- if (isNonVCL)
+ if (isNonVCL) {
data[1] = mPayloadType; // Marker bit should not be set in case of Non-VCL
- else
+ } else {
data[1] = (1 << 7) | mPayloadType; // M-bit
+ }
data[2] = (mSeqNo >> 8) & 0xff;
data[3] = mSeqNo & 0xff;
data[4] = rtpTime >> 24;
@@ -1074,6 +1098,7 @@
uint32_t rtpTime = mRTPTimeBase + (timeUs * 9 / 100LL);
+ CHECK(mediaBuf->range_length() > 0);
const uint8_t *mediaData =
(const uint8_t *)mediaBuf->data() + mediaBuf->range_offset();
@@ -1523,7 +1548,7 @@
// TODO : Develop more advanced moderator based on AS & TMMBR value
void ARTPWriter::ModerateInstantTraffic(uint32_t samplePeriod, uint32_t limitBytes) {
unsigned int bytes = mTrafficRec->readBytesForLastPeriod(samplePeriod);
- if(bytes > limitBytes) {
+ if (bytes > limitBytes) {
ALOGI("Nuclear moderator. #seq = %d \t\t %d bits / 10ms",
mSeqNo, bytes * 8);
usleep(4000);
diff --git a/media/libstagefright/rtsp/TrafficRecorder.h b/media/libstagefright/rtsp/TrafficRecorder.h
index 4796560..f8e7c03 100644
--- a/media/libstagefright/rtsp/TrafficRecorder.h
+++ b/media/libstagefright/rtsp/TrafficRecorder.h
@@ -18,7 +18,7 @@
#define A_TRAFFIC_RECORDER_H_
-#include <utils/Log.h>
+#include <android-base/logging.h>
#include <utils/RefBase.h>
namespace android {
@@ -27,12 +27,12 @@
template <class Time, class Bytes>
class TrafficRecorder : public RefBase {
private:
- int mSize;
- int mSizeMask;
- Time* mTimeArray = NULL;
- Bytes* mBytesArray = NULL;
- int mHeadIdx = 0;
- int mTailIdx = 0;
+ size_t mSize;
+ size_t mSizeMask;
+ Time *mTimeArray = NULL;
+ Bytes *mBytesArray = NULL;
+ size_t mHeadIdx = 0;
+ size_t mTailIdx = 0;
Time mClock = 0;
Time mLastTimeOfPrint = 0;
@@ -59,29 +59,26 @@
template <class Time, class Bytes>
TrafficRecorder<Time, Bytes>::TrafficRecorder(size_t size) {
- int exp;
- for (exp = 0 ; exp < 32 ; exp++) {
- if (size <= (1 << exp))
+ size_t exp;
+ for (exp = 0; exp < 32; exp++) {
+ if (size <= (1ul << exp)) {
break;
+ }
}
- mSize = (1 << exp); // size = 2^exp
+ mSize = (1ul << exp); // size = 2^exp
mSizeMask = mSize - 1;
- ALOGV("TrafficRecorder Init size %u", mSize);
- if (mTimeArray != NULL)
- free(mTimeArray);
- if (mBytesArray != NULL)
- free(mBytesArray);
- mTimeArray = (Time*)malloc(sizeof(Time) * mSize);
- mBytesArray = (Bytes*)malloc(sizeof(Bytes) * mSize);
+ LOG(VERBOSE) << "TrafficRecorder Init size " << mSize;
+ mTimeArray = new Time[mSize];
+ mBytesArray = new Bytes[mSize];
init();
}
template <class Time, class Bytes>
TrafficRecorder<Time, Bytes>::~TrafficRecorder() {
- free(mTimeArray);
- free(mBytesArray);
+ delete[] mTimeArray;
+ delete[] mBytesArray;
}
template <class Time, class Bytes>
@@ -101,14 +98,14 @@
Bytes TrafficRecorder<Time, Bytes>::readBytesForLastPeriod(Time period) {
Bytes bytes = 0;
- int i = mTailIdx;
- while(i != mHeadIdx) {
- ALOGV("READ %d time %d \t EndOfPeriod %d", i, mTimeArray[i], mClock - period);
+ size_t i = mTailIdx;
+ while (i != mHeadIdx) {
+ LOG(VERBOSE) << "READ " << i << " time " << mTimeArray[i] << " \t EndOfPeriod " << mClock - period;
if (mTimeArray[i] < mClock - period) {
break;
}
bytes += mBytesArray[i];
- i = (i - 1 + mSize) & mSizeMask;
+ i = (i + mSize - 1) & mSizeMask;
}
mHeadIdx = i;
return bytes;
@@ -116,8 +113,8 @@
template <class Time, class Bytes>
void TrafficRecorder<Time, Bytes>::writeBytes(Bytes bytes) {
- int writeIdx;
- if(mClock == mTimeArray[mTailIdx]) {
+ size_t writeIdx;
+ if (mClock == mTimeArray[mTailIdx]) {
writeIdx = mTailIdx;
mBytesArray[writeIdx] += bytes;
} else {
@@ -126,9 +123,9 @@
mBytesArray[writeIdx] = bytes;
}
- ALOGV("WRITE %d time %d", writeIdx, mClock);
+ LOG(VERBOSE) << "WRITE " << writeIdx << " time " << mClock;
if (writeIdx == mHeadIdx) {
- ALOGW("Traffic recorder size exceeded at %d", mHeadIdx);
+ LOG(WARNING) << "Traffic recorder size exceeded at " << mHeadIdx;
mHeadIdx = (mHeadIdx + 1) & mSizeMask;
}
@@ -140,7 +137,7 @@
void TrafficRecorder<Time, Bytes>::printAccuBitsForLastPeriod(Time period, Time unit) {
Time duration = mClock - mLastTimeOfPrint;
float numOfUnit = (float)duration / unit;
- if(duration > period) {
+ if (duration > period) {
ALOGD("Actual Tx period %.0f ms \t %.0f Bits/Unit",
numOfUnit * 1000.f, mAccuBytesOfPrint * 8.f / numOfUnit);
mLastTimeOfPrint = mClock;
@@ -148,6 +145,7 @@
init();
}
}
+
} // namespace android
#endif // A_TRAFFIC_RECORDER_H_