Skip to content

Commit

Permalink
stream_cb: add cancel_fn callback
Browse files Browse the repository at this point in the history
This allows stream_cb backends to implement blocking
behavior inside read_fn, and still get notified when the user
wants to cancel and stop playback.

Signed-off-by: Aman Gupta <[email protected]>
  • Loading branch information
tmm1 committed Sep 27, 2019
1 parent 4fdd094 commit c7d0a8f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions DOCS/client-api-changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ API changes

::
--- mpv 0.30.0 ---
1.106 - Add cancel_fn to mpv_stream_cb_info
1.105 - Fix deadlock problems with MPV_RENDER_PARAM_ADVANCED_CONTROL and if
the "vd-lavc-dr" option is enabled (which it is by default).
There were no actual API changes.
Expand Down
2 changes: 1 addition & 1 deletion libmpv/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ extern "C" {
* relational operators (<, >, <=, >=).
*/
#define MPV_MAKE_VERSION(major, minor) (((major) << 16) | (minor) | 0UL)
#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(1, 105)
#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(1, 106)

/**
* The API user is allowed to "#define MPV_ENABLE_DEPRECATED 0" before
Expand Down
17 changes: 17 additions & 0 deletions libmpv/stream_cb.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ typedef int64_t (*mpv_stream_cb_size_fn)(void *cookie);
*/
typedef void (*mpv_stream_cb_close_fn)(void *cookie);

/**
* Cancel callback used to implement a custom stream.
*
* This callback is used to interrupt any current or future read and seek
* operations. It will be called from a separate thread than the demux
* thread, and should not block.
*
* This callback can be NULL.
*
* Available since API 1.106.
*
* @param cookie opaque cookie identifying the stream,
* returned from mpv_stream_cb_open_fn
*/
typedef void (*mpv_stream_cb_cancel_fn)(void *cookie);

/**
* See mpv_stream_cb_open_ro_fn callback.
*/
Expand All @@ -170,6 +186,7 @@ typedef struct mpv_stream_cb_info {
mpv_stream_cb_seek_fn seek_fn;
mpv_stream_cb_size_fn size_fn;
mpv_stream_cb_close_fn close_fn;
mpv_stream_cb_cancel_fn cancel_fn; /* since API 1.106 */
} mpv_stream_cb_info;

/**
Expand Down
8 changes: 8 additions & 0 deletions stream/stream_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#include "options/path.h"
#include "player/client.h"
#include "libmpv/stream_cb.h"
#include "misc/thread_tools.h"

struct priv {
mpv_stream_cb_info info;
struct mp_cancel *cancel;
};

static int fill_buffer(stream_t *s, char *buffer, int max_len)
Expand Down Expand Up @@ -98,6 +100,12 @@ static int open_cb(stream_t *stream)
stream->read_chunk = 64 * 1024;
stream->close = s_close;

if (p->info.cancel_fn && stream->cancel) {
p->cancel = mp_cancel_new(p);
mp_cancel_set_parent(p->cancel, stream->cancel);
mp_cancel_set_cb(p->cancel, p->info.cancel_fn, p->info.cookie);
}

return STREAM_OK;
}

Expand Down

0 comments on commit c7d0a8f

Please sign in to comment.