Skip to content

Commit

Permalink
feat: Add TS transmuxer
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad committed Jul 2, 2023
1 parent b1e7cc4 commit 4a4d84f
Show file tree
Hide file tree
Showing 9 changed files with 593 additions and 38 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ Shaka Player supports:
- Raw MP3 to MP3 in MP4
- Raw AC-3 to AC-3 in MP4
- Raw EC-3 to EC-3 in MP4
- AAC in MPEG-2 TS to AAC in MP4,
with help from [mux.js][] v6.2.0+
- AAC in MPEG-2 TS to AAC in MP4
- AC-3 in MPEG-2 TS to AC-3 in MP4
- EC-3 in MPEG-2 TS to EC-3 in MP4
- H.264 in MPEG-2 TS to H.264 in MP4,
with help from [mux.js][] v6.2.0+
- Muxed AAC and H.264 in MPEG-2 TS to AAC and H.264 in MP4,
Expand Down
1 change: 1 addition & 0 deletions build/types/transmuxer
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
+../../lib/transmuxer/mp3_transmuxer.js
+../../lib/transmuxer/mpeg_audio.js
+../../lib/transmuxer/muxjs_transmuxer.js
+../../lib/transmuxer/ts_transmuxer.js
6 changes: 2 additions & 4 deletions lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,7 @@ shaka.media.MediaSourceEngine = class {
shaka.util.MimeUtils.getFullTypeWithAllCodecs(
stream.mimeType, stream.codecs);
const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
if (needTransmux &&
TransmuxerEngine.isSupported(mimeTypeWithAllCodecs, contentType)) {
if (needTransmux) {
const transmuxerPlugin =
TransmuxerEngine.findTransmuxer(mimeTypeWithAllCodecs);
if (transmuxerPlugin) {
Expand Down Expand Up @@ -1649,8 +1648,7 @@ shaka.media.MediaSourceEngine = class {
shaka.util.MimeUtils.getFullTypeWithAllCodecs(
stream.mimeType, stream.codecs);
const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
if (needTransmux &&
TransmuxerEngine.isSupported(newMimeTypeWithAllCodecs, contentType)) {
if (needTransmux) {
const transmuxerPlugin =
TransmuxerEngine.findTransmuxer(newMimeTypeWithAllCodecs);
if (transmuxerPlugin) {
Expand Down
51 changes: 32 additions & 19 deletions lib/transmuxer/transmuxer_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

goog.provide('shaka.transmuxer.TransmuxerEngine');

goog.require('goog.asserts');
goog.require('shaka.util.IDestroyable');


Expand All @@ -27,41 +26,58 @@ shaka.transmuxer.TransmuxerEngine = class {
/**
* @param {string} mimeType
* @param {!shaka.extern.TransmuxerPlugin} plugin
* @param {number} priority
* @export
*/
static registerTransmuxer(mimeType, plugin, priority) {
const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
goog.asserts.assert(priority == undefined || priority > 0,
'explicit priority must be > 0');
const mimeTypeNormalizated = TransmuxerEngine.normalizeMimeType_(mimeType);
const existing = TransmuxerEngine.transmuxerMap_[mimeTypeNormalizated];
if (!existing || priority >= existing.priority) {
TransmuxerEngine.transmuxerMap_[mimeTypeNormalizated] = {
priority: priority,
plugin: plugin,
};
}
const key = mimeTypeNormalizated + '-' + priority;
TransmuxerEngine.transmuxerMap_[key] = {
priority: priority,
plugin: plugin,
};
}

/**
* @param {string} mimeType
* @param {number} priority
* @export
*/
static unregisterTransmuxer(mimeType) {
static unregisterTransmuxer(mimeType, priority) {
const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
const mimeTypeNormalizated = TransmuxerEngine.normalizeMimeType_(mimeType);
delete TransmuxerEngine.transmuxerMap_[mimeTypeNormalizated];
const key = mimeTypeNormalizated + '-' + priority;
delete TransmuxerEngine.transmuxerMap_[key];
}

/**
* @param {string} mimeType
* @param {string=} contentType
* @return {?shaka.extern.TransmuxerPlugin}
* @export
*/
static findTransmuxer(mimeType) {
static findTransmuxer(mimeType, contentType) {
const TransmuxerEngine = shaka.transmuxer.TransmuxerEngine;
const mimeTypeNormalizated = TransmuxerEngine.normalizeMimeType_(mimeType);
const object = TransmuxerEngine.transmuxerMap_[mimeTypeNormalizated];
return object ? object.plugin : null;
const priorities = [
TransmuxerEngine.PluginPriority.APPLICATION,
TransmuxerEngine.PluginPriority.PREFERRED,
TransmuxerEngine.PluginPriority.FALLBACK,
];
for (const priority of priorities) {
const key = mimeTypeNormalizated + '-' + priority;
const object = TransmuxerEngine.transmuxerMap_[key];
if (object) {
const transmuxer = object.plugin();
const isSupported = transmuxer.isSupported(mimeType, contentType);
transmuxer.destroy();
if (isSupported) {
return object.plugin;
}
}
}
return null;
}

/**
Expand All @@ -85,10 +101,7 @@ shaka.transmuxer.TransmuxerEngine = class {
if (!transmuxerPlugin) {
return false;
}
const transmuxer = transmuxerPlugin();
const isSupported = transmuxer.isSupported(mimeType, contentType);
transmuxer.destroy();
return isSupported;
return true;
}

/**
Expand Down
Loading

0 comments on commit 4a4d84f

Please sign in to comment.