Skip to content

Commit

Permalink
Polishing crossorigin API and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed May 17, 2024
1 parent 38cdf9d commit 4eb2e0c
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 36 deletions.
5 changes: 5 additions & 0 deletions just_audio/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.9.38

* Migrate to package:web.
* Add AudioPlayer.setWebCrossOrigin for CORS on web (@danielwinkler).

## 0.9.37

* Support useLazyPreparation on iOS/macOS.
Expand Down
5 changes: 2 additions & 3 deletions just_audio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ Please also consider pressing the thumbs up button at the top of [this page](htt
| read from file | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| read from asset | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| read from byte stream | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| request headers | ✅ | ✅ | ✅ | [^1] | ✅ | ✅ |
| request headers | ✅ | ✅ | ✅ | * | ✅ | ✅ |
| DASH | ✅ | | | | ✅ | ✅ |
| HLS | ✅ | ✅ | ✅ | | ✅ | ✅ |
| ICY metadata | ✅ | ✅ | ✅ | | | |
Expand All @@ -456,8 +456,7 @@ Please also consider pressing the thumbs up button at the top of [this page](htt
| equalizer | ✅ | | | | | ✅ |
| volume boost | ✅ | | | | | ✅ |

[^1]: While headers cannot be set directly, cookies can be used to send information in the [Cookie header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie).
See also `setWebCrossOrigin` to send cookies when loading audio files from a different origin (see [HTMLMediaElement crossOrigin](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/crossOrigin)).
(*): While request headers cannot be set directly on Web, cookies can be used to send information in the [Cookie header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie). See also `AudioPlayer.setWebCrossOrigin` to allow sending cookies when loading audio files from the same origin or a different origin.


## Experimental features
Expand Down
38 changes: 32 additions & 6 deletions just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class AudioPlayer {
bool _playInterrupted = false;
bool _platformLoading = false;
AndroidAudioAttributes? _androidAudioAttributes;
WebCrossOrigin? _webCrossOrigin;
final bool _androidApplyAudioAttributes;
final bool _handleAudioSessionActivation;

Expand Down Expand Up @@ -1196,16 +1197,27 @@ class AudioPlayer {
usage: audioAttributes.usage.value));
}

Future<void> setWebCrossOrigin(bool useCredentials) async {
/// Set the `crossorigin` attribute on the `<audio>` element backing this
/// player instance on web (see
/// [HTMLMediaElement crossorigin](https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/crossOrigin) ).
///
/// If [webCrossOrigin] is null (the initial state), the URL will be fetched
/// without CORS. If it is `useCredentials`, a CORS request will be made
/// exchanging credentials (via cookies/certificates/HTTP authentication)
/// regardless of the origin. If it is 'anonymous', a CORS request will be
/// made, but credentials are exchanged only if the URL is fetched from the
/// same origin.
Future<void> setWebCrossOrigin(WebCrossOrigin? webCrossOrigin) async {
if (_disposed) return;
if (!kIsWeb && !_isUnitTest()) return;
final WebCrossOriginMessage crossOriginMsg = useCredentials
? WebCrossOriginMessage.useCredentials
: WebCrossOriginMessage.anonymous;

await (await _platform).webSetCrossOrigin(
WebSetCrossOriginRequest(crossOrigin: crossOriginMsg),
await (await _platform).setWebCrossOrigin(
SetWebCrossOriginRequest(
crossOrigin: webCrossOrigin == null
? null
: WebCrossOriginMessage.values[webCrossOrigin.index]),
);
_webCrossOrigin = webCrossOrigin;
}

/// Release all resources associated with this player. You must invoke this
Expand Down Expand Up @@ -1458,6 +1470,11 @@ class AudioPlayer {
? ShuffleModeMessage.all
: ShuffleModeMessage.none));
if (checkInterruption()) return platform;
if (kIsWeb && _webCrossOrigin != null) {
await platform.setWebCrossOrigin(SetWebCrossOriginRequest(
crossOrigin: WebCrossOriginMessage.values[_webCrossOrigin!.index],
));
}
for (var audioEffect in _audioPipeline._audioEffects) {
await audioEffect._activate(platform);
if (checkInterruption()) return platform;
Expand Down Expand Up @@ -3498,6 +3515,9 @@ class DefaultShuffleOrder extends ShuffleOrder {
/// An enumeration of modes that can be passed to [AudioPlayer.setLoopMode].
enum LoopMode { off, one, all }

/// Possible values that can be passed to [AudioPlayer.setWebCrossOrigin].
enum WebCrossOrigin { anonymous, useCredentials }

/// The stand-in platform implementation to use when the player is in the idle
/// state and the native platform is deallocated.
class _IdleAudioPlayer extends AudioPlayerPlatform {
Expand Down Expand Up @@ -3595,6 +3615,12 @@ class _IdleAudioPlayer extends AudioPlayerPlatform {
return SetShuffleOrderResponse();
}

@override
Future<SetWebCrossOriginResponse> setWebCrossOrigin(
SetWebCrossOriginRequest request) async {
return SetWebCrossOriginResponse();
}

@override
Future<SetAutomaticallyWaitsToMinimizeStallingResponse>
setAutomaticallyWaitsToMinimizeStalling(
Expand Down
2 changes: 1 addition & 1 deletion just_audio/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ environment:
flutter: ">=3.10.0"

dependencies:
just_audio_platform_interface: ^4.2.3
just_audio_platform_interface: ^4.3.0
# just_audio_platform_interface:
# path: ../just_audio_platform_interface
just_audio_web: ^0.4.11
Expand Down
4 changes: 4 additions & 0 deletions just_audio_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.3.0

* Add setWebCrossOrigin for CORS on web (@danielwinkler).

## 4.2.2

* Add setAllowsExternalPlayback on iOS/macOS.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ abstract class AudioPlayerPlatform {
}

/// Sets the 'crossOrigin' attribute on the web audio element.
Future<WebSetCrossOriginResponse> webSetCrossOrigin(
WebSetCrossOriginRequest request) {
throw UnimplementedError("webSetCrossOrigin() has not been implemented.");
Future<SetWebCrossOriginResponse> setWebCrossOrigin(
SetWebCrossOriginRequest request) {
throw UnimplementedError("setWebCrossOrigin() has not been implemented.");
}
}

Expand Down Expand Up @@ -1492,15 +1492,12 @@ class AndroidEqualizerMessage extends AudioEffectMessage {
};
}

class WebSetCrossOriginRequest {
final WebCrossOriginMessage crossOrigin;
class SetWebCrossOriginRequest {
final WebCrossOriginMessage? crossOrigin;

WebSetCrossOriginRequest({required this.crossOrigin});
SetWebCrossOriginRequest({required this.crossOrigin});
}

class WebSetCrossOriginResponse {
static WebSetCrossOriginResponse fromMap(Map<dynamic, dynamic> map) =>
WebSetCrossOriginResponse();
}
class SetWebCrossOriginResponse {}

enum WebCrossOriginMessage { anonymous, useCredentials }
2 changes: 1 addition & 1 deletion just_audio_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the just_audio plugin. Different pl
homepage: https://github.com/ryanheise/just_audio/tree/master/just_audio_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 4.2.3
version: 4.3.0

dependencies:
flutter:
Expand Down
2 changes: 1 addition & 1 deletion just_audio_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 0.4.11

* Bump package:web upper bound to <0.6.0
* Add AudioPlayer.setWebCrossOrigin for CORS on web.
* Add AudioPlayer.setWebCrossOrigin for CORS on web (@danielwinkler).

## 0.4.10

Expand Down
20 changes: 7 additions & 13 deletions just_audio_web/lib/just_audio_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,13 @@ class Html5AudioPlayer extends JustAudioPlayer {
}

@override
Future<WebSetCrossOriginResponse> webSetCrossOrigin(
WebSetCrossOriginRequest request) async {
switch (request.crossOrigin) {
case WebCrossOriginMessage.anonymous:
_audioElement.crossOrigin = 'anonymous';
break;
case WebCrossOriginMessage.useCredentials:
_audioElement.crossOrigin = 'use-credentials';
break;
default:
throw Exception("Unknown CrossOriginMessage: $request.crossOrigin");
}
return WebSetCrossOriginResponse();
Future<SetWebCrossOriginResponse> setWebCrossOrigin(
SetWebCrossOriginRequest request) async {
_audioElement.crossOrigin = const {
WebCrossOriginMessage.anonymous: 'anonymous',
WebCrossOriginMessage.useCredentials: 'use-credentials',
}[request.crossOrigin];
return SetWebCrossOriginResponse();
}

@override
Expand Down
2 changes: 1 addition & 1 deletion just_audio_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ flutter:
fileName: just_audio_web.dart

dependencies:
just_audio_platform_interface: ^4.2.3
just_audio_platform_interface: ^4.3.0
# just_audio_platform_interface:
# path: ../just_audio_platform_interface
flutter:
Expand Down

0 comments on commit 4eb2e0c

Please sign in to comment.