blob: 40bfc55824ce2f56d81f3d21fd88c94c4c3db0cf [file] [log] [blame]
Andreas Huber1156dc92011-03-08 15:59:28 -08001/*
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
James Dong5b1b8a92011-05-25 19:37:03 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "HTTPBase"
19#include <utils/Log.h>
20
Andreas Huber1156dc92011-03-08 15:59:28 -080021#include "include/HTTPBase.h"
22
Andreas Huber34fba662011-03-11 08:47:51 -080023#if CHROMIUM_AVAILABLE
Colin Crossbea455c2012-06-15 13:56:18 -070024#include "include/chromium_http_stub.h"
Andreas Huber34fba662011-03-11 08:47:51 -080025#endif
26
Andreas Huberdab718b2011-07-13 15:45:01 -070027#include <media/stagefright/foundation/ADebug.h>
James Dong5b1b8a92011-05-25 19:37:03 -070028#include <media/stagefright/foundation/ALooper.h>
Andreas Huberdab718b2011-07-13 15:45:01 -070029
Andreas Huber1156dc92011-03-08 15:59:28 -080030#include <cutils/properties.h>
Andreas Huberdab718b2011-07-13 15:45:01 -070031#include <cutils/qtaguid.h>
Andreas Huber1156dc92011-03-08 15:59:28 -080032
33namespace android {
34
James Dong5b1b8a92011-05-25 19:37:03 -070035HTTPBase::HTTPBase()
36 : mNumBandwidthHistoryItems(0),
37 mTotalTransferTimeUs(0),
38 mTotalTransferBytes(0),
39 mPrevBandwidthMeasureTimeUs(0),
40 mPrevEstimatedBandWidthKbps(0),
Andreas Huber9b80c2b2011-06-30 15:47:02 -070041 mBandWidthCollectFreqMs(5000),
Ashish Sharmaa23456b2011-07-07 17:57:05 -070042 mUIDValid(false),
43 mUID(0) {
James Dong5b1b8a92011-05-25 19:37:03 -070044}
Andreas Huber1156dc92011-03-08 15:59:28 -080045
46// static
47sp<HTTPBase> HTTPBase::Create(uint32_t flags) {
Andreas Huber34fba662011-03-11 08:47:51 -080048#if CHROMIUM_AVAILABLE
Colin Crossbea455c2012-06-15 13:56:18 -070049 HTTPBase *dataSource = createChromiumHTTPDataSource(flags);
50 if (dataSource) {
51 return dataSource;
52 }
Andreas Huber34fba662011-03-11 08:47:51 -080053#endif
54 {
Andreas Huberdab718b2011-07-13 15:45:01 -070055 TRESPASS();
56
57 return NULL;
Andreas Huber1156dc92011-03-08 15:59:28 -080058 }
59}
60
James Dong5b1b8a92011-05-25 19:37:03 -070061void 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
Steve Block29357bc2012-01-06 19:20:56 +0000117 ALOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs);
James Dong5b1b8a92011-05-25 19:37:03 -0700118 return BAD_VALUE;
119 }
120
Steve Blockdf64d152012-01-04 20:05:49 +0000121 ALOGI("frequency set to %d ms", freqMs);
James Dong5b1b8a92011-05-25 19:37:03 -0700122 mBandWidthCollectFreqMs = freqMs;
123 return OK;
124}
125
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700126void 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
Andreas Huberdab718b2011-07-13 15:45:01 -0700141// static
Ashish Sharmaa23456b2011-07-07 17:57:05 -0700142void HTTPBase::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) {
143 int res = qtaguid_tagSocket(sockfd, kTag, uid);
144 if (res != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000145 ALOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid());
Ashish Sharmaa23456b2011-07-07 17:57:05 -0700146 }
147}
148
149// static
150void HTTPBase::UnRegisterSocketUserTag(int sockfd) {
151 int res = qtaguid_untagSocket(sockfd);
152 if (res != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000153 ALOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid());
Ashish Sharmaa23456b2011-07-07 17:57:05 -0700154 }
Andreas Huberdab718b2011-07-13 15:45:01 -0700155}
156
Andreas Huber1156dc92011-03-08 15:59:28 -0800157} // namespace android