Browse Source

slow down sense with audio packets

Sanjay Govind 10 months ago
parent
commit
c23919c817
3 changed files with 107 additions and 91 deletions
  1. 96 88
      helpers/pof_usb.c
  2. 1 0
      helpers/pof_usb.h
  3. 10 3
      helpers/pof_usb_xbox360.c

+ 96 - 88
helpers/pof_usb.c

@@ -9,7 +9,7 @@
 #define POF_USB_VID (0x1430)
 #define POF_USB_PID (0x0150)
 
-#define POF_USB_EP_IN  (0x81)
+#define POF_USB_EP_IN (0x81)
 #define POF_USB_EP_OUT (0x02)
 
 #define POF_USB_ACTUAL_OUTPUT_SIZE 0x20
@@ -26,7 +26,7 @@ static const uint8_t hid_report_desc[] = {0x06, 0x00, 0xFF, 0x09, 0x01, 0xA1, 0x
 
 static usbd_respond pof_usb_ep_config(usbd_device* dev, uint8_t cfg);
 static usbd_respond
-    pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
+pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
 static void pof_usb_send(usbd_device* dev, uint8_t* buf, uint16_t len);
 static int32_t pof_usb_receive(usbd_device* dev, uint8_t* buf, uint16_t max_len);
 
@@ -40,18 +40,18 @@ static int32_t pof_thread_worker(void* context) {
 
     uint32_t len_data = 0;
     uint8_t tx_data[POF_USB_TX_MAX_SIZE] = {0};
-    uint32_t timeout = TIMEOUT_NORMAL; // FuriWaitForever; //ms
+    uint32_t timeout = TIMEOUT_NORMAL;  // FuriWaitForever; //ms
     uint32_t last = 0;
 
-    while(true) {
+    while (true) {
         uint32_t now = furi_get_tick();
         uint32_t flags = furi_thread_flags_wait(EventAll, FuriFlagWaitAny, timeout);
-        if(flags & EventRx) { //fast flag
-            if(virtual_portal->speaker) {
+        if (flags & EventRx) {  // fast flag
+            if (virtual_portal->speaker) {
                 uint8_t buf[POF_USB_RX_MAX_SIZE];
                 len_data = pof_usb_receive(dev, buf, POF_USB_RX_MAX_SIZE);
                 // https://github.com/xMasterX/all-the-plugins/blob/dev/base_pack/wav_player/wav_player_hal.c
-                if(len_data > 0) {
+                if (len_data > 0) {
                     /*
                     FURI_LOG_RAW_I("pof_usb_receive: ");
                     for(uint32_t i = 0; i < len_data; i++) {
@@ -59,69 +59,77 @@ static int32_t pof_thread_worker(void* context) {
                     }
                     FURI_LOG_RAW_I("\r\n");
                     */
-                   virtual_portal_process_audio(virtual_portal, buf, len_data);
-                   timeout = TIMEOUT_AFTER_RESPONSE;
-                   last = now;
+                    virtual_portal_process_audio(virtual_portal, buf, len_data);
                 }
-            } else if(pof_usb->dataAvailable > 0) {
+            }
+            if (pof_usb->dataAvailable > 0) {
                 memset(tx_data, 0, sizeof(tx_data));
                 int send_len =
                     virtual_portal_process_message(virtual_portal, pof_usb->data, tx_data);
-                if(send_len > 0) {
+                if (send_len > 0) {
                     pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
                     timeout = TIMEOUT_AFTER_RESPONSE;
+                    if (virtual_portal->speaker) {
+                        timeout = TIMEOUT_AFTER_MUSIC;
+                    }
                     last = now;
                 }
                 pof_usb->dataAvailable = 0;
             }
 
             // Check next status time since the timeout based one might be starved by incoming packets.
-            if(now > last + timeout) {
+            if (now > last + timeout) {
                 memset(tx_data, 0, sizeof(tx_data));
                 len_data = virtual_portal_send_status(virtual_portal, tx_data);
-                if(len_data > 0) {
+                if (len_data > 0) {
                     pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
                 }
                 last = now;
                 timeout = TIMEOUT_NORMAL;
+                if (virtual_portal->speaker) {
+                    timeout = TIMEOUT_AFTER_MUSIC;
+                }
             }
 
-            flags &= ~EventRx; // clear flag
+            flags &= ~EventRx;  // clear flag
         }
 
-        if(flags) {
-            if(flags & EventResetSio) {
+        if (flags) {
+            if (flags & EventResetSio) {
             }
-            if(flags & EventTxComplete) {
+            if (flags & EventTxComplete) {
                 pof_usb->tx_complete = true;
             }
 
-            if(flags & EventTxImmediate) {
+            if (flags & EventTxImmediate) {
                 pof_usb->tx_immediate = true;
-                if(pof_usb->tx_complete) {
+                if (pof_usb->tx_complete) {
                     flags |= EventTx;
                 }
             }
 
-            if(flags & EventTx) {
+            if (flags & EventTx) {
                 pof_usb->tx_complete = false;
                 pof_usb->tx_immediate = false;
             }
 
-            if(flags & EventExit) {
+            if (flags & EventExit) {
                 FURI_LOG_I(TAG, "exit");
                 break;
             }
         }
 
-        if(flags == (uint32_t)FuriFlagErrorISR) { // timeout
+        if (flags == (uint32_t)FuriFlagErrorISR) {  // timeout
             memset(tx_data, 0, sizeof(tx_data));
             len_data = virtual_portal_send_status(virtual_portal, tx_data);
-            if(len_data > 0) {
+            if (len_data > 0) {
                 pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
             }
             last = now;
             timeout = TIMEOUT_NORMAL;
+            if (virtual_portal->speaker) {
+                timeout = TIMEOUT_AFTER_MUSIC;
+            }
         }
     }
 
@@ -153,7 +161,7 @@ static void pof_usb_deinit(usbd_device* dev) {
     usbd_reg_control(dev, NULL);
 
     PoFUsb* pof_usb = pof_cur;
-    if(!pof_usb || pof_usb->dev != dev) {
+    if (!pof_usb || pof_usb->dev != dev) {
         return;
     }
     pof_cur = NULL;
@@ -196,7 +204,7 @@ static void pof_usb_wakeup(usbd_device* dev) {
 
 static void pof_usb_suspend(usbd_device* dev) {
     PoFUsb* pof_usb = pof_cur;
-    if(!pof_usb || pof_usb->dev != dev) return;
+    if (!pof_usb || pof_usb->dev != dev) return;
 }
 
 static void pof_usb_rx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
@@ -216,19 +224,19 @@ static void pof_usb_tx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep)
 }
 
 static usbd_respond pof_usb_ep_config(usbd_device* dev, uint8_t cfg) {
-    switch(cfg) {
-    case 0: // deconfig
-        usbd_ep_deconfig(dev, POF_USB_EP_OUT);
-        usbd_ep_deconfig(dev, POF_USB_EP_IN);
-        usbd_reg_endpoint(dev, POF_USB_EP_OUT, NULL);
-        usbd_reg_endpoint(dev, POF_USB_EP_IN, NULL);
-        return usbd_ack;
-    case 1: // config
-        usbd_ep_config(dev, POF_USB_EP_IN, USB_EPTYPE_INTERRUPT, POF_USB_EP_IN_SIZE);
-        usbd_ep_config(dev, POF_USB_EP_OUT, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
-        usbd_reg_endpoint(dev, POF_USB_EP_IN, pof_usb_tx_ep_callback);
-        usbd_reg_endpoint(dev, POF_USB_EP_OUT, pof_usb_rx_ep_callback);
-        return usbd_ack;
+    switch (cfg) {
+        case 0:  // deconfig
+            usbd_ep_deconfig(dev, POF_USB_EP_OUT);
+            usbd_ep_deconfig(dev, POF_USB_EP_IN);
+            usbd_reg_endpoint(dev, POF_USB_EP_OUT, NULL);
+            usbd_reg_endpoint(dev, POF_USB_EP_IN, NULL);
+            return usbd_ack;
+        case 1:  // config
+            usbd_ep_config(dev, POF_USB_EP_IN, USB_EPTYPE_INTERRUPT, POF_USB_EP_IN_SIZE);
+            usbd_ep_config(dev, POF_USB_EP_OUT, USB_EPTYPE_INTERRUPT, POF_USB_EP_OUT_SIZE);
+            usbd_reg_endpoint(dev, POF_USB_EP_IN, pof_usb_tx_ep_callback);
+            usbd_reg_endpoint(dev, POF_USB_EP_OUT, pof_usb_rx_ep_callback);
+            return usbd_ack;
     }
     return usbd_fail;
 }
@@ -252,8 +260,8 @@ static const struct usb_device_descriptor usb_pof_dev_descr = {
     .idVendor = POF_USB_VID,
     .idProduct = POF_USB_PID,
     .bcdDevice = VERSION_BCD(1, 0, 0),
-    .iManufacturer = 1, // UsbDevManuf
-    .iProduct = 2, // UsbDevProduct
+    .iManufacturer = 1,  // UsbDevManuf
+    .iProduct = 2,       // UsbDevProduct
     .iSerialNumber = 0,
     .bNumConfigurations = 1,
 };
@@ -314,7 +322,7 @@ static const struct PoFUsbDescriptor usb_pof_cfg_descr = {
 
 /* Control requests handler */
 static usbd_respond
-    pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
+pof_hid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
     UNUSED(callback);
     uint8_t wValueH = req->wValue >> 8;
     uint16_t length = req->wLength;
@@ -322,52 +330,52 @@ static usbd_respond
     PoFUsb* pof_usb = pof_cur;
 
     /* HID control requests */
-    if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
-           (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
-       req->wIndex == 0) {
-        switch(req->bRequest) {
-        case USB_HID_SETIDLE:
-            return usbd_ack;
-        case USB_HID_SETPROTOCOL:
-            return usbd_ack;
-        case USB_HID_GETREPORT:
-            dev->status.data_ptr = pof_usb->tx_data;
-            dev->status.data_count = sizeof(pof_usb->tx_data);
-            return usbd_ack;
-        case USB_HID_SETREPORT:
-            if(wValueH == HID_REPORT_TYPE_INPUT) {
-                if(length == POF_USB_RX_MAX_SIZE) {
-                    return usbd_ack;
-                }
-            } else if(wValueH == HID_REPORT_TYPE_OUTPUT) {
-                memcpy(pof_usb->data, req->data, req->wLength);
-                pof_usb->dataAvailable += req->wLength;
-                furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventRx);
-
+    if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
+            (USB_REQ_INTERFACE | USB_REQ_CLASS) &&
+        req->wIndex == 0) {
+        switch (req->bRequest) {
+            case USB_HID_SETIDLE:
                 return usbd_ack;
-            } else if(wValueH == HID_REPORT_TYPE_FEATURE) {
+            case USB_HID_SETPROTOCOL:
                 return usbd_ack;
-            }
-            return usbd_fail;
-        default:
-            return usbd_fail;
+            case USB_HID_GETREPORT:
+                dev->status.data_ptr = pof_usb->tx_data;
+                dev->status.data_count = sizeof(pof_usb->tx_data);
+                return usbd_ack;
+            case USB_HID_SETREPORT:
+                if (wValueH == HID_REPORT_TYPE_INPUT) {
+                    if (length == POF_USB_RX_MAX_SIZE) {
+                        return usbd_ack;
+                    }
+                } else if (wValueH == HID_REPORT_TYPE_OUTPUT) {
+                    memcpy(pof_usb->data, req->data, req->wLength);
+                    pof_usb->dataAvailable += req->wLength;
+                    furi_thread_flags_set(furi_thread_get_id(pof_usb->thread), EventRx);
+
+                    return usbd_ack;
+                } else if (wValueH == HID_REPORT_TYPE_FEATURE) {
+                    return usbd_ack;
+                }
+                return usbd_fail;
+            default:
+                return usbd_fail;
         }
     }
 
-    if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
-           (USB_REQ_INTERFACE | USB_REQ_STANDARD) &&
-       req->wIndex == 0 && req->bRequest == USB_STD_GET_DESCRIPTOR) {
-        switch(wValueH) {
-        case USB_DTYPE_HID:
-            dev->status.data_ptr = (uint8_t*)&(usb_pof_cfg_descr.hid_desc);
-            dev->status.data_count = sizeof(usb_pof_cfg_descr.hid_desc);
-            return usbd_ack;
-        case USB_DTYPE_HID_REPORT:
-            dev->status.data_ptr = (uint8_t*)hid_report_desc;
-            dev->status.data_count = sizeof(hid_report_desc);
-            return usbd_ack;
-        default:
-            return usbd_fail;
+    if (((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
+            (USB_REQ_INTERFACE | USB_REQ_STANDARD) &&
+        req->wIndex == 0 && req->bRequest == USB_STD_GET_DESCRIPTOR) {
+        switch (wValueH) {
+            case USB_DTYPE_HID:
+                dev->status.data_ptr = (uint8_t*)&(usb_pof_cfg_descr.hid_desc);
+                dev->status.data_count = sizeof(usb_pof_cfg_descr.hid_desc);
+                return usbd_ack;
+            case USB_DTYPE_HID_REPORT:
+                dev->status.data_ptr = (uint8_t*)hid_report_desc;
+                dev->status.data_count = sizeof(hid_report_desc);
+                return usbd_ack;
+            default:
+                return usbd_fail;
         }
     }
     return usbd_fail;
@@ -389,15 +397,15 @@ PoFUsb* pof_usb_start(VirtualPortal* virtual_portal) {
     pof_usb->usb.str_serial_descr = NULL;
     pof_usb->usb.cfg_descr = (void*)&usb_pof_cfg_descr;
 
-    if(!furi_hal_usb_set_config(&pof_usb->usb, pof_usb)) {
+    if (!furi_hal_usb_set_config(&pof_usb->usb, pof_usb)) {
         FURI_LOG_E(TAG, "USB locked, can not start");
-        if(pof_usb->usb.str_manuf_descr) {
+        if (pof_usb->usb.str_manuf_descr) {
             free(pof_usb->usb.str_manuf_descr);
         }
-        if(pof_usb->usb.str_prod_descr) {
+        if (pof_usb->usb.str_prod_descr) {
             free(pof_usb->usb.str_prod_descr);
         }
-        if(pof_usb->usb.str_serial_descr) {
+        if (pof_usb->usb.str_serial_descr) {
             free(pof_usb->usb.str_serial_descr);
         }
 
@@ -409,7 +417,7 @@ PoFUsb* pof_usb_start(VirtualPortal* virtual_portal) {
 }
 
 void pof_usb_stop(PoFUsb* pof_usb) {
-    if(pof_usb) {
+    if (pof_usb) {
         furi_hal_usb_set_config(pof_usb->usb_prev, NULL);
     }
 }

+ 1 - 0
helpers/pof_usb.h

@@ -21,6 +21,7 @@
 
 #define TIMEOUT_NORMAL 32
 #define TIMEOUT_AFTER_RESPONSE 100
+#define TIMEOUT_AFTER_MUSIC 300
 
 typedef struct PoFUsb PoFUsb;
 

+ 10 - 3
helpers/pof_usb_xbox360.c

@@ -71,6 +71,9 @@ static int32_t pof_thread_worker(void* context) {
                     pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
                     timeout = TIMEOUT_AFTER_RESPONSE;
                     last = now;
+                    if (virtual_portal->speaker) {
+                        timeout = TIMEOUT_AFTER_MUSIC;
+                    }
                 }
             } else if (len_data > 0 && buf[0] == 0x0b && buf[1] == 0x17) {
                 // 360 audio packets start with 0b 17, samples start after the two byte header
@@ -82,8 +85,6 @@ static int32_t pof_thread_worker(void* context) {
                 FURI_LOG_RAW_I("\r\n");
                 */
                 virtual_portal_process_audio(virtual_portal, buf + 2, len_data - 2);
-                timeout = TIMEOUT_AFTER_RESPONSE;
-                last = now;
             }
 
             // Check next status time since the timeout based one might be starved by incoming packets.
@@ -97,6 +98,9 @@ static int32_t pof_thread_worker(void* context) {
                 }
                 last = now;
                 timeout = TIMEOUT_NORMAL;
+                if (virtual_portal->speaker) {
+                    timeout = TIMEOUT_AFTER_MUSIC;
+                }
             }
 
             flags &= ~EventRx;  // clear flag
@@ -135,8 +139,11 @@ static int32_t pof_thread_worker(void* context) {
                 tx_data[1] = 0x14;
                 pof_usb_send(dev, tx_data, POF_USB_ACTUAL_OUTPUT_SIZE);
             }
-            timeout = TIMEOUT_NORMAL;
             last = now;
+            timeout = TIMEOUT_NORMAL;
+            if (virtual_portal->speaker) {
+                timeout = TIMEOUT_AFTER_MUSIC;
+            }
         }
     }