cast 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 82

cast #

Dart package to discover and connect with Chromecast devices

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  cast: ^0.1.1

Usage #

Start discovery service:

CastDiscoveryService().start();

List devices:

Widget build(BuildContext context) {
  StreamBuilder<List<CastDevice>>(
    stream: CastDiscoveryService().stream,
    initialData: CastDiscoveryService().devices,
    builder: (context, snapshot) {
      return Column(
        children: snapshot.data.map((device) {
          return ListTile(
            title: Text(device.name),
            onTap: () {
              _connect(context, device);
            },
          );
        }).toList(),
      );
    },
  );
}

Connect to device:

Future<void> _connect(BuildContext context, CastDevice object) async {
  final session = await CastSessionManager().startSession(object);

  session.stateStream.listen((state) {
    if (state == CastSessionState.connected) {
      _sendMessage(session);
    }
  });

  session.messageStream.listen((message) {
    print('receive message: $message');
  });

  session.sendMessage(CastSession.kNamespaceReceiver, {
    'type': 'LAUNCH',
    'appId': 'YouTube', // set the appId of your app here
  });
}

CastSessionManager is used to keep track of all sessions.

Send message:

void _sendMessage(CastSession session) {
  session.sendMessage('urn:x-cast:namespace-of-the-app', {
    'type': 'sample',
  });
}

Except for the launch message, you should wait until the session have a connected state before sending message.

Note #

Some informations about the protocol used https://docs.rs/crate/gcast/0.1.5/source/PROTOCOL.md

$ pub global activate protoc_plugin
$ export PATH="$PATH":"$HOME/.pub-cache/bin"
$ protoc -I=./lib/cast_channel --dart_out=./lib/cast_channel ./lib/cast_channel/cast_channel.proto --plugin "pub run protoc_plugin"

Author #

[0.2.0] #

  • Improve service discovery #1

[0.1.1] #

  • Add kNamespaceMedia to CastSession

[0.1.0] #

  • First release

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:cast/cast.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cast Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Scaffold(
        appBar: AppBar(),
        body: MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    CastDiscoveryService().start();
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<List<CastDevice>>(
      stream: CastDiscoveryService().stream,
      initialData: CastDiscoveryService().devices,
      builder: (context, snapshot) {
        return Column(
          children: snapshot.data.map((device) {
            return ListTile(
              title: Text(device.name),
              onTap: () {
                _connect(context, device);
              },
            );
          }).toList(),
        );
      },
    );
  }

  Future<void> _connect(BuildContext context, CastDevice object) async {
    final session = await CastSessionManager().startSession(object);

    session.stateStream.listen((state) {
      if (state == CastSessionState.connected) {
        final snackBar = SnackBar(content: Text('Connected'));
        Scaffold.of(context).showSnackBar(snackBar);

        _sendMessage(session);
      }
    });

    session.messageStream.listen((message) {
      print('receive message: $message');
    });

    session.sendMessage(CastSession.kNamespaceReceiver, {
      'type': 'LAUNCH',
      'appId': 'YouTube', // set the appId of your app here
    });
  }

  void _sendMessage(CastSession session) {
    session.sendMessage('urn:x-cast:namespace-of-the-app', {
      'type': 'sample',
    });
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  cast: ^0.2.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:cast/cast.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
64
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
82
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because:

  • package:cast/cast.dart that imports:
  • package:cast/discovery_service.dart that imports:
  • package:mdns_plugin/mdns_plugin.dart that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because:

  • package:cast/cast.dart that imports:
  • package:cast/discovery_service.dart that imports:
  • package:mdns_plugin/mdns_plugin.dart that declares support for platforms: android, ios

Package does not support Flutter platform web

Because:

  • package:cast/cast.dart that imports:
  • package:cast/discovery_service.dart that imports:
  • package:mdns_plugin/mdns_plugin.dart that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because:

  • package:cast/cast.dart that imports:
  • package:cast/discovery_service.dart that imports:
  • package:mdns_plugin/mdns_plugin.dart that declares support for platforms: android, ios

Package not compatible with SDK dart

Because:

  • cast that is a package requiring null.

Health suggestions

Fix lib/socket.dart. (-0.50 points)

Analysis of lib/socket.dart reported 1 hint:

line 27 col 11: Close instances of dart.core.Sink.

Format lib/cast_channel/cast_channel.pb.dart.

Run flutter format to format lib/cast_channel/cast_channel.pb.dart.

Format lib/cast_channel/cast_channel.pbenum.dart.

Run flutter format to format lib/cast_channel/cast_channel.pbenum.dart.

Fix additional 5 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/cast_channel/cast_channel.pbjson.dart (Run flutter format to format lib/cast_channel/cast_channel.pbjson.dart.)
  • lib/cast_channel/cast_channel.pbserver.dart (Run flutter format to format lib/cast_channel/cast_channel.pbserver.dart.)
  • lib/discovery_service.dart (Run flutter format to format lib/discovery_service.dart.)
  • lib/session.dart (Run flutter format to format lib/session.dart.)
  • lib/session_manager.dart (Run flutter format to format lib/session_manager.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
mdns_plugin ^1.1.4 1.1.4
protobuf ^1.0.1 1.0.1
Transitive dependencies
collection 1.14.12 1.14.13
fixnum 0.10.11
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
pedantic ^1.8.0
protoc_plugin ^19.0.1