Skip to content

Commit

Permalink
feat: local test server (#1354)
Browse files Browse the repository at this point in the history
* feat: local test server

* fix: disable web security for web workflow
  • Loading branch information
Gustl22 committed Dec 24, 2022
1 parent 7a10be3 commit 06be429
Show file tree
Hide file tree
Showing 26 changed files with 255 additions and 16 deletions.
26 changes: 20 additions & 6 deletions .github/workflows/build.yaml
Expand Up @@ -55,10 +55,14 @@ jobs:
chromedriver --port=4444 &
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional
( cd server; dart run bin/server.dart ) &
flutter drive \
--driver=test_driver/integration_test.dart \
--target=integration_test/app_test.dart \
-d web-server --web-browser-flag="--autoplay-policy=no-user-gesture-required"
-d web-server \
--web-browser-flag="--autoplay-policy=no-user-gesture-required" \
--web-browser-flag="--disable-web-security" \
--dart-define USE_LOCAL_SERVER=true
android:
runs-on: macOS-latest
Expand Down Expand Up @@ -109,7 +113,9 @@ jobs:
echo "Emulator started"
- name: Run Flutter integration tests
working-directory: ./packages/audioplayers/example
run: "flutter test integration_test"
run: |
( cd server; dart run bin/server.dart ) &
flutter test integration_test --dart-define USE_LOCAL_SERVER=true
ios:
runs-on: macOS-latest
Expand All @@ -136,7 +142,9 @@ jobs:
flutter build ios --release --no-codesign
- name: Run Flutter integration tests
working-directory: ./packages/audioplayers/example
run: "flutter test integration_test"
run: |
( cd server; dart run bin/server.dart ) &
flutter test integration_test --dart-define USE_LOCAL_SERVER=true
macos:
runs-on: macOS-latest
Expand All @@ -160,7 +168,9 @@ jobs:
flutter build macos --release
- name: Run Flutter integration tests
working-directory: ./packages/audioplayers/example
run: "flutter test -d macos integration_test"
run: |
( cd server; dart run bin/server.dart ) &
flutter test -d macos integration_test --dart-define USE_LOCAL_SERVER=true
windows:
runs-on: windows-latest
Expand All @@ -186,8 +196,11 @@ jobs:
Import-Certificate -FilePath Scream\Install\driver\x64\Scream.cat -CertStoreLocation Cert:\LocalMachine\TrustedPublisher
Scream\Install\helpers\devcon-x64.exe install Scream\Install\driver\x64\Scream.inf *Scream
- name: Run Flutter integration tests
shell: powershell
working-directory: ./packages/audioplayers/example
run: "flutter test -d windows integration_test"
run: |
Start-Process -NoNewWindow -WorkingDirectory "server" dart -ArgumentList "run", "bin/server.dart"
flutter test -d windows integration_test --dart-define USE_LOCAL_SERVER=true
linux:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -218,4 +231,5 @@ jobs:
run: |
export DISPLAY=:99
sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 &
flutter test -d linux integration_test
( cd server; dart run bin/server.dart ) &
flutter test -d linux integration_test --dart-define USE_LOCAL_SERVER=true
14 changes: 10 additions & 4 deletions packages/audioplayers/example/README.md
Expand Up @@ -4,8 +4,14 @@ This is an example usage of audioplayers plugin.

It's a simple app with three tabs.

- Remote Url: Plays audio from a remote url from the Internet.
- Local File: Downloads a file to your device in order to play it from your device.
- Local Asset: Play one of the assets bundled with this app.
- Remote Url: Plays audio from a remote url from the Internet.
- Local File: Downloads a file to your device in order to play it from your device.
- Local Asset: Play one of the assets bundled with this app.

This example bundles a `PlayerWidget` that could be used as a very simple audio player interface.
This example bundles a `PlayerWidget` that could be used as a very simple audio player interface.

## Dart Environment Variables

Set the following variables as additional args `--dart-define MY_VAR=xyz`:

- `USE_LOCAL_SERVER`: uses links to local server instead of public accessible links, default: `false`.
4 changes: 4 additions & 0 deletions packages/audioplayers/example/analysis_options.yaml
@@ -1 +1,5 @@
include: package:flame_lint/analysis_options.yaml

linter:
rules:
do_not_use_environment: false
Binary file not shown.
Binary file added packages/audioplayers/example/assets/coins.wav
Binary file not shown.
21 changes: 15 additions & 6 deletions packages/audioplayers/example/lib/tabs/sources.dart
@@ -1,18 +1,27 @@
import 'dart:io';

import 'package:audioplayers/audioplayers.dart';
import 'package:audioplayers_example/components/btn.dart';
import 'package:audioplayers_example/components/tab_wrapper.dart';
import 'package:audioplayers_example/components/tgl.dart';
import 'package:audioplayers_example/utils.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';

const _wavUrl1 = 'https://luan.xyz/files/audio/coins.wav';
const _wavUrl2 = 'https://luan.xyz/files/audio/laser.wav';
const _mp3Url1 = 'https://luan.xyz/files/audio/ambient_c_motion.mp3';
const _mp3Url2 = 'https://luan.xyz/files/audio/nasa_on_a_mission.mp3';
const _m3u8StreamUrl =
'https://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/nonuk/sbr_low/ak/bbc_radio_one.m3u8';
const useLocalServer = bool.fromEnvironment('USE_LOCAL_SERVER');

final localhost = kIsWeb || !Platform.isAndroid ? 'localhost' : '10.0.2.2';
final host = useLocalServer ? 'http://$localhost:8080' : 'https://luan.xyz';

final _wavUrl1 = '$host/files/audio/coins.wav';
final _wavUrl2 = '$host/files/audio/laser.wav';
final _mp3Url1 = '$host/files/audio/ambient_c_motion.mp3';
final _mp3Url2 = '$host/files/audio/nasa_on_a_mission.mp3';
final _m3u8StreamUrl = useLocalServer
? '$host/files/live_streams/nasa_power_of_the_rovers.m3u8'
: 'https://a.files.bbci.co.uk/media/live/manifesto/audio/simulcast/hls/nonuk/sbr_low/ak/bbc_radio_one.m3u8';
const _mpgaStreamUrl = 'https://timesradio.wireless.radio/stream';

const _asset1 = 'laser.wav';
Expand Down
14 changes: 14 additions & 0 deletions packages/audioplayers/example/server/.gitignore
@@ -0,0 +1,14 @@
# https://dart.dev/guides/libraries/private-files
.DS_Store
.atom/
.idea
.packages
.pub/
.dart_tool/
build/
ios/.generated/
packages
pubspec.lock
.flutter-plugins
flutter_export_environment.sh
.last_build_id
14 changes: 14 additions & 0 deletions packages/audioplayers/example/server/README.md
@@ -0,0 +1,14 @@
A simple HTTP server to provide audio sources using [package:shelf](https://pub.dev/packages/shelf).
This server listens to loop-back (localhost, 127.0.0.1).

To run this server locally, run as follows:

```bash
$ dart run bin/server.dart
```

Environment variables:

- `LATENCY`: the timeout until the server should respond in milliseconds, default: `0`.
- `PORT`: the port the server should listen on, default: `8080`.
- `LOG_REQUESTS`: log the network requests, default: `false`.
1 change: 1 addition & 0 deletions packages/audioplayers/example/server/analysis_options.yaml
@@ -0,0 +1 @@
include: package:flame_lint/analysis_options.yaml
51 changes: 51 additions & 0 deletions packages/audioplayers/example/server/bin/server.dart
@@ -0,0 +1,51 @@
// ignore_for_file: avoid_print
import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_static/shelf_static.dart' as shelf_static;

Future<void> main() async {
final port = int.parse(Platform.environment['PORT'] ?? '8080');
final requestTimeoutMillis =
int.parse(Platform.environment['LATENCY'] ?? '0');
final isLogRequests =
(Platform.environment['LOG_REQUESTS'] ?? 'false') == 'true';

final cascade = Cascade().add(_staticHandler);

var pipeline = const Pipeline();
if (isLogRequests) {
pipeline = pipeline.addMiddleware(logRequests());
}

final handler = pipeline
.addMiddleware(
(innerHandler) => (req) async {
await Future<void>.delayed(
Duration(milliseconds: requestTimeoutMillis),
);
return await innerHandler(req);
},
)
.addHandler(cascade.handler);

final server = await shelf_io.serve(
handler,
InternetAddress.loopbackIPv4,
port,
);

// TODO(Gustl22): provide an audio streaming endpoint:
// Inspiration: https://github.com/daspinola/video-stream-sample/blob/master/server.js

print(
'Serving at http://${server.address.host}:${server.port} with latency of $requestTimeoutMillis ms',
);
}

final _staticHandler = shelf_static.createStaticHandler(
'public',
defaultDocument: 'index.html',
serveFilesOutsidePath: true,
);
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/audioplayers/example/server/public/files/LICENSE
1 change: 1 addition & 0 deletions packages/audioplayers/example/server/public/files/audio
@@ -0,0 +1,25 @@
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-PLAYLIST-TYPE:VOD
#EXTINF:6.400000,
nasa_power_of_the_rovers0.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers1.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers2.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers3.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers4.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers5.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers6.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers7.ts
#EXTINF:6.400000,
nasa_power_of_the_rovers8.ts
#EXTINF:2.410311,
nasa_power_of_the_rovers9.ts
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
85 changes: 85 additions & 0 deletions packages/audioplayers/example/server/public/index.html
@@ -0,0 +1,85 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" sizes="64x64" href="favicon_64.png">
<title>Audioplayers Test Server</title>
</head>

<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
font-family: sans-serif;
min-height: 100vh;
margin: 0;
}

p {
margin-top: 1rem;
font-size: large;
max-width: 600px;
}

img {
max-width: 70vw;
}

code {
background: #eee;
padding: 3px;
}

.centerFlex {
display: flex;
align-items: center;
}

.centerFlex > * {
padding: 10px;
}

#content {
flex: 1;
}

footer {
background: lightgrey;
width: 100%;
justify-content: center;
align-items: center;
display: flex;
}
</style>

<body>
<div id="content">

<div class="centerFlex">
<img src="/favicon_64.png" alt="Favicon">
<h1>Audioplayers Test Server</h1>
</div>
<p>This server provides audio files and streams to test <a
href="https://pub.dev/packages/audioplayers">Audioplayers</a>
package:</p>
<ul>
<li><a href="/files/audio/laser.wav">/files/audio/ambient_c_motion.mp3</a></li>
<li><a href="/files/audio/coins.wav">/files/audio/coins.wav</a></li>
<li><a href="/files/audio/laser.wav">/files/audio/laser.wav</a></li>
<li><a href="/files/audio/nasa_on_a_mission.mp3">/files/audio/nasa_on_a_mission.mp3</a></li>
<li>
<a href="/files/live_streams/nasa_power_of_the_rovers.m3u8">/files/live_streams/nasa_power_of_the_rovers.m3u8</a>
</li>
</ul>
</div>
<footer>
<a href="/files/LICENSE">License</a>
</footer>
</body>

</html>
14 changes: 14 additions & 0 deletions packages/audioplayers/example/server/pubspec.yaml
@@ -0,0 +1,14 @@
name: audioplayers_test_server
publish_to: none

environment:
sdk: ">=2.12.0 <3.0.0"

dependencies:
shelf: ^1.2.0
shelf_router: ^1.0.0
shelf_static: ^1.0.0

dev_dependencies:
flame_lint: ^0.1.0
http: ^0.13.0

0 comments on commit 06be429

Please sign in to comment.