Merge "Tuner JNI: LnbCallback" into rvc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
d0e4132654
@@ -20,12 +20,12 @@ import android.annotation.IntDef;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.content.Context;
|
||||
import android.hardware.tv.tuner.V1_0.Constants;
|
||||
import android.media.tv.tuner.Tuner.Result;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/**
|
||||
* LNB (low-noise block downconverter) for satellite tuner.
|
||||
@@ -145,7 +145,8 @@ public class Lnb implements AutoCloseable {
|
||||
|
||||
int mId;
|
||||
LnbCallback mCallback;
|
||||
Context mContext;
|
||||
Executor mExecutor;
|
||||
|
||||
|
||||
private native int nativeSetVoltage(int voltage);
|
||||
private native int nativeSetTone(int tone);
|
||||
@@ -159,10 +160,20 @@ public class Lnb implements AutoCloseable {
|
||||
mId = id;
|
||||
}
|
||||
|
||||
void setCallback(@Nullable LnbCallback callback) {
|
||||
void setCallback(Executor executor, @Nullable LnbCallback callback) {
|
||||
mCallback = callback;
|
||||
if (mCallback == null) {
|
||||
return;
|
||||
mExecutor = executor;
|
||||
}
|
||||
|
||||
private void onEvent(int eventType) {
|
||||
if (mExecutor != null && mCallback != null) {
|
||||
mExecutor.execute(() -> mCallback.onEvent(eventType));
|
||||
}
|
||||
}
|
||||
|
||||
private void onDiseqcMessage(byte[] diseqcMessage) {
|
||||
if (mExecutor != null && mCallback != null) {
|
||||
mExecutor.execute(() -> mCallback.onDiseqcMessage(diseqcMessage));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +229,9 @@ public class Lnb implements AutoCloseable {
|
||||
* Releases the LNB instance.
|
||||
*/
|
||||
public void close() {
|
||||
nativeClose();
|
||||
int res = nativeClose();
|
||||
if (res != Tuner.RESULT_SUCCESS) {
|
||||
TunerUtils.throwExceptionForResult(res, "Failed to close LNB");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -829,6 +829,9 @@ public class Tuner implements AutoCloseable {
|
||||
Objects.requireNonNull(executor, "executor must not be null");
|
||||
Objects.requireNonNull(cb, "LnbCallback must not be null");
|
||||
checkResource(TunerResourceManager.TUNER_RESOURCE_TYPE_LNB);
|
||||
if (mLnb != null) {
|
||||
mLnb.setCallback(executor, cb);
|
||||
}
|
||||
return mLnb;
|
||||
}
|
||||
|
||||
@@ -847,7 +850,11 @@ public class Tuner implements AutoCloseable {
|
||||
Objects.requireNonNull(name, "LNB name must not be null");
|
||||
Objects.requireNonNull(executor, "executor must not be null");
|
||||
Objects.requireNonNull(cb, "LnbCallback must not be null");
|
||||
return nativeOpenLnbByName(name);
|
||||
mLnb = nativeOpenLnbByName(name);
|
||||
if (mLnb != null) {
|
||||
mLnb.setCallback(executor, cb);
|
||||
}
|
||||
return mLnb;
|
||||
}
|
||||
|
||||
private boolean requestLnb() {
|
||||
@@ -872,12 +879,6 @@ public class Tuner implements AutoCloseable {
|
||||
return nativeOpenTimeFilter();
|
||||
}
|
||||
|
||||
private void onLnbEvent(int eventType) {
|
||||
if (mHandler != null) {
|
||||
mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_LNB_EVENT, eventType, 0));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens a Descrambler in tuner.
|
||||
*
|
||||
|
||||
@@ -137,6 +137,8 @@ public final class TunerUtils {
|
||||
msg = "";
|
||||
}
|
||||
switch (r) {
|
||||
case Tuner.RESULT_SUCCESS:
|
||||
return;
|
||||
case Tuner.RESULT_INVALID_ARGUMENT:
|
||||
throw new IllegalArgumentException(msg);
|
||||
case Tuner.RESULT_INVALID_STATE:
|
||||
|
||||
@@ -83,7 +83,7 @@ public class TimeFilter implements AutoCloseable {
|
||||
@Result
|
||||
public int clearTimestamp() {
|
||||
int res = nativeClearTimestamp();
|
||||
if (res == 0) {
|
||||
if (res == Tuner.RESULT_SUCCESS) {
|
||||
mEnable = false;
|
||||
}
|
||||
return res;
|
||||
|
||||
@@ -155,6 +155,7 @@ struct fields_t {
|
||||
jmethodID onFilterEventID;
|
||||
jmethodID lnbInitID;
|
||||
jmethodID onLnbEventID;
|
||||
jmethodID onLnbDiseqcMessageID;
|
||||
jmethodID onDvrRecordStatusID;
|
||||
jmethodID onDvrPlaybackStatusID;
|
||||
jmethodID descramblerInitID;
|
||||
@@ -170,19 +171,31 @@ static int IP_V6_LENGTH = 16;
|
||||
|
||||
namespace android {
|
||||
/////////////// LnbCallback ///////////////////////
|
||||
LnbCallback::LnbCallback(jweak tunerObj, LnbId id) : mObject(tunerObj), mId(id) {}
|
||||
LnbCallback::LnbCallback(jobject lnbObj, LnbId id) : mId(id) {
|
||||
JNIEnv *env = AndroidRuntime::getJNIEnv();
|
||||
mLnb = env->NewWeakGlobalRef(lnbObj);
|
||||
}
|
||||
|
||||
Return<void> LnbCallback::onEvent(LnbEventType lnbEventType) {
|
||||
ALOGD("LnbCallback::onEvent, type=%d", lnbEventType);
|
||||
JNIEnv *env = AndroidRuntime::getJNIEnv();
|
||||
env->CallVoidMethod(
|
||||
mObject,
|
||||
mLnb,
|
||||
gFields.onLnbEventID,
|
||||
(jint)lnbEventType);
|
||||
return Void();
|
||||
}
|
||||
Return<void> LnbCallback::onDiseqcMessage(const hidl_vec<uint8_t>& /*diseqcMessage*/) {
|
||||
Return<void> LnbCallback::onDiseqcMessage(const hidl_vec<uint8_t>& diseqcMessage) {
|
||||
ALOGD("LnbCallback::onDiseqcMessage");
|
||||
JNIEnv *env = AndroidRuntime::getJNIEnv();
|
||||
jbyteArray array = env->NewByteArray(diseqcMessage.size());
|
||||
env->SetByteArrayRegion(
|
||||
array, 0, diseqcMessage.size(), reinterpret_cast<jbyte*>(diseqcMessage[0]));
|
||||
|
||||
env->CallVoidMethod(
|
||||
mLnb,
|
||||
gFields.onLnbDiseqcMessageID,
|
||||
array);
|
||||
return Void();
|
||||
}
|
||||
|
||||
@@ -875,6 +888,10 @@ jobject JTuner::openFrontendById(int id) {
|
||||
return NULL;
|
||||
}
|
||||
mFe = fe;
|
||||
mFeId = id;
|
||||
if (mDemux != NULL) {
|
||||
mDemux->setFrontendDataSource(mFeId);
|
||||
}
|
||||
sp<FrontendCallback> feCb = new FrontendCallback(mObject, id);
|
||||
fe->setCallback(feCb);
|
||||
|
||||
@@ -1089,16 +1106,16 @@ jobject JTuner::getLnbIds() {
|
||||
|
||||
jobject JTuner::openLnbById(int id) {
|
||||
sp<ILnb> iLnbSp;
|
||||
mTuner->openLnbById(id, [&](Result, const sp<ILnb>& lnb) {
|
||||
Result r;
|
||||
mTuner->openLnbById(id, [&](Result res, const sp<ILnb>& lnb) {
|
||||
r = res;
|
||||
iLnbSp = lnb;
|
||||
});
|
||||
if (iLnbSp == nullptr) {
|
||||
if (r != Result::SUCCESS || iLnbSp == nullptr) {
|
||||
ALOGE("Failed to open lnb");
|
||||
return NULL;
|
||||
}
|
||||
mLnb = iLnbSp;
|
||||
sp<LnbCallback> lnbCb = new LnbCallback(mObject, id);
|
||||
mLnb->setCallback(lnbCb);
|
||||
|
||||
JNIEnv *env = AndroidRuntime::getJNIEnv();
|
||||
jobject lnbObj = env->NewObject(
|
||||
@@ -1106,6 +1123,9 @@ jobject JTuner::openLnbById(int id) {
|
||||
gFields.lnbInitID,
|
||||
(jint) id);
|
||||
|
||||
sp<LnbCallback> lnbCb = new LnbCallback(lnbObj, id);
|
||||
mLnb->setCallback(lnbCb);
|
||||
|
||||
sp<Lnb> lnbSp = new Lnb(iLnbSp, lnbObj);
|
||||
lnbSp->incStrong(lnbObj);
|
||||
env->SetLongField(lnbObj, gFields.lnbContext, (jlong) lnbSp.get());
|
||||
@@ -1129,14 +1149,15 @@ jobject JTuner::openLnbByName(jstring name) {
|
||||
return NULL;
|
||||
}
|
||||
mLnb = iLnbSp;
|
||||
sp<LnbCallback> lnbCb = new LnbCallback(mObject, id);
|
||||
mLnb->setCallback(lnbCb);
|
||||
|
||||
jobject lnbObj = env->NewObject(
|
||||
env->FindClass("android/media/tv/tuner/Lnb"),
|
||||
gFields.lnbInitID,
|
||||
id);
|
||||
|
||||
sp<LnbCallback> lnbCb = new LnbCallback(lnbObj, id);
|
||||
mLnb->setCallback(lnbCb);
|
||||
|
||||
sp<Lnb> lnbSp = new Lnb(iLnbSp, lnbObj);
|
||||
lnbSp->incStrong(lnbObj);
|
||||
env->SetLongField(lnbObj, gFields.lnbContext, (jlong) lnbSp.get());
|
||||
@@ -1206,12 +1227,21 @@ Result JTuner::openDemux() {
|
||||
return Result::SUCCESS;
|
||||
}
|
||||
Result res;
|
||||
uint32_t id;
|
||||
sp<IDemux> demuxSp;
|
||||
mTuner->openDemux([&](Result r, uint32_t demuxId, const sp<IDemux>& demux) {
|
||||
mDemux = demux;
|
||||
mDemuxId = demuxId;
|
||||
demuxSp = demux;
|
||||
id = demuxId;
|
||||
res = r;
|
||||
ALOGD("open demux, id = %d", demuxId);
|
||||
});
|
||||
if (res == Result::SUCCESS) {
|
||||
mDemux = demuxSp;
|
||||
mDemuxId = id;
|
||||
if (mFe != NULL) {
|
||||
mDemux->setFrontendDataSource(mFeId);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -2242,8 +2272,6 @@ static void android_media_tv_Tuner_native_init(JNIEnv *env) {
|
||||
|
||||
gFields.onFrontendEventID = env->GetMethodID(clazz, "onFrontendEvent", "(I)V");
|
||||
|
||||
gFields.onLnbEventID = env->GetMethodID(clazz, "onLnbEvent", "(I)V");
|
||||
|
||||
jclass frontendClazz = env->FindClass("android/media/tv/tuner/Tuner$Frontend");
|
||||
gFields.frontendInitID =
|
||||
env->GetMethodID(frontendClazz, "<init>", "(Landroid/media/tv/tuner/Tuner;I)V");
|
||||
@@ -2251,6 +2279,8 @@ static void android_media_tv_Tuner_native_init(JNIEnv *env) {
|
||||
jclass lnbClazz = env->FindClass("android/media/tv/tuner/Lnb");
|
||||
gFields.lnbContext = env->GetFieldID(lnbClazz, "mNativeContext", "J");
|
||||
gFields.lnbInitID = env->GetMethodID(lnbClazz, "<init>", "(I)V");
|
||||
gFields.onLnbEventID = env->GetMethodID(lnbClazz, "onEvent", "(I)V");
|
||||
gFields.onLnbDiseqcMessageID = env->GetMethodID(lnbClazz, "onDiseqcMessage", "([B)V");
|
||||
|
||||
jclass filterClazz = env->FindClass("android/media/tv/tuner/filter/Filter");
|
||||
gFields.filterContext = env->GetFieldID(filterClazz, "mNativeContext", "J");
|
||||
@@ -3204,8 +3234,14 @@ static int android_media_tv_Tuner_lnb_send_diseqc_msg(JNIEnv* env, jobject lnb,
|
||||
return (jint) r;
|
||||
}
|
||||
|
||||
static int android_media_tv_Tuner_close_lnb(JNIEnv*, jobject) {
|
||||
return 0;
|
||||
static int android_media_tv_Tuner_close_lnb(JNIEnv* env, jobject lnb) {
|
||||
sp<Lnb> lnbSp = getLnb(env, lnb);
|
||||
Result r = lnbSp->getILnb()->close();
|
||||
if (r == Result::SUCCESS) {
|
||||
lnbSp->decStrong(lnb);
|
||||
env->SetLongField(lnb, gFields.lnbContext, 0);
|
||||
}
|
||||
return (jint) r;
|
||||
}
|
||||
|
||||
static void android_media_tv_Tuner_dvr_set_fd(JNIEnv *env, jobject dvr, jobject jfd) {
|
||||
|
||||
@@ -70,7 +70,7 @@ struct LnbCallback : public ILnbCallback {
|
||||
LnbCallback(jweak tunerObj, LnbId id);
|
||||
virtual Return<void> onEvent(LnbEventType lnbEventType);
|
||||
virtual Return<void> onDiseqcMessage(const hidl_vec<uint8_t>& diseqcMessage);
|
||||
jweak mObject;
|
||||
jweak mLnb;
|
||||
LnbId mId;
|
||||
};
|
||||
|
||||
@@ -199,6 +199,7 @@ private:
|
||||
static sp<ITuner> mTuner;
|
||||
hidl_vec<FrontendId> mFeIds;
|
||||
sp<IFrontend> mFe;
|
||||
int mFeId;
|
||||
hidl_vec<LnbId> mLnbIds;
|
||||
sp<ILnb> mLnb;
|
||||
sp<IDemux> mDemux;
|
||||
|
||||
Reference in New Issue
Block a user