• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HTTP_BASE_H_
18 
19 #define HTTP_BASE_H_
20 
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/DataSource.h>
23 #include <media/stagefright/MediaErrors.h>
24 #include <utils/threads.h>
25 
26 namespace android {
27 
28 struct HTTPBase : public DataSource {
29     enum Flags {
30         // Don't log any URLs.
31         kFlagIncognito = 1
32     };
33 
34     HTTPBase();
35 
36     virtual status_t connect(
37             const char *uri,
38             const KeyedVector<String8, String8> *headers = NULL,
39             off64_t offset = 0) = 0;
40 
41     virtual void disconnect() = 0;
42 
43     // Returns true if bandwidth could successfully be estimated,
44     // false otherwise.
45     virtual bool estimateBandwidth(int32_t *bandwidth_bps);
46 
47     virtual status_t getEstimatedBandwidthKbps(int32_t *kbps);
48 
49     virtual status_t setBandwidthStatCollectFreq(int32_t freqMs);
50 
51     static status_t UpdateProxyConfig(
52             const char *host, int32_t port, const char *exclusionList);
53 
54     void setUID(uid_t uid);
55     bool getUID(uid_t *uid) const;
56 
57     static sp<HTTPBase> Create(uint32_t flags = 0);
58 
59     static void RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag);
60     static void UnRegisterSocketUserTag(int sockfd);
61 
62 protected:
63     void addBandwidthMeasurement(size_t numBytes, int64_t delayUs);
64 
65 private:
66     struct BandwidthEntry {
67         int64_t mDelayUs;
68         size_t mNumBytes;
69     };
70 
71     Mutex mLock;
72 
73     List<BandwidthEntry> mBandwidthHistory;
74     size_t mNumBandwidthHistoryItems;
75     int64_t mTotalTransferTimeUs;
76     size_t mTotalTransferBytes;
77 
78     enum {
79         kMinBandwidthCollectFreqMs = 1000,   // 1 second
80         kMaxBandwidthCollectFreqMs = 60000,  // one minute
81     };
82 
83     int64_t mPrevBandwidthMeasureTimeUs;
84     int32_t mPrevEstimatedBandWidthKbps;
85     int32_t mBandWidthCollectFreqMs;
86 
87     bool mUIDValid;
88     uid_t mUID;
89 
90     DISALLOW_EVIL_CONSTRUCTORS(HTTPBase);
91 };
92 
93 }  // namespace android
94 
95 #endif  // HTTP_BASE_H_
96