media-stream-player
Version:
Player built on top of media-stream-library
36 lines • 1.5 kB
JavaScript
import { MessageType, utils, components, } from 'media-stream-library';
/**
* Attach ONVIF metadata handlers to a pipeline
*
* @param pipeline The (HTML5 video) pipeline to modify
* @param handlers The handlers to deal with XML data
*/
export const attachMetadataHandler = (pipeline, { parser, cb }) => {
/**
* When a metadata handler is available on this component, it will be
* called in sync with the player, using a scheduler to synchronize the
* callback with the video presentation time.
*/
const scheduler = new utils.Scheduler(pipeline, cb, 30);
const xmlParser = new DOMParser();
const xmlMessageHandler = (msg) => {
const xmlDocument = xmlParser.parseFromString(msg.data.toString(), 'text/xml');
const newMsg = parser({ ...msg, xmlDocument });
if (msg.ntpTimestamp !== undefined) {
scheduler.run(newMsg);
}
};
// Add extra components to the pipeline.
const onvifDepay = new components.ONVIFDepay();
const onvifHandlerPipe = components.Tube.fromHandlers((msg) => {
if (msg.type === MessageType.XML) {
xmlMessageHandler(msg);
}
}, undefined);
pipeline.insertAfter(pipeline.rtsp, onvifDepay);
pipeline.insertAfter(onvifDepay, onvifHandlerPipe);
// Initialize the scheduler when presentation time is ready
pipeline.onSync = (ntpPresentationTime) => scheduler.init(ntpPresentationTime);
return scheduler;
};
//# sourceMappingURL=metadata.js.map