Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set default subtitle from hls #688

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/src/asms/better_player_asms_subtitle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class BetterPlayerAsmsSubtitle {
///List of subtitle segments. Only used when [isSegmented] is true.
final List<BetterPlayerAsmsSubtitleSegment>? segments;

///If the subtitle is the default
final bool? isDefault;

BetterPlayerAsmsSubtitle({
this.language,
this.name,
Expand All @@ -40,5 +43,6 @@ class BetterPlayerAsmsSubtitle {
this.isSegmented,
this.segmentsTime,
this.segments,
this.isDefault,
});
}
1 change: 1 addition & 0 deletions lib/src/core/better_player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ class BetterPlayerController {
asmsIsSegmented: asmsSubtitle.isSegmented,
asmsSegmentsTime: asmsSubtitle.segmentsTime,
asmsSegments: asmsSubtitle.segments,
selectedByDefault: asmsSubtitle.isDefault,
),
);
});
Expand Down
8 changes: 8 additions & 0 deletions lib/src/hls/better_player_hls_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:better_player/src/hls/hls_parser/hls_media_playlist.dart';
import 'package:better_player/src/hls/hls_parser/hls_playlist_parser.dart';
import 'package:better_player/src/hls/hls_parser/rendition.dart';
import 'package:better_player/src/hls/hls_parser/segment.dart';
import 'package:better_player/src/hls/hls_parser/util.dart';

///HLS helper class
class BetterPlayerHlsUtils {
Expand Down Expand Up @@ -137,6 +138,12 @@ class BetterPlayerHlsUtils {
targetDuration = parsedSubtitle.targetDurationUs! ~/ 1000;
}

bool isDefault = false;

if(rendition.format.selectionFlags != null) {
isDefault = Util.checkBitPositionIsSet(rendition.format.selectionFlags!, 1);
siloebb marked this conversation as resolved.
Show resolved Hide resolved
}

return BetterPlayerAsmsSubtitle(
name: rendition.format.label,
language: rendition.format.language,
Expand All @@ -145,6 +152,7 @@ class BetterPlayerHlsUtils {
isSegmented: isSegmented,
segmentsTime: targetDuration,
segments: asmsSegments,
isDefault: isDefault
);
} catch (exception) {
BetterPlayerUtils.log("Failed to process subtitles playlist: $exception");
Expand Down
9 changes: 8 additions & 1 deletion lib/src/hls/hls_parser/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Format {
this.channelCount,
String? language,
this.accessibilityChannel,
this.isDefault,
}) : language = language?.toLowerCase();

factory Format.createVideoContainerFormat(
Expand All @@ -37,7 +38,9 @@ class Format {
required int? height,
required double? frameRate,
int selectionFlags = Util.selectionFlagDefault,
int? roleFlags}) =>
int? roleFlags,
bool? isDefault,
}) =>
Format(
id: id,
label: label,
Expand All @@ -51,6 +54,7 @@ class Format {
height: height,
frameRate: frameRate,
roleFlags: roleFlags,
isDefault: isDefault,
);

/// An identifier for the format, or null if unknown or not applicable.
Expand Down Expand Up @@ -111,6 +115,9 @@ class Format {
/// The Accessibility channel, or null if not known or applicable.
final int? accessibilityChannel;

/// If track is marked as default, or null if not known or applicable
final bool? isDefault;

Format copyWithMetadata(Metadata metadata) => Format(
id: id,
label: label,
Expand Down
9 changes: 7 additions & 2 deletions lib/src/hls/hls_parser/hls_playlist_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ class HlsPlaylistParser {

static int _parseSelectionFlags(String line) {
int flags = 0;

if (parseOptionalBooleanAttribute(
line: line,
pattern: regexpDefault,
Expand All @@ -622,8 +623,12 @@ class HlsPlaylistParser {
required String pattern,
required bool defaultValue,
}) {
final List<Match> list = line.allMatches(pattern).toList();
return list.isEmpty ? defaultValue : list.first.pattern == booleanTrue;
final regExp = RegExp(pattern);
siloebb marked this conversation as resolved.
Show resolved Hide resolved
final List<Match> list = regExp.allMatches(line).toList();
final ret = list.isEmpty
? defaultValue
: line.substring(list.first.start, list.first.end).contains(booleanTrue);
return ret;
}

static int _parseRoleFlags(
Expand Down
7 changes: 7 additions & 0 deletions lib/src/hls/hls_parser/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ class Util {
static List<String> splitCodecs(String? codecs) => codecs?.isNotEmpty != true
? <String>[]
: codecs!.trim().split(RegExp('(\\s*,\\s*)'));

static bool checkBitPositionIsSet(int number, int bitPosition) {
if ((number & (1 << (bitPosition - 1))) > 0)
return true;
else
return false;
}
}

class CencType {
Expand Down