From b84d3e289a7832c6bf787c28c4f9d36bd4272482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Ribeiro?= Date: Sun, 12 Oct 2014 16:57:21 -0300 Subject: [PATCH] merge stats and playback_info (close #95) --- src/main.js | 7 ++--- src/peer.js | 6 ++-- src/playback_info.js | 61 +++++++++++++++++++++++++++++++++++++++-- src/settings.js | 6 ---- src/stats.js | 65 -------------------------------------------- 5 files changed, 63 insertions(+), 82 deletions(-) delete mode 100644 src/stats.js diff --git a/src/main.js b/src/main.js index ae80810..003506d 100644 --- a/src/main.js +++ b/src/main.js @@ -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') @@ -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) } @@ -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) @@ -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(), }) @@ -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.") } diff --git a/src/peer.js b/src/peer.js index b96a98c..8103453 100644 --- a/src/peer.js +++ b/src/peer.js @@ -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 { @@ -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() } @@ -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) diff --git a/src/playback_info.js b/src/playback_info.js index 37e5583..a3af51e 100644 --- a/src/playback_info.js +++ b/src/playback_info.js @@ -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) { @@ -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() { diff --git a/src/settings.js b/src/settings.js index 098b579..34a90bb 100644 --- a/src/settings.js +++ b/src/settings.js @@ -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 diff --git a/src/stats.js b/src/stats.js deleted file mode 100644 index 03de6e4..0000000 --- a/src/stats.js +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2014 Flávio Ribeiro . -// All rights reserved. -// Use of this source code is governed by a Apache -// license that can be found in the LICENSE file. - -var BaseObject = require('base_object'); -var _ = require('underscore') -var Settings = require('./settings') - -class Stats extends BaseObject { - constructor() { - this.recv_cdn = 0 - this.recv_p2p = 0 - this.sent_p2p = 0 - this.bufferLength = 0 - } - - setEmitter(main) { - this.main = main - if (Settings.statsReport) { - this.addEventListeners() - this.bufferLengthTimer = setInterval(() => this.updateBufferLength(), 1000) - this.triggerStats({status: "on"}) - } - } - - addEventListeners() { - this.listenTo(this.main.resourceRequester.p2pManager.swarm, "swarm:sizeupdate", (event) => this.triggerStats(event)) - this.listenTo(this.main.uploadHandler, 'uploadhandler:update', (event) => this.triggerStats(event)) - Clappr.Mediator.on(this.main.uniqueId + ':fragmentloaded', () => this.onFragmentLoaded()) - } - - onFragmentLoaded() { - var bitrate = Math.floor(this.main.getCurrentBitrate() / 1000) - bitrate = !_.isNaN(bitrate) ? bitrate : 'UNKNOWN' - this.triggerStats({state: this.main.currentState, currentBitrate: bitrate}) - } - - updateBufferLength() { - this.bufferLength = this.main.el.globoGetbufferLength() || 0 - this.triggerStats({bufferLength: this.bufferLength}) - } - - updateStats(method=null) { - if (method === "p2p") this.recv_p2p++ - else if (method === "cdn") this.recv_cdn++ - else if (method === "p2psent") this.sent_p2p++ - var stats = {chunksFromP2P: this.recv_p2p, chunksFromCDN: this.recv_cdn, chunksSent: this.sent_p2p} - this.triggerStats(stats) - } - - triggerStats(metrics) { - this.main.trigger('playback:p2phlsstats:add', metrics) - this.main.trigger('playback:stats:add', metrics) - } -} - -Stats.getInstance = function() { - if (this._instance === undefined) { - this._instance = new this() - } - return this._instance -} - -module.exports = Stats