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