IMediaSource.h revision 1f1fc459ddb67d1162f2dbb10d14e57f42841da2
17a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker/*
27a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * Copyright (C) 2009 The Android Open Source Project
37a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker *
47a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * Licensed under the Apache License, Version 2.0 (the "License");
57a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * you may not use this file except in compliance with the License.
67a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * You may obtain a copy of the License at
77a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker *
87a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker *      http://www.apache.org/licenses/LICENSE-2.0
97a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker *
107a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * Unless required by applicable law or agreed to in writing, software
117a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * distributed under the License is distributed on an "AS IS" BASIS,
127a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * See the License for the specific language governing permissions and
147a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker * limitations under the License.
157a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker */
167a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
177a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker#ifndef IMEDIA_SOURCE_BASE_H_
187a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
1945ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker#define IMEDIA_SOURCE_BASE_H_
2045ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker
2145ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker#include <binder/IInterface.h>
2245ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker#include <media/stagefright/MediaErrors.h>
237a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
247a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubakernamespace android {
257a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
267a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubakerstruct MediaSource;
277a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubakerclass MetaData;
287a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubakerclass MediaBuffer;
297a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubakerclass MediaBufferGroup;
307a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
317a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubakerclass IMediaSource : public IInterface {
327a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubakerpublic:
337a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    DECLARE_META_INTERFACE(MediaSource);
347a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
357a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    enum {
367a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker        // Maximum number of buffers would be read in readMultiple.
377a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker        kMaxNumReadMultiple = 128,
387a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    };
397a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
407a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // To be called before any other methods on this object, except
417a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // getFormat().
427a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    virtual status_t start(MetaData *params = NULL) = 0;
437a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
447a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // Any blocking read call returns immediately with a result of NO_INIT.
457a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // It is an error to call any methods other than start after this call
467a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // returns. Any buffers the object may be holding onto at the time of
477a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // the stop() call are released.
487a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // Also, it is imperative that any buffers output by this object and
497a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // held onto by callers be released before a call to stop() !!!
507a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    virtual status_t stop() = 0;
517a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
527a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // Returns the format of the data output by this media source.
537a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    virtual sp<MetaData> getFormat() = 0;
547a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
557a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // Options that modify read() behaviour. The default is to
567a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // a) not request a seek
577a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    // b) not be late, i.e. lateness_us = 0
587a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker    struct ReadOptions {
5945ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        enum SeekMode {
6045ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker            SEEK_PREVIOUS_SYNC,
6145ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker            SEEK_NEXT_SYNC,
6245ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker            SEEK_CLOSEST_SYNC,
6345ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker            SEEK_CLOSEST,
6445ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        };
6545ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker
6645ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        ReadOptions();
6745ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker
6845ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        // Reset everything back to defaults.
6945ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        void reset();
7045ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker
7145ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC);
7245ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        void clearSeekTo();
7345ff13ea28005b5af0caa80dbdeb09d49bd73fafChad Brubaker        bool getSeekTo(int64_t *time_us, SeekMode *mode) const;
747a2c973db7756a60f1cdd6cf67411115c1576081Chad Brubaker
75        void setLateBy(int64_t lateness_us);
76        int64_t getLateBy() const;
77
78        void setNonBlocking();
79        void clearNonBlocking();
80        bool getNonBlocking() const;
81
82    private:
83        enum Options {
84            kSeekTo_Option      = 1,
85        };
86
87        uint32_t mOptions;
88        int64_t mSeekTimeUs;
89        SeekMode mSeekMode;
90        int64_t mLatenessUs;
91        bool mNonBlocking;
92    };
93
94    // Returns a new buffer of data. Call blocks until a
95    // buffer is available, an error is encountered or the end of the stream
96    // is reached.
97    // End of stream is signalled by a result of ERROR_END_OF_STREAM.
98    // A result of INFO_FORMAT_CHANGED indicates that the format of this
99    // MediaSource has changed mid-stream, the client can continue reading
100    // but should be prepared for buffers of the new configuration.
101    virtual status_t read(
102            MediaBuffer **buffer, const ReadOptions *options = NULL) = 0;
103
104    // Returns a vector of new buffers of data. The vector size could be
105    // <= |maxNumBuffers|. Used for buffers with small size
106    // since all buffer data are passed back by binder, not shared memory.
107    // Call blocks until an error is encountered, or the end of the stream is
108    // reached, or format change is hit, or |kMaxNumReadMultiple| buffers have
109    // been read.
110    // End of stream is signalled by a result of ERROR_END_OF_STREAM.
111    // A result of INFO_FORMAT_CHANGED indicates that the format of this
112    // MediaSource has changed mid-stream, the client can continue reading
113    // but should be prepared for buffers of the new configuration.
114    virtual status_t readMultiple(
115            Vector<MediaBuffer *> *buffers, uint32_t maxNumBuffers = 1) = 0;
116
117    // Causes this source to suspend pulling data from its upstream source
118    // until a subsequent read-with-seek. Currently only supported by
119    // OMXCodec.
120    virtual status_t pause()  = 0;
121
122    // The consumer of this media source requests that the given buffers
123    // are to be returned exclusively in response to read calls.
124    // This will be called after a successful start() and before the
125    // first read() call.
126    // Callee assumes ownership of the buffers if no error is returned.
127    virtual status_t setBuffers(const Vector<MediaBuffer *> & /* buffers */) = 0;
128
129};
130
131class BnMediaSource: public BnInterface<IMediaSource>
132{
133public:
134    BnMediaSource();
135
136    virtual status_t    onTransact(uint32_t code, const Parcel& data, Parcel* reply,
137                                uint32_t flags = 0);
138
139    virtual status_t pause() {
140        return ERROR_UNSUPPORTED;
141    }
142
143    virtual status_t setBuffers(const Vector<MediaBuffer *> & /* buffers */) {
144        return ERROR_UNSUPPORTED;
145    }
146
147    virtual status_t readMultiple(
148            Vector<MediaBuffer *> * /* buffers */, uint32_t /* maxNumBuffers = 1 */) {
149        return ERROR_UNSUPPORTED;
150    }
151protected:
152    virtual ~BnMediaSource();
153
154private:
155    MediaBufferGroup *mGroup;
156};
157
158
159}  // namespace android
160
161#endif  // IMEDIA_SOURCE_BASE_H_
162