Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setUrl with headers is causing high CPU usage on physical iOS devices #1082

Closed
jxstxn1 opened this issue Oct 4, 2023 · 11 comments
Closed

setUrl with headers is causing high CPU usage on physical iOS devices #1082

jxstxn1 opened this issue Oct 4, 2023 · 11 comments
Assignees
Labels
1 backlog bug Something isn't working

Comments

@jxstxn1
Copy link

jxstxn1 commented Oct 4, 2023

Which API doesn't behave as documented, and how does it misbehave?
When I try to load audio from the web and using headers, the CPU Usage will ramp up 100%+ and stay there even if the audio is paused.

Minimal reproduction project
A fork with the header already set is available here:
https://github.com/jxstxn1/just_audio

To Reproduce (i.e. user steps, not code)
Steps to reproduce the behavior:

  1. Fork the modified repository from Minimal reproduction project
  2. open XCode debug Tab
  3. Run the app on a physical iOS Device
  4. Click on the button Load audio without header and check the XCode debug tab for a reference.
  5. Click on the button load audio with header and check XCode to verify that the CPU ramps up and stays high

Error messages

No error message is thrown

Expected behavior
CPU usage will stay at the same level as playing audio without headers

Screenshots
Screenshot 2023-10-04 at 15 25 17

Desktop (please complete the following information):

  • OS: MacOS Sonoma 14.0

Smartphone (please complete the following information):

  • Device: iPhone 13 Pro Max
  • OS: iOS 17.0.2

Flutter SDK version

[✓] Flutter (Channel stable, 3.13.6, on macOS 14.0 23A344 darwin-arm64 (Rosetta), locale en-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.82.2)
[✓] Connected device (3 available)            
[✓] Network resources

Additional context
The problem got already mentioned in #747 and #858

@jxstxn1 jxstxn1 added 1 backlog bug Something isn't working labels Oct 4, 2023
@ryanheise
Copy link
Owner

And when you don't set the headers, are you saying the CPU usage is lower? Because I think that in the web implementation, the headers are ignored anyway.

@ryanheise
Copy link
Owner

Edit: Apologies, I see you are referring to iOS, not web.

@ryanheise
Copy link
Owner

I'm a bit confused by your "Steps to reproduce - NOT CODE" since the first user step involves writing code. Please edit your bug report so that you provide a working reproduction project that I can git clone without my needing to write code, and under the steps to reproduce, provide the "user steps" (not code).

@jxstxn1
Copy link
Author

jxstxn1 commented Oct 4, 2023

I'm a bit confused by your "Steps to reproduce - NOT CODE" since the first user step involves writing code.

-> Updated the first step to clone the linked fork of just_audio.

Please edit your bug report so that you provide a working reproduction project that I can git clone without my needing to write code.

There already is an updated fork of just_audio linked in the Minimal reproduction project Section of the repo.

@ryanheise
Copy link
Owner

If you didn't include any steps for the user to perform because the audio starts playing automatically without any user interaction, please edit your example so that there is at least a button that the user can press and provide instructional user steps accordingly. I'm not going to be able to see the before and after effect without having such a user step.

@jxstxn1
Copy link
Author

jxstxn1 commented Oct 4, 2023

I updated the example repo and the steps to reproduce. I hope they are clear now :)

@ryanheise
Copy link
Owner

Hi @jxstxn1 I have created a branch feature/extractor_options which provides a constructor option useProxyForRequestHeaders that defaults to true. You can now set it to false if you would like the native platform to directly handle the headers without going through just_audio's proxy web server, and this might reduce CPU usage.

Since this was made as part of another change involving the platform interface, to test it you will need to clone the repo, and then edit the pubspec.yaml files by commenting/uncommenting the lines as indicated. Here's a patch:

diff --git a/just_audio/example/pubspec.yaml b/just_audio/example/pubspec.yaml
index f187b4c..e39b7c9 100644
--- a/just_audio/example/pubspec.yaml
+++ b/just_audio/example/pubspec.yaml
@@ -18,11 +18,11 @@ dependencies:
     path: ../

 # Uncomment when testing platform interface changes.
-# dependency_overrides:
-#   just_audio_platform_interface:
-#     path: ../../just_audio_platform_interface
-#   just_audio_web:
-#     path: ../../just_audio_web
+dependency_overrides:
+  just_audio_platform_interface:
+    path: ../../just_audio_platform_interface
+  just_audio_web:
+    path: ../../just_audio_web

 dev_dependencies:
   # TODO: uncomment these dependencies when they have nullsafe versions.
diff --git a/just_audio/pubspec.yaml b/just_audio/pubspec.yaml
index 5d41e12..bff8530 100644
--- a/just_audio/pubspec.yaml
+++ b/just_audio/pubspec.yaml
@@ -14,12 +14,12 @@ environment:
   flutter: ">=3.0.0"

 dependencies:
-  just_audio_platform_interface: ^4.2.2
-  # just_audio_platform_interface:
-  #   path: ../just_audio_platform_interface
-  just_audio_web: ^0.4.8
-  # just_audio_web:
-  #   path: ../just_audio_web
+  # just_audio_platform_interface: ^4.2.2
+  just_audio_platform_interface:
+    path: ../just_audio_platform_interface
+  # just_audio_web: ^0.4.8
+  just_audio_web:
+    path: ../just_audio_web
   audio_session: ^0.1.14
   rxdart: '>=0.26.0 <0.28.0'
   path: ^1.8.0
diff --git a/just_audio_background/example/pubspec.yaml b/just_audio_background/example/pubspec.yaml
index 1618cfb..d9a3463 100644
--- a/just_audio_background/example/pubspec.yaml
+++ b/just_audio_background/example/pubspec.yaml
@@ -16,11 +16,11 @@ dependencies:
     path: ..

 # Uncomment when testing platform interface changes.
-# dependency_overrides:
-#   just_audio_platform_interface:
-#     path: ../../just_audio_platform_interface
-#   just_audio_web:
-#     path: ../../just_audio_web
+dependency_overrides:
+  just_audio_platform_interface:
+    path: ../../just_audio_platform_interface
+  just_audio_web:
+    path: ../../just_audio_web

 dev_dependencies:
   # TODO: uncomment these dependencies when they have nullsafe versions.
diff --git a/just_audio_background/pubspec.yaml b/just_audio_background/pubspec.yaml
index 6f405a5..ed169d8 100644
--- a/just_audio_background/pubspec.yaml
+++ b/just_audio_background/pubspec.yaml
@@ -25,5 +25,5 @@ dev_dependencies:
   flutter_lints: ^2.0.1

 environment:
-  sdk: ">=2.14.0 <4.0.0"
+  sdk: ">=2.17.0 <4.0.0"
   flutter: ">=3.0.0"
diff --git a/just_audio_web/pubspec.yaml b/just_audio_web/pubspec.yaml
index bb096d9..02dfa08 100644
--- a/just_audio_web/pubspec.yaml
+++ b/just_audio_web/pubspec.yaml
@@ -11,9 +11,9 @@ flutter:
         fileName: just_audio_web.dart

 dependencies:
-  just_audio_platform_interface: ^4.2.2
-  # just_audio_platform_interface:
-  #   path: ../just_audio_platform_interface
+  # just_audio_platform_interface: ^4.2.2
+  just_audio_platform_interface:
+    path: ../just_audio_platform_interface
   flutter:
     sdk: flutter
   flutter_web_plugins:

Note that headers are implemented natively on iOS by using the AVURLAssetHTTPHeaderFieldsKey which is not a public API. Until now I had been avoiding it by implementing my own proxy, although since flutter's video_player uses the private API I figure I should at least provide the option to you. There are some caveats to iOS's native implemention in that it's known to ignore the user-agent header on older versions of iOS and if you send a request to a server that does not support range requests, it only sends the headers on the first request. I have no influence over iOS's native implementation, but I am just providing you the option to be able to use it directly without going through the indirect layer I offer via the proxy.

Let me know how it goes and if it works as intended I can push another release.

@ryanheise
Copy link
Owner

Just fixed a bug on this branch where it assumed headers were always specified (it should now also work where no headers are specified).

@ryanheise
Copy link
Owner

@jxstxn1 I've just published the platform interface, so it is now possible to test without patching the pubspec files.

Please let me know if this solves your high CPU usage issue and I can push out the release.

@ryanheise
Copy link
Owner

I've published the changes mentioned above. I'll close this issue for now, but if you still find it gives high CPU usage, make a comment below and I can reopen it.

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with just_audio.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 backlog bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants