feat(recording): add safety limits and adaptive headers
This commit is contained in:
@@ -85,6 +85,8 @@ class ServicioGrabacionRadio {
|
||||
_reloj = reloj ?? DateTime.now;
|
||||
|
||||
static const _claveDirectorio = 'grabacion_radio_directorio';
|
||||
static const _claveMaxBytes = 'grabacion_radio_max_bytes_v1';
|
||||
static const int maxBytesPorDefecto = 500 * 1024 * 1024;
|
||||
|
||||
final http.Client? _clienteExterno;
|
||||
final Future<Directory> Function()? _resolverDirectorioBase;
|
||||
@@ -97,17 +99,21 @@ class ServicioGrabacionRadio {
|
||||
http.Client? _clienteActivo;
|
||||
Timer? _timerAutoStop;
|
||||
String? _directorioConfigurado;
|
||||
int _maxBytes = maxBytesPorDefecto;
|
||||
|
||||
EstadoGrabacionRadio get estado => _estado;
|
||||
Stream<EstadoGrabacionRadio> get estadoStream => _estadoController.stream;
|
||||
String? get directorioConfigurado => _directorioConfigurado;
|
||||
int get maxBytes => _maxBytes;
|
||||
|
||||
Future<void> inicializar() async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
_directorioConfigurado = prefs.getString(_claveDirectorio);
|
||||
_maxBytes = prefs.getInt(_claveMaxBytes) ?? maxBytesPorDefecto;
|
||||
} catch (_) {
|
||||
_directorioConfigurado = null;
|
||||
_maxBytes = maxBytesPorDefecto;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,6 +151,18 @@ class ServicioGrabacionRadio {
|
||||
_emitir(_estado);
|
||||
}
|
||||
|
||||
Future<void> guardarMaxBytes(int bytes) async {
|
||||
if (bytes <= 0) {
|
||||
throw ArgumentError('El tamaño máximo debe ser mayor que cero');
|
||||
}
|
||||
_maxBytes = bytes;
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setInt(_claveMaxBytes, bytes);
|
||||
} catch (_) {}
|
||||
_emitir(_estado);
|
||||
}
|
||||
|
||||
Future<void> iniciar(
|
||||
Emisora emisora, {
|
||||
Duration? duracion,
|
||||
@@ -199,7 +217,11 @@ class ServicioGrabacionRadio {
|
||||
_subscripcionStream = response.stream.listen(
|
||||
(chunk) {
|
||||
_sink?.add(chunk);
|
||||
_emitir(_estado.copyWith(bytes: _estado.bytes + chunk.length));
|
||||
final nuevosBytes = _estado.bytes + chunk.length;
|
||||
_emitir(_estado.copyWith(bytes: nuevosBytes));
|
||||
if (nuevosBytes >= _maxBytes) {
|
||||
unawaited(detener());
|
||||
}
|
||||
},
|
||||
onDone: () => unawaited(_finalizar()),
|
||||
onError: (Object error) => unawaited(_fallar(error)),
|
||||
|
||||
Reference in New Issue
Block a user