dbus_message_unref(reply);
}
+static int get_device_for_preference(pa_stream_manager *m, stream_parent *sp, stream_direction_t direction, uint32_t device_id, pa_tz_device **device) {
+ pa_assert(m);
+ pa_assert(sp);
+ pa_assert(device);
+
+ if (device_id == 0) {
+ /* get a device of default role from previous device type */
+ if (!(*device = pa_device_manager_get_device(m->dm, sp->preferred_device.types[direction], NULL))) {
+ pa_log_error("could not get device[%s]", sp->preferred_device.types[direction]);
+ return -1;
+ }
+ pa_log_debug("unset preferred device, rollback to type(%s), id(%u)", pa_tz_device_get_type(*device), pa_tz_device_get_id(*device));
+ } else {
+ /* get the device of device id */
+ if (!(*device = pa_device_manager_get_device_by_id(m->dm, device_id))) {
+ pa_log_error("could not get device by id[%u]", device_id);
+ return -1;
+ }
+ pa_log_debug("requested preferred device type(%s), id(%u)", pa_tz_device_get_type(*device), pa_tz_device_get_id(*device));
+ }
+
+ return 0;
+}
+
static void handle_set_stream_preferred_device(DBusConnection *conn, DBusMessage *msg, void *userdata) {
const char *device_direction = NULL;
uint32_t sp_id = 0;
uint32_t stream_index;
pa_hashmap *devices;
void *new_device = NULL;
- ret_msg_t ret = RET_MSG_OK;
+ ret_msg_t ret = RET_MSG_OK;
DBusMessage *reply = NULL;
pa_stream_manager *m = (pa_stream_manager*)userdata;
goto finish;
}
- if (device_id == 0) {
- /* in case of unset */
- if (!sp->preferred_device.types[direction]) {
- pa_log_debug("it is already unset");
- goto finish;
- }
- /* get a device of default role from previous device type */
- if (!(device = pa_device_manager_get_device(m->dm, sp->preferred_device.types[direction], NULL))) {
- pa_log_error("could not get device[%s]", sp->preferred_device.types[direction]);
- ret = RET_MSG_ERROR_DEVICE_NOT_FOUND;
- goto finish;
- }
- pa_log_debug("unset preferred device, rollback to type(%s), id(%u)", pa_tz_device_get_type(device), pa_tz_device_get_id(device));
- } else {
- /* get device role from device id */
- if (!(device = pa_device_manager_get_device_by_id(m->dm, device_id))) {
- pa_log_error("could not get device by id[%u]", device_id);
- ret = RET_MSG_ERROR_DEVICE_NOT_FOUND;
- goto finish;
- }
- pa_log_debug("requested preferred device type(%s), id(%u)", pa_tz_device_get_type(device), pa_tz_device_get_id(device));
+ /* allow only auto routing type */
+ if (sp->route_type != STREAM_ROUTE_TYPE_AUTO &&
+ sp->route_type != STREAM_ROUTE_TYPE_AUTO_LAST_CONNECTED) {
+ pa_log_error("not allowed this route type[%d] of this parent_id[%u]", sp->route_type, sp_id);
+ ret = RET_MSG_ERROR_POLICY;
+ goto finish;
}
- /* only allow built-in device types */
- if (!device_type_is_builtin(device->type)) {
- pa_log_error("This device(id:%u, type:%s) is not built-in type", device_id, device->type);
- ret = RET_MSG_ERROR_POLICY;
+ if (device_id == 0 && !sp->preferred_device.types[direction]) {
+ pa_log_debug("it is already unset");
goto finish;
}
- /* allow only auto routing type */
- if (sp->route_type != STREAM_ROUTE_TYPE_AUTO &&
- sp->route_type != STREAM_ROUTE_TYPE_AUTO_LAST_CONNECTED) {
- pa_log_error("not allowed this route type[%d] of this parent_id[%u]", sp->route_type, sp_id);
+ if (get_device_for_preference(m, sp, direction, device_id, &device) < 0) {
+ ret = RET_MSG_ERROR_DEVICE_NOT_FOUND;
+ goto finish;
+ }
+
+ /* only allow built-in device types */
+ if (!device_type_is_builtin(device->type)) {
+ pa_log_error("This device(id:%u, type:%s) is not built-in type", device_id, device->type);
ret = RET_MSG_ERROR_POLICY;
goto finish;
}
/* get default role of the device */
device_role = pa_tz_device_get_role(device, NULL);
- if (device_id == 0) {
- sp->preferred_device.types[direction] = NULL;
- sp->preferred_device.roles[direction] = NULL;
- } else {
- sp->preferred_device.types[direction] = device->type;
- sp->preferred_device.roles[direction] = device_role;
- }
+ sp->preferred_device.types[direction] = (device_id == 0) ? NULL : device->type;
+ sp->preferred_device.roles[direction] = (device_id == 0) ? NULL : device_role;
pa_log_info("preferred device role is set to [%s] of device type[%s], direction[%s]",
sp->preferred_device.roles[direction], device->type, direction == STREAM_DIRECTION_OUT ? "out" : "in");
- if (direction == STREAM_DIRECTION_OUT) {
- streams = sp->idx_sink_inputs;
- devices = device->playback_devices;
- } else {
- streams = sp->idx_source_outputs;
- devices = device->capture_devices;
- }
+ streams = (direction == STREAM_DIRECTION_OUT) ? sp->idx_sink_inputs : sp->idx_source_outputs;
+ devices = (direction == STREAM_DIRECTION_OUT) ? device->playback_devices : device->capture_devices;
count = pa_idxset_size(streams);
PA_IDXSET_FOREACH(stream, streams, idx) {
stream_index, prev_device_type, prev_device_role, pa_proplist_gets(device_props, PA_PROP_DEVICE_ROLE));
}
}
- if (new_device) {
- if (direction == STREAM_DIRECTION_OUT)
- pa_sink_input_move_to(stream, PA_SINK(new_device), false);
- else
- pa_source_output_move_to(stream, PA_SOURCE(new_device), false);
- }
+ if (new_device)
+ (direction == STREAM_DIRECTION_OUT) ? pa_sink_input_move_to(stream, PA_SINK(new_device), false) :
+ pa_source_output_move_to(stream, PA_SOURCE(new_device), false);
+
if (count == 1)
/* Use PROCESS_COMMAND_CHANGE_ROUTE_BY_STREAM_FOCUS_CHANGED here.
* It's not about the focus change, but this command exactly does what is needed here