Skip to content

Commit

Permalink
fix: 支持mp3
Browse files Browse the repository at this point in the history
  • Loading branch information
wudechang committed Sep 3, 2024
1 parent d9fd803 commit 24bfa0e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/xgplayer-hls/src/hls/buffer-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class BufferService {
} else if (this._mse) {
const isFirstAppend = !this._sourceCreated
if (isFirstAppend) {
this._createMseSource(video?.codec, audio?.codec)
this._createMseSource(video?.codec, audio?.codec, audio?.container)
}
this._needInitSegment = false
const mse = this._mse
Expand Down Expand Up @@ -265,7 +265,7 @@ export class BufferService {
/**
* @private
*/
_createMseSource (videoCodec, audioCodec) {
_createMseSource (videoCodec, audioCodec, container) {
logger.debug(`create mse source, videoCodec=${videoCodec}, audioCodec=${audioCodec}`)
const mse = this._mse
if (!mse) return
Expand All @@ -276,6 +276,9 @@ export class BufferService {
if (audioCodec) {
mse.createSource(MSE.AUDIO, `audio/mp4;codecs=${audioCodec}`)
this._sourceCreated = true
} else if (container) {
mse.createSource(MSE.AUDIO, `${container};codecs=""`)
this._sourceCreated = true
}
this.hls.emit(EVENT.SOURCEBUFFER_CREATED)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class Transmuxer {
timescale: audioTrack.timescale,
firstDts: audioTrack.firstDts / videoTrack.timescale,
firstPts: audioTrack.firstPts / videoTrack.timescale,
duration: audioTrack.samplesDuration / videoTrack.timescale
duration: audioTrack.samplesDuration / videoTrack.timescale,
container: audioTrack.container
}
const newId = `${videoTrack.codec}/${videoTrack.width}/${videoTrack.height}/${audioTrack.codec}/${audioTrack.config}`
if (newId !== this._initSegmentId) {
Expand Down
4 changes: 3 additions & 1 deletion packages/xgplayer-transmuxer/src/codec/mpeg.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ export class MPEG {
const frameDuration = (header.samplesPerFrame * 90000) / header.sampleRate
const stamp = pts + frameIndex * frameDuration
const sample = {
unit: data.subarray(offset, offset + header.frameLength),
data: data.subarray(offset, offset + header.frameLength),
pts: stamp,
dts: stamp
}
sample.size = sample.data.byteLength
track.config = []
track.channelCount = header.channelCount
track.sampleRate = header.sampleRate
track.container = 'audio/mpeg'
track.samples.push(sample)

return { length: header.frameLength }
Expand Down
4 changes: 3 additions & 1 deletion packages/xgplayer-transmuxer/src/model/audio-track.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class AudioTrack {

codec = ''

container = ''

sequenceNumber = 0

sampleDuration = 0
Expand Down Expand Up @@ -77,7 +79,7 @@ export class AudioTrack {
return !!(
this.sampleRate &&
this.channelCount &&
this.codec &&
(this.codec || this.container) &&
(this.codecType === AudioCodecType.AAC ||
this.codecType === AudioCodecType.G711PCMA ||
this.codecType === AudioCodecType.G711PCMU ||
Expand Down

0 comments on commit 24bfa0e

Please sign in to comment.