]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/api/live/live.ts
Cleanup lives on server restart
[github/Chocobozzz/PeerTube.git] / server / tests / api / live / live.ts
index b41b5fc2edad0d06625ff7ce84b76aff8c9dea64..aa2e1318ae24bab818b8ef7d4761277b0822fc72 100644 (file)
@@ -2,22 +2,28 @@
 
 import 'mocha'
 import * as chai from 'chai'
+import { FfmpegCommand } from 'fluent-ffmpeg'
 import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io'
 import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models'
 import {
   addVideoToBlacklist,
   checkLiveCleanup,
+  checkLiveSegmentHash,
   checkResolutionsInMasterPlaylist,
+  checkSegmentHash,
   cleanupTests,
   createLive,
   doubleFollow,
   flushAndRunMultipleServers,
   getLive,
+  getPlaylist,
   getVideo,
   getVideoIdFromUUID,
   getVideosList,
+  killallServers,
   makeRawRequest,
   removeVideo,
+  reRunServer,
   sendRTMPStream,
   sendRTMPStreamInVideo,
   ServerInfo,
@@ -28,8 +34,11 @@ import {
   testImage,
   updateCustomSubConfig,
   updateLive,
+  viewVideo,
+  wait,
   waitJobs,
-  waitUntilLiveStarts
+  waitUntilLiveStarts,
+  waitUntilLog
 } from '../../../../shared/extra-utils'
 
 const expect = chai.expect
@@ -118,7 +127,7 @@ describe('Test live', function () {
         const live: LiveVideo = resLive.body
 
         if (server.url === servers[0].url) {
-          expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
+          expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
           expect(live.streamKey).to.not.be.empty
         } else {
           expect(live.rtmpUrl).to.be.null
@@ -182,7 +191,7 @@ describe('Test live', function () {
         const live: LiveVideo = res.body
 
         if (server.url === servers[0].url) {
-          expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':1936/live')
+          expect(live.rtmpUrl).to.equal('rtmp://' + server.hostname + ':' + servers[0].rtmpPort + '/live')
           expect(live.streamKey).to.not.be.empty
         } else {
           expect(live.rtmpUrl).to.be.null
@@ -213,7 +222,7 @@ describe('Test live', function () {
     let rtmpUrl: string
 
     before(function () {
-      rtmpUrl = 'rtmp://' + servers[0].hostname + ':1936'
+      rtmpUrl = 'rtmp://' + servers[0].hostname + ':' + servers[0].rtmpPort + ''
     })
 
     async function createLiveWrapper () {
@@ -313,6 +322,19 @@ describe('Test live', function () {
         expect(hlsPlaylist.files).to.have.lengthOf(0)
 
         await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions)
+
+        for (let i = 0; i < resolutions.length; i++) {
+          const segmentName = `${i}-000001.ts`
+          await waitUntilLog(servers[0], `${video.uuid}/${segmentName}`, 1, false)
+
+          const res = await getPlaylist(`${servers[0].url}/static/streaming-playlists/hls/${video.uuid}/${i}.m3u8`)
+          const subPlaylist = res.text
+
+          expect(subPlaylist).to.contain(segmentName)
+
+          const baseUrlAndPath = servers[0].url + '/static/streaming-playlists/hls'
+          await checkLiveSegmentHash(baseUrlAndPath, video.uuid, segmentName, hlsPlaylist)
+        }
       }
     }
 
@@ -419,6 +441,80 @@ describe('Test live', function () {
     })
   })
 
+  describe('Live views', function () {
+    let liveVideoId: string
+    let command: FfmpegCommand
+
+    async function countViews (expected: number) {
+      for (const server of servers) {
+        const res = await getVideo(server.url, liveVideoId)
+        const video: VideoDetails = res.body
+
+        expect(video.views).to.equal(expected)
+      }
+    }
+
+    before(async function () {
+      this.timeout(30000)
+
+      const liveAttributes = {
+        name: 'live video',
+        channelId: servers[0].videoChannel.id,
+        privacy: VideoPrivacy.PUBLIC
+      }
+
+      const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
+      liveVideoId = res.body.video.uuid
+
+      command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId)
+      await waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId)
+      await waitJobs(servers)
+    })
+
+    it('Should display no views for a live', async function () {
+      await countViews(0)
+    })
+
+    it('Should view a live twice and display 1 view', async function () {
+      this.timeout(30000)
+
+      await viewVideo(servers[0].url, liveVideoId)
+      await viewVideo(servers[0].url, liveVideoId)
+
+      await wait(7000)
+
+      await waitJobs(servers)
+
+      await countViews(1)
+    })
+
+    it('Should wait and display 0 views', async function () {
+      this.timeout(30000)
+
+      await wait(7000)
+      await waitJobs(servers)
+
+      await countViews(0)
+    })
+
+    it('Should view a live on a remote and on local and display 2 views', async function () {
+      this.timeout(30000)
+
+      await viewVideo(servers[0].url, liveVideoId)
+      await viewVideo(servers[1].url, liveVideoId)
+      await viewVideo(servers[1].url, liveVideoId)
+
+      await wait(7000)
+      await waitJobs(servers)
+
+      await countViews(2)
+    })
+
+    after(async function () {
+      await stopFfmpeg(command)
+    })
+  })
+
   describe('Live socket messages', function () {
 
     async function createLiveWrapper () {
@@ -503,6 +599,65 @@ describe('Test live', function () {
     })
   })
 
+  describe('After a server restart', function () {
+    let liveVideoId: string
+    let liveVideoReplayId: string
+
+    async function createLiveWrapper (saveReplay: boolean) {
+      const liveAttributes = {
+        name: 'live video',
+        channelId: servers[0].videoChannel.id,
+        privacy: VideoPrivacy.PUBLIC,
+        saveReplay
+      }
+
+      const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes)
+      return res.body.video.uuid
+    }
+
+    before(async function () {
+      this.timeout(60000)
+
+      liveVideoId = await createLiveWrapper(false)
+      liveVideoReplayId = await createLiveWrapper(true)
+
+      await Promise.all([
+        sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId),
+        sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoReplayId)
+      ])
+
+      await Promise.all([
+        waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoId),
+        waitUntilLiveStarts(servers[0].url, servers[0].accessToken, liveVideoReplayId)
+      ])
+
+      await killallServers([ servers[0] ])
+      await reRunServer(servers[0])
+
+      await wait(5000)
+    })
+
+    it('Should cleanup lives', async function () {
+      this.timeout(60000)
+
+      const res = await getVideo(servers[0].url, liveVideoId)
+      const video: VideoDetails = res.body
+
+      expect(video.state.id).to.equal(VideoState.LIVE_ENDED)
+    })
+
+    it('Should save a live replay', async function () {
+      this.timeout(60000)
+
+      await waitJobs(servers)
+
+      const res = await getVideo(servers[0].url, liveVideoReplayId)
+      const video: VideoDetails = res.body
+
+      expect(video.state.id).to.equal(VideoState.PUBLISHED)
+    })
+  })
+
   after(async function () {
     await cleanupTests(servers)
   })