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#if CHROMIUM_AVAILABLE
24#include "include/chromium_http_stub.h"
25#endif
26
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/foundation/ALooper.h>
29
30#include <cutils/properties.h>
31#include <cutils/qtaguid.h>
32
33namespace android {
34
35HTTPBase::HTTPBase()
36    : mNumBandwidthHistoryItems(0),
37      mTotalTransferTimeUs(0),
38      mTotalTransferBytes(0),
39      mPrevBandwidthMeasureTimeUs(0),
40      mPrevEstimatedBandWidthKbps(0),
41      mBandWidthCollectFreqMs(5000),
42      mUIDValid(false),
43      mUID(0) {
44}
45
46// static
47sp<HTTPBase> HTTPBase::Create(uint32_t flags) {
48#if CHROMIUM_AVAILABLE
49        HTTPBase *dataSource = createChromiumHTTPDataSource(flags);
50        if (dataSource) {
51           return dataSource;
52        }
53#endif
54    {
55        TRESPASS();
56
57        return NULL;
58    }
59}
60
61void HTTPBase::addBandwidthMeasurement(
62        size_t numBytes, int64_t delayUs) {
63    Mutex::Autolock autoLock(mLock);
64
65    BandwidthEntry entry;
66    entry.mDelayUs = delayUs;
67    entry.mNumBytes = numBytes;
68    mTotalTransferTimeUs += delayUs;
69    mTotalTransferBytes += numBytes;
70
71    mBandwidthHistory.push_back(entry);
72    if (++mNumBandwidthHistoryItems > 100) {
73        BandwidthEntry *entry = &*mBandwidthHistory.begin();
74        mTotalTransferTimeUs -= entry->mDelayUs;
75        mTotalTransferBytes -= entry->mNumBytes;
76        mBandwidthHistory.erase(mBandwidthHistory.begin());
77        --mNumBandwidthHistoryItems;
78
79        int64_t timeNowUs = ALooper::GetNowUs();
80        if (timeNowUs - mPrevBandwidthMeasureTimeUs >=
81                mBandWidthCollectFreqMs * 1000LL) {
82
83            if (mPrevBandwidthMeasureTimeUs != 0) {
84                mPrevEstimatedBandWidthKbps =
85                    (mTotalTransferBytes * 8E3 / mTotalTransferTimeUs);
86            }
87            mPrevBandwidthMeasureTimeUs = timeNowUs;
88        }
89    }
90
91}
92
93bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) {
94    Mutex::Autolock autoLock(mLock);
95
96    if (mNumBandwidthHistoryItems < 2) {
97        return false;
98    }
99
100    *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs);
101
102    return true;
103}
104
105status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) {
106    Mutex::Autolock autoLock(mLock);
107    *kbps = mPrevEstimatedBandWidthKbps;
108    return OK;
109}
110
111status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) {
112    Mutex::Autolock autoLock(mLock);
113
114    if (freqMs < kMinBandwidthCollectFreqMs
115            || freqMs > kMaxBandwidthCollectFreqMs) {
116
117        ALOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs);
118        return BAD_VALUE;
119    }
120
121    ALOGI("frequency set to %d ms", freqMs);
122    mBandWidthCollectFreqMs = freqMs;
123    return OK;
124}
125
126void HTTPBase::setUID(uid_t uid) {
127    mUIDValid = true;
128    mUID = uid;
129}
130
131bool HTTPBase::getUID(uid_t *uid) const {
132    if (!mUIDValid) {
133        return false;
134    }
135
136    *uid = mUID;
137
138    return true;
139}
140
141// static
142void HTTPBase::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) {
143    int res = qtaguid_tagSocket(sockfd, kTag, uid);
144    if (res != 0) {
145        ALOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid());
146    }
147}
148
149// static
150void HTTPBase::UnRegisterSocketUserTag(int sockfd) {
151    int res = qtaguid_untagSocket(sockfd);
152    if (res != 0) {
153        ALOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid());
154    }
155}
156
157}  // namespace android
158