1 /*
2  * Copyright 2017, 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 _ANDROID_MEDIA_MEDIA2DATASOURCE_H_
18 #define _ANDROID_MEDIA_MEDIA2DATASOURCE_H_
19 
20 #include "jni.h"
21 
22 #include <media/DataSource.h>
23 #include <media/stagefright/foundation/ABase.h>
24 #include <utils/Errors.h>
25 #include <utils/Mutex.h>
26 
27 namespace android {
28 
29 // The native counterpart to a Java android.media.Media2DataSource. It inherits from
30 // DataSource.
31 //
32 // If the java DataSource returns an error or throws an exception it
33 // will be considered to be in a broken state, and the only further call this
34 // will make is to close().
35 class JMedia2DataSource : public DataSource {
36 public:
37     JMedia2DataSource(JNIEnv *env, jobject source);
38     virtual ~JMedia2DataSource();
39 
40     virtual status_t initCheck() const override;
41     virtual ssize_t readAt(off64_t offset, void *data, size_t size) override;
42     virtual status_t getSize(off64_t *size) override;
43 
44     virtual String8 toString() override;
45     virtual String8 getMIMEType() const override;
46     virtual void close() override;
47 private:
48     // Protect all member variables with mLock because this object will be
49     // accessed on different threads.
50     Mutex mLock;
51 
52     // The status of the java DataSource. Set to OK unless an error occurred or
53     // close() was called.
54     status_t mJavaObjStatus;
55     // Only call the java getSize() once so the app can't change the size on us.
56     bool mSizeIsCached;
57     off64_t mCachedSize;
58 
59     jobject mMedia2DataSourceObj;
60     jmethodID mReadAtMethod;
61     jmethodID mGetSizeMethod;
62     jmethodID mCloseMethod;
63     jbyteArray mByteArrayObj;
64 
65     DISALLOW_EVIL_CONSTRUCTORS(JMedia2DataSource);
66 };
67 
68 }  // namespace android
69 
70 #endif  // _ANDROID_MEDIA_MEDIA2DATASOURCE_H_
71