Skip to content

Commit

Permalink
command: remove legacy hook API
Browse files Browse the repository at this point in the history
Hopefully nothing uses this. (I know one exception, but, well, what can
I do.)
  • Loading branch information
wm4 committed Mar 6, 2020
1 parent 7a76b57 commit 2337fa4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 104 deletions.
3 changes: 3 additions & 0 deletions DOCS/interface-changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Interface changes
- the OSX bundle now logs to "~/Library/Logs/mpv.log" by default
- deprecate the --cache-secs option (once removed, the cache cannot be
limited by time anymore)
- remove deprecated legacy hook API ("hook-add", "hook-ack"). Use either the
libmpv API (mpv_hook_add(), mpv_hook_continue()), or the Lua scripting
wrappers (mp.add_hook()).
--- mpv 0.32.0 ---
- change behavior when using legacy option syntax with options that start
with two dashes (``--`` instead of a ``-``). Now, using the recommended
Expand Down
41 changes: 0 additions & 41 deletions DOCS/man/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1286,47 +1286,6 @@ The following hooks are currently defined:
Run before closing a file, and before actually uninitializing
everything. It's not possible to resume playback in this state.

Legacy hook API
~~~~~~~~~~~~~~~

.. warning::

The legacy API is deprecated and will be removed soon.

There are two special commands involved. Also, the client must listen for
client messages (``MPV_EVENT_CLIENT_MESSAGE`` in the C API).

``hook-add <hook-name> <id> <priority>``
Subscribe to the hook identified by the first argument (basically, the
name of event). The ``id`` argument is an arbitrary integer chosen by the
user. ``priority`` is used to sort all hook handlers globally across all
clients. Each client can register multiple hook handlers (even for the
same hook-name). Once the hook is registered, it cannot be unregistered.

When a specific event happens, all registered handlers are run serially.
This uses a protocol every client has to follow explicitly. When a hook
handler is run, a client message (``MPV_EVENT_CLIENT_MESSAGE``) is sent to
the client which registered the hook. This message has the following
arguments:

1. the string ``hook_run``
2. the ``id`` argument the hook was registered with as string (this can be
used to correctly handle multiple hooks registered by the same client,
as long as the ``id`` argument is unique in the client)
3. something undefined, used by the hook mechanism to track hook execution

Upon receiving this message, the client can handle the event. While doing
this, the player core will still react to requests, but playback will
typically be stopped.

When the client is done, it must continue the core's hook execution by
running the ``hook-ack`` command.

``hook-ack <string>``
Run the next hook in the global chain of hooks. The argument is the 3rd
argument of the client message that starts hook execution for the
current client.

Input Command Prefixes
----------------------

Expand Down
2 changes: 1 addition & 1 deletion player/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ int mpv_hook_add(mpv_handle *ctx, uint64_t reply_userdata,
const char *name, int priority)
{
lock_core(ctx);
mp_hook_add(ctx->mpctx, ctx->name, name, reply_userdata, priority, false);
mp_hook_add(ctx->mpctx, ctx->name, name, reply_userdata, priority);
unlock_core(ctx);
return 0;
}
Expand Down
69 changes: 8 additions & 61 deletions player/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ struct hook_handler {
uint64_t user_id; // user-chosen ID
int priority; // priority for global hook order
int64_t seq; // unique ID, != 0, also for fixed order on equal priorities
bool legacy; // old cmd based hook API
bool active; // hook is currently in progress (only 1 at a time for now)
};

Expand Down Expand Up @@ -178,29 +177,13 @@ static int invoke_hook_handler(struct MPContext *mpctx, struct hook_handler *h)
h->active = true;

uint64_t reply_id = 0;
void *data;
int msg;
if (h->legacy) {
mpv_event_client_message *m = talloc_ptrtype(NULL, m);
*m = (mpv_event_client_message){0};
MP_TARRAY_APPEND(m, m->args, m->num_args, "hook_run");
MP_TARRAY_APPEND(m, m->args, m->num_args,
talloc_asprintf(m, "%llu", (long long)h->user_id));
MP_TARRAY_APPEND(m, m->args, m->num_args,
talloc_asprintf(m, "%llu", (long long)h->seq));
data = m;
msg = MPV_EVENT_CLIENT_MESSAGE;
} else {
mpv_event_hook *m = talloc_ptrtype(NULL, m);
*m = (mpv_event_hook){
.name = talloc_strdup(m, h->type),
.id = h->seq,
},
reply_id = h->user_id;
data = m;
msg = MPV_EVENT_HOOK;
}
int r = mp_client_send_event(mpctx, h->client, reply_id, msg, data);
mpv_event_hook *m = talloc_ptrtype(NULL, m);
*m = (mpv_event_hook){
.name = talloc_strdup(m, h->type),
.id = h->seq,
},
reply_id = h->user_id;
int r = mp_client_send_event(mpctx, h->client, reply_id, MPV_EVENT_HOOK, m);
if (r < 0) {
MP_WARN(mpctx, "Sending hook command failed. Removing hook.\n");
hook_remove(mpctx, h);
Expand Down Expand Up @@ -261,11 +244,8 @@ static int compare_hook(const void *pa, const void *pb)
}

void mp_hook_add(struct MPContext *mpctx, const char *client, const char *name,
uint64_t user_id, int pri, bool legacy)
uint64_t user_id, int pri)
{
if (legacy)
MP_WARN(mpctx, "The old hook API is deprecated! Use the libmpv API.\n");

struct command_ctx *cmd = mpctx->command_ctx;
struct hook_handler *h = talloc_ptrtype(cmd, h);
int64_t seq = ++cmd->hook_seq;
Expand All @@ -275,7 +255,6 @@ void mp_hook_add(struct MPContext *mpctx, const char *client, const char *name,
.user_id = user_id,
.priority = pri,
.seq = seq,
.legacy = legacy,
};
MP_TARRAY_APPEND(cmd, cmd->hooks, cmd->num_hooks, h);
qsort(cmd->hooks, cmd->num_hooks, sizeof(cmd->hooks[0]), compare_hook);
Expand Down Expand Up @@ -5411,33 +5390,6 @@ static void cmd_write_watch_later_config(void *p)
mp_write_watch_later_conf(mpctx);
}

static void cmd_hook_add(void *p)
{
struct mp_cmd_ctx *cmd = p;
struct MPContext *mpctx = cmd->mpctx;

if (!cmd->cmd->sender) {
MP_ERR(mpctx, "Can be used from client API only.\n");
cmd->success = false;
return;
}
mp_hook_add(mpctx, cmd->cmd->sender, cmd->args[0].v.s, cmd->args[1].v.i,
cmd->args[2].v.i, true);
}

static void cmd_hook_ack(void *p)
{
struct mp_cmd_ctx *cmd = p;
struct MPContext *mpctx = cmd->mpctx;

if (!cmd->cmd->sender) {
MP_ERR(mpctx, "Can be used from client API only.\n");
cmd->success = false;
return;
}
mp_hook_continue(mpctx, cmd->cmd->sender, cmd->args[0].v.i);
}

static void cmd_mouse(void *p)
{
struct mp_cmd_ctx *cmd = p;
Expand Down Expand Up @@ -5950,11 +5902,6 @@ const struct mp_cmd_def mp_cmds[] = {

{ "write-watch-later-config", cmd_write_watch_later_config },

{ "hook-add", cmd_hook_add, { OPT_STRING("arg0", v.s, 0),
OPT_INT("arg1", v.i, 0),
OPT_INT("arg2", v.i, 0) }},
{ "hook-ack", cmd_hook_ack, { OPT_INT("arg0", v.i, 0) }},

{ "mouse", cmd_mouse, { OPT_INT("x", v.i, 0),
OPT_INT("y", v.i, 0),
OPT_INT("button", v.i, 0, OPTDEF_INT(-1)),
Expand Down
2 changes: 1 addition & 1 deletion player/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ bool mp_hook_test_completion(struct MPContext *mpctx, char *type);
void mp_hook_start(struct MPContext *mpctx, char *type);
int mp_hook_continue(struct MPContext *mpctx, char *client, uint64_t id);
void mp_hook_add(struct MPContext *mpctx, const char *client, const char *name,
uint64_t user_id, int pri, bool legacy);
uint64_t user_id, int pri);

void mark_seek(struct MPContext *mpctx);

Expand Down

2 comments on commit 2337fa4

@chainikdn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plex Media Player uses this. Now it can't set proper aid/sid values and every video starts w/o audio playing.

@avih
Copy link
Member

@avih avih commented on 2337fa4 Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hooks API was not removed, just replaced with a different one, and the old one was deprecated for a while. Plex should use the new API.

Please sign in to comment.