📄 shoutcaststream.h
字号:
/*
* ==============================================================================
* Name : ShoutcastStream.h
* Part of : Shoutcast Engine
* Interface :
* Description : Defines the shoutcast stream class of the shoutcast engine.
* Version : 2
*
* Copyright (c) 2006, Nokia Corporation All rights reserved. Redistribution
* and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met: Redistributions
* of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer. Redistributions in binary form
* must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided
* with the distribution. Neither the name of the Nokia Corporation nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission. THIS
* SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* ==============================================================================
*/
#ifndef __SHOUTCASTSTREAM_H
#define __SHOUTCASTSTREAM_H
// INCLUDE FILES
#include <e32std.h>
#include <es_sock.h>
#include <in_sock.h>
#include <mda\common\audio.h>
#include <MdaAudioOutputStream.h>
#if RELEASE_LOG
#include <flogger.h>
#endif
#include "MShoutcastStreamObserver.h"
#include "ScMetadata.h"
// FORWARD DECLARATIONS
class CEventDispatcher;
// CLASS DECLARATION
/**
* Frame info structure.
*/
class TAudioFrameInfo
{
public:
TInt iMode; // encoding mode
TInt iBitRate; // bitrate (bit/s)
TInt iSamplingRate; // sampling frequency (Hz)
TInt iChannels; // number of channels
TInt iFrameSize; // encoded size (bytes)
TInt iFrameSamples; // decoded size (samples per channel)
TInt iSamplingRateOut; // sampling frequency after conversion (Hz)
TInt iChannelsOut; // number of audio channels after conversion (1 or 2)
TInt iFrameSamplesOut; // decoded size after conversion (samples per channel)
TInt iPadding; // padding flag (TRUE or FALSE, TRUE if p slot exists)
TInt iId; // id of algorithm (1 MPEG-1, 0 MPEG-2)
};
/**
* This class is the heart of the shoutcast engine. It handles the shoutcast protocol,
* interfaces to the audio stream player, and extracts metadata from the data stream.
*
*/
class CShoutcastStream : public CActive,
public MMdaAudioOutputStreamCallback
{
//instantiation
public: // Contructors
/**
* Create a CShoutcastStream object
* @param aUrl The URL of the stream
* @param aObserver The observer of the shoutcast stream object
* @return pointer to new object
*/
static CShoutcastStream* NewL(const TDesC8& aUrl, MShoutcastStreamObserver& aObserver);
/**
* Destructor.
*/
~CShoutcastStream();
public: // New functions
void CloseAndClean();
/**
* Get the requested meta data entry
* @return the requested meta data entry
*/
CMetaDataEntry* GetMetaDataEntryL(TInt aIndex);
/**
* Get the number of metadata entries
* @return number of metadata entries
*/
TInt GetNumberOfMetaDataEntries();
/**
* Get the maximum volume level
* @return Maximum Volume Level
*/
TInt MaxVolume();
/**
* Preparing the stream and allocates resources.
*/
void PrimeL();
/**
* Start the play process and moving data to the stream output.
*/
void PlayL();
/**
* Pauses the play process.
*/
void Pause();
/**
* Terminate the play process
*/
void Stop();
/**
* Set the volume
* @param aVolume The desired volume level
*/
void SetVolume(TInt aVolume);
/**
* Get the current volume level
* @return Volume Level
*/
TInt Volume();
public: // from MMdaAudioOutputStreamCallback
/**
* Called when CMdaAudioOutputStream::Open() has completed,
* indicating that the audio output stream is ready for use.
* @param aError An error value which indicates if open was completed successfully.
* KErrNone if succeeded
*/
virtual void MaoscOpenComplete(TInt aError);
/**
* Called when the descriptor has been copied.
* @param aError KErrNone if the copy succeeded, otherwise one of the system
* error codes. KErrAbort indicates that the client has stopped the stream playing
* before the descriptor has been copied.
*/
virtual void MaoscBufferCopied(TInt aError, const TDesC8& aBuffer);
/**
* Called when playback terminates as a result of a CMdaAudioOutputStream::Stop().
* @param aError An error value which indicates play success or not. KErrNone if
* the close succeeded, otherwise one of the system error codes.
*/
virtual void MaoscPlayComplete(TInt aError);
private:
/**
* C++ default constructor.
*/
CShoutcastStream();
/**
* By default Symbian 2nd phase constructor is private.
* @param aUrl The URL of the stream
* @param aObserver The observer of the shoutcast stream
*/
void ConstructL(const TDesC8& aUrl, MShoutcastStreamObserver& aObserver);
/**
* Fill the play buffer
*/
void FillBufferL();
// from CActive
void DoCancel();
void RunL();
TInt RunError(TInt aError);
/**
* Parse the URL and extracts the IP address
*/
void GetIPFromURL();
/**
* Remove any metadata from the buffer
*/
void RemoveMetadata(TDes8& ptrMetadata);
/**
* Retreive and remove any metadata from the buffer
*/
void GetAndRemoveMetadata(TUint8 *aBuf,TInt &aLength);
/**
* Establish connection to the server
* @param aReconnecting Indicates whether this is a reconnection or new connection
*/
void ConnectToServerL(TBool aReconnecting = EFalse);
/**
* Send a request to the server
*/
void SendRequestToServerL();
/**
* Receive response from the server
*/
void ReceiveResponseFromServerL();
/**
* Parse response from the server
*/
void ParseResponseFromServerL();
/**
* Parse response from the server
*/
void InitStreamOutputL();
/**
* Reset buffer variables
*/
void ResetBufferVars();
/**
* Request to read data from buffer
*/
void ReadRequest();
/**
* Done reading data
*/
TInt ReadRequestDone(TInt aRequested,TInt aRead);
/**
* Request more data from the network
*/
void ReadRequestHelper();
/**
* Data is ready for reading
*/
TInt ReadRequestHelperDone();
/**
* Disconnected from server
* @param aError The error code
*/
void Disconnect(TInt aError);
/**
* Scans the header to extract encoding data.
* @param aBuf the buffer to be scanned
* @param aLength length of the buffer.
* @return ETrue if sucessfull, otherwise, EFalse.
*/
TInt ScanHeader(TUint8 *aBuf, TInt aLength);
/**
* Returns the byte position of the first frame.
* @param aBuf the buffer to be scanned
* @param aLength length of the buffer.
* @return byte position of the first frame
*/
TInt Mp3SeekSync(TUint8 *aBuf, TInt aLength);
/**
* Returns the byte position of the first frame.
* @param aBuf the buffer to be scanned
* @param aLength length of the buffer.
* @return byte position of the first frame
*/
TInt AacSeekSync(TUint8 *aBuf, TInt aLength);
/**
* Test if the pointer points to a valid AAC frame
* @param aPtr Pointer to the presumed frame
* @param aBufLen Length of the data
* @return Length of the buffer
*/
TBool IsValidAacFrame( const TUint8* aPtr, TInt aBufLen );
/**
* Returns frame info.
* @param aBuf Pointer to the data
* @param aBufLen Length of the data
* @param aInfo Contains the frame info on return
* @return frame length
*/
TInt GetMp3FrameInfo( const TUint8* aBuf, TInt aBufLen, TAudioFrameInfo& aInfo );
/**
* Returns frame info.
* @param aBuf Pointer to the data
* @param aBufLen Length of the data
* @param aInfo Contains the frame info on return
* @return frame length
*/
TInt GetAacFrameInfo( const TUint8* aBuf, TInt aBufLen, TAudioFrameInfo& aInfo );
/**
* Get audio settings from the audio stream.
*/
void GetAudioSettings();
/**
* Derive audio settings from data received from the server.
*/
void DeriveAudioSettings();
private: // Data
// Engine States
enum TState
{
ENotConnected,
EResolving,
EConnecting,
ESendingRequest,
EReceivingResponse,
EAnswerOK,
EAnswerError,
EInitMP3Dec,
EInitStreamOutput,
EData,
EDisconnecting
};
// Current engine state
TState iState;
// Event dispatcher
CEventDispatcher* iDispatcher;
// Output Stream object
CMdaAudioOutputStream* iStreamOutput;
// Settings for audio stream (but not used)
TMdaAudioDataSettings iSettings;
// Sampling rate found in header
TInt iSamplingRate;
// Bit rate (bps) found in header
TInt iBitRate;
// Number of channels found in header
TInt iChannels;
// Buffers for url parsing
TBuf8<URL_SIZE> iURL;
TBuf8<URL_SIZE> iPath;
TBuf8<URL_SIZE> iIp;
// stream buffering related
TUint8 *iBuffer2Decode;
TUint8 *iBuffer2Play;
TPtr8 iPtrBuffer2Decode;
TPtr8 iPtrBuffer2Play;
TInt iLenBuffer2Decode;
TInt iLenBuffer2Play;
TInt iPosBuffer2Decode;
TInt iReadData;
TBool iReadingActive;
//for accounting and displaying
TInt iTotalBytesReceived;
//nr of hundred of KBytes
TInt iNrHKB;
TInt iRRreq,iRRread;
//bitrate
TInt iBitrate;
TInt iInstantBitrate;
TTime iLastReading;
RArray<TInt> iBitsRead;
RArray<TInt64> iTimeIntervals;
TBool iWritingToStream;
TBool iPausedForBuffering;
TInt iMaxFrameSize;
TInt iVolume;
//MetaData related
TInt iMetaint;
TInt iCnt;
TInt iDataSeen;
RPointerArray<CMetaDataEntry> iMetadata;
TBuf<METADATA_BUFFER_SIZE> iTempMetadata;
TUint8 *iMetadataBuffer;
TMdaAudioDataSettings iAudioSettings;
TFourCC iDataType;
// Networking related
TInetAddr* iAddr;
RSocketServ iSocksvr;
RSocket iSock;
#if RELEASE_LOG
RFileLogger iLog;
#endif
};
#endif // __SHOUTCASTSTREAM_H
// End of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -