Skip to content

Commit 0261ddb

Browse files
authored
fix: don't use MSE If the video don't have a video track (#78)
1 parent 01134f1 commit 0261ddb

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

packages/griffith-mp4/src/mp4/mp4Probe.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,13 @@ export default class MP4Probe {
114114

115115
getMP4Data() {
116116
const {duration, timescale} = findBox(this.mp4BoxTree, 'mvhd')
117-
const {width, height} = findBox(this.mp4BoxTree, 'videoTkhd')
118-
const {samples} = findBox(this.mp4BoxTree, 'videoStsz')
119-
const {SPS, PPS} = findBox(this.mp4BoxTree, 'avcC')
117+
120118
const {channelCount, sampleRate} = findBox(this.mp4BoxTree, 'mp4a')
121119
const {timescale: audioTimescale, duration: audioDuration} = findBox(
122120
this.mp4BoxTree,
123121
'audioMdhd'
124122
)
125-
const {timescale: videoTimescale, duration: videoDuration} = findBox(
126-
this.mp4BoxTree,
127-
'videoMdhd'
128-
)
123+
129124
const {
130125
ESDescrTag: {
131126
DecSpecificDescrTag: {audioConfig},
@@ -135,18 +130,33 @@ export default class MP4Probe {
135130
this.mp4Data = {
136131
duration,
137132
timescale,
138-
width,
139-
height,
140-
SPS,
141-
PPS,
142133
channelCount,
143134
sampleRate,
144135
audioConfig,
145136
audioDuration,
146-
videoDuration,
147137
audioTimescale,
148-
videoTimescale,
149-
videoSamplesLength: samples.length,
138+
}
139+
140+
const hasVideoStream = findBox(this.mp4BoxTree, 'videoTrak')
141+
if (hasVideoStream) {
142+
const {width, height} = findBox(this.mp4BoxTree, 'videoTkhd')
143+
const {samples} = findBox(this.mp4BoxTree, 'videoStsz')
144+
const {SPS, PPS} = findBox(this.mp4BoxTree, 'avcC')
145+
const {timescale: videoTimescale, duration: videoDuration} = findBox(
146+
this.mp4BoxTree,
147+
'videoMdhd'
148+
)
149+
150+
this.mp4Data = {
151+
...this.mp4Data,
152+
width,
153+
height,
154+
SPS,
155+
PPS,
156+
videoDuration,
157+
videoTimescale,
158+
videoSamplesLength: samples.length,
159+
}
150160
}
151161
}
152162
}

packages/griffith-mp4/src/mse/controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ export default class MSE {
6363
handleAppendBuffer = (buffer, type) => {
6464
if (this.mediaSource.readyState === 'open') {
6565
try {
66-
this.sourceBuffers[type].appendBuffer(buffer)
66+
if (this.sourceBuffers[type]) {
67+
this.sourceBuffers[type].appendBuffer(buffer)
68+
}
6769
} catch (error) {
6870
// see https://developers.google.com/web/updates/2017/10/quotaexceedederror
6971
if (error.name === 'QuotaExceededError') {

packages/griffith-mp4/src/player.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,35 @@ import MSE from './mse'
55
const {isSafari} = ua
66

77
export default class Player extends Component {
8+
useMSE = true
9+
810
componentDidMount() {
911
this.mse = new MSE(this.video, this.props.src)
10-
this.mse.init()
12+
this.mse.init().then(() => {
13+
// don't use MSE If the video don't have a video track
14+
if (!this.mse.mp4Probe.mp4Data.videoDuration) {
15+
this.useMSE = false
16+
this.video.src = this.props.src
17+
}
18+
})
1119
}
1220

1321
componentDidUpdate(prevProps) {
14-
if (this.props.src !== prevProps.src) {
22+
if (this.props.src !== prevProps.src && this.useMSE) {
1523
this.mse.changeQuality(this.props.src)
1624
}
1725
}
1826

1927
componentWillUnmount() {
20-
this.mse.destroy()
28+
if (this.useMSE) {
29+
this.mse.destroy()
30+
}
2131
}
2232

2333
handleTimeUpdate = e => {
24-
this.mse.handleTimeUpdate()
34+
if (this.useMSE) {
35+
this.mse.handleTimeUpdate()
36+
}
2537
this.props.onTimeUpdate(e)
2638
}
2739

@@ -31,20 +43,24 @@ export default class Player extends Component {
3143

3244
if (isSafari && buffered && buffered.length > 0) {
3345
if (currentTime - 0.1 > buffered.start(0)) {
34-
this.mse.seek(this.video.currentTime)
46+
if (this.useMSE) {
47+
this.mse.seek(this.video.currentTime)
48+
}
3549
} else if (currentTime < buffered.start(0)) {
3650
this.handleSafariBug()
3751
return
3852
}
3953
} else {
40-
this.mse.seek(this.video.currentTime)
54+
if (this.useMSE) {
55+
this.mse.seek(this.video.currentTime)
56+
}
4157
}
4258
this.props.onSeeking(e)
4359
}
4460

4561
handlePlay = e => {
4662
const {currentTime} = this.video
47-
if (currentTime === 0) {
63+
if (currentTime === 0 && this.useMSE) {
4864
this.mse.seek(0)
4965
}
5066

0 commit comments

Comments
 (0)