blob: 3c5a8a52b82bcfb7c177db8ddb0004aa85321bfb [file] [log] [blame]
Andreas Huber5f5719e2011-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 Donga9d0feb2011-05-25 19:37:03 -070017//#define LOG_NDEBUG 0
18#define LOG_TAG "HTTPBase"
19#include <utils/Log.h>
20
Andreas Huber5f5719e2011-03-08 15:59:28 -080021#include "include/HTTPBase.h"
22
Andreas Huber92b94c72011-03-11 08:47:51 -080023#if CHROMIUM_AVAILABLE
Andreas Huber5f5719e2011-03-08 15:59:28 -080024#include "include/ChromiumHTTPDataSource.h"
Andreas Huber92b94c72011-03-11 08:47:51 -080025#endif
26
Andreas Huberc3119332011-07-13 15:45:01 -070027#include <media/stagefright/foundation/ADebug.h>
James Donga9d0feb2011-05-25 19:37:03 -070028#include <media/stagefright/foundation/ALooper.h>
Andreas Huberc3119332011-07-13 15:45:01 -070029
Andreas Huber5f5719e2011-03-08 15:59:28 -080030#include <cutils/properties.h>
Andreas Huberc3119332011-07-13 15:45:01 -070031#include <cutils/qtaguid.h>
Andreas Huber5f5719e2011-03-08 15:59:28 -080032
33namespace android {
34
James Donga9d0feb2011-05-25 19:37:03 -070035HTTPBase::HTTPBase()
36 : mNumBandwidthHistoryItems(0),
37 mTotalTransferTimeUs(0),
38 mTotalTransferBytes(0),
39 mPrevBandwidthMeasureTimeUs(0),
40 mPrevEstimatedBandWidthKbps(0),
Andreas Huber603d7392011-06-30 15:47:02 -070041 mBandWidthCollectFreqMs(5000),
Ashish Sharmad5a20d82011-07-07 17:57:05 -070042 mUIDValid(false),
43 mUID(0) {
James Donga9d0feb2011-05-25 19:37:03 -070044}
Andreas Huber5f5719e2011-03-08 15:59:28 -080045
46// static
47sp<HTTPBase> HTTPBase::Create(uint32_t flags) {
Andreas Huber92b94c72011-03-11 08:47:51 -080048#if CHROMIUM_AVAILABLE
Andreas Huber5f5719e2011-03-08 15:59:28 -080049 return new ChromiumHTTPDataSource(flags);
Andreas Huber92b94c72011-03-11 08:47:51 -080050#endif
51 {
Andreas Huberc3119332011-07-13 15:45:01 -070052 TRESPASS();
53
54 return NULL;
Andreas Huber5f5719e2011-03-08 15:59:28 -080055 }
56}
57
James Donga9d0feb2011-05-25 19:37:03 -070058void HTTPBase::addBandwidthMeasurement(
59 size_t numBytes, int64_t delayUs) {
60 Mutex::Autolock autoLock(mLock);
61
62 BandwidthEntry entry;
63 entry.mDelayUs = delayUs;
64 entry.mNumBytes = numBytes;
65 mTotalTransferTimeUs += delayUs;
66 mTotalTransferBytes += numBytes;
67
68 mBandwidthHistory.push_back(entry);
69 if (++mNumBandwidthHistoryItems > 100) {
70 BandwidthEntry *entry = &*mBandwidthHistory.begin();
71 mTotalTransferTimeUs -= entry->mDelayUs;
72 mTotalTransferBytes -= entry->mNumBytes;
73 mBandwidthHistory.erase(mBandwidthHistory.begin());
74 --mNumBandwidthHistoryItems;
75
76 int64_t timeNowUs = ALooper::GetNowUs();
77 if (timeNowUs - mPrevBandwidthMeasureTimeUs >=
78 mBandWidthCollectFreqMs * 1000LL) {
79
80 if (mPrevBandwidthMeasureTimeUs != 0) {
81 mPrevEstimatedBandWidthKbps =
82 (mTotalTransferBytes * 8E3 / mTotalTransferTimeUs);
83 }
84 mPrevBandwidthMeasureTimeUs = timeNowUs;
85 }
86 }
87
88}
89
90bool HTTPBase::estimateBandwidth(int32_t *bandwidth_bps) {
91 Mutex::Autolock autoLock(mLock);
92
93 if (mNumBandwidthHistoryItems < 2) {
94 return false;
95 }
96
97 *bandwidth_bps = ((double)mTotalTransferBytes * 8E6 / mTotalTransferTimeUs);
98
99 return true;
100}
101
102status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) {
103 Mutex::Autolock autoLock(mLock);
104 *kbps = mPrevEstimatedBandWidthKbps;
105 return OK;
106}
107
108status_t HTTPBase::setBandwidthStatCollectFreq(int32_t freqMs) {
109 Mutex::Autolock autoLock(mLock);
110
111 if (freqMs < kMinBandwidthCollectFreqMs
112 || freqMs > kMaxBandwidthCollectFreqMs) {
113
114 LOGE("frequency (%d ms) is out of range [1000, 60000]", freqMs);
115 return BAD_VALUE;
116 }
117
118 LOGI("frequency set to %d ms", freqMs);
119 mBandWidthCollectFreqMs = freqMs;
120 return OK;
121}
122
Andreas Huber603d7392011-06-30 15:47:02 -0700123void HTTPBase::setUID(uid_t uid) {
124 mUIDValid = true;
125 mUID = uid;
126}
127
128bool HTTPBase::getUID(uid_t *uid) const {
129 if (!mUIDValid) {
130 return false;
131 }
132
133 *uid = mUID;
134
135 return true;
136}
137
Andreas Huberc3119332011-07-13 15:45:01 -0700138// static
Ashish Sharmad5a20d82011-07-07 17:57:05 -0700139void HTTPBase::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) {
140 int res = qtaguid_tagSocket(sockfd, kTag, uid);
141 if (res != 0) {
142 LOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid());
143 }
144}
145
146// static
147void HTTPBase::UnRegisterSocketUserTag(int sockfd) {
148 int res = qtaguid_untagSocket(sockfd);
149 if (res != 0) {
150 LOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid());
151 }
Andreas Huberc3119332011-07-13 15:45:01 -0700152}
153
Andreas Huber5f5719e2011-03-08 15:59:28 -0800154} // namespace android