2022-02-23 00:33:55 +01:00
|
|
|
import { buildCastLoadRequest, castLoadMedia } from "./castUtils"
|
|
|
|
import EventEmitter from 'events'
|
|
|
|
|
|
|
|
export default class CastPlayer extends EventEmitter {
|
|
|
|
constructor(ctx) {
|
|
|
|
super()
|
|
|
|
|
|
|
|
this.ctx = ctx
|
|
|
|
this.player = null
|
|
|
|
this.playerController = null
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
this.libraryItem = null
|
2022-02-23 00:33:55 +01:00
|
|
|
this.audioTracks = []
|
|
|
|
this.currentTrackIndex = 0
|
2022-03-18 01:10:47 +01:00
|
|
|
this.isHlsTranscode = null
|
2022-02-23 00:33:55 +01:00
|
|
|
this.currentTime = 0
|
|
|
|
this.playWhenReady = false
|
|
|
|
this.defaultPlaybackRate = 1
|
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
// TODO: Use canDisplayType on receiver to check mime types
|
2022-04-16 19:37:10 +02:00
|
|
|
this.playableMimeTypes = []
|
2022-03-05 22:37:30 +01:00
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
this.coverUrl = ''
|
|
|
|
this.castPlayerState = 'IDLE'
|
|
|
|
|
|
|
|
// Supported audio codecs for chromecast
|
2022-03-18 01:10:47 +01:00
|
|
|
|
2022-02-23 00:33:55 +01:00
|
|
|
this.supportedAudioCodecs = ['opus', 'mp3', 'aac', 'flac', 'webma', 'wav']
|
|
|
|
|
|
|
|
this.initialize()
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentTrack() {
|
|
|
|
return this.audioTracks[this.currentTrackIndex] || {}
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
this.player = this.ctx.$root.castPlayer
|
|
|
|
this.playerController = this.ctx.$root.castPlayerController
|
|
|
|
this.playerController.addEventListener(
|
|
|
|
cast.framework.RemotePlayerEventType.MEDIA_INFO_CHANGED, this.evtMediaInfoChanged.bind(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
evtMediaInfoChanged() {
|
|
|
|
// Use the current session to get an up to date media status.
|
|
|
|
let session = cast.framework.CastContext.getInstance().getCurrentSession()
|
|
|
|
if (!session) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let media = session.getMediaSession()
|
|
|
|
if (!media) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentItemId = media.media.itemId
|
|
|
|
if (currentItemId && this.currentTrackIndex !== currentItemId - 1) {
|
|
|
|
this.currentTrackIndex = currentItemId - 1
|
|
|
|
}
|
|
|
|
|
2022-05-29 19:55:14 +02:00
|
|
|
// TODO: Emit finished event
|
2022-02-23 00:33:55 +01:00
|
|
|
if (media.playerState !== this.castPlayerState) {
|
|
|
|
this.emit('stateChange', media.playerState)
|
|
|
|
this.castPlayerState = media.playerState
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
if (this.playerController) {
|
|
|
|
this.playerController.stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
async set(libraryItem, tracks, isHlsTranscode, startTime, playWhenReady = false) {
|
|
|
|
this.libraryItem = libraryItem
|
2022-02-23 00:33:55 +01:00
|
|
|
this.audioTracks = tracks
|
2022-03-18 01:10:47 +01:00
|
|
|
this.isHlsTranscode = isHlsTranscode
|
2022-02-23 00:33:55 +01:00
|
|
|
this.playWhenReady = playWhenReady
|
|
|
|
|
|
|
|
this.currentTime = startTime
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
var coverImg = this.ctx.$store.getters['globals/getLibraryItemCoverSrc'](libraryItem)
|
2022-02-23 00:33:55 +01:00
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
|
|
this.coverUrl = coverImg
|
|
|
|
} else {
|
2022-02-23 02:02:01 +01:00
|
|
|
this.coverUrl = `${window.location.origin}${coverImg}`
|
2022-02-23 00:33:55 +01:00
|
|
|
}
|
|
|
|
|
2022-04-10 13:02:53 +02:00
|
|
|
var request = buildCastLoadRequest(this.libraryItem, this.coverUrl, this.audioTracks, this.currentTime, playWhenReady, this.defaultPlaybackRate)
|
2022-02-23 00:33:55 +01:00
|
|
|
|
|
|
|
var castSession = cast.framework.CastContext.getInstance().getCurrentSession()
|
|
|
|
await castLoadMedia(castSession, request)
|
|
|
|
}
|
|
|
|
|
|
|
|
resetStream(startTime) {
|
|
|
|
// Cast only direct play for now
|
|
|
|
}
|
|
|
|
|
|
|
|
playPause() {
|
|
|
|
if (this.playerController) this.playerController.playOrPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
play() {
|
|
|
|
if (this.playerController) this.playerController.playOrPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
pause() {
|
|
|
|
if (this.playerController) this.playerController.playOrPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
getCurrentTime() {
|
|
|
|
var currentTrackOffset = this.currentTrack.startOffset || 0
|
|
|
|
return this.player ? currentTrackOffset + this.player.currentTime : 0
|
|
|
|
}
|
|
|
|
|
|
|
|
getDuration() {
|
|
|
|
if (!this.audioTracks.length) return 0
|
|
|
|
var lastTrack = this.audioTracks[this.audioTracks.length - 1]
|
|
|
|
return lastTrack.startOffset + lastTrack.duration
|
|
|
|
}
|
|
|
|
|
|
|
|
setPlaybackRate(playbackRate) {
|
|
|
|
this.defaultPlaybackRate = playbackRate
|
|
|
|
}
|
|
|
|
|
|
|
|
async seek(time, playWhenReady) {
|
|
|
|
if (!this.player) return
|
2022-06-04 19:07:38 +02:00
|
|
|
|
|
|
|
this.playWhenReady = playWhenReady
|
2022-02-23 00:33:55 +01:00
|
|
|
if (time < this.currentTrack.startOffset || time > this.currentTrack.startOffset + this.currentTrack.duration) {
|
|
|
|
// Change Track
|
2022-04-10 13:02:53 +02:00
|
|
|
var request = buildCastLoadRequest(this.libraryItem, this.coverUrl, this.audioTracks, time, playWhenReady, this.defaultPlaybackRate)
|
2022-02-23 00:33:55 +01:00
|
|
|
var castSession = cast.framework.CastContext.getInstance().getCurrentSession()
|
|
|
|
await castLoadMedia(castSession, request)
|
|
|
|
} else {
|
|
|
|
var offsetTime = time - (this.currentTrack.startOffset || 0)
|
|
|
|
this.player.currentTime = Math.max(0, offsetTime)
|
|
|
|
this.playerController.seek()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setVolume(volume) {
|
|
|
|
if (!this.player) return
|
|
|
|
this.player.volumeLevel = volume
|
|
|
|
this.playerController.setVolumeLevel()
|
|
|
|
}
|
|
|
|
}
|