Skip to content

Commit f4070a7

Browse files
committed
fix: Adjust HLS settings to not initialize twice in certain scenarios
1 parent 0b2a93e commit f4070a7

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed

ember-stereo/src/stereo-connections/base.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ export default class Sound extends Evented {
2525
static canPlay(url, mimeType) {
2626
let usablePlatform = this.canUseConnection(url);
2727
if (!usablePlatform) {
28-
debug('ember-stereo:canPlay')(
29-
`can not use ${this.debugName} on this platform`
30-
);
28+
debug('ember-stereo:canPlay')(`can not use connection on this platform`);
3129
return false;
3230
}
3331

@@ -36,14 +34,20 @@ export default class Sound extends Evented {
3634
if (mimeType) {
3735
let result = this.canPlayMimeType(mimeType);
3836
if (result) {
39-
debug(this.debugName)(`can play mime type ${mimeType} (${this.url})`);
37+
debug('ember-stereo:canPlay')(
38+
`can play mime type ${mimeType} (${this.url})`
39+
);
4040
} else {
41-
debug(this.debugName)(`can't play mime type ${mimeType} (${this.url})`);
41+
debug('ember-stereo:canPlay')(
42+
`can't play mime type ${mimeType} (${this.url})`
43+
);
4244
}
4345

4446
return result;
4547
} else {
46-
debug(this.debugName)(`can't play mime type ${mimeType} (${this.url})`);
48+
debug('ember-stereo:canPlay')(
49+
`can't play mime type ${mimeType} (${this.url})`
50+
);
4751
/* eslint-enable no-console */
4852
return true;
4953
}

ember-stereo/src/stereo-connections/hls.js

+50-36
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ export default class HLSSound extends BaseSound {
7878

7979
@waitFor
8080
async setup() {
81-
await this.loadHLS().then(({ HLS }) => {
82-
let video = document.createElement('video');
83-
video.setAttribute('crossorigin', 'anonymous');
84-
this.video = video;
85-
86-
let options = { debug: false, startFragPrefetch: true };
81+
if (!this.hls && !this.video) {
82+
this.debug('Setting up HLS');
83+
let options = {
84+
debug: true,
85+
startFragPrefetch: true,
86+
};
8787

8888
if (this.options?.xhr) {
8989
options.xhrSetup = (xhr, url) => {
@@ -105,13 +105,29 @@ export default class HLSSound extends BaseSound {
105105
delete this.options.xhr;
106106
}
107107

108-
let hls = new HLS({ ...options, ...(this.options || {}) });
108+
await this.loadHLS().then(({ HLS }) => {
109+
if (this.hls) {
110+
this.hls.destroy();
111+
}
109112

110-
this.hls = hls;
111-
this._setupHLSEvents(hls, HLS);
112-
this._setupPlayerEvents(video);
113-
hls.attachMedia(video);
114-
});
113+
if (this.video) {
114+
this.video.removeAttribute('src');
115+
}
116+
117+
let hls = new HLS({ ...options, ...(this.options || {}) });
118+
this.hls = hls;
119+
120+
let video = document.createElement('video');
121+
video.setAttribute('crossorigin', 'anonymous');
122+
video.setAttribute('webkit-playsinline', '');
123+
video.setAttribute('playsinline', '');
124+
this.video = video;
125+
126+
this._setupHLSEvents(hls, HLS);
127+
this._setupPlayerEvents(this.video);
128+
hls.attachMedia(this.video);
129+
});
130+
}
115131
}
116132

117133
_setupHLSEvents(instance, HLS) {
@@ -133,35 +149,33 @@ export default class HLSSound extends BaseSound {
133149
instance.on(HLS.Events.ERROR, (e, data) => this._onHLSError(e, data, HLS));
134150

135151
instance.on(HLS.Events.MEDIA_ATTACHED, () => {
136-
this.debug('media attached');
152+
this.debug('media attached, loading source');
137153
instance.loadSource(this.url);
154+
});
138155

139-
instance.on(HLS.Events.MANIFEST_PARSED, (e, data) => {
140-
this.debug(
141-
`manifest parsed and loaded, found ${data.levels.length} quality level(s)`
142-
);
143-
this.manifest = data;
144-
});
156+
instance.on(HLS.Events.MANIFEST_PARSED, (e, data) => {
157+
this.debug(
158+
`manifest parsed and loaded, found ${data.levels.length} quality level(s)`
159+
);
160+
this.manifest = data;
161+
});
145162

146-
instance.on(HLS.Events.LEVEL_LOADED, (e, data) => {
147-
this.debug(`level ${data.level} loaded`);
148-
this.live = data.details.live;
149-
this._signalAudioIsReady();
150-
});
163+
instance.on(HLS.Events.LEVEL_LOADED, (e, data) => {
164+
this.debug(`level ${data.level} loaded`);
165+
this.live = data.details.live;
166+
this._signalAudioIsReady();
167+
});
151168

152-
instance.on(HLS.Events.AUDIO_TRACK_LOADED, () => {
153-
this.debug('audio track loaded');
154-
this._signalAudioIsReady();
155-
});
169+
instance.on(HLS.Events.AUDIO_TRACK_LOADED, () => {
170+
this.debug('audio track loaded');
171+
this._signalAudioIsReady();
172+
});
156173

157-
instance.on(HLS.Events.ERROR, (e, data) =>
158-
this._onHLSError(e, data, HLS)
159-
);
174+
instance.on(HLS.Events.ERROR, (e, data) => this._onHLSError(e, data, HLS));
160175

161-
instance.on(HLS.Events.FRAG_CHANGED, (e, f) => {
162-
// this._updateAudioBuffer(f.frag);
163-
this._updateId3Info(f.frag);
164-
});
176+
instance.on(HLS.Events.FRAG_CHANGED, (e, f) => {
177+
// this._updateAudioBuffer(f.frag);
178+
this._updateId3Info(f.frag);
165179
});
166180
}
167181

@@ -267,7 +281,7 @@ export default class HLSSound extends BaseSound {
267281

268282
_onHLSError(error, data, HLS) {
269283
if (data.fatal) {
270-
this.debug(data);
284+
this.debug('HLS fatal error', data);
271285
switch (data.type) {
272286
case HLS.ErrorTypes.NETWORK_ERROR:
273287
this._giveUpAndDie(`${data.details}`);

0 commit comments

Comments
 (0)