live-permanent.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { wait } from '@peertube/peertube-core-utils'
  3. import { LiveVideoCreate, VideoPrivacy, VideoState, VideoStateType } from '@peertube/peertube-models'
  4. import {
  5. ConfigCommand,
  6. PeerTubeServer,
  7. cleanupTests,
  8. createMultipleServers,
  9. doubleFollow,
  10. setAccessTokensToServers,
  11. setDefaultVideoChannel,
  12. stopFfmpeg,
  13. waitJobs
  14. } from '@peertube/peertube-server-commands'
  15. import { checkLiveCleanup } from '@tests/shared/live.js'
  16. import { expect } from 'chai'
  17. describe('Permanent live', function () {
  18. let servers: PeerTubeServer[] = []
  19. let videoUUID: string
  20. async function createLiveWrapper (permanentLive: boolean) {
  21. const attributes: LiveVideoCreate = {
  22. channelId: servers[0].store.channel.id,
  23. privacy: VideoPrivacy.PUBLIC,
  24. name: 'my super live',
  25. saveReplay: false,
  26. permanentLive
  27. }
  28. const { uuid } = await servers[0].live.create({ fields: attributes })
  29. return uuid
  30. }
  31. async function checkVideoState (videoId: string, state: VideoStateType) {
  32. for (const server of servers) {
  33. const video = await server.videos.get({ id: videoId })
  34. expect(video.state.id).to.equal(state)
  35. }
  36. }
  37. before(async function () {
  38. this.timeout(120000)
  39. servers = await createMultipleServers(2)
  40. // Get the access tokens
  41. await setAccessTokensToServers(servers)
  42. await setDefaultVideoChannel(servers)
  43. // Server 1 and server 2 follow each other
  44. await doubleFollow(servers[0], servers[1])
  45. await servers[0].config.enableMinimumTranscoding()
  46. await servers[0].config.updateExistingConfig({
  47. newConfig: {
  48. live: {
  49. enabled: true,
  50. allowReplay: true,
  51. maxDuration: -1,
  52. transcoding: {
  53. enabled: true,
  54. resolutions: ConfigCommand.getCustomConfigResolutions(true)
  55. }
  56. }
  57. }
  58. })
  59. })
  60. it('Should create a non permanent live and update it to be a permanent live', async function () {
  61. this.timeout(20000)
  62. const videoUUID = await createLiveWrapper(false)
  63. {
  64. const live = await servers[0].live.get({ videoId: videoUUID })
  65. expect(live.permanentLive).to.be.false
  66. }
  67. await servers[0].live.update({ videoId: videoUUID, fields: { permanentLive: true } })
  68. {
  69. const live = await servers[0].live.get({ videoId: videoUUID })
  70. expect(live.permanentLive).to.be.true
  71. }
  72. })
  73. it('Should create a permanent live', async function () {
  74. this.timeout(20000)
  75. videoUUID = await createLiveWrapper(true)
  76. const live = await servers[0].live.get({ videoId: videoUUID })
  77. expect(live.permanentLive).to.be.true
  78. await waitJobs(servers)
  79. })
  80. it('Should stream into this permanent live', async function () {
  81. this.timeout(240_000)
  82. const beforePublication = new Date()
  83. const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID })
  84. for (const server of servers) {
  85. await server.live.waitUntilPublished({ videoId: videoUUID })
  86. }
  87. await checkVideoState(videoUUID, VideoState.PUBLISHED)
  88. for (const server of servers) {
  89. const video = await server.videos.get({ id: videoUUID })
  90. expect(new Date(video.publishedAt)).greaterThan(beforePublication)
  91. }
  92. await stopFfmpeg(ffmpegCommand)
  93. await servers[0].live.waitUntilWaiting({ videoId: videoUUID })
  94. await waitJobs(servers)
  95. })
  96. it('Should have cleaned up this live', async function () {
  97. this.timeout(40000)
  98. await wait(5000)
  99. await waitJobs(servers)
  100. for (const server of servers) {
  101. const videoDetails = await server.videos.get({ id: videoUUID })
  102. expect(videoDetails.streamingPlaylists).to.have.lengthOf(0)
  103. }
  104. await checkLiveCleanup({ server: servers[0], permanent: true, videoUUID })
  105. })
  106. it('Should have set this live to waiting for live state', async function () {
  107. this.timeout(20000)
  108. await checkVideoState(videoUUID, VideoState.WAITING_FOR_LIVE)
  109. })
  110. it('Should be able to stream again in the permanent live', async function () {
  111. this.timeout(60000)
  112. await servers[0].config.updateExistingConfig({
  113. newConfig: {
  114. live: {
  115. enabled: true,
  116. allowReplay: true,
  117. maxDuration: -1,
  118. transcoding: {
  119. enabled: true,
  120. resolutions: ConfigCommand.getCustomConfigResolutions(false)
  121. }
  122. }
  123. }
  124. })
  125. const ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: videoUUID })
  126. for (const server of servers) {
  127. await server.live.waitUntilPublished({ videoId: videoUUID })
  128. }
  129. await checkVideoState(videoUUID, VideoState.PUBLISHED)
  130. const count = await servers[0].live.countPlaylists({ videoUUID })
  131. // master playlist and 720p playlist
  132. expect(count).to.equal(2)
  133. await stopFfmpeg(ffmpegCommand)
  134. })
  135. it('Should have appropriate sessions', async function () {
  136. this.timeout(60000)
  137. await servers[0].live.waitUntilWaiting({ videoId: videoUUID })
  138. const { data, total } = await servers[0].live.listSessions({ videoId: videoUUID })
  139. expect(total).to.equal(2)
  140. expect(data).to.have.lengthOf(2)
  141. for (const session of data) {
  142. expect(session.startDate).to.exist
  143. expect(session.endDate).to.exist
  144. expect(session.error).to.not.exist
  145. }
  146. })
  147. it('Should remove the live and have cleaned up the directory', async function () {
  148. this.timeout(60000)
  149. await servers[0].videos.remove({ id: videoUUID })
  150. await waitJobs(servers)
  151. await checkLiveCleanup({ server: servers[0], permanent: true, videoUUID })
  152. })
  153. after(async function () {
  154. await cleanupTests(servers)
  155. })
  156. })