]> code.delx.au - pulseaudio/blobdiff - src/modules/module-device-manager.c
core: Add extended stream API to support compressed formats
[pulseaudio] / src / modules / module-device-manager.c
index 776a687a14209997ba0c8dbbd40a6f13f0ec4563..c28affd1ee07fd85bcf7c781e491e9e69b1d2e3a 100644 (file)
@@ -518,6 +518,9 @@ static void route_sink_input(struct userdata *u, pa_sink_input *si) {
     pa_assert(u);
     pa_assert(u->do_routing);
 
+    if (si->save_sink)
+        return;
+
     /* Skip this if it is already in the process of being moved anyway */
     if (!si->sink)
         return;
@@ -544,7 +547,7 @@ static void route_sink_input(struct userdata *u, pa_sink_input *si) {
         return;
 
     if (si->sink != sink)
-        pa_sink_input_move_to(si, sink, TRUE);
+        pa_sink_input_move_to(si, sink, FALSE);
 }
 
 static pa_hook_result_t route_sink_inputs(struct userdata *u, pa_sink *ignore_sink) {
@@ -573,6 +576,9 @@ static void route_source_output(struct userdata *u, pa_source_output *so) {
     pa_assert(u);
     pa_assert(u->do_routing);
 
+    if (so->save_source)
+        return;
+
     if (so->direct_on_input)
         return;
 
@@ -602,7 +608,7 @@ static void route_source_output(struct userdata *u, pa_source_output *so) {
         return;
 
     if (so->source != source)
-        pa_source_output_move_to(so, source, TRUE);
+        pa_source_output_move_to(so, source, FALSE);
 }
 
 static pa_hook_result_t route_source_outputs(struct userdata *u, pa_source* ignore_source) {
@@ -692,7 +698,7 @@ static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint3
 
         pa_strlcpy(entry.icon, pa_strnull(pa_proplist_gets(sink->proplist, PA_PROP_DEVICE_ICON_NAME)), sizeof(entry.icon));
 
-    } else  if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE) {
+    } else if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE) {
         pa_source *source;
 
         pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE);
@@ -811,22 +817,24 @@ static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_n
         return PA_HOOK_OK;
 
     if (new_data->sink)
-        pa_log_debug("Overriding device for stream, even although it is already set. I am evil that way...");
-
-    if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
-        role_index = get_role_index("none");
-    else
-        role_index = get_role_index(role);
+        pa_log_debug("Not restoring device for stream because already set.");
+    else {
+        if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
+            role_index = get_role_index("none");
+        else
+            role_index = get_role_index(role);
 
-    if (PA_INVALID_INDEX != role_index) {
-        uint32_t device_index;
+        if (PA_INVALID_INDEX != role_index) {
+            uint32_t device_index;
 
-        device_index = u->preferred_sinks[role_index];
-        if (PA_INVALID_INDEX != device_index) {
-            pa_sink *sink;
+            device_index = u->preferred_sinks[role_index];
+            if (PA_INVALID_INDEX != device_index) {
+                pa_sink *sink;
 
-            if ((sink = pa_idxset_get_by_index(u->core->sinks, device_index))) {
-                new_data->sink = sink;
+                if ((sink = pa_idxset_get_by_index(u->core->sinks, device_index))) {
+                    if (!pa_sink_input_new_data_set_sink(new_data, sink, FALSE))
+                        pa_log_debug("Not restoring device for stream because no supported format was found");
+                }
             }
         }
     }
@@ -849,22 +857,24 @@ static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_ou
         return PA_HOOK_OK;
 
     if (new_data->source)
-        pa_log_debug("Overriding device for stream, even although it is already set. I am evil that way...");
-
-    if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
-        role_index = get_role_index("none");
-    else
-        role_index = get_role_index(role);
+        pa_log_debug("Not restoring device for stream because already set.");
+    else {
+        if (!(role = pa_proplist_gets(new_data->proplist, PA_PROP_MEDIA_ROLE)))
+            role_index = get_role_index("none");
+        else
+            role_index = get_role_index(role);
 
-    if (PA_INVALID_INDEX != role_index) {
-        uint32_t device_index;
+        if (PA_INVALID_INDEX != role_index) {
+            uint32_t device_index;
 
-        device_index = u->preferred_sources[role_index];
-        if (PA_INVALID_INDEX != device_index) {
-            pa_source *source;
+            device_index = u->preferred_sources[role_index];
+            if (PA_INVALID_INDEX != device_index) {
+                pa_source *source;
 
-            if ((source = pa_idxset_get_by_index(u->core->sources, device_index))) {
-                new_data->source = source;
+                if ((source = pa_idxset_get_by_index(u->core->sources, device_index))) {
+                    new_data->source = source;
+                    new_data->save_source = FALSE;
+                }
             }
         }
     }
@@ -929,8 +939,6 @@ static pa_hook_result_t source_unlink_hook_callback(pa_core *c, pa_source *sourc
 
 
 static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
-    pa_sink *sink;
-    pa_source *source;
     uint32_t idx;
     char *n;
 
@@ -942,29 +950,31 @@ static void apply_entry(struct userdata *u, const char *name, struct entry *e) {
         return;
 
     if ((n = get_name(name, "sink:"))) {
-        for (sink = pa_idxset_first(u->core->sinks, &idx); sink; sink = pa_idxset_next(u->core->sinks, &idx)) {
-            if (!pa_streq(sink->name, n)) {
+        pa_sink *s;
+        PA_IDXSET_FOREACH(s, u->core->sinks, idx) {
+            if (!pa_streq(s->name, n)) {
                 continue;
             }
 
-            pa_log_info("Setting description for sink %s to '%s'", sink->name, e->description);
-            pa_sink_set_description(sink, e->description);
+            pa_log_info("Setting description for sink %s to '%s'", s->name, e->description);
+            pa_sink_set_description(s, e->description);
         }
         pa_xfree(n);
     }
     else if ((n = get_name(name, "source:"))) {
-        for (source = pa_idxset_first(u->core->sources, &idx); source; source = pa_idxset_next(u->core->sources, &idx)) {
-            if (!pa_streq(source->name, n)) {
+        pa_source *s;
+        PA_IDXSET_FOREACH(s, u->core->sources, idx) {
+            if (!pa_streq(s->name, n)) {
                 continue;
             }
 
-            if (source->monitor_of) {
-                pa_log_warn("Cowardly refusing to set the description for monitor source %s.", source->name);
+            if (s->monitor_of) {
+                pa_log_warn("Cowardly refusing to set the description for monitor source %s.", s->name);
                 continue;
             }
 
-            pa_log_info("Setting description for source %s to '%s'", source->name, e->description);
-            pa_source_set_description(source, e->description);
+            pa_log_info("Setting description for source %s to '%s'", s->name, e->description);
+            pa_source_set_description(s, e->description);
         }
         pa_xfree(n);
     }
@@ -1022,33 +1032,33 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
 
         if ((e = read_entry(u, name))) {
             uint32_t idx;
-            char *devname;
-            pa_bool_t available = FALSE;
+            char *device_name;
+            uint32_t found_index = PA_INVALID_INDEX;
 
-            if ((devname = get_name(name, "sink:"))) {
+            if ((device_name = get_name(name, "sink:"))) {
                 pa_sink* s;
                 PA_IDXSET_FOREACH(s, u->core->sinks, idx) {
-                    if (strcmp(s->name, devname) == 0) {
-                        available = TRUE;
+                    if (strcmp(s->name, device_name) == 0) {
+                        found_index = s->index;
                         break;
                     }
                 }
-                pa_xfree(devname);
-            } else if ((devname = get_name(name, "source:"))) {
+                pa_xfree(device_name);
+            } else if ((device_name = get_name(name, "source:"))) {
                 pa_source* s;
                 PA_IDXSET_FOREACH(s, u->core->sources, idx) {
-                    if (strcmp(s->name, devname) == 0) {
-                        available = TRUE;
+                    if (strcmp(s->name, device_name) == 0) {
+                        found_index = s->index;
                         break;
                     }
                 }
-                pa_xfree(devname);
+                pa_xfree(device_name);
             }
 
             pa_tagstruct_puts(reply, name);
             pa_tagstruct_puts(reply, e->description);
             pa_tagstruct_puts(reply, e->icon);
-            pa_tagstruct_put_boolean(reply, available);
+            pa_tagstruct_putu32(reply, found_index);
             pa_tagstruct_putu32(reply, NUM_ROLES);
 
             for (uint32_t i = ROLE_NONE; i < NUM_ROLES; ++i) {
@@ -1164,7 +1174,7 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
             goto fail;
 
         if (PA_INVALID_INDEX == (role_index = get_role_index(role)))
-           goto fail;
+            goto fail;
 
         /* Cycle through the devices given and make sure they exist */
         h = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
@@ -1199,9 +1209,7 @@ static int extension_cb(pa_native_protocol *p, pa_module *m, pa_native_connectio
             if (first) {
                 first = FALSE;
                 sink_mode = (0 == strncmp("sink:", s, 5));
-            } else if ((sink_mode && 0 != strncmp("sink:", s, 5))
-                       || (!sink_mode && 0 != strncmp("source:", s, 7)))
-            {
+            } else if ((sink_mode && 0 != strncmp("sink:", s, 5)) || (!sink_mode && 0 != strncmp("source:", s, 7))) {
                 while ((device = pa_hashmap_steal_first(h))) {
                     pa_xfree(device->device);
                     pa_xfree(device);
@@ -1379,6 +1387,11 @@ static pa_hook_result_t connection_unlink_hook_cb(pa_native_protocol *p, pa_nati
     return PA_HOOK_OK;
 }
 
+struct prioritised_indexes {
+    uint32_t index;
+    int32_t priority;
+};
+
 int pa__init(pa_module*m) {
     pa_modargs *ma = NULL;
     struct userdata *u;
@@ -1387,6 +1400,7 @@ int pa__init(pa_module*m) {
     pa_source *source;
     uint32_t idx;
     pa_bool_t do_routing = FALSE, on_hotplug = TRUE, on_rescue = TRUE;
+    uint32_t total_devices;
 
     pa_assert(m);
 
@@ -1422,20 +1436,20 @@ int pa__init(pa_module*m) {
     u->source_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_new_hook_callback, u);
 
     /* The following slots are used to deal with routing */
-    /* A little bit later than module-stream-restore, module-intended-roles */
-    u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+15, (pa_hook_cb_t) sink_input_new_hook_callback, u);
-    u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+15, (pa_hook_cb_t) source_output_new_hook_callback, u);
+    /* A little bit later than module-stream-restore, but before module-intended-roles */
+    u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY+5, (pa_hook_cb_t) sink_input_new_hook_callback, u);
+    u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY+5, (pa_hook_cb_t) source_output_new_hook_callback, u);
 
     if (on_hotplug) {
-        /* A little bit later than module-stream-restore, module-intended-roles */
-        u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+15, (pa_hook_cb_t) sink_put_hook_callback, u);
-        u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+15, (pa_hook_cb_t) source_put_hook_callback, u);
+        /* A little bit later than module-stream-restore, but before module-intended-roles */
+        u->sink_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+5, (pa_hook_cb_t) sink_put_hook_callback, u);
+        u->source_put_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE+5, (pa_hook_cb_t) source_put_hook_callback, u);
     }
 
     if (on_rescue) {
-        /* A little bit later than module-stream-restore, module-intended-roles, a little bit earlier than module-rescue-streams, ... */
-        u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+15, (pa_hook_cb_t) sink_unlink_hook_callback, u);
-        u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+15, (pa_hook_cb_t) source_unlink_hook_callback, u);
+        /* A little bit later than module-stream-restore, a little bit earlier than module-intended-roles, module-rescue-streams, ... */
+        u->sink_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_LATE+5, (pa_hook_cb_t) sink_unlink_hook_callback, u);
+        u->source_unlink_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE+5, (pa_hook_cb_t) source_unlink_hook_callback, u);
     }
 
     if (!(fname = pa_state_path("device-manager", TRUE)))
@@ -1447,15 +1461,63 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
-    pa_log_info("Sucessfully opened database file '%s'.", fname);
+    pa_log_info("Successfully opened database file '%s'.", fname);
     pa_xfree(fname);
 
-    /* We cycle over all the available sinks so that they are added to our database if they are not in it yet */
-    PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
-        subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
+    /* Attempt to inject the devices into the list in priority order */
+    total_devices = PA_MAX(pa_idxset_size(m->core->sinks), pa_idxset_size(m->core->sources));
+    if (total_devices > 0 && total_devices < 128) {
+        uint32_t i;
+        struct prioritised_indexes p_i[128];
+
+        /* We cycle over all the available sinks so that they are added to our database if they are not in it yet */
+        i = 0;
+        PA_IDXSET_FOREACH(sink, m->core->sinks, idx) {
+            pa_log_debug("Found sink index %u", sink->index);
+            p_i[i  ].index = sink->index;
+            p_i[i++].priority = sink->priority;
+        }
+        /* Bubble sort it (only really useful for first time creation) */
+        if (i > 1)
+          for (uint32_t j = 0; j < i; ++j)
+              for (uint32_t k = 0; k < i; ++k)
+                  if (p_i[j].priority > p_i[k].priority) {
+                      struct prioritised_indexes tmp_pi = p_i[k];
+                      p_i[k] = p_i[j];
+                      p_i[j] = tmp_pi;
+                  }
+        /* Register it */
+        for (uint32_t j = 0; j < i; ++j)
+            subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, p_i[j].index, u);
+
+
+        /* We cycle over all the available sources so that they are added to our database if they are not in it yet */
+        i = 0;
+        PA_IDXSET_FOREACH(source, m->core->sources, idx) {
+            p_i[i  ].index = source->index;
+            p_i[i++].priority = source->priority;
+        }
+        /* Bubble sort it (only really useful for first time creation) */
+        if (i > 1)
+          for (uint32_t j = 0; j < i; ++j)
+              for (uint32_t k = 0; k < i; ++k)
+                  if (p_i[j].priority > p_i[k].priority) {
+                      struct prioritised_indexes tmp_pi = p_i[k];
+                      p_i[k] = p_i[j];
+                      p_i[j] = tmp_pi;
+                  }
+        /* Register it */
+        for (uint32_t j = 0; j < i; ++j)
+            subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, p_i[j].index, u);
+    }
+    else if (total_devices > 0) {
+        /* This user has a *lot* of devices... */
+        PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
+            subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW, sink->index, u);
 
-    PA_IDXSET_FOREACH(source, m->core->sources, idx)
-        subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
+        PA_IDXSET_FOREACH(source, m->core->sources, idx)
+            subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_NEW, source->index, u);
+    }
 
     /* Perform the routing (if it's enabled) which will update our priority list cache too */
     for (uint32_t i = 0; i < NUM_ROLES; ++i) {
@@ -1478,7 +1540,7 @@ fail:
     if (ma)
         pa_modargs_free(ma);
 
-    return  -1;
+    return -1;
 }
 
 void pa__done(pa_module*m) {