I am working on an application which requires streaming a video from one computer (lets call it video computer) to another computer (user computer). The streaming model is such that video file bytes are sent from video computer to user computer "as it is" and decoding is done at user end.
The bytes being received at the user end are stored in a System.IO.FileStream
object. The length of the fileStream
object (in bytes) is set at start of buffering (because there is provision for sending metadata about the video file in the beginning).
As buffering starts, source of a System.Windows.Controls.MediaElement
object is set to the filestream
object.
All goes well if the user has no desire to seek the video and the buffering rate stays higher than the playing rate. However, one cannot rely on luck. I need a mechanism to check if duration of video buffered is less than the current play time... so that the video must get paused (This could happen when the user seeks video at far ahead time, or if buffering rate is slow). Then corrective measures should be taken and playback should get started only when a minimum duration has been buffered.
Thus I need a mechanism to "DETERMINE buffered duration in seconds (i.e. find position of buffer pointer in seconds) given position of buffer pointer in bytes on buffering timeline OR DETERMINE number of bytes that have been played (or passed behind play pointer) given current play pointer position in seconds".
At any instant, the following quantities are known:
- position of buffer pointer in bytes
- position of play pointer in seconds
- duration of video
- length of video in bytes
It is possible to pause/play mediaElement
or seek it to a position in seconds.
Any help will be appreciated.
[Note that one cannot say that bufferPositionInSeconds
= bufferPositionInBits/videoBitRate because bitrate is variable for most videos in practice and also because of existence of metatdata in the file.]