]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-go-live.component.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-go-live.component.ts
CommitLineData
b5b68755 1import { forkJoin } from 'rxjs'
d4a8e7a6 2import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
c6c0fa6c 3import { Router } from '@angular/router'
2e257e36 4import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
c6c0fa6c
C
5import { scrollToTop } from '@app/helpers'
6import { FormValidatorService } from '@app/shared/shared-forms'
d4a8e7a6 7import { Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
f8c00564 8import { LiveVideoService } from '@app/shared/shared-video-live'
c6c0fa6c 9import { LoadingBarService } from '@ngx-loading-bar/core'
b6898035 10import { LiveVideo, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models'
c6c0fa6c
C
11import { VideoSend } from './video-send'
12
13@Component({
14 selector: 'my-video-go-live',
15 templateUrl: './video-go-live.component.html',
16 styleUrls: [
17 '../shared/video-edit.component.scss',
1e2fe802 18 './video-go-live.component.scss',
c6c0fa6c
C
19 './video-send.scss'
20 ]
21})
2e257e36 22export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
c6c0fa6c
C
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
25
1e2fe802
C
26 firstStepPermanentLive: boolean
27
c6c0fa6c
C
28 isInUpdateForm = false
29
a5cf76af 30 liveVideo: LiveVideo
2e80d256 31
c6c0fa6c
C
32 videoId: number
33 videoUUID: string
2e80d256
C
34 videoShortUUID: string
35
c6c0fa6c
C
36 error: string
37
c6c0fa6c
C
38 constructor (
39 protected formValidatorService: FormValidatorService,
40 protected loadingBar: LoadingBarService,
41 protected notifier: Notifier,
42 protected authService: AuthService,
43 protected serverService: ServerService,
44 protected videoService: VideoService,
45 protected videoCaptionService: VideoCaptionService,
a5cf76af 46 private liveVideoService: LiveVideoService,
2e257e36
C
47 private router: Router,
48 private hooks: HooksService
9df52d66 49 ) {
c6c0fa6c
C
50 super()
51 }
52
53 ngOnInit () {
54 super.ngOnInit()
55 }
56
2e257e36
C
57 ngAfterViewInit () {
58 this.hooks.runAction('action:go-live.init', 'video-edit')
59 }
60
c6c0fa6c
C
61 canDeactivate () {
62 return { canDeactivate: true }
63 }
64
65 goLive () {
d487a997
C
66 const name = 'Live'
67
b5b68755 68 const video: LiveVideoCreate = {
d487a997 69 name,
a3f45a2a 70 privacy: this.highestPrivacy,
c6c0fa6c
C
71 nsfw: this.serverConfig.instance.isNSFW,
72 waitTranscoding: true,
1e2fe802 73 permanentLive: this.firstStepPermanentLive,
b6898035 74 latencyMode: LiveVideoLatencyMode.DEFAULT,
86c5229b 75 saveReplay: this.isReplayAllowed(),
c6c0fa6c
C
76 channelId: this.firstStepChannelId
77 }
78
c6c0fa6c 79 // Go live in private mode, but correctly fill the update form with the first user choice
b6898035 80 const toPatch = { ...video, privacy: this.firstStepPrivacyId }
c6c0fa6c
C
81 this.form.patchValue(toPatch)
82
1378c0d3
C
83 this.liveVideoService.goLive(video)
84 .subscribe({
85 next: res => {
86 this.videoId = res.video.id
87 this.videoUUID = res.video.uuid
2e80d256 88 this.videoShortUUID = res.video.shortUUID
1378c0d3 89 this.isInUpdateForm = true
c6c0fa6c 90
1378c0d3 91 this.firstStepDone.emit(name)
f8c00564 92
1378c0d3
C
93 this.fetchVideoLive()
94 },
95
96 error: err => {
97 this.firstStepError.emit()
c6c0fa6c 98
1378c0d3 99 let message = err.message
a056ca48 100
1378c0d3 101 const error = err.body as PeerTubeProblemDocument
a056ca48 102
1378c0d3
C
103 if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
104 message = $localize`Cannot create live because this instance have too many created lives`
105 } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
106 message = $localize`Cannot create live because you created too many lives`
107 }
e030bfb5 108
1378c0d3 109 this.notifier.error(message)
a056ca48 110 }
1378c0d3 111 })
c6c0fa6c
C
112 }
113
cc4bf76c
C
114 async updateSecondStep () {
115 if (!await this.isFormValid()) return
c6c0fa6c
C
116
117 const video = new VideoEdit()
118 video.patch(this.form.value)
119 video.id = this.videoId
120 video.uuid = this.videoUUID
2e80d256 121 video.shortUUID = this.videoShortUUID
c6c0fa6c 122
b5b68755 123 const liveVideoUpdate: LiveVideoUpdate = {
bb4ba6d9 124 saveReplay: this.form.value.saveReplay,
b6898035 125 latencyMode: this.form.value.latencyMode,
bb4ba6d9 126 permanentLive: this.form.value.permanentLive
b5b68755
C
127 }
128
c6c0fa6c 129 // Update the video
b5b68755
C
130 forkJoin([
131 this.updateVideoAndCaptions(video),
c6c0fa6c 132
b5b68755 133 this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
1378c0d3
C
134 ]).subscribe({
135 next: () => {
b5b68755
C
136 this.notifier.success($localize`Live published.`)
137
d4a8e7a6 138 this.router.navigateByUrl(Video.buildWatchUrl(video))
b5b68755 139 },
c6c0fa6c 140
1378c0d3 141 error: err => {
b5b68755
C
142 this.error = err.message
143 scrollToTop()
144 console.error(err)
145 }
1378c0d3 146 })
b5b68755 147 }
c6c0fa6c 148
b5b68755
C
149 getMaxLiveDuration () {
150 return this.serverConfig.live.maxDuration / 1000
c6c0fa6c
C
151 }
152
ddb62a85
C
153 isWaitTranscodingEnabled () {
154 return this.form.value['saveReplay'] === true
155 }
156
1e2fe802
C
157 getNormalLiveDescription () {
158 if (this.isReplayAllowed()) {
39e68a32 159 return $localize`Stream only once, replay will replace your live`
1e2fe802
C
160 }
161
162 return $localize`Stream only once`
163 }
164
165 getPermanentLiveDescription () {
166 if (this.isReplayAllowed()) {
39e68a32 167 return $localize`Stream multiple times, replays will be separate videos`
1e2fe802
C
168 }
169
170 return $localize`Stream multiple times using the same URL`
171 }
172
173 private isReplayAllowed () {
174 return this.serverConfig.live.allowReplay
175 }
176
c6c0fa6c 177 private fetchVideoLive () {
a5cf76af 178 this.liveVideoService.getVideoLive(this.videoId)
1378c0d3
C
179 .subscribe({
180 next: liveVideo => {
a5cf76af 181 this.liveVideo = liveVideo
c6c0fa6c
C
182 },
183
1378c0d3 184 error: err => {
c6c0fa6c
C
185 this.firstStepError.emit()
186 this.notifier.error(err.message)
187 }
1378c0d3 188 })
c6c0fa6c
C
189 }
190}