Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add media-types module with simpleTypeFromSourceType function #4

Merged
merged 3 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

# @videojs/vhs-utils

Objects and functions shared throughtout @videojs/http-streaming code
vhs-utils serves two purposes:

1. It extracts objects and functions shared throughout @videojs/http-streaming code to save on package size. See [the original @videojs/http-streaming PR](https://github.com/videojs/http-streaming/pull/637) for details.
2. It exports functions useful to plugin authors to mimic some logic VHS uses internally.
gkatsev marked this conversation as resolved.
Show resolved Hide resolved

Note that for the case of 2, although the functions are exported, they may change at any time, although appropriate semantic versioning of this module will be maintained.

## Installation

Expand Down
36 changes: 36 additions & 0 deletions src/media-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const MPEGURL_REGEX = /^(audio|video|application)\/(x-|vnd\.apple\.)?mpegurl/i;
const DASH_REGEX = /^application\/dash\+xml/i;

/**
* Returns a string that describes the type of source based on a video source object's
* media type.
*
* @see {@link https://dev.w3.org/html5/pf-summary/video.html#dom-source-type|Source Type}
*
* @param {string} type
* Video source object media type
* @return {('hls'|'dash'|'vhs-json'|null)}
* VHS source type string
*/
export const simpleTypeFromSourceType = (type) => {
if (MPEGURL_REGEX.test(type)) {
return 'hls';
}

if (DASH_REGEX.test(type)) {
return 'dash';
}

// Denotes the special case of a manifest object passed to http-streaming instead of a
// source URL.
//
// See https://en.wikipedia.org/wiki/Media_type for details on specifying media types.
//
// In this case, vnd is for vendor, VHS is for this project, and the +json suffix
// identifies the structure of the media type.
if (type === 'application/vnd.vhs+json') {
return 'vhs-json';
}

return null;
};
41 changes: 41 additions & 0 deletions test/media-types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import QUnit from 'qunit';
import { simpleTypeFromSourceType } from '../src/media-types';

QUnit.module('simpleTypeFromSourceType');

QUnit.test('simpleTypeFromSourceType converts HLS mime types to hls', function(assert) {
assert.equal(
simpleTypeFromSourceType('aPplicatiOn/x-MPegUrl'),
'hls',
'supports application/x-mpegurl'
);
assert.equal(
simpleTypeFromSourceType('aPplicatiOn/VnD.aPPle.MpEgUrL'),
'hls',
'supports application/vnd.apple.mpegurl'
);
});

QUnit.test('simpleTypeFromSourceType converts DASH mime type to dash', function(assert) {
assert.equal(
simpleTypeFromSourceType('aPplication/dAsh+xMl'),
'dash',
'supports application/dash+xml'
);
});

QUnit.test(
'simpleTypeFromSourceType does not convert non HLS/DASH mime types',
function(assert) {
assert.notOk(simpleTypeFromSourceType('video/mp4'), 'does not support video/mp4');
assert.notOk(simpleTypeFromSourceType('video/x-flv'), 'does not support video/x-flv');
}
);

QUnit.test('simpleTypeFromSourceType converts VHS media type to vhs-json', function(assert) {
assert.equal(
simpleTypeFromSourceType('application/vnd.vhs+json'),
'vhs-json',
'supports application/vnd.vhs+json'
);
});