linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH xawtv3 1/2] alsa: Fix not being able to get a larger latency on capture devices with small max period sizes
@ 2016-02-05 15:17 Hans de Goede
  2016-02-05 15:17 ` [PATCH xawtv3 2/2] xawtv: Allow setting alsa_latency from ~/.xawtv Hans de Goede
  0 siblings, 1 reply; 2+ messages in thread
From: Hans de Goede @ 2016-02-05 15:17 UTC (permalink / raw)
  To: Linux Media Mailing List; +Cc: Hans de Goede

On some capture devices (e.g. bttv cards) the max period size is somewhat
small. Since we only use 2 perios on these devices we end up with only
allowing small latencies, which is a problem when e.g. using a usb codec
as output.

This commit fixes this by adjusting the amount of periods on the capture
side when this happens.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 common/alsa_stream.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/common/alsa_stream.c b/common/alsa_stream.c
index 3e33b5e..1165554 100644
--- a/common/alsa_stream.c
+++ b/common/alsa_stream.c
@@ -109,9 +109,10 @@ static void getparams_periods(snd_pcm_t *handle,
 		      snd_pcm_hw_params_t *params,
 		      unsigned int *usecs,
 		      unsigned int *count,
-		      const char *id)
+		      int allow_adjust, const char *id)
 {
     unsigned min = 0, max = 0;
+    unsigned desired = *usecs * *count;
 
     snd_pcm_hw_params_get_periods_min(params, &min, 0);
     snd_pcm_hw_params_get_periods_max(params, &max, 0);
@@ -137,6 +138,13 @@ static void getparams_periods(snd_pcm_t *handle,
 	if (*usecs > max)
 	    *usecs = max;
     }
+
+    /* If we deviate from the desired size by more then 20% adjust count */
+    if (allow_adjust && (((*usecs * *count) < (desired *  8 / 10)) ||
+                         ((*usecs * *count) > (desired * 12 / 10)))) {
+        *count = (desired + *usecs / 2) / *usecs;
+        getparams_periods(handle, params, usecs, count, 0, id);
+    }
 }
 
 static int setparams_periods(snd_pcm_t *handle,
@@ -351,12 +359,16 @@ static int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle,
     /* Negotiate period parameters */
 
     c_periodtime = latency * 1000 / c_periods;
-    getparams_periods(chandle, c_hwparams, &c_periodtime, &c_periods, "capture");
+    getparams_periods(chandle, c_hwparams, &c_periodtime, &c_periods, 1, "capture");
     p_periods = c_periods * 2;
     p_periodtime = c_periodtime;
-    getparams_periods(phandle, p_hwparams, &p_periodtime, &p_periods, "playback");
+    getparams_periods(phandle, p_hwparams, &p_periodtime, &p_periods, 0, "playback");
     c_periods = p_periods / 2;
 
+    if (verbose)
+	fprintf(error_fp, "alsa: Capture %u periods of %u usecs, Playback %u periods of %u usecs\n",
+		c_periods, c_periodtime, p_periods, p_periodtime);
+
     /*
      * Some playback devices support a very limited periodtime range. If the user needs to
      * use a higher latency to avoid overrun/underrun, use an alternate algorithm of incresing
@@ -365,10 +377,10 @@ static int setparams(snd_pcm_t *phandle, snd_pcm_t *chandle,
     if (p_periodtime < c_periodtime) {
 	c_periodtime = p_periodtime;
 	c_periods = round (latency * 1000.0 / c_periodtime + 0.5);
-	getparams_periods(chandle, c_hwparams, &c_periodtime, &c_periods, "capture");
+	getparams_periods(chandle, c_hwparams, &c_periodtime, &c_periods, 0, "capture");
 	p_periods = c_periods * 2;
 	p_periodtime = c_periodtime;
-	getparams_periods(phandle, p_hwparams, &p_periodtime, &p_periods, "playback");
+	getparams_periods(phandle, p_hwparams, &p_periodtime, &p_periods, 0, "playback");
 	c_periods = p_periods / 2;
     }
 
-- 
2.5.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH xawtv3 2/2] xawtv: Allow setting alsa_latency from ~/.xawtv
  2016-02-05 15:17 [PATCH xawtv3 1/2] alsa: Fix not being able to get a larger latency on capture devices with small max period sizes Hans de Goede
@ 2016-02-05 15:17 ` Hans de Goede
  0 siblings, 0 replies; 2+ messages in thread
From: Hans de Goede @ 2016-02-05 15:17 UTC (permalink / raw)
  To: Linux Media Mailing List; +Cc: Hans de Goede

This avoids the need to always have to specify this on the cmdline on
systems which need a value different from the default.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 man/xawtvrc.5 |  5 +++++
 x11/xt.c      | 16 +++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/man/xawtvrc.5 b/man/xawtvrc.5
index 136ceba..f1c3333 100644
--- a/man/xawtvrc.5
+++ b/man/xawtvrc.5
@@ -209,6 +209,11 @@ details.
 .TP
 .B filter = name
 Enable the specified filter.
+.TP
+.B alsa_latency = time_in_ms
+This can be used to specify the latency for the ALSA digital sound loopback
+which xawtv does. The default is 30ms if you're getting sound dropouts on your
+system try increasing this setting.
 .SS The [launch] section
 You can start other programs from within xawtv.  This is configured
 with entries in the "[launch]" section:
diff --git a/x11/xt.c b/x11/xt.c
index 2065ff4..9affb4a 100644
--- a/x11/xt.c
+++ b/x11/xt.c
@@ -65,6 +65,10 @@
 
 /*----------------------------------------------------------------------*/
 
+#define ALSA_LATENCY_DEFAULT 30
+#define STR(X) __STR(X)
+#define __STR(X) #X
+
 XtAppContext      app_context;
 Widget            app_shell, tv;
 Widget            on_shell;
@@ -167,7 +171,7 @@ XtResource args_desc[] = {
 	"alsa_latency",
 	XtCValue, XtRInt, sizeof(int),
 	XtOffset(struct ARGS*,alsa_latency),
-	XtRString, "30"
+	XtRString, STR(ALSA_LATENCY_DEFAULT)
     },{
 	/* Integer */
 	"debug",
@@ -1414,9 +1418,15 @@ static void x11_mute_notify(int val)
 {
     if (val)
 	alsa_thread_stop();
-    else if (!alsa_thread_is_running())
+    else if (!alsa_thread_is_running()) {
+        if (args.readconfig && args.alsa_latency == ALSA_LATENCY_DEFAULT) {
+            int val = cfg_get_int("global", "alsa_latency");
+            if (val > 0)
+                args.alsa_latency = val;
+        }
 	alsa_thread_startup(alsa_out, alsa_cap, args.alsa_latency,
 			    stderr, debug);
+    }
 }
 #endif
 
@@ -1698,7 +1708,7 @@ usage(void)
 	    "      -(no)alsa       enable/disable alsa streaming. Default: enabled\n"
 	    "      -(no)alsa-cap   manually specify an alsa capture interface\n"
 	    "      -(no)alsa-pb    manually specify an alsa playback interface\n"
-	    "      -alsa-latency   manually specify an alsa latency in ms. Default: 30\n"
+	    "      -alsa-latency   manually specify an alsa latency in ms. Default: " STR(ALSA_LATENCY_DEFAULT) "\n"
 #endif
 	    "  -b  -bpp n          color depth of the display is n (n=24,32)\n"
 	    "  -o  -outfile file   filename base for snapshots\n"
-- 
2.5.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-02-05 15:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-05 15:17 [PATCH xawtv3 1/2] alsa: Fix not being able to get a larger latency on capture devices with small max period sizes Hans de Goede
2016-02-05 15:17 ` [PATCH xawtv3 2/2] xawtv: Allow setting alsa_latency from ~/.xawtv Hans de Goede

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).