Skip to content

Commit

Permalink
Merge pull request #42 from digitalgust/dev
Browse files Browse the repository at this point in the history
fix miniaudio not close error on minijvm exit
  • Loading branch information
digitalgust authored Feb 22, 2024
2 parents 1e770a4 + db3547d commit 99d9790
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 37 deletions.
39 changes: 31 additions & 8 deletions desktop/glfw_gui/c/deps/include/minial/miniaudio.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Audio playback and capture library. Choice of public domain or MIT-0. See license statements at the end of this file.
miniaudio - v0.11.20 - TBD
miniaudio - v0.11.21 - 2023-11-15

David Reid - mackron@gmail.com

Expand Down Expand Up @@ -3723,7 +3723,7 @@ extern "C" {

#define MA_VERSION_MAJOR 0
#define MA_VERSION_MINOR 11
#define MA_VERSION_REVISION 20
#define MA_VERSION_REVISION 21
#define MA_VERSION_STRING MA_XSTRINGIFY(MA_VERSION_MAJOR) "." MA_XSTRINGIFY(MA_VERSION_MINOR) "." MA_XSTRINGIFY(MA_VERSION_REVISION)

#if defined(_MSC_VER) && !defined(__clang__)
Expand Down Expand Up @@ -6716,7 +6716,8 @@ typedef enum
ma_device_notification_type_stopped,
ma_device_notification_type_rerouted,
ma_device_notification_type_interruption_began,
ma_device_notification_type_interruption_ended
ma_device_notification_type_interruption_ended,
ma_device_notification_type_unlocked
} ma_device_notification_type;

typedef struct
Expand Down Expand Up @@ -18668,6 +18669,14 @@ static void ma_device__on_notification_rerouted(ma_device* pDevice)
}
#endif

#if defined(MA_EMSCRIPTEN)
EMSCRIPTEN_KEEPALIVE
void ma_device__on_notification_unlocked(ma_device* pDevice)
{
ma_device__on_notification(ma_device_notification_init(pDevice, ma_device_notification_type_unlocked));
}
#endif


static void ma_device__on_data_inner(ma_device* pDevice, void* pFramesOut, const void* pFramesIn, ma_uint32 frameCount)
{
Expand Down Expand Up @@ -32797,9 +32806,9 @@ static ma_result ma_find_best_format__coreaudio(ma_context* pContext, AudioObjec

hasSupportedFormat = MA_FALSE;
for (iFormat = 0; iFormat < deviceFormatDescriptionCount; ++iFormat) {
ma_format format;
ma_result formatResult = ma_format_from_AudioStreamBasicDescription(&pDeviceFormatDescriptions[iFormat].mFormat, &format);
if (formatResult == MA_SUCCESS && format != ma_format_unknown) {
ma_format formatFromDescription;
ma_result formatResult = ma_format_from_AudioStreamBasicDescription(&pDeviceFormatDescriptions[iFormat].mFormat, &formatFromDescription);
if (formatResult == MA_SUCCESS && formatFromDescription != ma_format_unknown) {
hasSupportedFormat = MA_TRUE;
bestDeviceFormatSoFar = pDeviceFormatDescriptions[iFormat].mFormat;
break;
Expand Down Expand Up @@ -39800,6 +39809,7 @@ static ma_result ma_device_uninit__webaudio(ma_device* pDevice)
*/
device.webaudio.close();
device.webaudio = undefined;
device.pDevice = undefined;
}, pDevice->webaudio.deviceIndex);
}
#endif
Expand All @@ -39823,6 +39833,10 @@ static ma_uint32 ma_calculate_period_size_in_frames_from_descriptor__webaudio(co
*/
ma_uint32 periodSizeInFrames;

if (nativeSampleRate == 0) {
nativeSampleRate = MA_DEFAULT_SAMPLE_RATE;
}

if (pDescriptor->periodSizeInFrames == 0) {
if (pDescriptor->periodSizeInMilliseconds == 0) {
if (performanceProfile == ma_performance_profile_low_latency) {
Expand Down Expand Up @@ -40295,6 +40309,8 @@ static ma_result ma_device_init__webaudio(ma_device* pDevice, const ma_device_co
device.scriptNode.connect(device.webaudio.destination);
}

device.pDevice = pDevice;

return miniaudio.track_device(device);
}, pConfig->deviceType, channels, sampleRate, periodSizeInFrames, pDevice->webaudio.pIntermediaryBuffer, pDevice);

Expand Down Expand Up @@ -40467,8 +40483,15 @@ static ma_result ma_context_init__webaudio(ma_context* pContext, const ma_contex
miniaudio.unlock = function() {
for(var i = 0; i < miniaudio.devices.length; ++i) {
var device = miniaudio.devices[i];
if (device != null && device.webaudio != null && device.state === 2 /* ma_device_state_started */) {
device.webaudio.resume();
if (device != null &&
device.webaudio != null &&
device.state === window.miniaudio.device_state.started) {

device.webaudio.resume().then(() => {
Module._ma_device__on_notification_unlocked(device.pDevice);
},
(error) => {console.error("Failed to resume audiocontext", error);
});
}
}
miniaudio.unlock_event_types.map(function(event_type) {
Expand Down
18 changes: 15 additions & 3 deletions desktop/glfw_gui/c/jni_minial.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void setupCallback(Runtime *runtime, JClass *clazz) {
c8 *type_s;
c8 *clsname_s;
if (!refers._callback_minial_on_recv_frames) {
clsname_s = "org/mini/media/AudioDevice";
clsname_s = "org/mini/media/MADevice";
name_s = "onReceiveFrames";
type_s = "(JIJ)V";
Utf8String *clsname = env->utf8_create_part_c(clsname_s, 0, strlen(clsname_s));
Expand All @@ -337,7 +337,7 @@ void setupCallback(Runtime *runtime, JClass *clazz) {
env->utf8_destory(type);
}
if (!refers._callback_minial_on_send_frames) {
clsname_s = "org/mini/media/AudioDevice";
clsname_s = "org/mini/media/MADevice";
name_s = "onSendFrames";
type_s = "(JIJ)I";
Utf8String *clsname = env->utf8_create_part_c(clsname_s, 0, strlen(clsname_s));
Expand All @@ -349,7 +349,7 @@ void setupCallback(Runtime *runtime, JClass *clazz) {
env->utf8_destory(type);
}
if (!refers._callback_minial_on_stop) {
clsname_s = "org/mini/media/AudioDevice";
clsname_s = "org/mini/media/MADevice";
name_s = "onStop";
type_s = "(J)V";
Utf8String *clsname = env->utf8_create_part_c(clsname_s, 0, strlen(clsname_s));
Expand Down Expand Up @@ -504,6 +504,17 @@ int org_mini_media_MiniAudio_ma_engine_stop(Runtime *runtime, JClass *clazz) {
return 0;
}

int org_mini_media_MiniAudio_ma_engine_get_device(Runtime *runtime, JClass *clazz) {
JniEnv *env = runtime->jnienv;
s32 pos = 0;
ma_engine *handle_engine = (__refer) (intptr_t) env->localvar_getLong_2slot(runtime->localvar, pos);
pos += 2;

s64 r = (s64) (intptr_t) ma_engine_get_device(handle_engine);
env->push_long(runtime->stack, r);
return 0;
}

int org_mini_media_MiniAudio_ma_engine_get_channels(Runtime *runtime, JClass *clazz) {
JniEnv *env = runtime->jnienv;
s32 pos = 0;
Expand Down Expand Up @@ -905,6 +916,7 @@ static java_native_method method_minial_table[] = {
{"org/mini/media/MiniAudio", "ma_engine_play_sound", "(J[B)I", org_mini_media_MiniAudio_ma_engine_play_sound},
{"org/mini/media/MiniAudio", "ma_engine_start", "(J)I", org_mini_media_MiniAudio_ma_engine_start},
{"org/mini/media/MiniAudio", "ma_engine_stop", "(J)I", org_mini_media_MiniAudio_ma_engine_stop},
{"org/mini/media/MiniAudio", "ma_engine_get_device", "(J)J", org_mini_media_MiniAudio_ma_engine_get_device},
{"org/mini/media/MiniAudio", "ma_engine_get_channels", "(J)I", org_mini_media_MiniAudio_ma_engine_get_channels},
{"org/mini/media/MiniAudio", "ma_engine_get_sample_rate", "(J)I", org_mini_media_MiniAudio_ma_engine_get_sample_rate},
{"org/mini/media/MiniAudio", "ma_engine_get_format", "(J)I", org_mini_media_MiniAudio_ma_engine_get_format},
Expand Down
2 changes: 1 addition & 1 deletion desktop/glfw_gui/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>io.github.digitalgust</groupId>
<artifactId>glfw_gui</artifactId>
<name>${project.groupId}:${project.artifactId}</name>
<version>1.1.2</version>
<version>1.1.3</version>
<description>miniJVM desktop platform gui library</description>
<url>https://github.com/digitalgust/miniJVM</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public class MiniAudio {

public static native int ma_engine_stop(long handle_engine);

public static native long ma_engine_get_device(long handle_engine);

public static native int ma_engine_get_channels(long handle_engine);

public static native int ma_engine_get_sample_rate(long handle_engine);
Expand Down
6 changes: 3 additions & 3 deletions extlib/xgui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<groupId>io.github.digitalgust</groupId>
<artifactId>xgui</artifactId>
<name>${project.groupId}:${project.artifactId}</name>
<version>1.1.6</version>
<version>1.1.7</version>
<description>miniJVM mobile platform gui library</description>
<url>https://github.com/digitalgust/miniJVM</url>

Expand Down Expand Up @@ -158,12 +158,12 @@
<dependency>
<groupId>io.github.digitalgust</groupId>
<artifactId>glfw_gui</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>io.github.digitalgust</groupId>
<artifactId>glfm_gui</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
</dependency>
</dependencies>

Expand Down
3 changes: 3 additions & 0 deletions extlib/xgui/src/main/java/org/mini/glfw/GlfwCallBackImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.mini.gui.GCallBack;
import org.mini.gui.GForm;
import org.mini.gui.GToolkit;
import org.mini.media.MaDevice;
import org.mini.media.MiniAudio;

import java.io.File;

Expand Down Expand Up @@ -191,6 +193,7 @@ public void mainLoop() {
public void destroy() {
nvgDeleteGL3(vg);
Glfw.glfwTerminate();
MaDevice.stopAll();
vg = 0;
System.exit(0);//some thread not exit ,that will continue running
}
Expand Down
19 changes: 19 additions & 0 deletions extlib/xgui/src/main/java/org/mini/media/MaDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.mini.reflect.DirectMemObj;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
Expand Down Expand Up @@ -100,6 +101,7 @@ void checkThread() {
public void finalize() {
if (handle != 0) {
MiniAudio.ma_device_uninit(handle);
devices.remove(handle);
handle = 0;
}
}
Expand All @@ -119,6 +121,23 @@ public static int getFormatBytes(int format) {
return 0;
}

public static void putDevice(long deviceHandle) {
devices.put(deviceHandle, null);
}

public static void removeDevice(long deviceHandle) {
devices.remove(deviceHandle);
}

public static void stopAll() {
for (Iterator<Long> it = devices.keySet().iterator(); it.hasNext(); ) {
Long deviceHanle = it.next();
if (deviceHanle != null) {
MiniAudio.ma_device_stop(deviceHanle.longValue());
}
}
}

/**
* =================================================================================================
* <p>
Expand Down
5 changes: 5 additions & 0 deletions extlib/xgui/src/main/java/org/mini/media/engine/MaEngine.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mini.media.engine;

import org.mini.media.MaDevice;
import org.mini.media.MaNativeObject;
import org.mini.media.MiniAudio;

Expand All @@ -9,13 +10,16 @@ public class MaEngine extends MaNativeObject {
int format;
int channels;
int ratio;
long device;

public MaEngine() {
handle = MiniAudio.ma_engine_init();
if (handle != 0) {
format = MiniAudio.ma_engine_get_format(handle);
channels = MiniAudio.ma_engine_get_channels(handle);
ratio = MiniAudio.ma_engine_get_sample_rate(handle);
device = MiniAudio.ma_engine_get_device(handle);
MaDevice.putDevice(device);
}
}

Expand Down Expand Up @@ -72,6 +76,7 @@ public float getVolume() {
public void finalize() {
System.out.println("clean " + this + " " + handle);
MiniAudio.ma_engine_uninit(handle);
MaDevice.removeDevice(handle);
handle = 0;
}

Expand Down
8 changes: 0 additions & 8 deletions extlib/xgui/xgui.iml

This file was deleted.

4 changes: 2 additions & 2 deletions minijvm/c/jvm/jdwp.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ s32 jdwp_thread_dispacher(void *para) {
arraylist_remove(jdwpserver->clients, client);
}
}
threadSleep(10);
threadSleep(20);
}
jdwpserver->mode &= ~JDWP_MODE_DISPATCH;
return 0;
Expand Down Expand Up @@ -1562,7 +1562,7 @@ s32 jdwp_client_process(JdwpServer *jdwpserver, JdwpClient *client) {
jdwp_packet_put(jdwpserver, res);
utf8_destory(ustr);
while (!jvm->thread_list->length) {
threadSleep(10);
threadSleep(20);
}
Runtime *mainthread = (Runtime *) arraylist_get_value(jdwpserver->jvm->thread_list, 0);
if (jdwpserver->jvm->jdwp_suspend_on_start)event_on_vmstart(jdwpserver, mainthread->thrd_info->jthread);
Expand Down
18 changes: 15 additions & 3 deletions mobile/c/gui/jni_minial.m
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void setupCallback(Runtime *runtime, JClass *clazz) {
c8 *type_s;
c8 *clsname_s;
if (!refers._callback_minial_on_recv_frames) {
clsname_s = "org/mini/media/AudioDevice";
clsname_s = "org/mini/media/MADevice";
name_s = "onReceiveFrames";
type_s = "(JIJ)V";
Utf8String *clsname = env->utf8_create_part_c(clsname_s, 0, strlen(clsname_s));
Expand All @@ -337,7 +337,7 @@ void setupCallback(Runtime *runtime, JClass *clazz) {
env->utf8_destory(type);
}
if (!refers._callback_minial_on_send_frames) {
clsname_s = "org/mini/media/AudioDevice";
clsname_s = "org/mini/media/MADevice";
name_s = "onSendFrames";
type_s = "(JIJ)I";
Utf8String *clsname = env->utf8_create_part_c(clsname_s, 0, strlen(clsname_s));
Expand All @@ -349,7 +349,7 @@ void setupCallback(Runtime *runtime, JClass *clazz) {
env->utf8_destory(type);
}
if (!refers._callback_minial_on_stop) {
clsname_s = "org/mini/media/AudioDevice";
clsname_s = "org/mini/media/MADevice";
name_s = "onStop";
type_s = "(J)V";
Utf8String *clsname = env->utf8_create_part_c(clsname_s, 0, strlen(clsname_s));
Expand Down Expand Up @@ -504,6 +504,17 @@ int org_mini_media_MiniAudio_ma_engine_stop(Runtime *runtime, JClass *clazz) {
return 0;
}

int org_mini_media_MiniAudio_ma_engine_get_device(Runtime *runtime, JClass *clazz) {
JniEnv *env = runtime->jnienv;
s32 pos = 0;
ma_engine *handle_engine = (__refer) (intptr_t) env->localvar_getLong_2slot(runtime->localvar, pos);
pos += 2;

s64 r = (s64) (intptr_t) ma_engine_get_device(handle_engine);
env->push_long(runtime->stack, r);
return 0;
}

int org_mini_media_MiniAudio_ma_engine_get_channels(Runtime *runtime, JClass *clazz) {
JniEnv *env = runtime->jnienv;
s32 pos = 0;
Expand Down Expand Up @@ -905,6 +916,7 @@ int org_mini_media_MiniAudio_ma_sound_at_end(Runtime *runtime, JClass *clazz) {
{"org/mini/media/MiniAudio", "ma_engine_play_sound", "(J[B)I", org_mini_media_MiniAudio_ma_engine_play_sound},
{"org/mini/media/MiniAudio", "ma_engine_start", "(J)I", org_mini_media_MiniAudio_ma_engine_start},
{"org/mini/media/MiniAudio", "ma_engine_stop", "(J)I", org_mini_media_MiniAudio_ma_engine_stop},
{"org/mini/media/MiniAudio", "ma_engine_get_device", "(J)J", org_mini_media_MiniAudio_ma_engine_get_device},
{"org/mini/media/MiniAudio", "ma_engine_get_channels", "(J)I", org_mini_media_MiniAudio_ma_engine_get_channels},
{"org/mini/media/MiniAudio", "ma_engine_get_sample_rate", "(J)I", org_mini_media_MiniAudio_ma_engine_get_sample_rate},
{"org/mini/media/MiniAudio", "ma_engine_get_format", "(J)I", org_mini_media_MiniAudio_ma_engine_get_format},
Expand Down
Loading

0 comments on commit 99d9790

Please sign in to comment.