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> 27 #include <utils/Log.h> 28 #include <utils/RefBase.h> 29 #include <utils/Vector.h> 30 31 namespace android { 32 33 class MediaBufferBase; 34 35 class SourceBaseAllocTracker { 36 public: 37 SourceBaseAllocTracker() { 38 ALOGD("sourcebase allocated: %p", this); 39 } 40 virtual ~SourceBaseAllocTracker() { 41 ALOGD("sourcebase freed: %p", this); 42 } 43 }; 44 45 struct MediaTrack 46 // : public SourceBaseAllocTracker 47 { 48 MediaTrack(); 49 50 // To be called before any other methods on this object, except 51 // getFormat(). 52 virtual status_t start(MetaDataBase *params = NULL) = 0; 53 54 // Any blocking read call returns immediately with a result of NO_INIT. 55 // It is an error to call any methods other than start after this call 56 // returns. Any buffers the object may be holding onto at the time of 57 // the stop() call are released. 58 // Also, it is imperative that any buffers output by this object and 59 // held onto by callers be released before a call to stop() !!! 60 virtual status_t stop() = 0; 61 62 // Returns the format of the data output by this media track. 63 virtual status_t getFormat(MetaDataBase& format) = 0; 64 65 // Options that modify read() behaviour. The default is to 66 // a) not request a seek 67 // b) not be late, i.e. lateness_us = 0 68 struct ReadOptions { 69 enum SeekMode : int32_t { 70 SEEK_PREVIOUS_SYNC, 71 SEEK_NEXT_SYNC, 72 SEEK_CLOSEST_SYNC, 73 SEEK_CLOSEST, 74 SEEK_FRAME_INDEX, 75 }; 76 77 ReadOptions(); 78 79 // Reset everything back to defaults. 80 void reset(); 81 82 void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC); 83 void clearSeekTo(); 84 bool getSeekTo(int64_t *time_us, SeekMode *mode) const; 85 86 void setNonBlocking(); 87 void clearNonBlocking(); 88 bool getNonBlocking() const; 89 90 // Used to clear all non-persistent options for multiple buffer reads. 91 void clearNonPersistent() { 92 clearSeekTo(); 93 } 94 95 private: 96 enum Options { 97 kSeekTo_Option = 1, 98 }; 99 100 uint32_t mOptions; 101 int64_t mSeekTimeUs; 102 SeekMode mSeekMode; 103 bool mNonBlocking; 104 } __attribute__((packed)); // sent through Binder 105 106 // Returns a new buffer of data. Call blocks until a 107 // buffer is available, an error is encountered of the end of the stream 108 // is reached. 109 // End of stream is signalled by a result of ERROR_END_OF_STREAM. 110 // A result of INFO_FORMAT_CHANGED indicates that the format of this 111 // MediaSource has changed mid-stream, the client can continue reading 112 // but should be prepared for buffers of the new configuration. 113 virtual status_t read( 114 MediaBufferBase **buffer, const ReadOptions *options = NULL) = 0; 115 116 virtual ~MediaTrack(); 117 118 private: 119 MediaTrack(const MediaTrack &); 120 MediaTrack &operator=(const MediaTrack &); 121 }; 122 123 } // namespace android 124 125 #endif // MEDIA_SOURCE_BASE_H_ 126