video-go-live.component.ts 4.42 KB
Newer Older
1

Chocobozzz's avatar
Chocobozzz committed
2
import { forkJoin } from 'rxjs'
3
import { AfterViewChecked, 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
7
import { scrollToTop } from '@app/helpers'
import { FormValidatorService } from '@app/shared/shared-forms'
Chocobozzz's avatar
Chocobozzz committed
8
9
import { VideoCaptionService, VideoEdit, VideoService } from '@app/shared/shared-main'
import { LiveVideoService } from '@app/shared/shared-video-live'
10
import { LoadingBarService } from '@ngx-loading-bar/core'
Chocobozzz's avatar
Chocobozzz committed
11
import { LiveVideo, LiveVideoCreate, LiveVideoUpdate, PeerTubeProblemDocument, ServerErrorCode, VideoPrivacy } from '@shared/models'
12
13
14
15
16
17
18
19
20
21
import { VideoSend } from './video-send'

@Component({
  selector: 'my-video-go-live',
  templateUrl: './video-go-live.component.html',
  styleUrls: [
    '../shared/video-edit.component.scss',
    './video-send.scss'
  ]
})
22
export class VideoGoLiveComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
23
24
25
26
27
  @Output() firstStepDone = new EventEmitter<string>()
  @Output() firstStepError = new EventEmitter<void>()

  isInUpdateForm = false

28
  liveVideo: LiveVideo
29
30
31
32
33
34
35
36
37
38
39
40
  videoId: number
  videoUUID: string
  error: string

  constructor (
    protected formValidatorService: FormValidatorService,
    protected loadingBar: LoadingBarService,
    protected notifier: Notifier,
    protected authService: AuthService,
    protected serverService: ServerService,
    protected videoService: VideoService,
    protected videoCaptionService: VideoCaptionService,
41
    private liveVideoService: LiveVideoService,
42
43
    private router: Router,
    private hooks: HooksService
44
45
46
47
48
49
50
51
    ) {
    super()
  }

  ngOnInit () {
    super.ngOnInit()
  }

52
53
54
55
  ngAfterViewInit () {
    this.hooks.runAction('action:go-live.init', 'video-edit')
  }

56
57
58
59
60
  canDeactivate () {
    return { canDeactivate: true }
  }

  goLive () {
61
62
    const name = 'Live'

Chocobozzz's avatar
Chocobozzz committed
63
    const video: LiveVideoCreate = {
64
      name,
65
66
67
68
69
70
71
72
73
74
75
76
      privacy: VideoPrivacy.PRIVATE,
      nsfw: this.serverConfig.instance.isNSFW,
      waitTranscoding: true,
      commentsEnabled: true,
      downloadEnabled: true,
      channelId: this.firstStepChannelId
    }

    // Go live in private mode, but correctly fill the update form with the first user choice
    const toPatch = Object.assign({}, video, { privacy: this.firstStepPrivacyId })
    this.form.patchValue(toPatch)

77
    this.liveVideoService.goLive(video).subscribe(
78
79
80
81
82
      res => {
        this.videoId = res.video.id
        this.videoUUID = res.video.uuid
        this.isInUpdateForm = true

Chocobozzz's avatar
Chocobozzz committed
83
84
        this.firstStepDone.emit(name)

85
86
87
88
89
        this.fetchVideoLive()
      },

      err => {
        this.firstStepError.emit()
Chocobozzz's avatar
Chocobozzz committed
90
91
92

        let message = err.message

Chocobozzz's avatar
Chocobozzz committed
93
94
95
        const error = err.body as PeerTubeProblemDocument

        if (error?.code === ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED) {
Chocobozzz's avatar
Chocobozzz committed
96
          message = $localize`Cannot create live because this instance have too many created lives`
Chocobozzz's avatar
Chocobozzz committed
97
        } else if (error?.code === ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED) {
Chocobozzz's avatar
Chocobozzz committed
98
99
100
101
          message = $localize`Cannot create live because you created too many lives`
        }

        this.notifier.error(message)
102
103
104
105
106
107
108
109
110
111
112
113
114
115
      }
    )
  }

  updateSecondStep () {
    if (this.checkForm() === false) {
      return
    }

    const video = new VideoEdit()
    video.patch(this.form.value)
    video.id = this.videoId
    video.uuid = this.videoUUID

Chocobozzz's avatar
Chocobozzz committed
116
    const liveVideoUpdate: LiveVideoUpdate = {
Chocobozzz's avatar
Chocobozzz committed
117
118
      saveReplay: this.form.value.saveReplay,
      permanentLive: this.form.value.permanentLive
Chocobozzz's avatar
Chocobozzz committed
119
120
    }

121
    // Update the video
Chocobozzz's avatar
Chocobozzz committed
122
123
    forkJoin([
      this.updateVideoAndCaptions(video),
124

Chocobozzz's avatar
Chocobozzz committed
125
126
127
128
129
      this.liveVideoService.updateLive(this.videoId, liveVideoUpdate)
    ]).subscribe(
      () => {
        this.notifier.success($localize`Live published.`)

130
        this.router.navigate(['/w', video.uuid])
Chocobozzz's avatar
Chocobozzz committed
131
      },
132

Chocobozzz's avatar
Chocobozzz committed
133
134
135
136
137
138
139
      err => {
        this.error = err.message
        scrollToTop()
        console.error(err)
      }
    )
  }
140

Chocobozzz's avatar
Chocobozzz committed
141
142
  getMaxLiveDuration () {
    return this.serverConfig.live.maxDuration / 1000
143
144
  }

145
146
147
148
  isWaitTranscodingEnabled () {
    return this.form.value['saveReplay'] === true
  }

149
  private fetchVideoLive () {
150
    this.liveVideoService.getVideoLive(this.videoId)
151
      .subscribe(
152
153
        liveVideo => {
          this.liveVideo = liveVideo
154
155
156
157
158
159
160
161
162
        },

        err => {
          this.firstStepError.emit()
          this.notifier.error(err.message)
        }
      )
  }
}