Merge branch 'master' into m3u-minor-2

This commit is contained in:
Trek H 2020-01-28 16:18:54 +10:30
commit 48ee2410dc
22 changed files with 978 additions and 2726 deletions

View File

@ -1,195 +1,33 @@
/** /*
* HLS config AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/ */
import AbrController from './controller/abr-controller'; import XhrLoader from './utils/xhr-loader.js';
import BufferController from './controller/buffer-controller';
import CapLevelController from './controller/cap-level-controller';
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 = {
abrEwmaFastLive: number,
abrEwmaSlowLive: number,
abrEwmaFastVoD: number,
abrEwmaSlowVoD: number,
abrEwmaDefaultEstimate: number,
abrBandWidthFactor: number,
abrBandWidthUpFactor: number,
abrMaxWithRealBitrate: boolean,
maxStarvationDelay: number,
maxLoadingDelay: number,
};
export type BufferControllerConfig = {
appendErrorMaxRetry: number,
liveDurationInfinity: boolean,
liveBackBufferLength: number,
};
type CapLevelControllerConfig = {
capLevelToPlayerSize: boolean
};
export type EMEControllerConfig = {
licenseXhrSetup?: (xhr: XMLHttpRequest, url: string) => void,
emeEnabled: boolean,
widevineLicenseUrl?: string,
requestMediaKeySystemAccessFunc: MediaKeyFunc | null,
};
type FragmentLoaderConfig = {
fLoader: any, // TODO(typescript-loader): Once Loader is typed fill this in
fragLoadingTimeOut: number,
fragLoadingMaxRetry: number,
fragLoadingRetryDelay: number,
fragLoadingMaxRetryTimeout: number,
};
type FPSControllerConfig = {
capLevelOnFPSDrop: boolean,
fpsDroppedMonitoringPeriod: number,
fpsDroppedMonitoringThreshold: number,
};
type LevelControllerConfig = {
startLevel?: number
};
type MP4RemuxerConfig = {
stretchShortVideoTrack: boolean,
maxAudioFramesDrift: number,
};
type PlaylistLoaderConfig = {
pLoader: any, // TODO(typescript-loader): Once Loader is typed fill this in
manifestLoadingTimeOut: number,
manifestLoadingMaxRetry: number,
manifestLoadingRetryDelay: number,
manifestLoadingMaxRetryTimeout: number,
levelLoadingTimeOut: number,
levelLoadingMaxRetry: number,
levelLoadingRetryDelay: number,
levelLoadingMaxRetryTimeout: number
};
type StreamControllerConfig = {
autoStartLoad: boolean,
startPosition: number,
defaultAudioCodec?: string,
initialLiveManifestSize: number,
maxBufferLength: number,
maxBufferSize: number,
maxBufferHole: number,
lowBufferWatchdogPeriod: number,
highBufferWatchdogPeriod: number,
nudgeOffset: number,
nudgeMaxRetry: number,
maxFragLookUpTolerance: number,
liveSyncDurationCount: number,
liveMaxLatencyDurationCount: number,
liveSyncDuration?: number,
liveMaxLatencyDuration?: number,
maxMaxBufferLength: number,
startFragPrefetch: boolean,
};
type TimelineControllerConfig = {
cueHandler: any, // TODO(typescript-cues): Type once file is done
enableCEA708Captions: boolean,
enableWebVTT: boolean,
captionsTextTrack1Label: string,
captionsTextTrack1LanguageCode: string,
captionsTextTrack2Label: string,
captionsTextTrack2LanguageCode: string,
};
type TSDemuxerConfig = {
forceKeyFrameOnDiscontinuity: boolean,
};
export type HlsConfig =
{
debug: boolean,
enableWorker: boolean,
enableSoftwareAES: boolean,
minAutoBitrate: number,
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
fpsController: any, // TODO(typescript-fpscontroller): Type once file is done
} &
ABRControllerConfig &
BufferControllerConfig &
CapLevelControllerConfig &
EMEControllerConfig &
FPSControllerConfig &
FragmentLoaderConfig &
LevelControllerConfig &
MP4RemuxerConfig &
PlaylistLoaderConfig &
StreamControllerConfig &
Partial<TimelineControllerConfig> &
TSDemuxerConfig;
// If possible, keep hlsDefaultConfig shallow // If possible, keep hlsDefaultConfig shallow
// It is cloned whenever a new Hls instance is created, by keeping the config // It is cloned whenever a new Hls instance is created, by keeping the config
// shallow the properties are cloned, and we don't end up manipulating the default // shallow the properties are cloned, and we don't end up manipulating the default
export const hlsDefaultConfig: HlsConfig = { export const hlsDefaultConfig = {
autoStartLoad: true, // used by stream-controller
startPosition: -1, // used by stream-controller startPosition: -1, // used by stream-controller
defaultAudioCodec: void 0, // used by stream-controller
debug: false, // used by logger
capLevelOnFPSDrop: false, // used by fps-controller
capLevelToPlayerSize: false, // used by cap-level-controller
initialLiveManifestSize: 1, // used by stream-controller
maxBufferLength: 30, // used by stream-controller
maxBufferSize: 60 * 1000 * 1000, // used by stream-controller
maxBufferHole: 0.5, // used by stream-controller
lowBufferWatchdogPeriod: 0.5, // used by stream-controller
highBufferWatchdogPeriod: 3, // used by stream-controller
nudgeOffset: 0.1, // used by stream-controller
nudgeMaxRetry: 3, // used by stream-controller
maxFragLookUpTolerance: 0.25, // used by stream-controller
liveSyncDurationCount: 3, // used by stream-controller
liveMaxLatencyDurationCount: Infinity, // used by stream-controller
liveSyncDuration: void 0, // used by stream-controller
liveMaxLatencyDuration: void 0, // used by stream-controller
liveDurationInfinity: false, // used by buffer-controller
liveBackBufferLength: Infinity, // used by buffer-controller
maxMaxBufferLength: 600, // used by stream-controller
enableWorker: true, // used by demuxer
enableSoftwareAES: true, // used by decrypter
manifestLoadingTimeOut: 10000, // used by playlist-loader manifestLoadingTimeOut: 10000, // used by playlist-loader
manifestLoadingMaxRetry: 1, // used by playlist-loader manifestLoadingMaxRetry: 1, // used by playlist-loader
manifestLoadingRetryDelay: 1000, // used by playlist-loader manifestLoadingRetryDelay: 1000, // used by playlist-loader
@ -203,62 +41,8 @@ export const hlsDefaultConfig: HlsConfig = {
fragLoadingMaxRetry: 6, // used by fragment-loader fragLoadingMaxRetry: 6, // used by fragment-loader
fragLoadingRetryDelay: 1000, // used by fragment-loader fragLoadingRetryDelay: 1000, // used by fragment-loader
fragLoadingMaxRetryTimeout: 64000, // used by fragment-loader fragLoadingMaxRetryTimeout: 64000, // used by fragment-loader
startFragPrefetch: false, // used by stream-controller
fpsDroppedMonitoringPeriod: 5000, // used by fps-controller
fpsDroppedMonitoringThreshold: 0.2, // used by fps-controller
appendErrorMaxRetry: 3, // used by buffer-controller
loader: XhrLoader, loader: XhrLoader,
// loader: FetchLoader,
fLoader: void 0, // used by fragment-loader fLoader: void 0, // used by fragment-loader
pLoader: void 0, // used by playlist-loader pLoader: void 0, // used by playlist-loader
xhrSetup: void 0, // used by xhr-loader xhrSetup: void 0, // used by xhr-loader
licenseXhrSetup: void 0, // used by eme-controller
// fetchSetup: void 0,
abrController: AbrController,
bufferController: BufferController,
capLevelController: CapLevelController,
fpsController: FPSController,
stretchShortVideoTrack: false, // used by mp4-remuxer
maxAudioFramesDrift: 1, // used by mp4-remuxer
forceKeyFrameOnDiscontinuity: true, // used by ts-demuxer
abrEwmaFastLive: 3, // used by abr-controller
abrEwmaSlowLive: 9, // used by abr-controller
abrEwmaFastVoD: 3, // used by abr-controller
abrEwmaSlowVoD: 9, // used by abr-controller
abrEwmaDefaultEstimate: 5e5, // 500 kbps // used by abr-controller
abrBandWidthFactor: 0.95, // used by abr-controller
abrBandWidthUpFactor: 0.7, // used by abr-controller
abrMaxWithRealBitrate: false, // used by abr-controller
maxStarvationDelay: 4, // used by abr-controller
maxLoadingDelay: 4, // used by abr-controller
minAutoBitrate: 0, // used by hls
emeEnabled: false, // used by eme-controller
widevineLicenseUrl: void 0, // used by eme-controller
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
}; };
function timelineConfig (): TimelineControllerConfig {
if (!__USE_SUBTITLES__) {
// intentionally doing this over returning Partial<TimelineControllerConfig> above
// this has the added nice property of still requiring the object below to completely define all props.
return {} as any;
}
return {
cueHandler: Cues, // used by timeline-controller
enableCEA708Captions: true, // used by timeline-controller
enableWebVTT: true, // used by timeline-controller
captionsTextTrack1Label: 'English', // used by timeline-controller
captionsTextTrack1LanguageCode: 'en', // used by timeline-controller
captionsTextTrack2Label: 'Spanish', // used by timeline-controller
captionsTextTrack2LanguageCode: 'es' // used by timeline-controller
};
}

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,32 @@
/*
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/
/* /*
* *
* All objects in the event handling chain should inherit from this class * All objects in the event handling chain should inherit from this class
* *
*/ */
import Event from './events.js';
import { logger } from './utils/logger';
import { ErrorTypes, ErrorDetails } from './errors';
import Event from './events';
import Hls from './hls';
const FORBIDDEN_EVENT_NAMES = { const FORBIDDEN_EVENT_NAMES = {
'hlsEventGeneric': true, 'hlsEventGeneric': true,
@ -16,11 +35,7 @@ const FORBIDDEN_EVENT_NAMES = {
}; };
class EventHandler { class EventHandler {
hls: Hls; constructor(hls, ...events) {
handledEvents: any[];
useGenericHandler: boolean;
constructor (hls: Hls, ...events: any[]) {
this.hls = hls; this.hls = hls;
this.onEvent = this.onEvent.bind(this); this.onEvent = this.onEvent.bind(this);
this.handledEvents = events; this.handledEvents = events;
@ -35,8 +50,8 @@ class EventHandler {
this.onHandlerDestroyed(); this.onHandlerDestroyed();
} }
protected onHandlerDestroying () {} onHandlerDestroying() { }
protected onHandlerDestroyed () {} onHandlerDestroyed() { }
isEventHandler() { isEventHandler() {
return typeof this.handledEvents === 'object' && this.handledEvents.length && typeof this.onEvent === 'function'; return typeof this.handledEvents === 'object' && this.handledEvents.length && typeof this.onEvent === 'function';
@ -65,12 +80,12 @@ class EventHandler {
/** /**
* arguments: event (string), data (any) * arguments: event (string), data (any)
*/ */
onEvent (event: string, data: any) { onEvent(event, data) {
this.onEventGeneric(event, data); this.onEventGeneric(event, data);
} }
onEventGeneric (event: string, data: any) { onEventGeneric(event, data) {
let eventToFunction = function (event: string, data: any) { let eventToFunction = function (event, data) {
let funcName = 'on' + event.replace('hls', ''); let funcName = 'on' + event.replace('hls', '');
if (typeof this[funcName] !== 'function') { if (typeof this[funcName] !== 'function') {
throw new Error(`Event ${event} has no generic handler in this ${this.constructor.name} class (tried ${funcName})`); throw new Error(`Event ${event} has no generic handler in this ${this.constructor.name} class (tried ${funcName})`);
@ -81,7 +96,7 @@ class EventHandler {
try { try {
eventToFunction.call(this, event, data).call(); eventToFunction.call(this, event, data).call();
} catch (err) { } catch (err) {
logger.error(`An internal error happened while handling event ${event}. Error message: "${err.message}". Here is a stacktrace:`, err); console.error(`An internal error happened while handling event ${event}. Error message: "${err.message}". Here is a stacktrace:`, err);
this.hls.trigger(Event.ERROR, { type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event: event, err: err }); this.hls.trigger(Event.ERROR, { type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event: event, err: err });
} }
} }

View File

@ -1,110 +1,55 @@
/*
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/
/** /**
* @readonly * @readonly
* @enum {string} * @enum {string}
*/ */
const HlsEvents = { const HlsEvents = {
// fired before MediaSource is attaching to media element - data: { media }
MEDIA_ATTACHING: 'hlsMediaAttaching',
// fired when MediaSource has been succesfully attached to media element - data: { }
MEDIA_ATTACHED: 'hlsMediaAttached',
// fired before detaching MediaSource from media element - data: { }
MEDIA_DETACHING: 'hlsMediaDetaching',
// fired when MediaSource has been detached from media element - data: { }
MEDIA_DETACHED: 'hlsMediaDetached',
// fired when we buffer is going to be reset - data: { }
BUFFER_RESET: 'hlsBufferReset',
// fired when we know about the codecs that we need buffers for to push into - data: {tracks : { container, codec, levelCodec, initSegment, metadata }}
BUFFER_CODECS: 'hlsBufferCodecs',
// fired when sourcebuffers have been created - data: { tracks : tracks }
BUFFER_CREATED: 'hlsBufferCreated',
// fired when we append a segment to the buffer - data: { segment: segment object }
BUFFER_APPENDING: 'hlsBufferAppending',
// fired when we are done with appending a media segment to the buffer - data : { parent : segment parent that triggered BUFFER_APPENDING, pending : nb of segments waiting for appending for this segment parent}
BUFFER_APPENDED: 'hlsBufferAppended',
// fired when the stream is finished and we want to notify the media buffer that there will be no more data - data: { }
BUFFER_EOS: 'hlsBufferEos',
// fired when the media buffer should be flushed - data { startOffset, endOffset }
BUFFER_FLUSHING: 'hlsBufferFlushing',
// fired when the media buffer has been flushed - data: { }
BUFFER_FLUSHED: 'hlsBufferFlushed',
// fired to signal that a manifest loading starts - data: { url : manifestURL} // fired to signal that a manifest loading starts - data: { url : manifestURL}
MANIFEST_LOADING: 'hlsManifestLoading', MANIFEST_LOADING: 'hlsManifestLoading',
// fired after manifest has been loaded - data: { levels : [available quality levels], audioTracks : [ available audio tracks], url : manifestURL, stats : { trequest, tfirst, tload, mtime}} // fired after manifest has been loaded - data: { levels : [available quality levels], audioTracks : [ available audio tracks], url : manifestURL, stats : { trequest, tfirst, tload, mtime}}
MANIFEST_LOADED: 'hlsManifestLoaded', MANIFEST_LOADED: 'hlsManifestLoaded',
// fired after manifest has been parsed - data: { levels : [available quality levels], firstLevel : index of first quality level appearing in Manifest}
MANIFEST_PARSED: 'hlsManifestParsed',
// fired when a level switch is requested - data: { level : id of new level }
LEVEL_SWITCHING: 'hlsLevelSwitching',
// fired when a level switch is effective - data: { level : id of new level }
LEVEL_SWITCHED: 'hlsLevelSwitched',
// fired when a level playlist loading starts - data: { url : level URL, level : id of level being loaded} // fired when a level playlist loading starts - data: { url : level URL, level : id of level being loaded}
LEVEL_LOADING: 'hlsLevelLoading', LEVEL_LOADING: 'hlsLevelLoading',
// fired when a level playlist loading finishes - data: { details : levelDetails object, level : id of loaded level, stats : { trequest, tfirst, tload, mtime} } // fired when a level playlist loading finishes - data: { details : levelDetails object, level : id of loaded level, stats : { trequest, tfirst, tload, mtime} }
LEVEL_LOADED: 'hlsLevelLoaded', LEVEL_LOADED: 'hlsLevelLoaded',
// fired when a level's details have been updated based on previous details, after it has been loaded - data: { details : levelDetails object, level : id of updated level } // fired when a level's details have been updated based on previous details, after it has been loaded - data: { details : levelDetails object, level : id of updated level }
LEVEL_UPDATED: 'hlsLevelUpdated', LEVEL_UPDATED: 'hlsLevelUpdated',
// fired when a level's PTS information has been updated after parsing a fragment - data: { details : levelDetails object, level : id of updated level, drift: PTS drift observed when parsing last fragment }
LEVEL_PTS_UPDATED: 'hlsLevelPtsUpdated',
// fired to notify that audio track lists has been updated - data: { audioTracks : audioTracks }
AUDIO_TRACKS_UPDATED: 'hlsAudioTracksUpdated',
// fired when an audio track switching is requested - data: { id : audio track id }
AUDIO_TRACK_SWITCHING: 'hlsAudioTrackSwitching',
// fired when an audio track switch actually occurs - data: { id : audio track id }
AUDIO_TRACK_SWITCHED: 'hlsAudioTrackSwitched',
// fired when an audio track loading starts - data: { url : audio track URL, id : audio track id } // fired when an audio track loading starts - data: { url : audio track URL, id : audio track id }
AUDIO_TRACK_LOADING: 'hlsAudioTrackLoading', AUDIO_TRACK_LOADING: 'hlsAudioTrackLoading',
// fired when an audio track loading finishes - data: { details : levelDetails object, id : audio track id, stats : { trequest, tfirst, tload, mtime } } // fired when an audio track loading finishes - data: { details : levelDetails object, id : audio track id, stats : { trequest, tfirst, tload, mtime } }
AUDIO_TRACK_LOADED: 'hlsAudioTrackLoaded', AUDIO_TRACK_LOADED: 'hlsAudioTrackLoaded',
// fired to notify that subtitle track lists has been updated - data: { subtitleTracks : subtitleTracks }
SUBTITLE_TRACKS_UPDATED: 'hlsSubtitleTracksUpdated',
// fired when an subtitle track switch occurs - data: { id : subtitle track id }
SUBTITLE_TRACK_SWITCH: 'hlsSubtitleTrackSwitch',
// fired when a subtitle track loading starts - data: { url : subtitle track URL, id : subtitle track id } // fired when a subtitle track loading starts - data: { url : subtitle track URL, id : subtitle track id }
SUBTITLE_TRACK_LOADING: 'hlsSubtitleTrackLoading', SUBTITLE_TRACK_LOADING: 'hlsSubtitleTrackLoading',
// fired when a subtitle track loading finishes - data: { details : levelDetails object, id : subtitle track id, stats : { trequest, tfirst, tload, mtime } } // fired when a subtitle track loading finishes - data: { details : levelDetails object, id : subtitle track id, stats : { trequest, tfirst, tload, mtime } }
SUBTITLE_TRACK_LOADED: 'hlsSubtitleTrackLoaded', SUBTITLE_TRACK_LOADED: 'hlsSubtitleTrackLoaded',
// fired when a subtitle fragment has been processed - data: { success : boolean, frag : the processed frag }
SUBTITLE_FRAG_PROCESSED: 'hlsSubtitleFragProcessed',
// fired when the first timestamp is found - data: { id : demuxer id, initPTS: initPTS, frag : fragment object }
INIT_PTS_FOUND: 'hlsInitPtsFound',
// fired when a fragment loading starts - data: { frag : fragment object } // fired when a fragment loading starts - data: { frag : fragment object }
FRAG_LOADING: 'hlsFragLoading', FRAG_LOADING: 'hlsFragLoading',
// fired when a fragment loading is progressing - data: { frag : fragment object, { trequest, tfirst, loaded } } // fired when a fragment loading is progressing - data: { frag : fragment object, { trequest, tfirst, loaded } }
FRAG_LOAD_PROGRESS: 'hlsFragLoadProgress', FRAG_LOAD_PROGRESS: 'hlsFragLoadProgress',
// Identifier for fragment load aborting for emergency switch down - data: { frag : fragment object }
FRAG_LOAD_EMERGENCY_ABORTED: 'hlsFragLoadEmergencyAborted',
// fired when a fragment loading is completed - data: { frag : fragment object, payload : fragment payload, stats : { trequest, tfirst, tload, length } } // fired when a fragment loading is completed - data: { frag : fragment object, payload : fragment payload, stats : { trequest, tfirst, tload, length } }
FRAG_LOADED: 'hlsFragLoaded', FRAG_LOADED: 'hlsFragLoaded'
// fired when a fragment has finished decrypting - data: { id : demuxer id, frag: fragment object, payload : fragment payload, stats : { tstart, tdecrypt } }
FRAG_DECRYPTED: 'hlsFragDecrypted',
// fired when Init Segment has been extracted from fragment - data: { id : demuxer id, frag: fragment object, moov : moov MP4 box, codecs : codecs found while parsing fragment }
FRAG_PARSING_INIT_SEGMENT: 'hlsFragParsingInitSegment',
// fired when parsing sei text is completed - data: { id : demuxer id, frag: fragment object, samples : [ sei samples pes ] }
FRAG_PARSING_USERDATA: 'hlsFragParsingUserdata',
// fired when parsing id3 is completed - data: { id : demuxer id, frag: fragment object, samples : [ id3 samples pes ] }
FRAG_PARSING_METADATA: 'hlsFragParsingMetadata',
// fired when data have been extracted from fragment - data: { id : demuxer id, frag: fragment object, data1 : moof MP4 box or TS fragments, data2 : mdat MP4 box or null}
FRAG_PARSING_DATA: 'hlsFragParsingData',
// fired when fragment parsing is completed - data: { id : demuxer id, frag: fragment object }
FRAG_PARSED: 'hlsFragParsed',
// fired when fragment remuxed MP4 boxes have all been appended into SourceBuffer - data: { id : demuxer id, frag : fragment object, stats : { trequest, tfirst, tload, tparsed, tbuffered, length, bwEstimate } }
FRAG_BUFFERED: 'hlsFragBuffered',
// fired when fragment matching with current media position is changing - data : { id : demuxer id, frag : fragment object }
FRAG_CHANGED: 'hlsFragChanged',
// Identifier for a FPS drop event - data: { curentDropped, currentDecoded, totalDroppedFrames }
FPS_DROP: 'hlsFpsDrop',
// triggered when FPS drop triggers auto level capping - data: { level, droppedlevel }
FPS_DROP_LEVEL_CAPPING: 'hlsFpsDropLevelCapping',
// Identifier for an error event - data: { type : error type, details : error details, fatal : if true, hls.js cannot/will not try to recover, if false, hls.js will try to recover,other error specific data }
ERROR: 'hlsError',
// fired when hls.js instance starts destroying. Different from MEDIA_DETACHED as one could want to detach and reattach a media to the instance of hls.js to handle mid-rolls for example - data: { }
DESTROYING: 'hlsDestroying',
// fired when a decrypt key loading starts - data: { frag : fragment object }
KEY_LOADING: 'hlsKeyLoading',
// fired when a decrypt key loading is completed - data: { frag : fragment object, payload : key payload, stats : { trequest, tfirst, tload, length } }
KEY_LOADED: 'hlsKeyLoaded',
// fired upon stream controller state transitions - data: { previousState, nextState }
STREAM_STATE_TRANSITION: 'hlsStreamStateTransition'
}; };
export default HlsEvents; export default HlsEvents;

View File

@ -1,674 +1,50 @@
import * as URLToolkit from 'url-toolkit'; /*
AUTHOR
Trek Hopton <trek@ausocean.org>
import { LICENSE
ErrorTypes, This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
ErrorDetails
} from './errors';
import PlaylistLoader from './loader/playlist-loader'; It is free software: you can redistribute it and/or modify them
import FragmentLoader from './loader/fragment-loader'; under the terms of the GNU General Public License as published by the
import KeyLoader from './loader/key-loader'; Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
import { FragmentTracker } from './controller/fragment-tracker'; It is distributed in the hope that it will be useful, but WITHOUT
import StreamController from './controller/stream-controller'; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
import LevelController from './controller/level-controller'; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
import ID3TrackController from './controller/id3-track-controller'; for more details.
import { isSupported } from './is-supported'; You should have received a copy of the GNU General Public License in gpl.txt.
import { logger, enableLogs } from './utils/logger'; If not, see http://www.gnu.org/licenses.
import { hlsDefaultConfig, HlsConfig } from './config';
import HlsEvents from './events'; For hls.js Copyright notice and license, see LICENSE file.
import { Observer } from './observer';
/**
* @module Hls
* @class
* @constructor
*/ */
export default class Hls extends Observer {
public static defaultConfig?: HlsConfig;
public config: HlsConfig;
private _autoLevelCapping: number; import URLToolkit from '../url-toolkit/url-toolkit.js';
private abrController: any; import HlsEvents from './events.js';
private capLevelController: any; import PlaylistLoader from './loader/playlist-loader.js';
private levelController: any; import FragmentLoader from './loader/fragment-loader.js';
private streamController: any; import StreamController from './controller/stream-controller.js';
private networkControllers: any[]; import { hlsDefaultConfig } from './config.js';
private audioTrackController: any; import { Observer } from './observer.js';
private subtitleTrackController: any;
private emeController: any;
private coreComponents: any[];
private media: HTMLMediaElement | null = null;
private url: string | null = null;
/** class Hls extends Observer {
* @type {string} constructor() {
*/
static get version (): string {
return __VERSION__;
}
/**
* @type {boolean}
*/
static isSupported (): boolean {
return isSupported();
}
/**
* @type {HlsEvents}
*/
static get Events () {
return HlsEvents;
}
/**
* @type {HlsErrorTypes}
*/
static get ErrorTypes () {
return ErrorTypes;
}
/**
* @type {HlsErrorDetails}
*/
static get ErrorDetails () {
return ErrorDetails;
}
/**
* @type {HlsConfig}
*/
static get DefaultConfig (): HlsConfig {
if (!Hls.defaultConfig) {
return hlsDefaultConfig;
}
return Hls.defaultConfig;
}
/**
* @type {HlsConfig}
*/
static set DefaultConfig (defaultConfig: HlsConfig) {
Hls.defaultConfig = defaultConfig;
}
/**
* Creates an instance of an HLS client that can attach to exactly one `HTMLMediaElement`.
*
* @constructs Hls
* @param {HlsConfig} config
*/
constructor (userConfig: Partial<HlsConfig> = {}) {
super(); super();
this.pLoader = new PlaylistLoader(this);
this.streamController = new StreamController(this);
this.fragmentLoader = new FragmentLoader(this);
const defaultConfig = Hls.DefaultConfig; this.config = hlsDefaultConfig;
if ((userConfig.liveSyncDurationCount || userConfig.liveMaxLatencyDurationCount) && (userConfig.liveSyncDuration || userConfig.liveMaxLatencyDuration)) {
throw new Error('Illegal hls.js config: don\'t mix up liveSyncDurationCount/liveMaxLatencyDurationCount and liveSyncDuration/liveMaxLatencyDuration');
} }
// Shallow clone // url is the source URL. Can be relative or absolute.
this.config = { loadSource(url, callback) {
...defaultConfig, this.loadSuccess = callback;
...userConfig
};
const { config } = this;
if (config.liveMaxLatencyDurationCount !== void 0 && config.liveMaxLatencyDurationCount <= config.liveSyncDurationCount) {
throw new Error('Illegal hls.js config: "liveMaxLatencyDurationCount" must be gt "liveSyncDurationCount"');
}
if (config.liveMaxLatencyDuration !== void 0 && (config.liveSyncDuration === void 0 || config.liveMaxLatencyDuration <= config.liveSyncDuration)) {
throw new Error('Illegal hls.js config: "liveMaxLatencyDuration" must be gt "liveSyncDuration"');
}
enableLogs(config.debug);
this._autoLevelCapping = -1;
// core controllers and network loaders
/**
* @member {AbrController} abrController
*/
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);
// network controllers
/**
* @member {LevelController} levelController
*/
const levelController = this.levelController = new LevelController(this);
// FIXME: FragmentTracker must be defined before StreamController because the order of event handling is important
const fragmentTracker = new FragmentTracker(this);
/**
* @member {StreamController} streamController
*/
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));
}
/**
* @member {ICoreComponent[]}
*/
this.coreComponents = coreComponents;
}
/**
* Dispose of the instance
*/
destroy () {
logger.log('destroy');
this.trigger(HlsEvents.DESTROYING);
this.detachMedia();
this.coreComponents.concat(this.networkControllers).forEach(component => {
component.destroy();
});
this.url = null;
this.removeAllListeners();
this._autoLevelCapping = -1;
}
/**
* Attach a media element
* @param {HTMLMediaElement} media
*/
attachMedia (media: HTMLMediaElement) {
logger.log('attachMedia');
this.media = media;
this.trigger(HlsEvents.MEDIA_ATTACHING, { media: media });
}
/**
* Detach from the media
*/
detachMedia () {
logger.log('detachMedia');
this.trigger(HlsEvents.MEDIA_DETACHING);
this.media = null;
}
/**
* Set the source URL. Can be relative or absolute.
* @param {string} url
*/
loadSource (url: string) {
url = URLToolkit.buildAbsoluteURL(window.location.href, url, { alwaysNormalize: true }); url = URLToolkit.buildAbsoluteURL(window.location.href, url, { alwaysNormalize: true });
logger.log(`loadSource:${url}`);
this.url = url;
// when attaching to a source URL, trigger a playlist load
this.trigger(HlsEvents.MANIFEST_LOADING, { url: url }); this.trigger(HlsEvents.MANIFEST_LOADING, { url: url });
} }
/**
* Start loading data from the stream source.
* Depending on default config, client starts loading automatically when a source is set.
*
* @param {number} startPosition Set the start position to stream from
* @default -1 None (from earliest point)
*/
startLoad (startPosition: number = -1) {
logger.log(`startLoad(${startPosition})`);
this.networkControllers.forEach(controller => {
controller.startLoad(startPosition);
});
} }
/** export default Hls
* Stop loading of any stream data.
*/
stopLoad () {
logger.log('stopLoad');
this.networkControllers.forEach(controller => {
controller.stopLoad();
});
}
/**
* Swap through possible audio codecs in the stream (for example to switch from stereo to 5.1)
*/
swapAudioCodec () {
logger.log('swapAudioCodec');
this.streamController.swapAudioCodec();
}
/**
* When the media-element fails, this allows to detach and then re-attach it
* as one call (convenience method).
*
* Automatic recovery of media-errors by this process is configurable.
*/
recoverMediaError () {
logger.log('recoverMediaError');
let media = this.media;
this.detachMedia();
if (media) {
this.attachMedia(media);
}
}
/**
* @type {QualityLevel[]}
*/
// todo(typescript-levelController)
get levels (): any[] {
return this.levelController.levels;
}
/**
* Index of quality level currently played
* @type {number}
*/
get currentLevel (): number {
return this.streamController.currentLevel;
}
/**
* Set quality level index immediately .
* This will flush the current buffer to replace the quality asap.
* That means playback will interrupt at least shortly to re-buffer and re-sync eventually.
* @type {number} -1 for automatic level selection
*/
set currentLevel (newLevel: number) {
logger.log(`set currentLevel:${newLevel}`);
this.loadLevel = newLevel;
this.streamController.immediateLevelSwitch();
}
/**
* Index of next quality level loaded as scheduled by stream controller.
* @type {number}
*/
get nextLevel (): number {
return this.streamController.nextLevel;
}
/**
* Set quality level index for next loaded data.
* This will switch the video quality asap, without interrupting playback.
* May abort current loading of data, and flush parts of buffer (outside currently played fragment region).
* @type {number} -1 for automatic level selection
*/
set nextLevel (newLevel: number) {
logger.log(`set nextLevel:${newLevel}`);
this.levelController.manualLevel = newLevel;
this.streamController.nextLevelSwitch();
}
/**
* Return the quality level of the currently or last (of none is loaded currently) segment
* @type {number}
*/
get loadLevel (): number {
return this.levelController.level;
}
/**
* Set quality level index for next loaded data in a conservative way.
* This will switch the quality without flushing, but interrupt current loading.
* Thus the moment when the quality switch will appear in effect will only be after the already existing buffer.
* @type {number} newLevel -1 for automatic level selection
*/
set loadLevel (newLevel: number) {
logger.log(`set loadLevel:${newLevel}`);
this.levelController.manualLevel = newLevel;
}
/**
* get next quality level loaded
* @type {number}
*/
get nextLoadLevel (): number {
return this.levelController.nextLoadLevel;
}
/**
* Set quality level of next loaded segment in a fully "non-destructive" way.
* Same as `loadLevel` but will wait for next switch (until current loading is done).
* @type {number} level
*/
set nextLoadLevel (level: number) {
this.levelController.nextLoadLevel = level;
}
/**
* Return "first level": like a default level, if not set,
* falls back to index of first level referenced in manifest
* @type {number}
*/
get firstLevel (): number {
return Math.max(this.levelController.firstLevel, this.minAutoLevel);
}
/**
* Sets "first-level", see getter.
* @type {number}
*/
set firstLevel (newLevel: number) {
logger.log(`set firstLevel:${newLevel}`);
this.levelController.firstLevel = newLevel;
}
/**
* Return start level (level of first fragment that will be played back)
* if not overrided by user, first level appearing in manifest will be used as start level
* if -1 : automatic start level selection, playback will start from level matching download bandwidth
* (determined from download of first segment)
* @type {number}
*/
get startLevel (): number {
return this.levelController.startLevel;
}
/**
* set start level (level of first fragment that will be played back)
* if not overrided by user, first level appearing in manifest will be used as start level
* if -1 : automatic start level selection, playback will start from level matching download bandwidth
* (determined from download of first segment)
* @type {number} newLevel
*/
set startLevel (newLevel: number) {
logger.log(`set startLevel:${newLevel}`);
// if not in automatic start level detection, ensure startLevel is greater than minAutoLevel
if (newLevel !== -1) {
newLevel = Math.max(newLevel, this.minAutoLevel);
}
this.levelController.startLevel = newLevel;
}
/**
* set dynamically set capLevelToPlayerSize against (`CapLevelController`)
*
* @type {boolean}
*/
set capLevelToPlayerSize (shouldStartCapping: boolean) {
const newCapLevelToPlayerSize = !!shouldStartCapping;
if (newCapLevelToPlayerSize !== this.config.capLevelToPlayerSize) {
if (newCapLevelToPlayerSize) {
this.capLevelController.startCapping(); // If capping occurs, nextLevelSwitch will happen based on size.
} else {
this.capLevelController.stopCapping();
this.autoLevelCapping = -1;
this.streamController.nextLevelSwitch(); // Now we're uncapped, get the next level asap.
}
this.config.capLevelToPlayerSize = newCapLevelToPlayerSize;
}
}
/**
* Capping/max level value that should be used by automatic level selection algorithm (`ABRController`)
* @type {number}
*/
get autoLevelCapping (): number {
return this._autoLevelCapping;
}
/**
* get bandwidth estimate
* @type {number}
*/
get bandwidthEstimate (): number {
const bwEstimator = this.abrController._bwEstimator;
return bwEstimator ? bwEstimator.getEstimate() : NaN;
}
/**
* Capping/max level value that should be used by automatic level selection algorithm (`ABRController`)
* @type {number}
*/
set autoLevelCapping (newLevel: number) {
logger.log(`set autoLevelCapping:${newLevel}`);
this._autoLevelCapping = newLevel;
}
/**
* True when automatic level selection enabled
* @type {boolean}
*/
get autoLevelEnabled (): boolean {
return (this.levelController.manualLevel === -1);
}
/**
* Level set manually (if any)
* @type {number}
*/
get manualLevel (): number {
return this.levelController.manualLevel;
}
/**
* min level selectable in auto mode according to config.minAutoBitrate
* @type {number}
*/
get minAutoLevel (): number {
const { levels, config: { minAutoBitrate } } = this;
const len = levels ? levels.length : 0;
for (let i = 0; i < len; i++) {
const levelNextBitrate = levels[i].realBitrate
? Math.max(levels[i].realBitrate, levels[i].bitrate)
: levels[i].bitrate;
if (levelNextBitrate > minAutoBitrate) {
return i;
}
}
return 0;
}
/**
* max level selectable in auto mode according to autoLevelCapping
* @type {number}
*/
get maxAutoLevel (): number {
const { levels, autoLevelCapping } = this;
let maxAutoLevel;
if (autoLevelCapping === -1 && levels && levels.length) {
maxAutoLevel = levels.length - 1;
} else {
maxAutoLevel = autoLevelCapping;
}
return maxAutoLevel;
}
/**
* next automatically selected quality level
* @type {number}
*/
get nextAutoLevel (): number {
// ensure next auto level is between min and max auto level
return Math.min(Math.max(this.abrController.nextAutoLevel, this.minAutoLevel), this.maxAutoLevel);
}
/**
* this setter is used to force next auto level.
* this is useful to force a switch down in auto mode:
* in case of load error on level N, hls.js can set nextAutoLevel to N-1 for example)
* forced value is valid for one fragment. upon succesful frag loading at forced level,
* this value will be resetted to -1 by ABR controller.
* @type {number}
*/
set nextAutoLevel (nextLevel: number) {
this.abrController.nextAutoLevel = Math.max(this.minAutoLevel, nextLevel);
}
/**
* @type {AudioTrack[]}
*/
// todo(typescript-audioTrackController)
get audioTracks (): any[] {
const audioTrackController = this.audioTrackController;
return audioTrackController ? audioTrackController.audioTracks : [];
}
/**
* index of the selected audio track (index in audio track lists)
* @type {number}
*/
get audioTrack (): number {
const audioTrackController = this.audioTrackController;
return audioTrackController ? audioTrackController.audioTrack : -1;
}
/**
* selects an audio track, based on its index in audio track lists
* @type {number}
*/
set audioTrack (audioTrackId: number) {
const audioTrackController = this.audioTrackController;
if (audioTrackController) {
audioTrackController.audioTrack = audioTrackId;
}
}
/**
* @type {Seconds}
*/
get liveSyncPosition (): number {
return this.streamController.liveSyncPosition;
}
/**
* get alternate subtitle tracks list from playlist
* @type {SubtitleTrack[]}
*/
// todo(typescript-subtitleTrackController)
get subtitleTracks (): any[] {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController ? subtitleTrackController.subtitleTracks : [];
}
/**
* index of the selected subtitle track (index in subtitle track lists)
* @type {number}
*/
get subtitleTrack (): number {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController ? subtitleTrackController.subtitleTrack : -1;
}
/**
* select an subtitle track, based on its index in subtitle track lists
* @type {number}
*/
set subtitleTrack (subtitleTrackId: number) {
const subtitleTrackController = this.subtitleTrackController;
if (subtitleTrackController) {
subtitleTrackController.subtitleTrack = subtitleTrackId;
}
}
/**
* @type {boolean}
*/
get subtitleDisplay (): boolean {
const subtitleTrackController = this.subtitleTrackController;
return subtitleTrackController ? subtitleTrackController.subtitleDisplay : false;
}
/**
* Enable/disable subtitle display rendering
* @type {boolean}
*/
set subtitleDisplay (value: boolean) {
const subtitleTrackController = this.subtitleTrackController;
if (subtitleTrackController) {
subtitleTrackController.subtitleDisplay = value;
}
}
}

View File

@ -1,32 +1,53 @@
/*
AUTHOR
Trek Hopton <trek@ausocean.org>
import { buildAbsoluteURL } from 'url-toolkit'; LICENSE
import { logger } from '../utils/logger'; This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
import LevelKey from './level-key';
import { PlaylistLevelType } from '../types/loader';
export enum ElementaryStreamTypes { It is free software: you can redistribute it and/or modify them
AUDIO = 'audio', under the terms of the GNU General Public License as published by the
VIDEO = 'video', Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/
import URLToolkit from '../../url-toolkit/url-toolkit.js';
import LevelKey from './level-key.js';
export const ElementaryStreamTypes = {
AUDIO: 'audio',
VIDEO: 'video'
} }
export default class Fragment { export default class Fragment {
private _url: string | null = null; constructor() {
private _byteRange: number[] | null = null; this._url = null;
private _decryptdata: LevelKey | null = null; this._byteRange = null;
this._decryptdata = null;
// Holds the types of data this fragment supports // Holds the types of data this fragment supports
private _elementaryStreams: Record<ElementaryStreamTypes, boolean> = { this._elementaryStreams = {
[ElementaryStreamTypes.AUDIO]: false, [ElementaryStreamTypes.AUDIO]: false,
[ElementaryStreamTypes.VIDEO]: false [ElementaryStreamTypes.VIDEO]: false
}; };
// deltaPTS tracks the change in presentation timestamp between fragments // deltaPTS tracks the change in presentation timestamp between fragments
public deltaPTS: number = 0; this.deltaPTS = 0;
public rawProgramDateTime: string | null = null; this.rawProgramDateTime = null;
public programDateTime: number | null = null; this.programDateTime = null;
public title: string | null = null; this.title = null;
public tagList: Array<string[]> = []; this.tagList = [];
// TODO: Move at least baseurl to constructor. // TODO: Move at least baseurl to constructor.
// Currently we do a two-pass construction as use the Fragment class almost like a object for holding parsing state. // Currently we do a two-pass construction as use the Fragment class almost like a object for holding parsing state.
@ -35,35 +56,35 @@ export default class Fragment {
// Something to think on. // Something to think on.
// Discontinuity Counter // Discontinuity Counter
public cc!: number; this.cc;
this.type;
public type!: PlaylistLevelType;
// relurl is the portion of the URL that comes from inside the playlist. // relurl is the portion of the URL that comes from inside the playlist.
public relurl!: string; this.relurl;
// baseurl is the URL to the playlist // baseurl is the URL to the playlist
public baseurl!: string; this.baseurl;
// EXTINF has to be present for a m3u8 to be considered valid // EXTINF has to be present for a m3u8 to be considered valid
public duration!: number; this.duration;
// When this segment starts in the timeline // When this segment starts in the timeline
public start!: number; this.start;
// sn notates the sequence number for a segment, and if set to a string can be 'initSegment' // sn notates the sequence number for a segment, and if set to a string can be 'initSegment'
public sn: number | 'initSegment' = 0; this.sn = 0;
public urlId: number = 0; this.urlId = 0;
// level matches this fragment to a index playlist // level matches this fragment to a index playlist
public level: number = 0; this.level = 0;
// levelkey is the EXT-X-KEY that applies to this segment for decryption // levelkey is the EXT-X-KEY that applies to this segment for decryption
// core difference from the private field _decryptdata is the lack of the initialized IV // core difference from the private field _decryptdata is the lack of the initialized IV
// _decryptdata will set the IV for this segment based on the segment number in the fragment // _decryptdata will set the IV for this segment based on the segment number in the fragment
public levelkey?: LevelKey; this.levelkey;
// TODO(typescript-xhrloader) // TODO(typescript-xhrloader)
public loader: any; this.loader;
}
// setByteRange converts a EXT-X-BYTERANGE attribute into a two element array // setByteRange converts a EXT-X-BYTERANGE attribute into a two element array
setByteRange (value: string, previousFrag?: Fragment) { setByteRange(value, previousFrag) {
const params = value.split('@', 2); const params = value.split('@', 2);
const byteRange: number[] = []; const byteRange = [];
if (params.length === 1) { if (params.length === 1) {
byteRange[0] = previousFrag ? previousFrag.byteRangeEndOffset : 0; byteRange[0] = previousFrag ? previousFrag.byteRangeEndOffset : 0;
} else { } else {
@ -75,7 +96,7 @@ export default class Fragment {
get url() { get url() {
if (!this._url && this.relurl) { if (!this._url && this.relurl) {
this._url = buildAbsoluteURL(this.baseurl, this.relurl, { alwaysNormalize: true }); this._url = URLToolkit.buildAbsoluteURL(this.baseurl, this.relurl, { alwaysNormalize: true });
} }
return this._url; return this._url;
@ -85,7 +106,7 @@ export default class Fragment {
this._url = value; this._url = value;
} }
get byteRange (): number[] { get byteRange() {
if (!this._byteRange) { if (!this._byteRange) {
return []; return [];
} }
@ -104,7 +125,7 @@ export default class Fragment {
return this.byteRange[1]; return this.byteRange[1];
} }
get decryptdata (): LevelKey | null { get decryptdata() {
if (!this.levelkey && !this._decryptdata) { if (!this.levelkey && !this._decryptdata) {
return null; return null;
} }
@ -116,7 +137,7 @@ export default class Fragment {
// If the segment was encrypted with AES-128 // If the segment was encrypted with AES-128
// It must have an IV defined. We cannot substitute the Segment Number in. // It must have an IV defined. We cannot substitute the Segment Number in.
if (this.levelkey && this.levelkey.method === 'AES-128' && !this.levelkey.iv) { if (this.levelkey && this.levelkey.method === 'AES-128' && !this.levelkey.iv) {
logger.warn(`missing IV for initialization segment with method="${this.levelkey.method}" - compliance issue`); console.warn(`missing IV for initialization segment with method="${this.levelkey.method}" - compliance issue`);
} }
/* /*
@ -155,14 +176,14 @@ export default class Fragment {
/** /**
* @param {ElementaryStreamTypes} type * @param {ElementaryStreamTypes} type
*/ */
addElementaryStream (type: ElementaryStreamTypes) { addElementaryStream(type) {
this._elementaryStreams[type] = true; this._elementaryStreams[type] = true;
} }
/** /**
* @param {ElementaryStreamTypes} type * @param {ElementaryStreamTypes} type
*/ */
hasElementaryStream (type: ElementaryStreamTypes) { hasElementaryStream(type) {
return this._elementaryStreams[type] === true; return this._elementaryStreams[type] === true;
} }
@ -171,7 +192,7 @@ export default class Fragment {
* @param {number} segmentNumber - segment number to generate IV with * @param {number} segmentNumber - segment number to generate IV with
* @returns {Uint8Array} * @returns {Uint8Array}
*/ */
createInitializationVector (segmentNumber: number): Uint8Array { createInitializationVector(segmentNumber) {
let uint8View = new Uint8Array(16); let uint8View = new Uint8Array(16);
for (let i = 12; i < 16; i++) { for (let i = 12; i < 16; i++) {
@ -187,7 +208,7 @@ export default class Fragment {
* @param segmentNumber - the fragment's segment number * @param segmentNumber - the fragment's segment number
* @returns {LevelKey} - an object to be applied as a fragment's decryptdata * @returns {LevelKey} - an object to be applied as a fragment's decryptdata
*/ */
setDecryptDataFromLevelKey (levelkey: LevelKey, segmentNumber: number): LevelKey { setDecryptDataFromLevelKey(levelkey, segmentNumber) {
let decryptdata = levelkey; let decryptdata = levelkey;
if (levelkey && levelkey.method && levelkey.uri && !levelkey.iv) { if (levelkey && levelkey.method && levelkey.uri && !levelkey.iv) {

View File

@ -1,22 +1,45 @@
import { buildAbsoluteURL } from 'url-toolkit'; /*
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/
import URLToolkit from '../../url-toolkit/url-toolkit.js';
export default class LevelKey { export default class LevelKey {
private _uri: string | null = null; constructor(baseURI, relativeURI) {
this._uri = null;
public baseuri: string; this.baseuri;
public reluri: string; this.reluri;
public method: string | null = null; this.method = null;
public key: Uint8Array | null = null; this.key = null;
public iv: Uint8Array | null = null; this.iv = null;
constructor (baseURI: string, relativeURI: string) {
this.baseuri = baseURI; this.baseuri = baseURI;
this.reluri = relativeURI; this.reluri = relativeURI;
} }
get uri() { get uri() {
if (!this._uri && this.reluri) { if (!this._uri && this.reluri) {
this._uri = buildAbsoluteURL(this.baseuri, this.reluri, { alwaysNormalize: true }); this._uri = URLToolkit.buildAbsoluteURL(this.baseuri, this.reluri, { alwaysNormalize: true });
} }
return this._uri; return this._uri;

View File

@ -1,14 +1,32 @@
import * as URLToolkit from 'url-toolkit'; /*
AUTHOR
Trek Hopton <trek@ausocean.org>
import Fragment from './fragment'; LICENSE
import Level from './level'; This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
import LevelKey from './level-key';
import AttrList from '../utils/attr-list'; It is free software: you can redistribute it and/or modify them
import { logger } from '../utils/logger'; under the terms of the GNU General Public License as published by the
import { isCodecType, CodecType } from '../utils/codecs'; Free Software Foundation, either version 3 of the License, or (at your
import { MediaPlaylist, AudioGroup, MediaPlaylistType } from '../types/media-playlist'; option) any later version.
import { PlaylistLevelType } from '../types/loader';
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/
import URLToolkit from '../../url-toolkit/url-toolkit.js';
import Fragment from './fragment.js';
import Level from './level.js';
import LevelKey from './level-key.js';
import AttrList from '../utils/attr-list.js';
import { isCodecType } from '../utils/codecs.js';
/** /**
* M3U8 parser * M3U8 parser
@ -32,7 +50,7 @@ const LEVEL_PLAYLIST_REGEX_SLOW = /(?:(?:#(EXTM3U))|(?:#EXT-X-(PLAYLIST-TYPE):(.
const MP4_REGEX_SUFFIX = /\.(mp4|m4s|m4v|m4a)$/i; const MP4_REGEX_SUFFIX = /\.(mp4|m4s|m4v|m4a)$/i;
export default class M3U8Parser { export default class M3U8Parser {
static findGroup (groups: Array<AudioGroup>, mediaGroupId: string): AudioGroup | undefined { static findGroup(groups, mediaGroupId) {
for (let i = 0; i < groups.length; i++) { for (let i = 0; i < groups.length; i++) {
const group = groups[i]; const group = groups[i];
if (group.id === mediaGroupId) { if (group.id === mediaGroupId) {
@ -58,14 +76,14 @@ export default class M3U8Parser {
return URLToolkit.buildAbsoluteURL(baseUrl, url, { alwaysNormalize: true }); return URLToolkit.buildAbsoluteURL(baseUrl, url, { alwaysNormalize: true });
} }
static parseMasterPlaylist (string: string, baseurl: string) { static parseMasterPlaylist(string, baseurl) {
// TODO(typescript-level) // TODO(typescript-level)
let levels: Array<any> = []; let levels = [];
MASTER_PLAYLIST_REGEX.lastIndex = 0; MASTER_PLAYLIST_REGEX.lastIndex = 0;
// TODO(typescript-level) // TODO(typescript-level)
function setCodecs (codecs: Array<string>, level: any) { function setCodecs(codecs, level) {
['video', 'audio'].forEach((type: CodecType) => { ['video', 'audio'].forEach((type) => {
const filtered = codecs.filter((codec) => isCodecType(codec, type)); const filtered = codecs.filter((codec) => isCodecType(codec, type));
if (filtered.length) { if (filtered.length) {
const preferred = filtered.filter((codec) => { const preferred = filtered.filter((codec) => {
@ -81,10 +99,10 @@ export default class M3U8Parser {
level.unknownCodecs = codecs; level.unknownCodecs = codecs;
} }
let result: RegExpExecArray | null; let result;
while ((result = MASTER_PLAYLIST_REGEX.exec(string)) != null) { while ((result = MASTER_PLAYLIST_REGEX.exec(string)) != null) {
// TODO(typescript-level) // TODO(typescript-level)
const level: any = {}; const level = {};
const attrs = level.attrs = new AttrList(result[1]); const attrs = level.attrs = new AttrList(result[1]);
level.url = M3U8Parser.resolve(result[2], baseurl); level.url = M3U8Parser.resolve(result[2], baseurl);
@ -108,15 +126,15 @@ export default class M3U8Parser {
return levels; return levels;
} }
static parseMasterPlaylistMedia (string: string, baseurl: string, type: MediaPlaylistType, audioGroups: Array<AudioGroup> = []): Array<MediaPlaylist> { static parseMasterPlaylistMedia(string, baseurl, type, audioGroups = []) {
let result: RegExpExecArray | null; let result;
let medias: Array<MediaPlaylist> = []; let medias = [];
let id = 0; let id = 0;
MASTER_PLAYLIST_MEDIA_REGEX.lastIndex = 0; MASTER_PLAYLIST_MEDIA_REGEX.lastIndex = 0;
while ((result = MASTER_PLAYLIST_MEDIA_REGEX.exec(string)) !== null) { while ((result = MASTER_PLAYLIST_MEDIA_REGEX.exec(string)) !== null) {
const attrs = new AttrList(result[1]); const attrs = new AttrList(result[1]);
if (attrs.TYPE === type) { if (attrs.TYPE === type) {
const media: MediaPlaylist = { const media = {
id: id++, id: id++,
groupId: attrs['GROUP-ID'], groupId: attrs['GROUP-ID'],
name: attrs.NAME || attrs.LANGUAGE, name: attrs.NAME || attrs.LANGUAGE,
@ -146,16 +164,16 @@ export default class M3U8Parser {
return medias; return medias;
} }
static parseLevelPlaylist (string: string, baseurl: string, id: number, type: PlaylistLevelType, levelUrlId: number) { static parseLevelPlaylist(string, baseurl, id, type, levelUrlId) {
let currentSN = 0; let currentSN = 0;
let totalduration = 0; let totalduration = 0;
let level = new Level(baseurl); let level = new Level(baseurl);
let discontinuityCounter = 0; let discontinuityCounter = 0;
let prevFrag: Fragment | null = null; let prevFrag = null;
let frag: Fragment | null = new Fragment(); let frag = new Fragment();
let result: RegExpExecArray | RegExpMatchArray | null; let result;
let i: number; let i;
let levelkey: LevelKey | undefined; let levelkey;
let firstPdtIndex = null; let firstPdtIndex = null;
@ -209,7 +227,7 @@ export default class M3U8Parser {
} else { } else {
result = result[0].match(LEVEL_PLAYLIST_REGEX_SLOW); result = result[0].match(LEVEL_PLAYLIST_REGEX_SLOW);
if (!result) { if (!result) {
logger.warn('No matches on slow regex match for level playlist!'); console.warn('No matches on slow regex match for level playlist!');
continue; continue;
} }
for (i = 1; i < result.length; i++) { for (i = 1; i < result.length; i++) {
@ -294,13 +312,13 @@ export default class M3U8Parser {
break; break;
} }
default: default:
logger.warn(`line parsed but not handled: ${result}`); console.warn(`line parsed but not handled: ${result}`);
break; break;
} }
} }
} }
frag = prevFrag; frag = prevFrag;
// logger.log('found ' + level.fragments.length + ' fragments'); // console.log('found ' + level.fragments.length + ' fragments');
if (frag && !frag.relurl) { if (frag && !frag.relurl) {
level.fragments.pop(); level.fragments.pop();
totalduration -= frag.duration; totalduration -= frag.duration;
@ -316,7 +334,7 @@ export default class M3U8Parser {
// if the fragments are TS or MP4, except if we download them :/ // if the fragments are TS or MP4, except if we download them :/
// but this is to be able to handle SIDX. // but this is to be able to handle SIDX.
if (level.fragments.every((frag) => MP4_REGEX_SUFFIX.test(frag.relurl))) { if (level.fragments.every((frag) => MP4_REGEX_SUFFIX.test(frag.relurl))) {
logger.warn('MP4 fragments found but no init segment (probably no MAP, incomplete M3U8), trying to fetch SIDX'); console.warn('MP4 fragments found but no init segment (probably no MAP, incomplete M3U8), trying to fetch SIDX');
frag = new Fragment(); frag = new Fragment();
frag.relurl = level.fragments[0].relurl; frag.relurl = level.fragments[0].relurl;

View File

@ -1,4 +1,27 @@
import { EventEmitter } from 'eventemitter3'; /*
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/
import EventEmitter from '../eventemitter3/index.js';
/** /**
* Simple adapter sub-class of Nodejs-like EventEmitter. * Simple adapter sub-class of Nodejs-like EventEmitter.
@ -9,7 +32,7 @@ export class Observer extends EventEmitter {
* in every call to a handler, which is the purpose of our `trigger` method * in every call to a handler, which is the purpose of our `trigger` method
* extending the standard API. * extending the standard API.
*/ */
trigger (event: string, ...data: Array<any>): void { trigger(event, ...data) {
this.emit(event, event, ...data); this.emit(event, event, ...data);
} }
} }

View File

@ -1,132 +1,42 @@
import Level from '../loader/level'; /*
AUTHOR
Trek Hopton <trek@ausocean.org>
export interface LoaderContext { LICENSE
// target URL This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
url: string
// loader response type (arraybuffer or default response type for playlist)
responseType: string
// start byte range offset
rangeStart?: number
// end byte range offset
rangeEnd?: number
// true if onProgress should report partial chunk of loaded content
progressData?: boolean
}
export interface LoaderConfiguration { It is free software: you can redistribute it and/or modify them
// Max number of load retries under the terms of the GNU General Public License as published by the
maxRetry: number Free Software Foundation, either version 3 of the License, or (at your
// Timeout after which `onTimeOut` callback will be triggered option) any later version.
// (if loading is still not finished after that delay)
timeout: number
// Delay between an I/O error and following connection retry (ms).
// This to avoid spamming the server
retryDelay: number
// max connection retry delay (ms)
maxRetryDelay: number
}
export interface LoaderResponse { It is distributed in the hope that it will be useful, but WITHOUT
url: string, ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// TODO(jstackhouse): SharedArrayBuffer, es2017 extension to TS FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
data: string | ArrayBuffer for more details.
}
export interface LoaderStats { You should have received a copy of the GNU General Public License in gpl.txt.
// performance.now() just after load() has been called If not, see http://www.gnu.org/licenses.
trequest: number
// performance.now() of first received byte
tfirst: number
// performance.now() on load complete
tload: number
// performance.now() on parse completion
tparsed: number
// number of loaded bytes
loaded: number
// total number of bytes
total: number
}
type LoaderOnSuccess < T extends LoaderContext > = ( For hls.js Copyright notice and license, see LICENSE file.
response: LoaderResponse, */
stats: LoaderStats,
context: T,
networkDetails: any
) => void;
type LoaderOnProgress < T extends LoaderContext > = (
stats: LoaderStats,
context: T,
data: string | ArrayBuffer,
networkDetails: any,
) => void;
type LoaderOnError < T extends LoaderContext > = (
error: {
// error status code
code: number,
// error description
text: string,
},
context: T,
networkDetails: any,
) => void;
type LoaderOnTimeout < T extends LoaderContext > = (
stats: LoaderStats,
context: T,
) => void;
export interface LoaderCallbacks<T extends LoaderContext>{
onSuccess: LoaderOnSuccess<T>,
onError: LoaderOnError<T>,
onTimeout: LoaderOnTimeout<T>,
onProgress?: LoaderOnProgress<T>,
}
export interface Loader<T extends LoaderContext> {
destroy(): void
abort(): void
load(
context: LoaderContext,
config: LoaderConfiguration,
callbacks: LoaderCallbacks<T>,
): void
context: T
}
/** /**
* `type` property values for this loaders' context object * @readonly
* @enum * @enum {string}
*
*/ */
export enum PlaylistContextType { export const PlaylistContextType = {
MANIFEST = 'manifest', MANIFEST: 'manifest',
LEVEL = 'level', LEVEL: 'level',
AUDIO_TRACK = 'audioTrack', AUDIO_TRACK: 'audioTrack',
SUBTITLE_TRACK= 'subtitleTrack' SUBTITLE_TRACK: 'subtitleTrack'
} }
/** /**
* @enum {string} * @enum {string}
*/ */
export enum PlaylistLevelType { export const PlaylistLevelType = {
MAIN = 'main', MAIN: 'main',
AUDIO = 'audio', AUDIO: 'audio',
SUBTITLE = 'subtitle' SUBTITLE: 'subtitle'
}
export interface PlaylistLoaderContext extends LoaderContext {
loader?: Loader<PlaylistLoaderContext>
type: PlaylistContextType
// the level index to load
level: number | null
// TODO: what is id?
id: number | null
// defines if the loader is handling a sidx request for the playlist
isSidxRequest?: boolean
// internal reprsentation of a parsed m3u8 level playlist
levelDetails?: Level
} }

View File

@ -1,3 +1,26 @@
/*
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
For hls.js Copyright notice and license, see LICENSE file.
*/
// from http://mp4ra.org/codecs.html // from http://mp4ra.org/codecs.html
const sampleEntryCodesISO = { const sampleEntryCodesISO = {
audio: { audio: {
@ -63,14 +86,12 @@ const sampleEntryCodesISO = {
} }
}; };
export type CodecType = 'audio' | 'video'; function isCodecType(codec, type) {
function isCodecType (codec: string, type: CodecType): boolean {
const typeCodes = sampleEntryCodesISO[type]; const typeCodes = sampleEntryCodesISO[type];
return !!typeCodes && typeCodes[codec.slice(0, 4)] === true; return !!typeCodes && typeCodes[codec.slice(0, 4)] === true;
} }
function isCodecSupportedInMp4 (codec: string, type: CodecType): boolean { function isCodecSupportedInMp4(codec, type) {
return MediaSource.isTypeSupported(`${type || 'video'}/mp4;codecs="${codec}"`); return MediaSource.isTypeSupported(`${type || 'video'}/mp4;codecs="${codec}"`);
} }

View File

@ -1,32 +1,57 @@
<!DOCTYPE html> <!DOCTYPE html>
<!--
AUTHOR
Trek Hopton <trek@ausocean.org>
LICENSE
This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see http://www.gnu.org/licenses.
-->
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Mjpeg Player</title> <title>Mjpeg Player</title>
<script type="text/javascript" src="main.js"></script> <script type="module" src="./main.js"></script>
<script type="module" src="player.js"></script>
</head> </head>
<body style="height: 100%"> <body style="height: 100%">
<div class="card m-auto" style="width: 40rem;"> <div style="width: 40rem;">
<div class="card-body"> <div>
<div class="container-fluid"> <div>
<div class="form-group"> <div>
<input class="form-control-file" type="file" id="fileinput" onchange="play();"> <input type="file" id="fileInput">
</div> </div>
</div> </div>
<div class="container-fluid"> <div>
Frame Rate: <input type="text" id="rate" class="mb-3 mx-3" value="30"> fps <label>Playlist URL: </label>
<input type="url" id="url" placeholder="Enter URL">
<button id="urlBtn" type="submit">Load</button>
</div> </div>
<div class="container-fluid"> <div>
Frame Rate: <input type="text" id="rate" value="25"> fps
</div>
<div>
<img src="" id="viewer" style="max-width:100%; height:auto;"> <img src="" id="viewer" style="max-width:100%; height:auto;">
</div> </div>
</div> </div>
</div> </div>
<footer id="sticky-footer" class="footer fixed-bottom py-4 bg-dark text-white-50" style="width: 100%;"> <footer id="sticky-footer" style="width: 100%;">
<div class="container text-center"> <div>
<small>&copy;2019 Australian Ocean Laboratory Limited (AusOcean) (<a rel="license" href="https://www.ausocean.org/license">License</a>)</small> <small>&copy;2020 Australian Ocean Laboratory Limited (AusOcean) (<a rel="license" href="https://www.ausocean.org/license">License</a>)</small>
</div> </div>
</footer> </footer>
</body> </body>

View File

@ -1,12 +1,9 @@
/* /*
NAME
main.js
AUTHOR AUTHOR
Trek Hopton <trek@ausocean.org> Trek Hopton <trek@ausocean.org>
LICENSE LICENSE
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean) This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the under the terms of the GNU General Public License as published by the
@ -22,9 +19,66 @@ LICENSE
If not, see http://www.gnu.org/licenses. If not, see http://www.gnu.org/licenses.
*/ */
// play will process and play the chosen target file. import Hls from "./hlsjs/hls.js";
let started = false;
let player, viewer;
// init gets DOM elements once the document has been loaded and adds listeners where necessary.
function init() {
document.addEventListener('DOMContentLoaded', load);
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('urlBtn').addEventListener('click', load);
document.getElementById('fileInput').addEventListener('change', play);
viewer = document.getElementById('viewer');
}
);
}
init();
// load gets the URL from the URL input element or the browser's URL bar
// and creates an Hls instance to load the content from the URL.
function load() {
let url = document.getElementById('url').value;
if (url == "") {
// Get everything following the ? from the browser's URL bar.
url = window.location.search.slice(1);
document.getElementById('url').value = url;
} else if (url[0] == '/') {
url = window.location.protocol + '//' + window.location.host + url;
}
if (url == "") {
return;
}
let hls = new Hls();
hls.loadSource(url, append);
}
// append, on the first call, starts a player worker and passes it a frame rate and the video data,
// on subsequent calls it passes the video data to the player worker.
function append(data) {
if (!started) {
player = new Worker("player.js");
let rate = document.getElementById('rate');
if (rate.value && rate.value > 0) {
player.postMessage({ msg: "setFrameRate", data: rate.value });
}
player.onmessage = handleMessage;
player.postMessage({ msg: "loadMtsMjpeg", data: data }, [data]);
started = true;
} else {
player.postMessage({ msg: "appendMtsMjpeg", data: data }, [data]);
}
}
// play will process and play the target file chosen with the file input element.
function play() { function play() {
const viewer = document.getElementById('viewer');
const input = event.target.files[0]; const input = event.target.files[0];
const reader = new FileReader(); const reader = new FileReader();
@ -32,29 +86,11 @@ function play() {
const player = new Worker("player.js"); const player = new Worker("player.js");
let rate = document.getElementById('rate'); let rate = document.getElementById('rate');
if (rate.value && rate.value > 0) { if (rate.value > 0) {
player.postMessage({ msg: "setFrameRate", data: rate.value }); player.postMessage({ msg: "setFrameRate", data: rate.value });
} }
player.onmessage = e => { player.onmessage = handleMessage;
switch (e.data.msg) {
case "frame":
const blob = new Blob([new Uint8Array(e.data.data)], {
type: 'video/x-motion-jpeg'
});
const url = URL.createObjectURL(blob);
viewer.src = url;
break;
case "log":
console.log(e.data.data);
break;
case "stop":
break;
default:
console.error("unknown message from player");
break;
}
};
switch (input.name.split('.')[1]) { switch (input.name.split('.')[1]) {
case "mjpeg": case "mjpeg":
@ -71,5 +107,26 @@ function play() {
}; };
reader.onerror = error => reject(error); reader.onerror = error => reject(error);
reader.readAsArrayBuffer(input); reader.readAsArrayBuffer(input);
}
// handleMessage handles messgaes from the player workers, its main job is to update the display when a frame is received.
function handleMessage(e) {
switch (e.data.msg) {
case "frame":
const blob = new Blob([new Uint8Array(e.data.data)], {
type: 'video/x-motion-jpeg'
});
const url = URL.createObjectURL(blob);
viewer.src = url;
break;
case "log":
console.log(e.data.data);
break;
case "stop":
console.log("stopped");
break;
default:
console.error("unknown message from player");
break;
}
} }

View File

@ -1,12 +1,9 @@
/* /*
NAME
player.js
AUTHOR AUTHOR
Trek Hopton <trek@ausocean.org> Trek Hopton <trek@ausocean.org>
LICENSE LICENSE
This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean) This file is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the under the terms of the GNU General Public License as published by the
@ -22,42 +19,56 @@ LICENSE
If not, see http://www.gnu.org/licenses. If not, see http://www.gnu.org/licenses.
*/ */
let frameRate = 30; let frameRate = 25; //Keeps track of the frame rate, default is 25fps.
self.importScripts('./lex-mjpeg.js');
self.importScripts('./hlsjs/mts-demuxer.js');
const codecs = {
MJPEG: 1,
MTS_MJPEG: 2,
}
// onmessage is called whenever the main thread sends this worker a message.
onmessage = e => { onmessage = e => {
switch (e.data.msg) { switch (e.data.msg) {
case "setFrameRate": case "setFrameRate":
frameRate = e.data.data; frameRate = e.data.data;
break; break;
case "loadMjpeg": case "loadMjpeg":
self.importScripts('./lex-mjpeg.js'); player = new PlayerWorker();
let mjpeg = new Uint8Array(e.data.data); player.init(codecs.MJPEG);
let lex = new MJPEGLexer(mjpeg); player.append(e.data.data);
player = new Player(lex);
player.setFrameRate(frameRate); player.setFrameRate(frameRate);
player.start(); player.start();
break; break;
case "loadMtsMjpeg": case "loadMtsMjpeg":
self.importScripts('./hlsjs/mts-demuxer.js'); player = new PlayerWorker();
let mtsMjpeg = new Uint8Array(e.data.data); player.init(codecs.MTS_MJPEG);
let demux = new MTSDemuxer(); player.append(e.data.data);
let tracks = demux._getTracks();
demux.append(mtsMjpeg);
buf = new FrameBuffer(tracks.video.data);
player = new Player(buf);
player.setFrameRate(frameRate); //TODO: read frame rate from metadata.
player.start(); player.start();
break; break;
case "appendMtsMjpeg":
player.append(e.data.data);
break;
default: default:
console.error("unknown message from main thread"); console.error("unknown message from main thread");
break; break;
} }
}; };
class Player { // PlayerWorker has a FrameBuffer to hold frames and once started, passes them one at a time to the main thread.
constructor(buffer) { class PlayerWorker {
this.buffer = buffer; init(codec) {
this.frameRate = frameRate; this.frameRate = frameRate;
this.codec = codec;
switch (codec) {
case codecs.MJPEG:
this.frameSrc = new MJPEGLexer();
break;
case codecs.MTS_MJPEG:
this.frameSrc = new FrameBuffer();
break;
}
} }
setFrameRate(rate) { setFrameRate(rate) {
@ -65,7 +76,7 @@ class Player {
} }
start() { start() {
let frame = this.buffer.read(); let frame = this.frameSrc.read();
if (frame != null) { if (frame != null) {
postMessage({ msg: "frame", data: frame.buffer }, [frame.buffer]); postMessage({ msg: "frame", data: frame.buffer }, [frame.buffer]);
setTimeout(() => { this.start(); }, 1000 / this.frameRate); setTimeout(() => { this.start(); }, 1000 / this.frameRate);
@ -73,17 +84,51 @@ class Player {
postMessage({ msg: "stop" }); postMessage({ msg: "stop" });
} }
} }
append(data) {
this.frameSrc.append(data);
}
} }
// FrameBuffer allows an array of subarrays (MJPEG frames) to be read one at a time. // FrameBuffer allows an array of subarrays (MJPEG frames) to be read one at a time.
class FrameBuffer { class FrameBuffer {
constructor(src) { constructor() {
this.src = src; this.segments = [];
this.off = 0; this.off = { segment: 0, frame: 0 };
this.demuxer = new MTSDemuxer();
} }
// read returns the next single frame. // read returns the next single frame.
read() { read() {
return this.src[this.off++]; let off = this.off;
let prevOff = off;
if (this.incrementOff()) {
return this.segments[prevOff.segment][prevOff.frame];
} else {
return null;
}
}
append(data) {
let demuxed = this.demuxer.demux(new Uint8Array(data));
this.segments.push(demuxed.data);
}
incrementOff() {
if (!this.segments || !this.segments[this.off.segment]) {
return false;
}
if (this.off.frame + 1 >= this.segments[this.off.segment].length) {
if (this.off.segment + 1 >= this.segments.length) {
return false;
} else {
this.off.segment++;
this.off.frame = 0;
return true;
}
} else {
this.off.frame++;
return true;
}
} }
} }

View File

@ -128,6 +128,7 @@ func handleFlags() config.Config {
httpAddressPtr = flag.String("HttpAddress", "", "Destination address of http posts") httpAddressPtr = flag.String("HttpAddress", "", "Destination address of http posts")
verticalFlipPtr = flag.Bool("VerticalFlip", false, "Flip video vertically: Yes, No") verticalFlipPtr = flag.Bool("VerticalFlip", false, "Flip video vertically: Yes, No")
horizontalFlipPtr = flag.Bool("HorizontalFlip", false, "Flip video horizontally: Yes, No") horizontalFlipPtr = flag.Bool("HorizontalFlip", false, "Flip video horizontally: Yes, No")
loopPtr = flag.Bool("Loop", false, "Loop input source on completion (true/false)")
bitratePtr = flag.Uint("Bitrate", 0, "Bitrate of recorded video") bitratePtr = flag.Uint("Bitrate", 0, "Bitrate of recorded video")
heightPtr = flag.Uint("Height", 0, "Height in pixels") heightPtr = flag.Uint("Height", 0, "Height in pixels")
widthPtr = flag.Uint("Width", 0, "Width in pixels") widthPtr = flag.Uint("Width", 0, "Width in pixels")
@ -138,6 +139,7 @@ func handleFlags() config.Config {
saturationPtr = flag.Int("Saturation", 0, "Set Saturation. (100-100)") saturationPtr = flag.Int("Saturation", 0, "Set Saturation. (100-100)")
exposurePtr = flag.String("Exposure", "auto", "Set exposure mode. ("+strings.Join(raspivid.ExposureModes[:], ",")+")") exposurePtr = flag.String("Exposure", "auto", "Set exposure mode. ("+strings.Join(raspivid.ExposureModes[:], ",")+")")
autoWhiteBalancePtr = flag.String("Awb", "auto", "Set automatic white balance mode. ("+strings.Join(raspivid.AutoWhiteBalanceModes[:], ",")+")") autoWhiteBalancePtr = flag.String("Awb", "auto", "Set automatic white balance mode. ("+strings.Join(raspivid.AutoWhiteBalanceModes[:], ",")+")")
fileFPSPtr = flag.Int("FileFPS", 0, "File source frame processing FPS")
// Audio specific flags. // Audio specific flags.
sampleRatePtr = flag.Int("SampleRate", 48000, "Sample rate of recorded audio") sampleRatePtr = flag.Int("SampleRate", 48000, "Sample rate of recorded audio")
@ -254,6 +256,8 @@ func handleFlags() config.Config {
netsender.ConfigFile = *configFilePtr netsender.ConfigFile = *configFilePtr
} }
cfg.FileFPS = *fileFPSPtr
cfg.Loop = *loopPtr
cfg.CameraIP = *cameraIPPtr cfg.CameraIP = *cameraIPPtr
cfg.Rotation = *rotationPtr cfg.Rotation = *rotationPtr
cfg.HorizontalFlip = *horizontalFlipPtr cfg.HorizontalFlip = *horizontalFlipPtr

129
filter/difference.go Normal file
View File

@ -0,0 +1,129 @@
// +build !circleci
/*
DESCRIPTION
A filter that detects motion and discards frames without motion. The
algorithm calculates the absolute difference for each pixel between
two frames, then finds the mean. If the mean is above a given threshold,
then it is considered motion.
AUTHORS
Scott Barnard <scott@ausocean.org>
LICENSE
difference.go is Copyright (C) 2020 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package filter
import (
"fmt"
"image"
"image/color"
"io"
"gocv.io/x/gocv"
)
// Difference is a filter that provides basic motion detection. Difference calculates
// the absolute difference for each pixel between two frames, then finds the mean. If
// the mean is above a given threshold, then it is considered motion.
type Difference struct {
dst io.WriteCloser
thresh float64
prev gocv.Mat
debug bool
windows []*gocv.Window
}
// NewDifference returns a pointer to a new Difference struct.
func NewDifference(dst io.WriteCloser, debug bool, threshold float64) *Difference {
var windows []*gocv.Window
if debug {
windows = []*gocv.Window{gocv.NewWindow("Diff: Bounding boxes"), gocv.NewWindow("Diff: Motion")}
}
return &Difference{dst, threshold, gocv.NewMat(), debug, windows}
}
// Implements io.Closer.
// Close frees resources used by gocv, because it has to be done manually, due to
// it using c-go.
func (d *Difference) Close() error {
d.prev.Close()
for _, window := range d.windows {
window.Close()
}
return nil
}
// Implements io.Writer.
// Write applies the motion filter to the video stream. Only frames with motion
// are written to the destination encoder, frames without are discarded.
func (d *Difference) Write(f []byte) (int, error) {
if d.prev.Empty() {
var err error
d.prev, err = gocv.IMDecode(f, gocv.IMReadColor)
if err != nil {
return 0, err
}
return len(f), nil
}
img, err := gocv.IMDecode(f, gocv.IMReadColor)
defer img.Close()
if err != nil {
return 0, err
}
imgDelta := gocv.NewMat()
defer imgDelta.Close()
// Seperate foreground and background.
gocv.AbsDiff(img, d.prev, &imgDelta)
gocv.CvtColor(imgDelta, &imgDelta, gocv.ColorBGRToGray)
mean := imgDelta.Mean().Val1
// Update History.
d.prev = img.Clone()
// Draw debug information.
if d.debug {
if mean >= d.thresh {
gocv.PutText(
&img,
fmt.Sprintf("motion - mean:%f", mean),
image.Pt(32, 32),
gocv.FontHersheyPlain,
2.0,
color.RGBA{255, 0, 0, 0},
2,
)
}
d.windows[0].IMShow(img)
d.windows[1].IMShow(imgDelta)
d.windows[0].WaitKey(1)
}
// Don't write to destination if there is no motion.
if mean < d.thresh {
return len(f), nil
}
// Write to destination.
return d.dst.Write(f)
}

View File

@ -36,6 +36,12 @@ func NewMOGFilter(dst io.WriteCloser, area, threshold float64, history int, debu
return &NoOp{dst: dst} return &NoOp{dst: dst}
} }
// NewKNNFilter returns a pointer to a new NoOp struct for testing purposes only.
func NewKNNFilter(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool, hf int) *NoOp { func NewKNNFilter(dst io.WriteCloser, area, threshold float64, history, kernelSize int, debug bool, hf int) *NoOp {
return &NoOp{dst: dst} return &NoOp{dst: dst}
} }
// NewDiffference returns a pointer to a new NoOp struct for testing purposes only.
func NewDifference(dst io.WriteCloser, debug bool, threshold float64) *NoOp {
return &NoOp{dst: dst}
}

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.13
require ( require (
bitbucket.org/ausocean/iot v1.2.13 bitbucket.org/ausocean/iot v1.2.13
bitbucket.org/ausocean/utils v1.2.12 bitbucket.org/ausocean/utils v1.2.13
github.com/Comcast/gots v0.0.0-20190305015453-8d56e473f0f7 github.com/Comcast/gots v0.0.0-20190305015453-8d56e473f0f7
github.com/go-audio/audio v0.0.0-20181013203223-7b2a6ca21480 github.com/go-audio/audio v0.0.0-20181013203223-7b2a6ca21480
github.com/go-audio/wav v0.0.0-20181013172942-de841e69b884 github.com/go-audio/wav v0.0.0-20181013172942-de841e69b884

2
go.sum
View File

@ -12,6 +12,8 @@ bitbucket.org/ausocean/utils v1.2.11 h1:zA0FOaPjN960ryp8PKCkV5y50uWBYrIxCVnXjwbv
bitbucket.org/ausocean/utils v1.2.11/go.mod h1:uXzX9z3PLemyURTMWRhVI8uLhPX4uuvaaO85v2hcob8= bitbucket.org/ausocean/utils v1.2.11/go.mod h1:uXzX9z3PLemyURTMWRhVI8uLhPX4uuvaaO85v2hcob8=
bitbucket.org/ausocean/utils v1.2.12 h1:VnskjWTDM475TnQRhBQE0cNp9D6Y6OELrd4UkD2VVIQ= bitbucket.org/ausocean/utils v1.2.12 h1:VnskjWTDM475TnQRhBQE0cNp9D6Y6OELrd4UkD2VVIQ=
bitbucket.org/ausocean/utils v1.2.12/go.mod h1:uXzX9z3PLemyURTMWRhVI8uLhPX4uuvaaO85v2hcob8= bitbucket.org/ausocean/utils v1.2.12/go.mod h1:uXzX9z3PLemyURTMWRhVI8uLhPX4uuvaaO85v2hcob8=
bitbucket.org/ausocean/utils v1.2.13 h1:tUaIywtoMc1+zl1GCVQokX4mL5X7LNHX5O51AgAPrWA=
bitbucket.org/ausocean/utils v1.2.13/go.mod h1:uXzX9z3PLemyURTMWRhVI8uLhPX4uuvaaO85v2hcob8=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Comcast/gots v0.0.0-20190305015453-8d56e473f0f7 h1:LdOc9B9Bj6LEsKiXShkLA3/kpxXb6LJpH+ekU2krbzw= github.com/Comcast/gots v0.0.0-20190305015453-8d56e473f0f7 h1:LdOc9B9Bj6LEsKiXShkLA3/kpxXb6LJpH+ekU2krbzw=

View File

@ -86,6 +86,7 @@ const (
defaultAudioInputCodec = codecutil.ADPCM defaultAudioInputCodec = codecutil.ADPCM
defaultPSITime = 2 defaultPSITime = 2
defaultMotionInterval = 5 defaultMotionInterval = 5
defaultFileFPS = 0
// Ring buffer defaults. // Ring buffer defaults.
defaultRBMaxElements = 10000 defaultRBMaxElements = 10000
@ -126,6 +127,7 @@ const (
FilterMOG FilterMOG
FilterVariableFPS FilterVariableFPS
FilterKNN FilterKNN
FilterDifference
) )
// OS names // OS names
@ -297,6 +299,14 @@ type Config struct {
MOGMinArea float64 // Used to ignore small areas of motion detection. MOGMinArea float64 // Used to ignore small areas of motion detection.
MOGThreshold float64 // Intensity value from the KNN motion detection algorithm that is considered motion. MOGThreshold float64 // Intensity value from the KNN motion detection algorithm that is considered motion.
MOGHistory uint // Length of MOG filter's history MOGHistory uint // Length of MOG filter's history
// If true will restart reading of input after an io.EOF.
Loop bool
// Defines the rate at which frames from a file source are processed.
FileFPS int
// Difference filter parameters.
DiffThreshold float64 // Intensity value from the Difference motion detection algorithm that is considered motion.
} }
// TypeData contains information about all of the variables that // TypeData contains information about all of the variables that
@ -310,8 +320,10 @@ var TypeData = map[string]string{
"CameraIP": "string", "CameraIP": "string",
"CBR": "bool", "CBR": "bool",
"ClipDuration": "uint", "ClipDuration": "uint",
"DiffThreshold": "float",
"Exposure": "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks", "Exposure": "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks",
"Filters": "enums:NoOp,MOG,VariableFPS,KNN", "FileFPS": "int",
"Filters": "enums:NoOp,MOG,VariableFPS,KNN,Difference",
"FrameRate": "uint", "FrameRate": "uint",
"Height": "uint", "Height": "uint",
"HorizontalFlip": "bool", "HorizontalFlip": "bool",
@ -324,20 +336,21 @@ var TypeData = map[string]string{
"KNNMinArea": "float", "KNNMinArea": "float",
"KNNThreshold": "float", "KNNThreshold": "float",
"logging": "enum:Debug,Info,Warning,Error,Fatal", "logging": "enum:Debug,Info,Warning,Error,Fatal",
"Loop": "bool",
"MinFPS": "float", "MinFPS": "float",
"MinFrames": "uint", "MinFrames": "uint",
"mode": "enum:Normal,Paused,Burst", "mode": "enum:Normal,Paused,Burst,Loop",
"MOGHistory": "uint", "MOGHistory": "uint",
"MOGMinArea": "float", "MOGMinArea": "float",
"MOGThreshold": "float", "MOGThreshold": "float",
"MotionInterval": "int", "MotionInterval": "int",
"RBCapacity": "uint",
"RBMaxElements": "uint",
"RBWriteTimeout": "uint",
"Output": "enum:File,Http,Rtmp,Rtp", "Output": "enum:File,Http,Rtmp,Rtp",
"OutputPath": "string", "OutputPath": "string",
"Outputs": "enums:File,Http,Rtmp,Rtp", "Outputs": "enums:File,Http,Rtmp,Rtp",
"Quantization": "uint", "Quantization": "uint",
"RBCapacity": "uint",
"RBMaxElements": "uint",
"RBWriteTimeout": "uint",
"Rotation": "uint", "Rotation": "uint",
"RTMPURL": "string", "RTMPURL": "string",
"RTPAddress": "string", "RTPAddress": "string",
@ -521,6 +534,11 @@ func (c *Config) Validate() error {
} }
} }
if c.FileFPS <= 0 || (c.FileFPS > 0 && c.Input != InputFile) {
c.Logger.Log(logger.Info, pkg+"FileFPS bad or unset, defaulting", "FileFPS", defaultFileFPS)
c.FileFPS = defaultFileFPS
}
return nil return nil
} }

View File

@ -7,6 +7,7 @@ AUTHORS
Alan Noble <alan@ausocean.org> Alan Noble <alan@ausocean.org>
Dan Kortschak <dan@ausocean.org> Dan Kortschak <dan@ausocean.org>
Trek Hopton <trek@ausocean.org> Trek Hopton <trek@ausocean.org>
Scott Barnard <scott@ausocean.org>
LICENSE LICENSE
revid is Copyright (C) 2017-2020 the Australian Ocean Lab (AusOcean) revid is Copyright (C) 2017-2020 the Australian Ocean Lab (AusOcean)
@ -52,6 +53,7 @@ import (
"bitbucket.org/ausocean/av/filter" "bitbucket.org/ausocean/av/filter"
"bitbucket.org/ausocean/av/revid/config" "bitbucket.org/ausocean/av/revid/config"
"bitbucket.org/ausocean/iot/pi/netsender" "bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/utils/bitrate"
"bitbucket.org/ausocean/utils/ioext" "bitbucket.org/ausocean/utils/ioext"
"bitbucket.org/ausocean/utils/logger" "bitbucket.org/ausocean/utils/logger"
"bitbucket.org/ausocean/utils/vring" "bitbucket.org/ausocean/utils/vring"
@ -115,6 +117,9 @@ type Revid struct {
// err will channel errors from revid routines to the handle errors routine. // err will channel errors from revid routines to the handle errors routine.
err chan error err chan error
// bitrate is used for bitrate calculations.
bitrate bitrate.Calculator
} }
// New returns a pointer to a new Revid with the desired configuration, and/or // New returns a pointer to a new Revid with the desired configuration, and/or
@ -148,10 +153,8 @@ func (r *Revid) handleErrors() {
} }
// Bitrate returns the result of the most recent bitrate check. // Bitrate returns the result of the most recent bitrate check.
//
// TODO: get this working again.
func (r *Revid) Bitrate() int { func (r *Revid) Bitrate() int {
return -1 return r.bitrate.Bitrate()
} }
// reset swaps the current config of a Revid with the passed // reset swaps the current config of a Revid with the passed
@ -266,14 +269,14 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
return fmt.Errorf("could not initialise MTS ring buffer: %w", err) return fmt.Errorf("could not initialise MTS ring buffer: %w", err)
} }
w = newMtsSender( w = newMtsSender(
newHttpSender(r.ns, r.cfg.Logger.Log), newHttpSender(r.ns, r.cfg.Logger.Log, r.bitrate.Report),
r.cfg.Logger.Log, r.cfg.Logger.Log,
rb, rb,
r.cfg.ClipDuration, r.cfg.ClipDuration,
) )
mtsSenders = append(mtsSenders, w) mtsSenders = append(mtsSenders, w)
case config.OutputRTP: case config.OutputRTP:
w, err := newRtpSender(r.cfg.RTPAddress, r.cfg.Logger.Log, r.cfg.FrameRate) w, err := newRtpSender(r.cfg.RTPAddress, r.cfg.Logger.Log, r.cfg.FrameRate, r.bitrate.Report)
if err != nil { if err != nil {
r.cfg.Logger.Log(logger.Warning, pkg+"rtp connect error", "error", err.Error()) r.cfg.Logger.Log(logger.Warning, pkg+"rtp connect error", "error", err.Error())
} }
@ -295,6 +298,7 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
rtmpConnectionMaxTries, rtmpConnectionMaxTries,
rb, rb,
r.cfg.Logger.Log, r.cfg.Logger.Log,
r.bitrate.Report,
) )
if err != nil { if err != nil {
r.cfg.Logger.Log(logger.Warning, pkg+"rtmp connect error", "error", err.Error()) r.cfg.Logger.Log(logger.Warning, pkg+"rtmp connect error", "error", err.Error())
@ -342,6 +346,8 @@ func (r *Revid) setupPipeline(mtsEnc func(dst io.WriteCloser, rate float64) (io.
r.filters[i] = filter.NewVariableFPSFilter(dst, r.cfg.MinFPS, filter.NewMOGFilter(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.ShowWindows, r.cfg.MotionInterval)) r.filters[i] = filter.NewVariableFPSFilter(dst, r.cfg.MinFPS, filter.NewMOGFilter(dst, r.cfg.MOGMinArea, r.cfg.MOGThreshold, int(r.cfg.MOGHistory), r.cfg.ShowWindows, r.cfg.MotionInterval))
case config.FilterKNN: case config.FilterKNN:
r.filters[i] = filter.NewKNNFilter(dst, r.cfg.KNNMinArea, r.cfg.KNNThreshold, int(r.cfg.KNNHistory), int(r.cfg.KNNKernel), r.cfg.ShowWindows, r.cfg.MotionInterval) r.filters[i] = filter.NewKNNFilter(dst, r.cfg.KNNMinArea, r.cfg.KNNThreshold, int(r.cfg.KNNHistory), int(r.cfg.KNNKernel), r.cfg.ShowWindows, r.cfg.MotionInterval)
case config.FilterDifference:
r.filters[i] = filter.NewDifference(dst, r.cfg.ShowWindows, r.cfg.DiffThreshold)
default: default:
panic("Undefined Filter") panic("Undefined Filter")
} }
@ -428,13 +434,14 @@ func (r *Revid) Start() error {
return err return err
} }
err = r.input.Start() // Calculate delay between frames based on FileFPS.
if err != nil { d := time.Duration(0)
return fmt.Errorf("could not start input device: %w", err) if r.cfg.FileFPS != 0 {
d = time.Duration(1000/r.cfg.FileFPS) * time.Millisecond
} }
r.wg.Add(1) r.wg.Add(1)
go r.processFrom(r.input, 0) go r.processFrom(r.input, d)
r.running = true r.running = true
return nil return nil
@ -662,7 +669,7 @@ func (r *Revid) Update(vars map[string]string) error {
} }
case "Filters": case "Filters":
filters := strings.Split(value, ",") filters := strings.Split(value, ",")
m := map[string]int{"NoOp": config.FilterNoOp, "MOG": config.FilterMOG, "VariableFPS": config.FilterVariableFPS, "KNN": config.FilterKNN} m := map[string]int{"NoOp": config.FilterNoOp, "MOG": config.FilterMOG, "VariableFPS": config.FilterVariableFPS, "KNN": config.FilterKNN, "Difference": config.FilterDifference}
r.cfg.Filters = make([]int, len(filters)) r.cfg.Filters = make([]int, len(filters))
for i, filter := range filters { for i, filter := range filters {
v, ok := m[filter] v, ok := m[filter]
@ -810,6 +817,13 @@ func (r *Revid) Update(vars map[string]string) error {
break break
} }
r.cfg.KNNThreshold = v r.cfg.KNNThreshold = v
case "DiffThreshold":
v, err := strconv.ParseFloat(value, 64)
if err != nil {
r.cfg.Logger.Log(logger.Warning, pkg+"invalid DiffThreshold var", "value", value)
break
}
r.cfg.DiffThreshold = v
case "KNNKernel": case "KNNKernel":
v, err := strconv.Atoi(value) v, err := strconv.Atoi(value)
if err != nil { if err != nil {
@ -845,6 +859,18 @@ func (r *Revid) Update(vars map[string]string) error {
break break
} }
r.cfg.MOGHistory = uint(v) r.cfg.MOGHistory = uint(v)
case "FileFPS":
v, err := strconv.Atoi(value)
if err != nil {
r.cfg.Logger.Log(logger.Warning, pkg+"invalid FileFPS var", "value", value)
break
}
r.cfg.FileFPS = v
case "mode":
r.cfg.Loop = false
if value == "Loop" {
r.cfg.Loop = true
}
} }
} }
r.cfg.Logger.Log(logger.Info, pkg+"revid config changed", "config", fmt.Sprintf("%+v", r.cfg)) r.cfg.Logger.Log(logger.Info, pkg+"revid config changed", "config", fmt.Sprintf("%+v", r.cfg))
@ -853,14 +879,34 @@ func (r *Revid) Update(vars map[string]string) error {
// processFrom is run as a routine to read from a input data source, lex and // processFrom is run as a routine to read from a input data source, lex and
// then send individual access units to revid's encoders. // then send individual access units to revid's encoders.
func (r *Revid) processFrom(read io.Reader, delay time.Duration) { func (r *Revid) processFrom(in device.AVDevice, delay time.Duration) {
err := r.lexTo(r.filters[0], read, delay) defer r.wg.Done()
r.cfg.Logger.Log(logger.Debug, pkg+"finished lexing")
for l := true; l; l = r.cfg.Loop {
err := in.Start()
if err != nil {
r.err <- fmt.Errorf("could not start input device: %w", err)
return
}
// Lex data from input device, in, until finished or an error is encountered.
// For a continuous source e.g. a camera or microphone, we should remain
// in this call indefinitely unless in.Stop() is called and an io.EOF is forced.
r.cfg.Logger.Log(logger.Info, pkg+"lexing")
err = r.lexTo(r.filters[0], in, delay)
switch err { switch err {
case nil: // Do nothing. case nil, io.EOF:
case io.EOF: // TODO: handle this depending on loop mode. case io.ErrUnexpectedEOF:
r.cfg.Logger.Log(logger.Info, pkg+"unexpected EOF from input")
default: default:
r.err <- err r.err <- err
} }
r.wg.Done()
err = in.Stop()
if err != nil {
r.err <- fmt.Errorf("could not stop input source: %w", err)
}
}
r.cfg.Logger.Log(logger.Info, pkg+"finished lexing")
} }

View File

@ -61,19 +61,25 @@ const (
type httpSender struct { type httpSender struct {
client *netsender.Sender client *netsender.Sender
log func(lvl int8, msg string, args ...interface{}) log func(lvl int8, msg string, args ...interface{})
report func(sent int)
} }
// newHttpSender returns a pointer to a new httpSender. // newHttpSender returns a pointer to a new httpSender.
func newHttpSender(ns *netsender.Sender, log func(lvl int8, msg string, args ...interface{})) *httpSender { func newHttpSender(ns *netsender.Sender, log func(lvl int8, msg string, args ...interface{}), report func(sent int)) *httpSender {
return &httpSender{ return &httpSender{
client: ns, client: ns,
log: log, log: log,
report: report,
} }
} }
// Write implements io.Writer. // Write implements io.Writer.
func (s *httpSender) Write(d []byte) (int, error) { func (s *httpSender) Write(d []byte) (int, error) {
return len(d), httpSend(d, s.client, s.log) err := httpSend(d, s.client, s.log)
if err == nil {
s.report(len(d))
}
return len(d), err
} }
func (s *httpSender) Close() error { return nil } func (s *httpSender) Close() error { return nil }
@ -276,9 +282,10 @@ type rtmpSender struct {
ring *vring.Buffer ring *vring.Buffer
done chan struct{} done chan struct{}
wg sync.WaitGroup wg sync.WaitGroup
report func(sent int)
} }
func newRtmpSender(url string, timeout uint, retries int, rb *vring.Buffer, log func(lvl int8, msg string, args ...interface{})) (*rtmpSender, error) { func newRtmpSender(url string, timeout uint, retries int, rb *vring.Buffer, log func(lvl int8, msg string, args ...interface{}), report func(sent int)) (*rtmpSender, error) {
var conn *rtmp.Conn var conn *rtmp.Conn
var err error var err error
for n := 0; n < retries; n++ { for n := 0; n < retries; n++ {
@ -299,6 +306,7 @@ func newRtmpSender(url string, timeout uint, retries int, rb *vring.Buffer, log
log: log, log: log,
ring: rb, ring: rb,
done: make(chan struct{}), done: make(chan struct{}),
report: report,
} }
s.wg.Add(1) s.wg.Add(1)
go s.output() go s.output()
@ -364,6 +372,7 @@ func (s *rtmpSender) Write(d []byte) (int, error) {
if err != nil { if err != nil {
s.log(logger.Warning, pkg+"rtmpSender: ring buffer write error", "error", err.Error()) s.log(logger.Warning, pkg+"rtmpSender: ring buffer write error", "error", err.Error())
} }
s.report(len(d))
return len(d), nil return len(d), nil
} }
@ -404,9 +413,10 @@ type rtpSender struct {
log func(lvl int8, msg string, args ...interface{}) log func(lvl int8, msg string, args ...interface{})
encoder *rtp.Encoder encoder *rtp.Encoder
data []byte data []byte
report func(sent int)
} }
func newRtpSender(addr string, log func(lvl int8, msg string, args ...interface{}), fps uint) (*rtpSender, error) { func newRtpSender(addr string, log func(lvl int8, msg string, args ...interface{}), fps uint, report func(sent int)) (*rtpSender, error) {
conn, err := net.Dial("udp", addr) conn, err := net.Dial("udp", addr)
if err != nil { if err != nil {
return nil, err return nil, err
@ -414,6 +424,7 @@ func newRtpSender(addr string, log func(lvl int8, msg string, args ...interface{
s := &rtpSender{ s := &rtpSender{
log: log, log: log,
encoder: rtp.NewEncoder(conn, int(fps)), encoder: rtp.NewEncoder(conn, int(fps)),
report: report,
} }
return s, nil return s, nil
} }
@ -426,6 +437,7 @@ func (s *rtpSender) Write(d []byte) (int, error) {
if err != nil { if err != nil {
s.log(logger.Warning, pkg+"rtpSender: write error", err.Error()) s.log(logger.Warning, pkg+"rtpSender: write error", err.Error())
} }
s.report(len(d))
return len(d), nil return len(d), nil
} }