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 MEDIA_SOURCE_H_
18 
19 #define MEDIA_SOURCE_H_
20 
21 #include <sys/types.h>
22 
23 #include <media/stagefright/MediaErrors.h>
24 #include <utils/RefBase.h>
25 #include <utils/Vector.h>
26 
27 namespace android {
28 
29 class MediaBuffer;
30 class MetaData;
31 
32 struct MediaSource : public virtual RefBase {
33     MediaSource();
34 
35     // To be called before any other methods on this object, except
36     // getFormat().
37     virtual status_t start(MetaData *params = NULL) = 0;
38 
39     // Any blocking read call returns immediately with a result of NO_INIT.
40     // It is an error to call any methods other than start after this call
41     // returns. Any buffers the object may be holding onto at the time of
42     // the stop() call are released.
43     // Also, it is imperative that any buffers output by this object and
44     // held onto by callers be released before a call to stop() !!!
45     virtual status_t stop() = 0;
46 
47     // Returns the format of the data output by this media source.
48     virtual sp<MetaData> getFormat() = 0;
49 
50     struct ReadOptions;
51 
52     // Returns a new buffer of data. Call blocks until a
53     // buffer is available, an error is encountered of the end of the stream
54     // is reached.
55     // End of stream is signalled by a result of ERROR_END_OF_STREAM.
56     // A result of INFO_FORMAT_CHANGED indicates that the format of this
57     // MediaSource has changed mid-stream, the client can continue reading
58     // but should be prepared for buffers of the new configuration.
59     virtual status_t read(
60             MediaBuffer **buffer, const ReadOptions *options = NULL) = 0;
61 
62     // Options that modify read() behaviour. The default is to
63     // a) not request a seek
64     // b) not be late, i.e. lateness_us = 0
65     struct ReadOptions {
66         enum SeekMode {
67             SEEK_PREVIOUS_SYNC,
68             SEEK_NEXT_SYNC,
69             SEEK_CLOSEST_SYNC,
70             SEEK_CLOSEST,
71         };
72 
73         ReadOptions();
74 
75         // Reset everything back to defaults.
76         void reset();
77 
78         void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC);
79         void clearSeekTo();
80         bool getSeekTo(int64_t *time_us, SeekMode *mode) const;
81 
82         void setLateBy(int64_t lateness_us);
83         int64_t getLateBy() const;
84 
85         void setNonBlocking();
86         void clearNonBlocking();
87         bool getNonBlocking() const;
88 
89     private:
90         enum Options {
91             kSeekTo_Option      = 1,
92         };
93 
94         uint32_t mOptions;
95         int64_t mSeekTimeUs;
96         SeekMode mSeekMode;
97         int64_t mLatenessUs;
98         bool mNonBlocking;
99     };
100 
101     // Causes this source to suspend pulling data from its upstream source
102     // until a subsequent read-with-seek. Currently only supported by
103     // OMXCodec.
pauseMediaSource104     virtual status_t pause() {
105         return ERROR_UNSUPPORTED;
106     }
107 
108     // The consumer of this media source requests that the given buffers
109     // are to be returned exclusively in response to read calls.
110     // This will be called after a successful start() and before the
111     // first read() call.
112     // Callee assumes ownership of the buffers if no error is returned.
setBuffersMediaSource113     virtual status_t setBuffers(const Vector<MediaBuffer *> & /* buffers */) {
114         return ERROR_UNSUPPORTED;
115     }
116 
117 protected:
118     virtual ~MediaSource();
119 
120 private:
121     MediaSource(const MediaSource &);
122     MediaSource &operator=(const MediaSource &);
123 };
124 
125 }  // namespace android
126 
127 #endif  // MEDIA_SOURCE_H_
128