blob: 0cae6f4dd150eff85d4ddcb3d2c746fe539ef175 [file] [log] [blame]
Robert Shih0df451b2017-12-08 14:16:50 -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//#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
37using namespace android;
38
39struct AMediaDataSource {
40 void *userdata;
41 AMediaDataSourceReadAt readAt;
42 AMediaDataSourceGetSize getSize;
43};
44
45NdkDataSource::NdkDataSource(AMediaDataSource *dataSource)
46 : mDataSource(dataSource) {
47}
48
49status_t NdkDataSource::initCheck() const {
50 return OK;
51}
52
53ssize_t NdkDataSource::readAt(off64_t offset, void *data, size_t size) {
54 Mutex::Autolock l(mLock);
55 if (mDataSource->getSize == NULL || mDataSource->userdata == NULL) {
56 return -1;
57 }
58 return mDataSource->readAt(mDataSource->userdata, offset, data, size);
59}
60
61status_t NdkDataSource::getSize(off64_t *size) {
62 Mutex::Autolock l(mLock);
63 if (mDataSource->getSize == NULL || mDataSource->userdata == NULL) {
64 return NO_INIT;
65 }
66 if (size != NULL) {
67 *size = mDataSource->getSize(mDataSource->userdata);
68 }
69 return OK;
70}
71
72String8 NdkDataSource::toString() {
73 return String8::format("NdkDataSource(pid %d, uid %d)", getpid(), getuid());
74}
75
76String8 NdkDataSource::getMIMEType() const {
77 return String8("application/octet-stream");
78}
79
80extern "C" {
81
82EXPORT
83AMediaDataSource* AMediaDataSource_new() {
84 AMediaDataSource *mSource = new AMediaDataSource();
85 mSource->userdata = NULL;
86 mSource->readAt = NULL;
87 mSource->getSize = NULL;
88 return mSource;
89}
90
91EXPORT
92void AMediaDataSource_delete(AMediaDataSource *mSource) {
93 ALOGV("dtor");
94 if (mSource != NULL) {
95 delete mSource;
96 }
97}
98
99EXPORT
100void AMediaDataSource_setUserdata(AMediaDataSource *mSource, void *userdata) {
101 mSource->userdata = userdata;
102}
103
104EXPORT
105void AMediaDataSource_setReadAt(AMediaDataSource *mSource, AMediaDataSourceReadAt readAt) {
106 mSource->readAt = readAt;
107}
108
109EXPORT
110void AMediaDataSource_setGetSize(AMediaDataSource *mSource, AMediaDataSourceGetSize getSize) {
111 mSource->getSize = getSize;
112}
113
114} // extern "C"
115