Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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_BASE_H_ |
| 18 | |
| 19 | #define MEDIA_SOURCE_BASE_H_ |
| 20 | |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <binder/IMemory.h> |
| 24 | #include <binder/MemoryDealer.h> |
Marco Nelissen | 56f1938 | 2018-09-12 15:30:59 -0700 | [diff] [blame] | 25 | #include <media/MediaExtractorPluginApi.h> |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 26 | #include <media/stagefright/MediaErrors.h> |
| 27 | #include <media/stagefright/MetaData.h> |
Marco Nelissen | 2a3363a | 2018-09-13 13:15:30 -0700 | [diff] [blame] | 28 | #include <media/MediaExtractorPluginApi.h> |
Dongwon Kang | 42e497d | 2018-01-30 06:12:32 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 30 | #include <utils/RefBase.h> |
| 31 | #include <utils/Vector.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 35 | class MediaBufferBase; |
Marco Nelissen | 2a3363a | 2018-09-13 13:15:30 -0700 | [diff] [blame] | 36 | struct CMediaTrack; |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 37 | |
| 38 | class SourceBaseAllocTracker { |
| 39 | public: |
| 40 | SourceBaseAllocTracker() { |
| 41 | ALOGD("sourcebase allocated: %p", this); |
| 42 | } |
| 43 | virtual ~SourceBaseAllocTracker() { |
| 44 | ALOGD("sourcebase freed: %p", this); |
| 45 | } |
| 46 | }; |
| 47 | |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 48 | struct MediaTrack |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 49 | // : public SourceBaseAllocTracker |
| 50 | { |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 51 | MediaTrack(); |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 52 | |
| 53 | // To be called before any other methods on this object, except |
| 54 | // getFormat(). |
Marco Nelissen | 3db2dcc | 2018-10-03 14:09:37 -0700 | [diff] [blame^] | 55 | virtual status_t start() = 0; |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 56 | |
| 57 | // Any blocking read call returns immediately with a result of NO_INIT. |
| 58 | // It is an error to call any methods other than start after this call |
| 59 | // returns. Any buffers the object may be holding onto at the time of |
| 60 | // the stop() call are released. |
| 61 | // Also, it is imperative that any buffers output by this object and |
| 62 | // held onto by callers be released before a call to stop() !!! |
| 63 | virtual status_t stop() = 0; |
| 64 | |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 65 | // Returns the format of the data output by this media track. |
| 66 | virtual status_t getFormat(MetaDataBase& format) = 0; |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 67 | |
| 68 | // Options that modify read() behaviour. The default is to |
| 69 | // a) not request a seek |
| 70 | // b) not be late, i.e. lateness_us = 0 |
| 71 | struct ReadOptions { |
| 72 | enum SeekMode : int32_t { |
Marco Nelissen | 2a3363a | 2018-09-13 13:15:30 -0700 | [diff] [blame] | 73 | SEEK_PREVIOUS_SYNC = CMediaTrackReadOptions::SEEK_PREVIOUS_SYNC, |
| 74 | SEEK_NEXT_SYNC = CMediaTrackReadOptions::SEEK_NEXT_SYNC, |
| 75 | SEEK_CLOSEST_SYNC = CMediaTrackReadOptions::SEEK_CLOSEST_SYNC, |
| 76 | SEEK_CLOSEST = CMediaTrackReadOptions::SEEK_CLOSEST, |
| 77 | SEEK_FRAME_INDEX = CMediaTrackReadOptions::SEEK_FRAME_INDEX, |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
Marco Nelissen | 56f1938 | 2018-09-12 15:30:59 -0700 | [diff] [blame] | 80 | ReadOptions() { |
| 81 | reset(); |
| 82 | } |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 83 | |
| 84 | // Reset everything back to defaults. |
Marco Nelissen | 56f1938 | 2018-09-12 15:30:59 -0700 | [diff] [blame] | 85 | void reset() { |
| 86 | mOptions = 0; |
| 87 | mSeekTimeUs = 0; |
| 88 | mNonBlocking = false; |
| 89 | } |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 90 | |
| 91 | void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC); |
Marco Nelissen | 56f1938 | 2018-09-12 15:30:59 -0700 | [diff] [blame] | 92 | void clearSeekTo() { |
| 93 | mOptions &= ~kSeekTo_Option; |
| 94 | mSeekTimeUs = 0; |
| 95 | mSeekMode = SEEK_CLOSEST_SYNC; |
| 96 | } |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 97 | bool getSeekTo(int64_t *time_us, SeekMode *mode) const; |
| 98 | |
| 99 | void setNonBlocking(); |
| 100 | void clearNonBlocking(); |
| 101 | bool getNonBlocking() const; |
| 102 | |
| 103 | // Used to clear all non-persistent options for multiple buffer reads. |
| 104 | void clearNonPersistent() { |
| 105 | clearSeekTo(); |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | enum Options { |
| 110 | kSeekTo_Option = 1, |
| 111 | }; |
| 112 | |
| 113 | uint32_t mOptions; |
| 114 | int64_t mSeekTimeUs; |
| 115 | SeekMode mSeekMode; |
| 116 | bool mNonBlocking; |
| 117 | } __attribute__((packed)); // sent through Binder |
| 118 | |
| 119 | // Returns a new buffer of data. Call blocks until a |
| 120 | // buffer is available, an error is encountered of the end of the stream |
| 121 | // is reached. |
| 122 | // End of stream is signalled by a result of ERROR_END_OF_STREAM. |
| 123 | // A result of INFO_FORMAT_CHANGED indicates that the format of this |
| 124 | // MediaSource has changed mid-stream, the client can continue reading |
| 125 | // but should be prepared for buffers of the new configuration. |
| 126 | virtual status_t read( |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 127 | MediaBufferBase **buffer, const ReadOptions *options = NULL) = 0; |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 128 | |
Marco Nelissen | 8551bb0 | 2018-09-14 08:25:08 -0700 | [diff] [blame] | 129 | // Returns true if |read| supports nonblocking option, otherwise false. |
| 130 | // |readMultiple| if supported, always allows the nonblocking option. |
| 131 | virtual bool supportNonblockingRead() { |
| 132 | return false; |
| 133 | } |
| 134 | |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 135 | virtual ~MediaTrack(); |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 136 | |
| 137 | private: |
Marco Nelissen | 3d21ae3 | 2018-02-16 08:24:08 -0800 | [diff] [blame] | 138 | MediaTrack(const MediaTrack &); |
| 139 | MediaTrack &operator=(const MediaTrack &); |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 140 | }; |
| 141 | |
Marco Nelissen | 2a3363a | 2018-09-13 13:15:30 -0700 | [diff] [blame] | 142 | class MediaTrackCUnwrapper : public MediaTrack { |
| 143 | public: |
| 144 | explicit MediaTrackCUnwrapper(CMediaTrack *wrapper); |
| 145 | |
Marco Nelissen | 3db2dcc | 2018-10-03 14:09:37 -0700 | [diff] [blame^] | 146 | virtual status_t start(); |
Marco Nelissen | 2a3363a | 2018-09-13 13:15:30 -0700 | [diff] [blame] | 147 | virtual status_t stop(); |
| 148 | virtual status_t getFormat(MetaDataBase& format); |
| 149 | virtual status_t read(MediaBufferBase **buffer, const ReadOptions *options = NULL); |
| 150 | |
| 151 | virtual bool supportNonblockingRead(); |
| 152 | |
| 153 | protected: |
| 154 | virtual ~MediaTrackCUnwrapper(); |
| 155 | |
| 156 | private: |
| 157 | CMediaTrack *wrapper; |
| 158 | }; |
| 159 | |
Marco Nelissen | 56f1938 | 2018-09-12 15:30:59 -0700 | [diff] [blame] | 160 | class MediaTrackCUnwrapperV2 : public MediaTrack { |
| 161 | public: |
| 162 | explicit MediaTrackCUnwrapperV2(CMediaTrackV2 *wrapper); |
| 163 | |
Marco Nelissen | 3db2dcc | 2018-10-03 14:09:37 -0700 | [diff] [blame^] | 164 | virtual status_t start(); |
Marco Nelissen | 56f1938 | 2018-09-12 15:30:59 -0700 | [diff] [blame] | 165 | virtual status_t stop(); |
| 166 | virtual status_t getFormat(MetaDataBase& format); |
| 167 | virtual status_t read(MediaBufferBase **buffer, const ReadOptions *options = NULL); |
| 168 | |
| 169 | virtual bool supportNonblockingRead(); |
| 170 | |
| 171 | protected: |
| 172 | virtual ~MediaTrackCUnwrapperV2(); |
| 173 | |
| 174 | private: |
| 175 | CMediaTrackV2 *wrapper; |
| 176 | }; |
| 177 | |
Marco Nelissen | cb30d84 | 2018-01-26 15:29:02 -0800 | [diff] [blame] | 178 | } // namespace android |
| 179 | |
| 180 | #endif // MEDIA_SOURCE_BASE_H_ |