blob: 9d00e5e4a73c449a67570b965269aa8ac2ab4445 [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;
Robert Shih45b88d22018-02-05 13:52:39 -080043 AMediaDataSourceClose close;
Robert Shih0df451b2017-12-08 14:16:50 -080044};
45
46NdkDataSource::NdkDataSource(AMediaDataSource *dataSource)
Robert Shihd4faf9e2018-01-21 17:52:25 -080047 : 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
54NdkDataSource::~NdkDataSource() {
55 AMediaDataSource_delete(mDataSource);
Robert Shih0df451b2017-12-08 14:16:50 -080056}
57
58status_t NdkDataSource::initCheck() const {
59 return OK;
60}
61
62ssize_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
70status_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
81String8 NdkDataSource::toString() {
82 return String8::format("NdkDataSource(pid %d, uid %d)", getpid(), getuid());
83}
84
85String8 NdkDataSource::getMIMEType() const {
86 return String8("application/octet-stream");
87}
88
Robert Shih45b88d22018-02-05 13:52:39 -080089void NdkDataSource::close() {
90 if (mDataSource->close != NULL && mDataSource->userdata != NULL) {
91 mDataSource->close(mDataSource->userdata);
92 }
93}
94
Robert Shih0df451b2017-12-08 14:16:50 -080095extern "C" {
96
97EXPORT
98AMediaDataSource* AMediaDataSource_new() {
99 AMediaDataSource *mSource = new AMediaDataSource();
100 mSource->userdata = NULL;
101 mSource->readAt = NULL;
102 mSource->getSize = NULL;
Robert Shih45b88d22018-02-05 13:52:39 -0800103 mSource->close = NULL;
Robert Shih0df451b2017-12-08 14:16:50 -0800104 return mSource;
105}
106
107EXPORT
108void AMediaDataSource_delete(AMediaDataSource *mSource) {
109 ALOGV("dtor");
110 if (mSource != NULL) {
111 delete mSource;
112 }
113}
114
115EXPORT
116void AMediaDataSource_setUserdata(AMediaDataSource *mSource, void *userdata) {
117 mSource->userdata = userdata;
118}
119
120EXPORT
121void AMediaDataSource_setReadAt(AMediaDataSource *mSource, AMediaDataSourceReadAt readAt) {
122 mSource->readAt = readAt;
123}
124
125EXPORT
126void AMediaDataSource_setGetSize(AMediaDataSource *mSource, AMediaDataSourceGetSize getSize) {
127 mSource->getSize = getSize;
128}
129
Robert Shih45b88d22018-02-05 13:52:39 -0800130EXPORT
131void AMediaDataSource_setClose(AMediaDataSource *mSource, AMediaDataSourceClose close) {
132 mSource->close = close;
133}
134
Robert Shih0df451b2017-12-08 14:16:50 -0800135} // extern "C"
136