Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gallayl committed Aug 15, 2024
1 parent b471ade commit c299bdc
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 56 deletions.
2 changes: 1 addition & 1 deletion frontend/src/pages/movies/movie-player-v2/control-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const SoundControl = Shade<{
min="0"
max="100"
value={volume.toString()}
onchange={(e) => props.volume.setValue((e.target as HTMLInputElement).value as any)}
onchange={(e) => props.volume.setValue((e.target as HTMLInputElement).value as unknown as number)}
/>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export const getSubtitleTracks = (file: PiRatFile, ffProbeData: FfprobeData) =>
.map((subtitle) => (
<track
kind="captions"
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
label={subtitle.tags.title || subtitle.tags.language || subtitle.tags.filename}
src={`${environmentOptions.serviceUrl}/drives/files/${encodeURIComponent(
driveLetter,
)}/${encodeURIComponent(`${parentPath}/${fileName}-subtitle-${subtitle.index}.vtt`)}/download`}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
srclang={subtitle.tags.language}
/>
)) || []
Expand Down
52 changes: 26 additions & 26 deletions frontend/src/pages/movies/movie-player-v2/movie-player-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ScopedLogger } from '@furystack/logging'
import { ObservableValue, type Disposable } from '@furystack/utils'
import { ObservableValue } from '@furystack/utils'
import type { PiRatFile } from 'common'
import type { FfprobeData } from 'fluent-ffmpeg'
import { Lock } from 'semaphore-async-await'
Expand All @@ -21,7 +21,7 @@ export const audioCodecs = {
dts: 'dts+',
}

export class MoviePlayerService implements Disposable {
export class MoviePlayerService implements AsyncDisposable {
constructor(
private readonly file: PiRatFile,
private readonly ffprobe: FfprobeData,
Expand All @@ -36,40 +36,40 @@ export class MoviePlayerService implements Disposable {
this.url = URL.createObjectURL(this.MediaSource)

this.MediaSource.addEventListener('sourceopen', () => {
this.logger.verbose({ message: 'MediaSource opened' })
void this.logger.verbose({ message: 'MediaSource opened' })
this.MediaSource.duration = this.ffprobe.format.duration || 0
})

this.MediaSource.addEventListener('sourceclose', () => {
this.logger.verbose({ message: 'MediaSource closed' })
void this.logger.verbose({ message: 'MediaSource closed' })
})

this.MediaSource.addEventListener('sourceended', () => {
this.logger.verbose({ message: 'MediaSource ended' })
void this.logger.verbose({ message: 'MediaSource ended' })
})

this.chunkLength = 2

this.loadChunkForProgress(this.currentProgress)
void this.loadChunkForProgress(this.currentProgress)
}

public readonly MediaSource: MediaSource
public readonly url: string
private loadLock = new Lock()
public audioTrackId = new ObservableValue(0)
public async dispose() {
this.progress.dispose()
this.bufferZoneChangeSubscription.dispose()
this.bufferZones.dispose()
this.gapsInBuffersChangeSubscription.dispose()
this.gapsInBuffers.dispose()
this.lastLoadTime.dispose()
public async [Symbol.asyncDispose]() {
this.progress[Symbol.dispose]()
this.bufferZoneChangeSubscription[Symbol.dispose]()
this.bufferZones[Symbol.dispose]()
this.gapsInBuffersChangeSubscription[Symbol.dispose]()
this.gapsInBuffers[Symbol.dispose]()
this.lastLoadTime[Symbol.dispose]()
this.MediaSource.endOfStream()
;[...this.MediaSource.sourceBuffers].forEach((sb) => {
try {
this.MediaSource.removeSourceBuffer(sb)
} catch (error) {
this.logger.error({ message: 'Error disposing MediaSource Buffer', data: { error } })
void this.logger.error({ message: 'Error disposing MediaSource Buffer', data: { error } })
}
})
}
Expand All @@ -88,19 +88,19 @@ export class MoviePlayerService implements Disposable {
})

private bufferZoneChangeSubscription = this.bufferZones.subscribe((next) => {
this.logger.verbose({ message: 'Buffer zones changed', data: { next } })
void this.logger.verbose({ message: 'Buffer zones changed', data: { next } })
})

private gapsInBuffersChangeSubscription = this.gapsInBuffers.subscribe((next) => {
this.logger.verbose({ message: 'Gaps in buffers changed', data: { next } })
void this.logger.verbose({ message: 'Gaps in buffers changed', data: { next } })
})

private getActiveSourceBuffer = () => {
const existing = this.MediaSource.activeSourceBuffers[0]
if (existing) {
return existing
}
this.logger.verbose({ message: 'Creating new source buffer' })
void this.logger.verbose({ message: 'Creating new source buffer' })
const newSourceBuffer = this.MediaSource.addSourceBuffer(this.getMimeType())
newSourceBuffer.mode = 'segments'
newSourceBuffer.timestampOffset = this.progress.getValue()
Expand Down Expand Up @@ -141,7 +141,7 @@ export class MoviePlayerService implements Disposable {
public async loadChunkForProgress(progress: number) {
const from = this.getSegmentStartForProgress(progress)
const to = from + this.chunkLength
this.logger.verbose({ message: `Loading chunk from ${from} to ${to}` })
await this.logger.verbose({ message: `Loading chunk from ${from} to ${to}` })
try {
await this.loadLock.acquire()
const start = new Date().getTime()
Expand Down Expand Up @@ -194,11 +194,11 @@ export class MoviePlayerService implements Disposable {
const end = new Date().getTime()
this.lastLoadTime.setValue((end - start) / 1000)
} catch (error) {
this.logger.error({ message: 'Chunk loading error', data: { error } })
await this.logger.error({ message: 'Chunk loading error', data: { error } })
} finally {
this.loadLock.release()
}
this.logger.verbose({ message: `Loading ${from}-${to} loading finished` })
await this.logger.verbose({ message: `Loading ${from}-${to} loading finished` })
}

public progress: ObservableValue<number>
Expand All @@ -211,18 +211,18 @@ export class MoviePlayerService implements Disposable {
this.updateBufferZones()
const sb = this.getActiveSourceBuffer()
if (!sb.buffered.length) {
this.logger.information({ message: 'No buffered data, loading a chunk...', data: { progress } })
this.loadChunkForProgress(progress)
void this.logger.information({ message: 'No buffered data, loading a chunk...', data: { progress } })
void this.loadChunkForProgress(progress)
return
}

const isInGap = this.gapsInBuffers.getValue().some(([start, end]) => progress >= start && progress <= end)
if (isInGap) {
if (this.loadLock.getPermits()) {
this.logger.information({
void this.logger.information({
message: 'Progress inside a buffer gap',
})
this.loadChunkForProgress(progress)
void this.loadChunkForProgress(progress)
}
}

Expand All @@ -233,10 +233,10 @@ export class MoviePlayerService implements Disposable {

if (isGapApproaching) {
if (this.loadLock.getPermits()) {
this.logger.information({
void this.logger.information({
message: 'Gap approaching, write queue clear, loading a chunk...',
})
this.loadChunkForProgress(isGapApproaching[0])
void this.loadChunkForProgress(isGapApproaching[0])
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const MoviePlayerV2 = Shade<MoviePlayerProps>({
return new WatchProgressUpdater({
intervalMs: 10 * 1000,
onSave: async (progress) => {
watchProgressService.updateWatchEntry({
void watchProgressService.updateWatchEntry({
completed: video.duration - progress < 10,
driveLetter,
path,
Expand All @@ -42,7 +42,7 @@ export const MoviePlayerV2 = Shade<MoviePlayerProps>({
})

return () => {
watchProgressUpdater.dispose()
void watchProgressUpdater[Symbol.asyncDispose]()
}
},
render: ({ props, element, useDisposable, injector }) => {
Expand All @@ -57,7 +57,7 @@ export const MoviePlayerV2 = Shade<MoviePlayerProps>({
() => new MoviePlayerService(file, props.ffprobe, api, watchProgress?.watchedSeconds || 0, logger),
)

mediaService.MediaSource.addEventListener('sourceopen', async () => {
mediaService.MediaSource.addEventListener('sourceopen', () => {
mediaService.MediaSource.duration = props.ffprobe.format.duration || 0
})

Expand All @@ -67,7 +67,7 @@ export const MoviePlayerV2 = Shade<MoviePlayerProps>({
const createTimedOutHide = () =>
setTimeout(() => {
element.querySelectorAll('.hideOnPlay').forEach((el) => {
promisifyAnimation(
void promisifyAnimation(
el,
[
{
Expand All @@ -91,7 +91,7 @@ export const MoviePlayerV2 = Shade<MoviePlayerProps>({
const onMouseMove = () => {
clearTimeout(timeoutId)
element.querySelectorAll('.hideOnPlay').forEach((el) => {
promisifyAnimation(
void promisifyAnimation(
el,
[
{
Expand All @@ -113,7 +113,7 @@ export const MoviePlayerV2 = Shade<MoviePlayerProps>({
element.addEventListener('mousemove', onMouseMove)

return {
dispose: () => {
[Symbol.dispose]: () => {
element.removeEventListener('mousemove', onMouseMove)
},
}
Expand Down Expand Up @@ -142,12 +142,12 @@ export const MoviePlayerV2 = Shade<MoviePlayerProps>({
objectFit: 'cover',
}}
ontimeupdate={(ev) => {
const currentTime = (ev.currentTarget as HTMLVideoElement).currentTime as number
const { currentTime } = ev.currentTarget as HTMLVideoElement
mediaService.progress.setValue(currentTime || 0)
}}
onseeked={(ev) => {
const currentTime = (ev.currentTarget as HTMLVideoElement).currentTime as number
mediaService.loadChunkForProgress(currentTime)
const { currentTime } = ev.currentTarget as HTMLVideoElement
void mediaService.loadChunkForProgress(currentTime)
}}
currentTime={watchProgress?.watchedSeconds || 0}
src={mediaService.url}
Expand Down
2 changes: 1 addition & 1 deletion service/src/ffprobe-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class FfprobeService {
capacity: 100,
load: async (fullPath: string) => {
const ffprobeResult = await new Promise<FfprobeData>((resolve, reject) =>
fluendFfmpeg.ffprobe(fullPath, (err, data) => (err ? reject(err) : resolve(data))),
fluendFfmpeg.ffprobe(fullPath, (err, data) => (err ? reject(err as Error) : resolve(data))),
)
return ffprobeResult
},
Expand Down
6 changes: 3 additions & 3 deletions service/src/media/actions/stream-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const StreamAction: RequestAction<StreamEndpoint> = async ({ injector, ge
const audioStream = audioStreams[audio?.trackId || 0]
const videoStream = ffprobe.streams.find((stream) => stream.codec_type === 'video')

logger.information({
await logger.information({
message: `Starting stream from ${from} to ${to}, transcodeVideo: ${!!video?.codec}, transcodeAudio: ${!!audio?.audioCodec}`,
data: { fullPath, from, to, audio, video, audioStream, videoStream },
})
Expand Down Expand Up @@ -107,7 +107,7 @@ export const StreamAction: RequestAction<StreamEndpoint> = async ({ injector, ge
}

command.on('start', (commandLine) => {
logger.verbose({ message: `Spawned Ffmpeg with command: ${commandLine}` })
void logger.verbose({ message: `Spawned Ffmpeg with command: ${commandLine}` })
})

try {
Expand All @@ -123,7 +123,7 @@ export const StreamAction: RequestAction<StreamEndpoint> = async ({ injector, ge
})
.pipe(response, { end: true })
} catch (error) {
logger.error({ message: 'Stream error', data: { error } })
await logger.error({ message: 'Stream error', data: { error } })
}

return BypassResult()
Expand Down
Loading

0 comments on commit c299bdc

Please sign in to comment.