]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/videos/live.ts
Introduce server commands
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / live.ts
index 65942db0a1dda5b67ac4755ecefe2ebc1868de24..0efcc28831a9e2591364b3c547e2439797bee845 100644 (file)
@@ -1,60 +1,22 @@
-import * as ffmpeg from 'fluent-ffmpeg'
-import { LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
-import { buildAbsoluteFixturePath, wait } from '../miscs/miscs'
-import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
-import { getVideoWithToken } from './videos'
-import { omit } from 'lodash'
-
-function getLive (url: string, token: string, videoId: number | string, statusCodeExpected = 200) {
-  const path = '/api/v1/videos/live'
-
-  return makeGetRequest({
-    url,
-    token,
-    path: path + '/' + videoId,
-    statusCodeExpected
-  })
-}
-
-function updateLive (url: string, token: string, videoId: number | string, fields: LiveVideoUpdate, statusCodeExpected = 204) {
-  const path = '/api/v1/videos/live'
-
-  return makePutBodyRequest({
-    url,
-    token,
-    path: path + '/' + videoId,
-    fields,
-    statusCodeExpected
-  })
-}
-
-function createLive (url: string, token: string, fields: LiveVideoCreate, statusCodeExpected = 200) {
-  const path = '/api/v1/videos/live'
-
-  const attaches: any = {}
-  if (fields.thumbnailfile) attaches.thumbnailfile = fields.thumbnailfile
-  if (fields.previewfile) attaches.previewfile = fields.previewfile
+/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
 
-  const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
-
-  return makeUploadRequest({
-    url,
-    path,
-    token,
-    attaches,
-    fields: updatedFields,
-    statusCodeExpected
-  })
-}
+import { expect } from 'chai'
+import * as ffmpeg from 'fluent-ffmpeg'
+import { pathExists, readdir } from 'fs-extra'
+import { join } from 'path'
+import { buildAbsoluteFixturePath, wait } from '../miscs'
+import { ServerInfo } from '../server/servers'
 
-function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) {
-  const fixture = buildAbsoluteFixturePath('video_short.mp4')
+function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, fixtureName = 'video_short.mp4') {
+  const fixture = buildAbsoluteFixturePath(fixtureName)
 
   const command = ffmpeg(fixture)
   command.inputOption('-stream_loop -1')
   command.inputOption('-re')
-
-  command.outputOption('-c copy')
+  command.outputOption('-c:v libx264')
+  command.outputOption('-g 50')
+  command.outputOption('-keyint_min 2')
+  command.outputOption('-r 60')
   command.outputOption('-f flv')
 
   const rtmpUrl = rtmpBaseUrl + '/' + streamKey
@@ -63,7 +25,7 @@ function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) {
   command.on('error', err => {
     if (err?.message?.includes('Exiting normally')) return
 
-    console.error('Cannot send RTMP stream.', { err })
+    if (process.env.DEBUG) console.error(err)
   })
 
   if (process.env.DEBUG) {
@@ -75,30 +37,75 @@ function sendRTMPStream (rtmpBaseUrl: string, streamKey: string) {
   return command
 }
 
+function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
+  return new Promise<void>((res, rej) => {
+    command.on('error', err => {
+      return rej(err)
+    })
+
+    setTimeout(() => {
+      res()
+    }, successAfterMS)
+  })
+}
+
+async function testFfmpegStreamError (command: ffmpeg.FfmpegCommand, shouldHaveError: boolean) {
+  let error: Error
+
+  try {
+    await waitFfmpegUntilError(command, 35000)
+  } catch (err) {
+    error = err
+  }
+
+  await stopFfmpeg(command)
+
+  if (shouldHaveError && !error) throw new Error('Ffmpeg did not have an error')
+  if (!shouldHaveError && error) throw error
+}
+
 async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
   command.kill('SIGINT')
 
   await wait(500)
 }
 
-async function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
-  let video: VideoDetails
+async function waitUntilLivePublishedOnAllServers (servers: ServerInfo[], videoId: string) {
+  for (const server of servers) {
+    await server.liveCommand.waitUntilPublished({ videoId })
+  }
+}
 
-  do {
-    const res = await getVideoWithToken(url, token, videoId)
-    video = res.body
+async function checkLiveCleanup (server: ServerInfo, videoUUID: string, resolutions: number[] = []) {
+  const basePath = server.serversCommand.buildDirectory('streaming-playlists')
+  const hlsPath = join(basePath, 'hls', videoUUID)
 
-    await wait(500)
-  } while (video.state.id === VideoState.WAITING_FOR_LIVE)
-}
+  if (resolutions.length === 0) {
+    const result = await pathExists(hlsPath)
+    expect(result).to.be.false
+
+    return
+  }
+
+  const files = await readdir(hlsPath)
 
-// ---------------------------------------------------------------------------
+  // fragmented file and playlist per resolution + master playlist + segments sha256 json file
+  expect(files).to.have.lengthOf(resolutions.length * 2 + 2)
+
+  for (const resolution of resolutions) {
+    expect(files).to.contain(`${videoUUID}-${resolution}-fragmented.mp4`)
+    expect(files).to.contain(`${resolution}.m3u8`)
+  }
+
+  expect(files).to.contain('master.m3u8')
+  expect(files).to.contain('segments-sha256.json')
+}
 
 export {
-  getLive,
-  updateLive,
-  waitUntilLiveStarts,
-  createLive,
+  sendRTMPStream,
+  waitFfmpegUntilError,
+  testFfmpegStreamError,
   stopFfmpeg,
-  sendRTMPStream
+  waitUntilLivePublishedOnAllServers,
+  checkLiveCleanup
 }