Skip to content

Commit

Permalink
Ensure proxy is always initialised before use.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanheise committed Oct 25, 2021
1 parent 633dae5 commit 1423704
Showing 1 changed file with 19 additions and 32 deletions.
51 changes: 19 additions & 32 deletions just_audio/lib/just_audio.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AudioPlayer {
StreamSubscription? _playerDataSubscription;

final String _id;
_ProxyHttpServer? _proxy;
final _proxy = _ProxyHttpServer();
AudioSource? _audioSource;
final Map<String, AudioSource> _audioSources = {};
bool _disposed = false;
Expand Down Expand Up @@ -750,13 +750,6 @@ class AudioPlayer {
}

try {
if (!kIsWeb && (source._requiresProxy || _userAgent != null)) {
if (_proxy == null) {
_proxy = _ProxyHttpServer();
await _proxy!.start();
checkInterruption();
}
}
await source._setup(this);
checkInterruption();
source._shuffle(initialIndex: initialSeekValues?.index ?? 0);
Expand Down Expand Up @@ -1106,7 +1099,7 @@ class AudioPlayer {
_audioSource = null;
_audioSources.values.forEach((s) => s._dispose());
_audioSources.clear();
_proxy?.stop();
_proxy.stop();
await _durationSubject.close();
await _loopModeSubject.close();
await _shuffleModeEnabledSubject.close();
Expand Down Expand Up @@ -1871,6 +1864,7 @@ class AndroidLivePlaybackSpeedControl {
/// A local proxy HTTP server for making remote GET requests with headers.
class _ProxyHttpServer {
late HttpServer _server;
bool _running = false;

/// Maps request keys to [_ProxyHandler]s.
final Map<String, _ProxyHandler> _handlerMap = {};
Expand Down Expand Up @@ -1917,8 +1911,15 @@ class _ProxyHttpServer {
/// but differ in other respects such as the port or headers.
String _requestKey(Uri uri) => '${uri.path}?${uri.query}';

/// Start the server if it is not already running.
Future ensureRunning() async {
if (_running) return;
return await start();
}

/// Starts the server.
Future start() async {
_running = true;
_server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
_server.listen((request) async {
if (request.method == 'GET') {
Expand All @@ -1930,7 +1931,11 @@ class _ProxyHttpServer {
}

/// Stops the server
Future stop() => _server.close();
Future stop() async {
if (!_running) return;
_running = false;
return await _server.close();
}
}

/// Encapsulates the start and end of an HTTP range request.
Expand Down Expand Up @@ -2034,8 +2039,6 @@ abstract class AudioSource {

AudioSourceMessage _toMessage();

bool get _requiresProxy;

List<IndexedAudioSource> get sequence;

List<int> get shuffleIndices;
Expand Down Expand Up @@ -2089,7 +2092,8 @@ abstract class UriAudioSource extends IndexedAudioSource {
} else if (uri.scheme != 'file' &&
!kIsWeb &&
(headers != null || player._userAgent != null)) {
_overrideUri = player._proxy!.addUriAudioSource(this);
await player._proxy.ensureRunning();
_overrideUri = player._proxy.addUriAudioSource(this);
}
}

Expand Down Expand Up @@ -2139,9 +2143,6 @@ abstract class UriAudioSource extends IndexedAudioSource {
'assets',
...Uri.parse(assetPath).pathSegments,
]));

@override
bool get _requiresProxy => uri.scheme != 'file' && headers != null && !kIsWeb;
}

/// An [AudioSource] representing a regular media file such as an MP3 or M4A
Expand Down Expand Up @@ -2229,9 +2230,6 @@ class SilenceAudioSource extends IndexedAudioSource {
required Duration duration,
}) : super(tag: tag, duration: duration);

@override
bool get _requiresProxy => false;

@override
AudioSourceMessage _toMessage() =>
SilenceAudioSourceMessage(id: _id, duration: duration);
Expand Down Expand Up @@ -2450,9 +2448,6 @@ class ConcatenatingAudioSource extends AudioSource {
return indices;
}

@override
bool get _requiresProxy => children.any((source) => source._requiresProxy);

@override
AudioSourceMessage _toMessage() => ConcatenatingAudioSourceMessage(
id: _id,
Expand Down Expand Up @@ -2485,9 +2480,6 @@ class ClippingAudioSource extends IndexedAudioSource {
await child._setup(player);
}

@override
bool get _requiresProxy => child._requiresProxy;

@override
AudioSourceMessage _toMessage() => ClippingAudioSourceMessage(
id: _id,
Expand Down Expand Up @@ -2525,9 +2517,6 @@ class LoopingAudioSource extends AudioSource {
@override
List<int> get shuffleIndices => List.generate(count, (i) => i);

@override
bool get _requiresProxy => child._requiresProxy;

@override
AudioSourceMessage _toMessage() => LoopingAudioSourceMessage(
id: _id, child: child._toMessage(), count: count);
Expand All @@ -2551,7 +2540,8 @@ abstract class StreamAudioSource extends IndexedAudioSource {
_uri = _encodeDataUrl(await base64.encoder.bind(response.stream).join(),
response.contentType);
} else {
_uri = player._proxy!.addStreamAudioSource(this);
await player._proxy.ensureRunning();
_uri = player._proxy.addStreamAudioSource(this);
}
}

Expand All @@ -2562,9 +2552,6 @@ abstract class StreamAudioSource extends IndexedAudioSource {
/// a 500 response will be sent back to the player.
Future<StreamAudioResponse> request([int? start, int? end]);

@override
bool get _requiresProxy => !kIsWeb;

@override
AudioSourceMessage _toMessage() => ProgressiveAudioSourceMessage(
id: _id, uri: _uri.toString(), headers: null, tag: tag);
Expand Down

0 comments on commit 1423704

Please sign in to comment.