void CAudioEncoder::Initialize (void)
{
  // called from derived classes init function from the start function
  // in the media flow
  m_audioSrcFrameNumber = 0;
  m_audioDstFrameNumber = 0;
  m_audioDstSampleNumber = 0;
  m_audioSrcElapsedDuration = 0;
  m_audioDstElapsedDuration = 0;

  // destination parameters are from the audio profile
  m_audioDstType = GetFrameType();
  m_audioDstSampleRate = m_pConfig->GetIntegerValue(CFG_AUDIO_SAMPLE_RATE);
  m_audioDstChannels = m_pConfig->GetIntegerValue(CFG_AUDIO_CHANNELS);
  m_audioDstSamplesPerFrame = GetSamplesPerFrame();

  // if we need to resample
  if (m_audioDstSampleRate != m_audioSrcSampleRate) {
    // create a resampler for each audio destination channel - 
    // we will combine the channels before resampling
    m_audioResample = (resample_t *)malloc(sizeof(resample_t) *
					   m_audioDstChannels);
    for (int ix = 0; ix < m_audioDstChannels; ix++) {
      m_audioResample[ix] = st_resample_start(m_audioSrcSampleRate, 
					      m_audioDstSampleRate);
    }
  }

  // this calculation doesn't take into consideration the resampling
  // size of the src.  4 times might not be enough - we need most likely
  // 2 times the max of the src samples and the dest samples

  m_audioPreEncodingBufferLength = 0;
  m_audioPreEncodingBufferMaxLength =
    4 * DstSamplesToBytes(m_audioDstSamplesPerFrame);

  m_audioPreEncodingBuffer = (u_int8_t*)realloc(
						m_audioPreEncodingBuffer,
						m_audioPreEncodingBufferMaxLength);
}
Esempio n. 2
0
bool CMediaSource::SetAudioSrc(
			       MediaType srcType,
			       u_int8_t srcChannels,
			       u_int32_t srcSampleRate)
{
  // audio source info 
  m_audioSrcType = srcType;
  m_audioSrcChannels = srcChannels;
  m_audioSrcSampleRate = srcSampleRate;
  m_audioSrcSamplesPerFrame = 0;	// unknown, presumed variable

  // init audio encoder
  delete m_audioEncoder;

  m_audioEncoder = AudioEncoderCreate(
				      m_pConfig->GetStringValue(CONFIG_AUDIO_ENCODER));

  if (m_audioEncoder == NULL) {
    return false;
  }

  if (!m_audioEncoder->Init(m_pConfig, m_sourceRealTime)) {
    delete m_audioEncoder;
    m_audioEncoder = NULL;
    return false;
  }

  m_audioDstType = m_audioEncoder->GetFrameType();

  m_audioDstSamplesPerFrame = 
    m_audioEncoder->GetSamplesPerFrame();

  // if we need to resample
  if (m_audioDstSampleRate != m_audioSrcSampleRate) {
    // create a resampler for each audio destination channel - 
    // we will combine the channels before resampling
    m_audioResample = (resample_t *)malloc(sizeof(resample_t) *
					   m_audioDstChannels);
    for (int ix = 0; ix <= m_audioDstChannels; ix++) {
      m_audioResample[ix] = st_resample_start(m_audioSrcSampleRate, 
					      m_audioDstSampleRate);
    }
  }

  // this calculation doesn't take into consideration the resampling
  // size of the src.  4 times might not be enough - we need most likely
  // 2 times the max of the src samples and the dest samples

  m_audioPreEncodingBufferLength = 0;
  m_audioPreEncodingBufferMaxLength =
    4 * DstSamplesToBytes(m_audioDstSamplesPerFrame);

  m_audioPreEncodingBuffer = (u_int8_t*)realloc(
						m_audioPreEncodingBuffer,
						m_audioPreEncodingBufferMaxLength);
		
  if (m_audioPreEncodingBuffer == NULL) {
    delete m_audioEncoder;
    m_audioEncoder = NULL;
    return false;
  }

  return true;
}