Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adjust video quality #65

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/griffith-hls/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import VideoComponent from './Video'

export default {
pluginName: 'griffith-mp4',
pluginName: 'griffith-hls',
VideoComponent,
willHandleSrcChange: true,
}
1 change: 1 addition & 0 deletions packages/griffith-mp4/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import VideoComponent from './player'
export default {
pluginName: 'griffith-mp4',
VideoComponent,
willHandleSrcChange: true,
}
42 changes: 37 additions & 5 deletions packages/griffith-mp4/src/mse/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class MSE {
constructor(video, src) {
this.video = video
this.src = src
this.qualityChangeFlag = false
this.videoQueue = []
this.audioQueue = []
this.sourceBuffers = {
Expand Down Expand Up @@ -67,7 +68,7 @@ export default class MSE {

init() {
// 获取 mdat 外的数据
this.loadData()
return this.loadData()
.then(res => {
return new MP4Parse(new Uint8Array(res)).mp4BoxTreeObject
})
Expand Down Expand Up @@ -116,10 +117,17 @@ export default class MSE {
FMP4.moov(this.mp4Probe.mp4Data, 'audio')
)

this.mediaSource.addEventListener('sourceopen', () => {
// 如果是切换清晰度,mediaSource 的 readyState 已经 open 了,可以直接 append 数据。
// mediaSource is already open when we switch video quality.
if (this.qualityChangeFlag) {
this.handleAppendBuffer(videoRawData, 'video')
this.handleAppendBuffer(audioRawData, 'audio')
})
} else {
this.mediaSource.addEventListener('sourceopen', () => {
this.handleAppendBuffer(videoRawData, 'video')
this.handleAppendBuffer(audioRawData, 'audio')
})
}
})
}

Expand All @@ -144,10 +152,13 @@ export default class MSE {

// 对于已经请求的数据不再重复请求
// No need to repeat request video data
if (time && this.hasBufferedCache(this.video.currentTime)) {
if (
time &&
this.hasBufferedCache(this.video.currentTime) &&
!this.qualityChangeFlag
) {
return
}

this.handleReplayCase()

this.loadData(start, end).then(mdatBuffer => {
Expand Down Expand Up @@ -180,9 +191,30 @@ export default class MSE {
if (time) {
this.needUpdateTime = true
}

this.qualityChangeFlag = false
})
}

changeQuality(newSrc) {
this.src = newSrc
this.qualityChangeFlag = true
this.removeBuffer()

this.init().then(() => {
this.video.currentTime = this.video.currentTime
})
}

removeBuffer() {
for (const key in this.sourceBuffers) {
const track = this.sourceBuffers[key]
const length = track.buffered.length

track.remove(track.buffered.start(0), track.buffered.end(length - 1))
}
}

loadData(start = 0, end = MAGIC_NUMBER) {
return new Promise(resolve => {
new FragmentFetch(this.src, start, end, resolve)
Expand Down
2 changes: 1 addition & 1 deletion packages/griffith-mp4/src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Player extends Component {

componentDidUpdate(prevProps) {
if (this.props.src !== prevProps.src) {
this.mse = new MSE(this.video, this.props.src)
this.mse.changeQuality(this.props.src)
}
}

Expand Down