examples/sfexamples/oggvorbiscodec/src/OggVorbisUtil/OggPages.cpp

00001 // OggPages.cpp
00002 //
00003 // Copyright (c) Symbian Software Ltd 2005-2008.  All rights reserved.
00004 //
00005 
00006 
00007 #include <e32base.h>
00008 #include <e32debug.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 
00012 #ifdef SYMBIAN_CODEC_FLOAT_POINT
00013 #include "ogg/ogg.h"
00014 #endif
00015 #include <oggutil.h>
00016 #include "OggUtilBody.h"
00017 
00018 //--------------------------- COggPager ---------------------------
00019 
00020 EXPORT_C COggPager* COggPager::NewL()
00021     {
00022     COggPager* self = new(ELeave)COggPager;
00023     CleanupStack::PushL(self);
00024     self->ConstructL();
00025     CleanupStack::Pop(self);
00026     return self;
00027     }
00028 
00029 COggPager::COggPager()
00030     {
00031     }
00032 
00033 COggPager::~COggPager()
00034     {
00035     delete iBody;
00036     }
00037 
00038 void COggPager::ConstructL()
00039     {
00040     iBody = new(ELeave) COggPager::CBody;
00041     iBody->ConstructL();
00042     }
00043 
00044 EXPORT_C void COggPager::Reset()
00045     {
00046     iBody->Reset();
00047     }
00048 
00049 EXPORT_C TInt COggPager::GetBuffer(TPtr8& aBuf, TInt aSize)
00050     {
00051     return iBody->GetBuffer(aBuf, aSize);
00052     }
00053 
00054 EXPORT_C TInt COggPager::DataWritten(TDes8& aBuf)
00055     {
00056     return iBody->DataWritten(aBuf);
00057     }
00058 
00059 EXPORT_C TInt COggPager::NextPage(TOggPage& aPage)
00060     {
00061     return iBody->NextPage(aPage);
00062     }
00063 
00064 
00065 //----------------------- COggPager::CBody ------------------------
00066 
00067 COggPager::CBody::CBody()
00068     : iBuf(NULL, 0)
00069     {
00070     }
00071 
00072 void COggPager::CBody::ConstructL()
00073     {
00074 #ifdef SYMBIAN_CODEC_FLOAT_POINT
00075         iSyncState = &iOggSyncState;
00076         ogg_sync_init(iSyncState);
00077 #else
00078         iSyncState = ogg_sync_create();
00079     if (!iSyncState) 
00080         {
00081         User::Leave(KErrNoMemory);      
00082         }
00083 #endif
00084     }
00085 
00086 COggPager::CBody::~CBody()
00087     {
00088 #ifdef SYMBIAN_CODEC_FLOAT_POINT
00089         ogg_sync_clear(iSyncState);
00090 #else
00091         ogg_sync_destroy(iSyncState);
00092 #endif
00093     }
00094 
00095 void COggPager::CBody::Reset()
00096     {
00097     ogg_sync_reset(iSyncState);
00098     }
00099 
00109 TInt COggPager::CBody::GetBuffer(TPtr8& aBuf, TInt aSize)
00110     {
00111 #ifdef SYMBIAN_CODEC_FLOAT_POINT
00112         char* buf = ogg_sync_buffer(iSyncState, aSize);
00113 #else
00114         unsigned char* buf = ogg_sync_bufferin(iSyncState, aSize);
00115 #endif
00116 
00117     if (buf==NULL) 
00118         {
00119         return KErrNoMemory;
00120         }
00121     iBuf.Set((unsigned char*)buf, 0, aSize);
00122     aBuf.Set(iBuf);
00123     return KErrNone;
00124     }
00125 
00134 TInt COggPager::CBody::DataWritten(const TDes8& aBuf)
00135     {
00136     // check it's the right buffer
00137     if (aBuf.Ptr()!=iBuf.Ptr()) 
00138         {
00139         return KErrArgument;    
00140         }
00141     if (aBuf.Length()>iBuf.MaxLength()) 
00142         {
00143         return KErrOverflow;    
00144         }
00145     if (ogg_sync_wrote(iSyncState, aBuf.Length())<0) 
00146         {
00147         return KErrOverflow;    
00148         }
00149     else 
00150         {
00151         return KErrNone;        
00152         }
00153     }
00154 
00163 TInt COggPager::CBody::NextPage(TOggPage& aPage)
00164     {
00165     ogg_page page;
00166     memset(&page, 0, sizeof(page));
00167     while (ETrue)
00168         {
00169         TInt len = ogg_sync_pageseek(iSyncState, &page);
00170         if (len<0) // skipped -len bytes for next possible page start
00171             {
00172             RDebug::Printf("missing or corrupt data at this page position...continue\n");
00173             // go round again
00174             }
00175         else if (len==0)
00176             {
00177             return KErrNotFound; // need more data!
00178             }
00179         else
00180             { // len>0 --> found a page of length len bytes
00181             // copy from page to aPage
00182             OggUtil::MakeOggPage(page, aPage);
00183             return KErrNone;
00184             }
00185         }
00186     }

Generated by  doxygen 1.6.2