almost working

crashes - need to figure out jni thread crap
This commit is contained in:
Bryan Barnes 2014-01-16 01:36:04 -05:00
parent babaf41482
commit 18e5bd7c95
7 changed files with 166 additions and 12 deletions

View File

@ -5,6 +5,7 @@
#include "maple_cfg.h" #include "maple_cfg.h"
#include <time.h> #include <time.h>
#include <android/log.h> #include <android/log.h>
#include <jni.h>
#include "deps/zlib/zlib.h" #include "deps/zlib/zlib.h"
@ -664,6 +665,8 @@ struct maple_microphone: maple_base
{ {
case MDC_DeviceRequest: case MDC_DeviceRequest:
LOGD("maple_microphone::dma MDC_DeviceRequest"); LOGD("maple_microphone::dma MDC_DeviceRequest");
//this was copied from the controller case with just the id and name replaced!
//caps //caps
//4 //4
w32(MFID_4_Mic); w32(MFID_4_Mic);
@ -694,9 +697,10 @@ struct maple_microphone: maple_base
return MDRS_DeviceStatus; return MDRS_DeviceStatus;
//controller condition
case MDCF_GetCondition: case MDCF_GetCondition:
{ {
//this was copied from the controller case with just the id replaced!
//PlainJoystickState pjs; //PlainJoystickState pjs;
//config->GetInput(&pjs); //config->GetInput(&pjs);
//caps //caps
@ -738,8 +742,8 @@ struct maple_microphone: maple_base
{ {
//MONEY //MONEY
u32 function=r32(); u32 function=r32();
LOGD("maple_microphone::dma MDCF_MICControl function (1st word) %#010x\n", function); //LOGD("maple_microphone::dma MDCF_MICControl function (1st word) %#010x\n", function);
LOGD("maple_microphone::dma MDCF_MICControl words: %d\n", dma_count_in); //LOGD("maple_microphone::dma MDCF_MICControl words: %d\n", dma_count_in);
switch(function) switch(function)
{ {
@ -759,25 +763,32 @@ struct maple_microphone: maple_base
* *
*/ */
u32 subcommand=r32(); u32 subcommand=r32();
LOGD("maple_microphone::dma MDCF_MICControl subcommand (2nd word) %#010x\n", subcommand); //LOGD("maple_microphone::dma MDCF_MICControl subcommand (2nd word) %#010x\n", subcommand);
//u32 cmd = (number >> (8*n)) & 0xff
u32 cmd = subcommand & 0xFF;
LOGD("maple_microphone::dma MDCF_MICControl (3rd word) %#010x\n", r32()); u32 cmd = subcommand & 0xFF; //just get last byte for now, deal with params later
LOGD("maple_microphone::dma MDCF_MICControl (4th word) %#010x\n", r32());
//LOGD("maple_microphone::dma MDCF_MICControl (3rd word) %#010x\n", r32());
//LOGD("maple_microphone::dma MDCF_MICControl (4th word) %#010x\n", r32());
switch(cmd) switch(cmd)
{ {
case 0x01: case 0x01:
{
LOGD("maple_microphone::dma MDCF_MICControl someone wants some data!"); LOGD("maple_microphone::dma MDCF_MICControl someone wants some data!");
//maximum size of a Maple Bus packet is 256 words (1024 bytes)
//maybe i should start with 512 (same as vmu)
u8* micdata=(u8*)malloc(512);
get_mic_data(micdata); //this is crashing (jni issue)
wptr(micdata, 512);
return MDRS_DataTransfer; return MDRS_DataTransfer;
break; }
case 0x02: case 0x02:
LOGD("maple_microphone::dma MDCF_MICControl toggle recording!"); LOGD("maple_microphone::dma MDCF_MICControl toggle recording!");
//this is where i should start recording...
return MDRS_DeviceReply; return MDRS_DeviceReply;
break;
case 0x03: case 0x03:
return MDRS_DeviceReply; return MDRS_DeviceReply;
break;
default: default:
break; break;
} }

View File

@ -29,3 +29,4 @@ struct maple_device
}; };
maple_device* maple_Create(MapleDeviceType type); maple_device* maple_Create(MapleDeviceType type);
void get_mic_data(u8* buffer);

View File

@ -10,9 +10,11 @@
<uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/> <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
<uses-feature android:name="android.hardware.microphone" android:required="false"/>
<application <application
android:hardwareAccelerated="true" android:hardwareAccelerated="true"

View File

@ -35,8 +35,13 @@ extern "C"
//JNIEXPORT jint JNICALL Java_com_reicast_emulator_JNIdc_play(JNIEnv *env,jobject obj,jshortArray result,jint size); //JNIEXPORT jint JNICALL Java_com_reicast_emulator_JNIdc_play(JNIEnv *env,jobject obj,jshortArray result,jint size);
JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_initControllers(JNIEnv *env, jobject obj, jbooleanArray controllers) __attribute__((visibility("default"))); JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_initControllers(JNIEnv *env, jobject obj, jbooleanArray controllers) __attribute__((visibility("default")));
JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_setupMic(JNIEnv *env,jobject obj,jobject sip) __attribute__((visibility("default")));
}; };
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "Android.cpp JNI wtf", __VA_ARGS__)
void egl_stealcntx(); void egl_stealcntx();
void SetApplicationPath(wchar *path); void SetApplicationPath(wchar *path);
int dc_init(int argc,wchar* argv[]); int dc_init(int argc,wchar* argv[]);
@ -191,6 +196,9 @@ jshortArray jsamples;
jmethodID writemid; jmethodID writemid;
jobject track; jobject track;
jobject sipemu;
jmethodID getmicdata;
JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_run(JNIEnv *env,jobject obj,jobject trk) JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_run(JNIEnv *env,jobject obj,jobject trk)
{ {
install_prof_handler(0); install_prof_handler(0);
@ -204,6 +212,13 @@ JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_run(JNIEnv *env,jobject o
dc_run(); dc_run();
} }
JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_setupMic(JNIEnv *env,jobject obj,jobject sip)
{
sipemu = sip;
getmicdata = env->GetMethodID(env->GetObjectClass(sipemu),"getData","()[B");
}
JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_stop(JNIEnv *env,jobject obj) JNIEXPORT void JNICALL Java_com_reicast_emulator_JNIdc_stop(JNIEnv *env,jobject obj)
{ {
dc_term(); dc_term();
@ -330,3 +345,12 @@ bool os_IsAudioBuffered()
{ {
return jenv->CallIntMethod(track,writemid,jsamples,-1)==0; return jenv->CallIntMethod(track,writemid,jsamples,-1)==0;
} }
void get_mic_data(u8* buffer)
{
LOGD("get_mic_data called");
jbyteArray jdata = (jbyteArray)jenv->CallObjectMethod(sipemu,getmicdata);
LOGD("CallObjectMethod happened");
jenv->GetByteArrayRegion(jdata, 0, 512, (jbyte*)buffer);
LOGD("GetByteArrayRegion happened");
}

View File

@ -325,6 +325,13 @@ public class GL2JNIActivity extends Activity {
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
"Press the back button for a menu", Toast.LENGTH_SHORT).show(); "Press the back button for a menu", Toast.LENGTH_SHORT).show();
//totally hijacking this to setup mic
SipEmulator sip = new SipEmulator();
sip.startRecording();
JNIdc.setupMic(sip);
} }
private void runCompatibilityMode() { private void runCompatibilityMode() {

View File

@ -23,6 +23,8 @@ public class JNIdc
public static native void initControllers(boolean[] controllers); public static native void initControllers(boolean[] controllers);
public static native void setupMic(Object sip);
public static void show_osd() { public static void show_osd() {
JNIdc.vjoy(13, 1,0,0,0); JNIdc.vjoy(13, 1,0,0,0);
} }

View File

@ -0,0 +1,107 @@
package com.reicast.emulator;
import java.util.LinkedList;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;
public class SipEmulator extends Thread{
static final String TAG = "SipEmulator";
//one second of audio data in bytes
static final int BUFFER_SIZE = 22050;
//this needs to get set to the amount the mic normally sends per data request
//--->target 512!
static final int ONE_BLIP_SIZE = 512;
static final long TIME_TO_WAIT_BETWEEN_POLLS = 1000 / 44; //poll every ~23 ms
private AudioRecord record;
private LinkedList<byte[]> bytesReadBuffer;
private Thread recordThread;
private boolean continueRecording;
/*
16-bit PCM @ 11025 hz
== 176.4 kbit/s
== 22050 bytes/s
*/
public SipEmulator(){
Log.d(TAG, "SipEmulator constructor called");
init();
}
private void init(){
Log.d(TAG, "SipEmulator init called");
record = new AudioRecord(
MediaRecorder.AudioSource.MIC,
11025,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
BUFFER_SIZE);
bytesReadBuffer = new LinkedList<byte[]>();
}
public void startRecording(){
if(continueRecording){
return;
}
Log.d(TAG, "SipEmulator startRecording called");
record.startRecording();
continueRecording = true;
this.start();
}
public void stopRecording(){
Log.d(TAG, "SipEmulator stopRecording called");
record.stop();
continueRecording = false;
}
public byte[] getData(){
//Log.d(TAG, "SipEmulator getData called");
Log.d(TAG, "SipEmulator getData bytesReadBuffer size: "+bytesReadBuffer.size());
return bytesReadBuffer.poll();
}
public void configSomething(int what, int setting){
Log.d(TAG, "SipEmulator configSomething called");
}
public void run() {
Log.d(TAG, "recordThread starting");
//sleep to let some data come in
// try {
// Thread.sleep(TIME_TO_WAIT_BETWEEN_POLLS);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
while(continueRecording){
byte[] freshData = new byte[ONE_BLIP_SIZE];
int bytesRead = record.read(freshData, 0, ONE_BLIP_SIZE);
//Log.d(TAG, "recordThread recorded: "+bytesRead);
bytesReadBuffer.add(freshData);
try {
Thread.sleep(TIME_TO_WAIT_BETWEEN_POLLS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}