Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartatz committed Oct 4, 2024
1 parent 63bc476 commit af559fe
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Increase swap space
run: |
sudo swapoff -a
sudo fallocate -l '15G' '/mnt/swapfile'
sudo fallocate -l '5G' '/mnt/swapfile'
sudo mkswap '/mnt/swapfile'
sudo swapon '/mnt/swapfile'
- name: Install required dependencies
Expand Down
2 changes: 2 additions & 0 deletions src/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ const char* m3u8err_getmessage(const int code) {
return "Could not create the temporary directory";
case M3U8ERR_DOWNLOAD_COULD_NOT_MOVE_FILE:
return "Could not move file to specified location";
case M3U8ERR_DOWNLOAD_LIVESTREAM_UNSUPPORTED:
return "Downloading media streams from live-streaming playlists is not supported";
case M3U8ERR_DOWNLOAD_NO_TMPDIR:
return "Could not find a suitable directory for storing temporary files";
case M3U8ERR_EXPAND_FILENAME_FAILURE:
Expand Down
139 changes: 70 additions & 69 deletions src/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,75 +75,76 @@

#define M3U8ERR_DOWNLOAD_COULD_NOT_CREATE_TMPDIR -62 /* Could not create the temporary directory */
#define M3U8ERR_DOWNLOAD_COULD_NOT_MOVE_FILE -63 /* Could not move file to specified location */
#define M3U8ERR_DOWNLOAD_NO_TMPDIR -64 /* Could not find a suitable directory for storing temporary files */

#define M3U8ERR_EXPAND_FILENAME_FAILURE -65 /* Could not resolve filename */

#define M3U8ERR_FFMPEG_COMMAND_NOT_FOUND -66 /* Could locate the FFmpeg executable */
#define M3U8ERR_FFMPEG_MUXING_FAILURE -67 /* Could not mux media streams */

#define M3U8ERR_FSTREAM_LOCK_FAILURE -68 /* Could not lock file */
#define M3U8ERR_FSTREAM_OPEN_FAILURE -69 /* Could not open file */
#define M3U8ERR_FSTREAM_READ_EMPTY_FILE -70 /* Tried to read contents from an empty file */
#define M3U8ERR_FSTREAM_READ_FAILURE -71 /* Could not read data from file */
#define M3U8ERR_FSTREAM_SEEK_FAILURE -72 /* Could not seek file */
#define M3U8ERR_FSTREAM_TELL_FAILURE -73 /* Could not get current file position */
#define M3U8ERR_FSTREAM_WRITE_FAILURE -74 /* Could not write data to file */

#define M3U8ERR_GET_APP_FILENAME_FAILURE -75 /* Could not get app filename */

#define M3U8ERR_ITEM_EMPTY -76 /* This M3U8 tag must not contain empty items */
#define M3U8ERR_ITEM_INVALID_BRANGE -77 /* The value of this M3U8 item is not a valid byte range */
#define M3U8ERR_ITEM_INVALID_DTIME -78 /* The value of this M3U8 item is not a valid ISO/IEC 8601:2004 date/time representation */
#define M3U8ERR_ITEM_INVALID_ESTRING -79 /* The value of this M3U8 item is not a valid enumerated string */
#define M3U8ERR_ITEM_INVALID_UFLOAT -80 /* The value of this M3U8 attribute is not a valid decimal floating point number */
#define M3U8ERR_ITEM_INVALID_UINT -81 /* The value of this M3U8 attribute is not a valid decimal integer */
#define M3U8ERR_ITEM_INVALID_USTRING -82 /* The value of this M3U8 item is not a valid unquoted string */
#define M3U8ERR_ITEM_MISSING -83 /* This M3U8 tag is missing a required M3U8 item */
#define M3U8ERR_ITEM_VALUE_INVALID -84 /* The value of this M3U8 item is invalid or was not recognized */
#define M3U8ERR_ITEM_VALUE_TOO_LONG -85 /* The value of this M3U8 item is too long */

#define M3U8ERR_LOAD_UNSUPPORTED_URI -86 /* Could not load M3U8 playlist from this URI; either this protocol is not supported or it was not recognized */

#define M3U8ERR_MEDIA_NO_MATCHING_AUDIO -87 /* Could not find any audio stream matching this variant stream */
#define M3U8ERR_MEDIA_NO_MATCHING_CLOSED_CAPTIONS -88 /* Could not find any closed-captions stream matching this variant stream */
#define M3U8ERR_MEDIA_NO_MATCHING_SUBTITLES -89 /* Could not find any subtitle stream matching this variant stream */
#define M3U8ERR_MEDIA_NO_MATCHING_VIDEO -90 /* Could not find any video stream matching this variant stream */
#define M3U8ERR_MEDIA_PLAYLIST_NO_SEGMENTS -91 /* This M3U8 playlist has no media segments */
#define M3U8ERR_MEDIA_UNEXPECTED_CC -92 /* This Variant Stream must not have a CLOSED-CAPTIONS attribute whose value is anything other than NONE */

#define M3U8ERR_MEMORY_ALLOCATE_FAILURE -93 /* Could not allocate memory */

#define M3U8ERR_PARSER_INVALID_BRANGE -94 /* Could not parse this string as a byte range */
#define M3U8ERR_PARSER_INVALID_DTIME -95 /* Could not parse this string as an ISO/IEC 8601:2004 date/time representation */
#define M3U8ERR_PARSER_INVALID_ESTRING -96 /* Could not parse this string as an enumerated string */
#define M3U8ERR_PARSER_INVALID_FLOAT -97 /* Could not parse this string as a signed decimal floating point number */
#define M3U8ERR_PARSER_INVALID_HEXSEQ -98 /* Could not parse this string as an hexadecimal sequence */
#define M3U8ERR_PARSER_INVALID_QSTRING -99 /* Could not parse this string as a quoted string */
#define M3U8ERR_PARSER_INVALID_RESOLUTION -100 /* Could not parse this string as a decimal resolution */
#define M3U8ERR_PARSER_INVALID_UFLOAT -101 /* Could not parse this string as a decimal floating point number */
#define M3U8ERR_PARSER_INVALID_UINT -102 /* Could not parse this string as a decimal integer */
#define M3U8ERR_PARSER_INVALID_USTRING -103 /* Could not parse this string as an unquoted string */

#define M3U8ERR_PLAYLIST_LINE_TOO_LONG -104 /* This M3U8 playlist contains a line that is too long */
#define M3U8ERR_PLAYLIST_LINE_UNTERMINATED -105 /* This M3U8 playlist contains a line that was not terminated */
#define M3U8ERR_PLAYLIST_MISSING_TAG -106 /* This M3U8 playlist is missing a required M3U8 tag */
#define M3U8ERR_PLAYLIST_TOO_LARGE -107 /* This M3U8 playlist exceeds the maximum allowed size */
#define M3U8ERR_PLAYLIST_UNEXPECTED_ITEM -108 /* Encountered an unexpected item on the first line of the playlist */
#define M3U8ERR_PLAYLIST_UNEXPECTED_TAG -109 /* Encountered an unexpected playlist tag */
#define M3U8ERR_PLAYLIST_UNEXPECTED_URI -110 /* This M3U8 tag does not expect any URI to be supplied, but a URI was encountered */
#define M3U8ERR_PLAYLIST_UNKNOWN_TYPE -111 /* Could not determine the type of this M3U8 playlist */
#define M3U8ERR_PLAYLIST_WRONG_TAG_POSITION -112 /* This M3U8 playlist contains a M3U8 tag that is not in the expected position */

#define M3U8ERR_PRINTF_WRITE_FAILURE -113 /* Could not format string using snprintf */

#define M3U8ERR_TAG_DUPLICATE -114 /* This M3U8 tag cannot be specified multiple times in the same playlist */
#define M3U8ERR_TAG_MISSING_ATTRIBUTES -115 /* This M3U8 tag requires a list of attributes to be supplied */
#define M3U8ERR_TAG_MISSING_ITEMS -116 /* This M3U8 tag requires a list of items to be supplied */
#define M3U8ERR_TAG_MISSING_VALUE -117 /* This M3U8 tag requires a single-value option to be supplied */
#define M3U8ERR_TAG_NAME_INVALID -118 /* The name of this M3U8 tag is invalid or was not recognized */
#define M3U8ERR_TAG_NON_MATCHING_ATTRIBUTES -119 /* The attributes of this M3U8 tag do not match those of the other M3U8 tag with the same ID */
#define M3U8ERR_TAG_TRAILING_OPTIONS -120 /* This M3U8 tag does not require any value to be supplied, but trailing options were found */
#define M3U8ERR_DOWNLOAD_LIVESTREAM_UNSUPPORTED -64 /* Downloading media streams from live-streaming playlists is not supported */
#define M3U8ERR_DOWNLOAD_NO_TMPDIR -65 /* Could not find a suitable directory for storing temporary files */

#define M3U8ERR_EXPAND_FILENAME_FAILURE -66 /* Could not resolve filename */

#define M3U8ERR_FFMPEG_COMMAND_NOT_FOUND -67 /* Could locate the FFmpeg executable */
#define M3U8ERR_FFMPEG_MUXING_FAILURE -68 /* Could not mux media streams */

#define M3U8ERR_FSTREAM_LOCK_FAILURE -69 /* Could not lock file */
#define M3U8ERR_FSTREAM_OPEN_FAILURE -70 /* Could not open file */
#define M3U8ERR_FSTREAM_READ_EMPTY_FILE -71 /* Tried to read contents from an empty file */
#define M3U8ERR_FSTREAM_READ_FAILURE -72 /* Could not read data from file */
#define M3U8ERR_FSTREAM_SEEK_FAILURE -73 /* Could not seek file */
#define M3U8ERR_FSTREAM_TELL_FAILURE -74 /* Could not get current file position */
#define M3U8ERR_FSTREAM_WRITE_FAILURE -75 /* Could not write data to file */

#define M3U8ERR_GET_APP_FILENAME_FAILURE -76 /* Could not get app filename */

#define M3U8ERR_ITEM_EMPTY -77 /* This M3U8 tag must not contain empty items */
#define M3U8ERR_ITEM_INVALID_BRANGE -78 /* The value of this M3U8 item is not a valid byte range */
#define M3U8ERR_ITEM_INVALID_DTIME -79 /* The value of this M3U8 item is not a valid ISO/IEC 8601:2004 date/time representation */
#define M3U8ERR_ITEM_INVALID_ESTRING -80 /* The value of this M3U8 item is not a valid enumerated string */
#define M3U8ERR_ITEM_INVALID_UFLOAT -81 /* The value of this M3U8 attribute is not a valid decimal floating point number */
#define M3U8ERR_ITEM_INVALID_UINT -82 /* The value of this M3U8 attribute is not a valid decimal integer */
#define M3U8ERR_ITEM_INVALID_USTRING -83 /* The value of this M3U8 item is not a valid unquoted string */
#define M3U8ERR_ITEM_MISSING -84 /* This M3U8 tag is missing a required M3U8 item */
#define M3U8ERR_ITEM_VALUE_INVALID -85 /* The value of this M3U8 item is invalid or was not recognized */
#define M3U8ERR_ITEM_VALUE_TOO_LONG -86 /* The value of this M3U8 item is too long */

#define M3U8ERR_LOAD_UNSUPPORTED_URI -87 /* Could not load M3U8 playlist from this URI; either this protocol is not supported or it was not recognized */

#define M3U8ERR_MEDIA_NO_MATCHING_AUDIO -88 /* Could not find any audio stream matching this variant stream */
#define M3U8ERR_MEDIA_NO_MATCHING_CLOSED_CAPTIONS -89 /* Could not find any closed-captions stream matching this variant stream */
#define M3U8ERR_MEDIA_NO_MATCHING_SUBTITLES -90 /* Could not find any subtitle stream matching this variant stream */
#define M3U8ERR_MEDIA_NO_MATCHING_VIDEO -91 /* Could not find any video stream matching this variant stream */
#define M3U8ERR_MEDIA_PLAYLIST_NO_SEGMENTS -92 /* This M3U8 playlist has no media segments */
#define M3U8ERR_MEDIA_UNEXPECTED_CC -93 /* This Variant Stream must not have a CLOSED-CAPTIONS attribute whose value is anything other than NONE */

#define M3U8ERR_MEMORY_ALLOCATE_FAILURE -94 /* Could not allocate memory */

#define M3U8ERR_PARSER_INVALID_BRANGE -95 /* Could not parse this string as a byte range */
#define M3U8ERR_PARSER_INVALID_DTIME -96 /* Could not parse this string as an ISO/IEC 8601:2004 date/time representation */
#define M3U8ERR_PARSER_INVALID_ESTRING -97 /* Could not parse this string as an enumerated string */
#define M3U8ERR_PARSER_INVALID_FLOAT -98 /* Could not parse this string as a signed decimal floating point number */
#define M3U8ERR_PARSER_INVALID_HEXSEQ -99 /* Could not parse this string as an hexadecimal sequence */
#define M3U8ERR_PARSER_INVALID_QSTRING -100 /* Could not parse this string as a quoted string */
#define M3U8ERR_PARSER_INVALID_RESOLUTION -101 /* Could not parse this string as a decimal resolution */
#define M3U8ERR_PARSER_INVALID_UFLOAT -102 /* Could not parse this string as a decimal floating point number */
#define M3U8ERR_PARSER_INVALID_UINT -103 /* Could not parse this string as a decimal integer */
#define M3U8ERR_PARSER_INVALID_USTRING -104 /* Could not parse this string as an unquoted string */

#define M3U8ERR_PLAYLIST_LINE_TOO_LONG -105 /* This M3U8 playlist contains a line that is too long */
#define M3U8ERR_PLAYLIST_LINE_UNTERMINATED -106 /* This M3U8 playlist contains a line that was not terminated */
#define M3U8ERR_PLAYLIST_MISSING_TAG -107 /* This M3U8 playlist is missing a required M3U8 tag */
#define M3U8ERR_PLAYLIST_TOO_LARGE -108 /* This M3U8 playlist exceeds the maximum allowed size */
#define M3U8ERR_PLAYLIST_UNEXPECTED_ITEM -109 /* Encountered an unexpected item on the first line of the playlist */
#define M3U8ERR_PLAYLIST_UNEXPECTED_TAG -110 /* Encountered an unexpected playlist tag */
#define M3U8ERR_PLAYLIST_UNEXPECTED_URI -111 /* This M3U8 tag does not expect any URI to be supplied, but a URI was encountered */
#define M3U8ERR_PLAYLIST_UNKNOWN_TYPE -112 /* Could not determine the type of this M3U8 playlist */
#define M3U8ERR_PLAYLIST_WRONG_TAG_POSITION -113 /* This M3U8 playlist contains a M3U8 tag that is not in the expected position */

#define M3U8ERR_PRINTF_WRITE_FAILURE -114 /* Could not format string using snprintf */

#define M3U8ERR_TAG_DUPLICATE -115 /* This M3U8 tag cannot be specified multiple times in the same playlist */
#define M3U8ERR_TAG_MISSING_ATTRIBUTES -116 /* This M3U8 tag requires a list of attributes to be supplied */
#define M3U8ERR_TAG_MISSING_ITEMS -117 /* This M3U8 tag requires a list of items to be supplied */
#define M3U8ERR_TAG_MISSING_VALUE -118 /* This M3U8 tag requires a single-value option to be supplied */
#define M3U8ERR_TAG_NAME_INVALID -119 /* The name of this M3U8 tag is invalid or was not recognized */
#define M3U8ERR_TAG_NON_MATCHING_ATTRIBUTES -120 /* The attributes of this M3U8 tag do not match those of the other M3U8 tag with the same ID */
#define M3U8ERR_TAG_TRAILING_OPTIONS -121 /* This M3U8 tag does not require any value to be supplied, but trailing options were found */

const char* m3u8err_getmessage(const int code);

Expand Down
26 changes: 9 additions & 17 deletions src/m3u8.c
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ int m3u8_parse(struct M3U8Playlist* const playlist, const char* const s) {
struct M3U8Attribute* attributes = NULL;
struct M3U8Item* items = NULL;

enum M3U8TagType current = (enum M3U8TagType) 0;
enum M3U8TagType current = M3U8_TAG_TYPE_UNKNOWN;

int type = 0, err = 0;
size_t index = 0, size = 0;
Expand Down Expand Up @@ -2322,11 +2322,6 @@ int m3u8_parse(struct M3U8Playlist* const playlist, const char* const s) {
goto end;
}

/*
char attribute[size + 1];
memcpy(attribute, start, size);
attribute[size] = '\0';
*/
separator = strstr(start, "=");

if (separator == NULL) {
Expand Down Expand Up @@ -2431,17 +2426,7 @@ int m3u8_parse(struct M3U8Playlist* const playlist, const char* const s) {
if (err != M3U8ERR_SUCCESS) {
goto end;
}
/*
attribute.value = malloc(size + 1);

if (attribute.value == NULL) {
err = M3U8ERR_MEMORY_ALLOCATE_FAILURE;
goto end;
}
memcpy(attribute.value, separator, size);
attribute.value[size] = '\0';
*/
size = tag.attributes.size + sizeof(*tag.attributes.items) * 1;
attributes = realloc(tag.attributes.items, size);

Expand Down Expand Up @@ -2600,7 +2585,7 @@ int m3u8_parse(struct M3U8Playlist* const playlist, const char* const s) {
current |= tag.type;

size = playlist->tags.size + sizeof(*playlist->tags.items) * 1;
tags = (struct M3U8Tag*) realloc(playlist->tags.items, size);
tags = realloc(playlist->tags.items, size);

if (tags == NULL) {
err = M3U8ERR_MEMORY_ALLOCATE_FAILURE;
Expand Down Expand Up @@ -2664,6 +2649,10 @@ int m3u8_parse(struct M3U8Playlist* const playlist, const char* const s) {

if (err == M3U8ERR_SUCCESS) {
playlist->type = m3u8playlist_guess_type(playlist);
playlist->livestream = (
playlist->type == M3U8_PLAYLIST_TYPE_MEDIA &&
(current & M3U8_TAG_EXT_X_ENDLIST) == 0
);

if (playlist->type == M3U8_PLAYLIST_TYPE_UNKNOWN) {
err = M3U8ERR_PLAYLIST_UNKNOWN_TYPE;
Expand Down Expand Up @@ -2774,6 +2763,9 @@ void m3u8playlist_free(struct M3U8Playlist* const playlist) {
m3u8mhttpclient_free(&playlist->multi_client);
}

playlist->subresource = 0;
playlist->livestream = 0;

}

static char* uri_ensure_slashes(char* const s) {
Expand Down
3 changes: 3 additions & 0 deletions src/m3u8types.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ enum M3U8TagType {
M3U8_TAG_EXT_X_RENDITION_REPORT = 0x100000000
};

#define M3U8_TAG_TYPE_UNKNOWN ((enum M3U8TagType) 0x00000000)

enum M3U8AttributeType {
M3U8_ATTRIBUTE_METHOD = 0x00000001,
M3U8_ATTRIBUTE_URI,
Expand Down Expand Up @@ -272,6 +274,7 @@ struct M3U8Playlist {
struct M3U8BaseURI suburi;
struct M3U8HTTPClient client;
struct M3U8MultiHTTPClient multi_client;
int livestream;
int subresource;
};

Expand Down
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ int main(int argc, argv_t* argv[]) {
const struct M3U8StreamItem* item = NULL;
struct M3U8Stream* const resource = options.selected_streams.items[index];

if (resource->playlist.livestream) {
err = M3U8ERR_DOWNLOAD_LIVESTREAM_UNSUPPORTED;
goto end;
}

name = malloc(strlen(options.download_options.temporary_directory) + strlen(PATHSEP) + uintptrlen((uintptr_t) resource) + 1 + 4 + 1);

if (name == NULL) {
Expand Down

0 comments on commit af559fe

Please sign in to comment.