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

支持直播 #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
64 changes: 64 additions & 0 deletions demo/liveDemo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!Doctype html>
<html xmlns=http://www.w3.org/1999/xhtml> <head>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
<meta content=always name=referrer>
<link rel="shortcut icon" href=/favicon.ico type=image/x-icon> <title>H.265在线播放器DEMO</title>
<link rel="stylesheet" href="../dist/goldplay-h265.css">
<script src="../dist/goldplay-h265-sdk.js"></script>
<style>
.play-container {
width: 800px;
height: 600px;
/* background-color: #000; */
margin: 20px 0 0 100px;
float:left;
}

.inline {
display: inline-block;
width: 50px;
}
</style>
</head>

<body>
<div id="PlayerWrapper">
<h3>H.265 Player <b>在线播放器Demo1</b><em class="demo1" id="Demo1">DEMO</em></h3>
<div class="play-container"></div>
</div>
<br clear="all">
<br>
<script>
(function (win, doc) {
const Config = {
get basePath() {
let path = win.location.origin
let pathname = win.location.pathname
let demoIdx = pathname.lastIndexOf('/demo/')
return path + pathname.substr(0, demoIdx)
},
get buildPath() {
return this.basePath + '/dist/'
},
get libPath() {
return this.buildPath + 'lib/'
},
get src() {
return this.basePath + '/data/video2/playlist.m3u8'
}
}
let el = doc.querySelector('.play-container')
let player = new GoldPlay(el, {
sourceURL: Config.src,
type: 'HLS',
libPath: Config.libPath,
playBackRate: 1,
isLive: true,
})
// player.destroy()
})(window, document)
</script>
</body>

</html>
10 changes: 10 additions & 0 deletions src/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Player extends BaseClass {
autoPlay = true
duration = 0
tsNumber = 0
isLive = false
/**
* @property {string} sourceURL - The url of the video to play
* @property {string} source - The url of the video to play
Expand All @@ -85,6 +86,7 @@ class Player extends BaseClass {
* @property {AlertError} alertError - The alert info when error happens
* @property {Worker} httpWorker - set User's web worker
* @property {Function} afterLoadPlaylist - To handle operations after playlist is loaded
* @property {Boolean} isLive - If the video type is live
*/
constructor (el, options = {}) {
super()
Expand All @@ -106,6 +108,7 @@ class Player extends BaseClass {
this.startTime = options.startTime === undefined ? this.startTime : options.startTime
this.originStartTime = this.startTime
this.playbackRate = options.playbackRate === undefined ? this.playbackRate : options.playbackRate
this.isLive = options.isLive !== undefined ? options.isLive : this.isLive
}
setAlertError () {
this.options.alertError = this.alertError = AlertError.getInstance({
Expand Down Expand Up @@ -224,6 +227,13 @@ class Player extends BaseClass {
this.bindEvent()
}
bindEvent () {
this.events.on(Events.LoaderNextPlayListLoaded, (index, length, duration) => {
this.duration += duration
this.streamController.setBaseInfo({
tsNumber: index + length - 1,
duration: this.duration,
})
})
this.events.on(Events.PlayerOnPlay, () => {
this.play()
})
Expand Down
6 changes: 5 additions & 1 deletion src/action/StreamController.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ export default class StreamController extends BaseClass {
if (data.no === this.tsNumber) {
//the last one ts packet
this.logger.info('onRead', 'the last ts')
this.events.emit(Events.DemuxLast)
if(this.player.isLive) {
this.events.emit(Events.LastTSFileLoaded)
} else {
this.events.emit(Events.DemuxLast)
}
}
this.events.emit(Events.DemuxStartDemux, data)
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/config/EventsConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const Events = {
LoaderUpdateSpeed: 'Loader.updateSpeed',
LoaderLoadFile: 'Loader.loadFile',
LoaderPlayListLoaded: 'Loader.playlistLoaded',

LastTSFileLoaded: 'Loader.lastTSFileLoaded',
LoaderNextPlayListLoaded: 'Loader.loaderNextPlayListLoaded',
AudioPlayerReady: 'AudioPlayer.MSEReady',
AudioPlayerDataReady: 'AudioPlayer.dataReady',
AudioPlayerWait: 'AudioPlayer.wait',
Expand Down
3 changes: 3 additions & 0 deletions src/data/LoadData.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class LoadData extends BaseClass {
this.events.emit(Events.LoadDataFirstLoaded, buffer, time)
}
})
this.events.on(Events.LoaderNextPlayListLoaded, (index, length) => {
this.loadSegmentByNo(index)
})
}

setOptions(options) {
Expand Down
15 changes: 11 additions & 4 deletions src/loader/HLSLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class HLSLoader extends BaseLoader {

currentNo = null
maxRetryCount = BUFFER.maxRetryCount
end = 0

constructor(options) {
super()
Expand Down Expand Up @@ -75,12 +76,18 @@ class HLSLoader extends BaseLoader {
this.events.emit(Events.PlayerThrowError, errors)
return
}
let segments = data.segments
segments.forEach(item => {
item.start = Utils.msec2sec(item.start)
item.end = Utils.msec2sec(item.end)
let segments = []
data.segments.forEach(item => {
if(this.segmentPool.getBy(segment => item.name === segment.name).length !== 0) { // 去重
return
}
item.start = Utils.msec2sec(item.start) + this.end
item.end = Utils.msec2sec(item.end) + this.end
item.duration = Utils.msec2sec(item.duration)
segments.push(item)
})
this.end = segments[segments.length - 1].end
data.segments = segments
this.setSourceData(Object.freeze(data))
this.segmentPool.addAll(data.segments)
callback.call(this, data)
Expand Down
17 changes: 13 additions & 4 deletions src/loader/LoaderController.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LoaderController extends BaseController {
type = 'HLS'
options = null
dataController = null

segmentsStartIndex = 0
constructor(type, options) {
super()
this.type = type || this.type
Expand Down Expand Up @@ -62,18 +62,27 @@ class LoaderController extends BaseController {
this.events.on(Events.LoaderLoadFile, (segment, type, time) => {
this.exeLoader.loadFile(segment, type, time)
})
this.events.on(Events.LastTSFileLoaded, () => {
this.loadPlaylist(null, 'next')
})
}

loadPlaylist(callback) {
loadPlaylist(callback, type) {
this.exeLoader.loadPlaylist( (data) => {
if (!data) {
this.logger.error('run', 'start load m3u8', 'data:', data)
return
}
this.dataController.setLoadDataSourceData(this.exeLoader.getSourceData())
this.dataController.setLoadDataSegmentPool(this.exeLoader.getSegmentPool())
this.state = state.LOADED_PLAYLIST
this.events.emit(Events.LoaderPlayListLoaded, this)
if(type === 'next') {
this.events.emit(Events.LoaderNextPlayListLoaded, this.segmentsStartIndex, data.segments.length, this.exeLoader.getSourceData().duration)
this.segmentsStartIndex = data.segments.length + this.segmentsStartIndex
} else {
this.state = state.LOADED_PLAYLIST
this.events.emit(Events.LoaderPlayListLoaded, this)
this.segmentsStartIndex = data.segments.length + 1
}
if (typeof callback === 'function') {
callback.call(this, data)
}
Expand Down