Skip to content

Commit

Permalink
Bug 1023947 - Part 2 - Allow getting the current input device in cube…
Browse files Browse the repository at this point in the history
…b. r=kinetik

--HG--
extra : rebase_source : bc273a2f790b2fa09e8c50c95091f5db9856d42b
  • Loading branch information
padenot committed Jul 18, 2014
1 parent 637f84f commit 751a5dd
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 54 deletions.
8 changes: 4 additions & 4 deletions content/media/AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions layout/media/symbols.def.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions media/libcubeb/include/cubeb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions media/libcubeb/src/cubeb-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 9 additions & 9 deletions media/libcubeb/src/cubeb.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,34 +278,34 @@ 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;
}

// 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;
}

// 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;
Expand Down
4 changes: 2 additions & 2 deletions media/libcubeb/src/cubeb_alsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 2 additions & 2 deletions media/libcubeb/src/cubeb_audiotrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
91 changes: 78 additions & 13 deletions media/libcubeb/src/cubeb_audiounit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -794,27 +820,39 @@ 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,
kAudioDevicePropertyScopeOutput,
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,
Expand All @@ -825,13 +863,39 @@ 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;
}

// 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)->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;
}

Expand All @@ -841,16 +905,17 @@ 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)->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;
}
Expand Down Expand Up @@ -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
};
4 changes: 2 additions & 2 deletions media/libcubeb/src/cubeb_opensl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 2 additions & 2 deletions media/libcubeb/src/cubeb_pulse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 2 additions & 2 deletions media/libcubeb/src/cubeb_sndio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 2 additions & 2 deletions media/libcubeb/src/cubeb_wasapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions media/libcubeb/src/cubeb_winmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

0 comments on commit 751a5dd

Please sign in to comment.