aboutsummaryrefslogtreecommitdiff
path: root/src/M3U8.cpp
blob: 72421b760ae793e9b2678cddd0f15375f3c5b9e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "../include/M3U8.hpp"
#include <algorithm>

namespace QuickMedia {
    // static
    M3U8Stream M3U8Stream::get_highest_resolution_stream(const std::vector<M3U8Stream> &streams) {
        auto streams_copy = streams;
        std::sort(streams_copy.begin(), streams_copy.end(), [](const M3U8Stream &stream1, const M3U8Stream &stream2) {
            return stream1.height > stream2.height;
        });
        return streams_copy.front();
    }

    // TODO: Extract framerate
    static bool stream_metadata_from_string(const std::string &metadata_str, M3U8Stream &stream) {
        size_t index = metadata_str.find("RESOLUTION=");
        if(index == std::string::npos)
            return false;

        index += 11;

        int width = 0;
        int height = 0;
        if(sscanf(metadata_str.c_str() + index, "%dx%d", &width, &height) != 2)
            return false;

        stream.width = width;
        stream.height = height;
        return true;
    }

    static bool stream_extract_url(const std::string &m3u8_data, size_t offset, std::string &url) {
        if(offset >= m3u8_data.size())
            return false;

        if(m3u8_data[offset] == '#')
            return false;

        size_t line_end = m3u8_data.find("\n", offset);
        if(line_end == std::string::npos)
            line_end = m3u8_data.size();

        url = m3u8_data.substr(offset, line_end - offset);
        return true;
    }

    // TODO: Also check for EXT-X-I-FRAME-STREAM-INF?
    std::vector<M3U8Stream> m3u8_get_streams(const std::string &m3u8_data) {
        std::vector<M3U8Stream> streams;
        size_t index = 0;

        while(index < m3u8_data.size()) {
            index = m3u8_data.find("#EXT-X-STREAM-INF:", index);
            if(index == std::string::npos)
                break;

            index += 18;
            size_t line_end = m3u8_data.find("\n", index);
            if(line_end == std::string::npos)
                line_end = m3u8_data.size();

            std::string stream_metadata = m3u8_data.substr(index, line_end - index);
            M3U8Stream stream;
            if(stream_metadata_from_string(stream_metadata, stream) && stream_extract_url(m3u8_data, line_end + 1, stream.url)) {
                index = line_end + 1 + stream.url.size() + 1;
                streams.push_back(std::move(stream));
            } else {
                index = line_end + 1;
            }
        }

        return streams;
    }
}