RFC3199定义了MP3的RTP打包规则。首先来看看处理*.mp3的sesseion是如何创建的
static ServerMediaSession* createNewSMS(UsageEnvironment& env,
char const* fileName, FILE* /*fid*/) {
...
else if (strcmp(extension, ".mp3") == 0) {
// Assumed to be a MPEG-1 or 2 Audio file:
NEW_SMS("MPEG-1 or 2 Audio");
//去注释STREAM_USING_ADUS宏,传输时使用ADUs,而不是MP3裸帧
// To stream using 'ADUs' rather than raw MP3 frames, uncomment the following:
//#define STREAM_USING_ADUS 1
//去注释INTERLEAVE_ADUS宏,在传输之前奖ADUs重排序(交错)
// To also reorder ADUs before streaming, uncomment the following:
//#define INTERLEAVE_ADUS 1
// (For more information about ADUs and interleaving,
// see <http://www.live555.com/rtp-mp3/>)
Boolean useADUs = False;
Interleaving* interleaving = NULL;
#ifdef STREAM_USING_ADUS
useADUs = True;
#ifdef INTERLEAVE_ADUS
unsigned char interleaveCycle[] = {0,2,1,3}; // or choose your own...
unsigned const interleaveCycleSize
= (sizeof interleaveCycle)/(sizeof (unsigned char));
interleaving = new Interleaving(interleaveCycleSize, interleaveCycle); //创建一个用于交错的filter
#endif
#endif
sms->addSubsession(MP3AudioFileServerMediaSubsession::createNew(env, fileName, reuseSource, useADUs, interleaving)); //注意这里传递的参数
}
}
上面的代码打开宏STREAM_USING_ADUS,则会将MP3帧打包成ADU后,再发送。打开宏INTERLEAVE_ADUS,MP3打包成ADU后,将进行交错排列。可以看到默认情况下,这两个选项都是关闭的。在MP3AudioFileServerMediaSubsession::createNewStreamSource函数中会调用一个createNewStreamSourceCommon函数,处理ADU的打包操作。
FramedSource* MP3AudioFileServerMediaSubsession
::createNewStreamSourceCommon(FramedSource* baseMP3Source, unsigned mp3NumBytes, unsigned& estBitrate) {
FramedSource* streamSource;
fFileDuration = 0.0;
do {
streamSource = baseMP3Source; // by default
if (streamSource == NULL) break;
// Use the MP3 file size, plus the duration, to estimate the stream's bitrate:
if (mp3NumBytes > 0 && fFileDuration > 0.0) {
estBitrate = (unsigned)(mp3NumBytes/(125*fFileDuration) + 0.5); // kbps, rounded
} else {
estBitrate = 128; // kbps, estimate
}
if (fGenerateADUs) { //判断是否打包成ADU后发送
// Add a filter that converts the source MP3s to ADUs:
streamSource = ADUFromMP3Source::createNew(envir(), streamSource);
if (streamSource == NULL) break;
if (fInterleaving != NULL) {
// Add another filter that interleaves the ADUs before packetizing:
streamSource = MP3ADUinterleaver::createNew(envir(), *fInterleaving,
streamSource);
if (streamSource == NULL) break;
}
} else if (fFileDuration > 0.0) {
//
//注意了,对于不需要打包成ADU的情况,这里有一个打包再解包的过程,为是的方便定位
//
// Because this is a seekable file, insert a pair of filters: one that
// converts the input MP3 stream to ADUs; another that converts these
// ADUs back to MP3. This allows us to seek within the input stream without
// tripping over the MP3 'bit reservoir':
streamSource = ADUFromMP3Source::createNew(envir(), streamSource);