diff mbox

[FFmpeg-devel] avformat/hlsenc: fix hlsenc bug at windows system

Message ID 20170112050314.17668-1-lq@chinaffmpeg.org
State Superseded
Headers show

Commit Message

Liu Steven Jan. 12, 2017, 5:03 a.m. UTC
when hlsenc use flag second_level_segment_index,
second_level_segment_size and second_level_segment_duration,
the rename is ok but the output filename always use the old filename
so move the rename operation after the close the ts file and
before open new segment

Reported-by: Christian Johannesen <chrisjohannesen@gmail.com>
Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 libavformat/hlsenc.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

Comments

Bodecs Bela Jan. 12, 2017, 9:18 a.m. UTC | #1
2017.01.12. 6:03 keltezéssel, Steven Liu írta:
> when hlsenc use flag second_level_segment_index,
> second_level_segment_size and second_level_segment_duration,
> the rename is ok but the output filename always use the old filename
> so move the rename operation after the close the ts file and
> before open new segment
It is strange. I have tested my original patch on windows 7. Sorry.

>
> Reported-by: Christian Johannesen <chrisjohannesen@gmail.com>
> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
> ---
>   libavformat/hlsenc.c | 27 +++++++++++++++++++++++----
>   1 file changed, 23 insertions(+), 4 deletions(-)
>
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index a2c606c..772232b 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -499,7 +499,6 @@ static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double
>               }
>               av_free(filename);
>           }
> -        ff_rename(old_filename, hls->avf->filename, hls);
>           av_free(old_filename);
>       }
I suggest to remove here, in this same scope all old_filename related 
code, because it was used by ff_rename only:

-        char * old_filename = av_strdup(hls->avf->filename); // %%s 
will be %s after strftime
-        if (!old_filename) {
-            av_free(en);
-            return AVERROR(ENOMEM);
-        }

and at several places inside if() branch:
-                av_free(old_filename);



>   
> @@ -1239,14 +1238,22 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
>       if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
>                                      end_pts, AV_TIME_BASE_Q) >= 0) {
>           int64_t new_start_pos;
> +        char *old_filename = av_strdup(hls->avf->filename);
> +
> +        if (!old_filename) {
> +            return AVERROR(ENOMEM);
> +        }
> +
>           av_write_frame(oc, NULL); /* Flush any buffered data */
>   
>           new_start_pos = avio_tell(hls->avf->pb);
>           hls->size = new_start_pos - hls->start_pos;
>           ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
>           hls->start_pos = new_start_pos;
> -        if (ret < 0)
> +        if (ret < 0) {
> +            av_free(old_filename);
>               return ret;
> +        }
>   
>           hls->end_pts = pkt->pts;
>           hls->duration = 0;
> @@ -1261,6 +1268,10 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
>               if (hls->start_pos >= hls->max_seg_size) {
>                   hls->sequence++;
>                   ff_format_io_close(s, &oc->pb);
> +                if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
> +                     strlen(hls->current_segment_final_filename_fmt)) {
> +                    ff_rename(old_filename, hls->avf->filename, hls);
> +                }
>                   if (hls->vtt_avf)
>                       ff_format_io_close(s, &hls->vtt_avf->pb);
>                   ret = hls_start(s);
> @@ -1272,22 +1283,30 @@ static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
>               hls->number++;
>           } else {
>               ff_format_io_close(s, &oc->pb);
> +            if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
> +                strlen(hls->current_segment_final_filename_fmt)) {
> +                ff_rename(old_filename, hls->avf->filename, hls);
> +            }
>               if (hls->vtt_avf)
>                   ff_format_io_close(s, &hls->vtt_avf->pb);
>   
>               ret = hls_start(s);
>           }
>   
> -        if (ret < 0)
> +        if (ret < 0) {
> +            av_free(old_filename);
>               return ret;
> +        }
>   
>           if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE )
>               oc = hls->vtt_avf;
>           else
>           oc = hls->avf;
>   
> -        if ((ret = hls_window(s, 0)) < 0)
> +        if ((ret = hls_window(s, 0)) < 0) {
> +            av_free(old_filename);
>               return ret;
> +        }
>       }
>   
>       ret = ff_write_chained(oc, stream_index, pkt, s, 0);
Steven Liu Jan. 12, 2017, 9:39 a.m. UTC | #2
2017-01-12 17:18 GMT+08:00 Bodecs Bela <bodecsb@vivanet.hu>:

>
>
> 2017.01.12. 6:03 keltezéssel, Steven Liu írta:
>
>> when hlsenc use flag second_level_segment_index,
>> second_level_segment_size and second_level_segment_duration,
>> the rename is ok but the output filename always use the old filename
>> so move the rename operation after the close the ts file and
>> before open new segment
>>
> It is strange. I have tested my original patch on windows 7. Sorry.

That's ok :)

the mail title:
[FFmpeg-user] issue with [PATCH] avformat/hlsenc: actual segment file size
and duration in segment filenames




>
>
>> Reported-by: Christian Johannesen <chrisjohannesen@gmail.com>
>> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
>> ---
>>   libavformat/hlsenc.c | 27 +++++++++++++++++++++++----
>>   1 file changed, 23 insertions(+), 4 deletions(-)
>>
>> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
>> index a2c606c..772232b 100644
>> --- a/libavformat/hlsenc.c
>> +++ b/libavformat/hlsenc.c
>> @@ -499,7 +499,6 @@ static int hls_append_segment(struct AVFormatContext
>> *s, HLSContext *hls, double
>>               }
>>               av_free(filename);
>>           }
>> -        ff_rename(old_filename, hls->avf->filename, hls);
>>           av_free(old_filename);
>>       }
>>
> I suggest to remove here, in this same scope all old_filename related
> code, because it was used by ff_rename only:
>
> -        char * old_filename = av_strdup(hls->avf->filename); // %%s will
> be %s after strftime
> -        if (!old_filename) {
> -            av_free(en);
> -            return AVERROR(ENOMEM);
> -        }
>
> and at several places inside if() branch:
> -                av_free(old_filename);
>
>
>
>
>   @@ -1239,14 +1238,22 @@ static int hls_write_packet(AVFormatContext *s,
>> AVPacket *pkt)
>>       if (can_split && av_compare_ts(pkt->pts - hls->start_pts,
>> st->time_base,
>>                                      end_pts, AV_TIME_BASE_Q) >= 0) {
>>           int64_t new_start_pos;
>> +        char *old_filename = av_strdup(hls->avf->filename);
>> +
>> +        if (!old_filename) {
>> +            return AVERROR(ENOMEM);
>> +        }
>> +
>>           av_write_frame(oc, NULL); /* Flush any buffered data */
>>             new_start_pos = avio_tell(hls->avf->pb);
>>           hls->size = new_start_pos - hls->start_pos;
>>           ret = hls_append_segment(s, hls, hls->duration, hls->start_pos,
>> hls->size);
>>           hls->start_pos = new_start_pos;
>> -        if (ret < 0)
>> +        if (ret < 0) {
>> +            av_free(old_filename);
>>               return ret;
>> +        }
>>             hls->end_pts = pkt->pts;
>>           hls->duration = 0;
>> @@ -1261,6 +1268,10 @@ static int hls_write_packet(AVFormatContext *s,
>> AVPacket *pkt)
>>               if (hls->start_pos >= hls->max_seg_size) {
>>                   hls->sequence++;
>>                   ff_format_io_close(s, &oc->pb);
>> +                if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE |
>> HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
>> +                     strlen(hls->current_segment_final_filename_fmt)) {
>> +                    ff_rename(old_filename, hls->avf->filename, hls);
>> +                }
>>                   if (hls->vtt_avf)
>>                       ff_format_io_close(s, &hls->vtt_avf->pb);
>>                   ret = hls_start(s);
>> @@ -1272,22 +1283,30 @@ static int hls_write_packet(AVFormatContext *s,
>> AVPacket *pkt)
>>               hls->number++;
>>           } else {
>>               ff_format_io_close(s, &oc->pb);
>> +            if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE |
>> HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
>> +                strlen(hls->current_segment_final_filename_fmt)) {
>> +                ff_rename(old_filename, hls->avf->filename, hls);
>> +            }
>>               if (hls->vtt_avf)
>>                   ff_format_io_close(s, &hls->vtt_avf->pb);
>>                 ret = hls_start(s);
>>           }
>>   -        if (ret < 0)
>> +        if (ret < 0) {
>> +            av_free(old_filename);
>>               return ret;
>> +        }
>>             if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE )
>>               oc = hls->vtt_avf;
>>           else
>>           oc = hls->avf;
>>   -        if ((ret = hls_window(s, 0)) < 0)
>> +        if ((ret = hls_window(s, 0)) < 0) {
>> +            av_free(old_filename);
>>               return ret;
>> +        }
>>       }
>>         ret = ff_write_chained(oc, stream_index, pkt, s, 0);
>>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Bodecs Bela Jan. 12, 2017, 10:44 a.m. UTC | #3
Have you noticed my second comment also about code change in my earlier 
reply?


2017.01.12. 10:39 keltezéssel, Steven Liu írta:
> 2017-01-12 17:18 GMT+08:00 Bodecs Bela <bodecsb@vivanet.hu>:
>
>>
>> 2017.01.12. 6:03 keltezéssel, Steven Liu írta:
>>
>>> when hlsenc use flag second_level_segment_index,
>>> second_level_segment_size and second_level_segment_duration,
>>> the rename is ok but the output filename always use the old filename
>>> so move the rename operation after the close the ts file and
>>> before open new segment
>>>
>> It is strange. I have tested my original patch on windows 7. Sorry.
> That's ok :)
>
> the mail title:
> [FFmpeg-user] issue with [PATCH] avformat/hlsenc: actual segment file size
> and duration in segment filenames
>
I have read it now.  I have compiled under windows with cygwin. It may 
be the reason It did not occured for me.
I think to make the actual renaming after closing ts is a better 
approach than mine was.
>
>
>>
>>> Reported-by: Christian Johannesen <chrisjohannesen@gmail.com>
>>> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
>>> ---
>>>    libavformat/hlsenc.c | 27 +++++++++++++++++++++++----
>>>    1 file changed, 23 insertions(+), 4 deletions(-)
>>>
>>> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
>>> index a2c606c..772232b 100644
>>> --- a/libavformat/hlsenc.c
>>> +++ b/libavformat/hlsenc.c
>>> @@ -499,7 +499,6 @@ static int hls_append_segment(struct AVFormatContext
>>> *s, HLSContext *hls, double
>>>                }
>>>                av_free(filename);
>>>            }
>>> -        ff_rename(old_filename, hls->avf->filename, hls);
>>>            av_free(old_filename);
>>>        }
>>>
>> I suggest to remove here, in this same scope all old_filename related
>> code, because it was used by ff_rename only:
>>
>> -        char * old_filename = av_strdup(hls->avf->filename); // %%s will
>> be %s after strftime
>> -        if (!old_filename) {
>> -            av_free(en);
>> -            return AVERROR(ENOMEM);
>> -        }
>>
>> and at several places inside if() branch:
>> -                av_free(old_filename);
>>
>>
>>
>>
>>    @@ -1239,14 +1238,22 @@ static int hls_write_packet(AVFormatContext *s,
>>> AVPacket *pkt)
>>>        if (can_split && av_compare_ts(pkt->pts - hls->start_pts,
>>> st->time_base,
>>>                                       end_pts, AV_TIME_BASE_Q) >= 0) {
>>>            int64_t new_start_pos;
>>> +        char *old_filename = av_strdup(hls->avf->filename);
>>> +
>>> +        if (!old_filename) {
>>> +            return AVERROR(ENOMEM);
>>> +        }
>>> +
>>>            av_write_frame(oc, NULL); /* Flush any buffered data */
>>>              new_start_pos = avio_tell(hls->avf->pb);
>>>            hls->size = new_start_pos - hls->start_pos;
>>>            ret = hls_append_segment(s, hls, hls->duration, hls->start_pos,
>>> hls->size);
>>>            hls->start_pos = new_start_pos;
>>> -        if (ret < 0)
>>> +        if (ret < 0) {
>>> +            av_free(old_filename);
>>>                return ret;
>>> +        }
>>>              hls->end_pts = pkt->pts;
>>>            hls->duration = 0;
>>> @@ -1261,6 +1268,10 @@ static int hls_write_packet(AVFormatContext *s,
>>> AVPacket *pkt)
>>>                if (hls->start_pos >= hls->max_seg_size) {
>>>                    hls->sequence++;
>>>                    ff_format_io_close(s, &oc->pb);
>>> +                if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE |
>>> HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
>>> +                     strlen(hls->current_segment_final_filename_fmt)) {
>>> +                    ff_rename(old_filename, hls->avf->filename, hls);
>>> +                }
>>>                    if (hls->vtt_avf)
>>>                        ff_format_io_close(s, &hls->vtt_avf->pb);
>>>                    ret = hls_start(s);
>>> @@ -1272,22 +1283,30 @@ static int hls_write_packet(AVFormatContext *s,
>>> AVPacket *pkt)
>>>                hls->number++;
>>>            } else {
>>>                ff_format_io_close(s, &oc->pb);
>>> +            if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE |
>>> HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
>>> +                strlen(hls->current_segment_final_filename_fmt)) {
>>> +                ff_rename(old_filename, hls->avf->filename, hls);
>>> +            }
>>>                if (hls->vtt_avf)
>>>                    ff_format_io_close(s, &hls->vtt_avf->pb);
>>>                  ret = hls_start(s);
>>>            }
>>>    -        if (ret < 0)
>>> +        if (ret < 0) {
>>> +            av_free(old_filename);
>>>                return ret;
>>> +        }
>>>              if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE )
>>>                oc = hls->vtt_avf;
>>>            else
>>>            oc = hls->avf;
>>>    -        if ((ret = hls_window(s, 0)) < 0)
>>> +        if ((ret = hls_window(s, 0)) < 0) {
>>> +            av_free(old_filename);
>>>                return ret;
>>> +        }
>>>        }
>>>          ret = ff_write_chained(oc, stream_index, pkt, s, 0);
>>>
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel@ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
diff mbox

Patch

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index a2c606c..772232b 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -499,7 +499,6 @@  static int hls_append_segment(struct AVFormatContext *s, HLSContext *hls, double
             }
             av_free(filename);
         }
-        ff_rename(old_filename, hls->avf->filename, hls);
         av_free(old_filename);
     }
 
@@ -1239,14 +1238,22 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (can_split && av_compare_ts(pkt->pts - hls->start_pts, st->time_base,
                                    end_pts, AV_TIME_BASE_Q) >= 0) {
         int64_t new_start_pos;
+        char *old_filename = av_strdup(hls->avf->filename);
+
+        if (!old_filename) {
+            return AVERROR(ENOMEM);
+        }
+
         av_write_frame(oc, NULL); /* Flush any buffered data */
 
         new_start_pos = avio_tell(hls->avf->pb);
         hls->size = new_start_pos - hls->start_pos;
         ret = hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
         hls->start_pos = new_start_pos;
-        if (ret < 0)
+        if (ret < 0) {
+            av_free(old_filename);
             return ret;
+        }
 
         hls->end_pts = pkt->pts;
         hls->duration = 0;
@@ -1261,6 +1268,10 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
             if (hls->start_pos >= hls->max_seg_size) {
                 hls->sequence++;
                 ff_format_io_close(s, &oc->pb);
+                if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
+                     strlen(hls->current_segment_final_filename_fmt)) {
+                    ff_rename(old_filename, hls->avf->filename, hls);
+                }
                 if (hls->vtt_avf)
                     ff_format_io_close(s, &hls->vtt_avf->pb);
                 ret = hls_start(s);
@@ -1272,22 +1283,30 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
             hls->number++;
         } else {
             ff_format_io_close(s, &oc->pb);
+            if ((hls->flags & (HLS_SECOND_LEVEL_SEGMENT_SIZE | HLS_SECOND_LEVEL_SEGMENT_DURATION)) &&
+                strlen(hls->current_segment_final_filename_fmt)) {
+                ff_rename(old_filename, hls->avf->filename, hls);
+            }
             if (hls->vtt_avf)
                 ff_format_io_close(s, &hls->vtt_avf->pb);
 
             ret = hls_start(s);
         }
 
-        if (ret < 0)
+        if (ret < 0) {
+            av_free(old_filename);
             return ret;
+        }
 
         if( st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE )
             oc = hls->vtt_avf;
         else
         oc = hls->avf;
 
-        if ((ret = hls_window(s, 0)) < 0)
+        if ((ret = hls_window(s, 0)) < 0) {
+            av_free(old_filename);
             return ret;
+        }
     }
 
     ret = ff_write_chained(oc, stream_index, pkt, s, 0);