[FFmpeg-devel] avformat/hlsenc: fix hls start and tail segment duration problem

Submitted by Steven Liu on Jan. 9, 2017, 5:06 p.m.

Details

Message ID 20170109170604.26801-1-lq@chinaffmpeg.org
State New
Headers show

Commit Message

Steven Liu Jan. 9, 2017, 5:06 p.m.
fix ticket: #6067

Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
---
 libavformat/hlsenc.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

Comments

Steven Liu Jan. 10, 2017, 12:05 a.m.
2017-01-10 1:06 GMT+08:00 Steven Liu <lq@chinaffmpeg.org>:

> fix ticket: #6067
>
> Signed-off-by: Steven Liu <lq@chinaffmpeg.org>
> ---
>  libavformat/hlsenc.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index eeb450a..0fcb699 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -103,6 +103,7 @@ typedef struct HLSContext {
>      int64_t recording_time;
>      int has_video;
>      int has_subtitle;
> +    double dpp;           // duration per packet
>      int64_t start_pts;
>      int64_t end_pts;
>      double duration;      // last segment duration computed so far, in
> seconds
> @@ -1216,10 +1217,16 @@ static int hls_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>      if (pkt->pts == AV_NOPTS_VALUE)
>          is_ref_pkt = can_split = 0;
>
> -    if (is_ref_pkt)
> -        hls->duration = (double)(pkt->pts - hls->end_pts)
> -                                   * st->time_base.num /
> st->time_base.den;
> +    if (is_ref_pkt) {
> +        if (!hls->start_pos) {
> +            hls->duration = (double)(pkt->pts - hls->end_pts)
> +                                       * st->time_base.num /
> st->time_base.den;
> +            hls->dpp = (double)(pkt->duration) * st->time_base.num /
> st->time_base.den;
> +        } else {
> +            hls->duration += (double)(pkt->duration) * st->time_base.num
> / st->time_base.den;
> +        }
>
> +    }
>      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;
> @@ -1289,7 +1296,8 @@ 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);
> -        hls_append_segment(s, hls, hls->duration, hls->start_pos,
> hls->size);
> +        /* after av_write_trailer, then duration + 1 duration per packet
> */
> +        hls_append_segment(s, hls, hls->duration + hls->dpp,
> hls->start_pos, hls->size);
>      }
>
>      if (vtt_oc) {
> --
> 2.10.1.382.ga23ca1b.dirty
>
>
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>

update patch.

Patch hide | download patch | download mbox

diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index eeb450a..0fcb699 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -103,6 +103,7 @@  typedef struct HLSContext {
     int64_t recording_time;
     int has_video;
     int has_subtitle;
+    double dpp;           // duration per packet
     int64_t start_pts;
     int64_t end_pts;
     double duration;      // last segment duration computed so far, in seconds
@@ -1216,10 +1217,16 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (pkt->pts == AV_NOPTS_VALUE)
         is_ref_pkt = can_split = 0;
 
-    if (is_ref_pkt)
-        hls->duration = (double)(pkt->pts - hls->end_pts)
-                                   * st->time_base.num / st->time_base.den;
+    if (is_ref_pkt) {
+        if (!hls->start_pos) {
+            hls->duration = (double)(pkt->pts - hls->end_pts)
+                                       * st->time_base.num / st->time_base.den;
+            hls->dpp = (double)(pkt->duration) * st->time_base.num / st->time_base.den;
+        } else {
+            hls->duration += (double)(pkt->duration) * st->time_base.num / st->time_base.den;
+        }
 
+    }
     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;
@@ -1289,7 +1296,8 @@  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);
-        hls_append_segment(s, hls, hls->duration, hls->start_pos, hls->size);
+        /* after av_write_trailer, then duration + 1 duration per packet */
+        hls_append_segment(s, hls, hls->duration + hls->dpp, hls->start_pos, hls->size);
     }
 
     if (vtt_oc) {