Skip to content

Commit

Permalink
format audio only playlists differently
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Mar 17, 2021
1 parent 7291421 commit 740f110
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/toM3u8.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const addSidxSegmentsToPlaylists = (playlists, sidxMapping = {}) => {
return playlists;
};

export const formatAudioPlaylist = ({ attributes, segments, sidx }) => {
export const formatAudioPlaylist = ({ attributes, segments, sidx }, isAudioOnly) => {
const playlist = {
attributes: {
NAME: attributes.id,
Expand All @@ -91,6 +91,11 @@ export const formatAudioPlaylist = ({ attributes, segments, sidx }) => {
playlist.sidx = sidx;
}

if (isAudioOnly) {
playlist.attributes.AUDIO = 'audio';
playlist.attributes.SUBTITLES = 'subs';
}

return playlist;
};

Expand Down Expand Up @@ -129,7 +134,7 @@ export const formatVttPlaylist = ({ attributes, segments }) => {
};
};

export const organizeAudioPlaylists = (playlists, sidxMapping = {}) => {
export const organizeAudioPlaylists = (playlists, sidxMapping = {}, isAudioOnly = false) => {
let mainPlaylist;

const formattedPlaylists = playlists.reduce((a, playlist) => {
Expand All @@ -155,7 +160,9 @@ export const organizeAudioPlaylists = (playlists, sidxMapping = {}) => {
};
}

a[label].playlists.push(addSidxSegmentsToPlaylist(formatAudioPlaylist(playlist), sidxMapping));
const formatted = addSidxSegmentsToPlaylist(formatAudioPlaylist(playlist, isAudioOnly), sidxMapping);

a[label].playlists.push(formatted);

if (typeof mainPlaylist === 'undefined' && role === 'main') {
mainPlaylist = playlist;
Expand Down Expand Up @@ -280,8 +287,10 @@ export const toM3u8 = (dashPlaylists, locations, sidxMapping = {}) => {
master.suggestedPresentationDelay = suggestedPresentationDelay;
}

const isAudioOnly = master.playlists.length === 0;

if (audioPlaylists.length) {
master.mediaGroups.AUDIO.audio = organizeAudioPlaylists(audioPlaylists, sidxMapping);
master.mediaGroups.AUDIO.audio = organizeAudioPlaylists(audioPlaylists, sidxMapping, isAudioOnly);
}

if (vttPlaylists.length) {
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import locationsTemplate from './manifests/locations.mpd';
import multiperiod from './manifests/multiperiod.mpd';
import webmsegments from './manifests/webmsegments.mpd';
import multiperiodDynamic from './manifests/multiperiod-dynamic.mpd';
import audioOnly from './manifests/audio-only.mpd';
import {
parsedManifest as maatVttSegmentTemplateManifest
} from './manifests/maat_vtt_segmentTemplate.js';
Expand Down Expand Up @@ -42,6 +43,10 @@ import {
parsedManifest as vttCodecsManifest
} from './manifests/vtt_codecs.js';

import {
parsedManifest as audioOnlyManifest
} from './manifests/audio-only.js';

QUnit.module('mpd-parser');

QUnit.test('has VERSION', function(assert) {
Expand Down Expand Up @@ -88,6 +93,10 @@ QUnit.test('has parse', function(assert) {
name: 'vtt_codecs',
input: vttCodecsTemplate,
expected: vttCodecsManifest
}, {
name: 'audio-only',
input: audioOnly,
expected: audioOnlyManifest
}].forEach(({ name, input, expected }) => {
QUnit.test(`${name} test manifest`, function(assert) {
const actual = parse(input);
Expand Down
106 changes: 106 additions & 0 deletions test/manifests/audio-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
export const parsedManifest = {
allowCache: true,
discontinuityStarts: [],
segments: [],
endList: true,
mediaGroups: {
'AUDIO': {
audio: {
en: {
language: 'en',
autoselect: true,
default: true,
playlists: [
{
attributes: {
'NAME': '0',
'BANDWIDTH': 130803,
'CODECS': 'mp4a.40.2',
'PROGRAM-ID': 1,
'AUDIO': 'audio',
'SUBTITLES': 'subs'
},
uri: '',
endList: true,
timeline: 0,
resolvedUri: '',
targetDuration: 60,
segments: [],
mediaSequence: 1,
sidx: {
uri: 'http://localhost:10000/test/audio_en_2c_128k_aac.mp4',
resolvedUri: 'http://localhost:10000/test/audio_en_2c_128k_aac.mp4',
byterange: {
length: 224,
offset: 786
},
map: {
uri: '',
resolvedUri: 'http://localhost:10000/test/audio_en_2c_128k_aac.mp4',
byterange: {
length: 786,
offset: 0
}
},
duration: 60,
timeline: 0,
number: 0
}
}
],
uri: ''
},
es: {
language: 'es',
autoselect: true,
default: false,
playlists: [
{
attributes: {
'NAME': '1',
'BANDWIDTH': 130405,
'CODECS': 'mp4a.40.2',
'PROGRAM-ID': 1,
'AUDIO': 'audio',
'SUBTITLES': 'subs'
},
uri: '',
endList: true,
timeline: 0,
resolvedUri: '',
targetDuration: 60,
segments: [],
mediaSequence: 1,
sidx: {
uri: 'http://localhost:10000/test/audio_es_2c_128k_aac.mp4',
resolvedUri: 'http://localhost:10000/test/audio_es_2c_128k_aac.mp4',
byterange: {
length: 224,
offset: 786
},
map: {
uri: '',
resolvedUri: 'http://localhost:10000/test/audio_es_2c_128k_aac.mp4',
byterange: {
length: 786,
offset: 0
}
},
duration: 60,
timeline: 0,
number: 0
}
}
],
uri: ''
}
}
},
'VIDEO': {},
'CLOSED-CAPTIONS': {},
'SUBTITLES': {}
},
uri: '',
duration: 60,
playlists: []
};
25 changes: 25 additions & 0 deletions test/manifests/audio-only.mpd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Generated with https://github.com/google/shaka-packager version v2.4.1-c731217-release-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011" minBufferTime="PT2S" type="static" mediaPresentationDuration="PT60S">
<Period id="0">
<AdaptationSet id="0" contentType="audio" lang="en" subsegmentAlignment="true">
<Representation id="0" bandwidth="130803" codecs="mp4a.40.2" mimeType="audio/mp4" audioSamplingRate="48000">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
<BaseURL>audio_en_2c_128k_aac.mp4</BaseURL>
<SegmentBase indexRange="786-1009" timescale="48000">
<Initialization range="0-785"/>
</SegmentBase>
</Representation>
</AdaptationSet>
<AdaptationSet id="1" contentType="audio" lang="es" subsegmentAlignment="true">
<Representation id="1" bandwidth="130405" codecs="mp4a.40.2" mimeType="audio/mp4" audioSamplingRate="48000">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
<BaseURL>audio_es_2c_128k_aac.mp4</BaseURL>
<SegmentBase indexRange="786-1009" timescale="48000">
<Initialization range="0-785"/>
</SegmentBase>
</Representation>
</AdaptationSet>

</Period>
</MPD>

0 comments on commit 740f110

Please sign in to comment.