Skip to content

Commit

Permalink
fix: account for Chromecast sessions when casting
Browse files Browse the repository at this point in the history
closes #345
  • Loading branch information
trueChazza committed May 5, 2023
1 parent f828ab6 commit 5b56fb8
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions lib/media_server_web/templates/watch/watch.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,54 @@

<script>
window['__onGCastApiAvailable'] = function(isAvailable) {
if (isAvailable) {
cast.framework.CastContext.getInstance().setOptions({
receiverApplicationId: 'B7C82081',
autoJoinPolicy: chrome.cast.AutoJoinPolicy.PAGE_SCOPED
const context = cast.framework.CastContext.getInstance();
context.setOptions({
receiverApplicationId: 'B7C82081',
autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED
});
// Check if there is an existing session before starting a new one
const session = context.getCurrentSession();
if (session) {
console.log('Using existing session:', session);
loadMedia(session);
} else {
context.addEventListener(cast.framework.CastContextEventType.SESSION_STATE_CHANGED, function(event) {
if (event.sessionState === cast.framework.SessionState.SESSION_STARTED) {
console.log('New session started:', event.session);
loadMedia(event.session);
} else if (event.sessionState === cast.framework.SessionState.SESSION_ENDED ||
event.sessionState === cast.framework.SessionState.SESSION_ERROR) {
console.log('Session ended:', event.session);
}
});
context.requestSession().then(function(session) {
console.log('New session requested:', session);
loadMedia(session);
}).catch(function(error) {
console.log('Error requesting session:', error);
});
}
cast.framework.CastContext.getInstance().addEventListener(cast.framework.CastContextEventType.CAST_STATE_CHANGED, function({ castState }) {
if (castState === 'CONNECTED') {
const mediaInfo = new chrome.cast.media.MediaInfo('<%= System.get_env("APP_URL", "") <> assigns[:media_stream] %>', '<%= assigns[:mime_type] %>');
const request = new chrome.cast.media.LoadRequest(mediaInfo);
function loadMedia(session) {
const mediaInfo = new chrome.cast.media.MediaInfo('<%= System.get_env("APP_URL", "") <> assigns[:media_stream] %>', '<%= assigns[:mime_type] %>');
const request = new chrome.cast.media.LoadRequest(mediaInfo);
cast.framework.CastContext.getInstance().getCurrentSession().loadMedia(request).then(
function() {
console.log('Cast media loaded');
},
function(errorCode) {
console.log(errorCode);
});
}
session.loadMedia(request).then(function() {
console.log('Media loaded');
}).catch(function(error) {
console.log('Error loading media:', error);
});
}
}
}
</script>
Expand Down

0 comments on commit 5b56fb8

Please sign in to comment.