]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/smoothstreamingenc.c
configure: Add check_x86asm() helper function to simplify some expressions
[ffmpeg] / libavformat / smoothstreamingenc.c
index 08e6526533e773ced3995f7fd375f7767c093457..9a6682465ba87aec813064d12b8b5b8b43be5a1f 100644 (file)
 
 #include "libavutil/opt.h"
 #include "libavutil/avstring.h"
+#include "libavutil/file.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/intreadwrite.h"
 
-typedef struct {
+typedef struct Fragment {
     char file[1024];
     char infofile[1024];
     int64_t start_time, duration;
@@ -45,13 +46,13 @@ typedef struct {
     int64_t start_pos, size;
 } Fragment;
 
-typedef struct {
+typedef struct OutputStream {
     AVFormatContext *ctx;
     int ctx_inited;
     char dirname[1024];
     uint8_t iobuf[32768];
     URLContext *out;  // Current output stream where all output is written
-    URLContext *out2; // Auxillary output stream where all output also is written
+    URLContext *out2; // Auxiliary output stream where all output is also written
     URLContext *tail_out; // The actual main output stream, if we're currently seeked back to write elsewhere
     int64_t tail_pos, cur_pos, cur_start_pos;
     int packets_written;
@@ -63,9 +64,11 @@ typedef struct {
     char *private_str;
     int packet_size;
     int audio_tag;
+
+    const URLProtocol **protocols;
 } OutputStream;
 
-typedef struct {
+typedef struct SmoothStreamingContext {
     const AVClass *class;  /* Class for private options. */
     int window_size;
     int extra_window_size;
@@ -75,6 +78,8 @@ typedef struct {
     OutputStream *streams;
     int has_video, has_audio;
     int nb_fragments;
+
+    const URLProtocol **protocols;
 } SmoothStreamingContext;
 
 static int ism_write(void *opaque, uint8_t *buf, int buf_size)
@@ -108,7 +113,8 @@ static int64_t ism_seek(void *opaque, int64_t offset, int whence)
         os->tail_out = NULL;
     }
     if (offset >= os->cur_start_pos) {
-        ffurl_seek(os->out, offset - os->cur_start_pos, SEEK_SET);
+        if (os->out)
+            ffurl_seek(os->out, offset - os->cur_start_pos, SEEK_SET);
         os->cur_pos = offset;
         return offset;
     }
@@ -119,7 +125,8 @@ static int64_t ism_seek(void *opaque, int64_t offset, int whence)
             AVDictionary *opts = NULL;
             os->tail_out = os->out;
             av_dict_set(&opts, "truncate", "0", 0);
-            ret = ffurl_open(&os->out, frag->file, AVIO_FLAG_READ_WRITE, &os->ctx->interrupt_callback, &opts);
+            ret = ffurl_open(&os->out, frag->file, AVIO_FLAG_WRITE, &os->ctx->interrupt_callback, &opts,
+                             os->protocols, NULL);
             av_dict_free(&opts);
             if (ret < 0) {
                 os->out = os->tail_out;
@@ -127,7 +134,8 @@ static int64_t ism_seek(void *opaque, int64_t offset, int whence)
                 return ret;
             }
             av_dict_set(&opts, "truncate", "0", 0);
-            ffurl_open(&os->out2, frag->infofile, AVIO_FLAG_READ_WRITE, &os->ctx->interrupt_callback, &opts);
+            ffurl_open(&os->out2, frag->infofile, AVIO_FLAG_WRITE, &os->ctx->interrupt_callback, &opts,
+                       os->protocols, NULL);
             av_dict_free(&opts);
             ffurl_seek(os->out, offset - frag->start_pos, SEEK_SET);
             if (os->out2)
@@ -141,21 +149,24 @@ static int64_t ism_seek(void *opaque, int64_t offset, int whence)
 
 static void get_private_data(OutputStream *os)
 {
-    AVCodecContext *codec = os->ctx->streams[0]->codec;
-    uint8_t *ptr = codec->extradata;
-    int size = codec->extradata_size;
+    AVCodecParameters *par = os->ctx->streams[0]->codecpar;
+    uint8_t *ptr = par->extradata;
+    int size = par->extradata_size;
     int i;
-    if (codec->codec_id == AV_CODEC_ID_H264) {
+    if (par->codec_id == AV_CODEC_ID_H264) {
         ff_avc_write_annexb_extradata(ptr, &ptr, &size);
         if (!ptr)
-            ptr = codec->extradata;
+            ptr = par->extradata;
     }
     if (!ptr)
         return;
     os->private_str = av_mallocz(2*size + 1);
+    if (!os->private_str)
+        goto fail;
     for (i = 0; i < size; i++)
         snprintf(&os->private_str[2*i], 3, "%02x", ptr[i]);
-    if (ptr != codec->extradata)
+fail:
+    if (ptr != par->extradata)
         av_free(ptr);
 }
 
@@ -163,6 +174,9 @@ static void ism_free(AVFormatContext *s)
 {
     SmoothStreamingContext *c = s->priv_data;
     int i, j;
+
+    av_freep(&c->protocols);
+
     if (!c->streams)
         return;
     for (i = 0; i < s->nb_streams; i++) {
@@ -173,8 +187,8 @@ static void ism_free(AVFormatContext *s)
         os->out = os->out2 = os->tail_out = NULL;
         if (os->ctx && os->ctx_inited)
             av_write_trailer(os->ctx);
-        if (os->ctx && os->ctx->pb)
-            av_free(os->ctx->pb);
+        if (os->ctx)
+            avio_context_free(&os->ctx->pb);
         if (os->ctx)
             avformat_free_context(os->ctx);
         av_free(os->private_str);
@@ -185,18 +199,112 @@ static void ism_free(AVFormatContext *s)
     av_freep(&c->streams);
 }
 
+static void output_chunk_list(OutputStream *os, AVIOContext *out, int final, int skip, int window_size)
+{
+    int removed = 0, i, start = 0;
+    if (os->nb_fragments <= 0)
+        return;
+    if (os->fragments[0]->n > 0)
+        removed = 1;
+    if (final)
+        skip = 0;
+    if (window_size)
+        start = FFMAX(os->nb_fragments - skip - window_size, 0);
+    for (i = start; i < os->nb_fragments - skip; i++) {
+        Fragment *frag = os->fragments[i];
+        if (!final || removed)
+            avio_printf(out, "<c t=\"%"PRIu64"\" d=\"%"PRIu64"\" />\n", frag->start_time, frag->duration);
+        else
+            avio_printf(out, "<c n=\"%d\" d=\"%"PRIu64"\" />\n", frag->n, frag->duration);
+    }
+}
+
+static int write_manifest(AVFormatContext *s, int final)
+{
+    SmoothStreamingContext *c = s->priv_data;
+    AVIOContext *out;
+    char filename[1024], temp_filename[1024];
+    int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
+    int64_t duration = 0;
+
+    snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
+    snprintf(temp_filename, sizeof(temp_filename), "%s/Manifest.tmp", s->filename);
+    ret = s->io_open(s, &out, temp_filename, AVIO_FLAG_WRITE, NULL);
+    if (ret < 0) {
+        av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
+        return ret;
+    }
+    avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
+    for (i = 0; i < s->nb_streams; i++) {
+        OutputStream *os = &c->streams[i];
+        if (os->nb_fragments > 0) {
+            Fragment *last = os->fragments[os->nb_fragments - 1];
+            duration = last->start_time + last->duration;
+        }
+        if (s->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
+            video_chunks = os->nb_fragments;
+            video_streams++;
+        } else {
+            audio_chunks = os->nb_fragments;
+            audio_streams++;
+        }
+    }
+    if (!final) {
+        duration = 0;
+        video_chunks = audio_chunks = 0;
+    }
+    if (c->window_size) {
+        video_chunks = FFMIN(video_chunks, c->window_size);
+        audio_chunks = FFMIN(audio_chunks, c->window_size);
+    }
+    avio_printf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" Duration=\"%"PRIu64"\"", duration);
+    if (!final)
+        avio_printf(out, " IsLive=\"true\" LookAheadFragmentCount=\"%d\" DVRWindowLength=\"0\"", c->lookahead_count);
+    avio_printf(out, ">\n");
+    if (c->has_video) {
+        int last = -1, index = 0;
+        avio_printf(out, "<StreamIndex Type=\"video\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n", video_streams, video_chunks);
+        for (i = 0; i < s->nb_streams; i++) {
+            OutputStream *os = &c->streams[i];
+            if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
+                continue;
+            last = i;
+            avio_printf(out, "<QualityLevel Index=\"%d\" Bitrate=\"%d\" FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" CodecPrivateData=\"%s\" />\n", index, s->streams[i]->codecpar->bit_rate, os->fourcc, s->streams[i]->codecpar->width, s->streams[i]->codecpar->height, os->private_str);
+            index++;
+        }
+        output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
+        avio_printf(out, "</StreamIndex>\n");
+    }
+    if (c->has_audio) {
+        int last = -1, index = 0;
+        avio_printf(out, "<StreamIndex Type=\"audio\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n", audio_streams, audio_chunks);
+        for (i = 0; i < s->nb_streams; i++) {
+            OutputStream *os = &c->streams[i];
+            if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
+                continue;
+            last = i;
+            avio_printf(out, "<QualityLevel Index=\"%d\" Bitrate=\"%d\" FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" BitsPerSample=\"16\" PacketSize=\"%d\" AudioTag=\"%d\" CodecPrivateData=\"%s\" />\n", index, s->streams[i]->codecpar->bit_rate, os->fourcc, s->streams[i]->codecpar->sample_rate, s->streams[i]->codecpar->channels, os->packet_size, os->audio_tag, os->private_str);
+            index++;
+        }
+        output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
+        avio_printf(out, "</StreamIndex>\n");
+    }
+    avio_printf(out, "</SmoothStreamingMedia>\n");
+    avio_flush(out);
+    ff_format_io_close(s, &out);
+    return ff_rename(temp_filename, filename);
+}
+
 static int ism_write_header(AVFormatContext *s)
 {
     SmoothStreamingContext *c = s->priv_data;
     int ret = 0, i;
     AVOutputFormat *oformat;
 
-    ret = mkdir(s->filename, 0777);
-    if (ret) {
-        av_log(s, AV_LOG_ERROR, "mkdir(%s): %s\n", s->filename, strerror(errno));
-        return AVERROR(errno);
+    if (mkdir(s->filename, 0777) == -1 && errno != EEXIST) {
+        ret = AVERROR(errno);
+        goto fail;
     }
-    ret = 0;
 
     oformat = av_guess_format("ismv", NULL, NULL);
     if (!oformat) {
@@ -204,6 +312,12 @@ static int ism_write_header(AVFormatContext *s)
         goto fail;
     }
 
+    c->protocols = ffurl_get_protocols(s->protocol_whitelist, s->protocol_blacklist);
+    if (!c->protocols) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
     c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
     if (!c->streams) {
         ret = AVERROR(ENOMEM);
@@ -217,13 +331,18 @@ static int ism_write_header(AVFormatContext *s)
         AVDictionary *opts = NULL;
         char buf[10];
 
-        if (!s->streams[i]->codec->bit_rate) {
+        if (!s->streams[i]->codecpar->bit_rate) {
             av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
             ret = AVERROR(EINVAL);
             goto fail;
         }
-        snprintf(os->dirname, sizeof(os->dirname), "%s/QualityLevels(%d)", s->filename, s->streams[i]->codec->bit_rate);
-        mkdir(os->dirname, 0777);
+        snprintf(os->dirname, sizeof(os->dirname), "%s/QualityLevels(%d)", s->filename, s->streams[i]->codecpar->bit_rate);
+        if (mkdir(os->dirname, 0777) == -1 && errno != EEXIST) {
+            ret = AVERROR(errno);
+            goto fail;
+        }
+
+        os->protocols = c->protocols;
 
         ctx = avformat_alloc_context();
         if (!ctx) {
@@ -238,7 +357,9 @@ static int ism_write_header(AVFormatContext *s)
             ret = AVERROR(ENOMEM);
             goto fail;
         }
-        avcodec_copy_context(st->codec, s->streams[i]->codec);
+        avcodec_parameters_copy(st->codecpar, s->streams[i]->codecpar);
+        st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
+        st->time_base = s->streams[i]->time_base;
 
         ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, ism_write, ism_seek);
         if (!ctx->pb) {
@@ -256,12 +377,12 @@ static int ism_write_header(AVFormatContext *s)
         avio_flush(ctx->pb);
         av_dict_free(&opts);
         s->streams[i]->time_base = st->time_base;
-        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
+        if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
             c->has_video = 1;
             os->stream_type_tag = "video";
-            if (st->codec->codec_id == AV_CODEC_ID_H264) {
+            if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
                 os->fourcc = "H264";
-            } else if (st->codec->codec_id == AV_CODEC_ID_VC1) {
+            } else if (st->codecpar->codec_id == AV_CODEC_ID_VC1) {
                 os->fourcc = "WVC1";
             } else {
                 av_log(s, AV_LOG_ERROR, "Unsupported video codec\n");
@@ -271,10 +392,10 @@ static int ism_write_header(AVFormatContext *s)
         } else {
             c->has_audio = 1;
             os->stream_type_tag = "audio";
-            if (st->codec->codec_id == AV_CODEC_ID_AAC) {
+            if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
                 os->fourcc = "AACL";
                 os->audio_tag = 0xff;
-            } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
+            } else if (st->codecpar->codec_id == AV_CODEC_ID_WMAPRO) {
                 os->fourcc = "WMAP";
                 os->audio_tag = 0x0162;
             } else {
@@ -282,7 +403,7 @@ static int ism_write_header(AVFormatContext *s)
                 ret = AVERROR(EINVAL);
                 goto fail;
             }
-            os->packet_size = st->codec->block_align ? st->codec->block_align : 4;
+            os->packet_size = st->codecpar->block_align ? st->codecpar->block_align : 4;
         }
         get_private_data(os);
     }
@@ -290,7 +411,9 @@ static int ism_write_header(AVFormatContext *s)
     if (!c->has_video && c->min_frag_duration <= 0) {
         av_log(s, AV_LOG_WARNING, "no video stream and no min frag duration set\n");
         ret = AVERROR(EINVAL);
+        goto fail;
     }
+    ret = write_manifest(s, 0);
 
 fail:
     if (ret)
@@ -303,7 +426,7 @@ static int parse_fragment(AVFormatContext *s, const char *filename, int64_t *sta
     AVIOContext *in;
     int ret;
     uint32_t len;
-    if ((ret = avio_open2(&in, filename, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
+    if ((ret = s->io_open(s, &in, filename, AVIO_FLAG_READ, NULL)) < 0)
         return ret;
     ret = AVERROR(EIO);
     *moof_size = avio_rb32(in);
@@ -327,7 +450,7 @@ static int parse_fragment(AVFormatContext *s, const char *filename, int64_t *sta
         if (len < 8 || len >= *moof_size)
             goto fail;
         if (tag == MKTAG('u','u','i','d')) {
-            const uint8_t tfxd[] = {
+            static const uint8_t tfxd[] = {
                 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
                 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
             };
@@ -344,18 +467,22 @@ static int parse_fragment(AVFormatContext *s, const char *filename, int64_t *sta
         avio_seek(in, end, SEEK_SET);
     }
 fail:
-    avio_close(in);
+    ff_format_io_close(s, &in);
     return ret;
 }
 
 static int add_fragment(OutputStream *os, const char *file, const char *infofile, int64_t start_time, int64_t duration, int64_t start_pos, int64_t size)
 {
+    int err;
     Fragment *frag;
     if (os->nb_fragments >= os->fragments_size) {
         os->fragments_size = (os->fragments_size + 1) * 2;
-        os->fragments = av_realloc(os->fragments, sizeof(*os->fragments)*os->fragments_size);
-        if (!os->fragments)
-            return AVERROR(ENOMEM);
+        if ((err = av_reallocp(&os->fragments, sizeof(*os->fragments) *
+                               os->fragments_size)) < 0) {
+            os->fragments_size = 0;
+            os->nb_fragments = 0;
+            return err;
+        }
     }
     frag = av_mallocz(sizeof(*frag));
     if (!frag)
@@ -376,10 +503,10 @@ static int copy_moof(AVFormatContext *s, const char* infile, const char *outfile
 {
     AVIOContext *in, *out;
     int ret = 0;
-    if ((ret = avio_open2(&in, infile, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
+    if ((ret = s->io_open(s, &in, infile, AVIO_FLAG_READ, NULL)) < 0)
         return ret;
-    if ((ret = avio_open2(&out, outfile, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL)) < 0) {
-        avio_close(in);
+    if ((ret = s->io_open(s, &out, outfile, AVIO_FLAG_WRITE, NULL)) < 0) {
+        ff_format_io_close(s, &in);
         return ret;
     }
     while (size > 0) {
@@ -394,104 +521,11 @@ static int copy_moof(AVFormatContext *s, const char* infile, const char *outfile
         size -= n;
     }
     avio_flush(out);
-    avio_close(out);
-    avio_close(in);
+    ff_format_io_close(s, &out);
+    ff_format_io_close(s, &in);
     return ret;
 }
 
-static void output_chunk_list(OutputStream *os, AVIOContext *out, int final, int skip, int window_size)
-{
-    int removed = 0, i, start = 0;
-    if (os->nb_fragments <= 0)
-        return;
-    if (os->fragments[0]->n > 0)
-        removed = 1;
-    if (final)
-        skip = 0;
-    if (window_size)
-        start = FFMAX(os->nb_fragments - skip - window_size, 0);
-    for (i = start; i < os->nb_fragments - skip; i++) {
-        Fragment *frag = os->fragments[i];
-        if (!final || removed)
-            avio_printf(out, "<c t=\"%"PRIu64"\" d=\"%"PRIu64"\" />\n", frag->start_time, frag->duration);
-        else
-            avio_printf(out, "<c n=\"%d\" d=\"%"PRIu64"\" />\n", frag->n, frag->duration);
-    }
-}
-
-static int write_manifest(AVFormatContext *s, int final)
-{
-    SmoothStreamingContext *c = s->priv_data;
-    AVIOContext *out;
-    char filename[1024];
-    int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
-    int64_t duration = 0;
-
-    snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
-    ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
-    if (ret < 0)
-        return ret;
-    avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
-    for (i = 0; i < s->nb_streams; i++) {
-        OutputStream *os = &c->streams[i];
-        if (os->nb_fragments > 0) {
-            Fragment *last = os->fragments[os->nb_fragments - 1];
-            duration = last->start_time + last->duration;
-        }
-        if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
-            video_chunks = os->nb_fragments;
-            video_streams++;
-        } else {
-            audio_chunks = os->nb_fragments;
-            audio_streams++;
-        }
-    }
-    if (!final) {
-        duration = 0;
-        video_chunks = audio_chunks = 0;
-    }
-    if (c->window_size) {
-        video_chunks = FFMIN(video_chunks, c->window_size);
-        audio_chunks = FFMIN(audio_chunks, c->window_size);
-    }
-    avio_printf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" Duration=\"%"PRIu64"\"", duration);
-    if (!final)
-        avio_printf(out, " IsLive=\"true\" LookAheadFragmentCount=\"%d\" DVRWindowLength=\"0\"", c->lookahead_count);
-    avio_printf(out, ">\n");
-    if (c->has_video) {
-        int last = -1, index = 0;
-        avio_printf(out, "<StreamIndex Type=\"video\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n", video_streams, video_chunks);
-        for (i = 0; i < s->nb_streams; i++) {
-            OutputStream *os = &c->streams[i];
-            if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
-                continue;
-            last = i;
-            avio_printf(out, "<QualityLevel Index=\"%d\" Bitrate=\"%d\" FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" CodecPrivateData=\"%s\" />\n", index, s->streams[i]->codec->bit_rate, os->fourcc, s->streams[i]->codec->width, s->streams[i]->codec->height, os->private_str);
-            index++;
-        }
-        output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
-        avio_printf(out, "</StreamIndex>\n");
-    }
-    if (c->has_audio) {
-        int last = -1, index = 0;
-        avio_printf(out, "<StreamIndex Type=\"audio\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n", audio_streams, audio_chunks);
-        for (i = 0; i < s->nb_streams; i++) {
-            OutputStream *os = &c->streams[i];
-            if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
-                continue;
-            last = i;
-            avio_printf(out, "<QualityLevel Index=\"%d\" Bitrate=\"%d\" FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" BitsPerSample=\"16\" PacketSize=\"%d\" AudioTag=\"%d\" CodecPrivateData=\"%s\" />\n", index, s->streams[i]->codec->bit_rate, os->fourcc, s->streams[i]->codec->sample_rate, s->streams[i]->codec->channels, os->packet_size, os->audio_tag, os->private_str);
-            index++;
-        }
-        output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
-        avio_printf(out, "</StreamIndex>\n");
-    }
-    avio_printf(out, "</SmoothStreamingMedia>\n");
-    avio_flush(out);
-    avio_close(out);
-    return 0;
-}
-
 static int ism_flush(AVFormatContext *s, int final)
 {
     SmoothStreamingContext *c = s->priv_data;
@@ -500,13 +534,14 @@ static int ism_flush(AVFormatContext *s, int final)
     for (i = 0; i < s->nb_streams; i++) {
         OutputStream *os = &c->streams[i];
         char filename[1024], target_filename[1024], header_filename[1024];
-        int64_t start_pos = os->tail_pos, size;
+        int64_t size;
         int64_t start_ts, duration, moof_size;
         if (!os->packets_written)
             continue;
 
         snprintf(filename, sizeof(filename), "%s/temp", os->dirname);
-        ret = ffurl_open(&os->out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
+        ret = ffurl_open(&os->out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL,
+                         c->protocols, NULL);
         if (ret < 0)
             break;
         os->cur_start_pos = os->tail_pos;
@@ -518,14 +553,17 @@ static int ism_flush(AVFormatContext *s, int final)
 
         ffurl_close(os->out);
         os->out = NULL;
-        size = os->tail_pos - start_pos;
+        size = os->tail_pos - os->cur_start_pos;
         if ((ret = parse_fragment(s, filename, &start_ts, &duration, &moof_size, size)) < 0)
             break;
         snprintf(header_filename, sizeof(header_filename), "%s/FragmentInfo(%s=%"PRIu64")", os->dirname, os->stream_type_tag, start_ts);
         snprintf(target_filename, sizeof(target_filename), "%s/Fragments(%s=%"PRIu64")", os->dirname, os->stream_type_tag, start_ts);
         copy_moof(s, filename, header_filename, moof_size);
-        rename(filename, target_filename);
-        add_fragment(os, target_filename, header_filename, start_ts, duration, start_pos, size);
+        ret = ff_rename(filename, target_filename);
+        if (ret < 0)
+            break;
+        add_fragment(os, target_filename, header_filename, start_ts, duration,
+                     os->cur_start_pos, size);
     }
 
     if (c->window_size || (final && c->remove_at_exit)) {
@@ -549,7 +587,8 @@ static int ism_flush(AVFormatContext *s, int final)
         }
     }
 
-    write_manifest(s, final);
+    if (ret >= 0)
+        ret = write_manifest(s, final);
     return ret;
 }
 
@@ -558,14 +597,19 @@ static int ism_write_packet(AVFormatContext *s, AVPacket *pkt)
     SmoothStreamingContext *c = s->priv_data;
     AVStream *st = s->streams[pkt->stream_index];
     OutputStream *os = &c->streams[pkt->stream_index];
-    int64_t end_pts = (c->nb_fragments + 1) * c->min_frag_duration;
+    int64_t end_dts = (c->nb_fragments + 1) * (int64_t) c->min_frag_duration;
+    int ret;
+
+    if (st->first_dts == AV_NOPTS_VALUE)
+        st->first_dts = pkt->dts;
 
-    if ((!c->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
-        av_compare_ts(pkt->pts, st->time_base,
-                      end_pts, AV_TIME_BASE_Q) >= 0 &&
+    if ((!c->has_video || st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) &&
+        av_compare_ts(pkt->dts - st->first_dts, st->time_base,
+                      end_dts, AV_TIME_BASE_Q) >= 0 &&
         pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
 
-        ism_flush(s, 0);
+        if ((ret = ism_flush(s, 0)) < 0)
+            return ret;
         c->nb_fragments++;
     }
 
@@ -618,6 +662,5 @@ AVOutputFormat ff_smoothstreaming_muxer = {
     .write_header   = ism_write_header,
     .write_packet   = ism_write_packet,
     .write_trailer  = ism_write_trailer,
-    .codec_tag      = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
     .priv_class     = &ism_class,
 };