blob: baa3410cb4779de5c09091eba3a8c5ae4a8597b7 [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 Nelissen0e043b62018-11-14 11:26:05 -080026#include <media/stagefright/MediaBufferGroup.h>
Marco Nelissencb30d842018-01-26 15:29:02 -080027#include <media/stagefright/MediaErrors.h>
28#include <media/stagefright/MetaData.h>
Marco Nelissen2a3363a2018-09-13 13:15:30 -070029#include <media/MediaExtractorPluginApi.h>
Dongwon Kang42e497d2018-01-30 06:12:32 -080030#include <utils/Log.h>
Marco Nelissencb30d842018-01-26 15:29:02 -080031#include <utils/RefBase.h>
32#include <utils/Vector.h>
33
34namespace android {
35
Dongwon Kang1889c3e2018-02-01 13:44:57 -080036class MediaBufferBase;
Marco Nelissen2a3363a2018-09-13 13:15:30 -070037struct CMediaTrack;
Marco Nelissencb30d842018-01-26 15:29:02 -080038
39class SourceBaseAllocTracker {
40public:
41 SourceBaseAllocTracker() {
42 ALOGD("sourcebase allocated: %p", this);
43 }
44 virtual ~SourceBaseAllocTracker() {
45 ALOGD("sourcebase freed: %p", this);
46 }
47};
48
Marco Nelissen3d21ae32018-02-16 08:24:08 -080049struct MediaTrack
Marco Nelissencb30d842018-01-26 15:29:02 -080050// : public SourceBaseAllocTracker
51{
Marco Nelissen3d21ae32018-02-16 08:24:08 -080052 MediaTrack();
Marco Nelissencb30d842018-01-26 15:29:02 -080053
54 // To be called before any other methods on this object, except
55 // getFormat().
Marco Nelissen3db2dcc2018-10-03 14:09:37 -070056 virtual status_t start() = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -080057
58 // Any blocking read call returns immediately with a result of NO_INIT.
59 // It is an error to call any methods other than start after this call
60 // returns. Any buffers the object may be holding onto at the time of
61 // the stop() call are released.
62 // Also, it is imperative that any buffers output by this object and
63 // held onto by callers be released before a call to stop() !!!
64 virtual status_t stop() = 0;
65
Marco Nelissen3d21ae32018-02-16 08:24:08 -080066 // Returns the format of the data output by this media track.
67 virtual status_t getFormat(MetaDataBase& format) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -080068
69 // Options that modify read() behaviour. The default is to
70 // a) not request a seek
71 // b) not be late, i.e. lateness_us = 0
72 struct ReadOptions {
73 enum SeekMode : int32_t {
Marco Nelissen2a3363a2018-09-13 13:15:30 -070074 SEEK_PREVIOUS_SYNC = CMediaTrackReadOptions::SEEK_PREVIOUS_SYNC,
75 SEEK_NEXT_SYNC = CMediaTrackReadOptions::SEEK_NEXT_SYNC,
76 SEEK_CLOSEST_SYNC = CMediaTrackReadOptions::SEEK_CLOSEST_SYNC,
77 SEEK_CLOSEST = CMediaTrackReadOptions::SEEK_CLOSEST,
78 SEEK_FRAME_INDEX = CMediaTrackReadOptions::SEEK_FRAME_INDEX,
Marco Nelissencb30d842018-01-26 15:29:02 -080079 };
80
Marco Nelissen56f19382018-09-12 15:30:59 -070081 ReadOptions() {
82 reset();
83 }
Marco Nelissencb30d842018-01-26 15:29:02 -080084
85 // Reset everything back to defaults.
Marco Nelissen56f19382018-09-12 15:30:59 -070086 void reset() {
87 mOptions = 0;
88 mSeekTimeUs = 0;
89 mNonBlocking = false;
90 }
Marco Nelissencb30d842018-01-26 15:29:02 -080091
92 void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC);
Marco Nelissen56f19382018-09-12 15:30:59 -070093 void clearSeekTo() {
94 mOptions &= ~kSeekTo_Option;
95 mSeekTimeUs = 0;
96 mSeekMode = SEEK_CLOSEST_SYNC;
97 }
Marco Nelissencb30d842018-01-26 15:29:02 -080098 bool getSeekTo(int64_t *time_us, SeekMode *mode) const;
99
100 void setNonBlocking();
101 void clearNonBlocking();
102 bool getNonBlocking() const;
103
104 // Used to clear all non-persistent options for multiple buffer reads.
105 void clearNonPersistent() {
106 clearSeekTo();
107 }
108
109 private:
110 enum Options {
111 kSeekTo_Option = 1,
112 };
113
114 uint32_t mOptions;
115 int64_t mSeekTimeUs;
116 SeekMode mSeekMode;
117 bool mNonBlocking;
118 } __attribute__((packed)); // sent through Binder
119
120 // Returns a new buffer of data. Call blocks until a
121 // buffer is available, an error is encountered of the end of the stream
122 // is reached.
123 // End of stream is signalled by a result of ERROR_END_OF_STREAM.
124 // A result of INFO_FORMAT_CHANGED indicates that the format of this
125 // MediaSource has changed mid-stream, the client can continue reading
126 // but should be prepared for buffers of the new configuration.
127 virtual status_t read(
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800128 MediaBufferBase **buffer, const ReadOptions *options = NULL) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -0800129
Marco Nelissen8551bb02018-09-14 08:25:08 -0700130 // Returns true if |read| supports nonblocking option, otherwise false.
131 // |readMultiple| if supported, always allows the nonblocking option.
132 virtual bool supportNonblockingRead() {
133 return false;
134 }
135
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800136 virtual ~MediaTrack();
Marco Nelissencb30d842018-01-26 15:29:02 -0800137
138private:
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800139 MediaTrack(const MediaTrack &);
140 MediaTrack &operator=(const MediaTrack &);
Marco Nelissencb30d842018-01-26 15:29:02 -0800141};
142
Marco Nelissen2a3363a2018-09-13 13:15:30 -0700143class MediaTrackCUnwrapper : public MediaTrack {
144public:
145 explicit MediaTrackCUnwrapper(CMediaTrack *wrapper);
146
Marco Nelissen3db2dcc2018-10-03 14:09:37 -0700147 virtual status_t start();
Marco Nelissen2a3363a2018-09-13 13:15:30 -0700148 virtual status_t stop();
149 virtual status_t getFormat(MetaDataBase& format);
150 virtual status_t read(MediaBufferBase **buffer, const ReadOptions *options = NULL);
151
152 virtual bool supportNonblockingRead();
153
154protected:
155 virtual ~MediaTrackCUnwrapper();
156
157private:
158 CMediaTrack *wrapper;
159};
160
Marco Nelissen56f19382018-09-12 15:30:59 -0700161class MediaTrackCUnwrapperV2 : public MediaTrack {
162public:
163 explicit MediaTrackCUnwrapperV2(CMediaTrackV2 *wrapper);
164
Marco Nelissen3db2dcc2018-10-03 14:09:37 -0700165 virtual status_t start();
Marco Nelissen56f19382018-09-12 15:30:59 -0700166 virtual status_t stop();
167 virtual status_t getFormat(MetaDataBase& format);
168 virtual status_t read(MediaBufferBase **buffer, const ReadOptions *options = NULL);
169
170 virtual bool supportNonblockingRead();
171
172protected:
173 virtual ~MediaTrackCUnwrapperV2();
174
175private:
176 CMediaTrackV2 *wrapper;
177};
178
Marco Nelissen0e043b62018-11-14 11:26:05 -0800179class MediaTrackCUnwrapperV3 : public MediaTrack {
180public:
181 explicit MediaTrackCUnwrapperV3(CMediaTrackV3 *wrapper);
182
183 virtual status_t start();
184 virtual status_t stop();
185 virtual status_t getFormat(MetaDataBase& format);
186 virtual status_t read(MediaBufferBase **buffer, const ReadOptions *options = NULL);
187
188 virtual bool supportNonblockingRead();
189
190protected:
191 virtual ~MediaTrackCUnwrapperV3();
192
193private:
194 CMediaTrackV3 *wrapper;
195 MediaBufferGroup *bufferGroup;
196};
197
Marco Nelissencb30d842018-01-26 15:29:02 -0800198} // namespace android
199
200#endif // MEDIA_SOURCE_BASE_H_