Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Navaron Bracke
2023-12-07 14:15:38 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
c4012192cda06713fb29176081dc2660bf3e1bb6
c4012192
1 parent
bf7bfdee
reimplement flashlight delegate & use media capabilities which is more supported
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
lib/src/web/flashlight_delegate.dart
lib/src/web/flashlight_delegate.dart
0 → 100644
View file @
c401219
import
'dart:js_interop'
;
import
'package:mobile_scanner/src/enums/torch_state.dart'
;
import
'package:web/web.dart'
;
/// This class represents a flashlight delegate for the web platform.
///
/// It provides an interface to query and update the flashlight state of a [MediaStream].
final
class
FlashlightDelegate
{
/// Returns a list of supported flashlight modes for the given [mediaStream].
///
/// The [TorchState.off] mode is always supported, regardless of the return value.
Future
<
List
<
TorchState
>>
getSupportedFlashlightModes
(
MediaStream
?
mediaStream
)
async
{
if
(
mediaStream
==
null
)
{
return
[];
}
final
List
<
JSAny
?>
tracks
=
mediaStream
.
getVideoTracks
().
toDart
;
if
(
tracks
.
isEmpty
)
{
return
[];
}
final
MediaStreamTrack
?
track
=
tracks
.
first
as
MediaStreamTrack
?;
if
(
track
==
null
)
{
return
[];
}
try
{
final
MediaTrackCapabilities
capabilities
=
track
.
getCapabilities
();
return
[
if
(
capabilities
.
torch
)
TorchState
.
on
,
];
}
catch
(
_
)
{
// Firefox does not support getCapabilities() yet.
// https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/getCapabilities#browser_compatibility
return
[];
}
}
/// Returns whether the given [mediaStream] has a flashlight.
Future
<
bool
>
hasFlashlight
(
MediaStream
?
mediaStream
)
async
{
return
(
await
getSupportedFlashlightModes
(
mediaStream
)).
isNotEmpty
;
}
/// Set the flashlight state of the given [mediaStream] to the given [value].
Future
<
void
>
setFlashlightState
(
MediaStream
?
mediaStream
,
TorchState
value
)
async
{
if
(
mediaStream
==
null
)
{
return
;
}
if
(
await
hasFlashlight
(
mediaStream
))
{
final
List
<
JSAny
?>
tracks
=
mediaStream
.
getVideoTracks
().
toDart
;
if
(
tracks
.
isEmpty
)
{
return
;
}
final
bool
flashlightEnabled
=
switch
(
value
)
{
TorchState
.
on
=>
true
,
TorchState
.
off
||
TorchState
.
unavailable
=>
false
,
};
final
MediaStreamTrack
?
track
=
tracks
.
first
as
MediaStreamTrack
?;
final
MediaTrackConstraints
constraints
=
MediaTrackConstraints
(
advanced:
[
{
'torch'
:
flashlightEnabled
}.
jsify
(),
].
toJS
,
);
await
track
?.
applyConstraints
(
constraints
).
toDart
;
}
}
}
...
...
Please
register
or
login
to post a comment