-
Notifications
You must be signed in to change notification settings - Fork 564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DEPRECATED] Chromecast Plugin #296
Draft
Araxeus
wants to merge
17
commits into
th-ch:master
Choose a base branch
from
Araxeus:chromecast
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+409
−25
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
def0d18
chromecast first try
Araxeus 598abf3
log more info
Araxeus f35bc81
update broken music.youtube detection
Araxeus a4f06f5
added messagebox debugging
Araxeus ba87b35
Chromecast v0.9.0
Araxeus 2f55710
Chromecast v0.9.1
Araxeus 713120f
chromecast v1.0.0
Araxeus 3d5b51b
lint
Araxeus 48fc2cc
add defaults options to chromecast
Araxeus 599d9c3
Merge remote-tracking branch 'upstream/master' into chromecast
Araxeus 1484dd0
update `registerCallback()` in chromecast plugin
Araxeus 9d22a2b
remove redundant passive listener attribute
Araxeus 1fc8f7a
Merge branch 'master' into chromecast
Araxeus 0bdd6fa
chromecast fixes
Araxeus 81c48d4
force dns-packet version resolution
Araxeus 8dc1ab0
Update yarn.lock
Araxeus 207a058
lint
Araxeus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
const registerCallback = require("../../providers/song-info"); | ||
const getSongControls = require('../../providers/song-controls'); | ||
|
||
const { ipcMain } = require('electron') | ||
const ChromecastAPI = require('chromecast-api'); | ||
const { setOptions } = require("../../config/plugins"); | ||
|
||
let client; | ||
let deviceList = []; | ||
|
||
let controls; | ||
|
||
let options; | ||
|
||
module.exports = (win, initialOptions) => { | ||
const { play, pause } = getSongControls(win); | ||
controls = { play, pause }; | ||
|
||
options = initialOptions; | ||
|
||
client = new ChromecastAPI(); | ||
|
||
client.on('device', (device) => { | ||
if (!deviceList.includes(device.name)) { | ||
registerDevice(device); | ||
} | ||
}); | ||
ipcMain.on('volume-change', (_, v) => setVolume(v)); | ||
ipcMain.on('seeked-to', (_, s) => seekTo(s + 1.5)); | ||
}; | ||
|
||
function setVolume(volume) { | ||
if (options.syncVolume) { | ||
for (const device of client.devices) { | ||
device.setVolume(volume); | ||
} | ||
} | ||
} | ||
|
||
function seekTo(seconds) { | ||
if (options.syncSeek) { | ||
for (const device of client.devices) { | ||
device.seekTo(seconds); | ||
} | ||
} | ||
} | ||
|
||
function registerDevice(device) { | ||
deviceList.push(device.name); | ||
let currentStatus; | ||
device.on('status', async (status) => { | ||
currentStatus = status.playerState; | ||
if (options.syncStartTime) { | ||
currentStatus === "PLAYING" ? controls.play() : controls.pause(); | ||
} | ||
}) | ||
|
||
let currentUrl; | ||
let isPaused; | ||
registerCallback(songInfo => { | ||
if (!songInfo?.title) { | ||
return; | ||
} | ||
if (currentUrl !== songInfo.url) { //new song | ||
currentUrl = songInfo.url; | ||
if (options.syncStartTime) { | ||
isPaused = true; | ||
controls.pause(); | ||
} else { | ||
isPaused = songInfo.isPaused; | ||
} | ||
device.play(transformURL(songInfo.url)); | ||
|
||
} else if (isPaused !== songInfo.isPaused) { //paused status changed | ||
isPaused = songInfo.isPaused; | ||
if (isPaused && currentStatus === "PLAYING") { | ||
device.pause(); | ||
} else { | ||
device.resume(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
// will not be needed after https://github.com/alxhotel/chromecast-api/pull/69 (chromecastAPI v0.3.5) | ||
function transformURL(url) { | ||
const videoId = url.match(/(?:http(?:s?):\/\/)?(?:www\.)?(?:music\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)/); | ||
// videoId[1] should always be valid since regex should always be valid - rickroll video should never happen :) | ||
return "https://youtube.com/watch?v=" + (videoId.length > 1 ? videoId[1] : "dQw4w9WgXcQ"); | ||
} | ||
|
||
module.exports.setOption = (value, ...keys) => { | ||
for (const key of keys) { | ||
options[key] = value; | ||
} | ||
setOptions("chromecast", options); | ||
}; | ||
|
||
module.exports.menuCheck = (options_) => { | ||
if (!options) options = options_; | ||
} | ||
|
||
module.exports.refreshChromecast = () => { | ||
if (client) client.update(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { ipcRenderer } = require('electron'); | ||
|
||
const sendVolume = (volume) => ipcRenderer.send('volume-change', volume); | ||
const sendTime = (seconds) => ipcRenderer.send('seeked-to', seconds); | ||
|
||
module.exports = function checkVideoLoaded() { | ||
const video = document.querySelector("video"); | ||
video ? setup(video) : setTimeout(checkVideoLoaded, 500); | ||
} | ||
|
||
function setup(video) { | ||
|
||
sendVolume(video.volume); | ||
|
||
video.addEventListener('volumechange', e => | ||
sendVolume(e.target.muted ? 0 : e.target.volume)); | ||
|
||
video.addEventListener('seeking', e => | ||
sendTime(e.target.currentTime)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const { menuCheck, setOption, refreshChromecast } = require("./back"); | ||
|
||
module.exports = (_win, options) => { | ||
menuCheck(options); | ||
return [ | ||
{ | ||
label: "Sync Volume", | ||
type: "checkbox", | ||
checked: !!options.syncVolume, | ||
click: (item) => setOption(item.checked, "syncVolume") | ||
}, | ||
{ | ||
label: "Sync Start Time", | ||
type: "checkbox", | ||
checked: !!options.syncStartTime, | ||
click: (item) => setOption(item.checked, "syncStartTime") | ||
}, | ||
{ | ||
label: "Sync Seek", | ||
type: "checkbox", | ||
checked: !!options.syncSeek, | ||
click: (item) => setOption(item.checked, "syncSeek") | ||
}, | ||
{ type: "separator" }, | ||
{ | ||
label: "Refresh Device List", | ||
click: refreshChromecast | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,37 @@ | ||
const { ipcRenderer } = require("electron"); | ||
|
||
let videoStream = document.querySelector(".video-stream"); | ||
module.exports = () => { | ||
ipcRenderer.on("playPause", () => { | ||
if (!videoStream) { | ||
videoStream = document.querySelector(".video-stream"); | ||
} | ||
let video = document.querySelector("video"); | ||
|
||
if (videoStream.paused) { | ||
videoStream.play(); | ||
} else { | ||
videoStream.yns_pause ? | ||
videoStream.yns_pause() : | ||
videoStream.pause(); | ||
} | ||
}); | ||
module.exports = () => { | ||
ipcRenderer.on("playPause", (_e, toPlay) => playPause(toPlay)); | ||
}; | ||
module.exports.playPause = playPause; | ||
|
||
function playPause(toPlay = undefined) { | ||
if (!checkVideo()) return; | ||
|
||
switch (toPlay) { | ||
case true: | ||
video.play(); | ||
break; | ||
case false: | ||
pause(); | ||
break; | ||
default: //usually undefined | ||
video.paused ? video.play() : pause(); | ||
} | ||
|
||
function pause() { | ||
video.yns_pause ? | ||
video.yns_pause() : | ||
video.pause(); | ||
} | ||
} | ||
|
||
function checkVideo() { | ||
if (!video) { | ||
video = document.querySelector("video"); | ||
} | ||
|
||
return !!video; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice Rickroll 🙃 Maybe worth a comment in code, I wondered why this hardcoded ID was there at first 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a comment already 😅
I thought the module would update its version faster - and this method will be unneeded
but sadly it hasn't happened yet - the fix pr was merged but the npm version wasn't updated yet
(method should only be called when songInfo is updated which means the url regex is valid - which means you should never get rick-rolled 😝)