Skip to content

Commit

Permalink
try fix cast
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyiya committed May 5, 2024
1 parent de7b414 commit 77219c2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 69 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const defaultOptions = {
muted: false,
loop: false,
volume: 1,
preload: 'metadata',
preload: '',
playbackRate: 1,
playsinline: true,
lang: 'auto',
Expand Down
13 changes: 3 additions & 10 deletions packages/docs/public/oplayer.html
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected].1/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected].2/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/index.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/airplay.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected].0/dist/chromecast.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@oplayer/[email protected].1/dist/chromecast.min.js"></script>

<script>
var playlistScriptCdn = 'https://cdn.jsdelivr.net/npm/@oplayer/[email protected]/dist/playlist.min.js'
Expand Down Expand Up @@ -315,14 +315,7 @@
ODash({ library: 'https://cdn.dashjs.org/latest/dash.all.min.js' }),
OMpegts({ library: 'https://cdn.jsdelivr.net/npm/mpegts.js/dist/mpegts.min.js' }),
new OAirplay(),
new OChromecast({
loggerLevel: 0,
loadRequestBuilder(r) {
r.media.hlsSegmentFormat = 'fmp4'
r.media.hlsVideoSegmentFormat = 'fmp4'
return r
}
})
new OChromecast()
])
.create()
.on((e) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/hls/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oplayer/hls",
"version": "1.2.26-beta.1",
"version": "1.2.26-beta.2",
"description": "Hls plugin for oplayer",
"type": "module",
"main": "./dist/index.es.js",
Expand Down
17 changes: 8 additions & 9 deletions packages/hls/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { loadSDK, type Player, type PlayerPlugin, type RequiredPartial, type Source } from '@oplayer/core'
import type Hls from 'hls.js'
import type { ErrorData, HlsConfig, LevelSwitchedData, MediaAttachedData } from 'hls.js'
import type { ErrorData, HlsConfig, LevelSwitchedData } from 'hls.js'

const PLUGIN_NAME = 'oplayer-plugin-hls'

Expand Down Expand Up @@ -141,15 +141,14 @@ class HlsPlugin implements PlayerPlugin {
active(instance, HlsPlugin.library)
}

instance.once(HlsPlugin.library.Events.MEDIA_ATTACHED, (_, { media }: MediaAttachedData) => {
const $source = document.createElement('source') as HTMLSourceElement
$source.src = source.src
$source.type = source.type || (source.type = 'application/x-mpegurl')
media.appendChild($source)
const $source = document.createElement('source')
$source.setAttribute('src', source.src)
$source.setAttribute('type', source.type || (source.type = 'application/x-mpegurl'))
$source.setAttribute('data-hls', '')
$video.append($source)

instance.once(HlsPlugin.library.Events.DESTROYING, () => {
$source.remove()
})
instance.once(HlsPlugin.library.Events.DESTROYING, () => {
$source.remove()
})

instance.on(HlsPlugin.library.Events.ERROR, function (_, data) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oplayer/plugins",
"version": "1.0.13-chromecast.0",
"version": "1.0.13-chromecast.1",
"author": "shiyiya",
"description": "oplayer's plugin",
"homepage": "https://github.com/shiyiya/oplayer",
Expand Down
60 changes: 13 additions & 47 deletions packages/plugins/src/chromecast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@ export interface ChromeCastOptions {
resumeSavedSession?: boolean | undefined
/** The following flag enables Cast Connect(requires Chrome 87 or higher) */
androidReceiverCompatible?: boolean | undefined
/**
* DEBUG: 0, INFO: 800, WARNING: 900, ERROR: 1000, NONE: 1500
*/
loggerLevel: number
loadRequestBuilder: (request: chrome.cast.media.LoadRequest) => chrome.cast.media.LoadRequest
}

class ChromeCast implements PlayerPlugin {
public name = 'oplayer-plugin-chromecast'
public version = __VERSION__
readonly name = 'oplayer-plugin-chromecast'
readonly version = __VERSION__

public player: Player
public _player?: cast.framework.RemotePlayer
protected _player?: cast.framework.RemotePlayer

constructor(public options?: ChromeCastOptions) {}

Expand Down Expand Up @@ -76,15 +71,17 @@ class ChromeCast implements PlayerPlugin {

this.cast.setOptions({
receiverApplicationId: window.chrome.cast.media.DEFAULT_MEDIA_RECEIVER_APP_ID,
autoJoinPolicy: chrome.cast.AutoJoinPolicy.TAB_AND_ORIGIN_SCOPED,
autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED,
resumeSavedSession: true,
androidReceiverCompatible: true,
...this.options
})

//@ts-ignore
const errorCode = (await chrome.cast.requestSession()) as chrome.cast.ErrorCode
const errorCode = await this.cast.requestSession()
if (errorCode) {
throw new Error(`Chrome Cast Error Code: ${errorCode}`)
}

return this.cast.getCurrentSession()?.loadMedia(this.__buildRequest())
}

Expand Down Expand Up @@ -117,57 +114,26 @@ class ChromeCast implements PlayerPlugin {
request.autoplay = this.player.isPlaying
request.currentTime = this.player.currentTime

return this.options?.loadRequestBuilder(request) || request
return request
}

_loadCast() {
async _loadCast() {
if (!!window.cast?.framework) return

return new Promise<void>((resolve, reject) => {
window.__onGCastApiAvailable = (isAvailable) => {
window.__onGCastApiAvailable == undefined

if (isAvailable) {
if (typeof this.options?.loggerLevel != 'undefined') {
cast.framework.setLoggerLevel(this.options.loggerLevel)
}
resolve()
} else {
reject(Error('CAST_NOT_AVAILABLE'))
}
}

loadSDK(
'https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1',
'cast',
'__onGCastApiAvailable'
)
})
await loadSDK('https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1', 'cast')
await customElements.whenDefined('google-cast-launcher')
}

async start() {
try {
await this._loadCast()

// const State = window.cast.framework.CastState
// if (this.cast.getCastState() === State.NO_DEVICES_AVAILABLE) {
// throw new Error(`Chrome Cast Error Code: ${State.NO_DEVICES_AVAILABLE}`)
// }

const errorCode = await this.__requestChromeCast()
if (errorCode) {
throw new Error(`Chrome Cast Error Code: ${errorCode}`)
}
} catch (error) {
let msg = 'UNKNOWN ERROR'

if (error instanceof Error) {
msg = error.message
} else if (!!chrome.cast && error instanceof chrome.cast.Error) {
msg = `${error.code} - ${error.description} \n ${error.details}`
}

this.player.emit('notice', { text: msg })
this.player.emit('cast-error', error)
}
}

Expand Down

0 comments on commit 77219c2

Please sign in to comment.