]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | import { forkJoin } from 'rxjs' | |
3 | import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core' | |
4 | import { Router } from '@angular/router' | |
5 | import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core' | |
6 | import { scrollToTop } from '@app/helpers' | |
7 | import { FormValidatorService } from '@app/shared/shared-forms' | |
8 | import { Video, VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main' | |
9 | import { LiveVideoService } from '@app/shared/shared-video-live' | |
10 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
11 | import { LiveVideo, LiveVideoCreate, 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-send.scss' | |
20 | ] | |
21 | }) | |
22 | export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate { | |
23 | @Output() firstStepDone = new EventEmitter<string>() | |
24 | @Output() firstStepError = new EventEmitter<void>() | |
25 | ||
26 | isInUpdateForm = false | |
27 | ||
28 | liveVideo: LiveVideo | |
29 | ||
30 | videoId: number | |
31 | videoUUID: string | |
32 | videoShortUUID: string | |
33 | ||
34 | error: string | |
35 | ||
36 | constructor ( | |
37 | protected formValidatorService: FormValidatorService, | |
38 | protected loadingBar: LoadingBarService, | |
39 | protected notifier: Notifier, | |
40 | protected authService: AuthService, | |
41 | protected serverService: ServerService, | |
42 | protected videoService: VideoService, | |
43 | protected videoCaptionService: VideoCaptionService, | |
44 | private liveVideoService: LiveVideoService, | |
45 | private router: Router, | |
46 | private hooks: HooksService | |
47 | ) { | |
48 | super() | |
49 | } | |
50 | ||
51 | ngOnInit () { | |
52 | super.ngOnInit() | |
53 | } | |
54 | ||
55 | ngAfterViewInit () { | |
56 | this.hooks.runAction('action:go-live.init', 'video-edit') | |
57 | } | |
58 | ||
59 | canDeactivate () { | |
60 | return { canDeactivate: true } | |
61 | } | |
62 | ||
63 | goLive () { | |
64 | const name = 'Live' | |
65 | ||
66 | const video: LiveVideoCreate = { | |
67 | name, | |
68 | privacy: this.highestPrivacy, | |
69 | nsfw: this.serverConfig.instance.isNSFW, | |
70 | waitTranscoding: true, | |
71 | commentsEnabled: true, | |
72 | downloadEnabled: true, | |
73 | channelId: this.firstStepChannelId | |
74 | } | |
75 | ||
76 | // Go live in private mode, but correctly fill the update form with the first user choice | |
77 | const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId }) | |
78 | this.form.patchValue(toPatch) | |
79 | ||
80 | this.liveVideoService.goLive(video) | |
81 | .subscribe({ | |
82 | next: res => { | |
83 | this.videoId = res.video.id | |
84 | this.videoUUID = res.video.uuid | |
85 | this.videoShortUUID = res.video.shortUUID | |
86 | this.isInUpdateForm = true | |
87 | ||
88 | this.firstStepDone.emit(name) | |
89 | ||
90 | this.fetchVideoLive() | |
91 | }, | |
92 | ||
93 | error: err => { | |
94 | this.firstStepError.emit() | |
95 | ||
96 | let message = err.message | |
97 | ||
98 | const error = err.body as PeerTubeProblemDocument | |
99 | ||
100 | if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) { | |
101 | message = $localize`Cannot create live because this instance have too many created lives` | |
102 | } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) { | |
103 | message = $localize`Cannot create live because you created too many lives` | |
104 | } | |
105 | ||
106 | this.notifier.error(message) | |
107 | } | |
108 | }) | |
109 | } | |
110 | ||
111 | updateSecondStep () { | |
112 | if (this.checkForm() === false) { | |
113 | return | |
114 | } | |
115 | ||
116 | const video = new VideoEdit() | |
117 | video.patch(this.form.value) | |
118 | video.id = this.videoId | |
119 | video.uuid = this.videoUUID | |
120 | video.shortUUID = this.videoShortUUID | |
121 | ||
122 | const liveVideoUpdate: LiveVideoUpdate = { | |
123 | saveReplay: this.form.value.saveReplay, | |
124 | permanentLive: this.form.value.permanentLive | |
125 | } | |
126 | ||
127 | // Update the video | |
128 | forkJoin([ | |
129 | this.updateVideoAndCaptions(video), | |
130 | ||
131 | this.liveVideoService.updateLive(this.videoId, liveVideoUpdate) | |
132 | ]).subscribe({ | |
133 | next: () => { | |
134 | this.notifier.success($localize`Live published.`) | |
135 | ||
136 | this.router.navigateByUrl(Video.buildWatchUrl(video)) | |
137 | }, | |
138 | ||
139 | error: err => { | |
140 | this.error = err.message | |
141 | scrollToTop() | |
142 | console.error(err) | |
143 | } | |
144 | }) | |
145 | } | |
146 | ||
147 | getMaxLiveDuration () { | |
148 | return this.serverConfig.live.maxDuration / 1000 | |
149 | } | |
150 | ||
151 | isWaitTranscodingEnabled () { | |
152 | return this.form.value['saveReplay'] === true | |
153 | } | |
154 | ||
155 | private fetchVideoLive () { | |
156 | this.liveVideoService.getVideoLive(this.videoId) | |
157 | .subscribe({ | |
158 | next: liveVideo => { | |
159 | this.liveVideo = liveVideo | |
160 | }, | |
161 | ||
162 | error: err => { | |
163 | this.firstStepError.emit() | |
164 | this.notifier.error(err.message) | |
165 | } | |
166 | }) | |
167 | } | |
168 | } |