All Projects β†’ mhmiles β†’ Opencastswift

mhmiles / Opencastswift

Licence: mit
An open source implementation of the Google Cast SDK written in Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Opencastswift

android-cast-remote-display-sample
πŸ“» Google Cast's Remote Display Sample for Android
Stars: ✭ 38 (-54.22%)
Mutual labels:  chromecast
Noice
A native Android app to relax, improve focus and boost productivity with minimal background noise.
Stars: ✭ 264 (+218.07%)
Mutual labels:  chromecast
Soundcast
Cast audio from macOS to Chromecast
Stars: ✭ 684 (+724.1%)
Mutual labels:  chromecast
cast-web-api
Quick and dirty web API for Google Cast enabled devices.
Stars: ✭ 126 (+51.81%)
Mutual labels:  chromecast
Rhythmbox-Chromecast
Send all your music to your Chromecast (Audio) within Rhythmbox
Stars: ✭ 17 (-79.52%)
Mutual labels:  chromecast
Go Chromecast
cli for Google Chromecast, Home devices and Cast Groups
Stars: ✭ 325 (+291.57%)
Mutual labels:  chromecast
LazyMan-iOS
A simple app that lets you stream every live and archived NHL and MLB game from any of your iOS devices.
Stars: ✭ 73 (-12.05%)
Mutual labels:  chromecast
C8tsender
c8tsender is a Google Chromecast sender server with a built-in REST API and responsive playlist interface
Stars: ✭ 29 (-65.06%)
Mutual labels:  chromecast
Pi-OpenCast
πŸ“Ί Transform your Raspberry Pi into an awesome streaming device.
Stars: ✭ 29 (-65.06%)
Mutual labels:  chromecast
Awesome Smart Tv
⚑️A curated list of awesome resources for building Smart TV apps
Stars: ✭ 588 (+608.43%)
Mutual labels:  chromecast
chromecast-api
πŸ“Ί Chromecast Node.js module
Stars: ✭ 122 (+46.99%)
Mutual labels:  chromecast
LMS-to-Cast
Chromecast to LMS bridge
Stars: ✭ 45 (-45.78%)
Mutual labels:  chromecast
Chromecast mpris
πŸ“Ί Control Chromecasts from Linux and D-Bus
Stars: ✭ 413 (+397.59%)
Mutual labels:  chromecast
KinoCast
Kinox.to fΓΌr Android und Chromecast
Stars: ✭ 29 (-65.06%)
Mutual labels:  chromecast
Appleevents
The unofficial Apple Events app for macOS
Stars: ✭ 831 (+901.2%)
Mutual labels:  chromecast
lastcast
Scrobble music playing on Chromecast (from Spotify, Google Play, SoundCloud, ...) to last.fm and libre.fm
Stars: ✭ 73 (-12.05%)
Mutual labels:  chromecast
Bw plex
binge watching for plex
Stars: ✭ 322 (+287.95%)
Mutual labels:  chromecast
Homeassistant Config
My personal Home Assistant config
Stars: ✭ 66 (-20.48%)
Mutual labels:  chromecast
Audio Cast Radio
Play some radio stations on Chromecast/Chromecast Audio
Stars: ✭ 12 (-85.54%)
Mutual labels:  chromecast
React Native Google Cast
React Native wrapper for the Google Cast SDK
Stars: ✭ 475 (+472.29%)
Mutual labels:  chromecast

OpenCastSwift: An open source implementation of the Google Cast SDK written in Swift

Carthage Compatible GitHub release Swift 4.1 platforms

This framework implements the Google Cast APIs so they can be used in macOS and iOS apps. Google provides an official SDK but it is only for iOS and closed source.

OS support

I've tested this to work on macOS 10.12 and iOS 11. It may work on earlier versions, I just haven't tested it. Sample apps with some basic functionality are included for both macOS and iOS. The iOS app is more built-out with a working example of casting an audio stream.

This framework fails build for watchOS because watchOS does not support SSL over CFStream sockets. I've left the target in this project in hopes that support is added in a future version of watchOS 🀞🀞.

Basic usage

Finding Google Cast devices on the network

import OpenCastSwift

var scanner = CastDeviceScanner()

NotificationCenter.default.addObserver(forName: CastDeviceScanner.DeviceListDidChange, object: scanner, queue: nil) { [unowned self] _ in
	// self.scanner.devices contains the list of devices available
}

scanner.startScanning()

It's also possible to receive device list changes by setting the scanner's delegate.

Connecting to a device

CastClient is the class used to establish a connection and sending requests to a specific device, you instantiate it with a CastDevice instance received from CastDeviceScanner.

import OpenCastSwift

var client = CastClient(device: scanner.devices.first!)
client.connect()

Getting information about status changes

Implement the CastClientDelegate protocol to get information about the connection and the device's status:

protocol CastClientDelegate {    
    optional func castClient(_ client: CastClient, willConnectTo device: CastDevice)
    optional func castClient(_ client: CastClient, didConnectTo device: CastDevice)
    optional func castClient(_ client: CastClient, didDisconnectFrom device: CastDevice)
    optional func castClient(_ client: CastClient, connectionTo device: CastDevice, didFailWith error: NSError)

    optional func castClient(_ client: CastClient, deviceStatusDidChange status: CastStatus)
    optional func castClient(_ client: CastClient, mediaStatusDidChange status: CastMediaStatus)
}

Launching an app

To launch an app on the device, you use the launch method on CastClient:

// appId is the unique identifier of the caster app to launch. The CastAppIdentifier struct contains the identifiers of the default generic media player, YouTube, and Google Assistant.

client.launch(appId: CastAppIdentifier.defaultMediaPlayer) { [weak self] result in
		switch result {
		case .success(let app):
	    // here you would probably call client.load() to load some media

		case .failure(let error):
			print(error)
		}

}

Joining an app

To connect to an existing app session, you use the join method on CastClient:

// appId is the unique identifier of the caster app to join. A value of nil will cause the client to attempt to connect to the currently running app.

client.join() { [weak self] result in
		switch result {
		case .success(let app):
	    // here you would probably call client.load() to load some media

		case .failure(let error):
			print(error)
		}

}

Loading media

After you have an instance of CastApp, you can tell the client to load some media with it using the load method:

let videoURL = URL(string: "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")!
let posterURL = URL(string: "https://i.imgur.com/GPgh0AN.jpg")!

// create a CastMedia object to hold media information
let media = CastMedia(title: "Test Bars",
						url: videoURL,
						poster: posterURL,
						contentType: "application/vnd.apple.mpegurl",
						streamType: CastMediaStreamType.buffered,
						autoplay: true,
						currentTime: 0)

// app is the instance of the app you got from the client after calling launch, or from the status callbacks
client.load(media: media, with: app) { result in
          switch result {
          case .success(let status):
            print(status)

          case .failure(let error):
            print(error)
          }
    }

    // this media has been successfully loaded, status contains the initial status for this media
	// you can now call requestMediaStatus periodically to get updated media status
}

Getting media status periodically

After you start streaming some media, you will probably want to get updated status every second, to update the UI. You should call the method requestMediaStatus on CastClient, this sends a request to the device to get the most recent media status.

func updateStatus() {
	// app is a CastApp instance you got after launching the app

	client.requestMediaStatus(for: app) { result in
      switch result {
      case .success(let status):
        print(status)

      case .failure(let error):
        print(error)
      }
	}
}

Implemented features

  • Discover cast devices on the local network
  • Launch, join, leave, and quit cast applications
  • Playback and volume controls for both devices and groups
  • Custom channels via subclassing CastChannel
  • Send and receive binary payloads

There is even some stubbed out functionality for undocumented features like device setup, certificate validation, and renaming. Contributions are always appreciated. πŸ˜„

Apps that use OpenCastSwift

  • CastSync - A macOS menu bar app for syncing playback from your Google Cast device to iTunes or VLC on your computer

Thanks

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].