blob: 2abd5f5af4286de950591674fbffcd3b6e3e435d [file] [log] [blame]
Andreas Huber20111aa2009-07-14 16:56:47 -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 DATA_SOURCE_H_
18
19#define DATA_SOURCE_H_
20
21#include <sys/types.h>
Marco Nelissendab79b32019-11-18 08:25:47 -080022
23#include <android/IDataSource.h>
Andreas Huber0683eba2011-07-18 13:47:55 -070024#include <media/stagefright/MediaErrors.h>
Marco Nelissen7291da62019-12-17 13:01:55 -080025#include <media/stagefright/DataSourceBase.h>
Marco Nelissen0e043b62018-11-14 11:26:05 -080026#include <media/MediaExtractorPluginApi.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070027#include <utils/Errors.h>
Andreas Huber693d2712009-08-14 14:37:10 -070028#include <utils/RefBase.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070029#include <utils/threads.h>
30
Marco Nelissen2a243f02018-01-30 08:29:57 -080031
Andreas Huber20111aa2009-07-14 16:56:47 -070032namespace android {
33
34class String8;
35
Marco Nelissen2a243f02018-01-30 08:29:57 -080036class DataSource : public DataSourceBase, public virtual RefBase {
Andreas Huber20111aa2009-07-14 16:56:47 -070037public:
Marco Nelissencec44d02018-06-17 22:21:09 -070038 DataSource() : mWrapper(NULL) {}
Andreas Huber20111aa2009-07-14 16:56:47 -070039
Dongwon Kang6969da62018-02-12 20:55:14 -080040 // returns a pointer to IDataSource if it is wrapped.
41 virtual sp<IDataSource> getIDataSource() const {
42 return nullptr;
43 }
44
Dongwon Kangb486b522018-02-16 15:46:53 -080045 virtual String8 toString() {
46 return String8("<unspecified>");
47 }
48
49 virtual status_t reconnectAtOffset(off64_t /*offset*/) {
50 return ERROR_UNSUPPORTED;
51 }
52
53 ////////////////////////////////////////////////////////////////////////////
54
Dongwon Kangb486b522018-02-16 15:46:53 -080055 virtual String8 getUri() {
56 return String8();
57 }
58
59 virtual bool getUri(char *uriString, size_t bufferSize) final {
60 int ret = snprintf(uriString, bufferSize, "%s", getUri().c_str());
61 return ret >= 0 && static_cast<size_t>(ret) < bufferSize;
62 }
63
64 virtual String8 getMIMEType() const {
65 return String8("application/octet-stream");
66 }
67
Marco Nelissencec44d02018-06-17 22:21:09 -070068 CDataSource *wrap() {
69 if (mWrapper) {
70 return mWrapper;
71 }
72 mWrapper = new CDataSource();
73 mWrapper->handle = this;
74
75 mWrapper->readAt = [](void *handle, off64_t offset, void *data, size_t size) -> ssize_t {
76 return ((DataSource*)handle)->readAt(offset, data, size);
77 };
78 mWrapper->getSize = [](void *handle, off64_t *size) -> status_t {
79 return ((DataSource*)handle)->getSize(size);
80 };
81 mWrapper->flags = [](void *handle) -> uint32_t {
82 return ((DataSource*)handle)->flags();
83 };
84 mWrapper->getUri = [](void *handle, char *uriString, size_t bufferSize) -> bool {
85 return ((DataSource*)handle)->getUri(uriString, bufferSize);
86 };
87 return mWrapper;
88 }
89
Andreas Huber693d2712009-08-14 14:37:10 -070090protected:
Marco Nelissencec44d02018-06-17 22:21:09 -070091 virtual ~DataSource() {
92 delete mWrapper;
93 }
Andreas Huber693d2712009-08-14 14:37:10 -070094
Andreas Huber20111aa2009-07-14 16:56:47 -070095private:
Marco Nelissencec44d02018-06-17 22:21:09 -070096 CDataSource *mWrapper;
Andreas Huber20111aa2009-07-14 16:56:47 -070097 DataSource(const DataSource &);
98 DataSource &operator=(const DataSource &);
99};
100
Andreas Huber20111aa2009-07-14 16:56:47 -0700101} // namespace android
102
103#endif // DATA_SOURCE_H_