📄 org_tritonus_lowlevel_alsa_alsapcm.c
字号:
/* * org_tritonus_lowlevel_alsa_AlsaPcm.c *//* * Copyright (c) 2000 by Matthias Pfisterer <Matthias.Pfisterer@gmx.de> * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */#include <sys/asoundlib.h>#include <errno.h>#include "org_tritonus_lowlevel_alsa_AlsaPcm.h"static int DEBUG = 1;static voidthrowRuntimeException(JNIEnv *env, char* pStrMessage){ static jclass runtimeExceptionClass = NULL; if (runtimeExceptionClass == NULL) { runtimeExceptionClass = (*env)->FindClass(env, "java/lang/RuntimeException"); if (runtimeExceptionClass == NULL) { (*env)->FatalError(env, "cannot get class object for java.lang.RuntimeException"); } } (*env)->ThrowNew(env, runtimeExceptionClass, pStrMessage);}static jfieldIDgetNativeSeqFieldID(JNIEnv *env){ static jfieldID nativeHandleFieldID = NULL; if (nativeHandleFieldID == NULL) { // TODO: use a passed object rather than the name of the class jclass cls = (*env)->FindClass(env, "org/tritonus/lowlevel/alsa/AlsaPcm"); if (cls == NULL) { throwRuntimeException(env, "cannot get class object for org.tritonus.lowlevel.alsa.ASequencer0"); } nativeHandleFieldID = (*env)->GetFieldID(env, cls, "m_lNativeHandle", "J"); if (nativeHandleFieldID == NULL) { throwRuntimeException(env, "cannot get field ID for m_lNativeHandle"); } } return nativeHandleFieldID;}static snd_pcm_t*getNativeHandle(JNIEnv *env, jobject obj){ jfieldID fieldID = getNativeSeqFieldID(env); return (snd_pcm_t*) (*env)->GetLongField(env, obj, fieldID);}static voidsetNativeHandle(JNIEnv *env, jobject obj, snd_pcm_t* handle){ jfieldID fieldID = getNativeSeqFieldID(env); (*env)->SetLongField(env, obj, fieldID, (jlong) handle);}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: close * Signature: ()I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_close(JNIEnv *env, jobject obj){ snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn = snd_pcm_close(handle); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: drainPlayback * Signature: ()I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_drainPlayback(JNIEnv *env, jobject obj){ snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn = snd_pcm_playback_drain(handle); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: flushCapture * Signature: ()I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_flushCapture(JNIEnv *env, jobject obj){ snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn = snd_pcm_capture_flush(handle); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: flushChannel * Signature: (I)I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_flushChannel(JNIEnv *env, jobject obj, jint nChannel){ snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn = snd_pcm_channel_flush(handle, nChannel); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: flushPlayback * Signature: ()I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_flushPlayback(JNIEnv *env, jobject obj){ snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn = snd_pcm_playback_flush(handle); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: getChannelInfo * Signature: ([I[Ljava/lang/String;)I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_getChannelInfo(JNIEnv *env, jobject obj, jintArray anValues, jobjectArray astrValues){ jint* values = NULL; jstring strValue; int nLength; snd_pcm_channel_info_t channelInfo; snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn; memset(&channelInfo, 0, sizeof(channelInfo)); nReturn = snd_pcm_channel_info(handle, &channelInfo); nLength = (*env)->GetArrayLength(env, anValues); if (nLength < 18) { throwRuntimeException(env, "integer array does not have enough elements (18 required)"); } values = (*env)->GetIntArrayElements(env, anValues, NULL); if (values == NULL) { throwRuntimeException(env, "GetIntArrayElements failed"); } values[0] = channelInfo.subdevice; values[1] = channelInfo.channel; values[2] = channelInfo.mode; values[3] = channelInfo.flags; values[4] = channelInfo.formats; values[5] = channelInfo.rates; values[6] = channelInfo.min_rate; values[7] = channelInfo.max_rate; values[8] = channelInfo.min_voices; values[9] = channelInfo.max_voices; values[10] = channelInfo.buffer_size; values[11] = channelInfo.min_fragment_size; values[12] = channelInfo.max_fragment_size; values[13] = channelInfo.fragment_align; values[14] = channelInfo.fifo_size; values[15] = channelInfo.transfer_block_size; values[16] = channelInfo.mmap_size; values[17] = channelInfo.mixer_device;/* printf("mixer device: %d\n", channelInfo.mixer_device); printf("mixer eid name: %s\n", channelInfo.mixer_eid.name); printf("mixer eid index: %d\n", channelInfo.mixer_eid.index); printf("mixer eid type: %d\n", channelInfo.mixer_eid.type);*/ // values[18] = channelInfo.mixer_eid; (*env)->ReleaseIntArrayElements(env, anValues, values, 0); nLength = (*env)->GetArrayLength(env, astrValues); if (nLength < 1) { throwRuntimeException(env, "string array does not have enough elements (1 required)"); } strValue = (*env)->NewStringUTF(env, channelInfo.subname); if (strValue == NULL) { throwRuntimeException(env, "NewStringUTF() failed"); } (*env)->SetObjectArrayElement(env, astrValues, 0, strValue); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: getChannelSetup * Signature: ([I[Z)I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_getChannelSetup(JNIEnv *env, jobject obj, jintArray anValues, jbooleanArray abValues){ jint* values = NULL; jboolean* bvalues = NULL; int nLength; snd_pcm_channel_setup_t channelSetup; snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn; memset(&channelSetup, 0, sizeof(channelSetup)); nReturn = snd_pcm_channel_setup(handle, &channelSetup); if (nReturn < 0) { throwRuntimeException(env, "snd_pcm_channel_setup failed"); } nLength = (*env)->GetArrayLength(env, anValues); if (nLength < 12) { throwRuntimeException(env, "array does not have enough elements (12 required)"); } values = (*env)->GetIntArrayElements(env, anValues, NULL); if (values == NULL) { throwRuntimeException(env, "GetIntArrayElements failed"); } values[0] = channelSetup.channel; values[1] = channelSetup.mode; values[2] = channelSetup.format.format; values[3] = channelSetup.format.rate; values[4] = channelSetup.format.voices; values[5] = channelSetup.format.special; values[6] = channelSetup.buf.stream.queue_size; values[7] = channelSetup.buf.block.frag_size; values[8] = channelSetup.buf.block.frags; values[9] = channelSetup.buf.block.frags_min; values[10] = channelSetup.buf.block.frags_max; values[11] = channelSetup.msbits_per_sample; (*env)->ReleaseIntArrayElements(env, anValues, values, 0); nLength = (*env)->GetArrayLength(env, abValues); if (nLength < 1) { throwRuntimeException(env, "array does not have enough elements (1 required)"); } bvalues = (*env)->GetBooleanArrayElements(env, abValues, NULL); if (bvalues == NULL) { throwRuntimeException(env, "GetBooleanArrayElements failed"); } bvalues[0] = channelSetup.format.interleave; (*env)->ReleaseBooleanArrayElements(env, abValues, bvalues, 0); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm * Method: getChannelStatus * Signature: (I[I[J)I */JNIEXPORT jint JNICALLJava_org_tritonus_lowlevel_alsa_AlsaPcm_getChannelStatus(JNIEnv *env, jobject obj, jint nChannel, jintArray anValues, jlongArray alValues){ jint* values = NULL; jlong* lvalues = NULL; int nLength; snd_pcm_channel_status_t channelStatus; snd_pcm_t* handle = getNativeHandle(env, obj); int nReturn; memset(&channelStatus, 0, sizeof(channelStatus)); channelStatus.channel = nChannel; printf("getChannelStatus(): handle: %p\n", handle); nReturn = snd_pcm_channel_status(handle, &channelStatus); if (nReturn < 0) { return nReturn; // throwRuntimeException(env, "snd_pcm_channel_status failed"); } nLength = (*env)->GetArrayLength(env, anValues); if (nLength < 9) { throwRuntimeException(env, "array does not have enough elements (9 required)"); } values = (*env)->GetIntArrayElements(env, anValues, NULL); if (values == NULL) { throwRuntimeException(env, "GetIntArrayElements failed"); } values[0] = channelStatus.mode; values[1] = channelStatus.status; values[2] = channelStatus.scount; values[3] = channelStatus.frag; values[4] = channelStatus.count; values[5] = channelStatus.free; values[6] = channelStatus.underrun; values[7] = channelStatus.overrun; values[8] = channelStatus.overrange; (*env)->ReleaseIntArrayElements(env, anValues, values, 0); nLength = (*env)->GetArrayLength(env, alValues); if (nLength < 2) { throwRuntimeException(env, "array does not have enough elements (2 required)"); } lvalues = (*env)->GetLongArrayElements(env, alValues, NULL); if (lvalues == NULL) { throwRuntimeException(env, "GetLongArrayElements failed"); } lvalues[0] = (jlong) channelStatus.stime.tv_sec * 1000000 + channelStatus.stime.tv_usec; lvalues[1] = channelStatus.ust_stime; (*env)->ReleaseLongArrayElements(env, alValues, lvalues, 0); return nReturn;}/* * Class: org_tritonus_lowlevel_alsa_AlsaPcm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -