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