blob: 3f77bda90e59c734923c05b70236b6249614a579 [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>
25#include <media/stagefright/MediaErrors.h>
26#include <media/stagefright/MetaData.h>
Marco Nelissen2a3363a2018-09-13 13:15:30 -070027#include <media/MediaExtractorPluginApi.h>
Dongwon Kang42e497d2018-01-30 06:12:32 -080028#include <utils/Log.h>
Marco Nelissencb30d842018-01-26 15:29:02 -080029#include <utils/RefBase.h>
30#include <utils/Vector.h>
31
32namespace android {
33
Dongwon Kang1889c3e2018-02-01 13:44:57 -080034class MediaBufferBase;
Marco Nelissen2a3363a2018-09-13 13:15:30 -070035struct CMediaTrack;
Marco Nelissencb30d842018-01-26 15:29:02 -080036
37class SourceBaseAllocTracker {
38public:
39 SourceBaseAllocTracker() {
40 ALOGD("sourcebase allocated: %p", this);
41 }
42 virtual ~SourceBaseAllocTracker() {
43 ALOGD("sourcebase freed: %p", this);
44 }
45};
46
Marco Nelissen3d21ae32018-02-16 08:24:08 -080047struct MediaTrack
Marco Nelissencb30d842018-01-26 15:29:02 -080048// : public SourceBaseAllocTracker
49{
Marco Nelissen3d21ae32018-02-16 08:24:08 -080050 MediaTrack();
Marco Nelissencb30d842018-01-26 15:29:02 -080051
52 // To be called before any other methods on this object, except
53 // getFormat().
Marco Nelissen3d21ae32018-02-16 08:24:08 -080054 virtual status_t start(MetaDataBase *params = NULL) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -080055
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 Nelissen3d21ae32018-02-16 08:24:08 -080064 // Returns the format of the data output by this media track.
65 virtual status_t getFormat(MetaDataBase& format) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -080066
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 Nelissen2a3363a2018-09-13 13:15:30 -070072 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 Nelissencb30d842018-01-26 15:29:02 -080077 };
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 Kang1889c3e2018-02-01 13:44:57 -0800116 MediaBufferBase **buffer, const ReadOptions *options = NULL) = 0;
Marco Nelissencb30d842018-01-26 15:29:02 -0800117
Marco Nelissen8551bb02018-09-14 08:25:08 -0700118 // 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 Nelissen3d21ae32018-02-16 08:24:08 -0800124 virtual ~MediaTrack();
Marco Nelissencb30d842018-01-26 15:29:02 -0800125
126private:
Marco Nelissen3d21ae32018-02-16 08:24:08 -0800127 MediaTrack(const MediaTrack &);
128 MediaTrack &operator=(const MediaTrack &);
Marco Nelissencb30d842018-01-26 15:29:02 -0800129};
130
Marco Nelissen2a3363a2018-09-13 13:15:30 -0700131class MediaTrackCUnwrapper : public MediaTrack {
132public:
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
142protected:
143 virtual ~MediaTrackCUnwrapper();
144
145private:
146 CMediaTrack *wrapper;
147};
148
Marco Nelissencb30d842018-01-26 15:29:02 -0800149} // namespace android
150
151#endif // MEDIA_SOURCE_BASE_H_