X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Flive%2Flive.ts;h=7ea0ec198e4388bd0fd9b726d315c62545218505;hb=0d8de2756fdc43be61a82a96c17d12ee52ba0143;hp=f7ccb453d02c132c46b08d5094563b2d514aa848;hpb=68e70a745b2010cd0199864a2addd60d8f99c732;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/live/live.ts b/server/tests/api/live/live.ts index f7ccb453d..7ea0ec198 100644 --- a/server/tests/api/live/live.ts +++ b/server/tests/api/live/live.ts @@ -2,39 +2,54 @@ import 'mocha' import * as chai from 'chai' -import { LiveVideo, LiveVideoCreate, User, VideoDetails, VideoPrivacy } from '@shared/models' +import { FfmpegCommand } from 'fluent-ffmpeg' +import { join } from 'path' +import { ffprobePromise, getVideoStreamFromFile } from '@server/helpers/ffprobe-utils' +import { getLiveNotificationSocket } from '@shared/extra-utils/socket/socket-io' +import { LiveVideo, LiveVideoCreate, Video, VideoDetails, VideoPrivacy, VideoState, VideoStreamingPlaylistType } from '@shared/models' +import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' import { addVideoToBlacklist, + buildServerDirectory, + checkLiveCleanup, + checkLiveSegmentHash, + checkResolutionsInMasterPlaylist, cleanupTests, createLive, - createUser, doubleFollow, flushAndRunMultipleServers, getLive, - getMyUserInformation, + getPlaylist, getVideo, + getVideoIdFromUUID, getVideosList, + killallServers, makeRawRequest, removeVideo, + reRunServer, sendRTMPStream, + sendRTMPStreamInVideo, ServerInfo, setAccessTokensToServers, setDefaultVideoChannel, + stopFfmpeg, testFfmpegStreamError, testImage, updateCustomSubConfig, updateLive, - userLogin, - waitJobs + viewVideo, + wait, + waitJobs, + waitUntilLiveEnded, + waitUntilLivePublished, + waitUntilLiveSegmentGeneration, + waitUntilLog } from '../../../../shared/extra-utils' const expect = chai.expect describe('Test live', function () { let servers: ServerInfo[] = [] - let userId: number - let userAccessToken: string - let userChannelId: number before(async function () { this.timeout(120000) @@ -55,22 +70,6 @@ describe('Test live', function () { } }) - { - const user = { username: 'user1', password: 'superpassword' } - const res = await createUser({ - url: servers[0].url, - accessToken: servers[0].accessToken, - username: user.username, - password: user.password - }) - userId = res.body.user.id - - userAccessToken = await userLogin(servers[0], user) - - const resMe = await getMyUserInformation(servers[0].url, userAccessToken) - userChannelId = (resMe.body as User).videoChannels[0].id - } - // Server 1 and server 2 follow each other await doubleFollow(servers[0], servers[1]) }) @@ -133,7 +132,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 @@ -166,8 +165,8 @@ describe('Test live', function () { expect(video.privacy.id).to.equal(VideoPrivacy.UNLISTED) expect(video.nsfw).to.be.true - await makeRawRequest(server.url + video.thumbnailPath, 200) - await makeRawRequest(server.url + video.previewPath, 200) + await makeRawRequest(server.url + video.thumbnailPath, HttpStatusCode.OK_200) + await makeRawRequest(server.url + video.previewPath, HttpStatusCode.OK_200) } }) @@ -181,7 +180,7 @@ describe('Test live', function () { }) it('Should not be able to update a live of another server', async function () { - await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, 403) + await updateLive(servers[1].url, servers[1].accessToken, liveVideoUUID, { saveReplay: false }, HttpStatusCode.FORBIDDEN_403) }) it('Should update the live', async function () { @@ -197,7 +196,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 @@ -217,8 +216,8 @@ describe('Test live', function () { it('Should have the live deleted', async function () { for (const server of servers) { - await getVideo(server.url, liveVideoUUID, 404) - await getLive(server.url, server.accessToken, liveVideoUUID, 404) + await getVideo(server.url, liveVideoUUID, HttpStatusCode.NOT_FOUND_404) + await getLive(server.url, server.accessToken, liveVideoUUID, HttpStatusCode.NOT_FOUND_404) } }) }) @@ -228,18 +227,18 @@ 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 () { const liveAttributes = { name: 'user live', - channelId: userChannelId, + channelId: servers[0].videoChannel.id, privacy: VideoPrivacy.PUBLIC, saveReplay: false } - const res = await createLive(servers[0].url, userAccessToken, liveAttributes) + const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes) const uuid = res.body.video.uuid const resLive = await getLive(servers[0].url, servers[0].accessToken, uuid) @@ -295,42 +294,400 @@ describe('Test live', function () { }) describe('Live transcoding', function () { + let liveVideoId: 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 + } + + async function testVideoResolutions (liveVideoId: string, resolutions: number[]) { + for (const server of servers) { + const resList = await getVideosList(server.url) + const videos: Video[] = resList.body.data + + expect(videos.find(v => v.uuid === liveVideoId)).to.exist + + const resVideo = await getVideo(server.url, liveVideoId) + const video: VideoDetails = resVideo.body + + expect(video.streamingPlaylists).to.have.lengthOf(1) + + const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS) + expect(hlsPlaylist).to.exist + + // Only finite files are displayed + expect(hlsPlaylist.files).to.have.lengthOf(0) + + await checkResolutionsInMasterPlaylist(hlsPlaylist.playlistUrl, resolutions) + + for (let i = 0; i < resolutions.length; i++) { + const segmentNum = 1 + const segmentName = `${i}-00000${segmentNum}.ts` + await waitUntilLiveSegmentGeneration(servers[0], video.uuid, i, segmentNum) + + 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) + } + } + } + + function updateConf (resolutions: number[]) { + return updateCustomSubConfig(servers[0].url, servers[0].accessToken, { + live: { + enabled: true, + allowReplay: true, + maxDuration: null, + transcoding: { + enabled: true, + resolutions: { + '240p': resolutions.includes(240), + '360p': resolutions.includes(360), + '480p': resolutions.includes(480), + '720p': resolutions.includes(720), + '1080p': resolutions.includes(1080), + '2160p': resolutions.includes(2160) + } + } + } + }) + } + + before(async function () { + await updateConf([]) + }) it('Should enable transcoding without additional resolutions', async function () { - // enable - // stream - // wait federation + test + this.timeout(30000) + + liveVideoId = await createLiveWrapper(false) + + const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId) + await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId) + await waitJobs(servers) + + await testVideoResolutions(liveVideoId, [ 720 ]) + await stopFfmpeg(command) }) it('Should enable transcoding with some resolutions', async function () { - // enable - // stream - // wait federation + test + this.timeout(30000) + + const resolutions = [ 240, 480 ] + await updateConf(resolutions) + liveVideoId = await createLiveWrapper(false) + + const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId) + await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId) + await waitJobs(servers) + + await testVideoResolutions(liveVideoId, resolutions) + + await stopFfmpeg(command) }) it('Should enable transcoding with some resolutions and correctly save them', async function () { - // enable - // stream - // end stream - // wait federation + test + this.timeout(120000) + + const resolutions = [ 240, 360, 720 ] + + await updateConf(resolutions) + liveVideoId = await createLiveWrapper(true) + + const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoId, 'video_short2.webm') + await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId) + await waitJobs(servers) + + await testVideoResolutions(liveVideoId, resolutions) + + await stopFfmpeg(command) + await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId) + + await waitJobs(servers) + + const bitrateLimits = { + 720: 5000 * 1000, // 60FPS + 360: 1100 * 1000, + 240: 600 * 1000 + } + + for (const server of servers) { + const resVideo = await getVideo(server.url, liveVideoId) + const video: VideoDetails = resVideo.body + + expect(video.duration).to.be.greaterThan(1) + expect(video.files).to.have.lengthOf(0) + + const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS) + await makeRawRequest(hlsPlaylist.playlistUrl, HttpStatusCode.OK_200) + await makeRawRequest(hlsPlaylist.segmentsSha256Url, HttpStatusCode.OK_200) + + expect(hlsPlaylist.files).to.have.lengthOf(resolutions.length) + + for (const resolution of resolutions) { + const file = hlsPlaylist.files.find(f => f.resolution.id === resolution) + + expect(file).to.exist + expect(file.size).to.be.greaterThan(1) + + if (resolution >= 720) { + expect(file.fps).to.be.approximately(60, 2) + } else { + expect(file.fps).to.be.approximately(30, 2) + } + + const filename = `${video.uuid}-${resolution}-fragmented.mp4` + const segmentPath = buildServerDirectory(servers[0], join('streaming-playlists', 'hls', video.uuid, filename)) + + const probe = await ffprobePromise(segmentPath) + const videoStream = await getVideoStreamFromFile(segmentPath, probe) + + expect(probe.format.bit_rate).to.be.below(bitrateLimits[videoStream.height]) + + await makeRawRequest(file.torrentUrl, HttpStatusCode.OK_200) + await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) + } + } }) it('Should correctly have cleaned up the live files', async function () { - // check files + this.timeout(30000) + + await checkLiveCleanup(servers[0], liveVideoId, [ 240, 360, 720 ]) + }) + }) + + 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 waitUntilLivePublished(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 () { - it('Should correctly send a message when the live starts', async function () { - // local - // federation + async function createLiveWrapper () { + const liveAttributes = { + name: 'live video', + channelId: servers[0].videoChannel.id, + privacy: VideoPrivacy.PUBLIC + } + + const res = await createLive(servers[0].url, servers[0].accessToken, liveAttributes) + return res.body.video.uuid + } + + it('Should correctly send a message when the live starts and ends', async function () { + this.timeout(60000) + + const localStateChanges: VideoState[] = [] + const remoteStateChanges: VideoState[] = [] + + const liveVideoUUID = await createLiveWrapper() + await waitJobs(servers) + + { + const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID) + + const localSocket = getLiveNotificationSocket(servers[0].url) + localSocket.on('state-change', data => localStateChanges.push(data.state)) + localSocket.emit('subscribe', { videoId }) + } + + { + const videoId = await getVideoIdFromUUID(servers[1].url, liveVideoUUID) + + const remoteSocket = getLiveNotificationSocket(servers[1].url) + remoteSocket.on('state-change', data => remoteStateChanges.push(data.state)) + remoteSocket.emit('subscribe', { videoId }) + } + + const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) + + for (const server of servers) { + await waitUntilLivePublished(server.url, server.accessToken, liveVideoUUID) + } + + await waitJobs(servers) + + for (const stateChanges of [ localStateChanges, remoteStateChanges ]) { + expect(stateChanges).to.have.length.at.least(1) + expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.PUBLISHED) + } + + await stopFfmpeg(command) + + for (const server of servers) { + await waitUntilLiveEnded(server.url, server.accessToken, liveVideoUUID) + } + + await waitJobs(servers) + + for (const stateChanges of [ localStateChanges, remoteStateChanges ]) { + expect(stateChanges).to.have.length.at.least(2) + expect(stateChanges[stateChanges.length - 1]).to.equal(VideoState.LIVE_ENDED) + } }) - it('Should correctly send a message when the live ends', async function () { - // local - // federation + it('Should not receive a notification after unsubscribe', async function () { + this.timeout(60000) + + const stateChanges: VideoState[] = [] + + const liveVideoUUID = await createLiveWrapper() + await waitJobs(servers) + + const videoId = await getVideoIdFromUUID(servers[0].url, liveVideoUUID) + + const socket = getLiveNotificationSocket(servers[0].url) + socket.on('state-change', data => stateChanges.push(data.state)) + socket.emit('subscribe', { videoId }) + + const command = await sendRTMPStreamInVideo(servers[0].url, servers[0].accessToken, liveVideoUUID) + await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoUUID) + await waitJobs(servers) + + expect(stateChanges).to.have.lengthOf(1) + socket.emit('unsubscribe', { videoId }) + + await stopFfmpeg(command) + await waitJobs(servers) + + expect(stateChanges).to.have.lengthOf(1) + }) + }) + + 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(120000) + + 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([ + waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoId), + waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId) + ]) + + await waitUntilLiveSegmentGeneration(servers[0], liveVideoId, 0, 2) + await waitUntilLiveSegmentGeneration(servers[0], liveVideoReplayId, 0, 2) + + await killallServers([ servers[0] ]) + await reRunServer(servers[0]) + + await wait(5000) + }) + + it('Should cleanup lives', async function () { + this.timeout(60000) + + await waitUntilLiveEnded(servers[0].url, servers[0].accessToken, liveVideoId) + }) + + it('Should save a live replay', async function () { + this.timeout(120000) + + await waitUntilLivePublished(servers[0].url, servers[0].accessToken, liveVideoReplayId) }) })