Skip to content

Commit

Permalink
AudioOutputHelper のエスパー実装
Browse files Browse the repository at this point in the history
  • Loading branch information
melpon committed Sep 12, 2023
1 parent 081d8aa commit 60d0b1d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Sora/Sora.cs
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,57 @@ public string ConnectedSignalingURL
private static extern void sora_get_selected_signaling_url(IntPtr p, [Out] byte[] buf, int size);
[DllImport(DllName)]
private static extern void sora_get_connected_signaling_url(IntPtr p, [Out] byte[] buf, int size);

class AudioOutputHelper : IDisposable
{
private delegate void ChangeRouteCallbackDelegate(IntPtr userdata);

[AOT.MonoPInvokeCallback(typeof(ChangeRouteCallbackDelegate))]
static private void ChangeRouteCallback(IntPtr userdata)
{
var callback = GCHandle.FromIntPtr(userdata).Target as Action;
callback();
}

GCHandle onChangeRouteHandle;
IntPtr p;

public AudioOutputHelper(Action onChangeRoute)
{
onChangeRouteHandle = GCHandle.Alloc(onChangeRoute);
p = sora_audio_output_helper_create(ChangeRouteCallback, GCHandle.ToIntPtr(onChangeRouteHandle));
}

public void Dispose()
{
sora_audio_output_helper_destroy(p);
onChangeRouteHandle.Free();
}

public bool IsHandsfree()
{
return sora_audio_output_helper_is_handsfree(p) != 0;
}

public void SetHandsfree(bool enabled)
{
sora_audio_output_helper_set_handsfree(p, enabled ? 1 : 0);
}

#if UNITY_IOS && !UNITY_EDITOR
private const string DllName = "__Internal";
#else
private const string DllName = "SoraUnitySdk";
#endif

[DllImport(DllName)]
private static extern IntPtr sora_audio_output_helper_create(change_route_cb_t cb, IntPtr userdata);
[DllImport(DllName)]
private static extern void sora_audio_output_helper_destroy(IntPtr p);
[DllImport(DllName)]
private static extern int sora_audio_output_helper_is_handsfree(IntPtr p);
[DllImport(DllName)]
private static extern void sora_audio_output_helper_set_handsfree(IntPtr p, int enabled);
}

}
35 changes: 35 additions & 0 deletions src/unity.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "unity.h"

// Sora
#include <sora/audio_output_helper.h>

#include "device_list.h"
#include "sora.h"
#include "sora_conf.json.h"
Expand Down Expand Up @@ -274,6 +278,37 @@ void sora_get_connected_signaling_url(void* p, void* buf, int size) {
std::memcpy(buf, str.c_str(), std::min(size, (int)str.size()));
}

struct AudioOutputHelperImpl : public sora::AudioOutputHelper {
AudioOutputHelperImpl(change_route_cb_t cb, void* userdata)
: helper_(sora::CreateAudioOutputHelper()),
cb_(cb),
userdata_(userdata) {}
void OnChangeRoute() override { cb_(userdata_); }

bool IsHandsFree() override { return helper_->IsHandsFree(); }
void SetHandsFree(bool enabled) override { helper_->SetHandsFree(enabled); }

private:
std::unique_ptr<sora::AudioOutputHelper> helper_;
change_route_cb_t cb_;
void* userdata_;
};

void* sora_audio_output_helper_create(change_route_cb_t cb, void* userdata) {
return new AudioOutputHelperImpl(cb, userdata);
}
void sora_audio_output_helper_destroy(void* p) {
delete (AudioOutputHelperImpl*)p;
}
unity_bool_t sora_audio_output_helper_is_handsfree(void* p) {
auto helper = (AudioOutputHelperImpl*)p;
return helper->IsHandsFree() ? 1 : 0;
}
void sora_audio_output_helper_set_handsfree(void* p, unity_bool_t enabled) {
auto helper = (AudioOutputHelperImpl*)p;
helper->SetHandsFree(enabled != 0);
}

// iOS の場合は static link で名前が被る可能性があるので、別の名前にしておく
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
#if defined(SORA_UNITY_SDK_IOS)
Expand Down
11 changes: 11 additions & 0 deletions src/unity.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ UNITY_INTERFACE_EXPORT void sora_get_connected_signaling_url(void* p,
void* buf,
int size);

typedef void (*change_route_cb_t)(void* userdata);
UNITY_INTERFACE_EXPORT void* sora_audio_output_helper_create(
change_route_cb_t cb,
void* userdata);
UNITY_INTERFACE_EXPORT void sora_audio_output_helper_destroy(void* p);
UNITY_INTERFACE_EXPORT unity_bool_t
sora_audio_output_helper_is_handsfree(void* p);
UNITY_INTERFACE_EXPORT void sora_audio_output_helper_set_handsfree(
void* p,
unity_bool_t enabled);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 60d0b1d

Please sign in to comment.