VLCPlayerControlWebSocket.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. VLCMediaListPlayer *listPlayer = vpc.listPlayer;
  130. if (listPlayer) {
  131. [listPlayer play];
  132. }
  133. }
  134. - (void)playbackStarted
  135. {
  136. /*
  137. {
  138. "type": "play",
  139. "currentTime": 42
  140. }
  141. */
  142. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  143. NSDictionary *dict = @{ @"currentTime" : @([vpc playedTime].intValue),
  144. @"type" : @"play" };
  145. [self sendDataWithDict:dict];
  146. }
  147. #pragma mark - pause
  148. - (void)_respondToPause
  149. {
  150. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  151. VLCMediaListPlayer *listPlayer = vpc.listPlayer;
  152. if (listPlayer) {
  153. [listPlayer pause];
  154. }
  155. }
  156. - (void)playbackPaused
  157. {
  158. /*
  159. {
  160. "type": "pause",
  161. "currentTime": 42,
  162. }
  163. */
  164. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  165. NSDictionary *dict = @{ @"currentTime" : @([vpc playedTime].intValue),
  166. @"type" : @"pause" };
  167. [self sendDataWithDict:dict];
  168. }
  169. - (void)sendDataWithDict:(NSDictionary *)dict
  170. {
  171. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  172. VLCMedia *media = [vpc currentlyPlayingMedia];
  173. if (media) {
  174. NSError *error;
  175. NSData *returnData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&error];
  176. if (error != nil) {
  177. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  178. }
  179. [self sendData:returnData];
  180. }
  181. }
  182. #pragma mark - ended
  183. - (void)_respondToEnded
  184. {
  185. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  186. [vpc stopPlayback];
  187. }
  188. - (void)playbackEnded
  189. {
  190. /*
  191. {
  192. "type": "ended"
  193. }
  194. */
  195. NSDictionary *dict = @{ @"type" : @"ended" };
  196. [self sendDataWithDict:dict];
  197. }
  198. #pragma mark - seek
  199. - (void)_respondToSeek:(NSDictionary *)dictionary
  200. {
  201. /*
  202. {
  203. "currentTime" = 12514;
  204. "type" = seekTo;
  205. }
  206. */
  207. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  208. VLCMedia *media = [vpc currentlyPlayingMedia];
  209. if (!media)
  210. return;
  211. vpc.playbackPosition = [dictionary[@"currentTime"] floatValue] / (CGFloat)media.length.intValue;
  212. }
  213. - (void)playbackSeekTo
  214. {
  215. /*
  216. {
  217. "type": "seekTo",
  218. "currentTime": 42,
  219. "media": {
  220. "id": 42
  221. }
  222. }
  223. */
  224. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  225. VLCMedia *media = [vpc currentlyPlayingMedia];
  226. NSDictionary *mediaDict = @{ @"id" : media.url.absoluteString};
  227. NSDictionary *dict = @{ @"currentTime" : @([vpc playedTime].intValue),
  228. @"type" : @"seekTo",
  229. @"media" : mediaDict };
  230. [self sendDataWithDict:dict];
  231. }
  232. #pragma mark - openURL
  233. - (void)_respondToOpenURL:(NSDictionary *)dictionary
  234. {
  235. /*
  236.  {
  237. "type": "OpenURL",
  238. "url": "https://vimeo.com/74370512"
  239. }
  240. */
  241. BOOL needsMediaList = NO;
  242. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  243. VLCMediaList *mediaList = vpc.mediaList;
  244. if (!mediaList) {
  245. needsMediaList = YES;
  246. mediaList = [[VLCMediaList alloc] init];
  247. }
  248. NSString *urlString = dictionary[@"url"];
  249. if (urlString == nil || urlString.length == 0)
  250. return;
  251. /* force store update */
  252. NSUbiquitousKeyValueStore *ubiquitousKeyValueStore = [NSUbiquitousKeyValueStore defaultStore];
  253. [ubiquitousKeyValueStore synchronize];
  254. /* fetch data from cloud */
  255. NSMutableArray *recentURLs = [NSMutableArray arrayWithArray:[ubiquitousKeyValueStore arrayForKey:kVLCRecentURLs]];
  256. /* re-order array and add item */
  257. if ([recentURLs indexOfObject:urlString] != NSNotFound)
  258. [recentURLs removeObject:urlString];
  259. if (recentURLs.count >= 100)
  260. [recentURLs removeLastObject];
  261. [recentURLs addObject:urlString];
  262. /* sync back */
  263. [ubiquitousKeyValueStore setArray:recentURLs forKey:kVLCRecentURLs];
  264. [mediaList addMedia:[VLCMedia mediaWithURL:[NSURL URLWithString:urlString]]];
  265. if (needsMediaList) {
  266. [vpc playMediaList:mediaList firstIndex:0 subtitlesFilePath:nil];
  267. VLCFullscreenMovieTVViewController *movieVC = [VLCFullscreenMovieTVViewController fullscreenMovieTVViewController];
  268. if ([[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] != nil) {
  269. [[[[UIApplication sharedApplication].delegate.window rootViewController] presentedViewController] presentViewController:movieVC
  270. animated:NO
  271. completion:nil];
  272. } else {
  273. [[[UIApplication sharedApplication].delegate.window rootViewController] presentViewController:movieVC
  274. animated:NO
  275. completion:nil];
  276. }
  277. }
  278. }
  279. @end