From 711ce4a0ea9c66a815a1071b5fed09dbd8e286c6 Mon Sep 17 00:00:00 2001 From: guozhanxin Date: Thu, 19 Sep 2019 21:52:20 +0800 Subject: [PATCH] =?UTF-8?q?[usbd]=20Fixed=20Windows=20first=20recording=20?= =?UTF-8?q?failure.|=E4=BF=AE=E5=A4=8Dwindows=E4=B8=8B=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E5=BD=95=E9=9F=B3=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit windows下第一次录放音失败的原因是:第一录放音时 windows 会先连续发送开始、结束检测设备,然后才正式开始。线程来不及处理两次开始事件不会累积,最后导致直接结束, --- .../drivers/usb/usbdevice/class/audio_mic.c | 14 ++++++++++++-- .../usb/usbdevice/class/audio_speaker.c | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/components/drivers/usb/usbdevice/class/audio_mic.c b/components/drivers/usb/usbdevice/class/audio_mic.c index 242cd8a2d9..4ac85711ad 100644 --- a/components/drivers/usb/usbdevice/class/audio_mic.c +++ b/components/drivers/usb/usbdevice/class/audio_mic.c @@ -83,6 +83,7 @@ struct uac_audio_mic { rt_device_t dev; rt_event_t event; + rt_uint8_t open_count; rt_uint8_t *buffer; rt_uint32_t buffer_index; @@ -300,12 +301,16 @@ void mic_entry(void *parameter) while (1) { - if (rt_event_recv(mic.event, EVENT_RECORD_START, + if (rt_event_recv(mic.event, EVENT_RECORD_START | EVENT_RECORD_STOP, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 1000, &e) != RT_EOK) { continue; } + if (mic.open_count == 0) + { + continue; + } LOG_D("record start"); rt_device_open(mic.dev, RT_DEVICE_OFLAG_RDONLY); @@ -323,7 +328,10 @@ void mic_entry(void *parameter) RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 1000, &e) != RT_EOK) { - continue; + if (mic.open_count > 0) + continue; + else + break; } if (e & EVENT_RECORD_DATA) { @@ -351,12 +359,14 @@ static rt_err_t _record_start(ufunction_t func) mic.ep->request.req_type = UIO_REQUEST_WRITE; rt_usbd_io_request(func->device, mic.ep, &mic.ep->request); + mic.open_count ++; rt_event_send(mic.event, EVENT_RECORD_START); return 0; } static rt_err_t _record_stop(ufunction_t func) { + mic.open_count --; rt_event_send(mic.event, EVENT_RECORD_STOP); return 0; } diff --git a/components/drivers/usb/usbdevice/class/audio_speaker.c b/components/drivers/usb/usbdevice/class/audio_speaker.c index a6023db051..84c36d2053 100644 --- a/components/drivers/usb/usbdevice/class/audio_speaker.c +++ b/components/drivers/usb/usbdevice/class/audio_speaker.c @@ -83,6 +83,7 @@ struct uac_audio_speaker { rt_device_t dev; rt_event_t event; + rt_uint8_t open_count; rt_uint8_t *buffer; rt_uint32_t buffer_index; @@ -300,13 +301,17 @@ void speaker_entry(void *parameter) while (1) { - if (rt_event_recv(speaker.event, EVENT_AUDIO_START, + if (rt_event_recv(speaker.event, EVENT_AUDIO_START | EVENT_AUDIO_STOP, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 1000, &e) != RT_EOK) { continue; } - LOG_D("record start"); + if (speaker.open_count == 0) + { + continue; + } + LOG_D("play start"); rt_device_open(speaker.dev, RT_DEVICE_OFLAG_WRONLY); @@ -323,7 +328,10 @@ void speaker_entry(void *parameter) RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 1000, &e) != RT_EOK) { - continue; + if (speaker.open_count > 0) + continue; + else + break; } if (e & EVENT_AUDIO_DATA) { @@ -335,7 +343,7 @@ void speaker_entry(void *parameter) break; } } - LOG_D("record stop"); + LOG_D("play stop"); rt_device_close(speaker.dev); } @@ -351,6 +359,7 @@ static rt_err_t _audio_start(ufunction_t func) speaker.ep->request.req_type = UIO_REQUEST_READ_FULL; rt_usbd_io_request(func->device, speaker.ep, &speaker.ep->request); + speaker.open_count ++; rt_event_send(speaker.event, EVENT_AUDIO_START); return 0; @@ -358,6 +367,7 @@ static rt_err_t _audio_start(ufunction_t func) static rt_err_t _audio_stop(ufunction_t func) { + speaker.open_count --; rt_event_send(speaker.event, EVENT_AUDIO_STOP); return 0; } -- GitLab