-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f39648
commit f296e51
Showing
6 changed files
with
133 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { ChannelDB } from '@/db/ChannelDB.ts'; | ||
import { StreamLineupItem } from '@/db/derived_types/StreamLineup.ts'; | ||
import { StreamProgramCalculator } from '@/stream/StreamProgramCalculator.ts'; | ||
import dayjs from '@/util/dayjs.ts'; | ||
import NodeCache from 'node-cache'; | ||
import util from 'node:util'; | ||
|
||
const playlistFmtString = ```#EXTM3U | ||
#EXT-X-VERSION:3 | ||
#EXT-X-TARGETDURATION:10 | ||
#EXT-X-MEDIA-SEQUENCE:%d | ||
#EXT-X-DISCONTINUITY | ||
#EXTINF:%d, | ||
%s://%s/ffmpeg/stream/%s?index=%d%s | ||
```; | ||
|
||
type CachedCurrentIndex = { | ||
startTime: number; | ||
index: number; | ||
}; | ||
|
||
export class HlsPlaylistCreator { | ||
private static cache = new NodeCache({ | ||
stdTTL: +dayjs.duration({ days: 1 }), | ||
}); | ||
|
||
constructor( | ||
private channelDB: ChannelDB, | ||
private streamProgramCalculator: StreamProgramCalculator, | ||
) {} | ||
|
||
async createPlaylist(channelId: string, now: dayjs.Dayjs) { | ||
const lineupItemResult = | ||
await this.streamProgramCalculator.getCurrentLineupItem({ | ||
channelId, | ||
startTime: +now, | ||
allowSkip: false, | ||
}); | ||
|
||
if (lineupItemResult.isFailure()) { | ||
throw lineupItemResult.error; | ||
} | ||
|
||
const { lineupItem } = lineupItemResult.get(); | ||
|
||
const currentIndex = this.getLineupItemIndex(channelId, lineupItem); | ||
|
||
return util.format(playlistFmtString, currentIndex); | ||
} | ||
|
||
private getLineupItemIndex(channelId: string, lineupItem: StreamLineupItem) { | ||
const cachedValue = | ||
HlsPlaylistCreator.cache.get<CachedCurrentIndex>(channelId); | ||
if (cachedValue && cachedValue.startTime === lineupItem.programBeginMs) { | ||
return cachedValue.index; | ||
} else if (cachedValue) { | ||
HlsPlaylistCreator.cache.set<CachedCurrentIndex>(channelId, { | ||
startTime: lineupItem.programBeginMs, | ||
index: cachedValue.index + 1, | ||
}); | ||
return cachedValue.index + 1; | ||
} else { | ||
HlsPlaylistCreator.cache.set<CachedCurrentIndex>(channelId, { | ||
startTime: lineupItem.programBeginMs, | ||
index: 1, | ||
}); | ||
return 1; | ||
} | ||
} | ||
} |
Oops, something went wrong.