Skip to content
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

[WIP] feat: Split up code for modularization #2274

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,13 @@ <h4>Buffer &amp; Statistics</h4>
<script src="jsonpack.js"></script>

<!-- demo build -->
<script src="../dist/hls.js"></script>
<script src="../dist/hls.core.js"></script>
<script src="../dist/hls.subtitle-stream.js"></script>
<script src="../dist/hls.subtitle-track.js"></script>
<script src="../dist/hls.timeline.js"></script>
<script src="../dist/hls.audio-stream.js"></script>
<script src="../dist/hls.audio-track.js"></script>
<script src="../dist/hls.eme.js"></script>
<script src="../dist/hls-demo.js"></script>
</body>
</html>
25 changes: 1 addition & 24 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ import FPSController from './controller/fps-controller';
import XhrLoader from './utils/xhr-loader';
// import FetchLoader from './utils/fetch-loader';

import AudioTrackController from './controller/audio-track-controller';
import AudioStreamController from './controller/audio-stream-controller';

import * as Cues from './utils/cues';
import TimelineController from './controller/timeline-controller';
import SubtitleTrackController from './controller/subtitle-track-controller';
import { SubtitleStreamController } from './controller/subtitle-stream-controller';
import EMEController from './controller/eme-controller';
import { requestMediaKeySystemAccess, MediaKeyFunc } from './utils/mediakeys-helper';

type ABRControllerConfig = {
Expand Down Expand Up @@ -133,16 +126,6 @@ export type HlsConfig =
loader: any, // TODO(typescript-xhrloader): Type once XHR is done
xhrSetup?: (xhr: XMLHttpRequest, url: string) => void,

// Alt Audio
audioStreamController?: any, // TODO(typescript-audiostreamcontroller): Type once file is done
audioTrackController?: any, // TODO(typescript-audiotrackcontroller): Type once file is done
// Subtitle
subtitleStreamController?: any, // TODO(typescript-subtitlestreamcontroller): Type once file is done
subtitleTrackController?: any, // TODO(typescript-subtitletrackcontroller): Type once file is done
timelineController?: any, // TODO(typescript-timelinecontroller): Type once file is done
// EME
emeController?: typeof EMEController,

abrController: any, // TODO(typescript-abrcontroller): Type once file is done
bufferController: typeof BufferController,
capLevelController: any, // TODO(typescript-caplevelcontroller): Type once file is done
Expand Down Expand Up @@ -237,13 +220,7 @@ export const hlsDefaultConfig: HlsConfig = {
requestMediaKeySystemAccessFunc: requestMediaKeySystemAccess, // used by eme-controller

// Dynamic Modules
...timelineConfig(),
subtitleStreamController: (__USE_SUBTITLES__) ? SubtitleStreamController : void 0,
subtitleTrackController: (__USE_SUBTITLES__) ? SubtitleTrackController : void 0,
timelineController: (__USE_SUBTITLES__) ? TimelineController : void 0,
audioStreamController: (__USE_ALT_AUDIO__) ? AudioStreamController : void 0,
audioTrackController: (__USE_ALT_AUDIO__) ? AudioTrackController : void 0,
emeController: (__USE_EME_DRM__) ? EMEController : void 0
...timelineConfig()
};

function timelineConfig (): TimelineControllerConfig {
Expand Down
4 changes: 4 additions & 0 deletions src/controller/audio-stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { findFragWithCC } from '../utils/discontinuities';
import { FragmentState } from './fragment-tracker';
import Fragment, { ElementaryStreamTypes } from '../loader/fragment';
import BaseStreamController, { State } from './base-stream-controller';
import Hls from '../hls';
const { performance } = window;

const TICK_INTERVAL = 100; // how often to tick in ms
Expand Down Expand Up @@ -820,4 +821,7 @@ class AudioStreamController extends BaseStreamController {
}
}
}

Hls.add(AudioStreamController);

export default AudioStreamController;
3 changes: 3 additions & 0 deletions src/controller/audio-track-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Event from '../events';
import TaskLoop from '../task-loop';
import { logger } from '../utils/logger';
import { ErrorTypes, ErrorDetails } from '../errors';
import Hls from '../hls';

/**
* @class AudioTrackController
Expand Down Expand Up @@ -402,4 +403,6 @@ class AudioTrackController extends TaskLoop {
}
}

Hls.add(AudioTrackController);

export default AudioTrackController;
4 changes: 4 additions & 0 deletions src/controller/eme-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { logger } from '../utils/logger';
import { EMEControllerConfig } from '../config';
import { KeySystems, MediaKeyFunc } from '../utils/mediakeys-helper';

import Hls from '../hls';

const MAX_LICENSE_REQUEST_FAILURES = 3;

/**
Expand Down Expand Up @@ -505,4 +507,6 @@ class EMEController extends EventHandler {
}
}

Hls.add(EMEController);

export default EMEController;
3 changes: 3 additions & 0 deletions src/controller/subtitle-stream-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { findFragmentByPDT, findFragmentByPTS } from './fragment-finders';
import { FragmentState } from './fragment-tracker';
import BaseStreamController, { State } from './base-stream-controller';
import { mergeSubtitlePlaylists } from './level-helper';
import Hls from '../hls';

const { performance } = window;
const TICK_INTERVAL = 500; // how often to tick in ms
Expand Down Expand Up @@ -240,3 +241,5 @@ export class SubtitleStreamController extends BaseStreamController {
this.fragPrevious = null;
}
}

Hls.add(SubtitleStreamController);
3 changes: 3 additions & 0 deletions src/controller/subtitle-track-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Event from '../events';
import EventHandler from '../event-handler';
import { logger } from '../utils/logger';
import { computeReloadInterval } from './level-helper';
import Hls from '../hls';

class SubtitleTrackController extends EventHandler {
constructor (hls) {
Expand Down Expand Up @@ -241,4 +242,6 @@ function filterSubtitleTracks (textTrackList) {
return tracks;
}

Hls.add(SubtitleTrackController);

export default SubtitleTrackController;
5 changes: 4 additions & 1 deletion src/controller/timeline-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import OutputFilter from '../utils/output-filter';
import WebVTTParser from '../utils/webvtt-parser';
import { logger } from '../utils/logger';
import { sendAddTrackEvent, clearCurrentCues } from '../utils/texttrack-utils';
import Hls from '../hls';
import Fragment from '../loader/fragment';
import { HlsConfig } from '../config';

Expand All @@ -26,7 +27,7 @@ class TimelineController extends EventHandler {
private prevCC: number = -1;
private vttCCs: any = null;

constructor (hls) {
constructor (hls: Hls) {
super(hls, Event.MEDIA_ATTACHING,
Event.MEDIA_DETACHING,
Event.FRAG_PARSING_USERDATA,
Expand Down Expand Up @@ -367,6 +368,8 @@ class TimelineController extends EventHandler {
}
}

Hls.add(TimelineController);

function canReuseVttTextTrack (inUseTrack, manifestTrack): boolean {
return inUseTrack && inUseTrack.label === manifestTrack.name && !(inUseTrack.textTrack1 || inUseTrack.textTrack2);
}
Expand Down
107 changes: 26 additions & 81 deletions src/hls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ export default class Hls extends Observer {
Hls.defaultConfig = defaultConfig;
}

static registry: any[] = [];

static add (component: any): void {
// TODO: Use `Array.prototype.includes`
if (Hls.registry.indexOf(component) === -1) {
Hls.registry.push(component);
}
}

/**
* Creates an instance of an HLS client that can attach to exactly one `HTMLMediaElement`.
*
Expand Down Expand Up @@ -135,16 +144,10 @@ export default class Hls extends Observer {
// core controllers and network loaders

/**
* @member {AbrController} abrController
* @var {ICoreComponent[]}
*/
const abrController = this.abrController = new config.abrController(this); // eslint-disable-line new-cap
const bufferController = new config.bufferController(this); // eslint-disable-line new-cap
const capLevelController = this.capLevelController = new config.capLevelController(this); // eslint-disable-line new-cap
const fpsController = new config.fpsController(this); // eslint-disable-line new-cap
const playListLoader = new PlaylistLoader(this);
const fragmentLoader = new FragmentLoader(this);
const keyLoader = new KeyLoader(this);
const id3TrackController = new ID3TrackController(this);
this.abrController = new config.abrController(this); // eslint-disable-line new-cap
this.capLevelController = new config.capLevelController(this); // eslint-disable-line new-cap

// network controllers

Expand All @@ -161,85 +164,27 @@ export default class Hls extends Observer {
*/
const streamController = this.streamController = new StreamController(this, fragmentTracker);

let networkControllers = [levelController, streamController];

// optional audio stream controller
/**
* @var {ICoreComponent | Controller}
*/
let Controller = config.audioStreamController;
if (Controller) {
networkControllers.push(new Controller(this, fragmentTracker));
}

/**
* @member {INetworkController[]} networkControllers
*/
this.networkControllers = networkControllers;

/**
* @var {ICoreComponent[]}
*/
const coreComponents = [
playListLoader,
fragmentLoader,
keyLoader,
abrController,
bufferController,
capLevelController,
fpsController,
id3TrackController,
fragmentTracker
];

// optional audio track and subtitle controller
Controller = config.audioTrackController;
if (Controller) {
const audioTrackController = new Controller(this);

/**
* @member {AudioTrackController} audioTrackController
*/
this.audioTrackController = audioTrackController;
coreComponents.push(audioTrackController);
}

Controller = config.subtitleTrackController;
if (Controller) {
const subtitleTrackController = new Controller(this);

/**
* @member {SubtitleTrackController} subtitleTrackController
*/
this.subtitleTrackController = subtitleTrackController;
networkControllers.push(subtitleTrackController);
}

Controller = config.emeController;
if (Controller) {
const emeController = new Controller(this);

/**
* @member {EMEController} emeController
*/
this.emeController = emeController;
coreComponents.push(emeController);
}

// optional subtitle controllers
Controller = config.subtitleStreamController;
if (Controller) {
networkControllers.push(new Controller(this, fragmentTracker));
}
Controller = config.timelineController;
if (Controller) {
coreComponents.push(new Controller(this));
}
this.networkControllers = [levelController, streamController];

Hls.add(PlaylistLoader);
Hls.add(FragmentLoader);
Hls.add(KeyLoader);
Hls.add(config.abrController);
Hls.add(config.bufferController);
Hls.add(config.capLevelController);
Hls.add(config.fpsController);
Hls.add(ID3TrackController);
Hls.add(FragmentTracker);

/**
* @member {ICoreComponent[]}
*/
this.coreComponents = coreComponents;
this.coreComponents = Hls.registry.map(component => {
return new component(this, fragmentTracker);
});
}

/**
Expand Down
Loading