private void WorkerThread( )
{
// download start time and duration
DateTime start;
TimeSpan span;
while ( !stopEvent.WaitOne( 0, false ) )
{
try
{
// start Qwerk's camera
videoStreamer.startCamera( );
while ( !stopEvent.WaitOne( 0, false ) )
{
// get download start time
start = DateTime.Now;
TeRKIceLib.Image qwerkImage = videoStreamer.getFrame( 0 );
// increase frames' and bytes' counters
framesReceived++;
bytesReceived += qwerkImage.data.Length;
if ( qwerkImage.format == TeRKIceLib.ImageFormat.ImageJPEG )
{
Bitmap image = (Bitmap) Image.FromStream( new MemoryStream( qwerkImage.data ) );
// notify clients
if ( NewFrame != null )
{
NewFrame( this, new NewFrameEventArgs( image ) );
}
image.Dispose( );
}
else
{
if ( VideoSourceError != null )
{
VideoSourceError( this, new VideoSourceErrorEventArgs( "Video frame has unsupported format: " + qwerkImage.format ) );
}
}
// wait for a while ?
if ( frameInterval > 0 )
{
// get download duration
span = DateTime.Now.Subtract( start );
// miliseconds to sleep
int msec = frameInterval - (int) span.TotalMilliseconds;
while ( ( msec > 0 ) && ( stopEvent.WaitOne( 0, false ) == false ) )
{
// sleeping ...
Thread.Sleep( ( msec < 100 ) ? msec : 100 );
msec -= 100;
}
}
}
// stop Qwerk's camera
videoStreamer.stopCamera( );
}
catch ( Ice.SocketException )
{
if ( VideoSourceError != null )
{
VideoSourceError( this, new VideoSourceErrorEventArgs( "Connection lost to Qwerk's video service." ) );
}
}
catch
{
if ( VideoSourceError != null )
{
VideoSourceError( this, new VideoSourceErrorEventArgs( "Failed getting video frame from Qwerk." ) );
}
}
}
if ( PlayingFinished != null )
{
PlayingFinished( this, ReasonToFinishPlaying.StoppedByUser );
}
}
}