1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
import 'dart:async';
import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:video_player/video_player.dart';
import '../api/player_api_client.dart';
import '../providers/api_client_provider.dart';
// How often progress updates are emitted to the server while playing.
const _kProgressInterval = Duration(seconds: 5);
// Playback fraction at which the item is considered finished (95 %).
const _kFinishedThreshold = 0.95;
// ---------------------------------------------------------------------------
// VideoPlayerScreen
// ---------------------------------------------------------------------------
/// Full-screen video player that streams from `/api/v1/media/{id}/stream`.
///
/// Design decisions:
/// - [ConsumerStatefulWidget] gives access to Riverpod providers while
/// holding the mutable controller state in [State].
/// - Bearer token is attached via `httpHeaders` on [VideoPlayerController]
/// so the native platform layer (ExoPlayer / AVPlayer) can authenticate
/// directly without routing bytes through Dart.
/// - Progress updates (every [_kProgressInterval]) and the finished mark
/// are fire-and-forget: errors are swallowed silently so a transient
/// network blip never interrupts playback.
/// - Both controllers are disposed in [dispose] to prevent resource leaks.
/// - All async continuations guard on [mounted] before calling [setState].
class VideoPlayerScreen extends ConsumerStatefulWidget {
const VideoPlayerScreen({
super.key,
required this.mediaId,
this.mediaUrl,
});
/// The media item identifier extracted from the '/video/:mediaId' route path.
final String mediaId;
/// The resolved HLS/direct stream URL, optionally provided as route extra.
/// When null, [PlayerApiClient.streamUrl] is called to derive the URL so the
/// base URL stays in a single place (Dependency Inversion Principle).
final String? mediaUrl;
@override
ConsumerState<VideoPlayerScreen> createState() => _VideoPlayerScreenState();
}
// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------
class _VideoPlayerScreenState extends ConsumerState<VideoPlayerScreen> {
// Nullable until initialisation completes (or fails).
VideoPlayerController? _videoController;
ChewieController? _chewieController;
// Non-null when initialisation failed; shown in the error view.
String? _error;
// True while the controllers are being set up; shows a full-screen spinner.
bool _isLoading = true;
// Prevents emitting a "finished" update more than once per playback session.
bool _finishedEmitted = false;
// Periodic timer that fires every [_kProgressInterval] while playing.
Timer? _progressTimer;
// ---------------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------------
@override
void initState() {
super.initState();
// Defer initialisation so all Riverpod provider overrides are applied
// before we read from [ref] (important for widget tests).
WidgetsBinding.instance.addPostFrameCallback((_) => _initPlayer());
}
@override
void dispose() {
_progressTimer?.cancel();
_chewieController?.dispose();
_videoController?.dispose();
super.dispose();
}
// ---------------------------------------------------------------------------
// Player initialisation
// ---------------------------------------------------------------------------
/// Initialises [VideoPlayerController] and [ChewieController].
///
/// Steps:
/// 1. Resolve the stream URL (from route extra or [PlayerApiClient]).
/// 2. Read the bearer token for the `Authorization` header.
/// 3. Create and initialise [VideoPlayerController.networkUrl].
/// 4. Fetch the saved position via [getMediaProgress] and seek to it.
/// 5. Wrap in [ChewieController] and start the progress ticker.
Future<void> _initPlayer() async {
if (!mounted) return;
final client = ref.read(apiClientProvider);
final storage = ref.read(tokenStorageProvider);
final mediaIdInt = int.tryParse(widget.mediaId) ?? 0;
// Step 1: resolve the stream URL — prefer the route-extra URL so the
// calling screen can forward a pre-computed URL; fall back to streamUrl.
final url = widget.mediaUrl ?? client.streamUrl(mediaIdInt);
// Step 2: read the bearer token so the native player can authenticate
// without routing bytes through Dart (performance and correctness).
final token = await storage.readToken();
if (!mounted) return;
final headers = <String, String>{
if (token != null && token.isNotEmpty) 'Authorization': 'Bearer $token',
};
// Step 3: create and initialise the VideoPlayerController.
VideoPlayerController videoController;
try {
videoController = VideoPlayerController.networkUrl(
Uri.parse(url),
httpHeaders: headers,
);
await videoController.initialize();
} catch (e) {
if (!mounted) return;
setState(() {
_error = _initErrorMessage(e);
_isLoading = false;
});
return;
}
if (!mounted) {
videoController.dispose();
return;
}
// Step 4: resume from the server-saved position (best-effort; ignore
// errors so a missing progress row never blocks playback).
try {
final savedSeconds = await client.getMediaProgress(mediaIdInt);
if (savedSeconds != null && savedSeconds > 0) {
await videoController.seekTo(
Duration(milliseconds: (savedSeconds * 1000).round()),
);
}
} catch (_) {
// Progress fetch failure is non-fatal; start from the beginning.
}
if (!mounted) {
videoController.dispose();
return;
}
// Step 5: wrap in ChewieController with sensible defaults for a
// distraction-free full-screen experience.
final chewieController = ChewieController(
videoPlayerController: videoController,
autoPlay: true,
looping: false,
allowFullScreen: true,
allowMuting: true,
showOptions: false,
);
setState(() {
_videoController = videoController;
_chewieController = chewieController;
_isLoading = false;
});
// Start the periodic progress ticker now that playback is ready.
_startProgressTicker(mediaIdInt, client);
}
// ---------------------------------------------------------------------------
// Progress reporting
// ---------------------------------------------------------------------------
/// Starts a periodic timer that emits progress updates every
/// [_kProgressInterval] and marks the item finished at [_kFinishedThreshold].
///
/// The [client] reference is captured once here so we avoid accessing [ref]
/// inside the timer callback after the widget may have been disposed.
void _startProgressTicker(int mediaId, PlayerApiClient client) {
_progressTimer = Timer.periodic(_kProgressInterval, (_) async {
final vc = _videoController;
if (vc == null) return;
// Skip network calls while paused — no progress to record and avoids
// unnecessary server traffic when the user has paused playback.
if (!vc.value.isPlaying) return;
final position = vc.value.position;
final duration = vc.value.duration;
// Emit raw position update — fire-and-forget so a transient network
// error never interrupts playback.
try {
await client.updateProgress(
mediaId: mediaId,
positionSeconds: position.inMilliseconds / 1000.0,
);
} catch (_) {}
// Mark finished once when playback fraction reaches the threshold.
// Guard with [_finishedEmitted] to avoid duplicate server calls.
if (!_finishedEmitted &&
duration.inMilliseconds > 0 &&
position.inMilliseconds / duration.inMilliseconds >=
_kFinishedThreshold) {
_finishedEmitted = true;
try {
await client.updateProgressStatus(
mediaId: mediaId,
status: 'finished',
);
} catch (_) {}
}
});
}
// ---------------------------------------------------------------------------
// Error mapping
// ---------------------------------------------------------------------------
/// Converts a controller initialisation exception to a readable UI string.
///
/// Kept in the state class because it is tightly coupled to this screen's
/// error UI — no general-purpose helper needed (YAGNI).
String _initErrorMessage(Object e) {
final detail = e.toString();
if (detail.isNotEmpty && detail != 'null') {
return 'Playback failed: $detail';
}
return 'Could not start video playback. Please try again.';
}
// ---------------------------------------------------------------------------
// Build
// ---------------------------------------------------------------------------
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
backgroundColor: Colors.black,
foregroundColor: Colors.white,
title: Text('Video – ${widget.mediaId}'),
),
body: _buildBody(),
);
}
/// Selects the appropriate body widget based on current state.
Widget _buildBody() {
if (_isLoading) return _buildLoadingView();
if (_error != null) return _buildErrorView(_error!);
return _buildPlayerView();
}
/// Full-screen loading spinner shown while the player initialises.
Widget _buildLoadingView() {
return const Center(
key: Key('video_player_loading'),
child: CircularProgressIndicator(),
);
}
/// Error view shown when initialisation fails.
///
/// Provides a human-readable message and a retry button so the user can
/// attempt re-initialisation without navigating away.
Widget _buildErrorView(String message) {
return Center(
key: const Key('video_player_error'),
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(Icons.error_outline, color: Colors.white70, size: 64),
const SizedBox(height: 16),
Text(
message,
style: const TextStyle(color: Colors.white70),
textAlign: TextAlign.center,
key: const Key('video_player_error_message'),
),
const SizedBox(height: 24),
ElevatedButton(
key: const Key('video_player_retry'),
onPressed: _onRetry,
child: const Text('Retry'),
),
],
),
),
);
}
/// The Chewie player widget that fills the available space.
Widget _buildPlayerView() {
return Center(
key: const Key('video_player_chewie'),
child: AspectRatio(
aspectRatio: _videoController!.value.aspectRatio,
child: Chewie(controller: _chewieController!),
),
);
}
// ---------------------------------------------------------------------------
// Actions
// ---------------------------------------------------------------------------
/// Tears down current controllers and re-runs [_initPlayer].
///
/// Extracted to keep [_buildErrorView] below 30 lines (style guideline).
void _onRetry() {
_progressTimer?.cancel();
_chewieController?.dispose();
_videoController?.dispose();
setState(() {
_chewieController = null;
_videoController = null;
_error = null;
_isLoading = true;
_finishedEmitted = false;
});
_initPlayer();
}
}
|