blob: 174f4cc562fa81852854bf5a51d5d505ea69afad [file] [log] [blame]
Marco Nelissencb30d842018-01-26 15:29:02 -08001/*
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 Nelissen56f19382018-09-12 15:30:59 -070025#include <media/MediaExtractorPluginApi.h>
Marco Nelissencb30d842018-01-26 15:29:02 -080026#include <media/stagefright/MediaErrors.h>
27#include <media/stagefright/MetaData.h>
Marco Nelissen2a3363a2018-09-13 13:15:30 -070028#include <media/MediaExtractorPluginApi.h>
Dongwon Kang42e497d2018-01-30 06:12:32 -080029#include <utils/Log.h>
Marco Nelissencb30d842018-01-26 15:29:02 -080030#include <utils/RefBase.h>
31#include <utils/Vector.h>
32
33namespace android {
34
Dongwon Kang1889c3e2018-02-01 13:44:57 -080035class MediaBufferBase;
Marco Nelissen2a3363a2018-09-13 13:15:30 -070036struct CMediaTrack;
Marco Nelissencb30d842018-01-26 15:29:02 -080037
38class SourceBaseAllocTracker {
39public:
40 SourceBaseAllocTracker() {
41 ALOGD("sourcebase allocated: %p", this);
42 }
43 virtual ~SourceBaseAllocTracker() {
44 ALOGD("sourcebase freed: %p", this);
45 }
46};
47
Marco Nelissen3d21ae32018-02-16 08:24:08 -080048struct MediaTrack
Marco Nelissencb30d842018-01-26 15:29:02 -080049// : public SourceBaseAllocTracker
50{
Marco Nelissen3d21ae32018-02-16 08:24:08 -080051 MediaTrack();
Marco Nelissencb30d842018-01-26 15:29:02 -080052
53 // To be called before any other methods on this object, except
54 // getFormat().
Marco Nelissen3d21ae32018-02-16 08:24:08 -080055 virtual status_t start(MetaDataBase *params = NULL) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -080056
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 Nelissen3d21ae32018-02-16 08:24:08 -080065 // Returns the format of the data output by this media track.
66 virtual status_t getFormat(MetaDataBase& format) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -080067
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 Nelissen2a3363a2018-09-13 13:15:30 -070073 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 Nelissencb30d842018-01-26 15:29:02 -080078 };
79
Marco Nelissen56f19382018-09-12 15:30:59 -070080 ReadOptions() {
81 reset();
82 }
Marco Nelissencb30d842018-01-26 15:29:02 -080083
84 // Reset everything back to defaults.
Marco Nelissen56f19382018-09-12 15:30:59 -070085 void reset() {
86 mOptions = 0;
87 mSeekTimeUs = 0;
88 mNonBlocking = false;
89 }
Marco Nelissencb30d842018-01-26 15:29:02 -080090
91 void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC);
Marco Nelissen56f19382018-09-12 15:30:59 -070092 void clearSeekTo() {
93 mOptions &= ~kSeekTo_Option;
94 mSeekTimeUs = 0;
95 mSeekMode = SEEK_CLOSEST_SYNC;
96 }
Marco Nelissencb30d842018-01-26 15:29:02 -080097 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 Kang1889c3e2018-02-01 13:44:57 -0800127 MediaBufferBase **buffer, const ReadOptions *options = NULL) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -0800128
Marco Nelissen8551bb02018-09-14 08:25:08 -0700129 // 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 Nelissen3d21ae32018-02-16 08:24:08 -0800135 virtual ~MediaTrack();
Marco Nelissencb30d842018-01-26 15:29:02 -0800136
137private:
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800138 MediaTrack(const MediaTrack &);
139 MediaTrack &operator=(const MediaTrack &);
Marco Nelissencb30d842018-01-26 15:29:02 -0800140};
141
Marco Nelissen2a3363a2018-09-13 13:15:30 -0700142class MediaTrackCUnwrapper : public MediaTrack {
143public:
144 explicit MediaTrackCUnwrapper(CMediaTrack *wrapper);
145
146 virtual status_t start(MetaDataBase *params = NULL);
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
153protected:
154 virtual ~MediaTrackCUnwrapper();
155
156private:
157 CMediaTrack *wrapper;
158};
159
Marco Nelissen56f19382018-09-12 15:30:59 -0700160class MediaTrackCUnwrapperV2 : public MediaTrack {
161public:
162 explicit MediaTrackCUnwrapperV2(CMediaTrackV2 *wrapper);
163
164 virtual status_t start(MetaDataBase *params = NULL);
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
171protected:
172 virtual ~MediaTrackCUnwrapperV2();
173
174private:
175 CMediaTrackV2 *wrapper;
176};
177
Marco Nelissencb30d842018-01-26 15:29:02 -0800178} // namespace android
179
180#endif // MEDIA_SOURCE_BASE_H_