From cdaf50de8f997fa4eb6250490414320fa8737c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Brzezi=C5=84ski?= Date: Tue, 21 May 2024 17:49:42 +0200 Subject: [PATCH] osxaudio: Avoid using private APIs on iOS Turns out AudioConvertHostTimeToNanos and AudioGetCurrentHostTime are macOS-only APIs, which prevents apps using GStreamer on iOS from being accepted into App Store. This commit replaces those functions with a manual version of what they do - mach_absolute_time() for the current time, and data from mach_timebase_info() at the beginning to convert host timestamps to nanoseconds. Part-of: --- .../sys/osxaudio/gstosxcoreaudio.c | 26 +++++++++++++++---- .../sys/osxaudio/gstosxcoreaudio.h | 4 ++- .../sys/osxaudio/gstosxcoreaudiocommon.h | 4 +++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.c b/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.c index 758a4622e5..3d6a88a63b 100644 --- a/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.c +++ b/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.c @@ -65,6 +65,7 @@ gst_core_audio_init (GstCoreAudio * core_audio) core_audio->disabled_mixing = FALSE; #endif + mach_timebase_info (&core_audio->timebase); g_mutex_init (&core_audio->timing_lock); } @@ -108,6 +109,21 @@ _audio_unit_property_listener (void *inRefCon, AudioUnit inUnit, } } +static GstClockTime +_current_time_ns (GstCoreAudio * core_audio) +{ + guint64 mach_t = mach_absolute_time (); + return gst_util_uint64_scale (mach_t, core_audio->timebase.numer, + core_audio->timebase.denom); +} + +static GstClockTime +_host_time_to_ns (GstCoreAudio * core_audio, uint64_t host_time) +{ + return gst_util_uint64_scale (host_time, core_audio->timebase.numer, + core_audio->timebase.denom); +} + /************************** * Public API * *************************/ @@ -214,7 +230,7 @@ gboolean gst_core_audio_get_samples_and_latency (GstCoreAudio * core_audio, gdouble rate, guint * samples, gdouble * latency) { - uint64_t now_ns = AudioConvertHostTimeToNanos (AudioGetCurrentHostTime ()); + uint64_t now_ns = _current_time_ns (core_audio); gboolean ret = gst_core_audio_get_samples_and_latency_impl (core_audio, rate, samples, latency); @@ -224,8 +240,7 @@ gst_core_audio_get_samples_and_latency (GstCoreAudio * core_audio, CORE_AUDIO_TIMING_LOCK (core_audio); uint32_t samples_remain = 0; - uint64_t anchor_ns = - AudioConvertHostTimeToNanos (core_audio->anchor_hosttime); + uint64_t anchor_ns = core_audio->anchor_hosttime_ns; if (core_audio->is_src) { int64_t captured_ns = @@ -292,14 +307,15 @@ gst_core_audio_update_timing (GstCoreAudio * core_audio, kAudioTimeStampSampleHostTimeValid | kAudioTimeStampRateScalarValid; if ((inTimeStamp->mFlags & target_flags) == target_flags) { - core_audio->anchor_hosttime = inTimeStamp->mHostTime; + core_audio->anchor_hosttime_ns = + _host_time_to_ns (core_audio, inTimeStamp->mHostTime); core_audio->anchor_pend_samples = inNumberFrames; core_audio->rate_scalar = inTimeStamp->mRateScalar; GST_DEBUG_OBJECT (core_audio, "anchor hosttime_ns %" G_GUINT64_FORMAT " scalar_rate %f anchor_pend_samples %u", - AudioConvertHostTimeToNanos (core_audio->anchor_hosttime), + core_audio->anchor_hosttime_ns, core_audio->rate_scalar, core_audio->anchor_pend_samples); } } diff --git a/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.h b/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.h index 1a1eed6eea..2ea32f2e3b 100644 --- a/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.h +++ b/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudio.h @@ -45,6 +45,7 @@ #endif #endif #include +#include #include "gstosxaudioelement.h" @@ -111,8 +112,9 @@ struct _GstCoreAudio AudioDeviceIOProcID procID; #endif + mach_timebase_info_data_t timebase; GMutex timing_lock; - uint64_t anchor_hosttime; + uint64_t anchor_hosttime_ns; uint32_t anchor_pend_samples; float rate_scalar; }; diff --git a/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudiocommon.h b/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudiocommon.h index c4602a6b30..ba6dcffd3e 100644 --- a/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudiocommon.h +++ b/subprojects/gst-plugins-good/sys/osxaudio/gstosxcoreaudiocommon.h @@ -24,6 +24,8 @@ #include "gstosxcoreaudio.h" #include +G_BEGIN_DECLS + typedef struct { GMutex lock; @@ -64,3 +66,5 @@ OSStatus gst_core_audio_render_notify (GstCoreAudio * core_a AudioChannelLabel gst_audio_channel_position_to_core_audio (GstAudioChannelPosition position, int channel); GstAudioChannelPosition gst_core_audio_channel_label_to_gst (AudioChannelLabel label, int channel, gboolean warn); + +G_END_DECLS