From e863d68885e9413ea3a1a30eb830892b34b0f6d7 Mon Sep 17 00:00:00 2001 From: enm10k Date: Thu, 8 Feb 2024 12:22:41 +0900 Subject: [PATCH] =?UTF-8?q?Marshal.PtrToStringAnsi=20=E3=81=AB=E6=88=BB?= =?UTF-8?q?=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sora/Sora.cs | 11 +++++------ src/unity.cpp | 11 ++++------- src/unity.h | 3 +-- src/unity_renderer.cpp | 7 ------- src/unity_renderer.h | 1 - 5 files changed, 10 insertions(+), 23 deletions(-) diff --git a/Sora/Sora.cs b/Sora/Sora.cs index 8f384ae..c81f776 100644 --- a/Sora/Sora.cs +++ b/Sora/Sora.cs @@ -868,9 +868,9 @@ public void GetStats(Action onGetStats) } public (bool, string) GetTrackId(uint trackId) { - bool found = false; - string trackId_ = get_track_id(trackId, ref found); - return (found, trackId_); + IntPtr p = get_track_id(trackId); + bool found = p != IntPtr.Zero; + return (found, found ? Marshal.PtrToStringAnsi(p) : ""); } /// @@ -1102,9 +1102,8 @@ 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); - [DllImport(DllName)] - [return: MarshalAs(UnmanagedType.LPStr)] - private static extern string get_track_id(uint track_id, [MarshalAs(UnmanagedType.I1)] ref bool found); + [DllImport(DllName, CharSet=CharSet.Ansi)] + private static extern IntPtr get_track_id(uint track_id); public class AudioOutputHelper : IDisposable { diff --git a/src/unity.cpp b/src/unity.cpp index 8afa332..269f4ac 100644 --- a/src/unity.cpp +++ b/src/unity.cpp @@ -310,14 +310,11 @@ void sora_audio_output_helper_set_handsfree(void* p, unity_bool_t enabled) { helper->SetHandsfree(enabled != 0); } -UNITY_INTERFACE_EXPORT const char* get_track_id(ptrid_t track_id, - unity_bool_t* found) { +UNITY_INTERFACE_EXPORT const char* get_track_id(ptrid_t track_id) { void* p = sora_unity_sdk::IdPointer::Instance().Lookup(track_id); - *found = p != nullptr ? 1 : 0; - - // return *found ? ((sora_unity_sdk::UnityRenderer::Sink*)p)->GetTrackId().c_str() - return *found ? ((sora_unity_sdk::UnityRenderer::Sink*)p)->GetTrackIdC() - : nullptr; + return p != nullptr + ? ((sora_unity_sdk::UnityRenderer::Sink*)p)->GetTrackId().c_str() + : nullptr; } // iOS の場合は static link で名前が被る可能性があるので、別の名前にしておく diff --git a/src/unity.h b/src/unity.h index b568fd6..4405d8e 100644 --- a/src/unity.h +++ b/src/unity.h @@ -126,8 +126,7 @@ sora_audio_output_helper_is_handsfree(void* p); UNITY_INTERFACE_EXPORT void sora_audio_output_helper_set_handsfree( void* p, unity_bool_t enabled); -UNITY_INTERFACE_EXPORT const char* get_track_id(ptrid_t track_id, - unity_bool_t* found); +UNITY_INTERFACE_EXPORT const char* get_track_id(ptrid_t track_id); #ifdef __cplusplus } diff --git a/src/unity_renderer.cpp b/src/unity_renderer.cpp index be711e8..936a620 100644 --- a/src/unity_renderer.cpp +++ b/src/unity_renderer.cpp @@ -17,10 +17,6 @@ UnityRenderer::Sink::Sink(webrtc::VideoTrackInterface* track) updating_ = false; ptrid_ = IdPointer::Instance().Register(this); track_->AddOrUpdateSink(this, rtc::VideoSinkWants()); - - size_t size = strlen(track_id_.c_str()); - track_id_c_ = new char[size + 1]; - strcpy(track_id_c_, track_id_.c_str()); } UnityRenderer::Sink::~Sink() { RTC_LOG(LS_INFO) << "[" << (void*)this << "] Sink::~Sink"; @@ -41,9 +37,6 @@ ptrid_t UnityRenderer::Sink::GetSinkID() const { const std::string& UnityRenderer::Sink::GetTrackId() const { return track_id_; } -char* UnityRenderer::Sink::GetTrackIdC() const { - return track_id_c_; -} void UnityRenderer::Sink::SetTrack(webrtc::VideoTrackInterface* track) { track_->RemoveSink(this); diff --git a/src/unity_renderer.h b/src/unity_renderer.h index c1a50d2..f388397 100644 --- a/src/unity_renderer.h +++ b/src/unity_renderer.h @@ -32,7 +32,6 @@ class UnityRenderer { ~Sink(); ptrid_t GetSinkID() const; const std::string& GetTrackId() const; - char* GetTrackIdC() const; void SetTrack(webrtc::VideoTrackInterface* track); private: