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