From patchwork Tue Feb 21 15:13:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liu Steven X-Patchwork-Id: 2627 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.65.149 with SMTP id x21csp433698vsf; Tue, 21 Feb 2017 07:13:58 -0800 (PST) X-Received: by 10.223.170.204 with SMTP id i12mr1178982wrc.123.1487690038094; Tue, 21 Feb 2017 07:13:58 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 186si16799076wmx.129.2017.02.21.07.13.57; Tue, 21 Feb 2017 07:13:58 -0800 (PST) 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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EA875688303; Tue, 21 Feb 2017 17:13:45 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from smtpbg202.qq.com (smtpbg202.qq.com [184.105.206.29]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0D4C16802BB for ; Tue, 21 Feb 2017 17:13:38 +0200 (EET) X-QQ-mid: bizesmtp1t1487690009tcb1doqjs Received: from localhost (unknown [47.90.47.25]) by esmtp4.qq.com (ESMTP) with id ; Tue, 21 Feb 2017 23:13:28 +0800 (CST) X-QQ-SSF: 01100000004000F0F820B00A0000000 X-QQ-FEAT: nSUdqPGu3ttjdaAHzJLo2mTI+WyrTyvpHKx/GinKsINTsvL4gEsKEqryyHcCM dLfH6WEJ5MHdjD2cfsIm5PkgB+uJfclxdSmkeaKiSEpK7Dr5sbQwACamGwbM8vz4F5MiAWQ Nw/OalzX3MoAjPrBGV2LqWVWzKVDObZkaO3K4ITNqufldTA+uBCq7ESXzyPCPIZ4ln7ycLA LJfVtnTl9mnbZLIoBfqlSuZZIyoq07BdyWWxjALCXw3JDurNH/jJwVUR+jupDuIJXsZcaBC EdzNIqWdEI15Vj X-QQ-GoodBg: 0 From: Steven Liu To: ffmpeg-devel@ffmpeg.org Date: Tue, 21 Feb 2017 23:13:26 +0800 Message-Id: <20170221151326.3672-1-lq@chinaffmpeg.org> X-Mailer: git-send-email 2.10.1.382.ga23ca1b.dirty X-QQ-SENDSIZE: 520 X-QQ-Bgrelay: 1 Subject: [FFmpeg-devel] [PATCH] avformat/hlsenc: fix hls_flags temp_file bug X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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: Steven Liu MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" refer to ticket id: #6170 rename file from temp to origin name after complete current segment Signed-off-by: Steven Liu --- libavformat/hlsenc.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c index e673f59..712a01b 100644 --- a/libavformat/hlsenc.c +++ b/libavformat/hlsenc.c @@ -691,6 +691,17 @@ static void write_m3u8_head_block(HLSContext *hls, AVIOContext *out, int version av_log(hls, AV_LOG_VERBOSE, "EXT-X-MEDIA-SEQUENCE:%"PRId64"\n", sequence); } +static void hls_rename_temp_file(AVFormatContext *s, AVFormatContext *oc) +{ + size_t len = strlen(oc->filename); + char final_filename[sizeof(oc->filename)]; + + av_strlcpy(final_filename, oc->filename, len); + final_filename[len-4] = '\0'; + ff_rename(oc->filename, final_filename, s); + oc->filename[len-4] = '\0'; +} + static int hls_window(AVFormatContext *s, int last) { HLSContext *hls = s->priv_data; @@ -833,15 +844,6 @@ static int hls_start(AVFormatContext *s) char *filename, iv_string[KEYSIZE*2 + 1]; int err = 0; - if ((c->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) { - size_t len = strlen(oc->filename); - char final_filename[sizeof(oc->filename)]; - av_strlcpy(final_filename, oc->filename, len); - final_filename[len-4] = '\0'; - ff_rename(oc->filename, final_filename, s); - oc->filename[len-4] = '\0'; - } - if (c->flags & HLS_SINGLE_FILE) { av_strlcpy(oc->filename, c->basename, sizeof(oc->filename)); @@ -962,6 +964,7 @@ static int hls_start(AVFormatContext *s) av_strlcat(oc->filename, ".tmp", sizeof(oc->filename)); } + if (c->key_info_file) { if ((err = hls_encryption_start(s)) < 0) goto fail; @@ -1325,6 +1328,11 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt) new_start_pos = avio_tell(hls->avf->pb); hls->size = new_start_pos - hls->start_pos; + + if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) { + hls_rename_temp_file(s, oc); + } + ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size); hls->start_pos = new_start_pos; if (ret < 0) { @@ -1402,6 +1410,11 @@ static int hls_write_trailer(struct AVFormatContext *s) if (oc->pb) { hls->size = avio_tell(hls->avf->pb) - hls->start_pos; ff_format_io_close(s, &oc->pb); + + if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) { + hls_rename_temp_file(s, oc); + } + /* after av_write_trailer, then duration + 1 duration per packet */ hls_append_segment(s, hls, hls->duration + hls->dpp, hls->start_pos, hls->size); } @@ -1411,15 +1424,6 @@ static int hls_write_trailer(struct AVFormatContext *s) ff_rename(old_filename, hls->avf->filename, hls); } - if ((hls->flags & HLS_TEMP_FILE) && oc->filename[0] != 0) { - size_t len = strlen(oc->filename); - char final_filename[sizeof(oc->filename)]; - av_strlcpy(final_filename, oc->filename, len); - final_filename[len-4] = '\0'; - ff_rename(oc->filename, final_filename, s); - oc->filename[len-4] = '\0'; - } - if (vtt_oc) { if (vtt_oc->pb) av_write_trailer(vtt_oc);