<html>
    <head>
      <base href="https://bugs.freedesktop.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - module-switch-on-port-available will only select unavailable ports when a new sink or source appears"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=100451">100451</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>module-switch-on-port-available will only select unavailable ports when a new sink or source appears
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>PulseAudio
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>medium
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>modules
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>pulseaudio-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>vivek@collabora.co.uk
          </td>
        </tr>

        <tr>
          <th>QA Contact</th>
          <td>pulseaudio-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>lennart@poettering.net
          </td>
        </tr></table>
      <p>
        <div>
        <pre>module-switch-on-port-available - the helper function new_sink_source
iterates over the device ports and picks the one with the highest priority
but then, just before returning, does this:

    if (p->available != PA_AVAILABLE_NO)
        return NULL;

ie the selected port is returned _only_ if it is NOT available.

This logic error appears to be in pulseaudio 10.x also.

This is used by (for example):

  static pa_hook_result_t sink_new_hook_callback(pa_core *c, pa_sink_new_data
*new_data, void *u) {

      pa_device_port *p = new_sink_source(new_data->ports,
new_data->active_port);

      if (p) {
          pa_log_debug("Switching initial port for sink '%s' to '%s'",
new_data->name, p->name);
          pa_sink_new_data_set_port(new_data, p->name);
      }
      return PA_HOOK_OK;
  }

So this module will never choose an available port.

The p->available != PA_AVAILABLE_NO pattern appears all over the place in
this module but I _think_ this is the only place where it's used the
wrong way round.

The following fixed the symptom(s) here (and the pa -vvvv log indicated that
a more sensible set of choices was being made with it applied):

--- a/src/modules/module-switch-on-port-available.c
+++ b/src/modules/module-switch-on-port-available.c
@@ -256,7 +256,7 @@
                 p = i;
     if (!p)
         return NULL;
-    if (p->available != PA_AVAILABLE_NO)
+    if (p->available == PA_AVAILABLE_NO)
         return NULL;

     pa_assert_se(p = find_best_port(ports));</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are the QA Contact for the bug.</li>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>