2021-07-08 10:18:40 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
|
|
|
|
|
|
|
import { readdir } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
2022-08-17 15:25:58 +02:00
|
|
|
import { omit, wait } from '@shared/core-utils'
|
2022-05-03 11:38:07 +02:00
|
|
|
import {
|
|
|
|
HttpStatusCode,
|
|
|
|
LiveVideo,
|
|
|
|
LiveVideoCreate,
|
|
|
|
LiveVideoSession,
|
|
|
|
LiveVideoUpdate,
|
|
|
|
ResultList,
|
|
|
|
VideoCreateResult,
|
|
|
|
VideoDetails,
|
2022-10-12 16:09:02 +02:00
|
|
|
VideoPrivacy,
|
2022-05-03 11:38:07 +02:00
|
|
|
VideoState
|
|
|
|
} from '@shared/models'
|
2021-07-08 10:18:40 +02:00
|
|
|
import { unwrapBody } from '../requests'
|
2022-10-11 16:00:11 +02:00
|
|
|
import { ObjectStorageCommand, PeerTubeServer } from '../server'
|
2021-07-08 10:18:40 +02:00
|
|
|
import { AbstractCommand, OverrideCommandOptions } from '../shared'
|
|
|
|
import { sendRTMPStream, testFfmpegStreamError } from './live'
|
|
|
|
|
|
|
|
export class LiveCommand extends AbstractCommand {
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
get (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const path = '/api/v1/videos/live'
|
|
|
|
|
|
|
|
return this.getRequestBody<LiveVideo>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path: path + '/' + options.videoId,
|
2021-07-08 10:55:16 +02:00
|
|
|
implicitToken: true,
|
2021-07-08 10:18:40 +02:00
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
listSessions (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const path = `/api/v1/videos/live/${options.videoId}/sessions`
|
|
|
|
|
|
|
|
return this.getRequestBody<ResultList<LiveVideoSession>>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async findLatestSession (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const { data: sessions } = await this.listSessions(options)
|
|
|
|
|
|
|
|
return sessions[sessions.length - 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
getReplaySession (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const path = `/api/v1/videos/${options.videoId}/live-session`
|
|
|
|
|
|
|
|
return this.getRequestBody<LiveVideoSession>({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
implicitToken: true,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
update (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
fields: LiveVideoUpdate
|
|
|
|
}) {
|
|
|
|
const { videoId, fields } = options
|
|
|
|
const path = '/api/v1/videos/live'
|
|
|
|
|
|
|
|
return this.putBodyRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path: path + '/' + videoId,
|
|
|
|
fields,
|
2021-07-08 10:55:16 +02:00
|
|
|
implicitToken: true,
|
2021-07-08 10:18:40 +02:00
|
|
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
async create (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
fields: LiveVideoCreate
|
|
|
|
}) {
|
|
|
|
const { fields } = options
|
|
|
|
const path = '/api/v1/videos/live'
|
|
|
|
|
|
|
|
const attaches: any = {}
|
|
|
|
if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
|
|
|
|
if (fields.previewfile) attaches.previewfile = fields.previewfile
|
|
|
|
|
|
|
|
const body = await unwrapBody<{ video: VideoCreateResult }>(this.postUploadRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
path,
|
|
|
|
attaches,
|
2022-08-17 15:25:58 +02:00
|
|
|
fields: omit(fields, [ 'thumbnailfile', 'previewfile' ]),
|
2021-07-08 10:55:16 +02:00
|
|
|
implicitToken: true,
|
2021-07-08 10:18:40 +02:00
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
}))
|
|
|
|
|
|
|
|
return body.video
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
async quickCreate (options: OverrideCommandOptions & {
|
|
|
|
saveReplay: boolean
|
|
|
|
permanentLive: boolean
|
|
|
|
privacy?: VideoPrivacy
|
|
|
|
}) {
|
|
|
|
const { saveReplay, permanentLive, privacy } = options
|
|
|
|
|
|
|
|
const { uuid } = await this.create({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
fields: {
|
|
|
|
name: 'live',
|
|
|
|
permanentLive,
|
|
|
|
saveReplay,
|
|
|
|
channelId: this.server.store.channel.id,
|
|
|
|
privacy
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const video = await this.server.videos.getWithToken({ id: uuid })
|
|
|
|
const live = await this.get({ videoId: uuid })
|
|
|
|
|
|
|
|
return { video, live }
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-08 10:18:40 +02:00
|
|
|
async sendRTMPStreamInVideo (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
fixtureName?: string
|
2021-08-06 10:39:40 +02:00
|
|
|
copyCodecs?: boolean
|
2021-07-08 10:18:40 +02:00
|
|
|
}) {
|
2021-08-06 10:39:40 +02:00
|
|
|
const { videoId, fixtureName, copyCodecs } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
const videoLive = await this.get({ videoId })
|
2021-07-08 10:18:40 +02:00
|
|
|
|
2021-08-06 10:39:40 +02:00
|
|
|
return sendRTMPStream({ rtmpBaseUrl: videoLive.rtmpUrl, streamKey: videoLive.streamKey, fixtureName, copyCodecs })
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
async runAndTestStreamError (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
shouldHaveError: boolean
|
|
|
|
}) {
|
|
|
|
const command = await this.sendRTMPStreamInVideo(options)
|
|
|
|
|
|
|
|
return testFfmpegStreamError(command, options.shouldHaveError)
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
waitUntilPublished (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const { videoId } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
return this.waitUntilState({ videoId, state: VideoState.PUBLISHED })
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
waitUntilWaiting (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const { videoId } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
return this.waitUntilState({ videoId, state: VideoState.WAITING_FOR_LIVE })
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
waitUntilEnded (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
const { videoId } = options
|
2021-07-08 10:25:50 +02:00
|
|
|
return this.waitUntilState({ videoId, state: VideoState.LIVE_ENDED })
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2022-10-11 16:00:11 +02:00
|
|
|
async waitUntilSegmentGeneration (options: OverrideCommandOptions & {
|
|
|
|
server: PeerTubeServer
|
2021-07-08 10:18:40 +02:00
|
|
|
videoUUID: string
|
2022-06-23 10:29:43 +02:00
|
|
|
playlistNumber: number
|
2021-07-08 10:18:40 +02:00
|
|
|
segment: number
|
2022-10-11 16:00:11 +02:00
|
|
|
objectStorage: boolean
|
2021-07-08 10:18:40 +02:00
|
|
|
}) {
|
2022-10-11 16:00:11 +02:00
|
|
|
const { server, objectStorage, playlistNumber, segment, videoUUID } = options
|
2022-06-23 10:29:43 +02:00
|
|
|
|
2022-10-10 11:31:01 +02:00
|
|
|
const segmentName = `${playlistNumber}-00000${segment}.ts`
|
2022-10-11 16:00:11 +02:00
|
|
|
const baseUrl = objectStorage
|
2022-10-19 10:43:53 +02:00
|
|
|
? ObjectStorageCommand.getMockPlaylistBaseUrl() + 'hls'
|
2022-10-11 16:00:11 +02:00
|
|
|
: server.url + '/static/streaming-playlists/hls'
|
|
|
|
|
|
|
|
let error = true
|
|
|
|
|
|
|
|
while (error) {
|
|
|
|
try {
|
|
|
|
await this.getRawRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
url: `${baseUrl}/${videoUUID}/${segmentName}`,
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
|
|
|
|
2022-10-11 17:10:53 +02:00
|
|
|
const video = await server.videos.get({ id: videoUUID })
|
|
|
|
const hlsPlaylist = video.streamingPlaylists[0]
|
|
|
|
|
|
|
|
const shaBody = await server.streamingPlaylists.getSegmentSha256({ url: hlsPlaylist.segmentsSha256Url })
|
|
|
|
|
|
|
|
if (!shaBody[segmentName]) {
|
|
|
|
throw new Error('Segment SHA does not exist')
|
|
|
|
}
|
|
|
|
|
2022-10-11 16:00:11 +02:00
|
|
|
error = false
|
|
|
|
} catch {
|
|
|
|
error = true
|
|
|
|
await wait(100)
|
|
|
|
}
|
|
|
|
}
|
2022-10-10 11:31:01 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
async waitUntilReplacedByReplay (options: OverrideCommandOptions & {
|
|
|
|
videoId: number | string
|
|
|
|
}) {
|
|
|
|
let video: VideoDetails
|
|
|
|
|
|
|
|
do {
|
|
|
|
video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
|
|
|
|
|
|
|
|
await wait(500)
|
|
|
|
} while (video.isLive === true || video.state.id !== VideoState.PUBLISHED)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
getSegmentFile (options: OverrideCommandOptions & {
|
2022-06-23 10:29:43 +02:00
|
|
|
videoUUID: string
|
|
|
|
playlistNumber: number
|
|
|
|
segment: number
|
2022-10-04 10:03:17 +02:00
|
|
|
objectStorage?: boolean // default false
|
2022-06-23 10:29:43 +02:00
|
|
|
}) {
|
2022-10-04 10:03:17 +02:00
|
|
|
const { playlistNumber, segment, videoUUID, objectStorage = false } = options
|
2022-06-23 10:29:43 +02:00
|
|
|
|
|
|
|
const segmentName = `${playlistNumber}-00000${segment}.ts`
|
2022-10-04 10:03:17 +02:00
|
|
|
const baseUrl = objectStorage
|
2022-10-19 10:43:53 +02:00
|
|
|
? ObjectStorageCommand.getMockPlaylistBaseUrl()
|
2022-10-04 10:03:17 +02:00
|
|
|
: `${this.server.url}/static/streaming-playlists/hls`
|
|
|
|
|
|
|
|
const url = `${baseUrl}/${videoUUID}/${segmentName}`
|
2022-06-23 10:29:43 +02:00
|
|
|
|
|
|
|
return this.getRawRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
url,
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
getPlaylistFile (options: OverrideCommandOptions & {
|
|
|
|
videoUUID: string
|
|
|
|
playlistName: string
|
|
|
|
objectStorage?: boolean // default false
|
2021-07-08 10:18:40 +02:00
|
|
|
}) {
|
2022-10-04 10:03:17 +02:00
|
|
|
const { playlistName, videoUUID, objectStorage = false } = options
|
2021-07-08 10:18:40 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
const baseUrl = objectStorage
|
2022-10-19 10:43:53 +02:00
|
|
|
? ObjectStorageCommand.getMockPlaylistBaseUrl()
|
2022-10-04 10:03:17 +02:00
|
|
|
: `${this.server.url}/static/streaming-playlists/hls`
|
2021-07-08 10:18:40 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
const url = `${baseUrl}/${videoUUID}/${playlistName}`
|
|
|
|
|
|
|
|
return this.getRawRequest({
|
|
|
|
...options,
|
|
|
|
|
|
|
|
url,
|
|
|
|
implicitToken: false,
|
|
|
|
defaultExpectedStatus: HttpStatusCode.OK_200
|
|
|
|
})
|
2021-07-08 10:18:40 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
async countPlaylists (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoUUID: string
|
|
|
|
}) {
|
2021-07-16 09:04:35 +02:00
|
|
|
const basePath = this.server.servers.buildDirectory('streaming-playlists')
|
2021-07-08 10:18:40 +02:00
|
|
|
const hlsPath = join(basePath, 'hls', options.videoUUID)
|
|
|
|
|
|
|
|
const files = await readdir(hlsPath)
|
|
|
|
|
|
|
|
return files.filter(f => f.endsWith('.m3u8')).length
|
|
|
|
}
|
|
|
|
|
2021-07-08 10:25:50 +02:00
|
|
|
private async waitUntilState (options: OverrideCommandOptions & {
|
2021-07-08 10:18:40 +02:00
|
|
|
videoId: number | string
|
|
|
|
state: VideoState
|
|
|
|
}) {
|
|
|
|
let video: VideoDetails
|
|
|
|
|
|
|
|
do {
|
2021-07-16 09:04:35 +02:00
|
|
|
video = await this.server.videos.getWithToken({ token: options.token, id: options.videoId })
|
2021-07-08 10:18:40 +02:00
|
|
|
|
|
|
|
await wait(500)
|
|
|
|
} while (video.state.id !== options.state)
|
|
|
|
}
|
|
|
|
}
|