]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/server-commands/videos/live-command.ts
Merge branch 'release/4.3.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / server-commands / videos / live-command.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { readdir } from 'fs-extra'
4 import { join } from 'path'
5 import { omit, wait } from '@shared/core-utils'
6 import {
7 HttpStatusCode,
8 LiveVideo,
9 LiveVideoCreate,
10 LiveVideoSession,
11 LiveVideoUpdate,
12 ResultList,
13 VideoCreateResult,
14 VideoDetails,
15 VideoState
16 } from '@shared/models'
17 import { unwrapBody } from '../requests'
18 import { AbstractCommand, OverrideCommandOptions } from '../shared'
19 import { sendRTMPStream, testFfmpegStreamError } from './live'
20
21 export class LiveCommand extends AbstractCommand {
22
23 get (options: OverrideCommandOptions & {
24 videoId: number | string
25 }) {
26 const path = '/api/v1/videos/live'
27
28 return this.getRequestBody<LiveVideo>({
29 ...options,
30
31 path: path + '/' + options.videoId,
32 implicitToken: true,
33 defaultExpectedStatus: HttpStatusCode.OK_200
34 })
35 }
36
37 listSessions (options: OverrideCommandOptions & {
38 videoId: number | string
39 }) {
40 const path = `/api/v1/videos/live/${options.videoId}/sessions`
41
42 return this.getRequestBody<ResultList<LiveVideoSession>>({
43 ...options,
44
45 path,
46 implicitToken: true,
47 defaultExpectedStatus: HttpStatusCode.OK_200
48 })
49 }
50
51 async findLatestSession (options: OverrideCommandOptions & {
52 videoId: number | string
53 }) {
54 const { data: sessions } = await this.listSessions(options)
55
56 return sessions[sessions.length - 1]
57 }
58
59 getReplaySession (options: OverrideCommandOptions & {
60 videoId: number | string
61 }) {
62 const path = `/api/v1/videos/${options.videoId}/live-session`
63
64 return this.getRequestBody<LiveVideoSession>({
65 ...options,
66
67 path,
68 implicitToken: true,
69 defaultExpectedStatus: HttpStatusCode.OK_200
70 })
71 }
72
73 update (options: OverrideCommandOptions & {
74 videoId: number | string
75 fields: LiveVideoUpdate
76 }) {
77 const { videoId, fields } = options
78 const path = '/api/v1/videos/live'
79
80 return this.putBodyRequest({
81 ...options,
82
83 path: path + '/' + videoId,
84 fields,
85 implicitToken: true,
86 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
87 })
88 }
89
90 async create (options: OverrideCommandOptions & {
91 fields: LiveVideoCreate
92 }) {
93 const { fields } = options
94 const path = '/api/v1/videos/live'
95
96 const attaches: any = {}
97 if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
98 if (fields.previewfile) attaches.previewfile = fields.previewfile
99
100 const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
101 ...options,
102
103 path,
104 attaches,
105 fields: omit(fields, [ 'thumbnailfile', 'previewfile' ]),
106 implicitToken: true,
107 defaultExpectedStatus: HttpStatusCode.OK_200
108 }))
109
110 return body.video
111 }
112
113 async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
114 videoId: number | string
115 fixtureName?: string
116 copyCodecs?: boolean
117 }) {
118 const { videoId, fixtureName, copyCodecs } = options
119 const videoLive = await this.get({ videoId })
120
121 return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs })
122 }
123
124 async runAndTestStreamError (options: OverrideCommandOptions & {
125 videoId: number | string
126 shouldHaveError: boolean
127 }) {
128 const command = await this.sendRTMPStreamInVideo(options)
129
130 return testFfmpegStreamError(command, options.shouldHaveError)
131 }
132
133 waitUntilPublished (options: OverrideCommandOptions & {
134 videoId: number | string
135 }) {
136 const { videoId } = options
137 return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
138 }
139
140 waitUntilWaiting (options: OverrideCommandOptions & {
141 videoId: number | string
142 }) {
143 const { videoId } = options
144 return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
145 }
146
147 waitUntilEnded (options: OverrideCommandOptions & {
148 videoId: number | string
149 }) {
150 const { videoId } = options
151 return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
152 }
153
154 waitUntilSegmentGeneration (options: OverrideCommandOptions & {
155 videoUUID: string
156 playlistNumber: number
157 segment: number
158 totalSessions?: number
159 }) {
160 const { playlistNumber, segment, videoUUID, totalSessions = 1 } = options
161 const segmentName = `${playlistNumber}-00000${segment}.ts`
162
163 return this.server.servers.waitUntilLog(`${videoUUID}/${segmentName}`, totalSessions * 2, false)
164 }
165
166 getSegment (options: OverrideCommandOptions & {
167 videoUUID: string
168 playlistNumber: number
169 segment: number
170 }) {
171 const { playlistNumber, segment, videoUUID } = options
172
173 const segmentName = `${playlistNumber}-00000${segment}.ts`
174 const url = `${this.server.url}/static/streaming-playlists/hls/${videoUUID}/${segmentName}`
175
176 return this.getRawRequest({
177 ...options,
178
179 url,
180 implicitToken: false,
181 defaultExpectedStatus: HttpStatusCode.OK_200
182 })
183 }
184
185 async waitUntilReplacedByReplay (options: OverrideCommandOptions & {
186 videoId: number | string
187 }) {
188 let video: VideoDetails
189
190 do {
191 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
192
193 await wait(500)
194 } while (video.isLive === true || video.state.id !== VideoState.PUBLISHED)
195 }
196
197 async countPlaylists (options: OverrideCommandOptions & {
198 videoUUID: string
199 }) {
200 const basePath = this.server.servers.buildDirectory('streaming-playlists')
201 const hlsPath = join(basePath, 'hls', options.videoUUID)
202
203 const files = await readdir(hlsPath)
204
205 return files.filter(f => f.endsWith('.m3u8')).length
206 }
207
208 private async waitUntilState (options: OverrideCommandOptions & {
209 videoId: number | string
210 state: VideoState
211 }) {
212 let video: VideoDetails
213
214 do {
215 video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
216
217 await wait(500)
218 } while (video.state.id !== options.state)
219 }
220 }