[go: nahoru, domu]

Skip to content

Commit

Permalink
fix: Quick creation/start/stop sequence on Windows resulting in a crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
llfbandit committed Dec 17, 2023
1 parent 94793c2 commit a015d01
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 39 deletions.
3 changes: 3 additions & 0 deletions record/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.0.2
* fix: Creation sequence which may lead to unexpected behaviours.

## 5.0.1
* fix: close state stream controller on dispose.

Expand Down
53 changes: 23 additions & 30 deletions record/lib/src/record.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,23 @@ class AudioRecorder {
Timer? _amplitudeTimer;
late Duration _amplitudeTimerInterval;

// Recorder ID
final String _recorderId;
// Flag to create the recorder if needed with `_recorderId`.
bool? _created;

/// Completer to wait until the native player and its event stream are
/// created.
final _createCompleter = Completer<void>();
AudioRecorder() : _recorderId = _uuid.v4();

AudioRecorder() : _recorderId = _uuid.v4() {
_create();
}

Future<void> _create() async {
try {
await RecordPlatform.instance.create(_recorderId);
Future<bool> _create() async {
await RecordPlatform.instance.create(_recorderId);

final stream = RecordPlatform.instance.onStateChanged(_recorderId);
_stateStreamSubscription = stream.listen(
_stateStreamCtrl.add,
onError: _stateStreamCtrl.addError,
);
final stream = RecordPlatform.instance.onStateChanged(_recorderId);
_stateStreamSubscription = stream.listen(
_stateStreamCtrl.add,
onError: _stateStreamCtrl.addError,
);

_createCompleter.complete();
} catch (e, stackTrace) {
_createCompleter.completeError(e, stackTrace);
}
return true;
}

/// Starts new recording session.
Expand All @@ -55,7 +48,7 @@ class AudioRecorder {
RecordConfig config, {
required String path,
}) async {
await _createCompleter.future;
_created ??= await _create();

await RecordPlatform.instance.start(_recorderId, config, path: path);

Expand All @@ -67,7 +60,7 @@ class AudioRecorder {
/// When stopping the record, you must rely on stream close event to get
/// full recorded data.
Future<Stream<Uint8List>> startStream(RecordConfig config) async {
await _createCompleter.future;
_created ??= await _create();
await _stopRecordStream();

final stream = await RecordPlatform.instance.startStream(
Expand Down Expand Up @@ -95,7 +88,7 @@ class AudioRecorder {
///
/// Returns the output path if any.
Future<String?> stop() async {
await _createCompleter.future;
_created ??= await _create();
_amplitudeTimer?.cancel();

final path = await RecordPlatform.instance.stop(_recorderId);
Expand All @@ -107,34 +100,34 @@ class AudioRecorder {

/// Pauses recording session.
Future<void> pause() async {
await _createCompleter.future;
_created ??= await _create();
_amplitudeTimer?.cancel();
return RecordPlatform.instance.pause(_recorderId);
}

/// Resumes recording session after [pause].
Future<void> resume() async {
await _createCompleter.future;
_created ??= await _create();
_startAmplitudeTimer();
return RecordPlatform.instance.resume(_recorderId);
}

/// Checks if there's valid recording session.
/// So if session is paused, this method will still return [true].
Future<bool> isRecording() async {
await _createCompleter.future;
_created ??= await _create();
return RecordPlatform.instance.isRecording(_recorderId);
}

/// Checks if recording session is paused.
Future<bool> isPaused() async {
await _createCompleter.future;
_created ??= await _create();
return RecordPlatform.instance.isPaused(_recorderId);
}

/// Checks and requests for audio record permission.
Future<bool> hasPermission() async {
await _createCompleter.future;
_created ??= await _create();
return RecordPlatform.instance.hasPermission(_recorderId);
}

Expand All @@ -145,20 +138,20 @@ class AudioRecorder {
/// On web, and in general, you should already have permission before
/// accessing this method otherwise the list may return an empty list.
Future<List<InputDevice>> listInputDevices() async {
await _createCompleter.future;
_created ??= await _create();
return RecordPlatform.instance.listInputDevices(_recorderId);
}

/// Gets current average & max amplitudes (dBFS)
/// Always returns zeros on unsupported platforms
Future<Amplitude> getAmplitude() async {
await _createCompleter.future;
_created ??= await _create();
return RecordPlatform.instance.getAmplitude(_recorderId);
}

/// Checks if the given encoder is supported on the current platform.
Future<bool> isEncoderSupported(AudioEncoder encoder) async {
await _createCompleter.future;
_created ??= await _create();
return RecordPlatform.instance.isEncoderSupported(_recorderId, encoder);
}

Expand Down
13 changes: 7 additions & 6 deletions record/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: record
description: Audio recorder from microphone to a given file path with multiple codecs, bit rate and sampling rate options.
version: 5.0.1
description: Audio recorder from microphone to file or stream with multiple codecs, bit rate and sampling rate options.
version: 5.0.2
homepage: https://github.com/llfbandit/record/tree/master/record

environment:
Expand All @@ -11,14 +11,15 @@ dependencies:
flutter:
sdk: flutter

# https://pub.dev/packages/uuid
uuid: ">=3.0.7 <5.0.0"

record_platform_interface: ^1.0.2
record_web: ^1.0.0
record_windows: ^1.0.0
record_web: ^1.0.4
record_windows: ^1.0.2
record_linux: '>=0.5.0 <1.0.0'
record_android: ^1.0.0
record_darwin: ^1.0.0
record_android: ^1.0.4
record_darwin: ^1.0.1

dev_dependencies:
# https://pub.dev/packages/flutter_lints
Expand Down
3 changes: 3 additions & 0 deletions record_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.2
* fix: Quick creation/start/stop sequence resulting in a crash.

## 1.0.1
* fix: UTF-16 to UTF-8 could fail.

Expand Down
2 changes: 1 addition & 1 deletion record_windows/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: record_windows
description: Windows specific implementation for record package called by record_platform_interface.
version: 1.0.1
version: 1.0.2
homepage: https://github.com/llfbandit/record/tree/master/record_windows

environment:
Expand Down
4 changes: 3 additions & 1 deletion record_windows/windows/record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ namespace record_windows
{
HRESULT hr = S_OK;

// Release reader callback first
SafeRelease(m_pReader);

if (m_pSource)
{
hr = m_pSource->Stop();
Expand Down Expand Up @@ -282,7 +285,6 @@ namespace record_windows

SafeRelease(m_pSource);
SafeRelease(m_pPresentationDescriptor);
SafeRelease(m_pReader);
SafeRelease(m_pWriter);
SafeRelease(m_pMediaType);
m_pConfig = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion record_windows/windows/record_readercallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace record_windows
else
{
// Reader error.
auto errorText = std::system_category().message(hr);
auto errorText = std::system_category().message(hrStatus);
printf("Record: Error when reading sample (0x%X)\n%s\n", hrStatus, errorText.c_str());

Stop();
Expand Down

0 comments on commit a015d01

Please sign in to comment.