1 /*
2  * Copyright 2015, 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_MEDIADATASOURCE_H_
18 #define _ANDROID_MEDIA_MEDIADATASOURCE_H_
19 
20 #include "jni.h"
21 
22 #include <media/IDataSource.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.MediaDataSource. It inherits from
30 // IDataSource so that it can be accessed remotely.
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 JMediaDataSource : public BnDataSource {
36 public:
37     enum {
38         kBufferSize = 64 * 1024,
39     };
40 
41     JMediaDataSource(JNIEnv *env, jobject source);
42     virtual ~JMediaDataSource();
43 
44     virtual sp<IMemory> getIMemory();
45     virtual ssize_t readAt(off64_t offset, size_t size);
46     virtual status_t getSize(off64_t* size);
47     virtual void close();
48 
49 private:
50     // Protect all member variables with mLock because this object will be
51     // accessed on different binder worker threads.
52     Mutex mLock;
53 
54     // The status of the java DataSource. Set to OK unless an error occurred or
55     // close() was called.
56     status_t mJavaObjStatus;
57     // Only call the java getSize() once so the app can't change the size on us.
58     bool mSizeIsCached;
59     off64_t mCachedSize;
60     sp<IMemory> mMemory;
61 
62     jobject mMediaDataSourceObj;
63     jmethodID mReadMethod;
64     jmethodID mGetSizeMethod;
65     jmethodID mCloseMethod;
66     jbyteArray mByteArrayObj;
67 
68     DISALLOW_EVIL_CONSTRUCTORS(JMediaDataSource);
69 };
70 
71 }  // namespace android
72 
73 #endif  // _ANDROID_MEDIA_MEDIADATASOURCE_H_
74