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      mMaxBandwidthHistoryItems(100) {
41}
42
43void HTTPBase::addBandwidthMeasurement(
44        size_t numBytes, int64_t delayUs) {
45    Mutex::Autolock autoLock(mLock);
46
47    BandwidthEntry entry;
48    entry.mDelayUs = delayUs;
49    entry.mNumBytes = numBytes;
50    mTotalTransferTimeUs += delayUs;
51    mTotalTransferBytes += numBytes;
52
53    mBandwidthHistory.push_back(entry);
54    if (++mNumBandwidthHistoryItems > mMaxBandwidthHistoryItems) {
55        BandwidthEntry *entry = &*mBandwidthHistory.begin();
56        mTotalTransferTimeUs -= entry->mDelayUs;
57        mTotalTransferBytes -= entry->mNumBytes;
58        mBandwidthHistory.erase(mBandwidthHistory.begin());
59        --mNumBandwidthHistoryItems;
60
61        int64_t timeNowUs = ALooper::GetNowUs();
62        if (timeNowUs - mPrevBandwidthMeasureTimeUs >=
63                mBandWidthCollectFreqMs * 1000LL) {
64
65            if (mPrevBandwidthMeasureTimeUs != 0) {
66                mPrevEstimatedBandWidthKbps =
67                    (mTotalTransferBytes * 8E3 / mTotalTransferTimeUs);
68            }
69            mPrevBandwidthMeasureTimeUs = timeNowUs;
70        }
71    }
72
73}
74
75bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) {
76    Mutex::Autolock autoLock(mLock);
77
78    if (mNumBandwidthHistoryItems < 2) {
79        return false;
80    }
81
82    *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs);
83
84    return true;
85}
86
87status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) {
88    Mutex::Autolock autoLock(mLock);
89    *kbps = mPrevEstimatedBandWidthKbps;
90    return OK;
91}
92
93status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) {
94    Mutex::Autolock autoLock(mLock);
95
96    if (freqMs < kMinBandwidthCollectFreqMs
97            || freqMs > kMaxBandwidthCollectFreqMs) {
98
99        ALOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs);
100        return BAD_VALUE;
101    }
102
103    ALOGI("frequency set to %d ms", freqMs);
104    mBandWidthCollectFreqMs = freqMs;
105    return OK;
106}
107
108void HTTPBase::setBandwidthHistorySize(size_t numHistoryItems) {
109    mMaxBandwidthHistoryItems = numHistoryItems;
110}
111
112// static
113void HTTPBase::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) {
114    int res = qtaguid_tagSocket(sockfd, kTag, uid);
115    if (res != 0) {
116        ALOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid());
117    }
118}
119
120// static
121void HTTPBase::UnRegisterSocketUserTag(int sockfd) {
122    int res = qtaguid_untagSocket(sockfd);
123    if (res != 0) {
124        ALOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid());
125    }
126}
127
128// static
129void HTTPBase::RegisterSocketUserMark(int sockfd, uid_t uid) {
130    setNetworkForUser(uid, sockfd);
131}
132
133// static
134void HTTPBase::UnRegisterSocketUserMark(int sockfd) {
135    RegisterSocketUserMark(sockfd, geteuid());
136}
137
138}  // namespace android
139