]> 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 522beb8bc6b1925f95709f06c75d37462709b048..0efcc28831a9e2591364b3c547e2439797bee845 100644 (file)
@@ -3,62 +3,9 @@
 import { expect } from 'chai'
 import * as ffmpeg from 'fluent-ffmpeg'
 import { pathExists, readdir } from 'fs-extra'
-import { omit } from 'lodash'
 import { join } from 'path'
-import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, VideoDetails, VideoState } from '@shared/models'
-import { buildAbsoluteFixturePath, buildServerDirectory, wait } from '../miscs/miscs'
-import { makeGetRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
+import { buildAbsoluteFixturePath, wait } from '../miscs'
 import { ServerInfo } from '../server/servers'
-import { getVideoWithToken } from './videos'
-
-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
-
-  const updatedFields = omit(fields, 'thumbnailfile', 'previewfile')
-
-  return makeUploadRequest({
-    url,
-    path,
-    token,
-    attaches,
-    fields: updatedFields,
-    statusCodeExpected
-  })
-}
-
-async function sendRTMPStreamInVideo (url: string, token: string, videoId: number | string, fixtureName?: string) {
-  const res = await getLive(url, token, videoId)
-  const videoLive = res.body as LiveVideo
-
-  return sendRTMPStream(videoLive.rtmpUrl, videoLive.streamKey, fixtureName)
-}
 
 function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, fixtureName = 'video_short.mp4') {
   const fixture = buildAbsoluteFixturePath(fixtureName)
@@ -91,7 +38,7 @@ function sendRTMPStream (rtmpBaseUrl: string, streamKey: string, fixtureName = '
 }
 
 function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
-  return new Promise((res, rej) => {
+  return new Promise<void>((res, rej) => {
     command.on('error', err => {
       return rej(err)
     })
@@ -102,17 +49,11 @@ function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 1
   })
 }
 
-async function runAndTestFfmpegStreamError (url: string, token: string, videoId: number | string, shouldHaveError: boolean) {
-  const command = await sendRTMPStreamInVideo(url, token, videoId)
-
-  return testFfmpegStreamError(command, shouldHaveError)
-}
-
 async function testFfmpegStreamError (command: ffmpeg.FfmpegCommand, shouldHaveError: boolean) {
   let error: Error
 
   try {
-    await waitFfmpegUntilError(command, 15000)
+    await waitFfmpegUntilError(command, 35000)
   } catch (err) {
     error = err
   }
@@ -129,31 +70,14 @@ async function stopFfmpeg (command: ffmpeg.FfmpegCommand) {
   await wait(500)
 }
 
-function waitUntilLiveStarts (url: string, token: string, videoId: number | string) {
-  return waitWhileLiveState(url, token, videoId, VideoState.WAITING_FOR_LIVE)
-}
-
-function waitUntilLivePublished (url: string, token: string, videoId: number | string) {
-  return waitWhileLiveState(url, token, videoId, VideoState.PUBLISHED)
-}
-
-function waitUntilLiveEnded (url: string, token: string, videoId: number | string) {
-  return waitWhileLiveState(url, token, videoId, VideoState.LIVE_ENDED)
-}
-
-async function waitWhileLiveState (url: string, token: string, videoId: number | string, state: VideoState) {
-  let video: VideoDetails
-
-  do {
-    const res = await getVideoWithToken(url, token, videoId)
-    video = res.body
-
-    await wait(500)
-  } while (video.state.id === state)
+async function waitUntilLivePublishedOnAllServers (servers: ServerInfo[], videoId: string) {
+  for (const server of servers) {
+    await server.liveCommand.waitUntilPublished({ videoId })
+  }
 }
 
 async function checkLiveCleanup (server: ServerInfo, videoUUID: string, resolutions: number[] = []) {
-  const basePath = buildServerDirectory(server, 'streaming-playlists')
+  const basePath = server.serversCommand.buildDirectory('streaming-playlists')
   const hlsPath = join(basePath, 'hls', videoUUID)
 
   if (resolutions.length === 0) {
@@ -177,30 +101,11 @@ async function checkLiveCleanup (server: ServerInfo, videoUUID: string, resoluti
   expect(files).to.contain('segments-sha256.json')
 }
 
-async function getPlaylistsCount (server: ServerInfo, videoUUID: string) {
-  const basePath = buildServerDirectory(server, 'streaming-playlists')
-  const hlsPath = join(basePath, 'hls', videoUUID)
-
-  const files = await readdir(hlsPath)
-
-  return files.filter(f => f.endsWith('.m3u8')).length
-}
-
-// ---------------------------------------------------------------------------
-
 export {
-  getLive,
-  getPlaylistsCount,
-  waitUntilLivePublished,
-  updateLive,
-  waitUntilLiveStarts,
-  createLive,
-  runAndTestFfmpegStreamError,
-  checkLiveCleanup,
-  stopFfmpeg,
-  sendRTMPStreamInVideo,
-  waitUntilLiveEnded,
-  waitFfmpegUntilError,
   sendRTMPStream,
-  testFfmpegStreamError
+  waitFfmpegUntilError,
+  testFfmpegStreamError,
+  stopFfmpeg,
+  waitUntilLivePublishedOnAllServers,
+  checkLiveCleanup
 }