diff --git a/segmenter.c b/segmenter.c
index 895dd4c..1c7b766 100644
--- a/segmenter.c
+++ b/segmenter.c
@@ -1,5 +1,5 @@
/* $Id$
- * $HeadURL
+ * $HeadURL$
*
* Copyright (c) 2009 Chase Douglas
*
@@ -84,75 +84,111 @@ static AVStream *add_output_stream(AVFormatContext *output_format_context, AVStr
return output_stream;
}
-int write_index_file(const char index[], const char tmp_index[], const unsigned int segment_duration, const char output_prefix[], const char http_prefix[], const unsigned int first_segment, const unsigned int last_segment, const int end, const int window) {
- FILE *index_fp;
- char *write_buf;
- unsigned int i;
-
- index_fp = fopen(tmp_index, "w");
- if (!index_fp) {
+FILE * start_index_file(const char tmp_index[])
+{
+ FILE * tmp_index_fp = fopen(tmp_index, "w+b");
+ if (!tmp_index_fp)
+ {
fprintf(stderr, "Could not open temporary m3u8 index file (%s), no index file will be created\n", tmp_index);
- return -1;
+ return NULL;
+ };
+
+ return tmp_index_fp;
+}
+
+FILE * write_index_file(FILE * tmp_index_fp,
+ const double segment_duration,
+ const char output_prefix[],
+ const char http_prefix[],
+ const unsigned int segment_index)
+{
+ char write_buf[1024] = { 0 };
+
+ if (!tmp_index_fp)
+ {
+ return NULL;
}
- write_buf = malloc(sizeof(char) * 1024);
- if (!write_buf) {
- fprintf(stderr, "Could not allocate write buffer for index file, index file will be invalid\n");
- fclose(index_fp);
- return -1;
- }
-
- if (window) {
- snprintf(write_buf, 1024, "#EXTM3U\n#EXT-X-TARGETDURATION:%u\n#EXT-X-MEDIA-SEQUENCE:%u\n", segment_duration, first_segment);
- }
- else {
- snprintf(write_buf, 1024, "#EXTM3U\n#EXT-X-TARGETDURATION:%u\n", segment_duration);
- }
- if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
+ snprintf(write_buf,
+ sizeof(write_buf),
+ "#EXTINF:%u,\n%s%s-%u.ts\n",
+ (int)(segment_duration + 0.5),
+ http_prefix,
+ output_prefix,
+ segment_index);
+
+ if (fwrite(write_buf, strlen(write_buf), 1, tmp_index_fp) != 1)
+ {
+ fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");
+ fclose(tmp_index_fp);
+ return NULL;
+ }
+
+ return tmp_index_fp;
+}
+
+void close_index_file(FILE * tmp_index_fp,
+ const char index[],
+ const unsigned int target_segment_duration,
+ const unsigned int first_segment,
+ const int window)
+{
+ char write_buf[1024] = { 0 };
+ FILE * index_fp = NULL;
+
+ if (!tmp_index_fp)
+ {
+ return;
+ }
+
+ index_fp = fopen(index, "wb");
+ if (!index_fp)
+ {
+ fprintf(stderr, "Could not open m3u8 index file (%s), no index file will be created\n", index);
+ return;
+ };
+
+ if (window)
+ {
+ snprintf(write_buf,
+ sizeof(write_buf),
+ "#EXTM3U\n#EXT-X-TARGETDURATION:%u\n#EXT-X-MEDIA-SEQUENCE:%u\n",
+ target_segment_duration,
+ first_segment);
+ }
+ else
+ {
+ snprintf(write_buf,
+ sizeof(write_buf),
+ "#EXTM3U\n#EXT-X-TARGETDURATION:%u\n",
+ target_segment_duration);
+ }
+
+ if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1)
+ {
fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");
- free(write_buf);
fclose(index_fp);
- return -1;
+ return;
}
- for (i = first_segment; i <= last_segment; i++) {
- snprintf(write_buf, 1024, "#EXTINF:%u,\n%s%s-%u.ts\n", segment_duration, http_prefix, output_prefix, i);
- if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
- fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");
- free(write_buf);
- fclose(index_fp);
- return -1;
+ /* rewind the temp index file and transfer it's contents into the index file */
+ {
+ char ch;
+
+ rewind(tmp_index_fp);
+ while (fread(&ch, 1, 1, tmp_index_fp) == 1)
+ {
+ fwrite(&ch, 1, 1, index_fp);
}
}
-
- if (end) {
- snprintf(write_buf, 1024, "#EXT-X-ENDLIST\n");
- if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
- fprintf(stderr, "Could not write last file and endlist tag to m3u8 index file\n");
- free(write_buf);
- fclose(index_fp);
- return -1;
- }
+
+ snprintf(write_buf, sizeof(write_buf), "#EXT-X-ENDLIST\n");
+ if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1)
+ {
+ fprintf(stderr, "Could not write last file and endlist tag to m3u8 index file\n");
}
- free(write_buf);
fclose(index_fp);
-
- // simple filecopy for windows
- // remove and rename doesnt work well on windows and apache because of file locking
- FILE *fs,*ft;
- char ch;
- fs = fopen(tmp_index,"r");
- ft = fopen(index,"w");
-
- while( (ch=getc(fs)) != EOF) {
- putc(ch,ft);
- }
-
- fclose(fs);
- fclose(ft);
-
- return 0;
}
typedef struct SMPacketLink
@@ -293,14 +329,15 @@ int main(int argc, char **argv)
{
const char *input;
const char *output_prefix;
- double segment_duration;
+ double target_segment_duration;
char *segment_duration_check;
const char *index;
char *tmp_index;
const char *http_prefix;
long max_tsfiles = 0;
char *max_tsfiles_check;
- double prev_segment_time = 0;
+ double prev_segment_time = 0.0;
+ double segment_duration = 0.0;
unsigned int output_index = 1;
AVInputFormat *ifmt;
AVOutputFormat *ofmt;
@@ -316,16 +353,14 @@ int main(int argc, char **argv)
int kill_file = 0;
unsigned int first_segment = 1;
unsigned int last_segment = 0;
- int write_index = 1;
int decode_done = 0;
char *dot;
int ret;
int i;
int remove_file;
FILE * pid_file;
- double packetStartTime = 0.0;
- double packetDuration = 0.0;
TSMPacketList * packetQueue = createPacketList();
+ FILE * tmp_index_fp = NULL;
if (argc < 6 || argc > 8) {
fprintf(stderr, "Usage: %s