USB audio: use new(std::nothrow) for allocations

* Use new(std::nothrow) for allocations;
* Input buffer index counter restored;
* Improve unsupported UI controls debug info tracing;
* Some cleanup.
This commit is contained in:
Siarzhuk Zharski 2013-08-06 06:14:28 +02:00
parent 743d75946f
commit 14f92002e1
7 changed files with 81 additions and 96 deletions

View File

@ -380,7 +380,7 @@ MixerUnit::MixerUnit(AudioControlInterface* interface,
mixerControlsSize = Mixer->length - 10 - Mixer->num_input_pins;
fStringIndex = *(mixerControlsData + mixerControlsSize);
#if 1 // TEST
#if 0 // TEST
if (fOutChannelsNumber > 2) {
// fOutChannelsNumber = 2;
// fChannelsConfig = 0x03;
@ -397,7 +397,7 @@ MixerUnit::MixerUnit(AudioControlInterface* interface,
mixerControlsData[10] = 0x02;
mixerControlsData[11] = 0x01;
}
#endif
#endif
} else {
usb_audio_output_channels_descriptor* OutChannels
@ -511,7 +511,7 @@ SelectorUnit::OutCluster()
{
if (fInterface == NULL)
return NULL;
for (int i = 0; i < fInputPins.Count(); i++) {
_AudioControl* control = fInterface->Find(fInputPins[i]);
if (control == NULL)
@ -724,7 +724,7 @@ ProcessingUnit::ProcessingUnit(AudioControlInterface* interface,
TRACE(UAC, "Number of input pins:%d\n", Processing->num_input_pins);
for (size_t i = 0; i < Processing->num_input_pins; i++) {
fInputPins.PushBack(Processing->input_pins[i]);
fInputPins.PushBack(Processing->input_pins[i]);
TRACE(UAC, "Input pin #%d:%d\n", i, fInputPins[i]);
}
@ -959,46 +959,46 @@ AudioControlInterface::Init(size_t interface, usb_interface_info* Interface)
InitACHeader(interface, Header);
break;
case USB_AUDIO_AC_INPUT_TERMINAL:
control = new InputTerminal(this, Header);
control = new(std::nothrow) InputTerminal(this, Header);
break;
case USB_AUDIO_AC_OUTPUT_TERMINAL:
control = new OutputTerminal(this, Header);
control = new(std::nothrow) OutputTerminal(this, Header);
break;
case USB_AUDIO_AC_MIXER_UNIT:
control = new MixerUnit(this, Header);
control = new(std::nothrow) MixerUnit(this, Header);
break;
case USB_AUDIO_AC_SELECTOR_UNIT:
control = new SelectorUnit(this, Header);
control = new(std::nothrow) SelectorUnit(this, Header);
break;
case USB_AUDIO_AC_FEATURE_UNIT:
control = new FeatureUnit(this, Header);
control = new(std::nothrow) FeatureUnit(this, Header);
break;
case USB_AUDIO_AC_PROCESSING_UNIT:
if (SpecReleaseNumber() < 200)
control = new ProcessingUnit(this, Header);
control = new(std::nothrow) ProcessingUnit(this, Header);
else
control = new EffectUnit(this, Header);
control = new(std::nothrow) EffectUnit(this, Header);
break;
case USB_AUDIO_AC_EXTENSION_UNIT:
if (SpecReleaseNumber() < 200)
control = new ExtensionUnit(this, Header);
control = new(std::nothrow) ExtensionUnit(this, Header);
else
control = new ProcessingUnit(this, Header);
control = new(std::nothrow) ProcessingUnit(this, Header);
break;
case USB_AUDIO_AC_EXTENSION_UNIT_R2:
control = new ExtensionUnit(this, Header);
control = new(std::nothrow) ExtensionUnit(this, Header);
break;
case USB_AUDIO_AC_CLOCK_SOURCE_R2:
control = new ClockSource(this, Header);
control = new(std::nothrow) ClockSource(this, Header);
break;
case USB_AUDIO_AC_CLOCK_SELECTOR_R2:
control = new ClockSelector(this, Header);
control = new(std::nothrow) ClockSelector(this, Header);
break;
case USB_AUDIO_AC_CLOCK_MULTIPLIER_R2:
control = new ClockMultiplier(this, Header);
control = new(std::nothrow) ClockMultiplier(this, Header);
break;
case USB_AUDIO_AC_SAMPLE_RATE_CONVERTER_R2:
control = new SampleRateConverter(this, Header);
control = new(std::nothrow) SampleRateConverter(this, Header);
break;
}
@ -1551,7 +1551,7 @@ AudioControlInterface::_CollectMixerUnitControls(
else
count++;
}
// take care about surround bus
if (inLeft == inRight)
break;
@ -1628,16 +1628,16 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
{ 15, 17, "Top Back" },
{ 16, 16, "Top Back Center" }
};
Vector<_MixPageCollector*> mixControls;
_MixPageCollector* genericPage = new _MixPageCollector("Mixer");
_MixPageCollector* genericPage = new(std::nothrow) _MixPageCollector("Mixer");
mixControls.PushBack(genericPage);
// page for extended in (>2) and out (>2) mixer controls
size_t controlsOnExMixerPage = 0;
_MixPageCollector* exMixerPage = new _MixPageCollector("Mixer");
size_t controlsOnExMixerPage = 0;
_MixPageCollector* exMixerPage = new(std::nothrow) _MixPageCollector("Mixer");
AudioChannelCluster* outCluster = mixer->OutCluster();
int inOffset = 0;
@ -1659,7 +1659,7 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
&& inChannel < inCluster->ChannelsCount(); in++) {
if ((inCluster->ChannelsConfig() & (1 << in)) == 0)
continue;
for (size_t out = 0, outChannel = 0; out < kChannels
&& outChannel < outCluster->ChannelsCount(); out++) {
if ((outCluster->ChannelsConfig() & (1 << out)) == 0)
@ -1677,7 +1677,7 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
(inOffset + inChannel) * outCluster->ChannelsCount()
+ outChannel, mixer->ID(), fInterface);
}
outChannel++;
}
@ -1693,7 +1693,7 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
controlIds[in][out], in, out);
// second step - distribute controls on
// mixer pages in logical groups
// mixer pages in logical groups
uint32 exChannelsMask = ~(B_CHANNEL_LEFT | B_CHANNEL_RIGHT);
bool inIsEx = (inCluster->ChannelsConfig() & exChannelsMask) != 0;
bool outIsEx = (outCluster->ChannelsConfig() & exChannelsMask) != 0;
@ -1707,8 +1707,8 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
control->Name(), channelPairs[i].name,
*mixControls[0]);
continue; // go next input cluster
}
}
if (!outIsEx) {
// special case - extended (>2 channels) input cluster
// connected to 2-channels output - add into generic "Mixer" page
@ -1729,9 +1729,9 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
if (in == out)
strlcpy(outName, channelPairs[out].name, sizeof(outName));
else
snprintf(outName, sizeof(outName), "%s to %s",
snprintf(outName, sizeof(outName), "%s to %s",
channelPairs[in].name, channelPairs[out].name);
controlsOnExMixerPage += _CollectMixerUnitControls(controlIds,
channelPairs[in].inLeft, channelPairs[out].inLeft,
channelPairs[in].inRight, channelPairs[out].inRight,
@ -1740,7 +1740,7 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
if (controlsOnExMixerPage >= 6) {
mixControls.PushBack(exMixerPage);
exMixerPage = new _MixPageCollector("Mixer");
exMixerPage = new(std::nothrow) _MixPageCollector("Mixer");
controlsOnExMixerPage = 0;
}
}
@ -1750,7 +1750,7 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
mixControls.PushBack(exMixerPage);
else
delete exMixerPage;
// final step - fill multiaudio controls info with
// already structured pages/controls info arrays
for (Vector<_MixPageCollector*>::Iterator I = mixControls.Begin();
@ -1808,7 +1808,7 @@ AudioControlInterface::ListMixControls(multi_mix_control_info* Info)
}
// separate input and output Feature units
// and collect mixer units that can be controlled
// and collect mixer units that can be controlled
AudioControlsMap InputControlsMap;
AudioControlsMap OutputControlsMap;
AudioControlsMap MixerControlsMap;
@ -1872,20 +1872,20 @@ AudioControlInterface::GetMix(multi_mix_value_info* Info)
ID_FROM_CTLID(Info->values[i].id));
continue;
}
switch (control->SubType()) {
case USB_AUDIO_AC_FEATURE_UNIT:
switch(CS_FROM_CTLID(Info->values[i].id)) {
case USB_AUDIO_VOLUME_CONTROL:
length = 2;
break;
//case 0: // Selector Unit
case USB_AUDIO_MUTE_CONTROL:
case USB_AUDIO_AUTOMATIC_GAIN_CONTROL:
length = 1;
break;
default:
TRACE(ERR, "Unsupported control type %#02x ignored.\n",
TRACE(ERR, "Unsupported control id:%08x of type %#02x "
"ignored.\n", ID_FROM_CTLID(Info->values[i].id),
CS_FROM_CTLID(Info->values[i].id));
continue;
}
@ -1897,8 +1897,8 @@ AudioControlInterface::GetMix(multi_mix_value_info* Info)
length = 2;
break;
default:
TRACE(ERR, "Control type %d is not suported\n",
control->SubType());
TRACE(ERR, "Control id:%08x of type %d is not supported\n",
ID_FROM_CTLID(Info->values[i].id), control->SubType());
continue;
}
@ -1977,7 +1977,7 @@ AudioControlInterface::SetMix(multi_mix_value_info* Info)
ID_FROM_CTLID(Info->values[i].id));
continue;
}
switch (control->SubType()) {
case USB_AUDIO_AC_FEATURE_UNIT:
switch(CS_FROM_CTLID(Info->values[i].id)) {
@ -2009,7 +2009,8 @@ AudioControlInterface::SetMix(multi_mix_value_info* Info)
Info->values[i].enable);
break;
default:
TRACE(ERR, "Unsupported control type %#02x ignored.\n",
TRACE(ERR, "Unsupported control id:%08x of type %#02x "
"ignored.\n", ID_FROM_CTLID(Info->values[i].id),
CS_FROM_CTLID(Info->values[i].id));
continue;
}
@ -2032,8 +2033,8 @@ AudioControlInterface::SetMix(multi_mix_value_info* Info)
Info->values[i].gain);
break;
default:
TRACE(ERR, "Control type %d is not suported\n",
control->SubType());
TRACE(ERR, "Control id:%08x of type %d is not supported\n",
Info->values[i].id, control->SubType());
continue;
}

View File

@ -29,7 +29,7 @@ public:
uint8 ChannelsCount() { return fOutChannelsNumber; }
uint32 ChannelsConfig() { return fChannelsConfig; }
bool HasChannel(uint32 location);
protected:
@ -104,7 +104,7 @@ public:
InputTerminal(AudioControlInterface* interface,
usb_audiocontrol_header_descriptor* Header);
virtual ~InputTerminal();
virtual const char* Name();
protected:
@ -128,7 +128,7 @@ public:
MixerUnit(AudioControlInterface* interface,
usb_audiocontrol_header_descriptor* Header);
virtual ~MixerUnit();
virtual const char* Name() { return "Mixer"; }
bool HasProgrammableControls();
bool IsControlProgrammable(int inChannel, int outChannel);

View File

@ -48,7 +48,7 @@ ASInterfaceDescriptor::ASInterfaceDescriptor(
fDelay(0),
fFormatTag(0)
{
// TODO: what aboput rev 2???????
// TODO: what aboput rev 2???????
fTerminalLink = Descriptor->terminal_link;
fDelay = Descriptor->r1.delay;
fFormatTag = Descriptor->r1.format_tag;
@ -119,18 +119,13 @@ _ASFormatDescriptor::GetSamFreq(const usb_audio_sampling_freq& freq)
return freq.bytes[0] | freq.bytes[1] << 8 | freq.bytes[2] << 16;
}
// TODO: beautify!!!
usb_audio_sampling_freq
_ASFormatDescriptor::GetSamFreq(uint32 samplingRate)
{
usb_audio_sampling_freq freq;
for (size_t i = 0; i < 3; i++)
freq.bytes[i] = 0xFF & samplingRate >> 8 * i;
// return freq.bytes[0] | freq.bytes[1] << 8 | freq.bytes[2] << 16;
/* data[0] = 0xFF & samplingRate;
data[1] = 0xFF & samplingRate >> 8;
data[2] = 0xFF & samplingRate >> 16; */
return freq;
}
@ -244,10 +239,10 @@ AudioStreamAlternate::SetSamplingRate(uint32 newRate)
TRACE(ERR, "Format not set for active alternate\n");
return B_NO_INIT;
}
Vector<uint32>& frequencies = format->fSampleFrequencies;
bool continuous = format->fSampleFrequencyType == 0;
if (newRate == 0) { // by default select max available
fSamplingRate = 0;
if (continuous)
@ -305,7 +300,7 @@ AudioStreamAlternate::GetSamplingRateIds()
TRACE(ERR, "Format not set for active alternate\n");
return 0;
}
uint32 rates = 0;
Vector<uint32>& frequencies = format->fSampleFrequencies;
if (format->fSampleFrequencyType == 0) { // continuous frequencies
@ -366,7 +361,7 @@ AudioStreamAlternate::GetFormatId()
Interface()->fFormatTag);
break;
}
return formats;
}
@ -425,14 +420,15 @@ AudioStreamingInterface::AudioStreamingInterface(
switch(Header->descriptor_subtype) {
case USB_AUDIO_AS_GENERAL:
if (ASInterface == 0)
ASInterface = new ASInterfaceDescriptor(
ASInterface = new(std::nothrow)
ASInterfaceDescriptor(
(usb_audio_streaming_interface_descriptor*)Header);
else
TRACE(ERR, "Duplicate AStream interface ignored.\n");
break;
case USB_AUDIO_AS_FORMAT_TYPE:
if (ASFormat == 0)
ASFormat = new TypeIFormatDescriptor(
ASFormat = new(std::nothrow) TypeIFormatDescriptor(
(usb_audio_format_descriptor*) Header);
else
TRACE(ERR, "Duplicate AStream format ignored.\n");
@ -449,7 +445,7 @@ AudioStreamingInterface::AudioStreamingInterface(
if (ASEndpoint == 0) {
usb_endpoint_descriptor* Endpoint
= Interface->endpoint[0].descr;
ASEndpoint = new ASEndpointDescriptor(Endpoint,
ASEndpoint = new(std::nothrow) ASEndpointDescriptor(Endpoint,
(usb_audio_streaming_endpoint_descriptor*)Header);
} else
TRACE(ERR, "Duplicate AStream endpoint ignored.\n");
@ -460,7 +456,7 @@ AudioStreamingInterface::AudioStreamingInterface(
"unknown descriptor type %#04x.\n", Header->descriptor_type);
}
fAlternates.Add(new AudioStreamAlternate(alt, ASInterface,
fAlternates.Add(new(std::nothrow) AudioStreamAlternate(alt, ASInterface,
ASEndpoint, ASFormat));
}
}

View File

@ -312,7 +312,7 @@ Device::_MultiGetDescription(multi_description* multiDescription)
for (int i = 0; i < fStreams.Count(); i++) {
uint8 id = fStreams[i]->TerminalLink();
_AudioControl* control = fAudioControl.Find(id);
//if (control->SubType() == USB_AUDIO_AC_OUTPUT_TERMINAL) {
// if (control->SubType() == USB_AUDIO_AC_OUTPUT_TERMINAL) {
// if (control->SubType() == USB_AUDIO_AC_INPUT_TERMINAL) {
// USBTerminals.PushFront(control);
// fStreams[i]->GetFormatsAndRates(Description);
@ -320,7 +320,7 @@ Device::_MultiGetDescription(multi_description* multiDescription)
// if (control->SubType() == IDSInputTerminal) {
USBTerminals.PushBack(control);
fStreams[i]->GetFormatsAndRates(&Description);
//}
// }
}
Vector<multi_channel_info> Channels;
@ -619,7 +619,8 @@ Device::_SetupEndpoints()
break;
case USB_AUDIO_INTERFACE_AUDIOSTREAMING_SUBCLASS:
{
Stream* stream = new Stream(this, i, &config->interface[i]);
Stream* stream = new(std::nothrow) Stream(this, i,
&config->interface[i]);
if (B_OK == stream->Init()) {
// put the stream in the correct order:
// first output that input ones.

View File

@ -46,7 +46,7 @@ usb_audio_device_added(usb_device device, void** cookie)
}
// no such device yet, create a new one
Device* audioDevice = new Device(device);
Device* audioDevice = new(std::nothrow) Device(device);
if (audioDevice == 0)
return ENODEV;

View File

@ -116,8 +116,8 @@ Stream::_ChooseAlternate()
fIsInput = (endpoint->fEndpointAddress & USB_ENDPOINT_ADDR_DIR_IN)
== USB_ENDPOINT_ADDR_DIR_IN;
// if (fIsInput)
// fCurrentBuffer = -1;
if (fIsInput)
fCurrentBuffer = -1;
TRACE(INF, "Alternate %d EP:%x selected for %s!\n",
fActiveAlternate, endpoint->fEndpointAddress,
@ -131,7 +131,6 @@ status_t
Stream::Init()
{
fStatus = _ChooseAlternate();
//if (fStatus != B_OK)
return fStatus;
}
@ -156,10 +155,10 @@ Stream::_SetupBuffers()
// allocate buffer for worst (maximal size) case
TypeIFormatDescriptor* format = static_cast<TypeIFormatDescriptor*>(
fAlternates[fActiveAlternate]->Format());
uint32 samplingRate = fAlternates[fActiveAlternate]->GetSamplingRate();
uint32 sampleSize = format->fNumChannels * format->fSubframeSize;
// data size pro 1 ms USB 1 frame or 1/8 ms USB 2 microframe
fPacketSize = samplingRate * sampleSize
/ (fDevice->fUSBVersion < 0x0200 ? 1000 : 8000);
@ -178,7 +177,7 @@ Stream::_SetupBuffers()
: DRIVER_NAME "_playback_area", (void**)&fDescriptors,
B_ANY_KERNEL_ADDRESS, fAreaSize, B_CONTIGUOUS,
B_READ_AREA | B_WRITE_AREA);
if (fArea < 0) {
TRACE(ERR, "Error of creating %#x - "
"bytes size buffer area:%#010x\n", fAreaSize, fArea);
@ -186,9 +185,6 @@ Stream::_SetupBuffers()
return fStatus;
}
// physical_entry PhysEntry;
// get_memory_map(fDescriptors, fAreaSize, &PhysEntry, 1);
TRACE(INF, "Created area id:%d at addr:%#010x size:%#010lx\n",
fArea, fDescriptors, fAreaSize);
}
@ -196,7 +192,7 @@ Stream::_SetupBuffers()
// descriptors count
fDescriptorsCount = fAreaSize
/ (sizeof(usb_iso_packet_descriptor) + fPacketSize);
// we need same size sub-buffers. round it
fDescriptorsCount /= kSamplesBufferCount;
fDescriptorsCount *= kSamplesBufferCount;
@ -258,11 +254,8 @@ Stream::Start()
{
status_t result = B_BUSY;
if (!fIsRunning) {
//if (!fIsInput) { // TODO: recording
for (size_t i = 0; i < kSamplesBufferCount; i++)
result = _QueueNextTransfer(i, true);
//} else
// result = B_OK;
for (size_t i = 0; i < kSamplesBufferCount; i++)
result = _QueueNextTransfer(i, true);
fIsRunning = result == B_OK;
}
return result;
@ -321,10 +314,10 @@ Stream::_TransferCallback(void* cookie, int32 status, void* data,
if (status == B_CANCELED || stream->fDevice->fRemoved) {
atomic_add(&stream->fInsideNotify, -1);
TRACE(ERR, "Cancelled: c:%p st:%#010x, data:%#010x, len:%d\n",
cookie, status, data, actualLength);
cookie, status, data, actualLength);
return;
}
stream->fCurrentBuffer = (stream->fCurrentBuffer + 1) % kSamplesBufferCount;
stream->_DumpDescriptors();
@ -337,7 +330,7 @@ Stream::_TransferCallback(void* cookie, int32 status, void* data,
release_sem_etc(stream->fDevice->fBuffersReadySem, 1, B_DO_NOT_RESCHEDULE);
TRACE(DTA, "st:%#010x, data:%#010x, len:%d\n", status, data, actualLength);
atomic_add(&stream->fInsideNotify, -1);
}
@ -429,20 +422,17 @@ Stream::SetGlobalFormat(multi_format_info* Format)
if (status != B_OK)
return status;
// set endpoint speed
// set endpoint speed
uint32 samplingRate = fAlternates[fActiveAlternate]->GetSamplingRate();
size_t actualLength = 0;
usb_audio_sampling_freq freq = _ASFormatDescriptor::GetSamFreq(samplingRate);
/* data[0] = 0xFF & samplingRate;
data[1] = 0xFF & samplingRate >> 8;
data[2] = 0xFF & samplingRate >> 16; */
uint8 address = fAlternates[fActiveAlternate]->Endpoint()->fEndpointAddress;
status = gUSBModule->send_request(fDevice->fDevice,
USB_REQTYPE_CLASS | USB_REQTYPE_ENDPOINT_OUT,
USB_AUDIO_SET_CUR, USB_AUDIO_SAMPLING_FREQ_CONTROL << 8,
address, sizeof(freq), &freq, &actualLength);
TRACE(ERR, "set_speed %02x%02x%02x for ep %#x %d: %x\n",
freq.bytes[0], freq.bytes[1], freq.bytes[2],
address, actualLength, status);
@ -527,11 +517,8 @@ Stream::GetBuffers(multi_buffer_list* List)
bool
Stream::ExchangeBuffer(multi_buffer_info* Info)
{
if (fProcessedBuffers <= 0) //{
// looks like somebody else has processed buffers but this stream
// release_sem_etc(fDevice->fBuffersReadySem, 1, B_DO_NOT_RESCHEDULE);
if (fProcessedBuffers <= 0)
return false;
// }
if (fIsInput) {
Info->recorded_real_time = system_time();// TODO fRealTime;

View File

@ -15,9 +15,9 @@
## 0x40 - USB audio control parsing
## default value: 0x01
#trace 0x7F
#trace 0x7F
## logfile [full path to private log file]
## logfile [full path to private log file]
## default path value: /var/log/usb_asix.log
## if disabled - all output goes to syslog