]> code.delx.au - pulseaudio/blobdiff - src/modules/module-combine.c
Merge most of elmarco/rtclock2
[pulseaudio] / src / modules / module-combine.c
index cef7a99a22aa9863a4ab1323839d3109ad24b9f4..d50e59aeb1d94047cf211a20dcb5ef3ba74d3ace 100644 (file)
@@ -5,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <errno.h>
 
+#include <pulse/rtclock.h>
 #include <pulse/timeval.h>
 #include <pulse/xmalloc.h>
 
@@ -36,6 +37,7 @@
 #include <pulsecore/sink-input.h>
 #include <pulsecore/memblockq.h>
 #include <pulsecore/log.h>
+#include <pulsecore/core-rtclock.h>
 #include <pulsecore/core-util.h>
 #include <pulsecore/modargs.h>
 #include <pulsecore/namereg.h>
@@ -43,7 +45,6 @@
 #include <pulsecore/thread.h>
 #include <pulsecore/thread-mq.h>
 #include <pulsecore/rtpoll.h>
-#include <pulsecore/rtclock.h>
 #include <pulsecore/core-error.h>
 #include <pulsecore/time-smoother.h>
 
@@ -55,12 +56,13 @@ PA_MODULE_VERSION(PACKAGE_VERSION);
 PA_MODULE_LOAD_ONCE(FALSE);
 PA_MODULE_USAGE(
         "sink_name=<name for the sink> "
+        "sink_properties=<properties for the sink> "
         "slaves=<slave sinks> "
         "adjust_time=<seconds> "
         "resample_method=<method> "
         "format=<sample format> "
-        "channels=<number of channels> "
         "rate=<sample rate> "
+        "channels=<number of channels> "
         "channel_map=<channel map>");
 
 #define DEFAULT_SINK_NAME "combined"
@@ -69,16 +71,17 @@ PA_MODULE_USAGE(
 
 #define DEFAULT_ADJUST_TIME 10
 
-#define REQUEST_LATENCY_USEC (PA_USEC_PER_MSEC * 200)
+#define BLOCK_USEC (PA_USEC_PER_MSEC * 200)
 
 static const char* const valid_modargs[] = {
     "sink_name",
+    "sink_properties",
     "slaves",
     "adjust_time",
     "resample_method",
     "format",
-    "channels",
     "rate",
+    "channels",
     "channel_map",
     NULL
 };
@@ -206,9 +209,9 @@ static void adjust_rates(struct userdata *u) {
             continue;
 
         if (o->total_latency < target_latency)
-            r -= (uint32_t) (((((double) target_latency - o->total_latency))/u->adjust_time)*r/PA_USEC_PER_SEC);
+            r -= (uint32_t) ((((double) (target_latency - o->total_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC);
         else if (o->total_latency > target_latency)
-            r += (uint32_t) (((((double) o->total_latency - target_latency))/u->adjust_time)*r/PA_USEC_PER_SEC);
+            r += (uint32_t) ((((double) (o->total_latency - target_latency))/(double)u->adjust_time)*(double)r/PA_USEC_PER_SEC);
 
         if (r < (uint32_t) (base_rate*0.9) || r > (uint32_t) (base_rate*1.1)) {
             pa_log_warn("[%s] sample rates too different, not adjusting (%u vs. %u).", pa_proplist_gets(o->sink_input->proplist, PA_PROP_MEDIA_NAME), base_rate, r);
@@ -222,9 +225,8 @@ static void adjust_rates(struct userdata *u) {
     pa_asyncmsgq_send(u->sink->asyncmsgq, PA_MSGOBJECT(u->sink), SINK_MESSAGE_UPDATE_LATENCY, NULL, (int64_t) avg_total_latency, NULL);
 }
 
-static void time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
+static void time_callback(pa_mainloop_api *a, pa_time_event *e, const struct timeval *t, void *userdata) {
     struct userdata *u = userdata;
-    struct timeval n;
 
     pa_assert(u);
     pa_assert(a);
@@ -232,9 +234,7 @@ static void time_callback(pa_mainloop_api*a, pa_time_event* e, const struct time
 
     adjust_rates(u);
 
-    pa_gettimeofday(&n);
-    n.tv_sec += u->adjust_time;
-    u->sink->core->mainloop->time_restart(e, &n);
+    pa_core_rttime_restart(u->core, e, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC);
 }
 
 static void process_render_null(struct userdata *u, pa_usec_t now) {
@@ -278,19 +278,22 @@ static void thread_func(void *userdata) {
         pa_make_realtime(u->core->realtime_priority+1);
 
     pa_thread_mq_install(&u->thread_mq);
-    pa_rtpoll_install(u->rtpoll);
 
-    u->thread_info.timestamp = pa_rtclock_usec();
+    u->thread_info.timestamp = pa_rtclock_now();
     u->thread_info.in_null_mode = FALSE;
 
     for (;;) {
         int ret;
 
+        if (PA_SINK_IS_OPENED(u->sink->thread_info.state))
+            if (u->sink->thread_info.rewind_requested)
+                pa_sink_process_rewind(u->sink, 0);
+
         /* If no outputs are connected, render some data and drop it immediately. */
         if (PA_SINK_IS_OPENED(u->sink->thread_info.state) && !u->thread_info.active_outputs) {
             pa_usec_t now;
 
-            now = pa_rtclock_usec();
+            now = pa_rtclock_now();
 
             if (!u->thread_info.in_null_mode || u->thread_info.timestamp <= now)
                 process_render_null(u, now);
@@ -385,7 +388,7 @@ static void request_memblock(struct output *o, size_t length) {
 
     /* OK, we need to prepare new data, but only if the sink is actually running */
     if (pa_atomic_load(&o->userdata->thread_info.running))
-        pa_asyncmsgq_send(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_NEED, o, length, NULL);
+        pa_asyncmsgq_send(o->outq, PA_MSGOBJECT(o->userdata->sink), SINK_MESSAGE_NEED, o, (int64_t) length, NULL);
 }
 
 /* Called from I/O thread context */
@@ -410,7 +413,6 @@ static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
     struct output *o;
 
     pa_sink_input_assert_ref(i);
-    pa_assert(nbytes > 0);
     pa_assert_se(o = i->userdata);
 
     pa_memblockq_rewind(o->memblockq, nbytes);
@@ -484,9 +486,9 @@ static void sink_input_kill_cb(pa_sink_input *i) {
     struct output *o;
 
     pa_sink_input_assert_ref(i);
-    pa_assert(o = i->userdata);
+    pa_assert_se(o = i->userdata);
 
-    pa_module_unload_request(o->userdata->module);
+    pa_module_unload_request(o->userdata->module, TRUE);
     output_free(o);
 }
 
@@ -501,7 +503,7 @@ static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t s
      * we are heard right-away. */
     if (PA_SINK_INPUT_IS_LINKED(state) &&
         i->thread_info.state == PA_SINK_INPUT_INIT)
-        pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
+        pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
 }
 
 /* Called from thread context */
@@ -525,7 +527,7 @@ static int sink_input_process_msg(pa_msgobject *obj, int code, void *data, int64
             if (PA_SINK_IS_OPENED(o->sink_input->sink->thread_info.state))
                 pa_memblockq_push_align(o->memblockq, chunk);
             else
-                pa_memblockq_flush(o->memblockq);
+                pa_memblockq_flush_write(o->memblockq);
 
             return 0;
     }
@@ -555,7 +557,7 @@ static void enable_output(struct output *o) {
 
     if (output_create_sink_input(o) >= 0) {
 
-        pa_memblockq_flush(o->memblockq);
+        pa_memblockq_flush_write(o->memblockq);
 
         pa_sink_input_put(o->sink_input);
 
@@ -588,7 +590,7 @@ static void unsuspend(struct userdata *u) {
     /* Let's resume */
     for (o = pa_idxset_first(u->outputs, &idx); o; o = pa_idxset_next(u->outputs, &idx)) {
 
-        pa_sink_suspend(o->sink, FALSE);
+        pa_sink_suspend(o->sink, FALSE, PA_SUSPEND_IDLE);
 
         if (PA_SINK_IS_OPENED(pa_sink_get_state(o->sink)))
             enable_output(o);
@@ -624,6 +626,7 @@ static int sink_set_state(pa_sink *sink, pa_sink_state_t state) {
 
         case PA_SINK_UNLINKED:
         case PA_SINK_INIT:
+        case PA_SINK_INVALID_STATE:
             ;
     }
 
@@ -645,7 +648,7 @@ static void update_max_request(struct userdata *u) {
     if (max_request <= 0)
         max_request = pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
 
-    pa_sink_set_max_request(u->sink, max_request);
+    pa_sink_set_max_request_within_thread(u->sink, max_request);
 }
 
 /* Called from thread context of the io thread */
@@ -658,16 +661,16 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
             pa_atomic_store(&u->thread_info.running, PA_PTR_TO_UINT(data) == PA_SINK_RUNNING);
 
             if (PA_PTR_TO_UINT(data) == PA_SINK_SUSPENDED)
-                pa_smoother_pause(u->thread_info.smoother, pa_rtclock_usec());
+                pa_smoother_pause(u->thread_info.smoother, pa_rtclock_now());
             else
-                pa_smoother_resume(u->thread_info.smoother, pa_rtclock_usec());
+                pa_smoother_resume(u->thread_info.smoother, pa_rtclock_now(), TRUE);
 
             break;
 
         case PA_SINK_MESSAGE_GET_LATENCY: {
             pa_usec_t x, y, c, *delay = data;
 
-            x = pa_rtclock_usec();
+            x = pa_rtclock_now();
             y = pa_smoother_get(u->thread_info.smoother, x);
 
             c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
@@ -724,7 +727,7 @@ static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offse
         case SINK_MESSAGE_UPDATE_LATENCY: {
             pa_usec_t x, y, latency = (pa_usec_t) offset;
 
-            x = pa_rtclock_usec();
+            x = pa_rtclock_now();
             y = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
 
             if (y > latency)
@@ -795,7 +798,7 @@ static int output_create_sink_input(struct output *o) {
     data.module = o->userdata->module;
     data.resample_method = o->userdata->resample_method;
 
-    o->sink_input = pa_sink_input_new(o->userdata->core, &data, PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE);
+    pa_sink_input_new(&o->sink_input, o->userdata->core, &data, PA_SINK_INPUT_VARIABLE_RATE|PA_SINK_INPUT_DONT_MOVE);
 
     pa_sink_input_new_data_done(&data);
 
@@ -813,7 +816,7 @@ static int output_create_sink_input(struct output *o) {
     o->sink_input->kill = sink_input_kill_cb;
     o->sink_input->userdata = o;
 
-    pa_sink_input_set_requested_latency(o->sink_input, REQUEST_LATENCY_USEC);
+    pa_sink_input_set_requested_latency(o->sink_input, BLOCK_USEC);
 
     return 0;
 }
@@ -867,7 +870,7 @@ static struct output *output_new(struct userdata *u, pa_sink *sink) {
     }
 
     if (PA_SINK_IS_OPENED(state) || state == PA_SINK_INIT) {
-        pa_sink_suspend(sink, FALSE);
+        pa_sink_suspend(sink, FALSE, PA_SUSPEND_IDLE);
 
         if (PA_SINK_IS_OPENED(pa_sink_get_state(sink)))
             if (output_create_sink_input(o) < 0)
@@ -1039,7 +1042,14 @@ int pa__init(pa_module*m) {
     pa_atomic_store(&u->thread_info.running, FALSE);
     u->thread_info.in_null_mode = FALSE;
     u->thread_info.counter = 0;
-    u->thread_info.smoother = pa_smoother_new(PA_USEC_PER_SEC, PA_USEC_PER_SEC*2, TRUE, 10);
+    u->thread_info.smoother = pa_smoother_new(
+            PA_USEC_PER_SEC,
+            PA_USEC_PER_SEC*2,
+            TRUE,
+            TRUE,
+            10,
+            0,
+            FALSE);
 
     if (pa_modargs_get_value_u32(ma, "adjust_time", &u->adjust_time) < 0) {
         pa_log("Failed to parse adjust_time value");
@@ -1048,8 +1058,9 @@ int pa__init(pa_module*m) {
 
     slaves = pa_modargs_get_value(ma, "slaves", NULL);
     u->automatic = !slaves;
-    ss = m->core->default_sample_spec;
 
+    ss = m->core->default_sample_spec;
+    map = m->core->default_channel_map;
     if ((pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0)) {
         pa_log("Invalid sample specification.");
         goto fail;
@@ -1068,6 +1079,12 @@ int pa__init(pa_module*m) {
     if (slaves)
         pa_proplist_sets(data.proplist, "combine.slaves", slaves);
 
+    if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) {
+        pa_log("Invalid properties");
+        pa_sink_new_data_done(&data);
+        goto fail;
+    }
+
     u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY);
     pa_sink_new_data_done(&data);
 
@@ -1083,11 +1100,8 @@ int pa__init(pa_module*m) {
     pa_sink_set_rtpoll(u->sink, u->rtpoll);
     pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
 
-    pa_sink_set_latency_range(u->sink, REQUEST_LATENCY_USEC, REQUEST_LATENCY_USEC);
-    u->block_usec = u->sink->thread_info.max_latency;
-
-    u->sink->thread_info.max_request =
-        pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec);
+    u->block_usec = BLOCK_USEC;
+    pa_sink_set_max_request(u->sink, pa_usec_to_bytes(u->block_usec, &u->sink->sample_spec));
 
     if (!u->automatic) {
         const char*split_state;
@@ -1100,7 +1114,7 @@ int pa__init(pa_module*m) {
         while ((n = pa_split(slaves, ",", &split_state))) {
             pa_sink *slave_sink;
 
-            if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK, TRUE)) || slave_sink == u->sink) {
+            if (!(slave_sink = pa_namereg_get(m->core, n, PA_NAMEREG_SINK)) || slave_sink == u->sink) {
                 pa_log("Invalid slave sink '%s'", n);
                 pa_xfree(n);
                 goto fail;
@@ -1153,12 +1167,8 @@ int pa__init(pa_module*m) {
         if (o->sink_input)
             pa_sink_input_put(o->sink_input);
 
-    if (u->adjust_time > 0) {
-        struct timeval tv;
-        pa_gettimeofday(&tv);
-        tv.tv_sec += u->adjust_time;
-        u->time_event = m->core->mainloop->time_new(m->core->mainloop, &tv, time_callback, u);
-    }
+    if (u->adjust_time > 0)
+        u->time_event = pa_core_rttime_new(m->core, pa_rtclock_now() + u->adjust_time * PA_USEC_PER_SEC, time_callback, u);
 
     pa_modargs_free(ma);