]>
Commit | Line | Data |
---|---|---|
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 { FormValidatorService } 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' | |
13 | ||
14 | @Component({ | |
15 | selector: 'my-video-go-live', | |
16 | templateUrl: './video-go-live.component.html', | |
17 | styleUrls: [ | |
18 | '../shared/video-edit.component.scss', | |
19 | './video-go-live.component.scss', | |
20 | './video-send.scss' | |
21 | ] | |
22 | }) | |
23 | export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate { | |
24 | @Output() firstStepDone = new EventEmitter<string>() | |
25 | @Output() firstStepError = new EventEmitter<void>() | |
26 | ||
27 | firstStepPermanentLive: boolean | |
28 | ||
29 | isInUpdateForm = false | |
30 | isUpdatingVideo = false | |
31 | isOrHasGoingLive = false | |
32 | ||
33 | liveVideo: LiveVideo | |
34 | ||
35 | videoId: number | |
36 | videoUUID: string | |
37 | videoShortUUID: string | |
38 | ||
39 | error: string | |
40 | ||
41 | constructor ( | |
42 | protected formValidatorService: FormValidatorService, | |
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 | |
52 | ) { | |
53 | super() | |
54 | } | |
55 | ||
56 | ngOnInit () { | |
57 | super.ngOnInit() | |
58 | } | |
59 | ||
60 | ngAfterViewInit () { | |
61 | this.hooks.runAction('action:go-live.init', 'video-edit') | |
62 | } | |
63 | ||
64 | canDeactivate () { | |
65 | return { canDeactivate: true } | |
66 | } | |
67 | ||
68 | goLive () { | |
69 | if (this.isOrHasGoingLive) return | |
70 | this.isOrHasGoingLive = true | |
71 | ||
72 | const name = 'Live' | |
73 | ||
74 | const video: LiveVideoCreate = { | |
75 | name, | |
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 | |
83 | } | |
84 | ||
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) | |
88 | ||
89 | this.liveVideoService.goLive(video) | |
90 | .subscribe({ | |
91 | next: res => { | |
92 | this.videoId = res.video.id | |
93 | this.videoUUID = res.video.uuid | |
94 | this.videoShortUUID = res.video.shortUUID | |
95 | this.isInUpdateForm = true | |
96 | ||
97 | this.firstStepDone.emit(name) | |
98 | ||
99 | this.fetchVideoLive() | |
100 | }, | |
101 | ||
102 | error: err => { | |
103 | this.firstStepError.emit() | |
104 | ||
105 | let message = err.message | |
106 | ||
107 | const error = err.body as PeerTubeProblemDocument | |
108 | ||
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` | |
113 | } | |
114 | ||
115 | this.notifier.error(message) | |
116 | } | |
117 | }) | |
118 | } | |
119 | ||
120 | async updateSecondStep () { | |
121 | if (!await this.isFormValid()) return | |
122 | ||
123 | this.isUpdatingVideo = true | |
124 | ||
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 | |
130 | ||
131 | const liveVideoUpdate: LiveVideoUpdate = { | |
132 | saveReplay: this.form.value.saveReplay, | |
133 | latencyMode: this.form.value.latencyMode, | |
134 | permanentLive: this.form.value.permanentLive | |
135 | } | |
136 | ||
137 | // Update the video | |
138 | forkJoin([ | |
139 | this.updateVideoAndCaptions(video), | |
140 | ||
141 | this.liveVideoService.updateLive(this.videoId, liveVideoUpdate) | |
142 | ]).subscribe({ | |
143 | next: () => { | |
144 | this.isUpdatingVideo = false | |
145 | ||
146 | this.notifier.success($localize`Live published.`) | |
147 | ||
148 | this.router.navigateByUrl(Video.buildWatchUrl(video)) | |
149 | }, | |
150 | ||
151 | error: err => { | |
152 | this.error = err.message | |
153 | scrollToTop() | |
154 | logger.error(err) | |
155 | } | |
156 | }) | |
157 | } | |
158 | ||
159 | getMaxLiveDuration () { | |
160 | return this.serverConfig.live.maxDuration / 1000 | |
161 | } | |
162 | ||
163 | isWaitTranscodingEnabled () { | |
164 | return this.form.value['saveReplay'] === true | |
165 | } | |
166 | ||
167 | getNormalLiveDescription () { | |
168 | if (this.isReplayAllowed()) { | |
169 | return $localize`Stream only once, replay will replace your live` | |
170 | } | |
171 | ||
172 | return $localize`Stream only once` | |
173 | } | |
174 | ||
175 | getPermanentLiveDescription () { | |
176 | if (this.isReplayAllowed()) { | |
177 | return $localize`Stream multiple times, replays will be separate videos` | |
178 | } | |
179 | ||
180 | return $localize`Stream multiple times using the same URL` | |
181 | } | |
182 | ||
183 | private isReplayAllowed () { | |
184 | return this.serverConfig.live.allowReplay | |
185 | } | |
186 | ||
187 | private fetchVideoLive () { | |
188 | this.liveVideoService.getVideoLive(this.videoId) | |
189 | .subscribe({ | |
190 | next: liveVideo => { | |
191 | this.liveVideo = liveVideo | |
192 | }, | |
193 | ||
194 | error: err => { | |
195 | this.firstStepError.emit() | |
196 | this.notifier.error(err.message) | |
197 | } | |
198 | }) | |
199 | } | |
200 | } |