]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts
Support transcoding profile in client admin
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / config / edit-custom-config / edit-custom-config.component.ts
1 import { forkJoin } from 'rxjs'
2 import { pairwise } from 'rxjs/operators'
3 import { ViewportScroller } from '@angular/common'
4 import { AfterViewChecked, Component, OnInit, ViewChild } from '@angular/core'
5 import { ConfigService } from '@app/+admin/config/shared/config.service'
6 import { Notifier } from '@app/core'
7 import { ServerService } from '@app/core/server/server.service'
8 import {
9 ADMIN_EMAIL_VALIDATOR,
10 CACHE_CAPTIONS_SIZE_VALIDATOR,
11 CACHE_PREVIEWS_SIZE_VALIDATOR,
12 INDEX_URL_VALIDATOR,
13 INSTANCE_NAME_VALIDATOR,
14 INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
15 SEARCH_INDEX_URL_VALIDATOR,
16 SERVICES_TWITTER_USERNAME_VALIDATOR,
17 SIGNUP_LIMIT_VALIDATOR,
18 TRANSCODING_THREADS_VALIDATOR
19 } from '@app/shared/form-validators/custom-config-validators'
20 import { USER_VIDEO_QUOTA_DAILY_VALIDATOR, USER_VIDEO_QUOTA_VALIDATOR } from '@app/shared/form-validators/user-validators'
21 import { FormReactive, FormValidatorService, SelectOptionsItem } from '@app/shared/shared-forms'
22 import { NgbNav } from '@ng-bootstrap/ng-bootstrap'
23 import { CustomConfig, ServerConfig } from '@shared/models'
24
25 @Component({
26 selector: 'my-edit-custom-config',
27 templateUrl: './edit-custom-config.component.html',
28 styleUrls: [ './edit-custom-config.component.scss' ]
29 })
30 export class EditCustomConfigComponent extends FormReactive implements OnInit, AfterViewChecked {
31 // FIXME: use built-in router
32 @ViewChild('nav') nav: NgbNav
33
34 initDone = false
35 customConfig: CustomConfig
36
37 resolutions: { id: string, label: string, description?: string }[] = []
38 liveResolutions: { id: string, label: string, description?: string }[] = []
39 transcodingThreadOptions: { label: string, value: number }[] = []
40 liveMaxDurationOptions: { label: string, value: number }[] = []
41
42 vodTranscodingProfileOptions: string[] = []
43 liveTranscodingProfileOptions: string[] = []
44
45 languageItems: SelectOptionsItem[] = []
46 categoryItems: SelectOptionsItem[] = []
47
48 signupAlertMessage: string
49
50 private serverConfig: ServerConfig
51
52 constructor (
53 private viewportScroller: ViewportScroller,
54 protected formValidatorService: FormValidatorService,
55 private notifier: Notifier,
56 private configService: ConfigService,
57 private serverService: ServerService
58 ) {
59 super()
60
61 this.resolutions = [
62 {
63 id: '0p',
64 label: $localize`Audio-only`,
65 description: $localize`A <code>.mp4</code> that keeps the original audio track, with no video`
66 },
67 {
68 id: '240p',
69 label: $localize`240p`
70 },
71 {
72 id: '360p',
73 label: $localize`360p`
74 },
75 {
76 id: '480p',
77 label: $localize`480p`
78 },
79 {
80 id: '720p',
81 label: $localize`720p`
82 },
83 {
84 id: '1080p',
85 label: $localize`1080p`
86 },
87 {
88 id: '1440p',
89 label: $localize`1440p`
90 },
91 {
92 id: '2160p',
93 label: $localize`2160p`
94 }
95 ]
96
97 this.liveResolutions = this.resolutions.filter(r => r.id !== '0p')
98
99 this.transcodingThreadOptions = [
100 { value: 0, label: $localize`Auto (via ffmpeg)` },
101 { value: 1, label: '1' },
102 { value: 2, label: '2' },
103 { value: 4, label: '4' },
104 { value: 8, label: '8' }
105 ]
106
107 this.vodTranscodingProfileOptions = [ 'default' ]
108 this.liveTranscodingProfileOptions = [ 'default' ]
109
110 this.liveMaxDurationOptions = [
111 { value: -1, label: $localize`No limit` },
112 { value: 1000 * 3600, label: $localize`1 hour` },
113 { value: 1000 * 3600 * 3, label: $localize`3 hours` },
114 { value: 1000 * 3600 * 5, label: $localize`5 hours` },
115 { value: 1000 * 3600 * 10, label: $localize`10 hours` }
116 ]
117 }
118
119 get videoQuotaOptions () {
120 return this.configService.videoQuotaOptions
121 }
122
123 get videoQuotaDailyOptions () {
124 return this.configService.videoQuotaDailyOptions
125 }
126
127 get availableThemes () {
128 return this.serverConfig.theme.registered
129 .map(t => t.name)
130 }
131
132 get liveRTMPPort () {
133 return this.serverConfig.live.rtmp.port
134 }
135
136 getAvailableTranscodingProfile (type: 'live' | 'vod') {
137 if (type === 'live') {
138 return this.serverConfig.live.transcoding.availableProfiles
139 }
140
141 return this.serverConfig.transcoding.availableProfiles
142 }
143
144 getTotalTranscodingThreads () {
145 const transcodingEnabled = this.form.value['transcoding']['enabled']
146 const transcodingThreads = this.form.value['transcoding']['threads']
147 const liveTranscodingEnabled = this.form.value['live']['transcoding']['enabled']
148 const liveTranscodingThreads = this.form.value['live']['transcoding']['threads']
149
150 // checks whether all enabled method are on fixed values and not on auto (= 0)
151 let noneOnAuto = !transcodingEnabled || +transcodingThreads > 0
152 noneOnAuto &&= !liveTranscodingEnabled || +liveTranscodingThreads > 0
153
154 // count total of fixed value, repalcing auto by a single thread (knowing it will display "at least")
155 let value = 0
156 if (transcodingEnabled) value += +transcodingThreads || 1
157 if (liveTranscodingEnabled) value += +liveTranscodingThreads || 1
158
159 return {
160 value,
161 atMost: noneOnAuto, // auto switches everything to a least estimation since ffmpeg will take as many threads as possible
162 unit: value > 1
163 ? $localize`threads`
164 : $localize`thread`
165 }
166 }
167
168 getResolutionKey (resolution: string) {
169 return 'transcoding.resolutions.' + resolution
170 }
171
172 ngOnInit () {
173 this.serverConfig = this.serverService.getTmpConfig()
174 this.serverService.getConfig()
175 .subscribe(config => {
176 this.serverConfig = config
177 })
178
179 const formGroupData: { [key in keyof CustomConfig ]: any } = {
180 instance: {
181 name: INSTANCE_NAME_VALIDATOR,
182 shortDescription: INSTANCE_SHORT_DESCRIPTION_VALIDATOR,
183 description: null,
184
185 isNSFW: false,
186 defaultNSFWPolicy: null,
187
188 terms: null,
189 codeOfConduct: null,
190
191 creationReason: null,
192 moderationInformation: null,
193 administrator: null,
194 maintenanceLifetime: null,
195 businessModel: null,
196
197 hardwareInformation: null,
198
199 categories: null,
200 languages: null,
201
202 defaultClientRoute: null,
203
204 customizations: {
205 javascript: null,
206 css: null
207 }
208 },
209 theme: {
210 default: null
211 },
212 services: {
213 twitter: {
214 username: SERVICES_TWITTER_USERNAME_VALIDATOR,
215 whitelisted: null
216 }
217 },
218 cache: {
219 previews: {
220 size: CACHE_PREVIEWS_SIZE_VALIDATOR
221 },
222 captions: {
223 size: CACHE_CAPTIONS_SIZE_VALIDATOR
224 }
225 },
226 signup: {
227 enabled: null,
228 limit: SIGNUP_LIMIT_VALIDATOR,
229 requiresEmailVerification: null
230 },
231 import: {
232 videos: {
233 http: {
234 enabled: null
235 },
236 torrent: {
237 enabled: null
238 }
239 }
240 },
241 trending: {
242 videos: {
243 algorithms: {
244 enabled: null,
245 default: null
246 }
247 }
248 },
249 admin: {
250 email: ADMIN_EMAIL_VALIDATOR
251 },
252 contactForm: {
253 enabled: null
254 },
255 user: {
256 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
257 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR
258 },
259 transcoding: {
260 enabled: null,
261 threads: TRANSCODING_THREADS_VALIDATOR,
262 allowAdditionalExtensions: null,
263 allowAudioFiles: null,
264 profile: null,
265 resolutions: {},
266 hls: {
267 enabled: null
268 },
269 webtorrent: {
270 enabled: null
271 }
272 },
273 live: {
274 enabled: null,
275
276 maxDuration: null,
277 maxInstanceLives: null,
278 maxUserLives: null,
279 allowReplay: null,
280
281 transcoding: {
282 enabled: null,
283 threads: TRANSCODING_THREADS_VALIDATOR,
284 profile: null,
285 resolutions: {}
286 }
287 },
288 autoBlacklist: {
289 videos: {
290 ofUsers: {
291 enabled: null
292 }
293 }
294 },
295 followers: {
296 instance: {
297 enabled: null,
298 manualApproval: null
299 }
300 },
301 followings: {
302 instance: {
303 autoFollowBack: {
304 enabled: null
305 },
306 autoFollowIndex: {
307 enabled: null,
308 indexUrl: INDEX_URL_VALIDATOR
309 }
310 }
311 },
312 broadcastMessage: {
313 enabled: null,
314 level: null,
315 dismissable: null,
316 message: null
317 },
318 search: {
319 remoteUri: {
320 users: null,
321 anonymous: null
322 },
323 searchIndex: {
324 enabled: null,
325 url: SEARCH_INDEX_URL_VALIDATOR,
326 disableLocalSearch: null,
327 isDefaultSearch: null
328 }
329 }
330 }
331
332 const defaultValues = {
333 transcoding: {
334 resolutions: {}
335 },
336 live: {
337 transcoding: {
338 resolutions: {}
339 }
340 }
341 }
342
343 for (const resolution of this.resolutions) {
344 defaultValues.transcoding.resolutions[resolution.id] = 'false'
345 formGroupData.transcoding.resolutions[resolution.id] = null
346 }
347
348 for (const resolution of this.liveResolutions) {
349 defaultValues.live.transcoding.resolutions[resolution.id] = 'false'
350 formGroupData.live.transcoding.resolutions[resolution.id] = null
351 }
352
353 this.buildForm(formGroupData)
354 this.loadForm()
355
356 this.checkTranscodingFields()
357 this.checkSignupField()
358 }
359
360 ngAfterViewChecked () {
361 if (!this.initDone) {
362 this.initDone = true
363 this.gotoAnchor()
364 }
365 }
366
367 isTranscodingEnabled () {
368 return this.form.value['transcoding']['enabled'] === true
369 }
370
371 isLiveEnabled () {
372 return this.form.value['live']['enabled'] === true
373 }
374
375 isLiveTranscodingEnabled () {
376 return this.form.value['live']['transcoding']['enabled'] === true
377 }
378
379 isSignupEnabled () {
380 return this.form.value['signup']['enabled'] === true
381 }
382
383 isSearchIndexEnabled () {
384 return this.form.value['search']['searchIndex']['enabled'] === true
385 }
386
387 isAutoFollowIndexEnabled () {
388 return this.form.value['followings']['instance']['autoFollowIndex']['enabled'] === true
389 }
390
391 trendingVideosAlgorithmsEnabledIncludes (algorithm: string) {
392 return this.form.value['trending']['videos']['algorithms']['enabled'].find((e: string) => e === algorithm)
393 }
394
395 async formValidated () {
396 const value: CustomConfig = this.form.getRawValue()
397
398 this.configService.updateCustomConfig(value)
399 .subscribe(
400 res => {
401 this.customConfig = res
402
403 // Reload general configuration
404 this.serverService.resetConfig()
405
406 this.updateForm()
407
408 this.notifier.success($localize`Configuration updated.`)
409 },
410
411 err => this.notifier.error(err.message)
412 )
413 }
414
415 gotoAnchor () {
416 const hashToNav = {
417 'customizations': 'advanced-configuration'
418 }
419 const hash = window.location.hash.replace('#', '')
420
421 if (hash && Object.keys(hashToNav).includes(hash)) {
422 this.nav.select(hashToNav[hash])
423 setTimeout(() => this.viewportScroller.scrollToAnchor(hash), 100)
424 }
425 }
426
427 hasConsistentOptions () {
428 if (this.hasLiveAllowReplayConsistentOptions()) return true
429
430 return false
431 }
432
433 hasLiveAllowReplayConsistentOptions () {
434 if (this.isTranscodingEnabled() === false && this.isLiveEnabled() && this.form.value['live']['allowReplay'] === true) {
435 return false
436 }
437
438 return true
439 }
440
441 private updateForm () {
442 this.form.patchValue(this.customConfig)
443 }
444
445 private loadForm () {
446 forkJoin([
447 this.configService.getCustomConfig(),
448 this.serverService.getVideoLanguages(),
449 this.serverService.getVideoCategories()
450 ]).subscribe(
451 ([ config, languages, categories ]) => {
452 this.customConfig = config
453
454 this.languageItems = languages.map(l => ({ label: l.label, id: l.id }))
455 this.categoryItems = categories.map(l => ({ label: l.label, id: l.id + '' }))
456
457 this.updateForm()
458 // Force form validation
459 this.forceCheck()
460 },
461
462 err => this.notifier.error(err.message)
463 )
464 }
465
466 private checkTranscodingFields () {
467 const hlsControl = this.form.get('transcoding.hls.enabled')
468 const webtorrentControl = this.form.get('transcoding.webtorrent.enabled')
469
470 webtorrentControl.valueChanges
471 .subscribe(newValue => {
472 if (newValue === false && !hlsControl.disabled) {
473 hlsControl.disable()
474 }
475
476 if (newValue === true && !hlsControl.enabled) {
477 hlsControl.enable()
478 }
479 })
480
481 hlsControl.valueChanges
482 .subscribe(newValue => {
483 if (newValue === false && !webtorrentControl.disabled) {
484 webtorrentControl.disable()
485 }
486
487 if (newValue === true && !webtorrentControl.enabled) {
488 webtorrentControl.enable()
489 }
490 })
491 }
492
493 private checkSignupField () {
494 const signupControl = this.form.get('signup.enabled')
495
496 signupControl.valueChanges
497 .pipe(pairwise())
498 .subscribe(([ oldValue, newValue ]) => {
499 if (oldValue !== true && newValue === true) {
500 // tslint:disable:max-line-length
501 this.signupAlertMessage = $localize`You enabled signup: we automatically enabled the "Block new videos automatically" checkbox of the "Videos" section just below.`
502
503 this.form.patchValue({
504 autoBlacklist: {
505 videos: {
506 ofUsers: {
507 enabled: true
508 }
509 }
510 }
511 })
512 }
513 })
514 }
515 }