Implementing video playback statistics in Firefox - part II

Over the past few days I have begun working on implementing video playback statistics in Firefox.  I ended my last post with some build failures and couldn't figure it out at the time.  The first part of the statistics that I began implementing was mozBytesReceived ( scroll down ), which is a readonly property on a media element that tells the user how many bytes have been downloaded so far. I continued working on this for the last 2 nights and finally I got it to build again.  The problem I'm now encountering is that I have not implemented the feature correctly and ran into a few cases where my initial idea was just wrong.

The build error I was getting last night was:

Undefined symbols for architecture x86_64

It also mentioned that a function was not properly defined, so after get Jon to look at it as well that's what I did. I found the problem pretty quickly and it ended up that the compiler was telling the truth ( who would have thought ), I was indeed missing a function declaration.  It actually turned out that in some of my trial and error coding I tried to implement my bytesDownloaded() function in both nsBuiltinDecoder.cpp and nsMediaDecoder.cpp, which was obviously wrong. I did a bit more searching and came across a piece of code that was referring to media statistics, so I took a more in depth look there.

I ended finding a statistics struct that defined some properties that looked similar to what I was working with ( mPlaybackRate, mDownloadRate, mTotalBytes, etc ) so I figured this would be a good place to see if I could begin working.  After looking through the nsBuiltinDecoder.cpp file I found a GetStatistics() function that defined some values for the statistics struct.  The function from a high level explanation did the following:

  1. Makes sure we are on the main thread using an assertion
  2. We create an instance of the Statistics struct
  3. We check to see if we have a resource yet, if we do get the values for the various struct properties, else provide some sane defaults

What I did next was define my own mozBytesReceived property in the struct and set it up so if there was no resource we would return 0 ( as there is no bytes downloaded yet ) and if there was a resource I would call my GetBytes() function I made previously on mPlaybackStatistics. So after doing this and building again ( in addition to removing the duplicated code I mentioned earlier ) I finally got it building again! The next thing I did was test to make that my implementation was correct and that I could access the property on the video element.

The first thing I did was find myself a video page and created a little test function in the console. My test function looked like the following:

setInterval(function(){ console.log( document.getElementsByTagName( "video" )[0].mozBytesDownloaded ); }, 100);

What it did was set an interval on which I would fire a function that logged the result of mozBytesDownloaded. So this seemed like a good idea to me so I ran it, which gave me lots of numbers!!!!

At this point I thought I had figured it out as I was getting numbers to flow through the console, I thought I was done! I should have known better that it's never that easy, nothing ever works the first try. I let the video finish and all seemed well at first, until I decided to click back to the start of the video. To my surprise the amount of bytes "downloaded" continued to rise. Wait, what? Why is it doing that? Well after some searching I found out the culprit was the GetBytes function I wrote yesterday:

PRUint64 GetBytes() {
return mAccumulatedBytes;

The problem was the mAccumulatedBytes wasn't doing what I thought it was. It wasn't returning the total amount of bytes that were downloaded, but rather the total amount of bytes that were decoded, which wasn't exactly accurate.  Back to the drawing board I suppose.

At this point I am trying to see if there is a better place for me to hook into in the decoder somewhere.  I looked for a while last night but didn't find anything that overly caught my eye.  I figure tonight I'll head over to the #media channel in IRC and see if someone there can point me in the right direction.