VLCPlayerControlWebSocket.m 9.7 KB

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