Skip to content

Commit f0a542e

Browse files
committed
fix: don't try to access optional track settings
Some properties defined in `package:web/web.dart` are defined as non-nullable, while some browsers don't support them. In my case, `aspectRatio` is not defined for [Firefox](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings/aspectRatio#browser_compatibility). This seems to be a problem with the IDL itself being inconsistent (see dart-lang/web#309). Checking availability before accessing the fields seems to be an endorsed method: dart-lang/web#181 (comment)
1 parent 4f06b3a commit f0a542e

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

lib/src/media_stream_track_impl.dart

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22
import 'dart:js_interop';
3+
import 'dart:js_interop_unsafe';
34
import 'dart:js_util' as js;
45
import 'dart:typed_data';
56

@@ -81,25 +82,49 @@ class MediaStreamTrackWeb extends MediaStreamTrack {
8182
var settings = jsTrack.getSettings();
8283
var _converted = <String, dynamic>{};
8384
if (kind == 'audio') {
84-
_converted['sampleRate'] = settings.sampleRate;
85-
_converted['sampleSize'] = settings.sampleSize;
86-
_converted['echoCancellation'] = settings.echoCancellation;
87-
_converted['autoGainControl'] = settings.autoGainControl;
88-
_converted['noiseSuppression'] = settings.noiseSuppression;
89-
_converted['latency'] = settings.latency;
90-
_converted['channelCount'] = settings.channelCount;
85+
if (settings.has('sampleRate')) {
86+
_converted['sampleRate'] = settings.sampleRate;
87+
}
88+
if (settings.has('sampleSize')) {
89+
_converted['sampleSize'] = settings.sampleSize;
90+
}
91+
if (settings.has('echoCancellation')) {
92+
_converted['echoCancellation'] = settings.echoCancellation;
93+
}
94+
if (settings.has('autoGainControl')) {
95+
_converted['autoGainControl'] = settings.autoGainControl;
96+
}
97+
if (settings.has('noiseSuppression')) {
98+
_converted['noiseSuppression'] = settings.noiseSuppression;
99+
}
100+
if (settings.has('latency')) _converted['latency'] = settings.latency;
101+
if (settings.has('channelCount')) {
102+
_converted['channelCount'] = settings.channelCount;
103+
}
91104
} else {
92-
_converted['width'] = settings.width;
93-
_converted['height'] = settings.height;
94-
_converted['aspectRatio'] = settings.aspectRatio;
95-
_converted['frameRate'] = settings.frameRate;
96-
if (isMobile) {
105+
if (settings.has('width')) {
106+
_converted['width'] = settings.width;
107+
}
108+
if (settings.has('height')) {
109+
_converted['height'] = settings.height;
110+
}
111+
if (settings.has('aspectRatio')) {
112+
_converted['aspectRatio'] = settings.aspectRatio;
113+
}
114+
if (settings.has('frameRate')) {
115+
_converted['frameRate'] = settings.frameRate;
116+
}
117+
if (isMobile && settings.has('facingMode')) {
97118
_converted['facingMode'] = settings.facingMode;
98119
}
99-
_converted['resizeMode'] = settings.resizeMode;
120+
if (settings.has('resizeMode')) {
121+
_converted['resizeMode'] = settings.resizeMode;
122+
}
123+
}
124+
if (settings.has('deviceId')) _converted['deviceId'] = settings.deviceId;
125+
if (settings.has('groupId')) {
126+
_converted['groupId'] = settings.groupId;
100127
}
101-
_converted['deviceId'] = settings.deviceId;
102-
_converted['groupId'] = settings.groupId;
103128
return _converted;
104129
}
105130

0 commit comments

Comments
 (0)