-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMultiScreenService.m
360 lines (291 loc) · 13.3 KB
/
MultiScreenService.m
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//
// Created by Jeremy White on 6/16/14.
// Copyright (c) 2014 LG Electronics. All rights reserved.
//
#import "MultiScreenService.h"
#import "DiscoveryManager.h"
#import "MultiScreenDiscoveryProvider.h"
#import "ConnectError.h"
#import "MultiScreenWebAppSession.h"
#import "ConnectUtil.h"
@implementation MultiScreenService
{
NSMutableDictionary *_sessions;
}
+ (NSDictionary *) discoveryParameters
{
return @{
@"serviceId" : kConnectSDKMultiScreenTVServiceId
};
}
- (void) setServiceDescription:(ServiceDescription *)serviceDescription
{
if (!serviceDescription)
return;
[super setServiceDescription:serviceDescription];
_device = [self deviceForId:serviceDescription.UUID];
}
- (MSDevice *) deviceForId:(NSString *)deviceId
{
if (!deviceId || deviceId.length == 0)
return nil;
__block MSDevice *device;
[[DiscoveryManager sharedManager].discoveryProviders enumerateObjectsUsingBlock:^(DiscoveryProvider *provider, NSUInteger idx, BOOL *stop) {
if ([provider isKindOfClass:[MultiScreenDiscoveryProvider class]])
{
MultiScreenDiscoveryProvider *multiScreenProvider = (MultiScreenDiscoveryProvider *) provider;
device = multiScreenProvider.devices[deviceId];
*stop = YES;
}
}];
return device;
}
#pragma mark - DeviceService methods
- (void) updateCapabilities
{
NSArray *caps = [NSArray new];
caps = [caps arrayByAddingObjectsFromArray:kMediaPlayerCapabilities];
caps = [caps arrayByAddingObjectsFromArray:@[
kMediaControlPlay,
kMediaControlPause,
kMediaControlDuration,
kMediaControlSeek,
kMediaControlPosition,
kMediaControlPlayState,
kMediaControlPlayStateSubscribe,
kWebAppLauncherLaunch,
kWebAppLauncherLaunchParams,
kWebAppLauncherJoin,
kWebAppLauncherConnect,
kWebAppLauncherDisconnect,
kWebAppLauncherMessageSend,
kWebAppLauncherMessageSendJSON,
kWebAppLauncherMessageReceive,
kWebAppLauncherMessageReceiveJSON,
kWebAppLauncherClose
]];
self.capabilities = caps;
}
- (void) connect
{
if (!_device)
{
NSError *connectError = [ConnectError generateErrorWithCode:ConnectStatusCodeError andDetails:@"Was unable to find the MSDevice instance for this IP address"];
if (self.delegate && [self.delegate respondsToSelector:@selector(deviceService:didFailConnectWithError:)])
dispatch_on_main(^{ [self.delegate deviceService:self didFailConnectWithError:connectError]; });
return;
}
self.connected = YES;
_sessions = [NSMutableDictionary new];
if (self.delegate && [self.delegate respondsToSelector:@selector(deviceServiceConnectionSuccess:)])
dispatch_on_main(^{ [self.delegate deviceServiceConnectionSuccess:self]; });
}
- (void) disconnect
{
[_sessions enumerateKeysAndObjectsUsingBlock:^(id key, MultiScreenWebAppSession *session, BOOL *stop) {
[session disconnectFromWebApp];
}];
self.connected = NO;
if (self.delegate && [self.delegate respondsToSelector:@selector(deviceService:disconnectedWithError:)])
dispatch_on_main(^{ [self.delegate deviceService:self disconnectedWithError:nil]; });
}
- (void) closeLaunchSession:(LaunchSession *)launchSession success:(SuccessBlock)success failure:(FailureBlock)failure
{
if (launchSession.sessionType == LaunchSessionTypeMedia)
[self.mediaPlayer closeMedia:launchSession success:success failure:failure];
else if (launchSession.sessionType == LaunchSessionTypeWebApp)
[self.webAppLauncher closeWebApp:launchSession success:success failure:failure];
else
{
if (failure)
failure([ConnectError generateErrorWithCode:ConnectStatusCodeError andDetails:@"Could not find launcher for provided LaunchSession."]);
}
}
#pragma mark - Media Player
- (id <MediaPlayer>) mediaPlayer
{
return self;
}
- (CapabilityPriorityLevel) mediaPlayerPriority
{
return CapabilityPriorityLevelHigh;
}
- (void) displayImage:(NSURL *)imageURL iconURL:(NSURL *)iconURL title:(NSString *)title description:(NSString *)description mimeType:(NSString *)mimeType success:(MediaPlayerDisplaySuccessBlock)success failure:(FailureBlock)failure
{
NSString *webAppId = @"ConnectSDKMediaPlayer";
[self.webAppLauncher joinWebAppWithId:webAppId success:^(WebAppSession *webAppSession) {
[webAppSession.mediaPlayer displayImage:imageURL iconURL:iconURL title:title description:description mimeType:mimeType success:success failure:failure];
} failure:^(NSError *error) {
[self.webAppLauncher launchWebApp:webAppId success:^(WebAppSession *webAppSession) {
[webAppSession connectWithSuccess:^(id responseObject) {
[webAppSession.mediaPlayer displayImage:imageURL iconURL:iconURL title:title description:description mimeType:mimeType success:success failure:failure];
} failure:failure];
} failure:failure];
}];
}
- (void) playMedia:(NSURL *)mediaURL iconURL:(NSURL *)iconURL title:(NSString *)title description:(NSString *)description mimeType:(NSString *)mimeType shouldLoop:(BOOL)shouldLoop success:(MediaPlayerDisplaySuccessBlock)success failure:(FailureBlock)failure
{
NSString *webAppId = @"ConnectSDKMediaPlayer";
[self.webAppLauncher joinWebAppWithId:webAppId success:^(WebAppSession *webAppSession) {
[webAppSession.mediaPlayer playMedia:mediaURL iconURL:iconURL title:title description:description mimeType:mimeType shouldLoop:shouldLoop success:success failure:failure];
} failure:^(NSError *error) {
[self.webAppLauncher launchWebApp:webAppId success:^(WebAppSession *webAppSession) {
[webAppSession connectWithSuccess:^(id responseObject) {
[webAppSession.mediaPlayer playMedia:mediaURL iconURL:iconURL title:title description:description mimeType:mimeType shouldLoop:shouldLoop success:success failure:failure];
} failure:failure];
} failure:failure];
}];
}
- (void) closeMedia:(LaunchSession *)launchSession success:(SuccessBlock)success failure:(FailureBlock)failure
{
[self.webAppLauncher closeWebApp:launchSession success:success failure:failure];
}
#pragma mark - Web App Launcher
- (id <WebAppLauncher>) webAppLauncher
{
return self;
}
- (CapabilityPriorityLevel) webAppLauncherPriority
{
return CapabilityPriorityLevelHigh;
}
- (void) launchWebApp:(NSString *)webAppId success:(WebAppLaunchSuccessBlock)success failure:(FailureBlock)failure
{
[self.webAppLauncher launchWebApp:webAppId params:nil relaunchIfRunning:YES success:success failure:failure];
}
- (void) launchWebApp:(NSString *)webAppId params:(NSDictionary *)params success:(WebAppLaunchSuccessBlock)success failure:(FailureBlock)failure
{
[self.webAppLauncher launchWebApp:webAppId params:params relaunchIfRunning:YES success:success failure:failure];
}
- (void) launchWebApp:(NSString *)webAppId relaunchIfRunning:(BOOL)relaunchIfRunning success:(WebAppLaunchSuccessBlock)success failure:(FailureBlock)failure
{
[self.webAppLauncher launchWebApp:webAppId params:nil relaunchIfRunning:relaunchIfRunning success:success failure:failure];
}
- (void) launchWebApp:(NSString *)webAppId params:(NSDictionary *)params relaunchIfRunning:(BOOL)relaunchIfRunning success:(WebAppLaunchSuccessBlock)success failure:(FailureBlock)failure
{
NSError *error;
if (!webAppId || webAppId.length == 0)
error = [ConnectError generateErrorWithCode:ConnectStatusCodeArgumentError andDetails:@"You must provide a valid web app id"];
if (!self.device)
error = [ConnectError generateErrorWithCode:ConnectStatusCodeError andDetails:@"Could not find a reference to the native device object"];
if (error)
{
if (failure)
failure(error);
return;
}
if (!params)
params = @{};
webAppId = [ConnectUtil urlEncode:webAppId];
[self.device getApplication:webAppId completionBlock:^(MSApplication *application, NSError *getError) {
if (getError || !application)
{
if (!getError)
getError = [ConnectError generateErrorWithCode:ConnectStatusCodeTvError andDetails:@"Experienced an unknown error getting app info, app may not be installed"];
if (failure)
failure(getError);
} else
{
[application launchWithOptions:params completionBlock:^(BOOL launchSuccess, NSError *launchError) {
if (launchSuccess)
{
LaunchSession *launchSession = [LaunchSession launchSessionForAppId:webAppId];
launchSession.sessionType = LaunchSessionTypeWebApp;
launchSession.service = self;
MultiScreenWebAppSession *webAppSession = _sessions[webAppId];
if (!webAppSession)
{
webAppSession = [[MultiScreenWebAppSession alloc] initWithLaunchSession:launchSession service:self];
_sessions[webAppId] = webAppSession;
}
webAppSession.application = application;
if (success)
success(webAppSession);
} else
{
if (!launchError)
launchError = [ConnectError generateErrorWithCode:ConnectStatusCodeTvError andDetails:@"Experienced an unknown error launching app"];
if (failure)
failure(launchError);
}
} queue:dispatch_get_main_queue()];
}
} queue:dispatch_get_main_queue()];
}
- (void) joinWebApp:(LaunchSession *)webAppLaunchSession success:(WebAppLaunchSuccessBlock)success failure:(FailureBlock)failure
{
[self.device getApplication:webAppLaunchSession.appId completionBlock:^(MSApplication *application, NSError *getError) {
if (getError || !application)
{
if (!getError)
getError = [ConnectError generateErrorWithCode:ConnectStatusCodeTvError andDetails:@"Experienced an unknown error getting app info, app may not be installed"];
if (failure)
failure(getError);
} else
{
MultiScreenWebAppSession *webAppSession = _sessions[webAppLaunchSession.appId];
if (!webAppSession)
{
webAppSession = [[MultiScreenWebAppSession alloc] initWithLaunchSession:webAppLaunchSession service:self];
_sessions[webAppLaunchSession.appId] = webAppSession;
}
webAppSession.application = application;
[webAppSession joinWithSuccess:success failure:failure];
}
} queue:dispatch_get_main_queue()];
}
- (void) joinWebAppWithId:(NSString *)webAppId success:(WebAppLaunchSuccessBlock)success failure:(FailureBlock)failure
{
LaunchSession *launchSession = [LaunchSession launchSessionForAppId:webAppId];
launchSession.sessionType = LaunchSessionTypeWebApp;
launchSession.service = self;
[self.webAppLauncher joinWebApp:launchSession success:success failure:failure];
}
// TODO: this method is returning a 404 error on app leave/re-join
- (void) closeWebApp:(LaunchSession *)launchSession success:(SuccessBlock)success failure:(FailureBlock)failure
{
NSError *error;
if (!launchSession || !launchSession.appId || launchSession.appId.length == 0)
error = [ConnectError generateErrorWithCode:ConnectStatusCodeArgumentError andDetails:@"You must provide a valid launch session"];
if (!self.device)
error = [ConnectError generateErrorWithCode:ConnectStatusCodeError andDetails:@"Could not find a reference to the native device object"];
if (error)
{
if (failure)
failure(error);
return;
}
[self.device getApplication:launchSession.appId completionBlock:^(MSApplication *application, NSError *getError) {
if (getError || !application)
{
if (!getError)
getError = [ConnectError generateErrorWithCode:ConnectStatusCodeTvError andDetails:@"Experienced an unknown error getting app info, app may not be installed"];
if (failure)
failure(getError);
} else
{
if (application.lastKnownStatus == MS_APP_RUNNING || application.lastKnownStatus == MS_APP_STARTING)
{
[application terminateWithCompletionBlock:^(BOOL terminateSuccess, NSError *terminateError) {
if (terminateSuccess)
{
[_sessions removeObjectForKey:launchSession.appId];
if (success)
success(nil);
} else
{
if (!terminateError)
terminateError = [ConnectError generateErrorWithCode:ConnectStatusCodeTvError andDetails:@"Experienced an unknown error terminating app"];
if (failure)
failure(terminateError);
}
} queue:dispatch_get_main_queue()];
} else
{
if (success)
success(nil);
}
}
} queue:dispatch_get_main_queue()];
}
@end