1 /*
2  * Copyright (C) 2010 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 THROTTLED_SOURCE_H_
18 
19 #define THROTTLED_SOURCE_H_
20 
21 #include <media/DataSource.h>
22 #include <utils/threads.h>
23 
24 namespace android {
25 
26 struct ThrottledSource : public DataSource {
27     ThrottledSource(
28             const sp<DataSource> &source,
29             int32_t bandwidthLimitBytesPerSecond);
30 
31     // implementation of readAt() that sleeps to achieve the desired max throughput
32     virtual ssize_t readAt(off64_t offset, void *data, size_t size);
33 
34     // returns an empty string to prevent callers from using the Uri to construct a new datasource
getUriThrottledSource35     virtual String8 getUri() {
36         return String8();
37     }
38 
39     // following methods all call through to the wrapped DataSource's methods
40 
initCheckThrottledSource41     status_t initCheck() const {
42         return mSource->initCheck();
43     }
44 
getSizeThrottledSource45     virtual status_t getSize(off64_t *size) {
46         return mSource->getSize(size);
47     }
48 
flagsThrottledSource49     virtual uint32_t flags() {
50         return mSource->flags();
51     }
52 
reconnectAtOffsetThrottledSource53     virtual status_t reconnectAtOffset(off64_t offset) {
54         return mSource->reconnectAtOffset(offset);
55     }
56 
57     virtual sp<DecryptHandle> DrmInitialization(const char *mime = NULL) {
58         return mSource->DrmInitialization(mime);
59     }
60 
getMIMETypeThrottledSource61     virtual String8 getMIMEType() const {
62         return mSource->getMIMEType();
63     }
64 
65 private:
66     Mutex mLock;
67 
68     sp<DataSource> mSource;
69     int32_t mBandwidthLimitBytesPerSecond;
70     int64_t mStartTimeUs;
71     size_t mTotalTransferred;
72 
73     ThrottledSource(const ThrottledSource &);
74     ThrottledSource &operator=(const ThrottledSource &);
75 };
76 
77 }  // namespace android
78 
79 #endif  // THROTTLED_SOURCE_H_
80