HTTPBase.cpp revision f0d689934e70d3e5b3784265e890377db04c7c1d
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "HTTPBase"
19#include <utils/Log.h>
20
21#include "include/HTTPBase.h"
22
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/foundation/ALooper.h>
25
26#include <cutils/properties.h>
27#include <cutils/qtaguid.h>
28
29#include <NetdClient.h>
30
31namespace android {
32
33HTTPBase::HTTPBase()
34    : mNumBandwidthHistoryItems(0),
35      mTotalTransferTimeUs(0),
36      mTotalTransferBytes(0),
37      mPrevBandwidthMeasureTimeUs(0),
38      mPrevEstimatedBandWidthKbps(0),
39      mBandWidthCollectFreqMs(5000) {
40}
41
42void HTTPBase::addBandwidthMeasurement(
43        size_t numBytes, int64_t delayUs) {
44    Mutex::Autolock autoLock(mLock);
45
46    BandwidthEntry entry;
47    entry.mDelayUs = delayUs;
48    entry.mNumBytes = numBytes;
49    mTotalTransferTimeUs += delayUs;
50    mTotalTransferBytes += numBytes;
51
52    mBandwidthHistory.push_back(entry);
53    if (++mNumBandwidthHistoryItems > 100) {
54        BandwidthEntry *entry = &*mBandwidthHistory.begin();
55        mTotalTransferTimeUs -= entry->mDelayUs;
56        mTotalTransferBytes -= entry->mNumBytes;
57        mBandwidthHistory.erase(mBandwidthHistory.begin());
58        --mNumBandwidthHistoryItems;
59
60        int64_t timeNowUs = ALooper::GetNowUs();
61        if (timeNowUs - mPrevBandwidthMeasureTimeUs >=
62                mBandWidthCollectFreqMs * 1000LL) {
63
64            if (mPrevBandwidthMeasureTimeUs != 0) {
65                mPrevEstimatedBandWidthKbps =
66                    (mTotalTransferBytes * 8E3 / mTotalTransferTimeUs);
67            }
68            mPrevBandwidthMeasureTimeUs = timeNowUs;
69        }
70    }
71
72}
73
74bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) {
75    Mutex::Autolock autoLock(mLock);
76
77    if (mNumBandwidthHistoryItems < 2) {
78        return false;
79    }
80
81    *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs);
82
83    return true;
84}
85
86status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) {
87    Mutex::Autolock autoLock(mLock);
88    *kbps = mPrevEstimatedBandWidthKbps;
89    return OK;
90}
91
92status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) {
93    Mutex::Autolock autoLock(mLock);
94
95    if (freqMs < kMinBandwidthCollectFreqMs
96            || freqMs > kMaxBandwidthCollectFreqMs) {
97
98        ALOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs);
99        return BAD_VALUE;
100    }
101
102    ALOGI("frequency set to %d ms", freqMs);
103    mBandWidthCollectFreqMs = freqMs;
104    return OK;
105}
106
107// static
108void HTTPBase::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) {
109    int res = qtaguid_tagSocket(sockfd, kTag, uid);
110    if (res != 0) {
111        ALOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid());
112    }
113}
114
115// static
116void HTTPBase::UnRegisterSocketUserTag(int sockfd) {
117    int res = qtaguid_untagSocket(sockfd);
118    if (res != 0) {
119        ALOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid());
120    }
121}
122
123// static
124void HTTPBase::RegisterSocketUserMark(int sockfd, uid_t uid) {
125    setNetworkForUser(uid, sockfd);
126}
127
128// static
129void HTTPBase::UnRegisterSocketUserMark(int sockfd) {
130    RegisterSocketUserMark(sockfd, geteuid());
131}
132
133}  // namespace android
134