1 import { forkJoin } from 'rxjs'
2 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
5 import { scrollToTop } from '@app/helpers'
6 import { FormReactiveService } from '@app/shared/shared-forms'
7 import { Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
8 import { LiveVideoService } from '@app/shared/shared-video-live'
9 import { LoadingBarService } from '@ngx-loading-bar/core'
10 import { logger } from '@root-helpers/logger'
11 import { LiveVideo, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
12 import { VideoSend } from './video-send'
15 selector: 'my-video-go-live',
16 templateUrl: './video-go-live.component.html',
18 '../shared/video-edit.component.scss',
19 './video-go-live.component.scss',
23 export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
24 @Output() firstStepDone = new EventEmitter<string>()
25 @Output() firstStepError = new EventEmitter<void>()
27 firstStepPermanentLive: boolean
29 isInUpdateForm = false
30 isUpdatingVideo = false
31 isOrHasGoingLive = false
37 videoShortUUID: string
42 protected formReactiveService: FormReactiveService,
43 protected loadingBar: LoadingBarService,
44 protected notifier: Notifier,
45 protected authService: AuthService,
46 protected serverService: ServerService,
47 protected videoService: VideoService,
48 protected videoCaptionService: VideoCaptionService,
49 private liveVideoService: LiveVideoService,
50 private router: Router,
51 private hooks: HooksService
61 this.hooks.runAction('action:go-live.init', 'video-edit')
65 return { canDeactivate: true }
69 if (this.isOrHasGoingLive) return
70 this.isOrHasGoingLive = true
74 const video: LiveVideoCreate = {
76 privacy: this.highestPrivacy,
77 nsfw: this.serverConfig.instance.isNSFW,
78 waitTranscoding: true,
79 permanentLive: this.firstStepPermanentLive,
80 latencyMode: LiveVideoLatencyMode.DEFAULT,
81 saveReplay: this.isReplayAllowed(),
82 channelId: this.firstStepChannelId
85 // Go live in private mode, but correctly fill the update form with the first user choice
86 const toPatch = { ...video, privacy: this.firstStepPrivacyId }
87 this.form.patchValue(toPatch)
89 this.liveVideoService.goLive(video)
92 this.videoId = res.video.id
93 this.videoUUID = res.video.uuid
94 this.videoShortUUID = res.video.shortUUID
95 this.isInUpdateForm = true
97 this.firstStepDone.emit(name)
103 this.firstStepError.emit()
105 let message = err.message
107 const error = err.body as PeerTubeProblemDocument
109 if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
110 message = $localize`Cannot create live because this instance have too many created lives`
111 } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
112 message = $localize`Cannot create live because you created too many lives`
115 this.notifier.error(message)
120 async updateSecondStep () {
121 if (!await this.isFormValid()) return
123 this.isUpdatingVideo = true
125 const video = new VideoEdit()
126 video.patch(this.form.value)
127 video.id = this.videoId
128 video.uuid = this.videoUUID
129 video.shortUUID = this.videoShortUUID
131 const liveVideoUpdate: LiveVideoUpdate = {
132 saveReplay: this.form.value.saveReplay,
133 latencyMode: this.form.value.latencyMode,
134 permanentLive: this.form.value.permanentLive
139 this.updateVideoAndCaptions(video),
141 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
144 this.isUpdatingVideo = false
146 this.notifier.success($localize`Live published.`)
148 this.router.navigateByUrl(Video.buildWatchUrl(video))
152 this.error = err.message
159 getMaxLiveDuration () {
160 return this.serverConfig.live.maxDuration / 1000
163 getNormalLiveDescription () {
164 if (this.isReplayAllowed()) {
165 return $localize`Stream only once, replay will replace your live`
168 return $localize`Stream only once`
171 getPermanentLiveDescription () {
172 if (this.isReplayAllowed()) {
173 return $localize`Stream multiple times, replays will be separate videos`
176 return $localize`Stream multiple times using the same URL`
179 private isReplayAllowed () {
180 return this.serverConfig.live.allowReplay
183 private fetchVideoLive () {
184 this.liveVideoService.getVideoLive(this.videoId)
187 this.liveVideo = liveVideo
191 this.firstStepError.emit()
192 this.notifier.error(err.message)