Skip to content

Commit

Permalink
Merge branch 'refactor-live' of https://github.com/bytedance/xgplayer
Browse files Browse the repository at this point in the history
…into refactor-live-loaderbuffer
  • Loading branch information
yqjiang committed Sep 20, 2019
2 parents 8bcc316 + 981f8ad commit bc6007c
Show file tree
Hide file tree
Showing 27 changed files with 4,098 additions and 1,082 deletions.
2 changes: 1 addition & 1 deletion packages/xgplayer-flv.js/browser/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/xgplayer-flv.js/dist/index.js

Large diffs are not rendered by default.

1,880 changes: 940 additions & 940 deletions packages/xgplayer-flv.js/src/flv/demux/flv-demuxer.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/xgplayer-flv.js/src/flv/demux/sps-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class SPSParser {
fps_num: fps_num
},

sar_ratio: {
par_ratio: {
width: sar_width,
height: sar_height
},
Expand Down Expand Up @@ -278,4 +278,4 @@ class SPSParser {

}

export default SPSParser;
export default SPSParser;
22 changes: 8 additions & 14 deletions packages/xgplayer-flv/src/Flv.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FlvDemuxer from './parse/demux'
import Mp4Remuxer from '../../xgplayer-remux/src/mp4'
import FetchLoader from '../../xgplayer-loader-fetch/src/index'
import { XgBuffer } from '../../xgplayer-buffer/src/index'
import { Tracks } from '../../xgplayer-buffer/src/track'
Expand Down Expand Up @@ -41,6 +42,7 @@ class FlvController {
this._context.registry('FETCH_LOADER', FetchLoader)
this._context.registry('LOADER_BUFFER', XgBuffer)
this._context.registry('TRACKS', Tracks)
this._context.registry('MP4_REMUXER', Mp4Remuxer)

this.initListeners()
this.initSourceOpenAndInitSegmentEvent()
Expand Down Expand Up @@ -76,24 +78,16 @@ class FlvController {
*/
initSourceOpenAndInitSegmentEvent () {
const sourceOpentask = createAsyncTask()
const initSegmentTask = createAsyncTask()
// const initSegmentTask = createAsyncTask()

this.mse.once('sourceopen', sourceOpentask.resolve)
this.once(REMUX_EVENTS.INIT_SEGMENT, initSegmentTask.resolve)

Promise.all([
initSegmentTask.promise,
sourceOpentask.promise
]).then((result) => {
if (result && result[0]) {
const initSegment = result[0]
// append ftyp and moov
this.handleAppendInitSegment(initSegment)
}
this.on(REMUX_EVENTS.INIT_SEGMENT, (type, segment) => {
this.handleAppendInitSegment(type, segment)
})

}

handleAppendInitSegment (initSegment) {
handleAppendInitSegment (type, initSegment) {
this.state.initSegmentArrived = true
this.mse.appendBuffer(initSegment)
}
Expand Down Expand Up @@ -152,7 +146,7 @@ class FlvController {
destroy () {}

get isSeekable () {
if (!this._context || !this._context.mediaInfo) {
if (!this._context || !this._context.mediaInfo.isComplete()) {
return true
}
return this._context.mediaInfo.keyframes !== null && this._context.mediaInfo.keyframes !== undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/xgplayer-flv/src/constants/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const LOADER_EVENTS = {
export const DEMUX_EVENTS = {
DEMUX_COMPLETE: 'DEMUX_COMPLETE',
DEMUX_ERROR: 'DEMUX_ERROR',
METADATA_PARSED: 'METADATA_COMPLETE',
METADATA_PARSED: 'METADATA_PARSED',
VIDEO_METADATA_CHANGE: 'VIDEO_METADATA_CHANGE',
AUDIO_METADATA_CHANGE: 'AUDIO_METADATA_CHANGE',
MEDIA_INFO: 'MEDIA_INFO'
Expand Down
30 changes: 17 additions & 13 deletions packages/xgplayer-flv/src/parse/demux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ class FlvDemuxer {
const onMetaData = this._context.onMetaData = info ? info.onMetaData : undefined

// fill mediaInfo
this._context.duration = onMetaData.duration
this._context.hasVideo = onMetaData.hasVideo
this._context.hsaAudio = onMetaData.hsaAudio
this._context.mediaInfo.duration = onMetaData.duration
this._context.mediaInfo.hasVideo = onMetaData.hasVideo
this._context.mediaInfo.hsaAudio = onMetaData.hsaAudio

let validate = this._datasizeValidator(chunk.datasize)
if (validate) {
Expand All @@ -231,7 +231,7 @@ class FlvDemuxer {
if (audioTrack && !audioTrack.hasSpecificConfig) {
let meta = audioTrack.meta
if (onMetaData.audiosamplerate) {
meta.audioSampleRate = onMetaData.audiosamplerate
meta.sampleRate = onMetaData.audiosamplerate
}

if (onMetaData.audiochannels) {
Expand Down Expand Up @@ -308,7 +308,7 @@ class FlvDemuxer {
}

if (format === 10 && !this._hasAudioSequence) {
meta.audioSampleRate = this._switchAudioSamplingFrequency(info)
meta.sampleRate = this._switchAudioSamplingFrequency(info)
meta.sampleRateIndex = (info & 12) >>> 2
meta.frameLenth = (info & 2) >>> 1
meta.channelCount = info & 1
Expand All @@ -327,17 +327,19 @@ class FlvDemuxer {
audioSampleRate = aacHeader.audiosamplerate || meta.audioSampleRate
audioSampleRateIndex = aacHeader.sampleRateIndex || meta.sampleRateIndex
refSampleDuration = Math.floor(1024 / audioSampleRate * meta.timescale)

meta.channelCount = aacHeader.channelCount
meta.audioSampleRate = audioSampleRate
meta.sampleRate = audioSampleRate
meta.sampleRateIndex = audioSampleRateIndex
meta.refSampleDuration = refSampleDuration
meta.duration = this._context.mediaInfo.duration

const audioMedia = this._context.mediaInfo.audio

// fill audio media info
audioMedia.codec = aacHeader.codec
audioMedia.channelCount = aacHeader.channelCount
audioMedia.sampleRate = aacHeader.audioSampleRate
audioMedia.sampleRate = audioSampleRate
audioMedia.sampleRateIndex = aacHeader.audioSampleRateIndex

if (this._hasScript && !this._hasAudioSequence && (!this.tracks.videoTrack || this._hasVideoSequence)) {
Expand All @@ -349,8 +351,8 @@ class FlvDemuxer {
this._hasAudioSequence = true
} else {
chunk.data = chunk.data.slice(1, chunk.data.length)
this.emit(DEMUX_EVENTS.DEMUX_COMPLETE)
track.samples.push(chunk)
this.emit(DEMUX_EVENTS.DEMUX_COMPLETE)
}

if (!validate) {
Expand All @@ -367,6 +369,7 @@ class FlvDemuxer {
// header
let info = this.loaderBuffer.shift(1)[0]
chunk.frameType = (info & 0xf0) >>> 4
chunk.isKeyframe = chunk.frameType === 5
// let tempCodecID = this.tracks.videoTrack.codecID
let codecID = info & 0x0f
this.tracks.videoTrack.codecID = codecID
Expand Down Expand Up @@ -431,7 +434,7 @@ class FlvDemuxer {
this._avcSequenceHeaderParser(chunk.data)
let validate = this._datasizeValidator(chunk.datasize)
if (validate) {
if (this._hasScript && !this._hasVideoSequence && (!this.tracks.audioTrack || this._hasAudioSequence)) {
if (this._hasScript && !this._hasVideoSequence) {
this.emit(DEMUX_EVENTS.METADATA_PARSED, 'video')
} else if (this._hasScript && this._hasVideoSequence) {
this.emit(DEMUX_EVENTS.VIDEO_METADATA_CHANGE)
Expand All @@ -442,17 +445,17 @@ class FlvDemuxer {
if (!this._datasizeValidator(chunk.datasize)) {
this.logger.warn(this.TAG, `invalid video tag datasize: ${chunk.datasize}`)
}
this.emit(DEMUX_EVENTS.DEMUX_COMPLETE)
this.tracks.videoTrack.samples.push(chunk)
this.emit(DEMUX_EVENTS.DEMUX_COMPLETE)
}
} else {
this.logger.warn(this.TAG, `video codeid is ${codecID}`)
chunk.data = this.loaderBuffer.shift(chunk.datasize - 1)
if (!this._datasizeValidator(chunk.datasize)) {
this.logger.warn(this.TAG, `invalid video tag datasize: ${chunk.datasize}`)
}
this.emit(DEMUX_EVENTS.DEMUX_COMPLETE)
this.tracks.videoTrack.samples.push(chunk)
this.emit(DEMUX_EVENTS.DEMUX_COMPLETE)
}
delete chunk.tagType
}
Expand Down Expand Up @@ -538,10 +541,11 @@ class FlvDemuxer {
videoMedia.level = meta.level
videoMedia.chromaFormat = meta.chromaFormat
videoMedia.frameRate = meta.frameRate
videoMedia.sarRatio = meta.sarRatio
videoMedia.parRatio = meta.parRatio
videoMedia.width = videoMedia.width === meta.presentWidth ? videoMedia.width : meta.presentWidth
videoMedia.height = videoMedia.height === meta.presentHeight ? videoMedia.width : meta.presentHeight

meta.duration = this._context.mediaInfo.duration
meta.avcc = new Uint8Array(data.length)
meta.avcc.set(data)
track.meta = meta
Expand All @@ -566,7 +570,7 @@ class FlvDemuxer {
*/
_switchAudioSamplingFrequency (info) {
let samplingFrequencyIndex = (info & 12) >>> 2
let samplingFrequencyList = [5500, 11025, 22050, 44100]
let samplingFrequencyList = [5500, 11025, 22050, 44100, 48000]
return samplingFrequencyList[samplingFrequencyIndex]
}

Expand Down
72 changes: 39 additions & 33 deletions packages/xgplayer-flv/src/parse/remux/Fmp4.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ class FMP4 {
0x61, 0x76, 0x63, 0x31 // avc1
]))
}
static moov (data) {
static moov ({ type, meta }) {
let size = 8
let mvhd = FMP4.mvhd(data.duration, data.timescale)
let trak1 = FMP4.videoTrak(data)
let trak2 = FMP4.audioTrak(data)
let mvex = FMP4.mvex(data.duration, data.timescale);
[mvhd, trak1, trak2, mvex].forEach(item => {
let mvhd = FMP4.mvhd(meta.duration, meta.timescale)
let trak

if (type === 'video') {
trak = FMP4.videoTrak(meta)
} else {
trak = FMP4.audioTrak(meta)
}

let mvex = FMP4.mvex(meta.duration, meta.timescale);
[mvhd, trak, mvex].forEach(item => {
size += item.byteLength
})
return FMP4.initBox(size, 'moov', mvhd, trak1, trak2, mvex)
return FMP4.initBox(size, 'moov', mvhd, trak, mvex)
}
static mvhd (duration, timeScale) {
let timescale = timeScale || 1000
Expand Down Expand Up @@ -90,23 +96,23 @@ class FMP4 {
}
static videoTrak (data) {
let size = 8

let tkhd = FMP4.tkhd({
id: 1,
duration: data.duration,
timescale: data.timescale,
width: data.width,
height: data.height,
width: data.presentWidth,
height: data.presentHeight,
type: 'video'
})
let mdia = FMP4.mdia({
type: 'video',
timescale: data.timescale,
duration: data.duration,
sps: data.sps,
pps: data.pps,
pixelRatio: data.pixelRatio,
width: data.width,
height: data.height
avcc: data.avcc,
parRatio: data.parRatio,
width: data.presentWidth,
height: data.presentHeight
});
[tkhd, mdia].forEach(item => {
size += item.byteLength
Expand All @@ -127,9 +133,9 @@ class FMP4 {
type: 'audio',
timescale: data.timescale,
duration: data.duration,
channelCount: data.audioChannelCount,
samplerate: data.audioSampleRate,
config: data.audioConfig
channelCount: data.channelCount,
samplerate: data.sampleRate,
config: data.config
});
[tkhd, mdia].forEach(item => {
size += item.byteLength
Expand Down Expand Up @@ -356,24 +362,24 @@ class FMP4 {
static avc1 (data) {
let buffer = new Buffer()
let size = 40// 8(avc1)+8(avcc)+8(btrt)+16(pasp)
let sps = data.sps
let pps = data.pps
// let sps = data.sps
// let pps = data.pps
let width = data.width
let height = data.height
let hSpacing = data.pixelRatio[0]
let vSpacing = data.pixelRatio[1]
let avccBuffer = new Buffer()
avccBuffer.write(new Uint8Array([
0x01, // version
sps[1], // profile
sps[2], // profile compatible
sps[3], // level
0xfc | 3,
0xE0 | 1 // 目前只处理一个sps
].concat([sps.length >>> 8 & 0xff, sps.length & 0xff])))
avccBuffer.write(sps, new Uint8Array([1, pps.length >>> 8 & 0xff, pps.length & 0xff]), pps)
let hSpacing = data.parRatio.height
let vSpacing = data.parRatio.width
// let avccBuffer = new Buffer()
// avccBuffer.write(new Uint8Array([
// 0x01, // version
// sps[1], // profile
// sps[2], // profile compatible
// sps[3], // level
// 0xfc | 3,
// 0xE0 | 1 // 目前只处理一个sps
// ].concat([sps.length >>> 8 & 0xff, sps.length & 0xff])))
// avccBuffer.write(sps, new Uint8Array([1, pps.length >>> 8 & 0xff, pps.length & 0xff]), pps)

let avcc = avccBuffer.buffer
let avcc = data.avcc
let avc1 = new Uint8Array([
0x00, 0x00, 0x00, // reserved
0x00, 0x00, 0x00, // reserved
Expand Down Expand Up @@ -551,7 +557,7 @@ class FMP4 {
if (data.id === 1) {
trunBox.set(Buffer.writeUint32(item.isKeyframe ? 0x02000000 : 0x01010000), writeOffset)
writeOffset += 4
trunBox.set(Buffer.writeUint32(item.cps), writeOffset)
trunBox.set(Buffer.writeUint32(item.cts), writeOffset)
writeOffset += 4
} else {
trunBox.set(Buffer.writeUint32(0x01000000), writeOffset)
Expand Down
Loading

0 comments on commit bc6007c

Please sign in to comment.