Bug 1023947 - Part 2 - Allow getting the current input device in cubeb. r=kinetik

--HG--
extra : rebase_source : bc273a2f790b2fa09e8c50c95091f5db9856d42b
This commit is contained in:
Paul Adenot 2014-07-18 19:21:27 +02:00
parent 13eeb6c3a3
commit e51df98a9f
13 changed files with 120 additions and 54 deletions

View File

@ -554,7 +554,7 @@ AudioStream::Init(int32_t aNumChannels, int32_t aRate,
void AudioStream::PanOutputIfNeeded(bool aMicrophoneActive)
{
#ifdef XP_MACOSX
cubeb_output_device* out;
cubeb_device* device;
int rv;
char name[128];
size_t length = sizeof(name);
@ -565,9 +565,9 @@ void AudioStream::PanOutputIfNeeded(bool aMicrophoneActive)
}
if (!strncmp(name, "MacBookPro", 10)) {
if (cubeb_stream_get_current_output_device(mCubebStream, &out) == CUBEB_OK) {
if (cubeb_stream_get_current_evice(mCubebStream, &device) == CUBEB_OK) {
// Check if we are currently outputing sound on external speakers.
if (!strcmp(out->name, "ispk")) {
if (!strcmp(device->name, "ispk")) {
// Pan everything to the right speaker.
if (aMicrophoneActive) {
if (cubeb_stream_set_panning(mCubebStream, 1.0) != CUBEB_OK) {
@ -583,7 +583,7 @@ void AudioStream::PanOutputIfNeeded(bool aMicrophoneActive)
NS_WARNING("Could not pan audio output to the center.");
}
}
cubeb_stream_output_device_destroy(mCubebStream, out);
cubeb_stream_device_destroy(mCubebStream, device);
}
}
#endif

View File

@ -137,8 +137,8 @@ cubeb_stream_stop
cubeb_stream_get_latency
cubeb_stream_set_volume
cubeb_stream_set_panning
cubeb_stream_get_current_output_device
cubeb_stream_output_device_destroy
cubeb_stream_get_current_device
cubeb_stream_device_destroy
cubeb_stream_register_device_changed_callback
th_comment_clear
th_comment_init

View File

@ -131,8 +131,9 @@ typedef struct {
/** Output device description */
typedef struct {
char * name; /**< The name of the output device */
} cubeb_output_device;
char * output_name; /**< The name of the output device */
char * input_name; /**< The name of the input device */
} cubeb_device;
/** Stream states signaled via state_callback. */
typedef enum {
@ -303,18 +304,18 @@ int cubeb_stream_set_panning(cubeb_stream * stream, float panning);
* @return CUBEB_ERROR_INVALID_PARAMETER if either stm, device or count are
* invalid pointers
*/
int cubeb_stream_get_current_output_device(cubeb_stream * stm,
cubeb_output_device ** const device);
int cubeb_stream_get_current_device(cubeb_stream * stm,
cubeb_device ** const device);
/**
* Destroy a cubeb_output_device structure.
* @param stream the stream passed in cubeb_stream_get_current_output_device
* Destroy a cubeb_device structure.
* @param stream the stream passed in cubeb_stream_get_current_device
* @param devices the devices to destroy
* @return CUBEB_OK in case of success
* @return CUBEB_ERROR_INVALID_PARAMETER if devices is an invalid pointer
*/
int cubeb_stream_output_device_destroy(cubeb_stream * stream,
cubeb_output_device * devices);
int cubeb_stream_device_destroy(cubeb_stream * stream,
cubeb_device * devices);
/**
* Set a callback to be notified when the output device changes.

View File

@ -30,10 +30,10 @@ struct cubeb_ops {
int (* stream_get_latency)(cubeb_stream * stream, uint32_t * latency);
int (* stream_set_volume)(cubeb_stream * stream, float volumes);
int (* stream_set_panning)(cubeb_stream * stream, float panning);
int (* stream_get_current_output_device)(cubeb_stream * stream,
cubeb_output_device ** const device);
int (* stream_output_device_destroy)(cubeb_stream * stream,
cubeb_output_device * device);
int (* stream_get_current_device)(cubeb_stream * stream,
cubeb_device ** const device);
int (* stream_device_destroy)(cubeb_stream * stream,
cubeb_device * device);
int (*stream_register_device_changed_callback)(cubeb_stream * stream,
cubeb_device_changed_callback device_changed_callback);

View File

@ -278,8 +278,8 @@ int cubeb_stream_set_panning(cubeb_stream * stream, float panning)
return stream->context->ops->stream_set_panning(stream, panning);
}
int cubeb_stream_get_current_output_device(cubeb_stream * stream,
cubeb_output_device ** const device)
int cubeb_stream_get_current_device(cubeb_stream * stream,
cubeb_device ** const device)
{
if (!stream || !device) {
return CUBEB_ERROR_INVALID_PARAMETER;
@ -287,16 +287,16 @@ int cubeb_stream_get_current_output_device(cubeb_stream * stream,
// If we find an implementation, call the function, it might not be available
// on some platforms.
if (stream->context->ops->stream_get_current_output_device) {
return stream->context->ops->stream_get_current_output_device(stream,
device);
if (stream->context->ops->stream_get_current_device) {
return stream->context->ops->stream_get_current_device(stream,
device);
}
return CUBEB_ERROR;
}
int cubeb_stream_output_device_destroy(cubeb_stream * stream,
cubeb_output_device * device)
int cubeb_stream_device_destroy(cubeb_stream * stream,
cubeb_device * device)
{
if (!stream || !device) {
return CUBEB_ERROR_INVALID_PARAMETER;
@ -304,8 +304,8 @@ int cubeb_stream_output_device_destroy(cubeb_stream * stream,
// If we find an implementation, call the function, it might not be available
// on some platforms.
if (stream->context->ops->stream_output_device_destroy) {
return stream->context->ops->stream_output_device_destroy(stream, device);
if (stream->context->ops->stream_device_destroy) {
return stream->context->ops->stream_device_destroy(stream, device);
}
return CUBEB_ERROR;

View File

@ -1131,7 +1131,7 @@ static struct cubeb_ops const alsa_ops = {
.stream_get_latency = alsa_stream_get_latency,
.stream_set_volume = alsa_stream_set_volume,
.stream_set_panning = alsa_stream_set_panning,
.stream_get_current_output_device = NULL,
.stream_output_device_destroy = NULL,
.stream_get_current_device = NULL,
.stream_device_destroy = NULL,
.stream_register_device_changed_callback = NULL
};

View File

@ -505,7 +505,7 @@ static struct cubeb_ops const audiotrack_ops = {
.stream_get_latency = audiotrack_stream_get_latency,
.stream_set_volume = audiotrack_stream_set_volume,
.stream_set_panning = audiotrack_stream_set_panning,
.stream_get_current_output_device = NULL,
.stream_output_device_destroy = NULL,
.stream_get_current_device = NULL,
.stream_device_destroy = NULL,
.stream_register_device_changed_callback = NULL
};

View File

@ -182,6 +182,32 @@ audiounit_get_output_device_id(AudioDeviceID * device_id)
return CUBEB_OK;
}
static int
audiounit_get_input_device_id(AudioDeviceID * device_id)
{
UInt32 size;
OSStatus r;
AudioObjectPropertyAddress input_device_address = {
kAudioHardwarePropertyDefaultInputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
size = sizeof(*device_id);
r = AudioObjectGetPropertyData(kAudioObjectSystemObject,
&input_device_address,
0,
NULL,
&size,
device_id);
if (r != noErr) {
return CUBEB_ERROR;
}
return CUBEB_OK;
}
static int audiounit_install_device_changed_callback(cubeb_stream * stm);
static int audiounit_uninstall_device_changed_callback();
@ -794,14 +820,15 @@ int audiounit_stream_set_panning(cubeb_stream * stm, float panning)
return CUBEB_OK;
}
int audiounit_stream_get_current_output_device(cubeb_stream * stm,
cubeb_output_device ** const device)
int audiounit_stream_get_current_device(cubeb_stream * stm,
cubeb_device ** const device)
{
OSStatus r;
UInt32 size;
UInt32 data;
char strdata[4];
AudioDeviceID output_device_id;
AudioDeviceID input_device_id;
AudioObjectPropertyAddress datasource_address = {
kAudioDevicePropertyDataSource,
@ -809,12 +836,23 @@ int audiounit_stream_get_current_output_device(cubeb_stream * stm,
kAudioObjectPropertyElementMaster
};
AudioObjectPropertyAddress datasource_address_input = {
kAudioDevicePropertyDataSource,
kAudioDevicePropertyScopeInput,
kAudioObjectPropertyElementMaster
};
*device = NULL;
if (audiounit_get_output_device_id(&output_device_id) != CUBEB_OK) {
return CUBEB_ERROR;
}
*device = malloc(sizeof(cubeb_device));
if (!*device) {
return CUBEB_ERROR;
}
size = sizeof(UInt32);
/* This fails with some USB headset, so simply return an empty string. */
r = AudioObjectGetPropertyData(output_device_id,
@ -825,13 +863,13 @@ int audiounit_stream_get_current_output_device(cubeb_stream * stm,
data = 0;
}
*device = malloc(sizeof(cubeb_output_device));
if (!*device) {
(*device)->output_name = malloc(size + 1);
if (!(*device)->output_name) {
return CUBEB_ERROR;
}
(*device)->name = malloc(size + 1);
if (!(*device)->name) {
(*device)->output_name = malloc(size + 1);
if (!(*device)->output_name) {
return CUBEB_ERROR;
}
@ -841,16 +879,43 @@ int audiounit_stream_get_current_output_device(cubeb_stream * stm,
strdata[2] = (char)(data >> 8);
strdata[3] = (char)(data);
memcpy((*device)->name, strdata, size);
(*device)->name[size] = '\0';
memcpy((*device)->output_name, strdata, size);
(*device)->output_name[size] = '\0';
if (audiounit_get_input_device_id(&input_device_id) != CUBEB_OK) {
return CUBEB_ERROR;
}
size = sizeof(UInt32);
r = AudioObjectGetPropertyData(input_device_id, &datasource_address_input, 0, NULL, &size, &data);
if (r != noErr) {
printf("Error when getting device !\n");
size = 0;
data = 0;
}
(*device)->input_name = malloc(size + 1);
if (!(*device)->input_name) {
return CUBEB_ERROR;
}
// Turn the four chars packed into a uint32 into a string
strdata[0] = (char)(data >> 24);
strdata[1] = (char)(data >> 16);
strdata[2] = (char)(data >> 8);
strdata[3] = (char)(data);
memcpy((*device)->input_name, strdata, size);
(*device)->input_name[size] = '\0';
return CUBEB_OK;
}
int audiounit_stream_output_device_destroy(cubeb_stream * stream,
cubeb_output_device * device)
int audiounit_stream_device_destroy(cubeb_stream * stream,
cubeb_device * device)
{
free(device->name);
free(device->output_name);
free(device->input_name);
free(device);
return CUBEB_OK;
}
@ -880,7 +945,7 @@ static struct cubeb_ops const audiounit_ops = {
.stream_get_latency = audiounit_stream_get_latency,
.stream_set_volume = audiounit_stream_set_volume,
.stream_set_panning = audiounit_stream_set_panning,
.stream_get_current_output_device = audiounit_stream_get_current_output_device,
.stream_output_device_destroy = audiounit_stream_output_device_destroy,
.stream_get_current_device = audiounit_stream_get_current_device,
.stream_device_destroy = audiounit_stream_device_destroy,
.stream_register_device_changed_callback = audiounit_stream_register_device_changed_callback
};

View File

@ -788,7 +788,7 @@ static struct cubeb_ops const opensl_ops = {
.stream_get_latency = opensl_stream_get_latency,
.stream_set_volume = opensl_stream_set_volume,
.stream_set_panning = opensl_stream_set_panning,
.stream_get_current_output_device = NULL,
.stream_output_device_destroy = NULL,
.stream_get_current_device = NULL,
.stream_device_destroy = NULL,
.stream_register_device_changed_callback = NULL
};

View File

@ -749,7 +749,7 @@ static struct cubeb_ops const pulse_ops = {
.stream_get_latency = pulse_stream_get_latency,
.stream_set_volume = pulse_stream_set_volume,
.stream_set_panning = pulse_stream_set_panning,
.stream_get_current_output_device = NULL,
.stream_output_device_destroy = NULL,
.stream_get_current_device = NULL,
.stream_device_destroy = NULL,
.stream_register_device_changed_callback = NULL
};

View File

@ -362,7 +362,7 @@ static struct cubeb_ops const sndio_ops = {
.stream_get_latency = sndio_stream_get_latency,
.stream_set_volume = sndio_stream_set_volume,
.stream_set_panning = sndio_stream_set_panning,
.stream_get_current_output_device = NULL,
.stream_output_device_destroy = NULL,
.stream_get_current_device = NULL,
.stream_device_destroy = NULL,
.stream_register_device_changed_callback = NULL
};

View File

@ -940,8 +940,8 @@ cubeb_ops const wasapi_ops = {
/*.stream_get_latency =*/ wasapi_stream_get_latency,
/*.stream_set_volume =*/ wasapi_stream_set_volume,
/*.stream_set_panning =*/ wasapi_stream_set_panning,
/*.stream_get_current_output_device =*/ NULL,
/*.stream_output_device_destroy =*/ NULL,
/*.stream_get_current_device =*/ NULL,
/*.stream_device_destroy =*/ NULL,
/*.stream_register_device_changed_callback =*/ NULL
};
} // namespace anonymous

View File

@ -715,7 +715,7 @@ static struct cubeb_ops const winmm_ops = {
/*.stream_get_latency = */ winmm_stream_get_latency,
/*.stream_set_volume =*/ winmm_stream_set_volume,
/*.stream_set_panning =*/ winmm_stream_set_panning,
/*.stream_get_current_output_device =*/ NULL,
/*.stream_output_device_destroy =*/ NULL,
/*.stream_get_current_device =*/ NULL,
/*.stream_device_destroy =*/ NULL,
/*.stream_register_device_changed_callback=*/ NULL
};