Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit b84d3e2

Browse files
committed
merge stats and playback_info (close #95)
1 parent 31d82a1 commit b84d3e2

File tree

5 files changed

+63
-82
lines changed

5 files changed

+63
-82
lines changed

src/main.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var log = require('./log')
77
var Settings = require('./settings')
88
var ResourceRequester = require('./resource_requester')
99
var UploadHandler = require('./upload_handler')
10-
var Stats = require('./stats')
1110
var PlaybackInfo = require('./playback_info')
1211

1312
var JST = require('./jst')
@@ -28,7 +27,6 @@ class P2PHLS extends HLS {
2827
options.swfPath = "assets/P2PHLSPlayer.swf"
2928
this.resourceRequester = new ResourceRequester({swarm: btoa(options.src), tracker: options.tracker})
3029
this.uploadHandler = UploadHandler.getInstance()
31-
this.stats = Stats.getInstance()
3230
this.playbackInfo = PlaybackInfo.getInstance()
3331
super(options)
3432
}
@@ -52,7 +50,6 @@ class P2PHLS extends HLS {
5250

5351
bootstrap() {
5452
super()
55-
this.stats.setEmitter(this)
5653
this.playbackInfo.setMain(this)
5754
this.el.playerSetminBufferLength(6)
5855
this.el.playerSetlowBufferLength(Settings.lowBufferLength)
@@ -68,7 +65,7 @@ class P2PHLS extends HLS {
6865

6966
requestResource(url) {
7067
this.currentUrl = url
71-
this.playbackInfo.addData({
68+
this.playbackInfo.updateData({
7269
'segmentSize': this.getAverageSegmentSize(),
7370
'levels': this.getLevels(),
7471
})
@@ -79,7 +76,7 @@ class P2PHLS extends HLS {
7976
if (this.currentUrl) {
8077
this.currentUrl = null
8178
this.el.resourceLoaded(chunk)
82-
this.stats.updateStats(method)
79+
this.playbackInfo.updateChunkStats(method)
8380
} else {
8481
log.debug("It seems a deadlock happened with timers on swarm.")
8582
}

src/peer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
var BaseObject = require('base_object');
77
var Storage = require('./storage');
88
var UploadHandler = require('./upload_handler')
9-
var Stats = require('./stats')
9+
var PlaybackInfo = require('./playback_info')
1010
var log = require('./log');
1111

1212
class Peer extends BaseObject {
@@ -17,7 +17,7 @@ class Peer extends BaseObject {
1717
this.dataChannel = params.dataChannel
1818
this.dataChannel.on("data", (data) => this.messageReceived(data))
1919
this.uploadHandler = UploadHandler.getInstance()
20-
this.stats = Stats.getInstance()
20+
this.playbackInfo = PlaybackInfo.getInstance()
2121
this.score = 1000
2222
this.sendPing()
2323
}
@@ -39,7 +39,7 @@ class Peer extends BaseObject {
3939
sendSatisfy(resource) {
4040
if (this.uploadHandler.getSlot(this.ident)) {
4141
this.send('satisfy', resource, this.storage.getItem(resource))
42-
this.stats.updateStats('p2psent')
42+
this.playbackInfo.updateChunkStats('p2psent')
4343
} else {
4444
log.warn("cannot send satisfy, no upload slot available")
4545
this.send("busy", resource)

src/playback_info.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@ var _ = require('underscore')
88

99
class PlaybackInfo extends BaseObject {
1010
constructor() {
11-
this.data = {}
11+
this.data = {
12+
'chunks': { 'recvCDN': 0, 'recvP2P': 0, 'sentP2P': 0 },
13+
'bufferLength': 0
14+
}
1215
}
1316

1417
setMain(main) {
1518
this.main = main
1619
this.data.delay = this.main.el.getDelay()
17-
this.listenTo(this.main, 'playback:stats:add', (metrics) => this.addData(metrics))
20+
this.addEventListeners()
21+
this.bufferLengthTimer = setInterval(() => this.updateBufferLength(), 1000)
22+
this.triggerStats({status: "on"})
1823
}
1924

20-
addData(metrics) {
25+
updateData(metrics) {
2126
this.data = _.extend(this.data, metrics)
27+
console.log(this.data)
2228
}
2329

2430
timeoutFor(command) {
@@ -30,6 +36,55 @@ class PlaybackInfo extends BaseObject {
3036
return segmentSize * 0.6
3137
}
3238
}
39+
40+
addEventListeners() {
41+
this.listenTo(this.main.resourceRequester.p2pManager.swarm, "swarm:sizeupdate", (event) => this.updateSwarmSize(event))
42+
this.listenTo(this.main.uploadHandler, 'uploadhandler:update', (event) => this.updateUploadSlots(event))
43+
Clappr.Mediator.on(this.main.uniqueId + ':fragmentloaded', () => this.onFragmentLoaded())
44+
}
45+
46+
onFragmentLoaded() {
47+
var bitrate = Math.floor(this.main.getCurrentBitrate() / 1000)
48+
bitrate = !_.isNaN(bitrate) ? bitrate : 'UNKNOWN'
49+
var data = {state: this.main.currentState, currentBitrate: bitrate}
50+
this.updateData(data)
51+
this.triggerStats(data)
52+
}
53+
54+
updateSwarmSize(data) {
55+
this.triggerStats(data)
56+
this.updateData(data)
57+
}
58+
59+
updateBufferLength() {
60+
this.bufferLength = this.main.el.globoGetbufferLength() || 0
61+
var data = {bufferLength: this.bufferLength}
62+
this.updateData(data)
63+
this.triggerStats(data)
64+
}
65+
66+
updateChunkStats(method=null) {
67+
console.log("update chunk stats", method)
68+
if (method === "p2p") this.data.chunks.recvP2P++
69+
else if (method === "cdn") this.data.chunks.recvCDN++
70+
else if (method === "p2psent") this.data.chunks.sentP2P++
71+
var stats = {
72+
chunksFromP2P: this.data.chunks.recvP2P,
73+
chunksFromCDN: this.data.chunks.recvCDN,
74+
chunksSent: this.data.chunks.sentP2P
75+
}
76+
this.triggerStats(stats)
77+
}
78+
79+
updateUploadSlots(metrics) {
80+
this.data.uploadSlots = metrics
81+
this.triggerStats(metrics)
82+
}
83+
84+
triggerStats(metrics) {
85+
this.main.trigger('playback:p2phlsstats:add', metrics)
86+
this.main.trigger('playback:stats:add', metrics)
87+
}
3388
}
3489

3590
PlaybackInfo.getInstance = function() {

src/settings.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
class Settings {
77
}
88

9-
/* Stats
10-
Turn on/off statistics report for P2PHLSStats and
11-
built-in Clappr stats module. If you're not using
12-
P2PHLSStats, doesn't make sense to turn it on. */
13-
Settings.statsReport = true
14-
159
/* logging
1610
Turn on/off logging on browser's console on
1711
initialization. You can always turn on/off

src/stats.js

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)