Skip to content

Commit

Permalink
add pipeline classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Dashkevich committed Oct 16, 2023
1 parent ef6fc74 commit 499a6ed
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 5 deletions.
Empty file removed src/pipelines/base.ts
Empty file.
4 changes: 4 additions & 0 deletions src/pipelines/basePipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default abstract class Pipeline {
public abstract loadRemoteAsset(uri: string): void;
public abstract loadLocalAsset(asset: string | ArrayBuffer): void;
}
Empty file removed src/pipelines/dash/.gitkeep
Empty file.
Empty file removed src/pipelines/hls/.gitkeep
Empty file.
11 changes: 11 additions & 0 deletions src/pipelines/mse/dash/dashPipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Pipeline from '@/pipelines/basePipeline.ts';

export default class DashPipeline extends Pipeline {
public loadLocalAsset(asset: string | ArrayBuffer): void {
//TODO
}

public loadRemoteAsset(uri: string): void {
//TODO
}
}
11 changes: 11 additions & 0 deletions src/pipelines/mse/hls/hlsPipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Pipeline from '@/pipelines/basePipeline.ts';

export default class HlsPipeline extends Pipeline {
public loadLocalAsset(asset: string | ArrayBuffer): void {
//TODO
}

public loadRemoteAsset(uri: string): void {
//TODO
}
}
7 changes: 7 additions & 0 deletions src/pipelines/mse/streamingPipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Pipeline from '@/pipelines/basePipeline.ts';

export default abstract class MsePipeLine extends Pipeline {
public abstract loadLocalAsset(asset: string | ArrayBuffer): void;

public abstract loadRemoteAsset(uri: string): void;
}
Empty file removed src/pipelines/native/.gitkeep
Empty file.
11 changes: 11 additions & 0 deletions src/pipelines/native/nativePipeline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Pipeline from '@/pipelines/basePipeline.ts';

export default class NativePipeline extends Pipeline {
public loadLocalAsset(asset: string | ArrayBuffer): void {
//TODO
}

public loadRemoteAsset(uri: string): void {
//TODO
}
}
20 changes: 20 additions & 0 deletions src/player/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export enum ErrorCategory {
Pipeline,
}

export enum ErrorCode {
// pipeline
NoSupportedPipelines,
}

export default abstract class PlayerError {
public abstract readonly category: ErrorCategory;
public abstract readonly code: ErrorCode;
public abstract readonly critical: boolean;
}

export class NoSupportedPipelinesError extends PlayerError {
public readonly category = ErrorCategory.Pipeline;
public readonly code = ErrorCode.NoSupportedPipelines;
public readonly critical = true;
}
14 changes: 14 additions & 0 deletions src/player/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import PlayerError from '@/player/errors.ts';

export const Events = {
EnterPictureInPictureMode: 'EnterPictureInPictureMode',
LeavePictureInPictureMode: 'LeavePictureInPictureMode',
Error: 'Error',
} as const;

abstract class PlayerEvent {
Expand All @@ -15,7 +18,18 @@ export class LeavePictureInPictureModeEvent extends PlayerEvent {
public readonly type = Events.LeavePictureInPictureMode;
}

export class ErrorEvent extends PlayerEvent {
public readonly type = Events.Error;
public readonly error: PlayerError;

public constructor(error: PlayerError) {
super();
this.error = error;
}
}

export interface EventToTypeMap {
[Events.EnterPictureInPictureMode]: EnterPictureInPictureModeEvent;
[Events.LeavePictureInPictureMode]: LeavePictureInPictureModeEvent;
[Events.Error]: ErrorEvent;
}
41 changes: 36 additions & 5 deletions src/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import Logger from '../utils/logger.ts';
import { getDefaultPlayerConfiguration } from './configuration.ts';
import EventEmitter from '../utils/eventEmitter.ts';

import { Events, EnterPictureInPictureModeEvent, LeavePictureInPictureModeEvent } from './events.ts';
import { Events, EnterPictureInPictureModeEvent, LeavePictureInPictureModeEvent, ErrorEvent } from './events.ts';
import type { EventToTypeMap } from './events.ts';
import Pipeline from '@/pipelines/basePipeline.ts';
import NativePipeline from '@/pipelines/native/nativePipeline.ts';
import { NoSupportedPipelinesError } from '@/player/errors.ts';

enum PlaybackState {
Playing = 'Playing',
Expand Down Expand Up @@ -36,8 +39,14 @@ interface PlayerVideoTrack {}
interface PlayerStats {}

export default class Player {
private static readonly pipelinesMap: Map<string, Pipeline> = new Map();

public static readonly Events = Events;

public static registerPipeline(mimeType: string, pipeline: Pipeline) {
Player.pipelinesMap.set(mimeType, pipeline);
}

private videoElement: HTMLVideoElement | null = null;
private pictureInPictureWindow: PictureInPictureWindow | null = null;
private configuration: PlayerConfiguration = getDefaultPlayerConfiguration();
Expand Down Expand Up @@ -388,13 +397,35 @@ export default class Player {
// TODO
}

private load(mimeType: string, pipelineHandler: (pipeline: Pipeline) => void): void {
const pipeline = Player.pipelinesMap.get(mimeType);

if (pipeline) {
return pipelineHandler(pipeline);
}

if (this.videoElement?.canPlayType(mimeType)) {
return pipelineHandler(new NativePipeline());
}

this.logger.warn('no supported pipelines found for ', mimeType);

this.eventEmitter.emit(Events.Error, new ErrorEvent(new NoSupportedPipelinesError()));
}

public loadRemoteAsset(uri: string, mimeType: string): void {
this.logger.info(uri, mimeType);
// TODO
if (this.videoElement === null) {
return this.warnAttempt('loadRemoteAsset');
}

return this.load(mimeType, (pipeline) => pipeline.loadRemoteAsset(uri));
}

public loadLocalAsset(asset: string | ArrayBuffer, mimeType: string): void {
this.logger.info(asset, mimeType);
// TODO
if (this.videoElement === null) {
return this.warnAttempt('loadLocalAsset');
}

return this.load(mimeType, (pipeline) => pipeline.loadLocalAsset(asset));
}
}

0 comments on commit 499a6ed

Please sign in to comment.