DataSource.h revision 3de157dd8f9cd45bf9b0406268f5830887105ae1
15f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)/*
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Copyright (C) 2009 The Android Open Source Project
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
55f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * you may not use this file except in compliance with the License.
65f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) * You may obtain a copy of the License at
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * See the License for the specific language governing permissions and
1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) * limitations under the License.
1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) */
1603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#ifndef DATA_SOURCE_H_
18c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
19c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#define DATA_SOURCE_H_
20c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sys/types.h>
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <media/stagefright/MediaErrors.h>
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/Errors.h>
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/KeyedVector.h>
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/List.h>
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/RefBase.h>
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <utils/threads.h>
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <drm/DrmManagerClient.h>
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace android {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochstruct AMessage;
34c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochstruct AString;
35c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochstruct IMediaHTTPService;
36c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass String8;
37c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass DataSource : public RefBase {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)public:
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    enum Flags {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        kWantsPrefetching      = 1,
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        kStreamedFromLocalHost = 2,
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        kIsCachingDataSource   = 4,
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        kIsHTTPBasedSource     = 8,
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    };
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    static sp<DataSource> CreateFromURI(
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            const sp<IMediaHTTPService> &httpService,
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)            const char *uri,
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            const KeyedVector<String8, String8> *headers = NULL,
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            AString *sniffedMIME = NULL);
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    DataSource() {}
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    virtual status_t initCheck() const = 0;
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    virtual ssize_t readAt(off64_t offset, void *data, size_t size) = 0;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    // Convenience methods:
6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    bool getUInt16(off64_t offset, uint16_t *x);
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool getUInt24(off64_t offset, uint32_t *x); // 3 byte int, returned as a 32-bit int
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool getUInt32(off64_t offset, uint32_t *x);
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    bool getUInt64(off64_t offset, uint64_t *x);
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // May return ERROR_UNSUPPORTED.
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual status_t getSize(off64_t *size);
6703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
6803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    virtual uint32_t flags() {
6903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        return 0;
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    virtual status_t reconnectAtOffset(off64_t offset) {
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        return ERROR_UNSUPPORTED;
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ////////////////////////////////////////////////////////////////////////////
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    bool sniff(String8 *mimeType, float *confidence, sp<AMessage> *meta);
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // The sniffer can optionally fill in "meta" with an AMessage containing
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // a dictionary of values that helps the corresponding extractor initialize
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    // its state without duplicating effort already exerted by the sniffer.
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    typedef bool (*SnifferFunc)(
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            const sp<DataSource> &source, String8 *mimeType,
85            float *confidence, sp<AMessage> *meta);
86
87    static void RegisterDefaultSniffers();
88
89    // for DRM
90    virtual sp<DecryptHandle> DrmInitialization(const char *mime = NULL) {
91        return NULL;
92    }
93    virtual void getDrmInfo(sp<DecryptHandle> &handle, DrmManagerClient **client) {};
94
95    virtual String8 getUri() {
96        return String8();
97    }
98
99    virtual String8 getMIMEType() const;
100
101protected:
102    virtual ~DataSource() {}
103
104private:
105    enum {
106        kDefaultMetaSize = 200000,
107    };
108
109    static Mutex gSnifferMutex;
110    static List<SnifferFunc> gSniffers;
111    static bool gSniffersRegistered;
112
113    static void RegisterSniffer_l(SnifferFunc func);
114
115    DataSource(const DataSource &);
116    DataSource &operator=(const DataSource &);
117};
118
119}  // namespace android
120
121#endif  // DATA_SOURCE_H_
122