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