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

Commit

Permalink
merge stats and playback_info (close #95)
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioribeiro committed Oct 12, 2014
1 parent 31d82a1 commit b84d3e2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 82 deletions.
7 changes: 2 additions & 5 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var log = require('./log')
var Settings = require('./settings')
var ResourceRequester = require('./resource_requester')
var UploadHandler = require('./upload_handler')
var Stats = require('./stats')
var PlaybackInfo = require('./playback_info')

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

bootstrap() {
super()
this.stats.setEmitter(this)
this.playbackInfo.setMain(this)
this.el.playerSetminBufferLength(6)
this.el.playerSetlowBufferLength(Settings.lowBufferLength)
Expand All @@ -68,7 +65,7 @@ class P2PHLS extends HLS {

requestResource(url) {
this.currentUrl = url
this.playbackInfo.addData({
this.playbackInfo.updateData({
'segmentSize': this.getAverageSegmentSize(),
'levels': this.getLevels(),
})
Expand All @@ -79,7 +76,7 @@ class P2PHLS extends HLS {
if (this.currentUrl) {
this.currentUrl = null
this.el.resourceLoaded(chunk)
this.stats.updateStats(method)
this.playbackInfo.updateChunkStats(method)
} else {
log.debug("It seems a deadlock happened with timers on swarm.")
}
Expand Down
6 changes: 3 additions & 3 deletions src/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var BaseObject = require('base_object');
var Storage = require('./storage');
var UploadHandler = require('./upload_handler')
var Stats = require('./stats')
var PlaybackInfo = require('./playback_info')
var log = require('./log');

class Peer extends BaseObject {
Expand All @@ -17,7 +17,7 @@ class Peer extends BaseObject {
this.dataChannel = params.dataChannel
this.dataChannel.on("data", (data) => this.messageReceived(data))
this.uploadHandler = UploadHandler.getInstance()
this.stats = Stats.getInstance()
this.playbackInfo = PlaybackInfo.getInstance()
this.score = 1000
this.sendPing()
}
Expand All @@ -39,7 +39,7 @@ class Peer extends BaseObject {
sendSatisfy(resource) {
if (this.uploadHandler.getSlot(this.ident)) {
this.send('satisfy', resource, this.storage.getItem(resource))
this.stats.updateStats('p2psent')
this.playbackInfo.updateChunkStats('p2psent')
} else {
log.warn("cannot send satisfy, no upload slot available")
this.send("busy", resource)
Expand Down
61 changes: 58 additions & 3 deletions src/playback_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ var _ = require('underscore')

class PlaybackInfo extends BaseObject {
constructor() {
this.data = {}
this.data = {
'chunks': { 'recvCDN': 0, 'recvP2P': 0, 'sentP2P': 0 },
'bufferLength': 0
}
}

setMain(main) {
this.main = main
this.data.delay = this.main.el.getDelay()
this.listenTo(this.main, 'playback:stats:add', (metrics) => this.addData(metrics))
this.addEventListeners()
this.bufferLengthTimer = setInterval(() => this.updateBufferLength(), 1000)
this.triggerStats({status: "on"})
}

addData(metrics) {
updateData(metrics) {
this.data = _.extend(this.data, metrics)
console.log(this.data)
}

timeoutFor(command) {
Expand All @@ -30,6 +36,55 @@ class PlaybackInfo extends BaseObject {
return segmentSize * 0.6
}
}

addEventListeners() {
this.listenTo(this.main.resourceRequester.p2pManager.swarm, "swarm:sizeupdate", (event) => this.updateSwarmSize(event))
this.listenTo(this.main.uploadHandler, 'uploadhandler:update', (event) => this.updateUploadSlots(event))
Clappr.Mediator.on(this.main.uniqueId + ':fragmentloaded', () => this.onFragmentLoaded())
}

onFragmentLoaded() {
var bitrate = Math.floor(this.main.getCurrentBitrate() / 1000)
bitrate = !_.isNaN(bitrate) ? bitrate : 'UNKNOWN'
var data = {state: this.main.currentState, currentBitrate: bitrate}
this.updateData(data)
this.triggerStats(data)
}

updateSwarmSize(data) {
this.triggerStats(data)
this.updateData(data)
}

updateBufferLength() {
this.bufferLength = this.main.el.globoGetbufferLength() || 0
var data = {bufferLength: this.bufferLength}
this.updateData(data)
this.triggerStats(data)
}

updateChunkStats(method=null) {
console.log("update chunk stats", method)
if (method === "p2p") this.data.chunks.recvP2P++
else if (method === "cdn") this.data.chunks.recvCDN++
else if (method === "p2psent") this.data.chunks.sentP2P++
var stats = {
chunksFromP2P: this.data.chunks.recvP2P,
chunksFromCDN: this.data.chunks.recvCDN,
chunksSent: this.data.chunks.sentP2P
}
this.triggerStats(stats)
}

updateUploadSlots(metrics) {
this.data.uploadSlots = metrics
this.triggerStats(metrics)
}

triggerStats(metrics) {
this.main.trigger('playback:p2phlsstats:add', metrics)
this.main.trigger('playback:stats:add', metrics)
}
}

PlaybackInfo.getInstance = function() {
Expand Down
6 changes: 0 additions & 6 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
class Settings {
}

/* Stats
Turn on/off statistics report for P2PHLSStats and
built-in Clappr stats module. If you're not using
P2PHLSStats, doesn't make sense to turn it on. */
Settings.statsReport = true

/* logging
Turn on/off logging on browser's console on
initialization. You can always turn on/off
Expand Down
65 changes: 0 additions & 65 deletions src/stats.js

This file was deleted.

0 comments on commit b84d3e2

Please sign in to comment.