From patchwork Thu Jun 23 15:00:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Li Kai X-Patchwork-Id: 36403 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp3525003pzb; Thu, 23 Jun 2022 08:01:01 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ujMmhFNjwVUbhFWxM/v2Uoui0LPr7hKPVJBkAFL0jz7OFFibeyv3w49i89muxnB0hdS94y X-Received: by 2002:a17:906:5055:b0:6ff:1dfb:1e2c with SMTP id e21-20020a170906505500b006ff1dfb1e2cmr8568162ejk.200.1655996461476; Thu, 23 Jun 2022 08:01:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655996461; cv=none; d=google.com; s=arc-20160816; b=DMOlsRCAskOx0kXqhO6dPqb6S5FmtWnvFFWCPBkm/JthRn2R0866G1btiraP51j731 KCiz//8kq0dIOyY/b59MIjjjGloFcO9dNgh8QDs2Q7Rcls32WBpq2Zioth17rRR7fzmc 6n9sgFR+RtzCYwJx7nGCDwiZ4+S6lpINBcND+cCD5xu9EaHe56jGyHueDNaoAwxNERKI MlbQ/LdpsjyJBySpSPmidr00gssLBom2RhLRHmvItOLuRqDX6GzPB1Gmcah97G4wmHmP 2VxBpapJAlJCV6AzZVL/OraarxnJrAZN+ggQQyNo1ZRqk1i4UmBM7YfC9tZHId77tapl TijA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=hl91LP3BjVPNVVm2r/5fOv/H3v1KeWKpvhTLzNX2P+Q=; b=jsoo4hJ0DAmBikCC6uKovKIl6LkcELNIrgEZL43rPh3taCJLFbxmxgHPLC3mZoucd0 XAzm/zeAJx5XRZETomnBVIEbkk1i1vhB/ngIYfpVKVAwHXavpEpPwFDLmWtxC2NqEywt fKbZ9Djwi4ov2hVg/N//mKwlcntZ40bw4xZqVUXGfVLGfROWAMNIeSn6a7r1WJwklFEn PUMpau0N3xF6dMkfC2KuoIkGkfRkYazfY+PdARJjh8dPo/lEspgXZhnuKCWOiDv9WEoS pAboWp2j0dCy7eYNcuD/NP/YPu4jkCNhEjQubRtRBRIMI41l0ZqHQuvN8bjMbN9GvKwm f23A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=O9ngbHg2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id sa13-20020a1709076d0d00b007035cc3a941si24431316ejc.875.2022.06.23.08.01.00; Thu, 23 Jun 2022 08:01:01 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=O9ngbHg2; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 714B468B6AC; Thu, 23 Jun 2022 18:00:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6ED0A68ABCF for ; Thu, 23 Jun 2022 18:00:50 +0300 (EEST) Received: by mail-pl1-f173.google.com with SMTP id m14so18394553plg.5 for ; Thu, 23 Jun 2022 08:00:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Zt9k3q9dww3h0Cwo5OY9EyYKuDkRceG/5ISpRJj1Y6E=; b=O9ngbHg2MHlU+pBMyI5ASGxEjGln5Cdu2D0A+QM2fgVPIqJMSs0aJB1yN9KosQGr3q 3f1mD9IelSFv5l+dFBYSLySWnojrZ0vTtZS4qwE76D0aQCIjei41v/ZMsswDOZi8Bcf2 +ccz6YVV4hy/8VDzVFkY/5V/2fF7YQaS3Ec1Y2+0ee3yDwOlpu6a/WTTW3LEpcahjbAT X2ZLrkmkdC9XMNI6dijRNYwOcPsbVibuwZF+vdcswDxUMHRn6yy8/Tr7GPib1TM0SmgQ VUHniB3fMm6M8MuGf7a3bkAfNu0XsInYlVzP429idMz/q89Tj4cASoCBCpqbHzqZ11mu RzIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=Zt9k3q9dww3h0Cwo5OY9EyYKuDkRceG/5ISpRJj1Y6E=; b=Juz5hqAns0sadeSACj7CvjGvfi/l2uKkNynIOHNLoGo7TexxAyV1jd5ux9e/v2bCvb kbTh9YfDkw3Ax+MeQaxuVJMQTRfGk7/h7e1IdxG6pu4wuW/+6SI93NE8gtZDMlVBUIob NYTUZyJsT28Tb1m3/7Cd2pX6f4DeoEvPex6ymd8KpKm00Tyl2M2Fwep9z4vkM8gz/T2I Wi5aAIXNYwu+9ID5G/ZJOKky7EUkQDftflL/REWwpHqzsMt0qtVmQaADrSbxRqh3tif6 i6GL6pIbaBwp9IvWcqs6DZUcO9VTxaha1ht3Tp8LMy3U48L9xIYMdsaV7Tq4xOpoDgeV fJIQ== X-Gm-Message-State: AJIora9nQulloNild9G49mFFJjINC9urHDh+ICjde8GyrKBMm3nThKQ/ wK+lXdhtXkBHcaZdD0L2GqSINhRhxCUGx7UV X-Received: by 2002:a17:902:c405:b0:16a:23c7:e9a6 with SMTP id k5-20020a170902c40500b0016a23c7e9a6mr22197707plk.58.1655996448109; Thu, 23 Jun 2022 08:00:48 -0700 (PDT) Received: from localhost.localdomain ([117.143.145.80]) by smtp.gmail.com with ESMTPSA id s6-20020a17090a2f0600b001eaae89de5fsm2054475pjd.1.2022.06.23.08.00.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 08:00:47 -0700 (PDT) From: wolfleekay@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Thu, 23 Jun 2022 23:00:40 +0800 Message-Id: <20220623150040.1981341-1-wolfleekay@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avformat/hls:use EXT-X-START instead of live_start_index if it's in playlist X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Li Kai Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: UigUghqE1TdF From: Li Kai Refer to https://datatracker.ietf.org/doc/html/rfc8216#section-4.3.5.2 Signed-off-by: Li Kai Signed-off-by: Li Kai --- libavformat/hls.c | 68 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/libavformat/hls.c b/libavformat/hls.c index 8204f55df3..96923c3eac 100644 --- a/libavformat/hls.c +++ b/libavformat/hls.c @@ -120,6 +120,8 @@ struct playlist { enum PlaylistType type; int64_t target_duration; int64_t start_seq_no; + int time_offset_flag; + int64_t start_time_offset; int n_segments; struct segment **segments; int needed; @@ -741,6 +743,7 @@ static int parse_playlist(HLSContext *c, const char *url, struct segment **prev_segments = NULL; int prev_n_segments = 0; int64_t prev_start_seq_no = -1; + const char *p; if (is_http && !in && c->http_persistent && c->playlist_pb) { in = c->playlist_pb; @@ -889,6 +892,18 @@ static int parse_playlist(HLSContext *c, const char *url, cur_init_section->key = NULL; } + } else if (av_strstart(line, "#EXT-X-START:", &ptr)) { + ret = ensure_playlist(c, &pls, url); + if (ret < 0) { + goto fail; + } + if (av_strstart(ptr, "TIME-OFFSET=", &p)) { + float offset = strtof(p, NULL); + pls->start_time_offset = offset * AV_TIME_BASE; + pls->time_offset_flag = 1; + } else { + goto fail; + } } else if (av_strstart(line, "#EXT-X-ENDLIST", &ptr)) { if (pls) pls->finished = 1; @@ -1719,12 +1734,53 @@ static int64_t select_cur_seq_no(HLSContext *c, struct playlist *pls) * require us to download a segment to inspect its timestamps. */ return c->cur_seq_no; - /* If this is a live stream, start live_start_index segments from the - * start or end */ - if (c->live_start_index < 0) - return pls->start_seq_no + FFMAX(pls->n_segments + c->live_start_index, 0); - else - return pls->start_seq_no + FFMIN(c->live_start_index, pls->n_segments - 1); + if (!pls->time_offset_flag) { + /* If this is a live stream, start live_start_index segments from the + * start or end */ + if (c->live_start_index < 0) + return pls->start_seq_no + FFMAX(pls->n_segments + + c->live_start_index, 0); + else + return pls->start_seq_no + FFMIN(c->live_start_index, + pls->n_segments - 1); + } else { + /* If playlist indicate a TIME-OFFSET, need to recalculate seq_no */ + int i; + int64_t start_timestamp; + int64_t playlist_duration = 0; + int64_t cur_timestamp = c->cur_timestamp == AV_NOPTS_VALUE ? 0 : + c->cur_timestamp; + + for (i = 0; i < pls->n_segments; i++) { + playlist_duration += pls->segments[i]->duration; + } + /* If the absolute value of TIME-OFFSET exceeds + * the duration of the Playlist, it indicates either the end of the + * Playlist (if positive) or the beginning of the Playlist (if + * negative). */ + if (pls->start_time_offset >=0 && + pls->start_time_offset > playlist_duration) + start_timestamp = cur_timestamp + playlist_duration; + else if (pls->start_time_offset >= 0 && + pls->start_time_offset <= playlist_duration) + start_timestamp = cur_timestamp + pls->start_time_offset; + else if (pls->start_time_offset < 0 && + pls->start_time_offset < -playlist_duration) + start_timestamp = cur_timestamp; + else if (pls->start_time_offset < 0 && + pls->start_time_offset > -playlist_duration) + start_timestamp = cur_timestamp + playlist_duration + + pls->start_time_offset; + else + start_timestamp = cur_timestamp; + + find_timestamp_in_playlist(c, pls, start_timestamp, &seq_no, NULL); + av_log(c, AV_LOG_DEBUG, "start_timestamp: %" PRId64 + "cur_timestamp:%" PRId64 + "cur_seq_no:%" PRId64 "\n", + start_timestamp, cur_timestamp, seq_no); + return seq_no; + } } /* Otherwise just start on the first segment. */