These are all the files you need.
Enjoy.

On Sat, Sep 5, 2009 at 11:40 PM, Ross Finlayson <finlay...@live555.com>wrote:

> I've written a file sink saving AAC with ADTS for my own program, and I was
>> wondering if
>> it can be incorporated in live555, in case other people need something
>> like this.
>>
>
> Sure.  Send the code to the mailing list, and I'll take a look at it.
>
> --
>
> Ross Finlayson
> Live Networks, Inc.
> http://www.live555.com/
> _______________________________________________
> live-devel mailing list
> live-devel@lists.live555.com
> http://lists.live555.com/mailman/listinfo/live-devel
>
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
more details.

You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
**********/
// "liveMedia"
// Copyright (c) 1996-2009 Live Networks, Inc.  All rights reserved.
// An ADTS Audio File Sinks to receive AAC
// Implementation

#include "ADTSAudioFileSink.hh"
#include "OutputFile.hh"
#include <cstring>


////////// ADTSAudioFileSink //////////

static unsigned const samplingFrequencyTable[16] = {
  96000, 88200, 64000, 48000,
  44100, 32000, 24000, 22050,
  16000, 12000, 11025, 8000,
  7350, 0, 0, 0
};

ADTSAudioFileSink
::ADTSAudioFileSink(UsageEnvironment& env, FILE* fid,unsigned profile, 
		    unsigned samplingFrequencyIndex,  unsigned channelConfiguration ,
		    unsigned bufferSize, char const* perFrameFileNamePrefix)
  : FileSink(env, fid, bufferSize, perFrameFileNamePrefix),
    profile(profile),samplingFrequencyIndex(samplingFrequencyIndex),
    channelConfiguration(channelConfiguration){

    memset(fixedHeader,0,sizeof fixedHeader);
    // |1111. 1111. 1111. |1|00|1| 
    fixedHeader[0]=0xFF;
    fixedHeader[1]=0xF1;
    
    fixedHeader[2] = (profile & 0x3)<<6;
    fixedHeader[2] |= (samplingFrequencyIndex & 0xF)<<2; //4bits
    fixedHeader[2] |= (channelConfiguration >>2) &0x1;
    fixedHeader[3] |= (channelConfiguration <<6) ;//2 bits
}
 
ADTSAudioFileSink::~ADTSAudioFileSink() {
}

ADTSAudioFileSink*
ADTSAudioFileSink::createNew(UsageEnvironment& env, char const* fileName,
				unsigned profile,
				unsigned samplingFrequency,
				unsigned channel_configuration ,
			    unsigned bufferSize, Boolean oneFilePerFrame) {
  int spindex =-1;
  int i = 0;

  while(spindex < 0 && i < 16 && samplingFrequencyTable[i]!=0){
	if(samplingFrequencyTable[i] == samplingFrequency){
	    spindex=i;
	}
	i++;
  }
  if(spindex<0)
      return NULL;

  do {
    FILE* fid;
    char const* perFrameFileNamePrefix;
    if (oneFilePerFrame) {
      // Create the fid for each frame
      fid = NULL;
      perFrameFileNamePrefix = fileName;
    } else {
      // Normal case: create the fid once
      fid = OpenOutputFile(env, fileName);
      if (fid == NULL) break;
      perFrameFileNamePrefix = NULL;
    }

    return new ADTSAudioFileSink(env, fid,profile,spindex,channel_configuration,bufferSize, perFrameFileNamePrefix);
  } while (0);

  return NULL;
}


Boolean ADTSAudioFileSink::sourceIsCompatibleWithUs(MediaSource& source){
    // the least I can do
    Boolean iscompatible = (strstr(source.MIMEtype(),"/MPEG4-GENERIC" )==NULL? False:True );
    return iscompatible;
}


void ADTSAudioFileSink::afterGettingFrame1(unsigned frameSize,
					  struct timeval presentationTime) {
 
  //				  0	1	2    3	  4	5	6
  unsigned char adts_header[7] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

  uint16_t frame_length = frameSize+7;//the size of adts header is included in the length

  memcpy(adts_header,fixedHeader,4*sizeof(unsigned char));

  adts_header[3]|= (frame_length>>11)&0x3;
  adts_header[4]|= (frame_length>>3)&0xFF;
  adts_header[5]|= (frame_length &0x7)<<5;

  /*for now, i don't understand how to compute adts_buffer_fullness, 
    7FF make it non applicable*/
  uint16_t bit_reservoir =0x7FF;
  
  adts_header[5]|= (bit_reservoir>>4) &0x1F;
  adts_header[6]|= (bit_reservoir&0x3F)<< 2;
  
  
  addData(adts_header, 7, presentationTime);

  // Call the parent class to complete the normal file write with the input data:
  FileSink::afterGettingFrame1(frameSize, presentationTime);
}
/**********
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)

This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
more details.

You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
**********/
// "liveMedia"
// Copyright (c) 1996-2009 Live Networks, Inc.  All rights reserved.
// An ADTS Audio File Sinks to receive AAC
// C++ header

#ifndef _ADTS_AUDIO_FILE_SINK_HH
#define _ADTS_AUDIO_FILE_SINK_HH

#ifndef _FILE_SINK_HH
#include "FileSink.hh"
#endif

class ADTSAudioFileSink: public FileSink {
public:
  static ADTSAudioFileSink* createNew(UsageEnvironment& env, char const* fileName,
				    unsigned profile=1,
				    unsigned samplingFrequency = 44100, 
				    unsigned channelConfiguration = 2 ,  
				    unsigned bufferSize = 10000,
				    Boolean oneFilePerFrame = False);
  // additionnal parameters needed for a ADTS header

protected:
  ADTSAudioFileSink(UsageEnvironment& env, FILE* fid,unsigned profile, 
		    unsigned samplingFrequencyIndex,  unsigned channelConfiguration ,
		    unsigned bufferSize, char const* perFrameFileNamePrefix);
      // called only by createNew()
  virtual ~ADTSAudioFileSink();

protected: // redefined virtual functions:
  virtual Boolean sourceIsCompatibleWithUs(MediaSource& source);
  virtual void afterGettingFrame1(unsigned frameSize,
				  struct timeval presentationTime);
private:
  uint8_t profile;
  uint8_t samplingFrequencyIndex;
  uint8_t channelConfiguration;
  
  unsigned char fixedHeader[4];

};

#endif
_______________________________________________
live-devel mailing list
live-devel@lists.live555.com
http://lists.live555.com/mailman/listinfo/live-devel

Reply via email to