DataSource.h revision a3630a418b4f65277a42cd4018cd3b0b7e134d0c
1/*
2 * Copyright (C) 2009 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 DATA_SOURCE_H_
18
19#define DATA_SOURCE_H_
20
21#include <sys/types.h>
22#include <media/stagefright/foundation/ADebug.h>
23#include <media/stagefright/MediaErrors.h>
24#include <utils/Errors.h>
25#include <utils/KeyedVector.h>
26#include <utils/List.h>
27#include <utils/RefBase.h>
28#include <utils/threads.h>
29#include <drm/DrmManagerClient.h>
30
31namespace android {
32
33struct AMessage;
34class String8;
35
36class DataSource : public RefBase {
37public:
38    enum Flags {
39        kWantsPrefetching      = 1,
40        kStreamedFromLocalHost = 2,
41        kIsCachingDataSource   = 4,
42        kIsHTTPBasedSource     = 8,
43    };
44
45    static sp<DataSource> CreateFromURI(
46            const char *uri,
47            const KeyedVector<String8, String8> *headers = NULL);
48
49    DataSource() {}
50
51    virtual status_t initCheck() const = 0;
52
53    virtual ssize_t readAt(off64_t offset, void *data, size_t size) = 0;
54
55    // Convenience methods:
56    bool getUInt16(off64_t offset, uint16_t *x);
57    bool getUInt24(off64_t offset, uint32_t *x); // 3 byte int, returned as a 32-bit int
58    bool getUInt32(off64_t offset, uint32_t *x);
59    bool getUInt64(off64_t offset, uint64_t *x);
60
61    // Reads in "count" entries of type T into vector *x.
62    // Returns true if "count" entries can be read.
63    // If fewer than "count" entries can be read, return false. In this case,
64    // the output vector *x will still have those entries that were read. Call
65    // x->size() to obtain the number of entries read.
66    // The optional parameter chunkSize specifies how many entries should be
67    // read from the data source at one time into a temporary buffer. Increasing
68    // chunkSize can improve the performance at the cost of extra memory usage.
69    // The default value for chunkSize is set to read at least 4k bytes at a
70    // time, depending on sizeof(T).
71    template <typename T>
72    bool getVector(off64_t offset, Vector<T>* x, size_t count,
73                   size_t chunkSize = (4095 / sizeof(T)) + 1);
74
75    // May return ERROR_UNSUPPORTED.
76    virtual status_t getSize(off64_t *size);
77
78    virtual uint32_t flags() {
79        return 0;
80    }
81
82    virtual status_t reconnectAtOffset(off64_t offset) {
83        return ERROR_UNSUPPORTED;
84    }
85
86    ////////////////////////////////////////////////////////////////////////////
87
88    bool sniff(String8 *mimeType, float *confidence, sp<AMessage> *meta);
89
90    // The sniffer can optionally fill in "meta" with an AMessage containing
91    // a dictionary of values that helps the corresponding extractor initialize
92    // its state without duplicating effort already exerted by the sniffer.
93    typedef bool (*SnifferFunc)(
94            const sp<DataSource> &source, String8 *mimeType,
95            float *confidence, sp<AMessage> *meta);
96
97    static void RegisterDefaultSniffers();
98
99    // for DRM
100    virtual sp<DecryptHandle> DrmInitialization(const char *mime = NULL) {
101        return NULL;
102    }
103    virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {};
104
105    virtual String8 getUri() {
106        return String8();
107    }
108
109    virtual String8 getMIMEType() const;
110
111protected:
112    virtual ~DataSource() {}
113
114private:
115    static Mutex gSnifferMutex;
116    static List<SnifferFunc> gSniffers;
117    static bool gSniffersRegistered;
118
119    static void RegisterSniffer_l(SnifferFunc func);
120
121    DataSource(const DataSource &);
122    DataSource &operator=(const DataSource &);
123};
124
125template <typename T>
126bool DataSource::getVector(off64_t offset, Vector<T>* x, size_t count,
127                           size_t chunkSize)
128{
129    x->clear();
130    if (chunkSize == 0) {
131        return false;
132    }
133    if (count == 0) {
134        return true;
135    }
136
137    T tmp[chunkSize];
138    ssize_t numBytesRead;
139    size_t numBytesPerChunk = chunkSize * sizeof(T);
140    size_t i;
141
142    for (i = 0; i + chunkSize < count; i += chunkSize) {
143        // This loops is executed when more than chunkSize records need to be
144        // read.
145        numBytesRead = this->readAt(offset, (void*)&tmp, numBytesPerChunk);
146        if (numBytesRead == -1) { // If readAt() returns -1, there is an error.
147            return false;
148        }
149        if (numBytesRead < numBytesPerChunk) {
150            // This case is triggered when the stream ends before the whole
151            // chunk is read.
152            x->appendArray(tmp, (size_t)numBytesRead / sizeof(T));
153            return false;
154        }
155        x->appendArray(tmp, chunkSize);
156        offset += numBytesPerChunk;
157    }
158
159    // There are (count - i) more records to read.
160    // Right now, (count - i) <= chunkSize.
161    // We do the same thing as above, but with chunkSize replaced by count - i.
162    numBytesRead = this->readAt(offset, (void*)&tmp, (count - i) * sizeof(T));
163    if (numBytesRead == -1) {
164        return false;
165    }
166    x->appendArray(tmp, (size_t)numBytesRead / sizeof(T));
167    return x->size() == count;
168}
169
170}  // namespace android
171
172#endif  // DATA_SOURCE_H_
173