Skip to content

Commit

Permalink
fix(web): Honor given encoder. This should fix Safari issues with AAC…
Browse files Browse the repository at this point in the history
… encoding.

fixes #388, fixes #376
  • Loading branch information
llfbandit committed Aug 27, 2024
1 parent 96240e3 commit 8ffff60
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
3 changes: 3 additions & 0 deletions record_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.1.3
* fix: Honor given encoder. This should fix Safari issues with AAC encoding.

## 1.1.2
* chore: Allow package:web ">=0.5.1 <2.0.0".

Expand Down
10 changes: 5 additions & 5 deletions record_web/lib/mime_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import 'package:record_platform_interface/record_platform_interface.dart';

const mimeTypes = {
// We apply same mime types for different encoding types for AAC
AudioEncoder.aacLc: ['audio/aac', 'audio/mp4'],
AudioEncoder.aacEld: ['audio/aac', 'audio/mp4'],
AudioEncoder.aacHe: ['audio/aac', 'audio/mp4'],
AudioEncoder.aacLc: ['audio/mp4', 'audio/aac'],
AudioEncoder.aacEld: ['audio/mp4', 'audio/aac'],
AudioEncoder.aacHe: ['audio/mp4', 'audio/aac'],

AudioEncoder.amrNb: ['audio/AMR'],
AudioEncoder.amrWb: ['audio/AMR-WB'],

AudioEncoder.opus: [
'audio/opus',
'audio/opus; codecs=opus',
'audio/webm; codecs=opus',
'audio/opus; codecs=opus',
'audio/opus',
],

AudioEncoder.flac: ['audio/flac', 'audio/x-flac'],
Expand Down
31 changes: 20 additions & 11 deletions record_web/lib/recorder/delegate/media_recorder_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class MediaRecorderDelegate extends RecorderDelegate {
web.AnalyserNode? _analyser;

final OnStateChanged onStateChanged;

final _defaultMimeType = 'audio/webm;codecs=opus';
RecordConfig? _config;

MediaRecorderDelegate({required this.onStateChanged}) {
ImportJsLibrary().import(
Expand Down Expand Up @@ -99,15 +98,17 @@ class MediaRecorderDelegate extends RecorderDelegate {
final mediaStream = await initMediaStream(config);

// Try to assign dedicated mime type.
// If contrainst isn't set, browser will record with its default codec.
final mimeType = getSupportedMimeType(config.encoder);
if (mimeType == null) {
throw '${config.encoder} not supported.';
}

final mediaRecorder = web.MediaRecorder(
mediaStream,
web.MediaRecorderOptions(
audioBitsPerSecond: config.bitRate,
bitsPerSecond: config.bitRate,
mimeType: mimeType ?? _defaultMimeType,
mimeType: mimeType,
),
);
mediaRecorder.ondataavailable =
Expand All @@ -122,6 +123,7 @@ class MediaRecorderDelegate extends RecorderDelegate {

_mediaRecorder = mediaRecorder;
_mediaStream = mediaStream;
_config = config;

onStateChanged(RecordState.record);
} catch (error) {
Expand Down Expand Up @@ -185,13 +187,19 @@ class MediaRecorderDelegate extends RecorderDelegate {
if (_chunks.isNotEmpty) {
_elapsedTime.stop();

final blob = await fixWebmDuration(
web.Blob(
_chunks.toJS,
web.BlobPropertyBag(type: _defaultMimeType),
),
_elapsedTime.elapsedMilliseconds.toJS,
).toDart;
final blob = switch (_config!.encoder) {
AudioEncoder.opus => await fixWebmDuration(
web.Blob(
_chunks.toJS,
web.BlobPropertyBag(type: _mediaRecorder!.mimeType),
),
_elapsedTime.elapsedMilliseconds.toJS,
).toDart,
_ => web.Blob(
_chunks.toJS,
web.BlobPropertyBag(type: _mediaRecorder!.mimeType),
),
};

audioUrl = web.URL.createObjectURL(blob);
}
Expand Down Expand Up @@ -219,6 +227,7 @@ class MediaRecorderDelegate extends RecorderDelegate {
_analyser = null;

_chunks = [];
_config = null;
}

void _createAudioContext(RecordConfig config, web.MediaStream stream) {
Expand Down
2 changes: 1 addition & 1 deletion record_web/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: record_web
description: Web specific implementation for record package called by record_platform_interface.
version: 1.1.2
version: 1.1.3
homepage: https://github.com/llfbandit/record/tree/master/record_web

environment:
Expand Down

0 comments on commit 8ffff60

Please sign in to comment.