From patchwork Tue Aug 8 22:05:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Baluta X-Patchwork-Id: 9889147 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 ED62D603FF for ; Tue, 8 Aug 2017 22:07:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DEFB528A2A for ; Tue, 8 Aug 2017 22:07:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D3B3128A29; Tue, 8 Aug 2017 22:07:24 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, T_DKIM_INVALID autolearn=no 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 DD44028A32 for ; Tue, 8 Aug 2017 22:07:23 +0000 (UTC) Received: from alsa0.perex.cz (localhost [127.0.0.1]) by alsa0.perex.cz (Postfix) with ESMTP id 5C1C7266E3C; Wed, 9 Aug 2017 00:06:32 +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 A5134266E26; Wed, 9 Aug 2017 00:06:30 +0200 (CEST) Received: from mail-wr0-f194.google.com (mail-wr0-f194.google.com [209.85.128.194]) by alsa0.perex.cz (Postfix) with ESMTP id D2C3C266E26 for ; Wed, 9 Aug 2017 00:06:18 +0200 (CEST) Received: by mail-wr0-f194.google.com with SMTP id y67so3324069wrb.3 for ; Tue, 08 Aug 2017 15:06:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=c07KBuDHTEVlgkaEj+2N2VzUsCteQqrD1Ia1R7Sxl4c=; b=mXN8HKi8ma356ZyJLsV+na1zprrp/g+AR5Pe07HVsKvHUrXRWJT+RlETY7AVuHS/0F M4N4yjSrU/qzsPgqoXebgAKy4ACbQwTfkZqyhV5HX+wKxSqCHrxBRu668e+ifwO0W74F 4HdMYSmF1GGzjO+VxaOf6Bw8U1mhtPZfNTgJRpbE9AN7NClWfMiWBQ0xpTT/dFrv7OOb ZttRqvtdHv0I0N5crYIWMd7YXrLDglF8qx+/szsbBwe7NZnKlQ15vX9vwQq0xQf/tHun DawmCgcYQq3ouWtySbWQXEHIjkvMUQxx8+NXLhZGy7OtuNZ+0sRAvoSWYaLgbHr/dc1U dIjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=c07KBuDHTEVlgkaEj+2N2VzUsCteQqrD1Ia1R7Sxl4c=; b=H8Xu6ubMuioQGaHBj+pACBtRXA45mF2VzWM7EKJ6vhEMU+ahBqugMBquaZW9Ayd8+a q7/Ea0s9TP4DeSV/gD5U78v/u+oiceBuiAhT6Ly9jr3QVXeV0DNYIcO+LJI5g6d05TnJ yELR87/UhVjbA3SBiDt7QO8k070DHsPpVnQaUirMGO3SQpeBRwkW9msWAPai5uBh+9G8 x3HSbKESzZr3CcK1NicI+wykqeDY59xuc5kXtTVMShT8j2672MadumQEYB1YLTuKfmBl Lq+3jG/8SU3bmgBqNpoCDfLdhbzhooM4IDyaAtp3cMW3pJj6/rZAOCF04+21u7rI3N7d 2Auw== X-Gm-Message-State: AHYfb5iIXMMOYAIKapT0uC9DrLB5O6Q5qtRvkQ9wAFR4kmOh1eKgZU1b uGRjWbt73qbYp0YE X-Received: by 10.223.177.2 with SMTP id l2mr4457212wra.65.1502229977981; Tue, 08 Aug 2017 15:06:17 -0700 (PDT) Received: from localhost.localdomain ([2a02:2f01:5040:14c8:152d:3d14:674f:951]) by smtp.gmail.com with ESMTPSA id g18sm3592132wrg.69.2017.08.08.15.06.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 08 Aug 2017 15:06:17 -0700 (PDT) From: Daniel Baluta To: tiwai@suse.com, alsa-devel@alsa-project.org Date: Wed, 9 Aug 2017 01:05:59 +0300 Message-Id: <1502229960-8516-3-git-send-email-daniel.baluta@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1502229960-8516-1-git-send-email-daniel.baluta@gmail.com> References: <1502229960-8516-1-git-send-email-daniel.baluta@gmail.com> Cc: mihai.serban@nxp.com, shengjiu.wang@nxp.com, Daniel Baluta , viorel.suman@nxp.com Subject: [alsa-devel] [PATCH 2/3] aplay: Refactor playback code 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 From: Daniel Baluta This introduces read_header function which tries to read the header of an audio file in order to determine its type. This has the following effects: (1) makes code easier to read (2) don't abort if file size is less than expected header (2), allows us to play small files with size smaller than any supported audio file headers. Suggested-by: Takashi Iwai Signed-off-by: Daniel Baluta --- aplay/aplay.c | 144 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 107 insertions(+), 37 deletions(-) diff --git a/aplay/aplay.c b/aplay/aplay.c index fb2909f..28da6c5 100644 --- a/aplay/aplay.c +++ b/aplay/aplay.c @@ -2801,6 +2801,89 @@ static void playback_go(int fd, size_t loaded, off64_t count, int rtype, char *n snd_pcm_nonblock(handle, nonblock); } +static int read_header(int *loaded, int header_size) +{ + int ret; + struct stat buf; + + ret = fstat(fd, &buf); + if (ret < 0) { + perror("fstat"); + prg_exit(EXIT_FAILURE); + } + + /* don't be adventurous, get out if file size is smaller than + * requested header size */ + if (buf.st_size < header_size) + return -1; + + if (*loaded < header_size) { + header_size -= *loaded; + ret = safe_read(fd, audiobuf + *loaded, header_size); + if (ret != header_size) { + error(_("read error")); + prg_exit(EXIT_FAILURE); + } + *loaded += header_size; + } + return 0; +} + +static int playback_au(char *name, int *loaded) +{ + if (read_header(loaded, sizeof(AuHeader)) < 0) + return -1; + + if (test_au(fd, audiobuf) < 0) + return -1; + + rhwparams.format = hwparams.format; + pbrec_count = calc_count(); + playback_go(fd, *loaded - sizeof(AuHeader), pbrec_count, FORMAT_AU, name); + + return 0; +} + +static int playback_voc(char *name, int *loaded) +{ + int ofs; + + if (read_header(loaded, sizeof(VocHeader)) < 0) + return -1; + + if ((ofs = test_vocfile(audiobuf)) < 0) + return -1; + + pbrec_count = calc_count(); + voc_play(fd, ofs, name); + + return 0; +} + +static int playback_wave(char *name, int *loaded) +{ + ssize_t dtawave; + + if (read_header(loaded, sizeof(WaveHeader)) < 0) + return -1; + + if ((dtawave = test_wavefile(fd, audiobuf, *loaded)) < 0) + return -1; + + pbrec_count = calc_count(); + playback_go(fd, dtawave, pbrec_count, FORMAT_WAVE, name); + + return 0; +} + +static int playback_raw(char *name, int *loaded) +{ + init_raw_data(); + pbrec_count = calc_count(); + playback_go(fd, *loaded, pbrec_count, FORMAT_RAW, name); + + return 0; +} /* * let's play or capture it (capture_type says VOC/WAVE/raw) @@ -2808,9 +2891,7 @@ static void playback_go(int fd, size_t loaded, off64_t count, int rtype, char *n static void playback(char *name) { - int ofs; - size_t dta; - ssize_t dtawave; + int loaded = 0; pbrec_count = LLONG_MAX; fdcount = 0; @@ -2824,40 +2905,29 @@ static void playback(char *name) prg_exit(EXIT_FAILURE); } } - /* read the file header */ - dta = sizeof(AuHeader); - if ((size_t)safe_read(fd, audiobuf, dta) != dta) { - error(_("read error")); - prg_exit(EXIT_FAILURE); - } - if (test_au(fd, audiobuf) >= 0) { - rhwparams.format = hwparams.format; - pbrec_count = calc_count(); - playback_go(fd, 0, pbrec_count, FORMAT_AU, name); - goto __end; - } - dta = sizeof(VocHeader); - if ((size_t)safe_read(fd, audiobuf + sizeof(AuHeader), - dta - sizeof(AuHeader)) != dta - sizeof(AuHeader)) { - error(_("read error")); - prg_exit(EXIT_FAILURE);; - } - if ((ofs = test_vocfile(audiobuf)) >= 0) { - pbrec_count = calc_count(); - voc_play(fd, ofs, name); - goto __end; - } - /* read bytes for WAVE-header */ - if ((dtawave = test_wavefile(fd, audiobuf, dta)) >= 0) { - pbrec_count = calc_count(); - playback_go(fd, dtawave, pbrec_count, FORMAT_WAVE, name); - } else { - /* should be raw data */ - init_raw_data(); - pbrec_count = calc_count(); - playback_go(fd, dta, pbrec_count, FORMAT_RAW, name); - } - __end: + + switch(file_type) { + case FORMAT_AU: + playback_au(name, &loaded); + break; + case FORMAT_VOC: + playback_voc(name, &loaded); + break; + case FORMAT_WAVE: + playback_wave(name, &loaded); + break; + case FORMAT_RAW: + playback_raw(name, &loaded); + break; + default: + /* parse the file header */ + if (playback_au(name, &loaded) < 0 && + playback_voc(name, &loaded) < 0 && + playback_wave(name, &loaded) < 0) + playback_raw(name, &loaded); /* should be raw data */ + break; + } + if (fd != 0) close(fd); }