static void hls_handler(void* param, const void* data, size_t bytes, int64_t pts, int64_t /*dts*/, int64_t duration) { hls_playlist_t* playlist = (hls_playlist_t*)param; int discontinue = 0; if (playlist->i > 0) { discontinue = (playlist->last_pts + HLS_DURATION * 1000 < pts/*discontinue*/ || pts + duration + HLS_DURATION * 1000 < playlist->pts/*rewind*/) ? 1 : 0; } playlist->pts = pts; playlist->last_pts = pts + duration; char name[128] = { 0 }; snprintf(name, sizeof(name), "%s/%d.ts", playlist->file.c_str(), playlist->i++); hls_m3u8_add(playlist->m3u8, name, pts, duration, discontinue); // add new segment hls_ts_t ts; ts.name = name; ts.size = bytes; ts.data = malloc(bytes); memcpy(ts.data, data, bytes); playlist->files.push_back(ts); // remove oldest segment while(playlist->files.size() > HLS_LIVE_NUM + 1) { hls_ts_t ts = playlist->files.front(); free(ts.data); playlist->files.pop_front(); } printf("new segment: %s\n", name); }
static int hls_handler(void* m3u8, const void* data, size_t bytes, int64_t pts, int64_t dts, int64_t duration) { static int64_t s_dts = -1; int discontinue = -1 != s_dts ? 0 : (dts > s_dts + HLS_DURATION / 2 ? 1 : 0); s_dts = dts; static int i = 0; char name[128] = {0}; snprintf(name, sizeof(name), "%d.ts", i++); hls_m3u8_add((hls_m3u8_t*)m3u8, name, pts, duration, discontinue); FILE* fp = fopen(name, "wb"); fwrite(data, 1, bytes, fp); fclose(fp); return 0; }