ThrottledSource.h revision a15ed9529e70caaf42aae78f9fe530abe38bcc1b
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/stagefright/DataSource.h>
22#include <utils/threads.h>
23
24namespace android {
25
26struct 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
35    virtual String8 getUri() {
36        return String8();
37    }
38
39    // following methods all call through to the wrapped DataSource's methods
40
41    status_t initCheck() const {
42        return mSource->initCheck();
43    }
44
45    virtual status_t getSize(off64_t *size) {
46        return mSource->getSize(size);
47    }
48
49    virtual uint32_t flags() {
50        return mSource->flags();
51    }
52
53    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
61    virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {
62        mSource->getDrmInfo(handle, client);
63    };
64
65    virtual String8 getMIMEType() const {
66        return mSource->getMIMEType();
67    }
68
69private:
70    Mutex mLock;
71
72    sp<DataSource> mSource;
73    int32_t mBandwidthLimitBytesPerSecond;
74    int64_t mStartTimeUs;
75    size_t mTotalTransferred;
76
77    ThrottledSource(const ThrottledSource &);
78    ThrottledSource &operator=(const ThrottledSource &);
79};
80
81}  // namespace android
82
83#endif  // THROTTLED_SOURCE_H_
84