]>
Commit | Line | Data |
---|---|---|
68e70a74 C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
3 | import { expect } from 'chai' | |
77e9f859 | 4 | import * as ffmpeg from 'fluent-ffmpeg' |
68e70a74 | 5 | import { pathExists, readdir } from 'fs-extra' |
97969c4e | 6 | import { omit } from 'lodash' |
68e70a74 | 7 | import { join } from 'path' |
97969c4e | 8 | import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models' |
68e70a74 | 9 | import { buildAbsoluteFixturePath, buildServerDirectory, wait } from '../miscs/miscs' |
77e9f859 | 10 | import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests' |
68e70a74 | 11 | import { ServerInfo } from '../server/servers' |
af4ae64f | 12 | import { getVideoWithToken } from './videos' |
77e9f859 C |
13 | |
14 | function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) { | |
15 | const path = '/api/v1/videos/live' | |
16 | ||
17 | return makeGetRequest({ | |
18 | url, | |
19 | token, | |
20 | path: path + '/' + videoId, | |
21 | statusCodeExpected | |
22 | }) | |
23 | } | |
24 | ||
25 | function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) { | |
26 | const path = '/api/v1/videos/live' | |
27 | ||
28 | return makePutBodyRequest({ | |
29 | url, | |
30 | token, | |
31 | path: path + '/' + videoId, | |
32 | fields, | |
33 | statusCodeExpected | |
34 | }) | |
35 | } | |
36 | ||
37 | function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) { | |
38 | const path = '/api/v1/videos/live' | |
39 | ||
af4ae64f C |
40 | const attaches: any = {} |
41 | if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile | |
42 | if (fields.previewfile) attaches.previewfile = fields.previewfile | |
43 | ||
44 | const updatedFields = omit(fields, 'thumbnailfile', 'previewfile') | |
77e9f859 C |
45 | |
46 | return makeUploadRequest({ | |
47 | url, | |
48 | path, | |
49 | token, | |
50 | attaches, | |
af4ae64f | 51 | fields: updatedFields, |
77e9f859 C |
52 | statusCodeExpected |
53 | }) | |
54 | } | |
55 | ||
ca5c612b | 56 | async function sendRTMPStreamInVideo (url: string, token: string, videoId: number | string, fixtureName?: string) { |
97969c4e C |
57 | const res = await getLive(url, token, videoId) |
58 | const videoLive = res.body as LiveVideo | |
59 | ||
ca5c612b | 60 | return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName) |
97969c4e C |
61 | } |
62 | ||
ca5c612b C |
63 | function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, fixtureName = 'video_short.mp4') { |
64 | const fixture = buildAbsoluteFixturePath(fixtureName) | |
77e9f859 C |
65 | |
66 | const command = ffmpeg(fixture) | |
67 | command.inputOption('-stream_loop -1') | |
68 | command.inputOption('-re') | |
68e70a74 C |
69 | command.outputOption('-c:v libx264') |
70 | command.outputOption('-g 50') | |
71 | command.outputOption('-keyint_min 2') | |
884d2c39 | 72 | command.outputOption('-r 60') |
77e9f859 C |
73 | command.outputOption('-f flv') |
74 | ||
75 | const rtmpUrl = rtmpBaseUrl + '/' + streamKey | |
76 | command.output(rtmpUrl) | |
77 | ||
78 | command.on('error', err => { | |
79 | if (err?.message?.includes('Exiting normally')) return | |
80 | ||
68e70a74 | 81 | if (process.env.DEBUG) console.error(err) |
77e9f859 C |
82 | }) |
83 | ||
84 | if (process.env.DEBUG) { | |
85 | command.on('stderr', data => console.log(data)) | |
86 | } | |
87 | ||
88 | command.run() | |
89 | ||
90 | return command | |
91 | } | |
92 | ||
97969c4e C |
93 | function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) { |
94 | return new Promise((res, rej) => { | |
95 | command.on('error', err => { | |
96 | return rej(err) | |
97 | }) | |
98 | ||
99 | setTimeout(() => { | |
100 | res() | |
101 | }, successAfterMS) | |
102 | }) | |
103 | } | |
104 | ||
68e70a74 | 105 | async function runAndTestFfmpegStreamError (url: string, token: string, videoId: number | string, shouldHaveError: boolean) { |
97969c4e | 106 | const command = await sendRTMPStreamInVideo(url, token, videoId) |
68e70a74 C |
107 | |
108 | return testFfmpegStreamError(command, shouldHaveError) | |
109 | } | |
110 | ||
111 | async function testFfmpegStreamError (command: ffmpeg.FfmpegCommand, shouldHaveError: boolean) { | |
97969c4e C |
112 | let error: Error |
113 | ||
114 | try { | |
2a9562fc | 115 | await waitFfmpegUntilError(command, 15000) |
97969c4e C |
116 | } catch (err) { |
117 | error = err | |
118 | } | |
119 | ||
120 | await stopFfmpeg(command) | |
121 | ||
122 | if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error') | |
123 | if (!shouldHaveError && error) throw error | |
124 | } | |
125 | ||
77e9f859 C |
126 | async function stopFfmpeg (command: ffmpeg.FfmpegCommand) { |
127 | command.kill('SIGINT') | |
128 | ||
129 | await wait(500) | |
130 | } | |
131 | ||
6b67897e C |
132 | function waitUntilLiveStarts (url: string, token: string, videoId: number | string) { |
133 | return waitWhileLiveState(url, token, videoId, VideoState.WAITING_FOR_LIVE) | |
134 | } | |
135 | ||
136 | function waitUntilLivePublished (url: string, token: string, videoId: number | string) { | |
137 | return waitWhileLiveState(url, token, videoId, VideoState.PUBLISHED) | |
138 | } | |
139 | ||
140 | async function waitWhileLiveState (url: string, token: string, videoId: number | string, state: VideoState) { | |
77e9f859 C |
141 | let video: VideoDetails |
142 | ||
143 | do { | |
144 | const res = await getVideoWithToken(url, token, videoId) | |
145 | video = res.body | |
146 | ||
147 | await wait(500) | |
6b67897e | 148 | } while (video.state.id === state) |
77e9f859 C |
149 | } |
150 | ||
68e70a74 | 151 | async function checkLiveCleanup (server: ServerInfo, videoUUID: string, resolutions: number[] = []) { |
ca5c612b | 152 | const basePath = buildServerDirectory(server, 'streaming-playlists') |
68e70a74 C |
153 | const hlsPath = join(basePath, 'hls', videoUUID) |
154 | ||
155 | if (resolutions.length === 0) { | |
156 | const result = await pathExists(hlsPath) | |
157 | expect(result).to.be.false | |
158 | ||
159 | return | |
160 | } | |
161 | ||
162 | const files = await readdir(hlsPath) | |
163 | ||
164 | // fragmented file and playlist per resolution + master playlist + segments sha256 json file | |
165 | expect(files).to.have.lengthOf(resolutions.length * 2 + 2) | |
166 | ||
167 | for (const resolution of resolutions) { | |
168 | expect(files).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`) | |
169 | expect(files).to.contain(`${resolution}.m3u8`) | |
170 | } | |
171 | ||
172 | expect(files).to.contain('master.m3u8') | |
173 | expect(files).to.contain('segments-sha256.json') | |
174 | } | |
175 | ||
77e9f859 C |
176 | // --------------------------------------------------------------------------- |
177 | ||
178 | export { | |
179 | getLive, | |
6b67897e | 180 | waitUntilLivePublished, |
77e9f859 C |
181 | updateLive, |
182 | waitUntilLiveStarts, | |
183 | createLive, | |
68e70a74 C |
184 | runAndTestFfmpegStreamError, |
185 | checkLiveCleanup, | |
77e9f859 | 186 | stopFfmpeg, |
97969c4e C |
187 | sendRTMPStreamInVideo, |
188 | waitFfmpegUntilError, | |
68e70a74 C |
189 | sendRTMPStream, |
190 | testFfmpegStreamError | |
77e9f859 | 191 | } |