VLCPlayerControlWebSocket.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*****************************************************************************
  2. * VLCPlayerControlWebSocket.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCPlayerControlWebSocket.h"
  13. #import "VLCMetadata.h"
  14. @implementation VLCPlayerControlWebSocket
  15. - (void)dealloc
  16. {
  17. [[NSNotificationCenter defaultCenter] removeObserver:self];
  18. }
  19. - (void)didOpen
  20. {
  21. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  22. [notificationCenter addObserver:self
  23. selector:@selector(playbackStarted)
  24. name:VLCPlaybackControllerPlaybackDidStart
  25. object:nil];
  26. [notificationCenter addObserver:self
  27. selector:@selector(playbackStarted)
  28. name:VLCPlaybackControllerPlaybackDidResume
  29. object:nil];
  30. [notificationCenter addObserver:self
  31. selector:@selector(_respondToPlaying)
  32. name:VLCPlaybackControllerPlaybackMetadataDidChange
  33. object:nil];
  34. [notificationCenter addObserver:self
  35. selector:@selector(playbackPaused)
  36. name:VLCPlaybackControllerPlaybackDidPause
  37. object:nil];
  38. [notificationCenter addObserver:self
  39. selector:@selector(playbackEnded)
  40. name:VLCPlaybackControllerPlaybackDidStop
  41. object:nil];
  42. [notificationCenter addObserver:self
  43. selector:@selector(playbackEnded)
  44. name:VLCPlaybackControllerPlaybackDidFail
  45. object:nil];
  46. [notificationCenter addObserver:self
  47. selector:@selector(playbackSeekTo)
  48. name:VLCPlaybackControllerPlaybackPositionUpdated
  49. object:nil];
  50. APLog(@"web socket did open");
  51. [super didOpen];
  52. }
  53. - (void)didReceiveMessage:(NSString *)msg
  54. {
  55. NSError *error;
  56. NSDictionary *receivedDict = [NSJSONSerialization JSONObjectWithData:[msg dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
  57. if (error != nil) {
  58. APLog(@"JSON deserialization failed for %@", msg);
  59. return;
  60. }
  61. NSString *type = receivedDict[@"type"];
  62. if (!type) {
  63. APLog(@"No type in received JSON dict %@", receivedDict);
  64. }
  65. if ([type isEqualToString:@"playing"]) {
  66. [self _respondToPlaying];
  67. } else if ([type isEqualToString:@"play"]) {
  68. [self _respondToPlay];
  69. } else if ([type isEqualToString:@"pause"]) {
  70. [self _respondToPause];
  71. } else if ([type isEqualToString:@"ended"]) {
  72. [self _respondToEnded];
  73. } else if ([type isEqualToString:@"seekTo"]) {
  74. [self _respondToSeek:receivedDict];
  75. } else if ([type isEqualToString:@"openURL"]) {
  76. [self performSelectorOnMainThread:@selector(_respondToOpenURL:) withObject:receivedDict waitUntilDone:NO];
  77. } else if ([type isEqualToString:@"volume"]) {
  78. [self sendMessage:@"VOLUME CONTROL NOT SUPPORTED ON THIS DEVICE"];
  79. } else
  80. [self sendMessage:@"INVALID REQUEST!"];
  81. }
  82. #ifndef NDEBUG
  83. - (void)didClose
  84. {
  85. APLog(@"web socket did close");
  86. [super didClose];
  87. }
  88. #endif
  89. - (void)_respondToPlaying
  90. {
  91. /* JSON response
  92. {
  93. "type": "playing",
  94. "currentTime": 42,
  95. "media": {
  96. "id": "some id",
  97. "title": "some title",
  98. "duration": 120000
  99. }
  100. }
  101. */
  102. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  103. NSDictionary *returnDict;
  104. if (vpc.isPlaying) {
  105. VLCMedia *media = [vpc currentlyPlayingMedia];
  106. if (media) {
  107. NSURL *url = media.url;
  108. NSString *mediaTitle = vpc.metadata.title;
  109. if (!mediaTitle) {
  110. mediaTitle = url.lastPathComponent;
  111. }
  112. NSDictionary *mediaDict = @{ @"id" : url.absoluteString,
  113. @"title" : mediaTitle,
  114. @"duration" : @([vpc mediaDuration])};
  115. returnDict = @{ @"currentTime" : @([vpc playedTime].intValue),
  116. @"type" : @"playing",
  117. @"media" : mediaDict };
  118. }
  119. }
  120. if (!returnDict) {
  121. returnDict = [NSDictionary dictionary];
  122. }
  123. [self sendDataWithDict:returnDict];
  124. }
  125. #pragma mark - play
  126. - (void)_respondToPlay
  127. {
  128. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  129. [vpc play];
  130. }
  131. - (void)playbackStarted
  132. {
  133. /*
  134. {
  135. "type": "play",
  136. "currentTime": 42
  137. }
  138. */
  139. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  140. NSDictionary *dict = @{ @"currentTime" : @([vpc playedTime].intValue),
  141. @"type" : @"play" };
  142. [self sendDataWithDict:dict];
  143. }
  144. #pragma mark - pause
  145. - (void)_respondToPause
  146. {
  147. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  148. [vpc pause];
  149. }
  150. - (void)playbackPaused
  151. {
  152. /*
  153. {
  154. "type": "pause",
  155. "currentTime": 42,
  156. }
  157. */
  158. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  159. NSDictionary *dict = @{ @"currentTime" : @([vpc playedTime].intValue),
  160. @"type" : @"pause" };
  161. [self sendDataWithDict:dict];
  162. }
  163. - (void)sendDataWithDict:(NSDictionary *)dict
  164. {
  165. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  166. VLCMedia *media = [vpc currentlyPlayingMedia];
  167. if (media) {
  168. NSError *error;
  169. NSData *returnData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
  170. if (error != nil) {
  171. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  172. }
  173. [self sendData:returnData];
  174. }
  175. }
  176. #pragma mark - ended
  177. - (void)_respondToEnded
  178. {
  179. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  180. [vpc stopPlayback];
  181. }
  182. - (void)playbackEnded
  183. {
  184. /*
  185. {
  186. "type": "ended"
  187. }
  188. */
  189. NSDictionary *dict = @{ @"type" : @"ended" };
  190. [self sendDataWithDict:dict];
  191. }
  192. #pragma mark - seek
  193. - (void)_respondToSeek:(NSDictionary *)dictionary
  194. {
  195. /*
  196. {
  197. "currentTime" = 12514;
  198. "type" = seekTo;
  199. }
  200. */
  201. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  202. VLCMedia *media = [vpc currentlyPlayingMedia];
  203. if (!media)
  204. return;
  205. vpc.playbackPosition = [dictionary[@"currentTime"] floatValue] / (CGFloat)media.length.intValue;
  206. }
  207. - (void)playbackSeekTo
  208. {
  209. /*
  210. {
  211. "type": "seekTo",
  212. "currentTime": 42,
  213. "media": {
  214. "id": 42
  215. }
  216. }
  217. */
  218. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  219. VLCMedia *media = [vpc currentlyPlayingMedia];
  220. NSDictionary *mediaDict = @{ @"id" : media.url.absoluteString};
  221. NSDictionary *dict = @{ @"currentTime" : @([vpc playedTime].intValue),
  222. @"type" : @"seekTo",
  223. @"media" : mediaDict };
  224. [self sendDataWithDict:dict];
  225. }
  226. #pragma mark - openURL
  227. - (void)_respondToOpenURL:(NSDictionary *)dictionary
  228. {
  229. /*
  230.  {
  231. "type": "OpenURL",
  232. "url": "https://vimeo.com/74370512"
  233. }
  234. */
  235. BOOL needsMediaList = NO;
  236. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  237. VLCMediaList *mediaList = vpc.mediaList;
  238. if (!mediaList) {
  239. needsMediaList = YES;
  240. mediaList = [[VLCMediaList alloc] init];
  241. }
  242. NSString *urlString = dictionary[@"url"];
  243. if (urlString == nil || urlString.length == 0)
  244. return;
  245. /* force store update */
  246. NSUbiquitousKeyValueStore *ubiquitousKeyValueStore = [NSUbiquitousKeyValueStore defaultStore];
  247. [ubiquitousKeyValueStore synchronize];
  248. /* fetch data from cloud */
  249. NSMutableArray *recentURLs = [NSMutableArray arrayWithArray:[ubiquitousKeyValueStore arrayForKey:kVLCRecentURLs]];
  250. /* re-order array and add item */
  251. if ([recentURLs indexOfObject:urlString] != NSNotFound)
  252. [recentURLs removeObject:urlString];
  253. if (recentURLs.count >= 100)
  254. [recentURLs removeLastObject];
  255. [recentURLs addObject:urlString];
  256. /* sync back */
  257. [ubiquitousKeyValueStore setArray:recentURLs forKey:kVLCRecentURLs];
  258. [mediaList addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:urlString]]];
  259. if (needsMediaList) {
  260. [vpc playMediaList:mediaList firstIndex:0 subtitlesFilePath:nil];
  261. VLCFullscreenMovieTVViewController *movieVC = [VLCFullscreenMovieTVViewController fullscreenMovieTVViewController];
  262. if ([[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] != nil) {
  263. [[[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] presentViewController:movieVC
  264. animated:NO
  265. completion:nil];
  266. } else {
  267. [[[UIApplication sharedApplication].delegate.window rootViewController] presentViewController:movieVC
  268. animated:NO
  269. completion:nil];
  270. }
  271. }
  272. }
  273. @end