Skip to content

Commit cf89082

Browse files
committed
improve: fix Mixcloud playback to use HLSHandler
1 parent e6ed545 commit cf89082

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/sources/mixcloud.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { PassThrough } from 'node:stream'
22
import {
33
encodeTrack,
44
http1makeRequest,
5-
loadHLSPlaylist,
65
logger,
76
makeRequest
87
} from '../utils.js'
8+
import HLSHandler from '../playback/hls/HLSHandler.js'
99

1010
const DECRYPTION_KEY = 'IFYOUWANTTHEARTISTSTOGETPAIDDONOTDOWNLOADFROMMIXCLOUD'
1111

@@ -335,19 +335,19 @@ export default class MixcloudSource {
335335
}
336336
}
337337

338-
if (encryptedUrl) {
338+
if (encryptedHls) {
339339
return {
340-
url: this._decrypt(encryptedUrl),
341-
protocol: 'https',
342-
format: 'aac'
340+
url: this._decrypt(encryptedHls),
341+
protocol: 'hls',
342+
format: 'mpegts'
343343
}
344344
}
345345

346-
if (encryptedHls) {
346+
if (encryptedUrl) {
347347
return {
348-
url: this._decrypt(encryptedHls),
349-
protocol: 'hls',
350-
format: 'aac'
348+
url: this._decrypt(encryptedUrl),
349+
protocol: 'https',
350+
format: 'm4a'
351351
}
352352
}
353353

@@ -357,9 +357,16 @@ export default class MixcloudSource {
357357
async loadStream(_decodedTrack, url, protocol) {
358358
try {
359359
if (protocol === 'hls') {
360-
const stream = new PassThrough()
361-
loadHLSPlaylist(url, stream)
362-
return { stream, type: 'aac' }
360+
const stream = new HLSHandler(url, {
361+
type: 'mpegts',
362+
strategy: 'segmented',
363+
localAddress: this.nodelink.routePlanner?.getIP(),
364+
headers: {
365+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
366+
'Referer': 'https://www.mixcloud.com/'
367+
}
368+
})
369+
return { stream, type: 'mpegts' }
363370
}
364371

365372
const options = {
@@ -380,14 +387,18 @@ export default class MixcloudSource {
380387

381388
const stream = new PassThrough()
382389
response.stream.on('data', (chunk) => stream.write(chunk))
383-
response.stream.on('end', () => stream.emit('finishBuffering'))
390+
response.stream.on('end', () => {
391+
if (!stream.writableEnded) {
392+
stream.emit('finishBuffering')
393+
stream.end()
394+
}
395+
})
384396
response.stream.on('error', (error) => {
385397
logger('error', 'Mixcloud', `Upstream stream error: ${error.message}`)
386-
stream.emit('error', error)
387-
stream.emit('finishBuffering')
398+
if (!stream.destroyed) stream.destroy(error)
388399
})
389400

390-
return { stream, type: protocol === 'hls' ? 'aac' : 'm4a' }
401+
return { stream, type: 'm4a' }
391402
} catch (e) {
392403
logger('error', 'Mixcloud', `Failed to load stream: ${e.message}`)
393404
return { exception: { message: e.message, severity: 'fault' } }

0 commit comments

Comments
 (0)