Skip to content

Commit

Permalink
added chromecast support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitrios Panteleimon Giakatos committed Jul 23, 2020
1 parent cb155ab commit 44a3cd9
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function initPlayers() {
container: document.getElementById('dplayer1'),
preload: 'none',
screenshot: true,
chromecast: true,
video: {
url: 'https://api.dogecloud.com/player/get.mp4?vcode=5ac682e6f8231991&userId=17&ext=.mp4',
pic: 'https://i.loli.net/2019/06/06/5cf8c5d9c57b510947.png',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"dependencies": {
"axios": "0.19.2",
"balloon-css": "^1.0.3",
"promise-polyfill": "8.1.3"
"promise-polyfill": "8.1.3",
"rxjs": "^6.6.0"
}
}
1 change: 1 addition & 0 deletions src/assets/chromecast.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/css/player.scss
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
.dplayer-volume,
.dplayer-camera-icon,
.dplayer-airplay-icon,
.dplayer-chromecast-icon,
.dplayer-play-icon {
display: none;
}
Expand Down
90 changes: 90 additions & 0 deletions src/js/controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import utils from './utils';
import Thumbnails from './thumbnails';
import Icons from './icons';
import { Subject } from 'rxjs';

let cast;
let runOnce = true;
let isCasting = false;

class Controller {
constructor(player) {
Expand Down Expand Up @@ -31,6 +36,7 @@ class Controller {
this.initSubtitleButton();
this.initHighlights();
this.initAirplayButton();
this.initChromecastButton();
if (!utils.isMobile) {
this.initVolumeButton();
}
Expand Down Expand Up @@ -279,6 +285,90 @@ class Controller {
}
}

initChromecast() {
const script = window.document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1');
window.document.body.appendChild(script);

window.__onGCastApiAvailable = (isAvailable) => {
if (isAvailable) {
cast = window.chrome.cast;
const sessionRequest = new cast.SessionRequest(cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID);
const apiConfig = new cast.ApiConfig(
sessionRequest,
() => {},
(status) => {
if (status === cast.ReceiverAvailability.AVAILABLE) {
console.log('chromecast: ', status);
}
}
);
cast.initialize(apiConfig, () => {});
}
};
}

initChromecastButton() {
if (this.player.options.chromecast) {
if (runOnce) {
runOnce = false;
this.initChromecast();
}
const discoverDevices = () => {
const subj = new Subject();
cast.requestSession(
(s) => {
this.session = s;
subj.next('CONNECTED');
launchMedia(this.player.options.video.url);
},
(err) => {
if (err.code === 'cancel') {
this.session = undefined;
subj.next('CANCEL');
} else {
console.error('Error selecting a cast device', err);
}
}
);
return subj;
};

const launchMedia = (media) => {
const mediaInfo = new cast.media.MediaInfo(media);
const request = new cast.media.LoadRequest(mediaInfo);

if (!this.session) {
window.open(media);
return false;
}
this.session.loadMedia(request, onMediaDiscovered.bind(this, 'loadMedia'), onMediaError).play();
return true;
};

const onMediaDiscovered = (how, media) => {
this.currentMedia = media;
};

const onMediaError = (err) => {
console.error('Error launching media', err);
};

this.player.template.chromecastButton.addEventListener('click', () => {
if (isCasting) {
isCasting = false;
this.currentMedia.stop();
this.session.stop();
this.initChromecast();
} else {
isCasting = true;
discoverDevices();
}
});
}
}

initSubtitleButton() {
if (this.player.options.subtitle) {
this.player.events.on('subtitle_show', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/js/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const tranTxt = {
Send: '发送',
Screenshot: '截图',
AirPlay: '无线投屏',
ChromeCast: 'ChromeCast',
s: '秒',
'Show subtitle': '显示字幕',
'Hide subtitle': '隐藏字幕',
Expand Down Expand Up @@ -93,6 +94,7 @@ const tranTxt = {
Send: '發送',
Screenshot: '截圖',
AirPlay: '無線投屏',
ChromeCast: 'ChromeCast',
s: '秒',
'Show subtitle': '顯示字幕',
'Hide subtitle': '隱藏字幕',
Expand Down
2 changes: 2 additions & 0 deletions src/js/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import camera from '../assets/camera.svg';
import airplay from '../assets/airplay.svg';
import subtitle from '../assets/subtitle.svg';
import loading from '../assets/loading.svg';
import chromecast from '../assets/chromecast.svg';

const Icons = {
play: play,
Expand All @@ -34,6 +35,7 @@ const Icons = {
subtitle: subtitle,
loading: loading,
airplay: airplay,
chromecast: chromecast,
};

export default Icons;
1 change: 1 addition & 0 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default (options) => {
lang: (navigator.language || navigator.browserLanguage).toLowerCase(),
screenshot: false,
airplay: true,
chromecast: false,
hotkey: true,
preload: 'metadata',
volume: 0.7,
Expand Down
2 changes: 2 additions & 0 deletions src/js/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Template {
pic: this.options.video.pic,
screenshot: this.options.screenshot,
airplay: this.options.airplay,
chromecast: this.options.chromecast,
preload: this.options.preload,
url: this.options.video.url,
subtitle: this.options.subtitle,
Expand Down Expand Up @@ -80,6 +81,7 @@ class Template {
this.qualityList = this.container.querySelector('.dplayer-quality-list');
this.camareButton = this.container.querySelector('.dplayer-camera-icon');
this.airplayButton = this.container.querySelector('.dplayer-airplay-icon');
this.chromecastButton = this.container.querySelector('.dplayer-chromecast-icon');
this.subtitleButton = this.container.querySelector('.dplayer-subtitle-icon');
this.subtitleButtonInner = this.container.querySelector('.dplayer-subtitle-icon .dplayer-icon-content');
this.subtitle = this.container.querySelector('.dplayer-subtitle');
Expand Down
5 changes: 5 additions & 0 deletions src/template/player.art
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<span class="dplayer-icon-content">{{@ icons.airplay }}</span>
</div>
{{ /if }}
{{ if options.chromecast }}
<div class="dplayer-icon dplayer-chromecast-icon" data-balloon="{{ tran('ChromeCast') }}" data-balloon-pos="up">
<span class="dplayer-icon-content">{{@ icons.chromecast }}</span>
</div>
{{ /if }}
<div class="dplayer-comment">
<button class="dplayer-icon dplayer-comment-icon" data-balloon="{{ tran('Send danmaku') }}" data-balloon-pos="up">
<span class="dplayer-icon-content">{{@ icons.comment }}</span>
Expand Down

0 comments on commit 44a3cd9

Please sign in to comment.