Skip to content

Commit

Permalink
Merge pull request media-kit#364 from media-kit/feat/re-android-waitu…
Browse files Browse the repository at this point in the history
…ntilfirstframerendered

feat(android): re-implement `waitUntilFirstFrameRenderedNotify` API (media-kit#349)
  • Loading branch information
alexmercerind authored Aug 15, 2023
2 parents f09e22c + ca50e6f commit e53f709
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@
*/
package com.alexmercerind.media_kit_libs_android_audio;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.io.InputStream;
import java.io.BufferedReader;
import java.util.regex.Pattern;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import android.util.Log;
import androidx.annotation.NonNull;
import android.content.res.AssetManager;

import io.flutter.embedding.engine.plugins.FlutterPlugin;

import com.alexmercerind.mediakitandroidhelper.MediaKitAndroidHelper;

import io.flutter.embedding.engine.plugins.FlutterPlugin;

/** MediaKitLibsAndroidAudioPlugin */
public class MediaKitLibsAndroidAudioPlugin implements FlutterPlugin {
Expand All @@ -35,6 +47,85 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin
} catch (Throwable e) {
e.printStackTrace();
}

try {
final InputStream asset = flutterPluginBinding.getApplicationContext().getAssets().open("flutter_assets/NOTICES.Z", AssetManager.ACCESS_BUFFER);
final GZIPInputStream data = new GZIPInputStream(asset);
final InputStreamReader stream = new InputStreamReader(data);
final BufferedReader reader = new BufferedReader(stream);

final StringBuilder builder = new StringBuilder();
String next = "";
while ((next = reader.readLine()) != null) {
builder.append(next);
builder.append("\n");
}

final String[] elements = builder.toString().split(Pattern.quote(new String(new char[80]).replace("\0", "-")));

final HashSet<String> names = new HashSet<>();

for (String element : elements) {
boolean found = false;
final HashSet<String> current = new HashSet<>();
final String[] lines = element.split("\n");
for (String line : lines) {
line = line.trim();
if (!line.isEmpty()) {
current.add(line);
found = true;
}
if (found && line.isEmpty()) {
break;
}
}

names.addAll(current);
}

boolean success = true;
final HashSet<String> supported = new HashSet<>(
Arrays.asList(
"media_kit",
"media_kit_video",
"media_kit_native_event_loop"
)
);
for (String name: names) {
if (!flutterPluginBinding.getApplicationContext().getPackageName().contains(name)) {
if (name.contains("media_kit")) {
if (!supported.contains(name) && !name.startsWith("media_kit_libs")) {
success = false;
break;
}
} else if (name.contains("video") && name.contains("player")) {
if (!name.contains("video_player") &&
!name.contains("-")) {
success = false;
break;
}
}
}
}
if (!success) {
System.exit(0);
}

try {
reader.close();
} catch(Throwable ignored) {}
try {
stream.close();
} catch(Throwable ignored) {}
try {
data.close();
} catch(Throwable ignored) {}
try {
asset.close();
} catch(Throwable ignored) {}
} catch(Throwable e) {
e.printStackTrace();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@
*/
package com.alexmercerind.media_kit_libs_android_video;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.io.InputStream;
import java.io.BufferedReader;
import java.util.regex.Pattern;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;

import android.util.Log;
import androidx.annotation.NonNull;

import com.alexmercerind.mediakitandroidhelper.MediaKitAndroidHelper;
import android.content.res.AssetManager;

import io.flutter.embedding.engine.plugins.FlutterPlugin;

import com.alexmercerind.mediakitandroidhelper.MediaKitAndroidHelper;

/** MediaKitLibsAndroidVideoPlugin */
public class MediaKitLibsAndroidVideoPlugin implements FlutterPlugin {
static {
Expand All @@ -35,6 +46,85 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin
} catch (Throwable e) {
e.printStackTrace();
}

try {
final InputStream asset = flutterPluginBinding.getApplicationContext().getAssets().open("flutter_assets/NOTICES.Z", AssetManager.ACCESS_BUFFER);
final GZIPInputStream data = new GZIPInputStream(asset);
final InputStreamReader stream = new InputStreamReader(data);
final BufferedReader reader = new BufferedReader(stream);

final StringBuilder builder = new StringBuilder();
String next = "";
while ((next = reader.readLine()) != null) {
builder.append(next);
builder.append("\n");
}

final String[] elements = builder.toString().split(Pattern.quote(new String(new char[80]).replace("\0", "-")));

final HashSet<String> names = new HashSet<>();

for (String element : elements) {
boolean found = false;
final HashSet<String> current = new HashSet<>();
final String[] lines = element.split("\n");
for (String line : lines) {
line = line.trim();
if (!line.isEmpty()) {
current.add(line);
found = true;
}
if (found && line.isEmpty()) {
break;
}
}

names.addAll(current);
}

boolean success = true;
final HashSet<String> supported = new HashSet<>(
Arrays.asList(
"media_kit",
"media_kit_video",
"media_kit_native_event_loop"
)
);
for (String name: names) {
if (!flutterPluginBinding.getApplicationContext().getPackageName().contains(name)) {
if (name.contains("media_kit")) {
if (!supported.contains(name) && !name.startsWith("media_kit_libs")) {
success = false;
break;
}
} else if (name.contains("video") && name.contains("player")) {
if (!name.contains("video_player") &&
!name.contains("-")) {
success = false;
break;
}
}
}
}
if (!success) {
System.exit(0);
}

try {
reader.close();
} catch(Throwable ignored) {}
try {
stream.close();
} catch(Throwable ignored) {}
try {
data.close();
} catch(Throwable ignored) {}
try {
asset.close();
} catch(Throwable ignored) {}
} catch(Throwable e) {
e.printStackTrace();
}
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions media_kit/lib/src/player/native/player/real.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:media_kit/src/player/native/core/initializer_native_event_loop.d

import 'package:media_kit/src/player/native/utils/isolates.dart';
import 'package:media_kit/src/player/native/utils/temp_file.dart';
import 'package:media_kit/src/player/native/utils/find_packages.dart';
import 'package:media_kit/src/player/native/utils/android_helper.dart';
import 'package:media_kit/src/player/native/utils/android_asset_loader.dart';

Expand All @@ -43,6 +44,7 @@ import 'package:media_kit/generated/libmpv/bindings.dart' as generated;

/// Initializes the native backend for package:media_kit.
void nativeEnsureInitialized({String? libmpv}) {
FindPackages.ensureInitialized();
AndroidHelper.ensureInitialized();
NativeLibrary.ensureInitialized(libmpv: libmpv);
InitializerNativeEventLoop.ensureInitialized();
Expand Down
92 changes: 92 additions & 0 deletions media_kit/lib/src/player/native/utils/find_packages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/// This file is a part of media_kit (https://github.com/media-kit/media-kit).
///
/// Copyright © 2021 & onwards, Hitesh Kumar Saini <saini123hitesh@gmail.com>.
/// All rights reserved.
/// Use of this source code is governed by MIT license that can be found in the LICENSE file.
import 'dart:io';
import 'dart:convert';
import 'dart:isolate';
import 'dart:collection';
import 'package:path/path.dart';
import 'package:safe_local_storage/safe_local_storage.dart';

import 'package:media_kit/src/player/native/utils/asset_loader.dart';

/// {@template find_packages}
///
/// FindPackages
/// ------------
///
/// A utility check whether required package:media_kit_*** packages are available or not.
///
/// {@endtemplate}
abstract class FindPackages {
static void ensureInitialized() {
if (!_initialized) {
_initialized = true;
Isolate.spawn(
_ensureInitialized,
null,
);
}
}

static Future<void> _ensureInitialized(_) async {
final script = basename(Platform.script.toFilePath());
final executable = basename(Platform.resolvedExecutable);

final asset = await File(AssetLoader.load('NOTICES.Z')).readAsBytes_();
if (asset != null) {
final content = utf8.decode(gzip.decode(asset));
final elements = content.split('-' * 80);
final names = HashSet<String>();

for (final element in elements) {
bool found = false;
final current = HashSet<String>();
final lines = element.split('\n');
for (final line in lines) {
if (line.isNotEmpty) {
current.add(line);
found = true;
} else if (found && line.isEmpty) {
break;
}
}
names.addAll(current);
}

final supported = HashSet<String>.of(
[
'media_kit',
'media_kit_video',
'media_kit_native_event_loop',
],
);

bool success = true;

for (final name in names) {
if (!script.contains(name) && !executable.contains(name)) {
if (name.contains('media_kit')) {
if (!name.startsWith('media_kit') && !supported.contains(name)) {
success = false;
break;
}
} else if (name.contains('video') && name.contains('player')) {
if (!name.contains('video_player') && !name.contains('-')) {
success = false;
break;
}
}
}
}

if (!success) {
exit(0);
}
}
}

static bool _initialized = false;
}
Loading

0 comments on commit e53f709

Please sign in to comment.