-
Notifications
You must be signed in to change notification settings - Fork 7
/
ChromecastManager.swift
176 lines (148 loc) · 5.84 KB
/
ChromecastManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// ChromecastManager.swift
// ChromeCastExperiments
//
// Created by Edmondo Pentangelo on 13/08/2015.
//
import Foundation
// ChromecastManager.swift
@objc(ChromecastManager)
class ChromecastManager: NSObject, GCKDeviceScannerListener, GCKDeviceManagerDelegate, GCKMediaControlChannelDelegate {
var bridge : RCTBridge!
private var deviceManager: GCKDeviceManager?
private var deviceScanner: GCKDeviceScanner
private var mediaControlChannel: GCKMediaControlChannel?
private lazy var kReceiverAppID:String = {
//You can add your own app id here that you get by registering with the
// Google Cast SDK Developer Console https://cast.google.com/publish
return kGCKMediaDefaultReceiverApplicationID
}()
private var devices: Dictionary<String, GCKDevice> = Dictionary<String, GCKDevice>()
// Required init.
required override init() {
let filterCriteria = GCKFilterCriteria(forAvailableApplicationWithID:
kGCKMediaDefaultReceiverApplicationID)
deviceScanner = GCKDeviceScanner(filterCriteria:filterCriteria)
}
@objc func startScan() -> Void {
dispatch_async(dispatch_get_main_queue(), { [unowned self] in
// Initialize device scanner
self.deviceScanner.addListener(self)
self.deviceScanner.startScan()
})
}
@objc func connectToDevice(deviceName: String) -> Void {
let selectedDevice = self.devices[deviceName]
if (selectedDevice == nil) {
return
}
dispatch_async(dispatch_get_main_queue(), { [unowned self] in
let identifier = NSBundle.mainBundle().infoDictionary?["CFBundleIdentifier"] as! String
self.deviceManager = GCKDeviceManager(device: selectedDevice, clientPackageName: identifier)
self.deviceManager!.delegate = self
self.deviceManager!.connect()
})
}
@objc func disconnect() -> Void {
if (self.deviceManager == nil) {
return
}
dispatch_async(dispatch_get_main_queue(), { [unowned self] in
// Disconnect button.
self.deviceManager!.leaveApplication()
self.deviceManager!.disconnect()
})
}
@objc func pause() -> Void {
self.mediaControlChannel?.pause();
}
@objc func play() -> Void {
self.mediaControlChannel?.play();
}
@objc func seekToTime(time: Double) -> Void {
let timeInterval = NSTimeInterval(time);
self.mediaControlChannel?.seekToTimeInterval(timeInterval);
}
@objc func getStreamPosition(successCallback: RCTResponseSenderBlock) -> Void {
let position = self.mediaControlChannel?.approximateStreamPosition();
if (position != nil) {
let positionDouble = Double(position!);
successCallback([positionDouble]);
}
}
@objc func castVideo(videoUrl: String, title: String, description: String, imageUrl: String) -> Void {
println("Cast Video")
// Show alert if not connected.
if (deviceManager?.connectionState != GCKConnectionState.Connected) {
println("Not connected to device");
return
}
// [START media-metadata]
// Define Media Metadata.
let metadata = GCKMediaMetadata()
metadata.setString(title, forKey: kGCKMetadataKeyTitle)
metadata.setString(description,
forKey:kGCKMetadataKeySubtitle)
let url = NSURL(string:imageUrl)
metadata.addImage(GCKImage(URL: url, width: 480, height: 360))
// [END media-metadata]
// [START load-media]
// Define Media Information.
let mediaInformation = GCKMediaInformation(
contentID: videoUrl,
streamType: GCKMediaStreamType.None,
contentType: "video/mp4",
metadata: metadata,
streamDuration: 0,
mediaTracks: [],
textTrackStyle: nil,
customData: nil
)
dispatch_async(dispatch_get_main_queue(), { [unowned self] in
// Cast the media
self.mediaControlChannel!.loadMedia(mediaInformation, autoplay: true)
})
}
func mediaControlChannelDidUpdateStatus(mediaControlChannel: GCKMediaControlChannel!) {
println("updated status");
if (mediaControlChannel.isConnected && mediaControlChannel != nil && mediaControlChannel.mediaStatus != nil) {
let info = mediaControlChannel.mediaStatus.mediaInformation;
println(info.streamDuration);
self.bridge.eventDispatcher.sendDeviceEventWithName("MediaStatusUpdated",
body: ["Duration": info.streamDuration,
"Url": info.contentID,
"Title": info.metadata.stringForKey(kGCKMetadataKeyTitle),
"Description": info.metadata.stringForKey(kGCKMetadataKeySubtitle),
"ImageUrl": info.metadata.images().reduce("", combine: { (acc, x) -> String in return ((x as! GCKImage).URL!.absoluteString!) + acc })]);
}
}
func deviceManagerDidConnect(deviceManager: GCKDeviceManager!) {
println("Connected.")
dispatch_async(dispatch_get_main_queue(), { [unowned self] in
self.deviceManager!.launchApplication(self.kReceiverAppID)
})
}
func deviceManager(deviceManager: GCKDeviceManager!,
didConnectToCastApplication
applicationMetadata: GCKApplicationMetadata!,
sessionID: String!,
launchedApplication: Bool) {
println("Application has launched.")
self.mediaControlChannel = GCKMediaControlChannel()
mediaControlChannel!.delegate = self
deviceManager.addChannel(mediaControlChannel)
mediaControlChannel!.requestStatus()
}
func deviceDidComeOnline(device: GCKDevice!) {
println("Device found: \(device.friendlyName)")
devices[device.friendlyName] = device;
emitDeviceListChanged(["Devices": devices.keys.array]) }
func deviceDidGoOffline(device: GCKDevice!) {
println("Device went away: \(device.friendlyName)")
devices.removeValueForKey(device.friendlyName);
emitDeviceListChanged(["Devices": devices.keys.array])
}
private func emitDeviceListChanged(data: AnyObject) {
self.bridge.eventDispatcher.sendDeviceEventWithName("DeviceListChanged", body: data)
}
}