From a7f3cf473707b19bff9ea80b227490c8b305b601 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 9 Mar 2018 06:00:51 +0100 Subject: client API: add mpv_create_weak_client() --- player/client.c | 23 +++++++++++++++++++++-- player/client.h | 1 + player/scripting.c | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) (limited to 'player') diff --git a/player/client.c b/player/client.c index a887a16d2a..d347033fe5 100644 --- a/player/client.c +++ b/player/client.c @@ -136,6 +136,7 @@ struct mpv_handle { uint64_t property_event_masks; // or-ed together event masks of all properties bool fuzzy_initialized; // see scripting.c wait_loaded() + bool is_weak; // can not keep core alive on its own struct mp_log_buffer *messages; }; @@ -269,6 +270,13 @@ struct mpv_handle *mp_new_client(struct mp_client_api *clients, const char *name return client; } +void mp_client_set_weak(struct mpv_handle *ctx) +{ + pthread_mutex_lock(&ctx->lock); + ctx->is_weak = true; + pthread_mutex_unlock(&ctx->lock); +} + const char *mpv_client_name(mpv_handle *ctx) { return ctx->name; @@ -416,8 +424,11 @@ static void mp_destroy_client(mpv_handle *ctx, bool terminate) if (mpctx->is_cli) { terminate = false; } else { - // If the last mpv_handle got destroyed, destroy the core. - if (clients->num_clients == 0) + // If the last strong mpv_handle got destroyed, destroy the core. + bool has_strong_ref = false; + for (int n = 0; n < clients->num_clients; n++) + has_strong_ref |= !clients->clients[n]->is_weak; + if (!has_strong_ref) terminate = true; // Reserve the right to destroy mpctx for us. @@ -550,6 +561,14 @@ mpv_handle *mpv_create_client(mpv_handle *ctx, const char *name) return new; } +mpv_handle *mpv_create_weak_client(mpv_handle *ctx, const char *name) +{ + mpv_handle *new = mpv_create_client(ctx, name); + if (new) + mp_client_set_weak(new); + return new; +} + int mpv_initialize(mpv_handle *ctx) { lock_core(ctx); diff --git a/player/client.h b/player/client.h index deec3c793b..9ecbe2ed35 100644 --- a/player/client.h +++ b/player/client.h @@ -32,6 +32,7 @@ bool mp_client_event_is_registered(struct MPContext *mpctx, int event); void mp_client_property_change(struct MPContext *mpctx, const char *name); struct mpv_handle *mp_new_client(struct mp_client_api *clients, const char *name); +void mp_client_set_weak(struct mpv_handle *ctx); struct mp_log *mp_client_get_log(struct mpv_handle *ctx); struct mpv_global *mp_client_get_global(struct mpv_handle *ctx); struct MPContext *mp_client_get_core(struct mpv_handle *ctx); diff --git a/player/scripting.c b/player/scripting.c index db622f0fe8..0838630bed 100644 --- a/player/scripting.c +++ b/player/scripting.c @@ -138,6 +138,7 @@ static int mp_load_script(struct MPContext *mpctx, const char *fname) talloc_free(arg); return -1; } + mp_client_set_weak(arg->client); arg->log = mp_client_get_log(arg->client); MP_DBG(arg, "Loading %s %s...\n", backend->name, fname); -- cgit v1.2.3