Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 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 IMEDIA_SOURCE_BASE_H_ |
| 18 | |
| 19 | #define IMEDIA_SOURCE_BASE_H_ |
| 20 | |
| 21 | #include <binder/IInterface.h> |
| 22 | #include <media/stagefright/MediaErrors.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | struct MediaSource; |
| 27 | class MetaData; |
| 28 | class MediaBuffer; |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 29 | class MediaBufferGroup; |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 30 | |
| 31 | class IMediaSource : public IInterface { |
| 32 | public: |
| 33 | DECLARE_META_INTERFACE(MediaSource); |
| 34 | |
Wei Jia | 1f1fc45 | 2016-05-11 16:17:22 -0700 | [diff] [blame] | 35 | enum { |
| 36 | // Maximum number of buffers would be read in readMultiple. |
| 37 | kMaxNumReadMultiple = 128, |
| 38 | }; |
| 39 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 40 | // To be called before any other methods on this object, except |
| 41 | // getFormat(). |
| 42 | virtual status_t start(MetaData *params = NULL) = 0; |
| 43 | |
| 44 | // Any blocking read call returns immediately with a result of NO_INIT. |
| 45 | // It is an error to call any methods other than start after this call |
| 46 | // returns. Any buffers the object may be holding onto at the time of |
| 47 | // the stop() call are released. |
| 48 | // Also, it is imperative that any buffers output by this object and |
| 49 | // held onto by callers be released before a call to stop() !!! |
| 50 | virtual status_t stop() = 0; |
| 51 | |
| 52 | // Returns the format of the data output by this media source. |
| 53 | virtual sp<MetaData> getFormat() = 0; |
| 54 | |
| 55 | // Options that modify read() behaviour. The default is to |
| 56 | // a) not request a seek |
| 57 | // b) not be late, i.e. lateness_us = 0 |
| 58 | struct ReadOptions { |
| 59 | enum SeekMode { |
| 60 | SEEK_PREVIOUS_SYNC, |
| 61 | SEEK_NEXT_SYNC, |
| 62 | SEEK_CLOSEST_SYNC, |
| 63 | SEEK_CLOSEST, |
| 64 | }; |
| 65 | |
| 66 | ReadOptions(); |
| 67 | |
| 68 | // Reset everything back to defaults. |
| 69 | void reset(); |
| 70 | |
| 71 | void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC); |
| 72 | void clearSeekTo(); |
| 73 | bool getSeekTo(int64_t *time_us, SeekMode *mode) const; |
| 74 | |
| 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 |
Wei Jia | 1f1fc45 | 2016-05-11 16:17:22 -0700 | [diff] [blame] | 95 | // buffer is available, an error is encountered or the end of the stream |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 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 | |
Wei Jia | 1f1fc45 | 2016-05-11 16:17:22 -0700 | [diff] [blame] | 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 | |
Wei Jia | d3f4e14 | 2016-06-13 14:51:43 -0700 | [diff] [blame^] | 117 | // Returns true if |readMultiple| is supported, otherwise false. |
| 118 | virtual bool supportReadMultiple() = 0; |
| 119 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 120 | // Causes this source to suspend pulling data from its upstream source |
| 121 | // until a subsequent read-with-seek. Currently only supported by |
| 122 | // OMXCodec. |
| 123 | virtual status_t pause() = 0; |
| 124 | |
| 125 | // The consumer of this media source requests that the given buffers |
| 126 | // are to be returned exclusively in response to read calls. |
| 127 | // This will be called after a successful start() and before the |
| 128 | // first read() call. |
| 129 | // Callee assumes ownership of the buffers if no error is returned. |
| 130 | virtual status_t setBuffers(const Vector<MediaBuffer *> & /* buffers */) = 0; |
| 131 | |
| 132 | }; |
| 133 | |
| 134 | class BnMediaSource: public BnInterface<IMediaSource> |
| 135 | { |
| 136 | public: |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 137 | BnMediaSource(); |
| 138 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 139 | virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, |
| 140 | uint32_t flags = 0); |
| 141 | |
| 142 | virtual status_t pause() { |
| 143 | return ERROR_UNSUPPORTED; |
| 144 | } |
| 145 | |
| 146 | virtual status_t setBuffers(const Vector<MediaBuffer *> & /* buffers */) { |
| 147 | return ERROR_UNSUPPORTED; |
| 148 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 149 | |
Wei Jia | 1f1fc45 | 2016-05-11 16:17:22 -0700 | [diff] [blame] | 150 | virtual status_t readMultiple( |
| 151 | Vector<MediaBuffer *> * /* buffers */, uint32_t /* maxNumBuffers = 1 */) { |
| 152 | return ERROR_UNSUPPORTED; |
| 153 | } |
Wei Jia | d3f4e14 | 2016-06-13 14:51:43 -0700 | [diff] [blame^] | 154 | |
| 155 | virtual bool supportReadMultiple() { |
| 156 | return false; |
| 157 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 158 | protected: |
| 159 | virtual ~BnMediaSource(); |
| 160 | |
| 161 | private: |
| 162 | MediaBufferGroup *mGroup; |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | |
| 166 | } // namespace android |
| 167 | |
| 168 | #endif // IMEDIA_SOURCE_BASE_H_ |