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