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