/* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include "common/common.h"
#include "common/msg.h"
#include "common/msg_control.h"
#include "input/input.h"
#include "input/cmd_list.h"
#include "misc/dispatch.h"
#include "options/m_config.h"
#include "options/m_option.h"
#include "options/m_property.h"
#include "options/path.h"
#include "options/parse_configfile.h"
#include "osdep/threads.h"
#include "osdep/timer.h"
#include "osdep/io.h"
#include "stream/stream.h"
#include "command.h"
#include "core.h"
#include "client.h"
#include "config.h"
/*
* Locking hierarchy:
*
* MPContext > mp_client_api.lock > mpv_handle.lock > * > mpv_handle.wakeup_lock
*
* MPContext strictly speaking has no locks, and instead is implicitly managed
* by MPContext.dispatch, which basically stops the playback thread at defined
* points in order to let clients access it in a synchronized manner. Since
* MPContext code accesses the client API, it's on top of the lock hierarchy.
*
*/
struct mp_client_api {
struct MPContext *mpctx;
pthread_mutex_t lock;
// -- protected by lock
struct mpv_handle **clients;
int num_clients;
uint64_t event_masks; // combined events of all clients, or 0 if unknown
};
struct observe_property {
char *name;
int id; // ==mp_get_property_id(name)
uint64_t event_mask; // ==mp_get_property_event_mask(name)
int64_t reply_id;
mpv_format format;
bool changed; // property change should be signaled to user
bool need_new_value; // a new value should be retrieved
bool updating; // a new value is being retrieved
bool dead; // property unobserved while retrieving value
bool new_value_valid, user_value_valid;
union m_option_value new_value, user_value;
struct mpv_handle *client;
};
struct mpv_handle {
// -- immmutable
char *name;
bool owner;
struct mp_log *log;
struct MPContext *mpctx;
struct mp_client_api *clients;
// -- not thread-safe
struct mpv_event *cur_event;
struct mpv_event_property cur_property_event;
pthread_mutex_t lock;
pthread_mutex_t wakeup_lock;
pthread_cond_t wakeup;
// -- protected by wakeup_lock
bool need_wakeup;
void (*wakeup_cb)(void *d);
void *wakeup_cb_ctx;
int wakeup_pipe[2];
// -- protected by lock
uint64_t event_mask;
bool queued_wakeup;
bool choke_warning;
mpv_event *events; // ringbuffer of max_events entries
int max_events; // allocated number of entries in events
int first_event; // events[first_event] is the first readable event
int num_events; // number of readable events
int reserved_events; // number of entries reserved for replies
struct observe_property **properties;
int num_properties;
int lowest_changed; // attempt at making change processing incremental
int properties_updating;
uint64_t property_event_masks; // or-ed together event masks of all properties
bool fuzzy_initialized;
struct mp_log_buffer *messages;
};
static bool gen_property_change_event(struct mpv_handle *ctx);
static void notify_property_events(struct mpv_handle *ctx, uint64_t event_mask);
void mp_clients_init(struct MPContext *mpctx)
{
mpctx->clients = talloc_ptrtype(NULL, mpctx->clients);
*mpctx->clients = (struct mp_client_api) {
.mpctx = mpctx,
};
pthread_mutex_init(&mpctx->clients->lock, NULL);
}
void mp_clients_destroy(struct MPContext *mpctx)
{
if (!mpctx->clients)
return;
assert(mpctx->clients->num_clients == 0);
pthread_mutex_destroy(&mpctx->clients->lock);
talloc_free(mpctx->clients);
mpctx->clients = NULL;
}
int mp_clients_num(struct MPContext *mpctx)
{
pthread_mutex_lock(&mpctx->clients->lock);
int num_clients = mpctx->clients->num_clients;
pthread_mutex_unlock(&mpctx->clients->lock);
return num_clients;
}
// Test for "fuzzy" initialization of all clients. That is, all clients have
// at least called mpv_wait_event() at least once since creation (or exited).
bool mp_clients_all_initialized(struct MPContext *mpctx)
{
bool all_ok = true;
pthread_mutex_lock(&mpctx->clients->lock);
for (int n = 0; n < mpctx->clients->num_clients; n++) {
struct mpv_handle *ctx = mpctx->clients->clients[n];
pthread_mutex_lock(&ctx->lock);
all_ok &= ctx->fuzzy_initialized;
pthread_mutex_unlock(&ctx->lock);
}
pthread_mutex_unlock(&mpctx->clients->lock);
return all_ok;
}
static void invalidate_global_event_mask(struct mpv_handle *ctx)
{
pthread_mutex_lock(&ctx->clients->lock);
ctx->clients->event_masks = 0;
pthread_mutex_unlock(&ctx->clients->lock);
}
static struct mpv_handle *find_client(struct mp_client_api *clients,
const char *name)
{
for (int n = 0; n < clients->num_clients; n++) {
if (strcmp(clients->clients[n]->name, name) == 0)
return clients->clients[n];
}
return NULL;
}
struct mpv_handle *mp_new_client(struct mp_client_api *clients, const char *name)
{
pthread_mutex_lock(&clients->lock);
char *unique_name = NULL;
if (find_client(clients, name)) {
for (int n = 2; n < 1000; n++) {
unique_name = talloc_asprintf(NULL, "%s%d", name, n);
if (!find_client(clients, unique_name))
break;
talloc_free(unique_name);
unique_name = NULL;
}
if (!unique_name) {
pthread_mutex_unlock(&clients->lock);
return NULL;
}
}
if (!unique_name)
unique_name = talloc_strdup(NULL, name);
int num_events = 1000;
struct mpv_handle *client = talloc_ptrtype(NULL, client);
*client = (struct mpv_handle){
.name = talloc_steal(client, unique_name),
.log = mp_log_new(client, clients->mpctx->log, unique_name),
.mpctx = clients->mpctx,
.clients = clients,
.cur_event = talloc_zero(client, struct mpv_event),
.events = talloc_array(client, mpv_event, num_events),
.max_events = num_events,
.event_mask = (1ULL << INTERNAL_EVENT_BASE) - 1, // exclude internal events
.wakeup_pipe = {-1, -1},
};
pthread_mutex_init(&client->lock, NULL);
pthread_mutex_init(&client->wakeup_lock, NULL);
pthread_cond_init(&client->wakeup, NULL);
MP_TARRAY_APPEND(clients, clients->clients, clients->num_clients, client);
clients->event_masks = 0;
pthread_mutex_unlock(&clients->lock);
mpv_request_event(client, MPV_EVENT_TICK, 0);
return client;
}
const char *mpv_client_name(mpv_handle *ctx)
{
return ctx->name;
}
struct mp_log *mp_client_get_log(struct mpv_handle *ctx)
{
return ctx->log;
}
struct MPContext *mp_client_get_core(struct mpv_handle *ctx)
{
return ctx->mpctx;
}
static void wakeup_client(struct mpv_handle *ctx)
{
pthread_mutex_lock(&ctx->wakeup_lock);
if (!ctx->need_wakeup) {
ctx->need_wakeup = true;
pthread_cond_signal(&ctx->wakeup);
|