From patchwork Thu Mar 11 15:15:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 12132035 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6274EC433E0 for ; Thu, 11 Mar 2021 15:56:15 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BE42D64ED1 for ; Thu, 11 Mar 2021 15:56:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BE42D64ED1 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:56378 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lKNfZ-0000rQ-OV for qemu-devel@archiver.kernel.org; Thu, 11 Mar 2021 10:56:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49416) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lKN2D-0003EX-9r for qemu-devel@nongnu.org; Thu, 11 Mar 2021 10:15:33 -0500 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]:40608) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lKN28-0001Pq-HK for qemu-devel@nongnu.org; Thu, 11 Mar 2021 10:15:33 -0500 Received: by mail-pf1-x42f.google.com with SMTP id x7so11235485pfi.7 for ; Thu, 11 Mar 2021 07:15:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=od/ubjSu0OOHMmkpTjRC1G/py4pzQgwR44+oIjfe5mM=; b=jGonowbcqcECP+YxjQ5MESN98M7w1e6Zwag+IiLFceBznq3oSbrPqERkV3X+R4Hmsr elm5h+JAyWJq3Rlmjtpny9UB9n1UwFTVH4ZdPAcZa6cI4pbJkEuhQ0no41OqN21RLxAP JcoT/BG+BJ1CyshQ4xGdimNg8AJlV3dbOb6OIScxYBF+sJgQmkfrQ6RI2Q0rZiX1scsb BxA5vb6VD/lqnDLtgtJPnlONK1mM771pVVN5vzNuo9wN07X85/smGseTz3kdylYv06tr JVVUqcR+T/qhNxBEKPg8WKvUXN/EOY/lPUzvLJLNKQWyNOM7nwiOr38RZ3yjYVtOWTQS Wi8Q== 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:mime-version :content-transfer-encoding; bh=od/ubjSu0OOHMmkpTjRC1G/py4pzQgwR44+oIjfe5mM=; b=ifBc6XpJFuIq0T+93lnux+n+/da6y3t71HOqCSa0jEmAQt3JaqY3BKmQXf4LnwZoZZ PIWC+qlwuw7e+/qx24yDhYlq+QRCK5P4lvjdvTZXXp74HCIbQNWENVX7QLp5GTm/WCXT QMdUryjZfXgmbkBMTqyFZvoosCLS908hwnC93lDvdvCk+CXa4Dw7iUWEYhLBmY6Yoi4e 8OJrFp+grHYTRaL0KsfEZoVyL6maH+A5QDDgr9z+dn8l0AV35bND6ZBjnA2YTGSa9kjg LcQ4zCQzbU+42g2qQD+7pFhYqklj4pwiY8PGR8hDaIcPzUSl2b2016i+NHpwafpfzU2b 7R3w== X-Gm-Message-State: AOAM532Bpr6TebkEKibTH2emCxUe4B5t/pYEyJp/zvYBl5iopst0dq7G yy/JROHFLVI88ECKT0uXUxUFlsQROfuMrw== X-Google-Smtp-Source: ABdhPJwUiyqGZ8TXVNGRorLOBpk4h+PAGfaSyTKR3qOtyjiK1j9nmi+IL3xeBuyJls4Asrxan3wfmQ== X-Received: by 2002:a63:e511:: with SMTP id r17mr7634839pgh.163.1615475726613; Thu, 11 Mar 2021 07:15:26 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c360:8200:a00f:6b19:eaae:a18e]) by smtp.gmail.com with ESMTPSA id j3sm2716732pjf.36.2021.03.11.07.15.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Mar 2021 07:15:26 -0800 (PST) From: Akihiko Odaki To: Subject: [PATCH v2 1/3] coreaudio: Drop support for macOS older than 10.6 Date: Fri, 12 Mar 2021 00:15:10 +0900 Message-Id: <20210311151512.22096-1-akihiko.odaki@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42f; envelope-from=akihiko.odaki@gmail.com; helo=mail-pf1-x42f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Akihiko Odaki , Gerd Hoffmann Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Mac OS X 10.6 was released in 2009. Signed-off-by: Akihiko Odaki Reviewed-by: Peter Maydell --- audio/coreaudio.c | 103 ---------------------------------------------- 1 file changed, 103 deletions(-) diff --git a/audio/coreaudio.c b/audio/coreaudio.c index b7c02e0e516..c5f0b615d64 100644 --- a/audio/coreaudio.c +++ b/audio/coreaudio.c @@ -32,10 +32,6 @@ #define AUDIO_CAP "coreaudio" #include "audio_int.h" -#ifndef MAC_OS_X_VERSION_10_6 -#define MAC_OS_X_VERSION_10_6 1060 -#endif - typedef struct coreaudioVoiceOut { HWVoiceOut hw; pthread_mutex_t mutex; @@ -45,9 +41,6 @@ typedef struct coreaudioVoiceOut { AudioDeviceIOProcID ioprocid; } coreaudioVoiceOut; -#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 -/* The APIs used here only become available from 10.6 */ - static OSStatus coreaudio_get_voice(AudioDeviceID *id) { UInt32 size = sizeof(*id); @@ -169,102 +162,6 @@ static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result) &size, result); } -#else -/* Legacy versions of functions using deprecated APIs */ - -static OSStatus coreaudio_get_voice(AudioDeviceID *id) -{ - UInt32 size = sizeof(*id); - - return AudioHardwareGetProperty( - kAudioHardwarePropertyDefaultOutputDevice, - &size, - id); -} - -static OSStatus coreaudio_get_framesizerange(AudioDeviceID id, - AudioValueRange *framerange) -{ - UInt32 size = sizeof(*framerange); - - return AudioDeviceGetProperty( - id, - 0, - 0, - kAudioDevicePropertyBufferFrameSizeRange, - &size, - framerange); -} - -static OSStatus coreaudio_get_framesize(AudioDeviceID id, UInt32 *framesize) -{ - UInt32 size = sizeof(*framesize); - - return AudioDeviceGetProperty( - id, - 0, - false, - kAudioDevicePropertyBufferFrameSize, - &size, - framesize); -} - -static OSStatus coreaudio_set_framesize(AudioDeviceID id, UInt32 *framesize) -{ - UInt32 size = sizeof(*framesize); - - return AudioDeviceSetProperty( - id, - NULL, - 0, - false, - kAudioDevicePropertyBufferFrameSize, - size, - framesize); -} - -static OSStatus coreaudio_get_streamformat(AudioDeviceID id, - AudioStreamBasicDescription *d) -{ - UInt32 size = sizeof(*d); - - return AudioDeviceGetProperty( - id, - 0, - false, - kAudioDevicePropertyStreamFormat, - &size, - d); -} - -static OSStatus coreaudio_set_streamformat(AudioDeviceID id, - AudioStreamBasicDescription *d) -{ - UInt32 size = sizeof(*d); - - return AudioDeviceSetProperty( - id, - 0, - 0, - 0, - kAudioDevicePropertyStreamFormat, - size, - d); -} - -static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result) -{ - UInt32 size = sizeof(*result); - - return AudioDeviceGetProperty( - id, - 0, - 0, - kAudioDevicePropertyDeviceIsRunning, - &size, - result); -} -#endif static void coreaudio_logstatus (OSStatus status) { From patchwork Thu Mar 11 15:15:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 12132041 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21166C433E0 for ; Thu, 11 Mar 2021 15:58:31 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 97E8D64FDE for ; Thu, 11 Mar 2021 15:58:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 97E8D64FDE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:35738 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lKNhl-0004Bs-K3 for qemu-devel@archiver.kernel.org; Thu, 11 Mar 2021 10:58:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49444) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lKN2E-0003HP-FW for qemu-devel@nongnu.org; Thu, 11 Mar 2021 10:15:34 -0500 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]:39183) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lKN2C-0001Qf-7D for qemu-devel@nongnu.org; Thu, 11 Mar 2021 10:15:34 -0500 Received: by mail-pl1-x62d.google.com with SMTP id j6so10339977plx.6 for ; Thu, 11 Mar 2021 07:15:31 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=zq2ofas2Lo2xsLPCq2xe7AqeSOMcLrLc5dBPspvq3x0=; b=Y13KqT0C/loY+f56a1wkE6eCS9dqCmTCKUOPDNoEEOJAoP7w48tNlT2pzDq2TGo0KG NTsxL+yG0rI/gffwj0xT0rXBej3gbzIWOAWiwcMsWxT8G80OUp21I/k3RGLeiYUy1cQL zkr502TNWdeBqdgKhBVJGQCSCTqBbXfmTIM4QvLxUe/3M/FubGUzr8yFDTObgSJeki/k wK/ZIYJk3dz8CBE04/x/vPxm9Z1ogZ/DQ6i8um3JWvwV3PSNjybDP44WJ3wzkleNf60n wMZsGUS5iRjnJHuPZ49hvFYTiERQw9ou0UtemDOJySnAYRaE69p38wPpyYzr7xqbpQv9 shtg== 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:mime-version:content-transfer-encoding; bh=zq2ofas2Lo2xsLPCq2xe7AqeSOMcLrLc5dBPspvq3x0=; b=czi5wbVk9T8crjRPKWLeO5tmHb9kFtirJNyHGk+pfiO65o/CZzQh9eNizykhZBWKxD p8iuJtvIe9QmyNee3ZC6oq/MURHtW/6o7pNr7NN6Y2ZEAk1BuILvmKhGtplLbEqIbA/D APJBauyALsZi1+MR8jQThsBkfj0abA/crcFAD6wqagVUGH4JXyGEZUiFCJ8oFs0KnNWh mya92PZgJFgRXktw7ehrL9B7BLLtOmsAzQ/KgsyyTJ/dI45UfnSd5DmBAPXWA66eW80y rZc+L3MCL+5Lr4WRfrkV1aNy16jpIaJC9MXfCHC6aYLb9iaKOh97ReuRhz3VApeFH0CI R3vQ== X-Gm-Message-State: AOAM532A6OmoW3lP+zjfVcUfT4e/OiH/g6C9EegqkRNDoH/ro8y4YoHs dBGjnkbeHbQx7z3KUzX9Zk8XI15WfNgOyg== X-Google-Smtp-Source: ABdhPJy6xHdaW6CWfMhSksoIThqoAcSJpqWnCVNlKcmYkw6WWK5DnC9DGBK5s5D6YciCHR4vyOi8wA== X-Received: by 2002:a17:90a:bb95:: with SMTP id v21mr9136391pjr.30.1615475729929; Thu, 11 Mar 2021 07:15:29 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c360:8200:a00f:6b19:eaae:a18e]) by smtp.gmail.com with ESMTPSA id j3sm2716732pjf.36.2021.03.11.07.15.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Mar 2021 07:15:29 -0800 (PST) From: Akihiko Odaki To: Subject: [PATCH v2 2/3] coreaudio: Extract device operations Date: Fri, 12 Mar 2021 00:15:11 +0900 Message-Id: <20210311151512.22096-2-akihiko.odaki@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210311151512.22096-1-akihiko.odaki@gmail.com> References: <20210311151512.22096-1-akihiko.odaki@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=akihiko.odaki@gmail.com; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Akihiko Odaki , Gerd Hoffmann Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" This change prepare to support dynamic device changes, which requires to perform device initialization/deinitialization multiple times. Signed-off-by: Akihiko Odaki --- audio/coreaudio.c | 137 +++++++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 57 deletions(-) diff --git a/audio/coreaudio.c b/audio/coreaudio.c index c5f0b615d64..ab43cdcac19 100644 --- a/audio/coreaudio.c +++ b/audio/coreaudio.c @@ -36,6 +36,8 @@ typedef struct coreaudioVoiceOut { HWVoiceOut hw; pthread_mutex_t mutex; AudioDeviceID outputDeviceID; + int frameSizeSetting; + uint32_t bufferCount; UInt32 audioDevicePropertyBufferFrameSize; AudioStreamBasicDescription outputStreamBasicDescription; AudioDeviceIOProcID ioprocid; @@ -253,6 +255,9 @@ static void GCC_FMT_ATTR (3, 4) coreaudio_logerr2 ( coreaudio_logstatus (status); } +#define coreaudio_playback_logerr(status, ...) \ + coreaudio_logerr2(status, "playback", __VA_ARGS__) + static inline UInt32 isPlaying (AudioDeviceID outputDeviceID) { OSStatus status; @@ -368,126 +373,100 @@ static OSStatus audioDeviceIOProc( return 0; } -static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as, - void *drv_opaque) +static OSStatus init_out_device(coreaudioVoiceOut *core) { OSStatus status; - coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; - int err; - const char *typ = "playback"; AudioValueRange frameRange; - Audiodev *dev = drv_opaque; - AudiodevCoreaudioPerDirectionOptions *cpdo = dev->u.coreaudio.out; - int frames; - struct audsettings obt_as; - - /* create mutex */ - err = pthread_mutex_init(&core->mutex, NULL); - if (err) { - dolog("Could not create mutex\nReason: %s\n", strerror (err)); - return -1; - } - - obt_as = *as; - as = &obt_as; - as->fmt = AUDIO_FORMAT_F32; - audio_pcm_init_info (&hw->info, as); status = coreaudio_get_voice(&core->outputDeviceID); if (status != kAudioHardwareNoError) { - coreaudio_logerr2 (status, typ, - "Could not get default output Device\n"); - return -1; + coreaudio_playback_logerr (status, + "Could not get default output Device\n"); + return status; } if (core->outputDeviceID == kAudioDeviceUnknown) { - dolog ("Could not initialize %s - Unknown Audiodevice\n", typ); - return -1; + dolog ("Could not initialize playback - Unknown Audiodevice\n"); + return status; } /* get minimum and maximum buffer frame sizes */ status = coreaudio_get_framesizerange(core->outputDeviceID, &frameRange); if (status != kAudioHardwareNoError) { - coreaudio_logerr2 (status, typ, - "Could not get device buffer frame range\n"); - return -1; + coreaudio_playback_logerr (status, + "Could not get device buffer frame range\n"); + return status; } - frames = audio_buffer_frames( - qapi_AudiodevCoreaudioPerDirectionOptions_base(cpdo), as, 11610); - if (frameRange.mMinimum > frames) { + if (frameRange.mMinimum > core->frameSizeSetting) { core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum; dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum); - } else if (frameRange.mMaximum < frames) { + } else if (frameRange.mMaximum < core->frameSizeSetting) { core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum; dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum); } else { - core->audioDevicePropertyBufferFrameSize = frames; + core->audioDevicePropertyBufferFrameSize = core->frameSizeSetting; } /* set Buffer Frame Size */ status = coreaudio_set_framesize(core->outputDeviceID, &core->audioDevicePropertyBufferFrameSize); if (status != kAudioHardwareNoError) { - coreaudio_logerr2 (status, typ, - "Could not set device buffer frame size %" PRIu32 "\n", - (uint32_t)core->audioDevicePropertyBufferFrameSize); - return -1; + coreaudio_playback_logerr (status, + "Could not set device buffer frame size %" PRIu32 "\n", + (uint32_t)core->audioDevicePropertyBufferFrameSize); + return status; } /* get Buffer Frame Size */ status = coreaudio_get_framesize(core->outputDeviceID, &core->audioDevicePropertyBufferFrameSize); if (status != kAudioHardwareNoError) { - coreaudio_logerr2 (status, typ, - "Could not get device buffer frame size\n"); - return -1; + coreaudio_playback_logerr (status, + "Could not get device buffer frame size\n"); + return status; } - hw->samples = (cpdo->has_buffer_count ? cpdo->buffer_count : 4) * - core->audioDevicePropertyBufferFrameSize; + core->hw.samples = core->bufferCount * core->audioDevicePropertyBufferFrameSize; /* get StreamFormat */ status = coreaudio_get_streamformat(core->outputDeviceID, &core->outputStreamBasicDescription); if (status != kAudioHardwareNoError) { - coreaudio_logerr2 (status, typ, - "Could not get Device Stream properties\n"); + coreaudio_playback_logerr (status, + "Could not get Device Stream properties\n"); core->outputDeviceID = kAudioDeviceUnknown; - return -1; + return status; } /* set Samplerate */ - core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq; - status = coreaudio_set_streamformat(core->outputDeviceID, &core->outputStreamBasicDescription); if (status != kAudioHardwareNoError) { - coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n", - as->freq); + coreaudio_playback_logerr (status, + "Could not set samplerate %lf\n", + core->outputStreamBasicDescription.mSampleRate); core->outputDeviceID = kAudioDeviceUnknown; - return -1; + return status; } /* set Callback */ core->ioprocid = NULL; status = AudioDeviceCreateIOProcID(core->outputDeviceID, audioDeviceIOProc, - hw, + &core->hw, &core->ioprocid); if (status != kAudioHardwareNoError || core->ioprocid == NULL) { - coreaudio_logerr2 (status, typ, "Could not set IOProc\n"); + coreaudio_playback_logerr (status, "Could not set IOProc\n"); core->outputDeviceID = kAudioDeviceUnknown; - return -1; + return status; } return 0; } -static void coreaudio_fini_out (HWVoiceOut *hw) +static void fini_out_device(coreaudioVoiceOut *core) { OSStatus status; - int err; - coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; /* stop playback */ if (isPlaying(core->outputDeviceID)) { @@ -504,6 +483,50 @@ static void coreaudio_fini_out (HWVoiceOut *hw) coreaudio_logerr(status, "Could not remove IOProc\n"); } core->outputDeviceID = kAudioDeviceUnknown; +} + +static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as, + void *drv_opaque) +{ + OSStatus status; + coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; + int err; + Audiodev *dev = drv_opaque; + AudiodevCoreaudioPerDirectionOptions *cpdo = dev->u.coreaudio.out; + struct audsettings obt_as; + + /* create mutex */ + err = pthread_mutex_init(&core->mutex, NULL); + if (err) { + dolog("Could not create mutex\nReason: %s\n", strerror (err)); + return -1; + } + + obt_as = *as; + as = &obt_as; + as->fmt = AUDIO_FORMAT_F32; + audio_pcm_init_info (&hw->info, as); + + core->frameSizeSetting = audio_buffer_frames( + qapi_AudiodevCoreaudioPerDirectionOptions_base(cpdo), as, 11610); + + core->bufferCount = cpdo->has_buffer_count ? cpdo->buffer_count : 4; + core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq; + + if (init_out_device(core)) { + return -1; + } + + return 0; +} + +static void coreaudio_fini_out (HWVoiceOut *hw) +{ + OSStatus status; + int err; + coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; + + fini_out_device(core); /* destroy mutex */ err = pthread_mutex_destroy(&core->mutex); From patchwork Thu Mar 11 15:15:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akihiko Odaki X-Patchwork-Id: 12132045 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61654C433E0 for ; Thu, 11 Mar 2021 16:00:14 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C236364FDE for ; Thu, 11 Mar 2021 16:00:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C236364FDE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:43488 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lKNjQ-0007TZ-9v for qemu-devel@archiver.kernel.org; Thu, 11 Mar 2021 11:00:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49464) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lKN2G-0003N5-Qh for qemu-devel@nongnu.org; Thu, 11 Mar 2021 10:15:36 -0500 Received: from mail-pg1-x52a.google.com ([2607:f8b0:4864:20::52a]:36675) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lKN2E-0001Sm-J8 for qemu-devel@nongnu.org; Thu, 11 Mar 2021 10:15:36 -0500 Received: by mail-pg1-x52a.google.com with SMTP id t26so13865240pgv.3 for ; Thu, 11 Mar 2021 07:15:34 -0800 (PST) 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 :mime-version:content-transfer-encoding; bh=asBR0bpNmjMs/IsTcbtrudJ5e+yviIyUB7a0tx6uDJA=; b=LwBBTPcCWc0y/N23OwOoILpnkSvtMHABW5EvWwRIQ54N9uSs860yGow4nepaXDF3a3 eTSA3V0iPP8GaYHOfMmW8wfBs29odCfB3x2h7odm7ks0gsupEKtVLsFUA91G1460c5us 65cttgMb6nlBGwJNNyBZeOr9ec1xS5nhCxe8JTk4w16R07K0NpgirdPsFlRxIf0yP0Mi a8dDOTQE0ntwL/TwiVfz+svsTcFdbw1Cau/OCxM8qFv0Dg/T0NYi8Jn+SuSpCkPMCACA NBM5k/b1SXLxfYcLIn8DrobMbykBWZi3rN9I6G0yelYP1QybFdcg4m8pyyKPTeY+2VQj LF4w== 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:mime-version:content-transfer-encoding; bh=asBR0bpNmjMs/IsTcbtrudJ5e+yviIyUB7a0tx6uDJA=; b=i9Pb1iWUmQNAAqmdP0wsjecbY4L8wxnG1MzfF63dusCNfzz/Kynm12I5me/2AQF58Y oKvsLIn8Vo0w60ViF4cRVBP+UsPddUHWgOgRhvik4gFLHsA7CHsaxcG5yNfLMW141sSW diw+nmUaswL/Ld4dh4u1Xu3LBJMOegjm2La+3oIuLd732Xldf/0X7RU9pbvQe+n6J3Jp W8dYRUGApBRvD1Ggzz5GWvqcpK60y+yoyY6+dFR+vsAl4gIlZ23MGrnu4rQBHuoMPb6r 8qzzTXgxDM/VY0oOQxayz/vj0IgCXxlLZccwaugbGmaGGpCIoO4BMGIshwwVnHvwKi9e 1f2g== X-Gm-Message-State: AOAM531En/9iW6UoxzRMDfkg6MusnIxbdHQxjgmNmRMqF1OX8sC3mdhL +KnUJxRioFEpS4mU6nV44MTPmc0QHRKLyA== X-Google-Smtp-Source: ABdhPJxRqOv2BiiJbcI68U3c1bO43ejh5Cfe8jSF1yK5lHYcUwgCqi4O96za4+SGH60jySvsUC3hIQ== X-Received: by 2002:a63:c10:: with SMTP id b16mr7932891pgl.326.1615475732927; Thu, 11 Mar 2021 07:15:32 -0800 (PST) Received: from localhost.localdomain ([2400:4050:c360:8200:a00f:6b19:eaae:a18e]) by smtp.gmail.com with ESMTPSA id j3sm2716732pjf.36.2021.03.11.07.15.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Mar 2021 07:15:32 -0800 (PST) From: Akihiko Odaki To: Subject: [PATCH v2 3/3] coreaudio: Handle output device change Date: Fri, 12 Mar 2021 00:15:12 +0900 Message-Id: <20210311151512.22096-3-akihiko.odaki@gmail.com> X-Mailer: git-send-email 2.24.3 (Apple Git-128) In-Reply-To: <20210311151512.22096-1-akihiko.odaki@gmail.com> References: <20210311151512.22096-1-akihiko.odaki@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52a; envelope-from=akihiko.odaki@gmail.com; helo=mail-pg1-x52a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-devel@nongnu.org, Akihiko Odaki , Gerd Hoffmann Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" An output device change can occur when plugging or unplugging an earphone. Signed-off-by: Akihiko Odaki --- audio/coreaudio.c | 206 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 164 insertions(+), 42 deletions(-) diff --git a/audio/coreaudio.c b/audio/coreaudio.c index ab43cdcac19..578ec9b8b2e 100644 --- a/audio/coreaudio.c +++ b/audio/coreaudio.c @@ -41,19 +41,21 @@ typedef struct coreaudioVoiceOut { UInt32 audioDevicePropertyBufferFrameSize; AudioStreamBasicDescription outputStreamBasicDescription; AudioDeviceIOProcID ioprocid; + bool enabled; } coreaudioVoiceOut; +static const AudioObjectPropertyAddress voice_addr = { + kAudioHardwarePropertyDefaultOutputDevice, + kAudioObjectPropertyScopeGlobal, + kAudioObjectPropertyElementMaster +}; + static OSStatus coreaudio_get_voice(AudioDeviceID *id) { UInt32 size = sizeof(*id); - AudioObjectPropertyAddress addr = { - kAudioHardwarePropertyDefaultOutputDevice, - kAudioObjectPropertyScopeGlobal, - kAudioObjectPropertyElementMaster - }; return AudioObjectGetPropertyData(kAudioObjectSystemObject, - &addr, + &voice_addr, 0, NULL, &size, @@ -258,18 +260,6 @@ static void GCC_FMT_ATTR (3, 4) coreaudio_logerr2 ( #define coreaudio_playback_logerr(status, ...) \ coreaudio_logerr2(status, "playback", __VA_ARGS__) -static inline UInt32 isPlaying (AudioDeviceID outputDeviceID) -{ - OSStatus status; - UInt32 result = 0; - status = coreaudio_get_isrunning(outputDeviceID, &result); - if (status != kAudioHardwareNoError) { - coreaudio_logerr(status, - "Could not determine whether Device is playing\n"); - } - return result; -} - static int coreaudio_lock (coreaudioVoiceOut *core, const char *fn_name) { int err; @@ -341,6 +331,11 @@ static OSStatus audioDeviceIOProc( return 0; } + if (inDevice != core->outputDeviceID) { + coreaudio_unlock (core, "audioDeviceIOProc(old device)"); + return 0; + } + frameCount = core->audioDevicePropertyBufferFrameSize; pending_frames = hw->pending_emul / hw->info.bytes_per_frame; @@ -392,6 +387,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) /* get minimum and maximum buffer frame sizes */ status = coreaudio_get_framesizerange(core->outputDeviceID, &frameRange); + if (status == kAudioHardwareBadObjectError) { + return 0; + } if (status != kAudioHardwareNoError) { coreaudio_playback_logerr (status, "Could not get device buffer frame range\n"); @@ -411,6 +409,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) /* set Buffer Frame Size */ status = coreaudio_set_framesize(core->outputDeviceID, &core->audioDevicePropertyBufferFrameSize); + if (status == kAudioHardwareBadObjectError) { + return 0; + } if (status != kAudioHardwareNoError) { coreaudio_playback_logerr (status, "Could not set device buffer frame size %" PRIu32 "\n", @@ -421,6 +422,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) /* get Buffer Frame Size */ status = coreaudio_get_framesize(core->outputDeviceID, &core->audioDevicePropertyBufferFrameSize); + if (status == kAudioHardwareBadObjectError) { + return 0; + } if (status != kAudioHardwareNoError) { coreaudio_playback_logerr (status, "Could not get device buffer frame size\n"); @@ -431,6 +435,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) /* get StreamFormat */ status = coreaudio_get_streamformat(core->outputDeviceID, &core->outputStreamBasicDescription); + if (status == kAudioHardwareBadObjectError) { + return 0; + } if (status != kAudioHardwareNoError) { coreaudio_playback_logerr (status, "Could not get Device Stream properties\n"); @@ -441,6 +448,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) /* set Samplerate */ status = coreaudio_set_streamformat(core->outputDeviceID, &core->outputStreamBasicDescription); + if (status == kAudioHardwareBadObjectError) { + return 0; + } if (status != kAudioHardwareNoError) { coreaudio_playback_logerr (status, "Could not set samplerate %lf\n", @@ -455,6 +465,9 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) audioDeviceIOProc, &core->hw, &core->ioprocid); + if (status == kAudioHardwareBadDeviceError) { + return 0; + } if (status != kAudioHardwareNoError || core->ioprocid == NULL) { coreaudio_playback_logerr (status, "Could not set IOProc\n"); core->outputDeviceID = kAudioDeviceUnknown; @@ -467,24 +480,94 @@ static OSStatus init_out_device(coreaudioVoiceOut *core) static void fini_out_device(coreaudioVoiceOut *core) { OSStatus status; + UInt32 isrunning; /* stop playback */ - if (isPlaying(core->outputDeviceID)) { - status = AudioDeviceStop(core->outputDeviceID, core->ioprocid); + status = coreaudio_get_isrunning(core->outputDeviceID, &isrunning); + if (status != kAudioHardwareBadObjectError) { if (status != kAudioHardwareNoError) { - coreaudio_logerr(status, "Could not stop playback\n"); + coreaudio_logerr(status, + "Could not determine whether Device is playing\n"); + } + + if (isrunning) { + status = AudioDeviceStop(core->outputDeviceID, core->ioprocid); + if (status != kAudioHardwareBadDeviceError && status != kAudioHardwareNoError) { + coreaudio_logerr(status, "Could not stop playback\n"); + } } } /* remove callback */ status = AudioDeviceDestroyIOProcID(core->outputDeviceID, core->ioprocid); - if (status != kAudioHardwareNoError) { + if (status != kAudioHardwareBadDeviceError && status != kAudioHardwareNoError) { coreaudio_logerr(status, "Could not remove IOProc\n"); } core->outputDeviceID = kAudioDeviceUnknown; } +static void update_device_playback_state(coreaudioVoiceOut *core) +{ + OSStatus status; + UInt32 isrunning; + + status = coreaudio_get_isrunning(core->outputDeviceID, &isrunning); + if (status != kAudioHardwareNoError) { + if (status != kAudioHardwareBadObjectError) { + coreaudio_logerr(status, + "Could not determine whether Device is playing\n"); + } + + return; + } + + if (core->enabled) { + /* start playback */ + if (!isrunning) { + status = AudioDeviceStart(core->outputDeviceID, core->ioprocid); + if (status != kAudioHardwareBadDeviceError && status != kAudioHardwareNoError) { + coreaudio_logerr (status, "Could not resume playback\n"); + } + } + } else { + /* stop playback */ + if (isrunning) { + status = AudioDeviceStop(core->outputDeviceID, + core->ioprocid); + if (status != kAudioHardwareBadDeviceError && status != kAudioHardwareNoError) { + coreaudio_logerr(status, "Could not pause playback\n"); + } + } + } +} + +static OSStatus handle_voice_change( + AudioObjectID in_object_id, + UInt32 in_number_addresses, + const AudioObjectPropertyAddress *in_addresses, + void *in_client_data) +{ + OSStatus status; + coreaudioVoiceOut *core = in_client_data; + + if (coreaudio_lock(core, __func__)) { + abort(); + } + + if (core->outputDeviceID) { + fini_out_device(core); + } + + status = init_out_device(core); + if (!status) { + update_device_playback_state(core); + } + + coreaudio_unlock (core, __func__); + return status; +} + static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque) { @@ -499,7 +582,11 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as, err = pthread_mutex_init(&core->mutex, NULL); if (err) { dolog("Could not create mutex\nReason: %s\n", strerror (err)); - return -1; + goto mutex_error; + } + + if (coreaudio_lock(core, __func__)) { + goto lock_error; } obt_as = *as; @@ -513,11 +600,43 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as, core->bufferCount = cpdo->has_buffer_count ? cpdo->buffer_count : 4; core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq; + status = AudioObjectAddPropertyListener(kAudioObjectSystemObject, + &voice_addr, handle_voice_change, + core); + if (status != kAudioHardwareNoError) { + coreaudio_playback_logerr (status, + "Could not listen to voice property change\n"); + goto listener_error; + } + if (init_out_device(core)) { - return -1; + goto device_error; } + coreaudio_unlock(core, __func__); return 0; + +device_error: + status = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, + &voice_addr, + handle_voice_change, + core); + if (status != kAudioHardwareNoError) { + coreaudio_playback_logerr(status, + "Could not remove voice property change listener\n"); + } + +listener_error: + coreaudio_unlock(core, __func__); + +lock_error: + err = pthread_mutex_destroy(&core->mutex); + if (err) { + dolog("Could not destroy mutex\nReason: %s\n", strerror (err)); + } + +mutex_error: + return -1; } static void coreaudio_fini_out (HWVoiceOut *hw) @@ -526,8 +645,22 @@ static void coreaudio_fini_out (HWVoiceOut *hw) int err; coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; + if (coreaudio_lock(core, __func__)) { + abort(); + } + + status = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, + &voice_addr, + handle_voice_change, + core); + if (status != kAudioHardwareNoError) { + coreaudio_logerr(status, "Could not remove voice property change listener\n"); + } + fini_out_device(core); + coreaudio_unlock(core, __func__); + /* destroy mutex */ err = pthread_mutex_destroy(&core->mutex); if (err) { @@ -537,27 +670,16 @@ static void coreaudio_fini_out (HWVoiceOut *hw) static void coreaudio_enable_out(HWVoiceOut *hw, bool enable) { - OSStatus status; coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; - if (enable) { - /* start playback */ - if (!isPlaying(core->outputDeviceID)) { - status = AudioDeviceStart(core->outputDeviceID, core->ioprocid); - if (status != kAudioHardwareNoError) { - coreaudio_logerr (status, "Could not resume playback\n"); - } - } - } else { - /* stop playback */ - if (isPlaying(core->outputDeviceID)) { - status = AudioDeviceStop(core->outputDeviceID, - core->ioprocid); - if (status != kAudioHardwareNoError) { - coreaudio_logerr(status, "Could not pause playback\n"); - } - } + if (coreaudio_lock(core, __func__)) { + abort(); } + + core->enabled = enable; + update_device_playback_state(core); + + coreaudio_unlock(core, __func__); } static void *coreaudio_audio_init(Audiodev *dev)