DataSource.h revision 1b86fe063badb5f28c467ade39be0f4008688947
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 IMediaHTTPService;
35class String8;
36
37class DataSource : public RefBase {
38public:
39    enum Flags {
40        kWantsPrefetching      = 1,
41        kStreamedFromLocalHost = 2,
42        kIsCachingDataSource   = 4,
43        kIsHTTPBasedSource     = 8,
44    };
45
46    static sp<DataSource> CreateFromURI(
47            const sp<IMediaHTTPService> &httpService,
48            const char *uri,
49            const KeyedVector<String8, String8> *headers = NULL);
50
51    DataSource() {}
52
53    virtual status_t initCheck() const = 0;
54
55    virtual ssize_t readAt(off64_t offset, void *data, size_t size) = 0;
56
57    // Convenience methods:
58    bool getUInt16(off64_t offset, uint16_t *x);
59    bool getUInt24(off64_t offset, uint32_t *x); // 3 byte int, returned as a 32-bit int
60    bool getUInt32(off64_t offset, uint32_t *x);
61    bool getUInt64(off64_t offset, uint64_t *x);
62
63    // May return ERROR_UNSUPPORTED.
64    virtual status_t getSize(off64_t *size);
65
66    virtual uint32_t flags() {
67        return 0;
68    }
69
70    virtual status_t reconnectAtOffset(off64_t offset) {
71        return ERROR_UNSUPPORTED;
72    }
73
74    ////////////////////////////////////////////////////////////////////////////
75
76    bool sniff(String8 *mimeType, float *confidence, sp<AMessage> *meta);
77
78    // The sniffer can optionally fill in "meta" with an AMessage containing
79    // a dictionary of values that helps the corresponding extractor initialize
80    // its state without duplicating effort already exerted by the sniffer.
81    typedef bool (*SnifferFunc)(
82            const sp<DataSource> &source, String8 *mimeType,
83            float *confidence, sp<AMessage> *meta);
84
85    static void RegisterDefaultSniffers();
86
87    // for DRM
88    virtual sp<DecryptHandle> DrmInitialization(const char *mime = NULL) {
89        return NULL;
90    }
91    virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {};
92
93    virtual String8 getUri() {
94        return String8();
95    }
96
97    virtual String8 getMIMEType() const;
98
99protected:
100    virtual ~DataSource() {}
101
102private:
103    static Mutex gSnifferMutex;
104    static List<SnifferFunc> gSniffers;
105    static bool gSniffersRegistered;
106
107    static void RegisterSniffer_l(SnifferFunc func);
108
109    DataSource(const DataSource &);
110    DataSource &operator=(const DataSource &);
111};
112
113}  // namespace android
114
115#endif  // DATA_SOURCE_H_
116