00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include <float.h>
00024 #if HAVE_UNISTD_H
00025 #include <unistd.h>
00026 #endif
00027
00028 #include "avformat.h"
00029 #include "internal.h"
00030 #include "os_support.h"
00031 #include "avc.h"
00032 #include "url.h"
00033 #include "isom.h"
00034
00035 #include "libavutil/opt.h"
00036 #include "libavutil/avstring.h"
00037 #include "libavutil/mathematics.h"
00038 #include "libavutil/intreadwrite.h"
00039
00040 typedef struct {
00041 char file[1024];
00042 char infofile[1024];
00043 int64_t start_time, duration;
00044 int n;
00045 int64_t start_pos, size;
00046 } Fragment;
00047
00048 typedef struct {
00049 AVFormatContext *ctx;
00050 int ctx_inited;
00051 char dirname[1024];
00052 uint8_t iobuf[32768];
00053 URLContext *out;
00054 URLContext *out2;
00055 URLContext *tail_out;
00056 int64_t tail_pos, cur_pos, cur_start_pos;
00057 int packets_written;
00058 const char *stream_type_tag;
00059 int nb_fragments, fragments_size, fragment_index;
00060 Fragment **fragments;
00061
00062 const char *fourcc;
00063 char *private_str;
00064 int packet_size;
00065 int audio_tag;
00066 } OutputStream;
00067
00068 typedef struct {
00069 const AVClass *class;
00070 int window_size;
00071 int extra_window_size;
00072 int lookahead_count;
00073 int min_frag_duration;
00074 int remove_at_exit;
00075 OutputStream *streams;
00076 int has_video, has_audio;
00077 int nb_fragments;
00078 } SmoothStreamingContext;
00079
00080 static int ism_write(void *opaque, uint8_t *buf, int buf_size)
00081 {
00082 OutputStream *os = opaque;
00083 if (os->out)
00084 ffurl_write(os->out, buf, buf_size);
00085 if (os->out2)
00086 ffurl_write(os->out2, buf, buf_size);
00087 os->cur_pos += buf_size;
00088 if (os->cur_pos >= os->tail_pos)
00089 os->tail_pos = os->cur_pos;
00090 return buf_size;
00091 }
00092
00093 static int64_t ism_seek(void *opaque, int64_t offset, int whence)
00094 {
00095 OutputStream *os = opaque;
00096 int i;
00097 if (whence != SEEK_SET)
00098 return AVERROR(ENOSYS);
00099 if (os->tail_out) {
00100 if (os->out) {
00101 ffurl_close(os->out);
00102 }
00103 if (os->out2) {
00104 ffurl_close(os->out2);
00105 }
00106 os->out = os->tail_out;
00107 os->out2 = NULL;
00108 os->tail_out = NULL;
00109 }
00110 if (offset >= os->cur_start_pos) {
00111 if (os->out)
00112 ffurl_seek(os->out, offset - os->cur_start_pos, SEEK_SET);
00113 os->cur_pos = offset;
00114 return offset;
00115 }
00116 for (i = os->nb_fragments - 1; i >= 0; i--) {
00117 Fragment *frag = os->fragments[i];
00118 if (offset >= frag->start_pos && offset < frag->start_pos + frag->size) {
00119 int ret;
00120 AVDictionary *opts = NULL;
00121 os->tail_out = os->out;
00122 av_dict_set(&opts, "truncate", "0", 0);
00123 ret = ffurl_open(&os->out, frag->file, AVIO_FLAG_READ_WRITE, &os->ctx->interrupt_callback, &opts);
00124 av_dict_free(&opts);
00125 if (ret < 0) {
00126 os->out = os->tail_out;
00127 os->tail_out = NULL;
00128 return ret;
00129 }
00130 av_dict_set(&opts, "truncate", "0", 0);
00131 ffurl_open(&os->out2, frag->infofile, AVIO_FLAG_READ_WRITE, &os->ctx->interrupt_callback, &opts);
00132 av_dict_free(&opts);
00133 ffurl_seek(os->out, offset - frag->start_pos, SEEK_SET);
00134 if (os->out2)
00135 ffurl_seek(os->out2, offset - frag->start_pos, SEEK_SET);
00136 os->cur_pos = offset;
00137 return offset;
00138 }
00139 }
00140 return AVERROR(EIO);
00141 }
00142
00143 static void get_private_data(OutputStream *os)
00144 {
00145 AVCodecContext *codec = os->ctx->streams[0]->codec;
00146 uint8_t *ptr = codec->extradata;
00147 int size = codec->extradata_size;
00148 int i;
00149 if (codec->codec_id == AV_CODEC_ID_H264) {
00150 ff_avc_write_annexb_extradata(ptr, &ptr, &size);
00151 if (!ptr)
00152 ptr = codec->extradata;
00153 }
00154 if (!ptr)
00155 return;
00156 os->private_str = av_mallocz(2*size + 1);
00157 for (i = 0; i < size; i++)
00158 snprintf(&os->private_str[2*i], 3, "%02x", ptr[i]);
00159 if (ptr != codec->extradata)
00160 av_free(ptr);
00161 }
00162
00163 static void ism_free(AVFormatContext *s)
00164 {
00165 SmoothStreamingContext *c = s->priv_data;
00166 int i, j;
00167 if (!c->streams)
00168 return;
00169 for (i = 0; i < s->nb_streams; i++) {
00170 OutputStream *os = &c->streams[i];
00171 ffurl_close(os->out);
00172 ffurl_close(os->out2);
00173 ffurl_close(os->tail_out);
00174 os->out = os->out2 = os->tail_out = NULL;
00175 if (os->ctx && os->ctx_inited)
00176 av_write_trailer(os->ctx);
00177 if (os->ctx && os->ctx->pb)
00178 av_free(os->ctx->pb);
00179 if (os->ctx)
00180 avformat_free_context(os->ctx);
00181 av_free(os->private_str);
00182 for (j = 0; j < os->nb_fragments; j++)
00183 av_free(os->fragments[j]);
00184 av_free(os->fragments);
00185 }
00186 av_freep(&c->streams);
00187 }
00188
00189 static void output_chunk_list(OutputStream *os, AVIOContext *out, int final, int skip, int window_size)
00190 {
00191 int removed = 0, i, start = 0;
00192 if (os->nb_fragments <= 0)
00193 return;
00194 if (os->fragments[0]->n > 0)
00195 removed = 1;
00196 if (final)
00197 skip = 0;
00198 if (window_size)
00199 start = FFMAX(os->nb_fragments - skip - window_size, 0);
00200 for (i = start; i < os->nb_fragments - skip; i++) {
00201 Fragment *frag = os->fragments[i];
00202 if (!final || removed)
00203 avio_printf(out, "<c t=\"%"PRIu64"\" d=\"%"PRIu64"\" />\n", frag->start_time, frag->duration);
00204 else
00205 avio_printf(out, "<c n=\"%d\" d=\"%"PRIu64"\" />\n", frag->n, frag->duration);
00206 }
00207 }
00208
00209 static int write_manifest(AVFormatContext *s, int final)
00210 {
00211 SmoothStreamingContext *c = s->priv_data;
00212 AVIOContext *out;
00213 char filename[1024];
00214 int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
00215 int64_t duration = 0;
00216
00217 snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
00218 ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
00219 if (ret < 0) {
00220 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", filename);
00221 return ret;
00222 }
00223 avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
00224 for (i = 0; i < s->nb_streams; i++) {
00225 OutputStream *os = &c->streams[i];
00226 if (os->nb_fragments > 0) {
00227 Fragment *last = os->fragments[os->nb_fragments - 1];
00228 duration = last->start_time + last->duration;
00229 }
00230 if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00231 video_chunks = os->nb_fragments;
00232 video_streams++;
00233 } else {
00234 audio_chunks = os->nb_fragments;
00235 audio_streams++;
00236 }
00237 }
00238 if (!final) {
00239 duration = 0;
00240 video_chunks = audio_chunks = 0;
00241 }
00242 if (c->window_size) {
00243 video_chunks = FFMIN(video_chunks, c->window_size);
00244 audio_chunks = FFMIN(audio_chunks, c->window_size);
00245 }
00246 avio_printf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" Duration=\"%"PRIu64"\"", duration);
00247 if (!final)
00248 avio_printf(out, " IsLive=\"true\" LookAheadFragmentCount=\"%d\" DVRWindowLength=\"0\"", c->lookahead_count);
00249 avio_printf(out, ">\n");
00250 if (c->has_video) {
00251 int last = -1, index = 0;
00252 avio_printf(out, "<StreamIndex Type=\"video\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n", video_streams, video_chunks);
00253 for (i = 0; i < s->nb_streams; i++) {
00254 OutputStream *os = &c->streams[i];
00255 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
00256 continue;
00257 last = i;
00258 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);
00259 index++;
00260 }
00261 output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
00262 avio_printf(out, "</StreamIndex>\n");
00263 }
00264 if (c->has_audio) {
00265 int last = -1, index = 0;
00266 avio_printf(out, "<StreamIndex Type=\"audio\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n", audio_streams, audio_chunks);
00267 for (i = 0; i < s->nb_streams; i++) {
00268 OutputStream *os = &c->streams[i];
00269 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
00270 continue;
00271 last = i;
00272 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);
00273 index++;
00274 }
00275 output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
00276 avio_printf(out, "</StreamIndex>\n");
00277 }
00278 avio_printf(out, "</SmoothStreamingMedia>\n");
00279 avio_flush(out);
00280 avio_close(out);
00281 return 0;
00282 }
00283
00284 static int ism_write_header(AVFormatContext *s)
00285 {
00286 SmoothStreamingContext *c = s->priv_data;
00287 int ret = 0, i;
00288 AVOutputFormat *oformat;
00289
00290 if (mkdir(s->filename, 0777) < 0) {
00291 av_log(s, AV_LOG_ERROR, "mkdir failed\n");
00292 ret = AVERROR(errno);
00293 goto fail;
00294 }
00295
00296 oformat = av_guess_format("ismv", NULL, NULL);
00297 if (!oformat) {
00298 ret = AVERROR_MUXER_NOT_FOUND;
00299 goto fail;
00300 }
00301
00302 c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
00303 if (!c->streams) {
00304 ret = AVERROR(ENOMEM);
00305 goto fail;
00306 }
00307
00308 for (i = 0; i < s->nb_streams; i++) {
00309 OutputStream *os = &c->streams[i];
00310 AVFormatContext *ctx;
00311 AVStream *st;
00312 AVDictionary *opts = NULL;
00313 char buf[10];
00314
00315 if (!s->streams[i]->codec->bit_rate) {
00316 av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
00317 ret = AVERROR(EINVAL);
00318 goto fail;
00319 }
00320 snprintf(os->dirname, sizeof(os->dirname), "%s/QualityLevels(%d)", s->filename, s->streams[i]->codec->bit_rate);
00321 if (mkdir(os->dirname, 0777) < 0) {
00322 ret = AVERROR(errno);
00323 av_log(s, AV_LOG_ERROR, "mkdir failed\n");
00324 goto fail;
00325 }
00326
00327 ctx = avformat_alloc_context();
00328 if (!ctx) {
00329 ret = AVERROR(ENOMEM);
00330 goto fail;
00331 }
00332 os->ctx = ctx;
00333 ctx->oformat = oformat;
00334 ctx->interrupt_callback = s->interrupt_callback;
00335
00336 if (!(st = avformat_new_stream(ctx, NULL))) {
00337 ret = AVERROR(ENOMEM);
00338 goto fail;
00339 }
00340 avcodec_copy_context(st->codec, s->streams[i]->codec);
00341 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
00342
00343 ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, ism_write, ism_seek);
00344 if (!ctx->pb) {
00345 ret = AVERROR(ENOMEM);
00346 goto fail;
00347 }
00348
00349 snprintf(buf, sizeof(buf), "%d", c->lookahead_count);
00350 av_dict_set(&opts, "ism_lookahead", buf, 0);
00351 av_dict_set(&opts, "movflags", "frag_custom", 0);
00352 if ((ret = avformat_write_header(ctx, &opts)) < 0) {
00353 goto fail;
00354 }
00355 os->ctx_inited = 1;
00356 avio_flush(ctx->pb);
00357 av_dict_free(&opts);
00358 s->streams[i]->time_base = st->time_base;
00359 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
00360 c->has_video = 1;
00361 os->stream_type_tag = "video";
00362 if (st->codec->codec_id == AV_CODEC_ID_H264) {
00363 os->fourcc = "H264";
00364 } else if (st->codec->codec_id == AV_CODEC_ID_VC1) {
00365 os->fourcc = "WVC1";
00366 } else {
00367 av_log(s, AV_LOG_ERROR, "Unsupported video codec\n");
00368 ret = AVERROR(EINVAL);
00369 goto fail;
00370 }
00371 } else {
00372 c->has_audio = 1;
00373 os->stream_type_tag = "audio";
00374 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
00375 os->fourcc = "AACL";
00376 os->audio_tag = 0xff;
00377 } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
00378 os->fourcc = "WMAP";
00379 os->audio_tag = 0x0162;
00380 } else {
00381 av_log(s, AV_LOG_ERROR, "Unsupported audio codec\n");
00382 ret = AVERROR(EINVAL);
00383 goto fail;
00384 }
00385 os->packet_size = st->codec->block_align ? st->codec->block_align : 4;
00386 }
00387 get_private_data(os);
00388 }
00389
00390 if (!c->has_video && c->min_frag_duration <= 0) {
00391 av_log(s, AV_LOG_WARNING, "no video stream and no min frag duration set\n");
00392 ret = AVERROR(EINVAL);
00393 }
00394 ret = write_manifest(s, 0);
00395
00396 fail:
00397 if (ret)
00398 ism_free(s);
00399 return ret;
00400 }
00401
00402 static int parse_fragment(AVFormatContext *s, const char *filename, int64_t *start_ts, int64_t *duration, int64_t *moof_size, int64_t size)
00403 {
00404 AVIOContext *in;
00405 int ret;
00406 uint32_t len;
00407 if ((ret = avio_open2(&in, filename, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
00408 return ret;
00409 ret = AVERROR(EIO);
00410 *moof_size = avio_rb32(in);
00411 if (*moof_size < 8 || *moof_size > size)
00412 goto fail;
00413 if (avio_rl32(in) != MKTAG('m','o','o','f'))
00414 goto fail;
00415 len = avio_rb32(in);
00416 if (len > *moof_size)
00417 goto fail;
00418 if (avio_rl32(in) != MKTAG('m','f','h','d'))
00419 goto fail;
00420 avio_seek(in, len - 8, SEEK_CUR);
00421 avio_rb32(in);
00422 if (avio_rl32(in) != MKTAG('t','r','a','f'))
00423 goto fail;
00424 while (avio_tell(in) < *moof_size) {
00425 uint32_t len = avio_rb32(in);
00426 uint32_t tag = avio_rl32(in);
00427 int64_t end = avio_tell(in) + len - 8;
00428 if (len < 8 || len >= *moof_size)
00429 goto fail;
00430 if (tag == MKTAG('u','u','i','d')) {
00431 const uint8_t tfxd[] = {
00432 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
00433 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
00434 };
00435 uint8_t uuid[16];
00436 avio_read(in, uuid, 16);
00437 if (!memcmp(uuid, tfxd, 16) && len >= 8 + 16 + 4 + 16) {
00438 avio_seek(in, 4, SEEK_CUR);
00439 *start_ts = avio_rb64(in);
00440 *duration = avio_rb64(in);
00441 ret = 0;
00442 break;
00443 }
00444 }
00445 avio_seek(in, end, SEEK_SET);
00446 }
00447 fail:
00448 avio_close(in);
00449 return ret;
00450 }
00451
00452 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)
00453 {
00454 Fragment *frag;
00455 if (os->nb_fragments >= os->fragments_size) {
00456 os->fragments_size = (os->fragments_size + 1) * 2;
00457 os->fragments = av_realloc(os->fragments, sizeof(*os->fragments)*os->fragments_size);
00458 if (!os->fragments)
00459 return AVERROR(ENOMEM);
00460 }
00461 frag = av_mallocz(sizeof(*frag));
00462 if (!frag)
00463 return AVERROR(ENOMEM);
00464 av_strlcpy(frag->file, file, sizeof(frag->file));
00465 av_strlcpy(frag->infofile, infofile, sizeof(frag->infofile));
00466 frag->start_time = start_time;
00467 frag->duration = duration;
00468 frag->start_pos = start_pos;
00469 frag->size = size;
00470 frag->n = os->fragment_index;
00471 os->fragments[os->nb_fragments++] = frag;
00472 os->fragment_index++;
00473 return 0;
00474 }
00475
00476 static int copy_moof(AVFormatContext *s, const char* infile, const char *outfile, int64_t size)
00477 {
00478 AVIOContext *in, *out;
00479 int ret = 0;
00480 if ((ret = avio_open2(&in, infile, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
00481 return ret;
00482 if ((ret = avio_open2(&out, outfile, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL)) < 0) {
00483 avio_close(in);
00484 return ret;
00485 }
00486 while (size > 0) {
00487 uint8_t buf[8192];
00488 int n = FFMIN(size, sizeof(buf));
00489 n = avio_read(in, buf, n);
00490 if (n <= 0) {
00491 ret = AVERROR(EIO);
00492 break;
00493 }
00494 avio_write(out, buf, n);
00495 size -= n;
00496 }
00497 avio_flush(out);
00498 avio_close(out);
00499 avio_close(in);
00500 return ret;
00501 }
00502
00503 static int ism_flush(AVFormatContext *s, int final)
00504 {
00505 SmoothStreamingContext *c = s->priv_data;
00506 int i, ret = 0;
00507
00508 for (i = 0; i < s->nb_streams; i++) {
00509 OutputStream *os = &c->streams[i];
00510 char filename[1024], target_filename[1024], header_filename[1024];
00511 int64_t start_pos = os->tail_pos, size;
00512 int64_t start_ts, duration, moof_size;
00513 if (!os->packets_written)
00514 continue;
00515
00516 snprintf(filename, sizeof(filename), "%s/temp", os->dirname);
00517 ret = ffurl_open(&os->out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
00518 if (ret < 0)
00519 break;
00520 os->cur_start_pos = os->tail_pos;
00521 av_write_frame(os->ctx, NULL);
00522 avio_flush(os->ctx->pb);
00523 os->packets_written = 0;
00524 if (!os->out || os->tail_out)
00525 return AVERROR(EIO);
00526
00527 ffurl_close(os->out);
00528 os->out = NULL;
00529 size = os->tail_pos - start_pos;
00530 if ((ret = parse_fragment(s, filename, &start_ts, &duration, &moof_size, size)) < 0)
00531 break;
00532 snprintf(header_filename, sizeof(header_filename), "%s/FragmentInfo(%s=%"PRIu64")", os->dirname, os->stream_type_tag, start_ts);
00533 snprintf(target_filename, sizeof(target_filename), "%s/Fragments(%s=%"PRIu64")", os->dirname, os->stream_type_tag, start_ts);
00534 copy_moof(s, filename, header_filename, moof_size);
00535 rename(filename, target_filename);
00536 add_fragment(os, target_filename, header_filename, start_ts, duration, start_pos, size);
00537 }
00538
00539 if (c->window_size || (final && c->remove_at_exit)) {
00540 for (i = 0; i < s->nb_streams; i++) {
00541 OutputStream *os = &c->streams[i];
00542 int j;
00543 int remove = os->nb_fragments - c->window_size - c->extra_window_size - c->lookahead_count;
00544 if (final && c->remove_at_exit)
00545 remove = os->nb_fragments;
00546 if (remove > 0) {
00547 for (j = 0; j < remove; j++) {
00548 unlink(os->fragments[j]->file);
00549 unlink(os->fragments[j]->infofile);
00550 av_free(os->fragments[j]);
00551 }
00552 os->nb_fragments -= remove;
00553 memmove(os->fragments, os->fragments + remove, os->nb_fragments * sizeof(*os->fragments));
00554 }
00555 if (final && c->remove_at_exit)
00556 rmdir(os->dirname);
00557 }
00558 }
00559
00560 if (ret >= 0)
00561 ret = write_manifest(s, final);
00562 return ret;
00563 }
00564
00565 static int ism_write_packet(AVFormatContext *s, AVPacket *pkt)
00566 {
00567 SmoothStreamingContext *c = s->priv_data;
00568 AVStream *st = s->streams[pkt->stream_index];
00569 OutputStream *os = &c->streams[pkt->stream_index];
00570 int64_t end_dts = (c->nb_fragments + 1LL) * c->min_frag_duration;
00571 int ret;
00572
00573 if (st->first_dts == AV_NOPTS_VALUE)
00574 st->first_dts = pkt->dts;
00575
00576 if ((!c->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
00577 av_compare_ts(pkt->dts - st->first_dts, st->time_base,
00578 end_dts, AV_TIME_BASE_Q) >= 0 &&
00579 pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
00580
00581 if ((ret = ism_flush(s, 0)) < 0)
00582 return ret;
00583 c->nb_fragments++;
00584 }
00585
00586 os->packets_written++;
00587 return ff_write_chained(os->ctx, 0, pkt, s);
00588 }
00589
00590 static int ism_write_trailer(AVFormatContext *s)
00591 {
00592 SmoothStreamingContext *c = s->priv_data;
00593 ism_flush(s, 1);
00594
00595 if (c->remove_at_exit) {
00596 char filename[1024];
00597 snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
00598 unlink(filename);
00599 rmdir(s->filename);
00600 }
00601
00602 ism_free(s);
00603 return 0;
00604 }
00605
00606 #define OFFSET(x) offsetof(SmoothStreamingContext, x)
00607 #define E AV_OPT_FLAG_ENCODING_PARAM
00608 static const AVOption options[] = {
00609 { "window_size", "number of fragments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
00610 { "extra_window_size", "number of fragments kept outside of the manifest before removing from disk", OFFSET(extra_window_size), AV_OPT_TYPE_INT, { .i64 = 5 }, 0, INT_MAX, E },
00611 { "lookahead_count", "number of lookahead fragments", OFFSET(lookahead_count), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, E },
00612 { "min_frag_duration", "minimum fragment duration (in microseconds)", OFFSET(min_frag_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
00613 { "remove_at_exit", "remove all fragments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
00614 { NULL },
00615 };
00616
00617 static const AVClass ism_class = {
00618 .class_name = "smooth streaming muxer",
00619 .item_name = av_default_item_name,
00620 .option = options,
00621 .version = LIBAVUTIL_VERSION_INT,
00622 };
00623
00624
00625 AVOutputFormat ff_smoothstreaming_muxer = {
00626 .name = "smoothstreaming",
00627 .long_name = NULL_IF_CONFIG_SMALL("Smooth Streaming Muxer"),
00628 .priv_data_size = sizeof(SmoothStreamingContext),
00629 .audio_codec = AV_CODEC_ID_AAC,
00630 .video_codec = AV_CODEC_ID_H264,
00631 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
00632 .write_header = ism_write_header,
00633 .write_packet = ism_write_packet,
00634 .write_trailer = ism_write_trailer,
00635 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
00636 .priv_class = &ism_class,
00637 };