Skip to content

Commit

Permalink
Support mono audio for PcmFormat::Int16Planes.
Browse files Browse the repository at this point in the history
  • Loading branch information
OtherCrashOverride committed Oct 8, 2016
1 parent f090597 commit 71af462
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
43 changes: 41 additions & 2 deletions src/Media/AlsaAudioSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ void AlsaAudioSinkElement::ProcessBuffer(PcmDataBufferSPTR pcmBuffer)

if (pcmData->Format == PcmFormat::Int16)
{
if (alsa_channels != 2)
{
throw InvalidOperationException();
}

if (pcmData->Channels < 2)
throw NotSupportedException();


int stride = pcmData->Channels * sizeof(short);
unsigned char* source = (unsigned char*)pcmData->Channel[0];

Expand All @@ -112,11 +121,32 @@ void AlsaAudioSinkElement::ProcessBuffer(PcmDataBufferSPTR pcmBuffer)
}
else if (pcmData->Format == PcmFormat::Int16Planes)
{
if (alsa_channels != 2)
{
throw InvalidOperationException();
}


short* channels[alsa_channels] = { 0 };

channels[0] = (short*)pcmData->Channel[0];
channels[1] = (short*)pcmData->Channel[1];
int channelCount = pcmData->Channels;

switch (channelCount)
{
case 0:
throw InvalidOperationException("Unexpected zero channel count.");

case 1:
// Output the mono channel over both stereo channels
channels[0] = (short*)pcmData->Channel[0];
channels[1] = (short*)pcmData->Channel[0];
break;

default:
channels[0] = (short*)pcmData->Channel[0];
channels[1] = (short*)pcmData->Channel[1];
break;
}

int index = 0;
for (int i = 0; i < pcmData->Samples; ++i)
Expand All @@ -135,6 +165,9 @@ void AlsaAudioSinkElement::ProcessBuffer(PcmDataBufferSPTR pcmBuffer)
throw InvalidOperationException();
}

if (pcmData->Channels < 2)
throw NotSupportedException();


float* channels[alsa_channels] = { 0 };

Expand Down Expand Up @@ -184,9 +217,15 @@ void AlsaAudioSinkElement::ProcessBuffer(PcmDataBufferSPTR pcmBuffer)
}
else if (pcmData->Format == PcmFormat::Int32)
{
if (alsa_channels != 2)
{
throw InvalidOperationException();
}

if (pcmData->Channels < 2)
throw NotSupportedException();


// Signed 32 bit, interleaved
int srcIndex = 0;
int dstIndex = 0;
Expand Down
56 changes: 35 additions & 21 deletions src/Media/AudioCodec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,34 +218,48 @@ void AudioCodecElement::ProcessBuffer(AVPacketBufferSPTR buffer, AVFrameBufferSP
}
else
{
int leftChannelIndex = av_get_channel_layout_channel_index(
decoded_frame->channel_layout,
AV_CH_FRONT_LEFT);

int rightChannelIndex = av_get_channel_layout_channel_index(
decoded_frame->channel_layout,
AV_CH_FRONT_RIGHT);

int centerChannelIndex = av_get_channel_layout_channel_index(
decoded_frame->channel_layout,
AV_CH_FRONT_CENTER);


// left, right, center
const int DOWNMIX_MAX_CHANNELS = 3;

void* channels[DOWNMIX_MAX_CHANNELS] = { 0 };
channels[0] = (void*)decoded_frame->data[leftChannelIndex];
channels[1] = (void*)decoded_frame->data[rightChannelIndex];

if (decoded_frame->channels > 2)
if (decoded_frame->channels == 1)
{
// Mono
int monoChannelIndex = av_get_channel_layout_channel_index(
decoded_frame->channel_layout,
AV_CH_FRONT_CENTER);


channels[0] = (void*)decoded_frame->data[monoChannelIndex];
}
else
{
channels[2] = (void*)decoded_frame->data[centerChannelIndex];
int leftChannelIndex = av_get_channel_layout_channel_index(
decoded_frame->channel_layout,
AV_CH_FRONT_LEFT);

int rightChannelIndex = av_get_channel_layout_channel_index(
decoded_frame->channel_layout,
AV_CH_FRONT_RIGHT);

int centerChannelIndex = av_get_channel_layout_channel_index(
decoded_frame->channel_layout,
AV_CH_FRONT_CENTER);


channels[0] = (void*)decoded_frame->data[leftChannelIndex];
channels[1] = (void*)decoded_frame->data[rightChannelIndex];

if (decoded_frame->channels > 2)
{
channels[2] = (void*)decoded_frame->data[centerChannelIndex];
}
//else
//{
// channels[2] = nullptr;
//}
}
//else
//{
// channels[2] = nullptr;
//}

int channelCount = std::min(DOWNMIX_MAX_CHANNELS, decoded_frame->channels);
for (int i = 0; i < channelCount; ++i)
Expand Down

0 comments on commit 71af462

Please sign in to comment.