npaw_forge 9.7.0 copy "npaw_forge: ^9.7.0" to clipboard
npaw_forge: ^9.7.0 copied to clipboard

NPAW Forge analytics for Flutter: the shared native Forge Rust core over Dart FFI on Android/iOS, the Forge Web runtime on Flutter Web, and typed video, ad, and session reporting.

example/lib/main.dart

// Copyright (C) NPAW - All Rights Reserved
//
// This source code is protected under international copyright law. All rights
// reserved and protected by the copyright holders. This file is confidential and
// only available to authorized individuals with the permission of the copyright
// holders. If you encounter this file and do not have permission, please contact
// the copyright holders and delete this file.
//
//
// @author: Victor Soto <victor.soto@nicepeopleatwork.com>
// @description: NPAW Forge Flutter example app playing a sample stream with automatic analytics.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:npaw_forge/npaw_forge.dart';
import 'package:npaw_forge_video_player/npaw_forge_video_player.dart';
import 'package:video_player/video_player.dart';

/// Demo account; override with --dart-define=FORGE_ACCOUNT_CODE=yourAccount.
const String accountCode = String.fromEnvironment(
  'FORGE_ACCOUNT_CODE',
  defaultValue: 'npawdemodev',
);

const String sampleStream = String.fromEnvironment(
  'FORGE_SAMPLE_STREAM',
  defaultValue:
      'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
);

/// Ingest endpoint; point smoke runs at stage, never production.
const String lmaEndpoint = String.fromEnvironment('FORGE_LMA_ENDPOINT');

/// Opt-in iOS access-log QoS smoke toggle
/// (--dart-define=FORGE_IOS_ACCESS_LOG=true).
const bool enableAccessLogQos =
    bool.fromEnvironment('FORGE_IOS_ACCESS_LOG', defaultValue: false);

/// Opt-in diagnostics printer for smoke evidence
/// (--dart-define=FORGE_DEBUG_DIAGNOSTICS=true).
const bool debugDiagnostics =
    bool.fromEnvironment('FORGE_DEBUG_DIAGNOSTICS', defaultValue: false);

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final forge = await NpawForge.start(
    ForgeConfig(
      accountCode: accountCode,
      appName: 'npaw-forge-flutter-example',
      lmaEndpoint: lmaEndpoint.isEmpty ? null : lmaEndpoint,
      logLevel: 'info',
    ),
  );
  if (enableAccessLogQos) {
    final active = forge.enableIosAccessLogQos();
    debugPrint('forge iOS access-log QoS feed active: $active');
  }
  if (debugDiagnostics) {
    forge.events.listen((event) => debugPrint('forge event: ${event.raw}'));
  }
  WidgetsBinding.instance.addObserver(ForgeLifecycleObserver(forge));
  runApp(ForgeExampleApp(forge: forge));
}

class ForgeExampleApp extends StatelessWidget {
  const ForgeExampleApp({required this.forge, super.key});

  final NpawForge forge;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'NPAW Forge Example',
      navigatorObservers: [ForgeRouteObserver(forge)],
      theme: ThemeData(colorSchemeSeed: const Color(0xFF2E5BFF)),
      home: PlayerScreen(forge: forge),
    );
  }
}

class PlayerScreen extends StatefulWidget {
  const PlayerScreen({required this.forge, super.key});

  final NpawForge forge;

  @override
  State<PlayerScreen> createState() => _PlayerScreenState();
}

class _PlayerScreenState extends State<PlayerScreen> {
  late final VideoPlayerController _controller;
  ForgeVideoPlayerAttachment? _attachment;
  Timer? _qosTimer;
  String _status = 'initializing';

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.networkUrl(Uri.parse(sampleStream));
    _setUp();
  }

  Future<void> _setUp() async {
    _attachment = await attachVideoPlayer(
      widget.forge,
      _controller,
      metadata: const ContentMetadata(
        title: 'Big Buck Bunny',
        contentId: 'bbb-mp4',
        isLive: false,
      ),
    );
    await _controller.initialize();
    await _controller.play();
    if (debugDiagnostics) {
      _qosTimer = Timer.periodic(const Duration(seconds: 5), (_) {
        final json = _attachment?.adapter.metadata().toJson();
        debugPrint('forge qos sample: bitrate=${json?['bitrate']} '
            'droppedFrames=${json?['droppedFrames']} qos=${json?['qos']}');
      });
    }
    if (mounted) {
      setState(() {
        _status = 'playing (forge ${widget.forge.nativeVersion ?? 'web'})';
      });
    }
  }

  @override
  void dispose() {
    _qosTimer?.cancel();
    // Detach analytics before the controller goes away.
    _attachment?.dispose();
    _controller.dispose();
    super.dispose();
  }

  Future<void> _seekBy(Duration delta) async {
    final position = await _controller.position;
    if (position != null) {
      await _controller.seekTo(position + delta);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('NPAW Forge Example')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            AspectRatio(
              aspectRatio: _controller.value.isInitialized
                  ? _controller.value.aspectRatio
                  : 16 / 9,
              child: VideoPlayer(_controller),
            ),
            const SizedBox(height: 12),
            Text(_status),
            const SizedBox(height: 12),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconButton(
                  icon: const Icon(Icons.play_arrow),
                  onPressed: () => _controller.play(),
                ),
                IconButton(
                  icon: const Icon(Icons.pause),
                  onPressed: () => _controller.pause(),
                ),
                IconButton(
                  icon: const Icon(Icons.replay_10),
                  onPressed: () => _seekBy(const Duration(seconds: -10)),
                ),
                IconButton(
                  icon: const Icon(Icons.forward_10),
                  onPressed: () => _seekBy(const Duration(seconds: 10)),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
0
points
707
downloads

Publisher

verified publishernicepeopleatwork.com

Weekly Downloads

NPAW Forge analytics for Flutter: the shared native Forge Rust core over Dart FFI on Android/iOS, the Forge Web runtime on Flutter Web, and typed video, ad, and session reporting.

Homepage
Repository

License

unknown (license)

Dependencies

ffi, flutter, flutter_web_plugins, web

More

Packages that depend on npaw_forge

Packages that implement npaw_forge