]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/live.ts
a391565a497acd7159acb404d406a54b75f497af
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / live.ts
1 import * as ffmpeg from 'fluent-ffmpeg'
2 import { omit } from 'lodash'
3 import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
4 import { buildAbsoluteFixturePath, wait } from '../miscs/miscs'
5 import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
6 import { getVideoWithToken } from './videos'
7
8 function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) {
9 const path = '/api/v1/videos/live'
10
11 return makeGetRequest({
12 url,
13 token,
14 path: path + '/' + videoId,
15 statusCodeExpected
16 })
17 }
18
19 function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) {
20 const path = '/api/v1/videos/live'
21
22 return makePutBodyRequest({
23 url,
24 token,
25 path: path + '/' + videoId,
26 fields,
27 statusCodeExpected
28 })
29 }
30
31 function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
32 const path = '/api/v1/videos/live'
33
34 const attaches: any = {}
35 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
36 if (fields.previewfile) attaches.previewfile = fields.previewfile
37
38 const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
39
40 return makeUploadRequest({
41 url,
42 path,
43 token,
44 attaches,
45 fields: updatedFields,
46 statusCodeExpected
47 })
48 }
49
50 async function sendRTMPStreamInVideo (url: string, token: string, videoId: number | string, onErrorCb?: Function) {
51 const res = await getLive(url, token, videoId)
52 const videoLive = res.body as LiveVideo
53
54 return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, onErrorCb)
55 }
56
57 function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, onErrorCb?: Function) {
58 const fixture = buildAbsoluteFixturePath('video_short.mp4')
59
60 const command = ffmpeg(fixture)
61 command.inputOption('-stream_loop -1')
62 command.inputOption('-re')
63
64 command.outputOption('-c copy')
65 command.outputOption('-f flv')
66
67 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
68 command.output(rtmpUrl)
69
70 command.on('error', err => {
71 if (err?.message?.includes('Exiting normally')) return
72
73 if (onErrorCb) onErrorCb(err)
74 })
75
76 if (process.env.DEBUG) {
77 command.on('stderr', data => console.log(data))
78 }
79
80 command.run()
81
82 return command
83 }
84
85 function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
86 return new Promise((res, rej) => {
87 command.on('error', err => {
88 return rej(err)
89 })
90
91 setTimeout(() => {
92 res()
93 }, successAfterMS)
94 })
95 }
96
97 async function testFfmpegStreamError (url: string, token: string, videoId: number | string, shouldHaveError: boolean) {
98 const command = await sendRTMPStreamInVideo(url, token, videoId)
99 let error: Error
100
101 try {
102 await waitFfmpegUntilError(command, 10000)
103 } catch (err) {
104 error = err
105 }
106
107 await stopFfmpeg(command)
108
109 if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error')
110 if (!shouldHaveError && error) throw error
111 }
112
113 async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
114 command.kill('SIGINT')
115
116 await wait(500)
117 }
118
119 async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
120 let video: VideoDetails
121
122 do {
123 const res = await getVideoWithToken(url, token, videoId)
124 video = res.body
125
126 await wait(500)
127 } while (video.state.id === VideoState.WAITING_FOR_LIVE)
128 }
129
130 // ---------------------------------------------------------------------------
131
132 export {
133 getLive,
134 updateLive,
135 waitUntilLiveStarts,
136 createLive,
137 testFfmpegStreamError,
138 stopFfmpeg,
139 sendRTMPStreamInVideo,
140 waitFfmpegUntilError,
141 sendRTMPStream
142 }