aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-10-30 15:09:00 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commit77e9f859c6ad75ba179dec74e5410cc651eaa49b (patch)
treeec89fe374d73d9de48cb971ad18cab7718e03223 /shared/extra-utils
parentd2345ce920a5f35cd17ca8d89456dd98e9f11a2c (diff)
downloadPeerTube-77e9f859c6ad75ba179dec74e5410cc651eaa49b.tar.gz
PeerTube-77e9f859c6ad75ba179dec74e5410cc651eaa49b.tar.zst
PeerTube-77e9f859c6ad75ba179dec74e5410cc651eaa49b.zip
Add check params live tests
Diffstat (limited to 'shared/extra-utils')
-rw-r--r--shared/extra-utils/index.ts3
-rw-r--r--shared/extra-utils/requests/requests.ts4
-rw-r--r--shared/extra-utils/videos/live.ts102
3 files changed, 106 insertions, 3 deletions
diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts
index af4d23856..d118b12d2 100644
--- a/shared/extra-utils/index.ts
+++ b/shared/extra-utils/index.ts
@@ -13,11 +13,12 @@ export * from './requests/requests'
13export * from './requests/check-api-params' 13export * from './requests/check-api-params'
14export * from './server/servers' 14export * from './server/servers'
15export * from './server/plugins' 15export * from './server/plugins'
16export * from './videos/services'
17export * from './videos/video-playlists' 16export * from './videos/video-playlists'
18export * from './users/users' 17export * from './users/users'
19export * from './users/accounts' 18export * from './users/accounts'
20export * from './moderation/abuses' 19export * from './moderation/abuses'
20export * from './videos/services'
21export * from './videos/live'
21export * from './videos/video-abuses' 22export * from './videos/video-abuses'
22export * from './videos/video-blacklist' 23export * from './videos/video-blacklist'
23export * from './videos/video-captions' 24export * from './videos/video-captions'
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts
index 0e9d67f0b..6b00871e0 100644
--- a/shared/extra-utils/requests/requests.ts
+++ b/shared/extra-utils/requests/requests.ts
@@ -63,7 +63,7 @@ function makeUploadRequest (options: {
63 path: string 63 path: string
64 token?: string 64 token?: string
65 fields: { [ fieldName: string ]: any } 65 fields: { [ fieldName: string ]: any }
66 attaches: { [ attachName: string ]: any | any[] } 66 attaches?: { [ attachName: string ]: any | any[] }
67 statusCodeExpected?: number 67 statusCodeExpected?: number
68}) { 68}) {
69 if (!options.statusCodeExpected) options.statusCodeExpected = 400 69 if (!options.statusCodeExpected) options.statusCodeExpected = 400
@@ -93,7 +93,7 @@ function makeUploadRequest (options: {
93 } 93 }
94 }) 94 })
95 95
96 Object.keys(options.attaches).forEach(attach => { 96 Object.keys(options.attaches || {}).forEach(attach => {
97 const value = options.attaches[attach] 97 const value = options.attaches[attach]
98 if (Array.isArray(value)) { 98 if (Array.isArray(value)) {
99 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) 99 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
diff --git a/shared/extra-utils/videos/live.ts b/shared/extra-utils/videos/live.ts
new file mode 100644
index 000000000..f500fdc3e
--- /dev/null
+++ b/shared/extra-utils/videos/live.ts
@@ -0,0 +1,102 @@
1import * as ffmpeg from 'fluent-ffmpeg'
2import { LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
3import { buildAbsoluteFixturePath, wait } from '../miscs/miscs'
4import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
5import { ServerInfo } from '../server/servers'
6import { getVideo, getVideoWithToken } from './videos'
7
8function 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
19function 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
31function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
32 const path = '/api/v1/videos/live'
33
34 let attaches: any = {}
35 if (fields.thumbnailfile) attaches = { thumbnailfile: fields.thumbnailfile }
36 if (fields.previewfile) attaches = { previewfile: fields.previewfile }
37
38 return makeUploadRequest({
39 url,
40 path,
41 token,
42 attaches,
43 fields,
44 statusCodeExpected
45 })
46}
47
48function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) {
49 const fixture = buildAbsoluteFixturePath('video_short.mp4')
50
51 const command = ffmpeg(fixture)
52 command.inputOption('-stream_loop -1')
53 command.inputOption('-re')
54
55 command.outputOption('-c copy')
56 command.outputOption('-f flv')
57
58 const rtmpUrl = rtmpBaseUrl + '/' + streamKey
59 command.output(rtmpUrl)
60
61 command.on('error', err => {
62 if (err?.message?.includes('Exiting normally')) return
63
64 console.error('Cannot send RTMP stream.', { err })
65 })
66
67 if (process.env.DEBUG) {
68 command.on('stderr', data => console.log(data))
69 }
70
71 command.run()
72
73 return command
74}
75
76async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
77 command.kill('SIGINT')
78
79 await wait(500)
80}
81
82async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
83 let video: VideoDetails
84
85 do {
86 const res = await getVideoWithToken(url, token, videoId)
87 video = res.body
88
89 await wait(500)
90 } while (video.state.id === VideoState.WAITING_FOR_LIVE)
91}
92
93// ---------------------------------------------------------------------------
94
95export {
96 getLive,
97 updateLive,
98 waitUntilLiveStarts,
99 createLive,
100 stopFfmpeg,
101 sendRTMPStream
102}