Home | History | Annotate | Download | only in libstagefright
      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/ChromiumHTTPDataSource.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 
     33 namespace android {
     34 
     35 HTTPBase::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
     47 sp<HTTPBase> HTTPBase::Create(uint32_t flags) {
     48 #if CHROMIUM_AVAILABLE
     49         return new ChromiumHTTPDataSource(flags);
     50 #endif
     51     {
     52         TRESPASS();
     53 
     54         return NULL;
     55     }
     56 }
     57 
     58 void 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 
     90 bool 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 
    102 status_t HTTPBase::getEstimatedBandwidthKbps(int32_t *kbps) {
    103     Mutex::Autolock autoLock(mLock);
    104     *kbps = mPrevEstimatedBandWidthKbps;
    105     return OK;
    106 }
    107 
    108 status_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 
    123 void HTTPBase::setUID(uid_t uid) {
    124     mUIDValid = true;
    125     mUID = uid;
    126 }
    127 
    128 bool HTTPBase::getUID(uid_t *uid) const {
    129     if (!mUIDValid) {
    130         return false;
    131     }
    132 
    133     *uid = mUID;
    134 
    135     return true;
    136 }
    137 
    138 // static
    139 void 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
    147 void 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     }
    152 }
    153 
    154 }  // namespace android
    155