1 /*
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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NdkMediaDataSource"
19
20 #include "NdkMediaDataSourcePriv.h"
21
22 #include <inttypes.h>
23 #include <jni.h>
24 #include <unistd.h>
25
26 #include <binder/IServiceManager.h>
27 #include <cutils/properties.h>
28 #include <utils/Log.h>
29 #include <utils/StrongPointer.h>
30 #include <media/NdkMediaError.h>
31 #include <media/NdkMediaDataSource.h>
32 #include <media/stagefright/InterfaceUtils.h>
33
34 #include "../../libstagefright/include/HTTPBase.h"
35 #include "../../libstagefright/include/NuCachedSource2.h"
36
37 using namespace android;
38
39 struct AMediaDataSource {
40 void *userdata;
41 AMediaDataSourceReadAt readAt;
42 AMediaDataSourceGetSize getSize;
43 AMediaDataSourceClose close;
44 };
45
NdkDataSource(AMediaDataSource * dataSource)46 NdkDataSource::NdkDataSource(AMediaDataSource *dataSource)
47 : mDataSource(AMediaDataSource_new()) {
48 AMediaDataSource_setReadAt(mDataSource, dataSource->readAt);
49 AMediaDataSource_setGetSize(mDataSource, dataSource->getSize);
50 AMediaDataSource_setClose(mDataSource, dataSource->close);
51 AMediaDataSource_setUserdata(mDataSource, dataSource->userdata);
52 }
53
~NdkDataSource()54 NdkDataSource::~NdkDataSource() {
55 AMediaDataSource_delete(mDataSource);
56 }
57
initCheck() const58 status_t NdkDataSource::initCheck() const {
59 return OK;
60 }
61
readAt(off64_t offset,void * data,size_t size)62 ssize_t NdkDataSource::readAt(off64_t offset, void *data, size_t size) {
63 Mutex::Autolock l(mLock);
64 if (mDataSource->getSize == NULL || mDataSource->userdata == NULL) {
65 return -1;
66 }
67 return mDataSource->readAt(mDataSource->userdata, offset, data, size);
68 }
69
getSize(off64_t * size)70 status_t NdkDataSource::getSize(off64_t *size) {
71 Mutex::Autolock l(mLock);
72 if (mDataSource->getSize == NULL || mDataSource->userdata == NULL) {
73 return NO_INIT;
74 }
75 if (size != NULL) {
76 *size = mDataSource->getSize(mDataSource->userdata);
77 }
78 return OK;
79 }
80
toString()81 String8 NdkDataSource::toString() {
82 return String8::format("NdkDataSource(pid %d, uid %d)", getpid(), getuid());
83 }
84
getMIMEType() const85 String8 NdkDataSource::getMIMEType() const {
86 return String8("application/octet-stream");
87 }
88
close()89 void NdkDataSource::close() {
90 if (mDataSource->close != NULL && mDataSource->userdata != NULL) {
91 mDataSource->close(mDataSource->userdata);
92 }
93 }
94
95 extern "C" {
96
97 EXPORT
AMediaDataSource_new()98 AMediaDataSource* AMediaDataSource_new() {
99 AMediaDataSource *mSource = new AMediaDataSource();
100 mSource->userdata = NULL;
101 mSource->readAt = NULL;
102 mSource->getSize = NULL;
103 mSource->close = NULL;
104 return mSource;
105 }
106
107 EXPORT
AMediaDataSource_delete(AMediaDataSource * mSource)108 void AMediaDataSource_delete(AMediaDataSource *mSource) {
109 ALOGV("dtor");
110 if (mSource != NULL) {
111 delete mSource;
112 }
113 }
114
115 EXPORT
AMediaDataSource_setUserdata(AMediaDataSource * mSource,void * userdata)116 void AMediaDataSource_setUserdata(AMediaDataSource *mSource, void *userdata) {
117 mSource->userdata = userdata;
118 }
119
120 EXPORT
AMediaDataSource_setReadAt(AMediaDataSource * mSource,AMediaDataSourceReadAt readAt)121 void AMediaDataSource_setReadAt(AMediaDataSource *mSource, AMediaDataSourceReadAt readAt) {
122 mSource->readAt = readAt;
123 }
124
125 EXPORT
AMediaDataSource_setGetSize(AMediaDataSource * mSource,AMediaDataSourceGetSize getSize)126 void AMediaDataSource_setGetSize(AMediaDataSource *mSource, AMediaDataSourceGetSize getSize) {
127 mSource->getSize = getSize;
128 }
129
130 EXPORT
AMediaDataSource_setClose(AMediaDataSource * mSource,AMediaDataSourceClose close)131 void AMediaDataSource_setClose(AMediaDataSource *mSource, AMediaDataSourceClose close) {
132 mSource->close = close;
133 }
134
135 } // extern "C"
136
137