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-18 14:49:51 +02:00
parent 743d75946f
commit 14f92002e1
7 changed files with 81 additions and 96 deletions
@@ -380,7 +380,7 @@ MixerUnit::MixerUnit(AudioControlInterface* interface,
mixerControlsSize = Mixer->length - 10 - Mixer->num_input_pins; mixerControlsSize = Mixer->length - 10 - Mixer->num_input_pins;
fStringIndex = *(mixerControlsData + mixerControlsSize); fStringIndex = *(mixerControlsData + mixerControlsSize);
#if 1 // TEST #if 0 // TEST
if (fOutChannelsNumber > 2) { if (fOutChannelsNumber > 2) {
// fOutChannelsNumber = 2; // fOutChannelsNumber = 2;
// fChannelsConfig = 0x03; // fChannelsConfig = 0x03;
@@ -959,46 +959,46 @@ AudioControlInterface::Init(size_t interface, usb_interface_info* Interface)
InitACHeader(interface, Header); InitACHeader(interface, Header);
break; break;
case USB_AUDIO_AC_INPUT_TERMINAL: case USB_AUDIO_AC_INPUT_TERMINAL:
control = new InputTerminal(this, Header); control = new(std::nothrow) InputTerminal(this, Header);
break; break;
case USB_AUDIO_AC_OUTPUT_TERMINAL: case USB_AUDIO_AC_OUTPUT_TERMINAL:
control = new OutputTerminal(this, Header); control = new(std::nothrow) OutputTerminal(this, Header);
break; break;
case USB_AUDIO_AC_MIXER_UNIT: case USB_AUDIO_AC_MIXER_UNIT:
control = new MixerUnit(this, Header); control = new(std::nothrow) MixerUnit(this, Header);
break; break;
case USB_AUDIO_AC_SELECTOR_UNIT: case USB_AUDIO_AC_SELECTOR_UNIT:
control = new SelectorUnit(this, Header); control = new(std::nothrow) SelectorUnit(this, Header);
break; break;
case USB_AUDIO_AC_FEATURE_UNIT: case USB_AUDIO_AC_FEATURE_UNIT:
control = new FeatureUnit(this, Header); control = new(std::nothrow) FeatureUnit(this, Header);
break; break;
case USB_AUDIO_AC_PROCESSING_UNIT: case USB_AUDIO_AC_PROCESSING_UNIT:
if (SpecReleaseNumber() < 200) if (SpecReleaseNumber() < 200)
control = new ProcessingUnit(this, Header); control = new(std::nothrow) ProcessingUnit(this, Header);
else else
control = new EffectUnit(this, Header); control = new(std::nothrow) EffectUnit(this, Header);
break; break;
case USB_AUDIO_AC_EXTENSION_UNIT: case USB_AUDIO_AC_EXTENSION_UNIT:
if (SpecReleaseNumber() < 200) if (SpecReleaseNumber() < 200)
control = new ExtensionUnit(this, Header); control = new(std::nothrow) ExtensionUnit(this, Header);
else else
control = new ProcessingUnit(this, Header); control = new(std::nothrow) ProcessingUnit(this, Header);
break; break;
case USB_AUDIO_AC_EXTENSION_UNIT_R2: case USB_AUDIO_AC_EXTENSION_UNIT_R2:
control = new ExtensionUnit(this, Header); control = new(std::nothrow) ExtensionUnit(this, Header);
break; break;
case USB_AUDIO_AC_CLOCK_SOURCE_R2: case USB_AUDIO_AC_CLOCK_SOURCE_R2:
control = new ClockSource(this, Header); control = new(std::nothrow) ClockSource(this, Header);
break; break;
case USB_AUDIO_AC_CLOCK_SELECTOR_R2: case USB_AUDIO_AC_CLOCK_SELECTOR_R2:
control = new ClockSelector(this, Header); control = new(std::nothrow) ClockSelector(this, Header);
break; break;
case USB_AUDIO_AC_CLOCK_MULTIPLIER_R2: case USB_AUDIO_AC_CLOCK_MULTIPLIER_R2:
control = new ClockMultiplier(this, Header); control = new(std::nothrow) ClockMultiplier(this, Header);
break; break;
case USB_AUDIO_AC_SAMPLE_RATE_CONVERTER_R2: case USB_AUDIO_AC_SAMPLE_RATE_CONVERTER_R2:
control = new SampleRateConverter(this, Header); control = new(std::nothrow) SampleRateConverter(this, Header);
break; break;
} }
@@ -1631,12 +1631,12 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
Vector<_MixPageCollector*> mixControls; Vector<_MixPageCollector*> mixControls;
_MixPageCollector* genericPage = new _MixPageCollector("Mixer"); _MixPageCollector* genericPage = new(std::nothrow) _MixPageCollector("Mixer");
mixControls.PushBack(genericPage); mixControls.PushBack(genericPage);
// page for extended in (>2) and out (>2) mixer controls // page for extended in (>2) and out (>2) mixer controls
size_t controlsOnExMixerPage = 0; size_t controlsOnExMixerPage = 0;
_MixPageCollector* exMixerPage = new _MixPageCollector("Mixer"); _MixPageCollector* exMixerPage = new(std::nothrow) _MixPageCollector("Mixer");
AudioChannelCluster* outCluster = mixer->OutCluster(); AudioChannelCluster* outCluster = mixer->OutCluster();
@@ -1740,7 +1740,7 @@ AudioControlInterface::_ListMixControlsForMixerUnit(int32& index,
if (controlsOnExMixerPage >= 6) { if (controlsOnExMixerPage >= 6) {
mixControls.PushBack(exMixerPage); mixControls.PushBack(exMixerPage);
exMixerPage = new _MixPageCollector("Mixer"); exMixerPage = new(std::nothrow) _MixPageCollector("Mixer");
controlsOnExMixerPage = 0; controlsOnExMixerPage = 0;
} }
} }
@@ -1879,13 +1879,13 @@ AudioControlInterface::GetMix(multi_mix_value_info* Info)
case USB_AUDIO_VOLUME_CONTROL: case USB_AUDIO_VOLUME_CONTROL:
length = 2; length = 2;
break; break;
//case 0: // Selector Unit
case USB_AUDIO_MUTE_CONTROL: case USB_AUDIO_MUTE_CONTROL:
case USB_AUDIO_AUTOMATIC_GAIN_CONTROL: case USB_AUDIO_AUTOMATIC_GAIN_CONTROL:
length = 1; length = 1;
break; break;
default: 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)); CS_FROM_CTLID(Info->values[i].id));
continue; continue;
} }
@@ -1897,8 +1897,8 @@ AudioControlInterface::GetMix(multi_mix_value_info* Info)
length = 2; length = 2;
break; break;
default: default:
TRACE(ERR, "Control type %d is not suported\n", TRACE(ERR, "Control id:%08x of type %d is not supported\n",
control->SubType()); ID_FROM_CTLID(Info->values[i].id), control->SubType());
continue; continue;
} }
@@ -2009,7 +2009,8 @@ AudioControlInterface::SetMix(multi_mix_value_info* Info)
Info->values[i].enable); Info->values[i].enable);
break; break;
default: 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)); CS_FROM_CTLID(Info->values[i].id));
continue; continue;
} }
@@ -2032,8 +2033,8 @@ AudioControlInterface::SetMix(multi_mix_value_info* Info)
Info->values[i].gain); Info->values[i].gain);
break; break;
default: default:
TRACE(ERR, "Control type %d is not suported\n", TRACE(ERR, "Control id:%08x of type %d is not supported\n",
control->SubType()); Info->values[i].id, control->SubType());
continue; continue;
} }
@@ -119,18 +119,13 @@ _ASFormatDescriptor::GetSamFreq(const usb_audio_sampling_freq& freq)
return freq.bytes[0] | freq.bytes[1] << 8 | freq.bytes[2] << 16; return freq.bytes[0] | freq.bytes[1] << 8 | freq.bytes[2] << 16;
} }
// TODO: beautify!!!
usb_audio_sampling_freq usb_audio_sampling_freq
_ASFormatDescriptor::GetSamFreq(uint32 samplingRate) _ASFormatDescriptor::GetSamFreq(uint32 samplingRate)
{ {
usb_audio_sampling_freq freq; usb_audio_sampling_freq freq;
for (size_t i = 0; i < 3; i++) for (size_t i = 0; i < 3; i++)
freq.bytes[i] = 0xFF & samplingRate >> 8 * 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; return freq;
} }
@@ -425,14 +420,15 @@ AudioStreamingInterface::AudioStreamingInterface(
switch(Header->descriptor_subtype) { switch(Header->descriptor_subtype) {
case USB_AUDIO_AS_GENERAL: case USB_AUDIO_AS_GENERAL:
if (ASInterface == 0) if (ASInterface == 0)
ASInterface = new ASInterfaceDescriptor( ASInterface = new(std::nothrow)
ASInterfaceDescriptor(
(usb_audio_streaming_interface_descriptor*)Header); (usb_audio_streaming_interface_descriptor*)Header);
else else
TRACE(ERR, "Duplicate AStream interface ignored.\n"); TRACE(ERR, "Duplicate AStream interface ignored.\n");
break; break;
case USB_AUDIO_AS_FORMAT_TYPE: case USB_AUDIO_AS_FORMAT_TYPE:
if (ASFormat == 0) if (ASFormat == 0)
ASFormat = new TypeIFormatDescriptor( ASFormat = new(std::nothrow) TypeIFormatDescriptor(
(usb_audio_format_descriptor*) Header); (usb_audio_format_descriptor*) Header);
else else
TRACE(ERR, "Duplicate AStream format ignored.\n"); TRACE(ERR, "Duplicate AStream format ignored.\n");
@@ -449,7 +445,7 @@ AudioStreamingInterface::AudioStreamingInterface(
if (ASEndpoint == 0) { if (ASEndpoint == 0) {
usb_endpoint_descriptor* Endpoint usb_endpoint_descriptor* Endpoint
= Interface->endpoint[0].descr; = Interface->endpoint[0].descr;
ASEndpoint = new ASEndpointDescriptor(Endpoint, ASEndpoint = new(std::nothrow) ASEndpointDescriptor(Endpoint,
(usb_audio_streaming_endpoint_descriptor*)Header); (usb_audio_streaming_endpoint_descriptor*)Header);
} else } else
TRACE(ERR, "Duplicate AStream endpoint ignored.\n"); TRACE(ERR, "Duplicate AStream endpoint ignored.\n");
@@ -460,7 +456,7 @@ AudioStreamingInterface::AudioStreamingInterface(
"unknown descriptor type %#04x.\n", Header->descriptor_type); "unknown descriptor type %#04x.\n", Header->descriptor_type);
} }
fAlternates.Add(new AudioStreamAlternate(alt, ASInterface, fAlternates.Add(new(std::nothrow) AudioStreamAlternate(alt, ASInterface,
ASEndpoint, ASFormat)); ASEndpoint, ASFormat));
} }
} }
@@ -312,7 +312,7 @@ Device::_MultiGetDescription(multi_description* multiDescription)
for (int i = 0; i < fStreams.Count(); i++) { for (int i = 0; i < fStreams.Count(); i++) {
uint8 id = fStreams[i]->TerminalLink(); uint8 id = fStreams[i]->TerminalLink();
_AudioControl* control = fAudioControl.Find(id); _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) { // if (control->SubType() == USB_AUDIO_AC_INPUT_TERMINAL) {
// USBTerminals.PushFront(control); // USBTerminals.PushFront(control);
// fStreams[i]->GetFormatsAndRates(Description); // fStreams[i]->GetFormatsAndRates(Description);
@@ -320,7 +320,7 @@ Device::_MultiGetDescription(multi_description* multiDescription)
// if (control->SubType() == IDSInputTerminal) { // if (control->SubType() == IDSInputTerminal) {
USBTerminals.PushBack(control); USBTerminals.PushBack(control);
fStreams[i]->GetFormatsAndRates(&Description); fStreams[i]->GetFormatsAndRates(&Description);
//} // }
} }
Vector<multi_channel_info> Channels; Vector<multi_channel_info> Channels;
@@ -619,7 +619,8 @@ Device::_SetupEndpoints()
break; break;
case USB_AUDIO_INTERFACE_AUDIOSTREAMING_SUBCLASS: 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()) { if (B_OK == stream->Init()) {
// put the stream in the correct order: // put the stream in the correct order:
// first output that input ones. // first output that input ones.
@@ -46,7 +46,7 @@ usb_audio_device_added(usb_device device, void** cookie)
} }
// no such device yet, create a new one // no such device yet, create a new one
Device* audioDevice = new Device(device); Device* audioDevice = new(std::nothrow) Device(device);
if (audioDevice == 0) if (audioDevice == 0)
return ENODEV; return ENODEV;
@@ -116,8 +116,8 @@ Stream::_ChooseAlternate()
fIsInput = (endpoint->fEndpointAddress & USB_ENDPOINT_ADDR_DIR_IN) fIsInput = (endpoint->fEndpointAddress & USB_ENDPOINT_ADDR_DIR_IN)
== USB_ENDPOINT_ADDR_DIR_IN; == USB_ENDPOINT_ADDR_DIR_IN;
// if (fIsInput) if (fIsInput)
// fCurrentBuffer = -1; fCurrentBuffer = -1;
TRACE(INF, "Alternate %d EP:%x selected for %s!\n", TRACE(INF, "Alternate %d EP:%x selected for %s!\n",
fActiveAlternate, endpoint->fEndpointAddress, fActiveAlternate, endpoint->fEndpointAddress,
@@ -131,7 +131,6 @@ status_t
Stream::Init() Stream::Init()
{ {
fStatus = _ChooseAlternate(); fStatus = _ChooseAlternate();
//if (fStatus != B_OK)
return fStatus; return fStatus;
} }
@@ -186,9 +185,6 @@ Stream::_SetupBuffers()
return fStatus; return fStatus;
} }
// physical_entry PhysEntry;
// get_memory_map(fDescriptors, fAreaSize, &PhysEntry, 1);
TRACE(INF, "Created area id:%d at addr:%#010x size:%#010lx\n", TRACE(INF, "Created area id:%d at addr:%#010x size:%#010lx\n",
fArea, fDescriptors, fAreaSize); fArea, fDescriptors, fAreaSize);
} }
@@ -258,11 +254,8 @@ Stream::Start()
{ {
status_t result = B_BUSY; status_t result = B_BUSY;
if (!fIsRunning) { if (!fIsRunning) {
//if (!fIsInput) { // TODO: recording for (size_t i = 0; i < kSamplesBufferCount; i++)
for (size_t i = 0; i < kSamplesBufferCount; i++) result = _QueueNextTransfer(i, true);
result = _QueueNextTransfer(i, true);
//} else
// result = B_OK;
fIsRunning = result == B_OK; fIsRunning = result == B_OK;
} }
return result; return result;
@@ -321,7 +314,7 @@ Stream::_TransferCallback(void* cookie, int32 status, void* data,
if (status == B_CANCELED || stream->fDevice->fRemoved) { if (status == B_CANCELED || stream->fDevice->fRemoved) {
atomic_add(&stream->fInsideNotify, -1); atomic_add(&stream->fInsideNotify, -1);
TRACE(ERR, "Cancelled: c:%p st:%#010x, data:%#010x, len:%d\n", TRACE(ERR, "Cancelled: c:%p st:%#010x, data:%#010x, len:%d\n",
cookie, status, data, actualLength); cookie, status, data, actualLength);
return; return;
} }
@@ -433,9 +426,6 @@ Stream::SetGlobalFormat(multi_format_info* Format)
uint32 samplingRate = fAlternates[fActiveAlternate]->GetSamplingRate(); uint32 samplingRate = fAlternates[fActiveAlternate]->GetSamplingRate();
size_t actualLength = 0; size_t actualLength = 0;
usb_audio_sampling_freq freq = _ASFormatDescriptor::GetSamFreq(samplingRate); 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; uint8 address = fAlternates[fActiveAlternate]->Endpoint()->fEndpointAddress;
status = gUSBModule->send_request(fDevice->fDevice, status = gUSBModule->send_request(fDevice->fDevice,
@@ -527,11 +517,8 @@ Stream::GetBuffers(multi_buffer_list* List)
bool bool
Stream::ExchangeBuffer(multi_buffer_info* Info) Stream::ExchangeBuffer(multi_buffer_info* Info)
{ {
if (fProcessedBuffers <= 0) //{ if (fProcessedBuffers <= 0)
// looks like somebody else has processed buffers but this stream
// release_sem_etc(fDevice->fBuffersReadySem, 1, B_DO_NOT_RESCHEDULE);
return false; return false;
// }
if (fIsInput) { if (fIsInput) {
Info->recorded_real_time = system_time();// TODO fRealTime; Info->recorded_real_time = system_time();// TODO fRealTime;