PR #26322 opened by progmanos URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26322 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26322.patch
Add implementation for changing the play rate for rtsp streams. >From a6c81b56d564d989186e9bb7167b71693cceeae8 Mon Sep 17 00:00:00 2001 From: Rashad Tatum <[email protected]> Date: Mon, 2 Jun 2025 17:12:37 -0400 Subject: [PATCH] Add new method for playing network based streams with a play rate. Add implementation for changing the play rate for rtsp streams. --- libavformat/avformat.h | 6 ++++++ libavformat/demux.h | 7 +++++++ libavformat/demux_utils.c | 6 ++++++ libavformat/rtsp.c | 1 + libavformat/rtsp.h | 10 ++++++++++ libavformat/rtspdec.c | 21 +++++++++++++++++++-- 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 67686273792..267ef482a17 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -2635,6 +2635,12 @@ int avformat_flush(AVFormatContext *s); */ int av_read_play(AVFormatContext *s); +/** + * Play a network-based stream (e.g. RTSP stream) with a given play rate + * (e.g. Scale value for RTSP) and timestamp position. + */ +int av_read_play_with_rate(AVFormatContext *s, double play_rate, int stream_index, int64_t timestamp); + /** * Pause a network-based stream (e.g. RTSP stream). * diff --git a/libavformat/demux.h b/libavformat/demux.h index 146c702494e..8a2094d0ffd 100644 --- a/libavformat/demux.h +++ b/libavformat/demux.h @@ -148,6 +148,13 @@ typedef struct FFInputFormat { int (*read_set_state)(struct AVFormatContext *, enum FFInputFormatStreamState state); + /** + * Play a network-based stream (e.g. RTSP stream) with a given play rate + * (e.g. Scale value for RTSP) and timestamp position. + */ + int (*read_play_with_rate)(struct AVFormatContext *s, double play_rate, int stream_index, int64_t timestamp); + + /** * Handle demuxer commands * This callback is used to either send a command to a demuxer diff --git a/libavformat/demux_utils.c b/libavformat/demux_utils.c index b1fb2726f64..6d9552b2790 100644 --- a/libavformat/demux_utils.c +++ b/libavformat/demux_utils.c @@ -182,6 +182,12 @@ int av_read_play(AVFormatContext *s) return AVERROR(ENOSYS); } +int av_read_play_with_rate(AVFormatContext *s, double play_rate, int stream_index, int64_t timestamp) { + if (ffifmt(s->iformat)->read_play_with_rate) + return ffifmt(s->iformat)->read_play_with_rate(s, play_rate, stream_index, timestamp); + return AVERROR(ENOSYS); +} + int av_read_pause(AVFormatContext *s) { if (ffifmt(s->iformat)->read_set_state) diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c index d455438fa56..bfc24375e6b 100644 --- a/libavformat/rtsp.c +++ b/libavformat/rtsp.c @@ -2248,6 +2248,7 @@ redirect: av_strlcpy(rt->real_challenge, real_challenge, sizeof(rt->real_challenge)); rt->state = RTSP_STATE_IDLE; rt->seek_timestamp = 0; /* default is to start stream at position zero */ + rt->scale = 1.0; /* default is to play at the normal rate */ return 0; fail: ff_rtsp_close_streams(s); diff --git a/libavformat/rtsp.h b/libavformat/rtsp.h index a235fc81a7f..fe2f86da10d 100644 --- a/libavformat/rtsp.h +++ b/libavformat/rtsp.h @@ -245,6 +245,16 @@ typedef struct RTSPState { * whenever we resume playback. Either way, the value is only used once, * see rtsp_read_play() and rtsp_read_seek(). */ int64_t seek_timestamp; + + /** the scale value requested when calling av_read_play(). This value + * is subsequently used as part of the "Scale" parameter when emitting + * the RTSP PLAY command. The "Scale" parameter determines the stream play + * rate. A value of 1 represents the normal play rate. Any other value is + * in regards to the normal play rate. A negative value represents reverse + * playback. If we are currently playing, this command is called instantly. + * If we are currently paused, this command is called whenever we resume + * playback. */ + double scale; int seq; /**< RTSP command sequence number */ diff --git a/libavformat/rtspdec.c b/libavformat/rtspdec.c index 0e592eebaed..1627efb46a7 100644 --- a/libavformat/rtspdec.c +++ b/libavformat/rtspdec.c @@ -40,6 +40,7 @@ #include "tls.h" #include "url.h" #include "version.h" +#include <stdio.h> static const struct RTSPStatusMessage { enum RTSPStatusCode code; @@ -542,7 +543,7 @@ static int rtsp_read_play(AVFormatContext *s) { RTSPState *rt = s->priv_data; RTSPMessageHeader reply1, *reply = &reply1; - int i; + int i, cmd_char_count = 0; char cmd[MAX_URL_SIZE]; av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state); @@ -579,11 +580,17 @@ static int rtsp_read_play(AVFormatContext *s) if (rt->state == RTSP_STATE_PAUSED) { cmd[0] = 0; } else { - snprintf(cmd, sizeof(cmd), + + cmd_char_count += snprintf(cmd, sizeof(cmd), "Range: npt=%"PRId64".%03"PRId64"-\r\n", rt->seek_timestamp / AV_TIME_BASE, rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000); + + snprintf(cmd + cmd_char_count, sizeof(cmd) - cmd_char_count, "Scale: %f\r\n", rt->scale); + } + + ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL); if (reply->status_code != RTSP_STATUS_OK) { return ff_rtsp_averror(reply->status_code, -1); @@ -608,6 +615,15 @@ static int rtsp_read_play(AVFormatContext *s) return 0; } +static int rtsp_read_play_with_rate(AVFormatContext *s, double play_rate, int stream_index, int64_t timestamp) { + RTSPState *rt = s->priv_data; + rt->seek_timestamp = av_rescale_q(timestamp, + s->streams[stream_index]->time_base, + AV_TIME_BASE_Q); + rt->scale = play_rate; + return rtsp_read_play(s); +} + /* pause the stream */ static int rtsp_read_pause(AVFormatContext *s) { @@ -1128,5 +1144,6 @@ const FFInputFormat ff_rtsp_demuxer = { .read_close = rtsp_read_close, .read_seek = rtsp_read_seek, .read_set_state = rtsp_read_set_state, + .read_play_with_rate = rtsp_read_play_with_rate, .handle_command = rtsp_handle_command, }; -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
