Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions host/simaudio/simaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <mach/mach_time.h>
#include <dispatch/dispatch.h>
Expand Down Expand Up @@ -141,15 +143,16 @@ struct dev {
struct ioproc procs[MAXPROCS];
pthread_t thread; int threadrun;
int sock; // Linux connection
int active_lock_fd; // flock'd marker for RunningBoard active-audio gating (output only)
uint64_t t0; double sample0; // host time at sample time sample0 (anchor of the clock)
float* ring; size_t ringcap, ringr, ringw; // input: frames read from Linux
pthread_mutex_t lock;
float volume; int mute;
float appvol; int appmute; // the process's own volume / mute ('appv' / 'appm', AudioSession sets it from sim_volume)
};
static struct dev devs[2] = {
{ kOutDev, kOutStream, 0, 2, 48000, 512, .sock = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 },
{ kInDev, kInStream, 1, 1, 48000, 512, .sock = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 },
{ kOutDev, kOutStream, 0, 2, 48000, 512, .sock = -1, .active_lock_fd = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 },
{ kInDev, kInStream, 1, 1, 48000, 512, .sock = -1, .active_lock_fd = -1, .lock = PTHREAD_MUTEX_INITIALIZER, .volume = 1, .appvol = 1 },
};
static struct dev* devof(AudioObjectID o) {
if (o == kOutDev || o == kOutStream || o == kOutAgg || o == kIOAgg) return &devs[0];
Expand Down Expand Up @@ -363,6 +366,46 @@ static void* iothread(void* arg) {
return NULL;
}

static int valid_bundle_component(const char* s) {
if (!s || !*s) return 0;
for (const char* p = s; *p; p++) {
unsigned char c = (unsigned char)*p;
int ok = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-';
if (!ok) return 0;
if (c == '.' && p[1] == '.') return 0;
}
return 1;
}

static void audio_lock_acquire(struct dev* d) {
if (d->input || d->active_lock_fd >= 0) return;
const char* sock = getenv("X86SIM_AUDIO_SOCKET");
if (!sock || !*sock) return;
const char* slash = strrchr(sock, '/');
if (!slash || slash == sock) return;
CFBundleRef mb = CFBundleGetMainBundle();
CFStringRef bid = mb ? CFBundleGetIdentifier(mb) : NULL;
char bstr[128];
if (!bid || !CFStringGetCString(bid, bstr, sizeof(bstr), kCFStringEncodingUTF8) || !valid_bundle_component(bstr)) return;
char dir_path[512], lock_path[640];
int n = snprintf(dir_path, sizeof(dir_path), "%.*s/simaudio-out-bundle", (int)(slash - sock), sock);
if (n <= 0 || (size_t)n >= sizeof(dir_path)) return;
mkdir(dir_path, 0755);
n = snprintf(lock_path, sizeof(lock_path), "%s/%s.lock", dir_path, bstr);
if (n <= 0 || (size_t)n >= sizeof(lock_path)) return;
int fd = open(lock_path, O_CREAT | O_RDWR | O_CLOEXEC, 0644);
if (fd < 0) return;
if (flock(fd, LOCK_EX | LOCK_NB) == 0) d->active_lock_fd = fd;
else close(fd);
}

static void audio_lock_release(struct dev* d) {
if (d->active_lock_fd < 0) return;
flock(d->active_lock_fd, LOCK_UN);
close(d->active_lock_fd);
d->active_lock_fd = -1;
}

static void dev_start(struct dev* d) {
if (d->threadrun) return;
timebase();
Expand All @@ -372,6 +415,8 @@ static void dev_start(struct dev* d) {
d->ringr = d->ringw = 0;
d->sock = connect_linux(d);
if (d->sock >= 0) { pthread_t t; pthread_create(&t, NULL, input_reader, d); pthread_detach(t); }
} else {
audio_lock_acquire(d);
}
d->threadrun = 1;
pthread_create(&d->thread, NULL, iothread, d);
Expand All @@ -384,6 +429,7 @@ static void dev_stop(struct dev* d) {
if (!pthread_equal(pthread_self(), d->thread)) pthread_join(d->thread, NULL); else pthread_detach(d->thread);
d->sample0 += 0; // the sample clock continues from the anchor on the next start
if (d->sock >= 0) { shutdown(d->sock, SHUT_RDWR); close(d->sock); d->sock = -1; }
audio_lock_release(d);
notify(d->id, FCC("goin"), FCC("glob"));
}

Expand Down
52 changes: 50 additions & 2 deletions host/simshim/compat/simshim.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <libkern/OSCacheControl.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -1000,13 +1001,60 @@ __attribute__((constructor)) static void shim_owner_init(void)
// real value.
static uintptr_t shim_rb_site; // return address of _lock_suspend's [props supportsBackgroundAudio]
static IMP shim_rb_audio_orig[2];
static int shim_rb_valid_bundle(const char* s)
{
if (!s || !*s)
return 0;
for (const char* p = s; *p; p++) {
unsigned char c = (unsigned char) *p;
int ok = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-';
if (!ok || (c == '.' && p[1] == '.'))
return 0;
}
return 1;
}
// Keep background-audio apps running while libsimaudio holds an active output stream lock
// (standing in for mediaserverd's RunningBoard running assertion on a real iPhone); still suspend
// idle UIBackgroundModes:["audio"] apps (Maps, Health) once playback stops or after SIGKILL.
static int shim_rb_audio_stream_active(id self)
{
const char* sock = getenv("X86SIM_AUDIO_SOCKET");
if (!sock || !*sock)
return 0;
const char* slash = strrchr(sock, '/');
if (!slash || slash == sock)
return 0;
SEL bsel = sel_registerName("bundleIdentifier");
if (!((BOOL (*)(id, SEL, SEL)) objc_msgSend)(self, sel_registerName("respondsToSelector:"), bsel))
return 0;
id bid = ((id (*)(id, SEL)) objc_msgSend)(self, bsel);
if (!bid || CFGetTypeID((CFTypeRef) bid) != CFStringGetTypeID())
return 0;
char bstr[128];
if (!CFStringGetCString((CFStringRef) bid, bstr, sizeof bstr, kCFStringEncodingUTF8) || !shim_rb_valid_bundle(bstr))
return 0;
char lock_path[640];
int n = snprintf(lock_path, sizeof lock_path, "%.*s/simaudio-out-bundle/%s.lock", (int) (slash - sock), sock, bstr);
if (n <= 0 || (size_t) n >= sizeof lock_path)
return 0;
int fd = open(lock_path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
return 0;
int busy = (flock(fd, LOCK_EX | LOCK_NB) != 0 && (errno == EWOULDBLOCK || errno == EAGAIN));
if (!busy)
flock(fd, LOCK_UN);
close(fd);
return busy;
}
static BOOL shim_rb_audio(id self, SEL sel, int k, uintptr_t ret)
{
BOOL r = ((BOOL (*)(id, SEL)) shim_rb_audio_orig[k])(self, sel);
if (ret != shim_rb_site)
if (ret != shim_rb_site || !r)
return r;
if (shim_rb_audio_stream_active(self))
return YES;
static int n;
if (r && n++ < 20)
if (n++ < 20)
write(2, "simshim: RunningBoard suspends a background-audio app\n", 54);
return NO;
}
Expand Down
Loading