From patchwork Thu Aug 31 13:37:08 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: keqiao.zhang@intel.com X-Patchwork-Id: 9932205 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3C29A602F0 for ; Thu, 31 Aug 2017 13:42:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2DFAC28955 for ; Thu, 31 Aug 2017 13:42:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 213F028972; Thu, 31 Aug 2017 13:42:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 397F828955 for ; Thu, 31 Aug 2017 13:42:06 +0000 (UTC) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 2F5072673F5; Thu, 31 Aug 2017 15:40:52 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa0.perex.cz (Postfix, from userid 1000) id 16B792673C4; Thu, 31 Aug 2017 15:40:49 +0200 (CEST) Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by alsa0.perex.cz (Postfix) with ESMTP id 638632673CC for ; Thu, 31 Aug 2017 15:40:43 +0200 (CEST) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 31 Aug 2017 06:40:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos; i="5.41,453,1498546800"; d="scan'208"; a="1212993069" Received: from zkq-optiplex-9020.sh.intel.com ([10.239.13.132]) by fmsmga002.fm.intel.com with ESMTP; 31 Aug 2017 06:40:41 -0700 From: Zhang Keqiao To: alsa-devel@alsa-project.org Date: Thu, 31 Aug 2017 21:37:08 +0800 Message-Id: <20170831133709.25537-4-keqiao.zhang@intel.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20170831133709.25537-1-keqiao.zhang@intel.com> References: <20170831133709.25537-1-keqiao.zhang@intel.com> Cc: keqiao.zhang@intel.com Subject: [alsa-devel] [PATCH v1 3/4] alsabat: add XRUN injections function X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: alsa-devel-bounces@alsa-project.org X-Virus-Scanned: ClamAV using ClamSMTP This patch add XRUN injections function, alsabat can trigger the XRUNs by writing the 'XRUN INJECTION' files. Signed-off-by: Zhang Keqiao --- bat/bat.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- bat/common.h | 6 ++++ 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/bat/bat.c b/bat/bat.c index b12e5d3..20fa926 100644 --- a/bat/bat.c +++ b/bat/bat.c @@ -127,8 +127,81 @@ static void get_sine_frequencies(struct bat *bat, char *freq) } } -/* parse the card and pcm ID of playback device and capture device */ -/* Then we can get the path for specify device to do the XRUN injection */ +static int xrun_injection(struct bat *bat) +{ + char playback_file[MAX_PATH + MAX_FILE]; + char capture_file[MAX_PATH + MAX_FILE]; + char filename[MAX_FILE] = XRUN_INJECTION_FILE; + char playback_filepath[MAX_PATH] = PLAYBACK_XRUN_INJECTION_PATH; + char capture_filepath[MAX_PATH] = CAPTURE_XRUN_INJECTION_PATH; + FILE *fp, *fc; + + if (bat->xrun_playback) { + /* replace the card ID and PCM ID in macro for xrun injection path */ + sprintf(playback_filepath, playback_filepath, bat->playback.card_id, + bat->playback.pcm_id); + sprintf(playback_file, "%s/%s", playback_filepath, filename); + bat->playback.xrun_file = playback_file; + + fp = fopen(playback_file, "w"); + if (fp == NULL) { + fprintf(bat->err, _("Can't open the XRUN injection file for playback: %s\n"), + playback_file); + return -ENOENT; /* No such file or directory */ + } + + /* enable XRUN for playback */ + fputs("1", fp); + fclose(fp); + } + + if (bat->xrun_capture) { + /* replace the card ID and PCM ID in macro for xrun injection path */ + sprintf(capture_filepath, capture_filepath, bat->capture.card_id, + bat->capture.pcm_id); + sprintf(capture_file, "%s/%s", capture_filepath, filename); + + fc = fopen(capture_file, "w"); + if (fc == NULL) { + fprintf(bat->err, _("Can't open the XRUN injection file for capture: %s\n"), + capture_file); + return -ENOENT; /* No such file or directory */ + } + + /* enable XRUN for capture */ + fputs("1", fc); + fclose(fc); + } + + return 0; +} + +static int check_xrun_period(struct bat *bat) +{ + int duration_t; + float duration_p; + char *ptrf; + + duration_t = (bat->frames / bat->rate) * 1000; + duration_p = strtof(bat->xarg, &ptrf); + bat->xrun_period = duration_p; + if (bat->xrun_period < 1 || bat->xrun_period > duration_t) { + fprintf(bat->err, _("Invalid period for xrun injections: (1, %d)\n"), + duration_t); + return -EINVAL; + } + + if (*ptrf == 'p') + bat->xrun_playback = 1; + else if (*ptrf == 'c') + bat->xrun_capture = 1; + else + bat->xrun_playback = bat->xrun_capture = 1; + return 0; +} + +/* parse the card and pcm ID of playback device and capture device, then */ +/* we can get the path for the specify device to do the XRUN injection */ static int parse_card_and_pcm_id(struct bat *bat) { char *tmp1; @@ -187,6 +260,7 @@ static int parse_card_and_pcm_id(struct bat *bat) return -EINVAL; } } + return 0; } @@ -238,6 +312,7 @@ static void test_loopback(struct bat *bat) { pthread_t capture_id, playback_id; int err; + int timeout = 0; int *thread_result_capture, *thread_result_playback; /* start playback */ @@ -261,6 +336,20 @@ static void test_loopback(struct bat *bat) exit(EXIT_FAILURE); } + /* check for xrun injections */ + while (bat->xrun_period) { + /* inject 1 XRUN every set time */ + usleep(bat->xrun_period * 1000); + timeout = timeout + (bat->xrun_period); + err = xrun_injection(bat); + if (err < 0) { + fprintf(bat->err, _("XRUN injections error: %d\n"), err); + exit(EXIT_FAILURE); + } + if (timeout >= ((bat->frames / bat->rate) * 1000)) + break; + } + /* wait for playback to complete */ err = thread_wait_completion(bat, playback_id, &thread_result_playback); if (err != 0) { @@ -625,6 +714,10 @@ static int bat_init(struct bat *bat) } if (bat->xarg) { + err = check_xrun_period(bat); + if (err < 0) + return err; + err = parse_card_and_pcm_id(bat); if (err < 0) return err; diff --git a/bat/common.h b/bat/common.h index 9f71c6c..6eebba0 100644 --- a/bat/common.h +++ b/bat/common.h @@ -15,6 +15,11 @@ #define TEMP_RECORD_FILE_NAME "/tmp/bat.wav.XXXXXX" #define DEFAULT_DEV_NAME "default" +#define PLAYBACK_XRUN_INJECTION_PATH "/proc/asound/card%s/pcm%sp/sub0" +#define CAPTURE_XRUN_INJECTION_PATH "/proc/asound/card%s/pcm%sc/sub0" +#define XRUN_INJECTION_FILE "xrun_injection" +#define MAX_PATH 256 +#define MAX_FILE 128 #define MAX_DEV 15 #define OPT_BASE 300 @@ -170,6 +175,7 @@ struct pcm { char *file; char *pcm_id; char *card_id; + char *xrun_file; enum _bat_op_mode mode; void *(*fct)(struct bat *); };