Skip to content

Commit

Permalink
cleanup: use hls.js esm build, save a few kb (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam authored Jan 31, 2025
1 parent 28efcf2 commit 2ba91c8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@solidjs/router": "^0.15.2",
"clsx": "^2.1.1",
"dayjs": "^1.11.13",
"hls.js": "^1.5.18",
"hls.js": "^1.5.20",
"leaflet": "^1.9.4",
"qr-scanner": "^1.4.2",
"solid-js": "^1.9.4"
Expand Down
39 changes: 17 additions & 22 deletions src/components/RouteVideoPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call */
import { createEffect, createResource, onCleanup, onMount } from 'solid-js'
import type { VoidComponent } from 'solid-js'
import { createEffect, createResource, onCleanup, onMount, type VoidComponent } from 'solid-js'
import clsx from 'clsx'
import Hls from 'hls.js/dist/hls.light.min.js'
import Hls from 'hls.js/dist/hls.light.mjs'

import { getQCameraStreamUrl } from '~/api/route'

type RouteVideoPlayerProps = {
Expand All @@ -14,32 +13,28 @@ type RouteVideoPlayerProps = {

const RouteVideoPlayer: VoidComponent<RouteVideoPlayerProps> = (props) => {
const [streamUrl] = createResource(() => props.routeName, getQCameraStreamUrl)
let video: HTMLVideoElement
let video!: HTMLVideoElement

onMount(() => {
const timeUpdate = () => props.onProgress?.(video.currentTime)
video.addEventListener('timeupdate', timeUpdate)
onCleanup(() => video.removeEventListener('timeupdate', timeUpdate))
if (props.ref) {
props.ref(video)
}
props.ref?.(video)
})
let hls = new Hls()

createEffect(() => {
hls?.destroy()
if (streamUrl()) {
if (Hls.isSupported()) {
hls = new Hls()
hls.loadSource(streamUrl()!)
hls.attachMedia(video)
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = streamUrl()!
} else {
console.error('Browser does not support hls')
}
if (!streamUrl()) return
if (Hls.isSupported()) {
const hls = new Hls()
hls.loadSource(streamUrl()!)
hls.attachMedia(video)
onCleanup(() => hls.destroy())
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = streamUrl()!
} else {
console.error('Browser does not support hls')
}
})
onCleanup(() => hls?.destroy())

return (
<div
Expand All @@ -49,7 +44,7 @@ const RouteVideoPlayer: VoidComponent<RouteVideoPlayerProps> = (props) => {
)}
>
<video
ref={video!}
ref={video}
class="absolute inset-0 size-full object-cover"
autoplay
muted
Expand Down
5 changes: 4 additions & 1 deletion src/hls-light-types.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
declare module 'hls.js/dist/hls.light.min.js'
declare module 'hls.js/dist/hls.light.mjs' {
import hls from 'hls.js'
export default hls
}

0 comments on commit 2ba91c8

Please sign in to comment.