DataSource.h revision 69d3d8a9540b0da787ea0beccad2517f057dd54d
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
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    };
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    // May return ERROR_UNSUPPORTED.
75    virtual status_t getSize(off64_t *size);
76
77    virtual uint32_t flags() {
78        return 0;
79    }
80
81    virtual String8 toString() {
82        return String8("<unspecified>");
83    }
84
85    virtual status_t reconnectAtOffset(off64_t offset) {
86        return ERROR_UNSUPPORTED;
87    }
88
89    ////////////////////////////////////////////////////////////////////////////
90
91    bool sniff(String8 *mimeType, float *confidence, sp<AMessage> *meta);
92
93    // The sniffer can optionally fill in "meta" with an AMessage containing
94    // a dictionary of values that helps the corresponding extractor initialize
95    // its state without duplicating effort already exerted by the sniffer.
96    typedef bool (*SnifferFunc)(
97            const sp<DataSource> &source, String8 *mimeType,
98            float *confidence, sp<AMessage> *meta);
99
100    static void RegisterDefaultSniffers();
101
102    // for DRM
103    virtual sp<DecryptHandle> DrmInitialization(const char *mime = NULL) {
104        return NULL;
105    }
106    virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {};
107
108    virtual String8 getUri() {
109        return String8();
110    }
111
112    virtual String8 getMIMEType() const;
113
114    virtual void close() {};
115
116protected:
117    virtual ~DataSource() {}
118
119private:
120    static Mutex gSnifferMutex;
121    static List<SnifferFunc> gSniffers;
122    static bool gSniffersRegistered;
123
124    static void RegisterSniffer_l(SnifferFunc func);
125
126    DataSource(const DataSource &);
127    DataSource &operator=(const DataSource &);
128};
129
130}  // namespace android
131
132#endif  // DATA_SOURCE_H_
133