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 
31 namespace android {
32 
33 struct AMessage;
34 struct AString;
35 class  IDataSource;
36 struct IMediaHTTPService;
37 class String8;
38 struct HTTPBase;
39 
40 class DataSource : public RefBase {
41 public:
42     enum Flags {
43         kWantsPrefetching      = 1,
44         kStreamedFromLocalHost = 2,
45         kIsCachingDataSource   = 4,
46         kIsHTTPBasedSource     = 8,
47         kIsLocalFileSource     = 16,
48     };
49 
50     static sp<DataSource> CreateFromURI(
51             const sp<IMediaHTTPService> &httpService,
52             const char *uri,
53             const KeyedVector<String8, String8> *headers = NULL,
54             String8 *contentType = NULL,
55             HTTPBase *httpSource = NULL);
56 
57     static sp<DataSource> CreateMediaHTTP(const sp<IMediaHTTPService> &httpService);
58     static sp<DataSource> CreateFromIDataSource(const sp<IDataSource> &source);
59     static sp<DataSource> CreateFromFd(int fd, int64_t offset, int64_t length);
60 
DataSource()61     DataSource() {}
62 
63     virtual status_t initCheck() const = 0;
64 
65     // Returns the number of bytes read, or -1 on failure. It's not an error if
66     // this returns zero; it just means the given offset is equal to, or
67     // beyond, the end of the source.
68     virtual ssize_t readAt(off64_t offset, void *data, size_t size) = 0;
69 
70     // Convenience methods:
71     bool getUInt16(off64_t offset, uint16_t *x);
72     bool getUInt24(off64_t offset, uint32_t *x); // 3 byte int, returned as a 32-bit int
73     bool getUInt32(off64_t offset, uint32_t *x);
74     bool getUInt64(off64_t offset, uint64_t *x);
75 
76     // Reads in "count" entries of type T into vector *x.
77     // Returns true if "count" entries can be read.
78     // If fewer than "count" entries can be read, return false. In this case,
79     // the output vector *x will still have those entries that were read. Call
80     // x->size() to obtain the number of entries read.
81     // The optional parameter chunkSize specifies how many entries should be
82     // read from the data source at one time into a temporary buffer. Increasing
83     // chunkSize can improve the performance at the cost of extra memory usage.
84     // The default value for chunkSize is set to read at least 4k bytes at a
85     // time, depending on sizeof(T).
86     template <typename T>
87     bool getVector(off64_t offset, Vector<T>* x, size_t count,
88                    size_t chunkSize = (4095 / sizeof(T)) + 1);
89 
90     // May return ERROR_UNSUPPORTED.
91     virtual status_t getSize(off64_t *size);
92 
flags()93     virtual uint32_t flags() {
94         return 0;
95     }
96 
toString()97     virtual String8 toString() {
98         return String8("<unspecified>");
99     }
100 
reconnectAtOffset(off64_t)101     virtual status_t reconnectAtOffset(off64_t /*offset*/) {
102         return ERROR_UNSUPPORTED;
103     }
104 
105     ////////////////////////////////////////////////////////////////////////////
106 
107     // for DRM
108     virtual sp<DecryptHandle> DrmInitialization(const char * /*mime*/ = NULL) {
109         return NULL;
110     }
getDrmInfo(sp<DecryptHandle> &,DrmManagerClient **)111     virtual void getDrmInfo(sp<DecryptHandle> &/*handle*/, DrmManagerClient ** /*client*/) {};
112 
getUri()113     virtual String8 getUri() {
114         return String8();
115     }
116 
117     virtual String8 getMIMEType() const;
118 
close()119     virtual void close() {};
120 
121     // creates an IDataSource wrapper to the DataSource.
122     virtual sp<IDataSource> asIDataSource();
123 
124     // returns a pointer to IDataSource if it is wrapped.
125     virtual sp<IDataSource> getIDataSource() const;
126 
127 protected:
~DataSource()128     virtual ~DataSource() {}
129 
130 private:
131     DataSource(const DataSource &);
132     DataSource &operator=(const DataSource &);
133 };
134 
135 template <typename T>
getVector(off64_t offset,Vector<T> * x,size_t count,size_t chunkSize)136 bool DataSource::getVector(off64_t offset, Vector<T>* x, size_t count,
137                            size_t chunkSize)
138 {
139     x->clear();
140     if (chunkSize == 0) {
141         return false;
142     }
143     if (count == 0) {
144         return true;
145     }
146 
147     T tmp[chunkSize];
148     ssize_t numBytesRead;
149     size_t numBytesPerChunk = chunkSize * sizeof(T);
150     size_t i;
151 
152     for (i = 0; i + chunkSize < count; i += chunkSize) {
153         // This loops is executed when more than chunkSize records need to be
154         // read.
155         numBytesRead = this->readAt(offset, (void*)&tmp, numBytesPerChunk);
156         if (numBytesRead == -1) { // If readAt() returns -1, there is an error.
157             return false;
158         }
159         if (numBytesRead < numBytesPerChunk) {
160             // This case is triggered when the stream ends before the whole
161             // chunk is read.
162             x->appendArray(tmp, (size_t)numBytesRead / sizeof(T));
163             return false;
164         }
165         x->appendArray(tmp, chunkSize);
166         offset += numBytesPerChunk;
167     }
168 
169     // There are (count - i) more records to read.
170     // Right now, (count - i) <= chunkSize.
171     // We do the same thing as above, but with chunkSize replaced by count - i.
172     numBytesRead = this->readAt(offset, (void*)&tmp, (count - i) * sizeof(T));
173     if (numBytesRead == -1) {
174         return false;
175     }
176     x->appendArray(tmp, (size_t)numBytesRead / sizeof(T));
177     return x->size() == count;
178 }
179 
180 }  // namespace android
181 
182 #endif  // DATA_SOURCE_H_
183