CRioMMMp3 Class Reference

#include <RioMMMp3.h>

Inherits CRioMMObject.

Public Member Functions

 CRioMMMp3 (char *, unsigned int, RioBlock, struct timeval RTT_average, RioMMInterface *)
 ~CRioMMMp3 ()
void PlayBlock (int, int)
void FastForward (void)
void FastRewind (void)
void GoTo (RioBlock)
void Play (void)
void Pause (void)
void Stop (void)
int init (void)
void WaitEndOfVideo (void)
RioBlock GetCurrentPosition (void)
RioBlock GetTotalPositions (void)

Private Member Functions

void StartMpg123 (void)
bool ReadNextBlock (unsigned int)

Static Private Member Functions

static void * PlayThread (void *)
static void PlayThreadCleanup (void *)

Private Attributes

int pipe_mpg123 [2]
pid_t pid
RioBlock NextRequestBlock
unsigned int PlayBuffer
pthread_t PlayThreadID
bool Paused
pthread_mutex_t PlayPause
RioMMInterfaceriomminterface

Detailed Description

Definition at line 27 of file RioMMMp3.h.


Constructor & Destructor Documentation

CRioMMMp3::CRioMMMp3 ( char *  mp3name,
unsigned int  blocksize,
RioBlock  numBuffers,
struct timeval  RTT_average,
RioMMInterface rio 
)

Definition at line 37 of file RioMMMp3.cpp.

00039           :CRioMMObject( mp3name, blocksize, numBuffers , RTT_average )
00040 {
00041     pid              = 0;
00042     PlayBuffer       = 0;
00043     PlayThreadID     = 0;
00044     riomminterface   = rio;
00045     NextRequestBlock = numBuffers;
00046     Paused           = false;
00047     pthread_mutex_init( &PlayPause, NULL );
00048 }

CRioMMMp3::~CRioMMMp3 (  ) 

Definition at line 51 of file RioMMMp3.cpp.

00052 {
00053     if( PlayThreadID != 0 )
00054         pthread_cancel( PlayThreadID );
00055 }


Member Function Documentation

void CRioMMMp3::FastForward ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 99 of file RioMMMp3.cpp.

00100 {
00101     NextRequestBlock += 10;
00102 }

void CRioMMMp3::FastRewind ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 105 of file RioMMMp3.cpp.

00106 {
00107     NextRequestBlock -= 10;
00108 }

RioBlock CRioMMMp3::GetCurrentPosition ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 179 of file RioMMMp3.cpp.

00180 {
00181     return( CurrentBlock );
00182 }

RioBlock CRioMMMp3::GetTotalPositions ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 185 of file RioMMMp3.cpp.

00186 {
00187     return( TotalBlocks );
00188 }

void CRioMMMp3::GoTo ( RioBlock  block  )  [virtual]

Implements CRioMMObject.

Definition at line 111 of file RioMMMp3.cpp.

00112 {
00113     NextRequestBlock = (int) block;
00114 }

int CRioMMMp3::init ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 58 of file RioMMMp3.cpp.

00059 {
00060     return 0;
00061 }

void CRioMMMp3::Pause ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 133 of file RioMMMp3.cpp.

00134 {
00135     if( PlayThreadID != 0 )
00136     {
00137         if( Paused )
00138         {
00139             pthread_mutex_unlock( &PlayPause );
00140             Paused = false;
00141         }
00142         else
00143         {
00144             pthread_mutex_lock( &PlayPause );
00145             Paused = true;
00146         }
00147     }
00148 }

void CRioMMMp3::Play ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 151 of file RioMMMp3.cpp.

00152 {
00153     if( PlayThreadID == 0 )
00154     {
00155         if( pthread_create( &PlayThreadID, NULL, &PlayThread, this ) )
00156         {
00157             RioErr << "Could not create play thread" << endl;
00158             return;
00159         }
00160     }
00161     else
00162     {
00163         if( Paused )
00164         {
00165             pthread_mutex_unlock( &PlayPause );
00166             Paused = false;
00167         }
00168     }
00169 }

void CRioMMMp3::PlayBlock ( int  buf,
int  size 
)

Definition at line 64 of file RioMMMp3.cpp.

00065 {
00066     int sent;
00067     int n, nwrite;
00068 
00069     char *buffer = (char *)Request[buf].Buffer;
00070 
00071     sent = 0;
00072     while( sent < size )
00073     {
00074         n = size - sent;
00075         if( n > MaxPipeDataSize )
00076             n = MaxPipeDataSize;
00077 
00078         pthread_mutex_lock( &PlayPause );
00079         nwrite = write( pipe_mpg123[1], (void*)buffer, n );
00080         pthread_mutex_unlock( &PlayPause );
00081 
00082         if( nwrite < 0 )
00083         {
00084             Rioperror("write: pipe mpg123");
00085         }
00086 
00087         sent   += nwrite;
00088         buffer += nwrite;
00089     }
00090 
00091     if( riomminterface )
00092     {
00093         QCustomEvent *event = new QCustomEvent( QEvent::User , &Request[buf].Block );
00094         QApplication::postEvent( (QObject*)riomminterface, event );
00095     }
00096 }

void * CRioMMMp3::PlayThread ( void *  param  )  [static, private]

Definition at line 192 of file RioMMMp3.cpp.

00193 {
00194     int  curBlockSize;
00195     bool EOM;         //End of Media
00196     CRioMMMp3 *Mp3;
00197     unsigned int curpos;
00198     unsigned int n_requests;
00199 
00200     pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL );
00201     pthread_cleanup_push( CRioMMMp3::PlayThreadCleanup, param );
00202 
00203     Mp3 = (CRioMMMp3 *)param;
00204 
00205     Mp3->StartMpg123();
00206     Mp3->Prefetch();
00207     Mp3->WaitPrefetch();
00208 
00209     EOM = false;
00210     while( !EOM )
00211     {
00212         // Sets current position
00213         curpos     = Mp3->PlayBuffer;
00214         n_requests = 0;
00215 
00216         // While current position is empty (current block hasn't arrived)
00217         if( Mp3->GetBufferStatus( curpos ) == BUFFEREMPTY )
00218         {
00219             usleep( 700000 );
00220         }
00221         else
00222         {
00223             if( Mp3->Request[curpos].Block < Mp3->TotalBlocks-1 )
00224                 curBlockSize = Mp3->BlockSize;
00225             else
00226             {
00227                 if( Mp3->Request[curpos].Block == Mp3->TotalBlocks-1 )
00228                     curBlockSize = Mp3->ObjSize % Mp3->BlockSize;
00229                 else
00230                 {
00231                     RioErr << "Unbounded streams are not supported yet!" << endl;
00232                     exit( -1 );
00233                 }
00234             }
00235 
00236             Mp3->PlayBlock( curpos, curBlockSize );
00237             Mp3->SetBufferStatus( Mp3->PlayBuffer, BUFFEREMPTY );
00238 
00239             Mp3->SyncMedia( (void *)Mp3->Request[curpos].Block );
00240         }
00241 
00242         Mp3->ReadNextBlock( Mp3->PlayBuffer );
00243 
00244         Mp3->PlayBuffer   = (curpos + 1 ) % Mp3->nBuffers;
00245         Mp3->CurrentBlock = Mp3->Request[curpos].Block;
00246         if( Mp3->CurrentBlock == Mp3->TotalBlocks - 1 )
00247             EOM = true;
00248     }
00249     sleep( 2 ); //wait for mpg123 to play the last block
00250     pthread_cleanup_pop( 0 );
00251     pthread_exit( NULL );
00252 }

void CRioMMMp3::PlayThreadCleanup ( void *  param  )  [static, private]

Definition at line 255 of file RioMMMp3.cpp.

00256 {
00257     CRioMMMp3 *Mp3;
00258 
00259     Mp3 = (CRioMMMp3 *)param;
00260     kill( Mp3->pid, SIGKILL );
00261     pthread_mutex_unlock( &Mp3->PlayPause );
00262     pthread_mutex_unlock( &Mp3->PlayThreadMutex );
00263     waitpid( Mp3->pid, NULL, 0 );
00264     Mp3->PlayThreadID = 0;
00265     Mp3->Stop();
00266 }

bool CRioMMMp3::ReadNextBlock ( unsigned int  index  )  [private]

Definition at line 268 of file RioMMMp3.cpp.

00269 {
00270     bool status;
00271 
00272     status = true;
00273     if( NextRequestBlock < TotalBlocks )
00274         Read( index, NextRequestBlock++ );
00275     else
00276         status = false;
00277 
00278     return( status );
00279 }

void CRioMMMp3::StartMpg123 ( void   )  [private]

Definition at line 282 of file RioMMMp3.cpp.

00283 {
00284     // create pipe to send data to mtv
00285     if( pipe( pipe_mpg123 ) < 0 )
00286     {
00287         Rioperror("pipe(). Failed to create pipe to mpg123.");
00288         return;
00289     }
00290 
00291     // associate pipe input to stdin
00292     if( dup2( pipe_mpg123[0], 0 ) == -1 )
00293     {
00294         Rioperror("dup2(). Failed to associate pipe to stdin.");
00295         return;
00296     }
00297 
00298     // start program mpg123
00299     if( (pid = fork()) != 0 )
00300     {
00301         if( pid < 0 )
00302         {
00303             Rioperror("fork(). Failed to fork process.");
00304             return;
00305         }
00306     }
00307     else
00308     {
00309         //run mpg123
00310         if( execlp( "mpg123","mpg123", "-", NULL ) == -1 )
00311         {
00312             Rioperror( "execlp(): Failed to execute mpg123." );
00313             exit( 1 );
00314         }
00315     }
00316 }

void CRioMMMp3::Stop ( void   )  [virtual]

Implements CRioMMObject.

Definition at line 117 of file RioMMMp3.cpp.

00118 {
00119     if( PlayThreadID != 0 )
00120     {
00121         pthread_cancel( PlayThreadID );
00122         if( Paused )
00123             pthread_mutex_unlock( &PlayPause );
00124     }
00125     PlayBuffer       = 0;
00126     PlayThreadID     = 0;
00127     NextRequestBlock = nBuffers;
00128 
00129     CRioMMObject::Stop();
00130 }

void CRioMMMp3::WaitEndOfVideo ( void   ) 

Definition at line 172 of file RioMMMp3.cpp.

00173 {
00174     pthread_join( PlayThreadID, NULL );
00175     PlayThreadID = 0;
00176 }


Field Documentation

Definition at line 31 of file RioMMMp3.h.

bool CRioMMMp3::Paused [private]

Definition at line 34 of file RioMMMp3.h.

pid_t CRioMMMp3::pid [private]

Definition at line 30 of file RioMMMp3.h.

int CRioMMMp3::pipe_mpg123[2] [private]

Definition at line 29 of file RioMMMp3.h.

unsigned int CRioMMMp3::PlayBuffer [private]

Definition at line 32 of file RioMMMp3.h.

pthread_mutex_t CRioMMMp3::PlayPause [private]

Definition at line 35 of file RioMMMp3.h.

pthread_t CRioMMMp3::PlayThreadID [private]

Definition at line 33 of file RioMMMp3.h.

Definition at line 36 of file RioMMMp3.h.


The documentation for this class was generated from the following files:
Generated on Wed Jul 4 16:03:32 2012 for RIO by  doxygen 1.6.3