CAudioEncoder *CAVMediaFlow::FindOrCreateAudioEncoder (CAudioProfile *ap) { const char *ap_name = ap->GetName(); CAudioEncoder *ae_ptr = m_audio_encoder_list; while (ae_ptr != NULL) { if (strcmp(ap_name, ae_ptr->GetProfileName()) == 0) { return ae_ptr; } ae_ptr = ae_ptr->GetNext(); } ae_ptr = AudioEncoderCreate(ap, m_audio_encoder_list, m_pConfig->GetIntegerValue(CONFIG_AUDIO_CHANNELS), m_pConfig->GetIntegerValue(CONFIG_AUDIO_SAMPLE_RATE), m_pConfig->GetIntegerValue(CONFIG_RTP_PAYLOAD_SIZE)); m_audio_encoder_list = ae_ptr; // need to init, find max samples here.. ae_ptr->Init(); m_maxAudioSamplesPerFrame = MAX(m_maxAudioSamplesPerFrame, ae_ptr->GetSamplesPerFrame()); ae_ptr->StartThread(); m_audioSource->AddSink(ae_ptr); debug_message("Added audio encoder %s", ap_name); return ae_ptr; }
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; }