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_BUFFERING_PARAMS_H_
18 #define _ANDROID_MEDIA_BUFFERING_PARAMS_H_
19 
20 #include <media/BufferingSettings.h>
21 
22 namespace android {
23 
24 // This entire class is inline
25 struct BufferingParams {
26     BufferingSettings settings;
27 
28     struct fields_t {
29         jclass      clazz;
30         jmethodID   constructID;
31 
32         jfieldID    initial_mark_ms;
33         jfieldID    resume_playback_mark_ms;
34 
initBufferingParams::fields_t35         void init(JNIEnv *env) {
36             jclass lclazz = env->FindClass("android/media/BufferingParams");
37             if (lclazz == NULL) {
38                 return;
39             }
40 
41             clazz = (jclass)env->NewGlobalRef(lclazz);
42             if (clazz == NULL) {
43                 return;
44             }
45 
46             constructID = env->GetMethodID(clazz, "<init>", "()V");
47 
48             initial_mark_ms = env->GetFieldID(clazz, "mInitialMarkMs", "I");
49             resume_playback_mark_ms = env->GetFieldID(clazz, "mResumePlaybackMarkMs", "I");
50 
51             env->DeleteLocalRef(lclazz);
52         }
53 
exitBufferingParams::fields_t54         void exit(JNIEnv *env) {
55             env->DeleteGlobalRef(clazz);
56             clazz = NULL;
57         }
58     };
59 
fillFromJobjectBufferingParams60     void fillFromJobject(JNIEnv *env, const fields_t& fields, jobject params) {
61         settings.mInitialMarkMs =
62             env->GetIntField(params, fields.initial_mark_ms);
63         settings.mResumePlaybackMarkMs =
64             env->GetIntField(params, fields.resume_playback_mark_ms);
65     }
66 
asJobjectBufferingParams67     jobject asJobject(JNIEnv *env, const fields_t& fields) {
68         jobject params = env->NewObject(fields.clazz, fields.constructID);
69         if (params == NULL) {
70             return NULL;
71         }
72         env->SetIntField(params, fields.initial_mark_ms, (jint)settings.mInitialMarkMs);
73         env->SetIntField(params, fields.resume_playback_mark_ms, (jint)settings.mResumePlaybackMarkMs);
74 
75         return params;
76     }
77 };
78 
79 }  // namespace android
80 
81 #endif  // _ANDROID_MEDIA_BUFFERING_PARAMS_H_
82