blob: 7a7599dd7873a8dab54c9bfae29e1032b56fa254 [file] [log] [blame]
Marco Nelissenb2487f02015-09-01 13:23:23 -07001/*
2 * Copyright (C) 2009 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 IMEDIA_SOURCE_BASE_H_
18
19#define IMEDIA_SOURCE_BASE_H_
20
Andy Hungf59c0ba2016-06-15 17:59:30 -070021#include <map>
22
Marco Nelissenb2487f02015-09-01 13:23:23 -070023#include <binder/IInterface.h>
Andy Hungf59c0ba2016-06-15 17:59:30 -070024#include <binder/IMemory.h>
25#include <media/stagefright/MediaBuffer.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070026#include <media/stagefright/MediaErrors.h>
27
28namespace android {
29
30struct MediaSource;
31class MetaData;
Wei Jiae9a5b962016-02-12 11:38:27 -080032class MediaBufferGroup;
Marco Nelissenb2487f02015-09-01 13:23:23 -070033
34class IMediaSource : public IInterface {
35public:
36 DECLARE_META_INTERFACE(MediaSource);
37
Wei Jia1f1fc452016-05-11 16:17:22 -070038 enum {
39 // Maximum number of buffers would be read in readMultiple.
40 kMaxNumReadMultiple = 128,
41 };
42
Marco Nelissenb2487f02015-09-01 13:23:23 -070043 // To be called before any other methods on this object, except
44 // getFormat().
45 virtual status_t start(MetaData *params = NULL) = 0;
46
47 // Any blocking read call returns immediately with a result of NO_INIT.
48 // It is an error to call any methods other than start after this call
49 // returns. Any buffers the object may be holding onto at the time of
50 // the stop() call are released.
51 // Also, it is imperative that any buffers output by this object and
52 // held onto by callers be released before a call to stop() !!!
53 virtual status_t stop() = 0;
54
55 // Returns the format of the data output by this media source.
56 virtual sp<MetaData> getFormat() = 0;
57
58 // Options that modify read() behaviour. The default is to
59 // a) not request a seek
60 // b) not be late, i.e. lateness_us = 0
61 struct ReadOptions {
Andy Hungf59c0ba2016-06-15 17:59:30 -070062 enum SeekMode : int32_t {
Marco Nelissenb2487f02015-09-01 13:23:23 -070063 SEEK_PREVIOUS_SYNC,
64 SEEK_NEXT_SYNC,
65 SEEK_CLOSEST_SYNC,
66 SEEK_CLOSEST,
67 };
68
69 ReadOptions();
70
71 // Reset everything back to defaults.
72 void reset();
73
74 void setSeekTo(int64_t time_us, SeekMode mode = SEEK_CLOSEST_SYNC);
75 void clearSeekTo();
76 bool getSeekTo(int64_t *time_us, SeekMode *mode) const;
77
Andy Hungf59c0ba2016-06-15 17:59:30 -070078 // TODO: remove this if unused.
Marco Nelissenb2487f02015-09-01 13:23:23 -070079 void setLateBy(int64_t lateness_us);
80 int64_t getLateBy() const;
81
82 void setNonBlocking();
83 void clearNonBlocking();
84 bool getNonBlocking() const;
85
Andy Hungf59c0ba2016-06-15 17:59:30 -070086 // Used to clear all non-persistent options for multiple buffer reads.
87 void clearNonPersistent() {
88 clearSeekTo();
89 }
90
Marco Nelissenb2487f02015-09-01 13:23:23 -070091 private:
92 enum Options {
93 kSeekTo_Option = 1,
94 };
95
96 uint32_t mOptions;
97 int64_t mSeekTimeUs;
98 SeekMode mSeekMode;
99 int64_t mLatenessUs;
100 bool mNonBlocking;
101 };
102
103 // Returns a new buffer of data. Call blocks until a
Wei Jia1f1fc452016-05-11 16:17:22 -0700104 // buffer is available, an error is encountered or the end of the stream
Marco Nelissenb2487f02015-09-01 13:23:23 -0700105 // is reached.
106 // End of stream is signalled by a result of ERROR_END_OF_STREAM.
107 // A result of INFO_FORMAT_CHANGED indicates that the format of this
108 // MediaSource has changed mid-stream, the client can continue reading
109 // but should be prepared for buffers of the new configuration.
Andy Hungf59c0ba2016-06-15 17:59:30 -0700110 //
111 // TODO: consider removing read() in favor of readMultiple().
Marco Nelissenb2487f02015-09-01 13:23:23 -0700112 virtual status_t read(
113 MediaBuffer **buffer, const ReadOptions *options = NULL) = 0;
114
Andy Hungf59c0ba2016-06-15 17:59:30 -0700115 // Returns a vector of new buffers of data, where the new buffers are added
116 // to the end of the vector.
Wei Jia1f1fc452016-05-11 16:17:22 -0700117 // Call blocks until an error is encountered, or the end of the stream is
118 // reached, or format change is hit, or |kMaxNumReadMultiple| buffers have
119 // been read.
Andy Hungf59c0ba2016-06-15 17:59:30 -0700120 // End of stream is signaled by a result of ERROR_END_OF_STREAM.
Wei Jia1f1fc452016-05-11 16:17:22 -0700121 // A result of INFO_FORMAT_CHANGED indicates that the format of this
122 // MediaSource has changed mid-stream, the client can continue reading
123 // but should be prepared for buffers of the new configuration.
Andy Hungf59c0ba2016-06-15 17:59:30 -0700124 //
125 // ReadOptions may be specified. Persistent options apply to all reads;
126 // non-persistent options (e.g. seek) apply only to the first read.
Wei Jia1f1fc452016-05-11 16:17:22 -0700127 virtual status_t readMultiple(
Andy Hungf59c0ba2016-06-15 17:59:30 -0700128 Vector<MediaBuffer *> *buffers, uint32_t maxNumBuffers = 1,
129 const ReadOptions *options = nullptr) = 0;
Wei Jia1f1fc452016-05-11 16:17:22 -0700130
Wei Jiad3f4e142016-06-13 14:51:43 -0700131 // Returns true if |readMultiple| is supported, otherwise false.
132 virtual bool supportReadMultiple() = 0;
133
Marco Nelissenb2487f02015-09-01 13:23:23 -0700134 // Causes this source to suspend pulling data from its upstream source
135 // until a subsequent read-with-seek. Currently only supported by
136 // OMXCodec.
137 virtual status_t pause() = 0;
138
139 // The consumer of this media source requests that the given buffers
140 // are to be returned exclusively in response to read calls.
141 // This will be called after a successful start() and before the
142 // first read() call.
143 // Callee assumes ownership of the buffers if no error is returned.
144 virtual status_t setBuffers(const Vector<MediaBuffer *> & /* buffers */) = 0;
145
146};
147
148class BnMediaSource: public BnInterface<IMediaSource>
149{
150public:
Wei Jiae9a5b962016-02-12 11:38:27 -0800151 BnMediaSource();
152
Marco Nelissenb2487f02015-09-01 13:23:23 -0700153 virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
154 uint32_t flags = 0);
155
156 virtual status_t pause() {
157 return ERROR_UNSUPPORTED;
158 }
159
160 virtual status_t setBuffers(const Vector<MediaBuffer *> & /* buffers */) {
161 return ERROR_UNSUPPORTED;
162 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800163
Wei Jia1f1fc452016-05-11 16:17:22 -0700164 virtual status_t readMultiple(
Andy Hungf59c0ba2016-06-15 17:59:30 -0700165 Vector<MediaBuffer *> * /* buffers */, uint32_t /* maxNumBuffers = 1 */,
166 const ReadOptions * /* options = nullptr */) {
Wei Jia1f1fc452016-05-11 16:17:22 -0700167 return ERROR_UNSUPPORTED;
168 }
Wei Jiad3f4e142016-06-13 14:51:43 -0700169
170 virtual bool supportReadMultiple() {
171 return false;
172 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700173
174 static const size_t kBinderMediaBuffers = 4; // buffers managed by BnMediaSource
175
Wei Jiae9a5b962016-02-12 11:38:27 -0800176protected:
177 virtual ~BnMediaSource();
178
179private:
Andy Hungf59c0ba2016-06-15 17:59:30 -0700180 uint32_t mBuffersSinceStop; // Buffer tracking variable
Marco Nelissenb2487f02015-09-01 13:23:23 -0700181
Andy Hungf59c0ba2016-06-15 17:59:30 -0700182 std::unique_ptr<MediaBufferGroup> mGroup;
183
184 // To prevent marshalling IMemory with each read transaction, we cache the IMemory pointer
185 // into a map.
186 //
187 // This is converted into an index, which is used to identify the associated memory
188 // on the receiving side. We hold a reference to the IMemory here to ensure it doesn't
189 // change underneath us.
190
191 struct IndexCache {
192 IndexCache() : mIndex(0) { }
193
194 // Returns the index of the IMemory stored in cache or 0 if not found.
195 uint64_t lookup(const sp<IMemory> &mem) {
196 auto p = mMemoryToIndex.find(mem.get());
197 if (p == mMemoryToIndex.end()) {
198 return 0;
199 }
200 if (MediaBuffer::isDeadObject(p->second.first)) {
201 // this object's dead
202 ALOGW("Attempting to lookup a dead IMemory");
203 (void)mMemoryToIndex.erase(p);
204 return 0;
205 }
206 ALOGW_IF(p->second.first.get() != mem.get(), "Mismatched buffers without reset");
207 return p->second.second;
208 }
209
210 // Returns the index of the IMemory stored in the index cache.
211 uint64_t insert(const sp<IMemory> &mem) {
212 auto p = mMemoryToIndex.find(mem.get());
213 if (p == mMemoryToIndex.end()) {
214 if (mIndex == UINT64_MAX) {
215 ALOGE("Index overflow");
216 mIndex = 1; // skip overflow condition and hope for the best
217 } else {
218 ++mIndex;
219 }
220 (void)mMemoryToIndex.emplace(// C++11 mem.get(), std::make_pair(mem, mIndex))
221 std::piecewise_construct,
222 std::forward_as_tuple(mem.get()), std::forward_as_tuple(mem, mIndex));
223 return mIndex;
224 }
225 ALOGW("IMemory already inserted into cache");
226 return p->second.second;
227 }
228
229 void reset() {
230 mMemoryToIndex.clear();
231 mIndex = 0;
232 }
233
234 void gc() {
235 for (auto it = mMemoryToIndex.begin(); it != mMemoryToIndex.end(); ) {
236 if (MediaBuffer::isDeadObject(it->second.first)) {
237 it = mMemoryToIndex.erase(it);
238 } else {
239 ++it;
240 }
241 }
242 }
243
244 private:
245 uint64_t mIndex;
246 // C++14 unordered_map erase on iterator is stable; C++11 has no guarantee.
247 // Could key on uintptr_t instead of IMemory *
248 std::map<IMemory *, std::pair<sp<IMemory>, uint64_t>> mMemoryToIndex;
249 } mIndexCache;
250};
Marco Nelissenb2487f02015-09-01 13:23:23 -0700251
252} // namespace android
253
254#endif // IMEDIA_SOURCE_BASE_H_