2022-01-11 11:31:22 +00:00
function initializeCastApi ( ) {
2022-01-15 06:33:16 +00:00
cast . framework . CastContext . getInstance ( ) . setOptions ( {
receiverApplicationId : chrome . cast . media . DEFAULT _MEDIA _RECEIVER _APP _ID , // Use built in reciver app on cast device, see https://developers.google.com/cast/docs/styled_receiver if you want to be able to add a theme, splash screen or watermark. Has a $5 one time fee.
autoJoinPolicy : chrome . cast . AutoJoinPolicy . ORIGIN _SCOPED
} ) ;
2022-01-11 11:31:22 +00:00
2022-01-15 06:33:16 +00:00
var player = new cast . framework . RemotePlayer ( ) ;
var playerController = new cast . framework . RemotePlayerController ( player ) ;
2022-01-11 11:31:22 +00:00
2022-01-15 06:33:16 +00:00
// Add event listerner to check if a connection to a cast device is initiated
playerController . addEventListener (
cast . framework . RemotePlayerEventType . IS _CONNECTED _CHANGED , function ( ) {
castConnectionChange ( player )
}
) ;
2022-02-24 02:36:31 +00:00
playerController . addEventListener (
cast . framework . RemotePlayerEventType . CURRENT _TIME _CHANGED , function ( ) {
castVideoProgress ( player )
}
) ;
playerController . addEventListener (
cast . framework . RemotePlayerEventType . IS _PAUSED _CHANGED , function ( ) {
castVideoPaused ( player )
}
) ;
2022-01-11 11:31:22 +00:00
}
2022-01-15 06:33:16 +00:00
function castConnectionChange ( player ) {
// If cast connection is initialized start cast
if ( player . isConnected ) {
// console.log("Cast Connected.");
castStart ( ) ;
} else if ( ! player . isConnected ) {
// console.log("Cast Disconnected.");
}
2022-01-11 11:31:22 +00:00
}
2022-02-24 02:36:31 +00:00
function castVideoProgress ( player ) {
var videoId = getVideoPlayerVideoId ( ) ;
if ( player . mediaInfo . contentId . includes ( videoId ) ) {
var currentTime = player . currentTime ;
var duration = player . duration ;
if ( ( currentTime % 10 ) <= 1.0 && currentTime != 0 && duration != 0 ) { // Check progress every 10 seconds or else progress is checked a few times a second
postVideoProgress ( videoId , currentTime ) ;
2022-02-25 03:39:33 +00:00
setProgressBar ( videoId , currentTime , duration ) ;
2022-02-24 02:36:31 +00:00
if ( ! getVideoPlayerWatchStatus ( ) ) { // Check if video is already marked as watched
if ( watchedThreshold ( currentTime , duration ) ) {
isWatched ( videoId ) ;
}
}
}
}
}
function castVideoPaused ( player ) {
var videoId = getVideoPlayerVideoId ( ) ;
var currentTime = player . currentTime ;
var duration = player . duration ;
if ( player . mediaInfo != null ) {
if ( player . mediaInfo . contentId . includes ( videoId ) ) {
if ( currentTime != 0 && duration != 0 ) {
postVideoProgress ( videoId , currentTime ) ;
}
}
}
}
2022-01-15 06:33:16 +00:00
function castStart ( ) {
var castSession = cast . framework . CastContext . getInstance ( ) . getCurrentSession ( ) ;
// Check if there is already media playing on the cast target to prevent recasting on page reload or switching to another video page
if ( ! castSession . getMediaSession ( ) ) {
2022-02-24 02:36:31 +00:00
var videoId = getVideoPlayerVideoId ( ) ;
var videoData = getVideoData ( videoId ) ;
var contentId = getURL ( ) + videoData . data . media _url ;
var contentTitle = videoData . data . title ;
var contentImage = getURL ( ) + videoData . data . vid _thumb _url ;
2022-01-15 06:33:16 +00:00
contentType = 'video/mp4' ; // Set content type, only videos right now so it is hard coded
2022-02-24 02:36:31 +00:00
contentCurrentTime = getVideoPlayerCurrentTime ( ) ; // Get video's current position
2022-02-12 12:08:19 +00:00
contentActiveSubtitle = [ ] ;
// Check if a subtitle is turned on.
2022-02-24 02:36:31 +00:00
for ( var i = 0 ; i < getVideoPlayer ( ) . textTracks . length ; i ++ ) {
if ( getVideoPlayer ( ) . textTracks [ i ] . mode == "showing" ) {
2022-02-12 12:08:19 +00:00
contentActiveSubtitle = [ i + 1 ] ;
}
}
contentSubtitles = [ ] ;
2022-02-24 02:36:31 +00:00
var videoSubtitles = videoData . data . subtitles ; // Array of subtitles
if ( typeof ( videoSubtitles ) != 'undefined' && videoData . config . downloads . subtitle ) {
for ( var i = 0 ; i < videoSubtitles . length ; i ++ ) {
2022-02-12 12:08:19 +00:00
subtitle = new chrome . cast . media . Track ( i , chrome . cast . media . TrackType . TEXT ) ;
2022-02-24 02:36:31 +00:00
subtitle . trackContentId = videoSubtitles [ i ] . media _url ;
2022-02-12 12:08:19 +00:00
subtitle . trackContentType = 'text/vtt' ;
subtitle . subtype = chrome . cast . media . TextTrackType . SUBTITLES ;
2022-02-24 02:36:31 +00:00
subtitle . name = videoSubtitles [ i ] . name ;
subtitle . language = videoSubtitles [ i ] . lang ;
2022-02-12 12:08:19 +00:00
subtitle . customData = null ;
contentSubtitles . push ( subtitle ) ;
}
}
2022-01-11 11:31:22 +00:00
2022-01-15 06:33:16 +00:00
mediaInfo = new chrome . cast . media . MediaInfo ( contentId , contentType ) ; // Create MediaInfo var that contains url and content type
// mediaInfo.streamType = chrome.cast.media.StreamType.BUFFERED; // Set type of stream, BUFFERED, LIVE, OTHER
mediaInfo . metadata = new chrome . cast . media . GenericMediaMetadata ( ) ; // Create metadata var and add it to MediaInfo
2022-02-12 12:08:19 +00:00
mediaInfo . metadata . title = contentTitle . replace ( "&" , "&" ) ; // Set the video title
2022-01-15 06:33:16 +00:00
mediaInfo . metadata . images = [ new chrome . cast . Image ( contentImage ) ] ; // Set the video thumbnail
2022-02-12 12:08:19 +00:00
// mediaInfo.textTrackStyle = new chrome.cast.media.TextTrackStyle();
mediaInfo . tracks = contentSubtitles ;
2022-01-11 11:31:22 +00:00
2022-01-15 06:33:16 +00:00
var request = new chrome . cast . media . LoadRequest ( mediaInfo ) ; // Create request with the previously set MediaInfo.
// request.queueData = new chrome.cast.media.QueueData(); // See https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.media.QueueData for playlist support.
request . currentTime = shiftCurrentTime ( contentCurrentTime ) ; // Set video start position based on the browser video position
2022-02-12 12:08:19 +00:00
request . activeTrackIds = contentActiveSubtitle ; // Set active subtitle based on video player
2022-01-15 06:33:16 +00:00
// request.autoplay = false; // Set content to auto play, true by default
castSession . loadMedia ( request ) . then (
function ( ) {
castSuccessful ( ) ;
} ,
function ( ) {
castFailed ( errorCode ) ;
}
) ; // Send request to cast device
}
2022-01-11 11:31:22 +00:00
}
2022-01-15 06:33:16 +00:00
function shiftCurrentTime ( contentCurrentTime ) { // Shift media back 3 seconds to prevent missing some of the content
if ( contentCurrentTime > 5 ) {
return ( contentCurrentTime - 3 ) ;
} else {
return ( 0 ) ;
}
2022-01-11 11:31:22 +00:00
}
2022-01-15 06:33:16 +00:00
function castSuccessful ( ) {
// console.log('Cast Successful.');
2022-02-24 02:36:31 +00:00
getVideoPlayer ( ) . pause ( ) ; // Pause browser video on successful cast
2022-01-11 11:31:22 +00:00
}
2022-01-15 06:33:16 +00:00
function castFailed ( errorCode ) {
console . log ( 'Error code: ' + errorCode ) ;
2022-01-11 11:31:22 +00:00
}
2022-01-15 06:33:16 +00:00
window [ '__onGCastApiAvailable' ] = function ( isAvailable ) {
if ( isAvailable ) {
initializeCastApi ( ) ;
}
2022-01-11 11:31:22 +00:00
}