]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-go-live.component.ts
Live streaming implementation first step
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-go-live.component.ts
1
2 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, Notifier, ServerService } from '@app/core'
5 import { scrollToTop } from '@app/helpers'
6 import { FormValidatorService } from '@app/shared/shared-forms'
7 import { VideoCaptionService, VideoEdit, VideoService, VideoLiveService } from '@app/shared/shared-main'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoCreate, VideoLive, VideoPrivacy } from '@shared/models'
10 import { VideoSend } from './video-send'
11
12 @Component({
13 selector: 'my-video-go-live',
14 templateUrl: './video-go-live.component.html',
15 styleUrls: [
16 '../shared/video-edit.component.scss',
17 './video-send.scss'
18 ]
19 })
20 export class VideoGoLiveComponent extends VideoSend implements OnInit, CanComponentDeactivate {
21 @Output() firstStepDone = new EventEmitter<string>()
22 @Output() firstStepError = new EventEmitter<void>()
23
24 isInUpdateForm = false
25
26 videoLive: VideoLive
27 videoId: number
28 videoUUID: string
29 error: string
30
31 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
32
33 constructor (
34 protected formValidatorService: FormValidatorService,
35 protected loadingBar: LoadingBarService,
36 protected notifier: Notifier,
37 protected authService: AuthService,
38 protected serverService: ServerService,
39 protected videoService: VideoService,
40 protected videoCaptionService: VideoCaptionService,
41 private videoLiveService: VideoLiveService,
42 private router: Router
43 ) {
44 super()
45 }
46
47 ngOnInit () {
48 super.ngOnInit()
49 }
50
51 canDeactivate () {
52 return { canDeactivate: true }
53 }
54
55 goLive () {
56 const video: VideoCreate = {
57 name: 'Live',
58 privacy: VideoPrivacy.PRIVATE,
59 nsfw: this.serverConfig.instance.isNSFW,
60 waitTranscoding: true,
61 commentsEnabled: true,
62 downloadEnabled: true,
63 channelId: this.firstStepChannelId
64 }
65
66 this.firstStepDone.emit(name)
67
68 // Go live in private mode, but correctly fill the update form with the first user choice
69 const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId })
70 this.form.patchValue(toPatch)
71
72 this.videoLiveService.goLive(video).subscribe(
73 res => {
74 this.videoId = res.video.id
75 this.videoUUID = res.video.uuid
76 this.isInUpdateForm = true
77
78 this.fetchVideoLive()
79 },
80
81 err => {
82 this.firstStepError.emit()
83 this.notifier.error(err.message)
84 }
85 )
86 }
87
88 updateSecondStep () {
89 if (this.checkForm() === false) {
90 return
91 }
92
93 const video = new VideoEdit()
94 video.patch(this.form.value)
95 video.id = this.videoId
96 video.uuid = this.videoUUID
97
98 // Update the video
99 this.updateVideoAndCaptions(video)
100 .subscribe(
101 () => {
102 this.notifier.success($localize`Live published.`)
103
104 this.router.navigate([ '/videos/watch', video.uuid ])
105 },
106
107 err => {
108 this.error = err.message
109 scrollToTop()
110 console.error(err)
111 }
112 )
113
114 }
115
116 private fetchVideoLive () {
117 this.videoLiveService.getVideoLive(this.videoId)
118 .subscribe(
119 videoLive => {
120 this.videoLive = videoLive
121 },
122
123 err => {
124 this.firstStepError.emit()
125 this.notifier.error(err.message)
126 }
127 )
128 }
129 }