blob: ba721647282a9df5450dadfad3d0e9b09ee3d675 [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)
Robert Shihd4faf9e2018-01-21 17:52:25 -080046 : mDataSource(AMediaDataSource_new()) {
47 AMediaDataSource_setReadAt(mDataSource, dataSource->readAt);
48 AMediaDataSource_setGetSize(mDataSource, dataSource->getSize);
49 AMediaDataSource_setClose(mDataSource, dataSource->close);
50 AMediaDataSource_setUserdata(mDataSource, dataSource->userdata);
51}
52
53NdkDataSource::~NdkDataSource() {
54 AMediaDataSource_delete(mDataSource);
Robert Shih0df451b2017-12-08 14:16:50 -080055}
56
57status_t NdkDataSource::initCheck() const {
58 return OK;
59}
60
61ssize_t NdkDataSource::readAt(off64_t offset, void *data, size_t size) {
62 Mutex::Autolock l(mLock);
63 if (mDataSource->getSize == NULL || mDataSource->userdata == NULL) {
64 return -1;
65 }
66 return mDataSource->readAt(mDataSource->userdata, offset, data, size);
67}
68
69status_t NdkDataSource::getSize(off64_t *size) {
70 Mutex::Autolock l(mLock);
71 if (mDataSource->getSize == NULL || mDataSource->userdata == NULL) {
72 return NO_INIT;
73 }
74 if (size != NULL) {
75 *size = mDataSource->getSize(mDataSource->userdata);
76 }
77 return OK;
78}
79
80String8 NdkDataSource::toString() {
81 return String8::format("NdkDataSource(pid %d, uid %d)", getpid(), getuid());
82}
83
84String8 NdkDataSource::getMIMEType() const {
85 return String8("application/octet-stream");
86}
87
88extern "C" {
89
90EXPORT
91AMediaDataSource* AMediaDataSource_new() {
92 AMediaDataSource *mSource = new AMediaDataSource();
93 mSource->userdata = NULL;
94 mSource->readAt = NULL;
95 mSource->getSize = NULL;
96 return mSource;
97}
98
99EXPORT
100void AMediaDataSource_delete(AMediaDataSource *mSource) {
101 ALOGV("dtor");
102 if (mSource != NULL) {
103 delete mSource;
104 }
105}
106
107EXPORT
108void AMediaDataSource_setUserdata(AMediaDataSource *mSource, void *userdata) {
109 mSource->userdata = userdata;
110}
111
112EXPORT
113void AMediaDataSource_setReadAt(AMediaDataSource *mSource, AMediaDataSourceReadAt readAt) {
114 mSource->readAt = readAt;
115}
116
117EXPORT
118void AMediaDataSource_setGetSize(AMediaDataSource *mSource, AMediaDataSourceGetSize getSize) {
119 mSource->getSize = getSize;
120}
121
122} // extern "C"
123