]>
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 C |
5 | import { scrollToTop } from '@app/helpers' |
6 | import { FormValidatorService } 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' |
1378c0d3 | 10 | import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode } from '@shared/models' |
c6c0fa6c C |
11 | import { 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 | 22 | export 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, |
86c5229b | 74 | saveReplay: this.isReplayAllowed(), |
c6c0fa6c C |
75 | channelId: this.firstStepChannelId |
76 | } | |
77 | ||
c6c0fa6c C |
78 | // Go live in private mode, but correctly fill the update form with the first user choice |
79 | const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId }) | |
80 | this.form.patchValue(toPatch) | |
81 | ||
1378c0d3 C |
82 | this.liveVideoService.goLive(video) |
83 | .subscribe({ | |
84 | next: res => { | |
85 | this.videoId = res.video.id | |
86 | this.videoUUID = res.video.uuid | |
2e80d256 | 87 | this.videoShortUUID = res.video.shortUUID |
1378c0d3 | 88 | this.isInUpdateForm = true |
c6c0fa6c | 89 | |
1378c0d3 | 90 | this.firstStepDone.emit(name) |
f8c00564 | 91 | |
1378c0d3 C |
92 | this.fetchVideoLive() |
93 | }, | |
94 | ||
95 | error: err => { | |
96 | this.firstStepError.emit() | |
c6c0fa6c | 97 | |
1378c0d3 | 98 | let message = err.message |
a056ca48 | 99 | |
1378c0d3 | 100 | const error = err.body as PeerTubeProblemDocument |
a056ca48 | 101 | |
1378c0d3 C |
102 | if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) { |
103 | message = $localize`Cannot create live because this instance have too many created lives` | |
104 | } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) { | |
105 | message = $localize`Cannot create live because you created too many lives` | |
106 | } | |
e030bfb5 | 107 | |
1378c0d3 | 108 | this.notifier.error(message) |
a056ca48 | 109 | } |
1378c0d3 | 110 | }) |
c6c0fa6c C |
111 | } |
112 | ||
cc4bf76c C |
113 | async updateSecondStep () { |
114 | if (!await this.isFormValid()) return | |
c6c0fa6c C |
115 | |
116 | const video = new VideoEdit() | |
117 | video.patch(this.form.value) | |
118 | video.id = this.videoId | |
119 | video.uuid = this.videoUUID | |
2e80d256 | 120 | video.shortUUID = this.videoShortUUID |
c6c0fa6c | 121 | |
b5b68755 | 122 | const liveVideoUpdate: LiveVideoUpdate = { |
bb4ba6d9 C |
123 | saveReplay: this.form.value.saveReplay, |
124 | permanentLive: this.form.value.permanentLive | |
b5b68755 C |
125 | } |
126 | ||
c6c0fa6c | 127 | // Update the video |
b5b68755 C |
128 | forkJoin([ |
129 | this.updateVideoAndCaptions(video), | |
c6c0fa6c | 130 | |
b5b68755 | 131 | this.liveVideoService.updateLive(this.videoId, liveVideoUpdate) |
1378c0d3 C |
132 | ]).subscribe({ |
133 | next: () => { | |
b5b68755 C |
134 | this.notifier.success($localize`Live published.`) |
135 | ||
d4a8e7a6 | 136 | this.router.navigateByUrl(Video.buildWatchUrl(video)) |
b5b68755 | 137 | }, |
c6c0fa6c | 138 | |
1378c0d3 | 139 | error: err => { |
b5b68755 C |
140 | this.error = err.message |
141 | scrollToTop() | |
142 | console.error(err) | |
143 | } | |
1378c0d3 | 144 | }) |
b5b68755 | 145 | } |
c6c0fa6c | 146 | |
b5b68755 C |
147 | getMaxLiveDuration () { |
148 | return this.serverConfig.live.maxDuration / 1000 | |
c6c0fa6c C |
149 | } |
150 | ||
ddb62a85 C |
151 | isWaitTranscodingEnabled () { |
152 | return this.form.value['saveReplay'] === true | |
153 | } | |
154 | ||
1e2fe802 C |
155 | getNormalLiveDescription () { |
156 | if (this.isReplayAllowed()) { | |
39e68a32 | 157 | return $localize`Stream only once, replay will replace your live` |
1e2fe802 C |
158 | } |
159 | ||
160 | return $localize`Stream only once` | |
161 | } | |
162 | ||
163 | getPermanentLiveDescription () { | |
164 | if (this.isReplayAllowed()) { | |
39e68a32 | 165 | return $localize`Stream multiple times, replays will be separate videos` |
1e2fe802 C |
166 | } |
167 | ||
168 | return $localize`Stream multiple times using the same URL` | |
169 | } | |
170 | ||
171 | private isReplayAllowed () { | |
172 | return this.serverConfig.live.allowReplay | |
173 | } | |
174 | ||
c6c0fa6c | 175 | private fetchVideoLive () { |
a5cf76af | 176 | this.liveVideoService.getVideoLive(this.videoId) |
1378c0d3 C |
177 | .subscribe({ |
178 | next: liveVideo => { | |
a5cf76af | 179 | this.liveVideo = liveVideo |
c6c0fa6c C |
180 | }, |
181 | ||
1378c0d3 | 182 | error: err => { |
c6c0fa6c C |
183 | this.firstStepError.emit() |
184 | this.notifier.error(err.message) | |
185 | } | |
1378c0d3 | 186 | }) |
c6c0fa6c C |
187 | } |
188 | } |