VLCPlayerControlWebSocket.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. @implementation VLCPlayerControlWebSocket
  14. - (void)dealloc
  15. {
  16. [[NSNotificationCenter defaultCenter] removeObserver:self];
  17. }
  18. - (void)didOpen
  19. {
  20. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  21. [notificationCenter addObserver:self
  22. selector:@selector(playbackStarted)
  23. name:VLCPlaybackControllerPlaybackDidStart
  24. object:nil];
  25. [notificationCenter addObserver:self
  26. selector:@selector(playbackStarted)
  27. name:VLCPlaybackControllerPlaybackDidResume
  28. object:nil];
  29. [notificationCenter addObserver:self
  30. selector:@selector(_respondToPlaying)
  31. name:VLCPlaybackControllerPlaybackMetadataDidChange
  32. object:nil];
  33. [notificationCenter addObserver:self
  34. selector:@selector(playbackPaused)
  35. name:VLCPlaybackControllerPlaybackDidPause
  36. object:nil];
  37. [notificationCenter addObserver:self
  38. selector:@selector(playbackEnded)
  39. name:VLCPlaybackControllerPlaybackDidStop
  40. object:nil];
  41. [notificationCenter addObserver:self
  42. selector:@selector(playbackEnded)
  43. name:VLCPlaybackControllerPlaybackDidFail
  44. object:nil];
  45. [notificationCenter addObserver:self
  46. selector:@selector(playbackSeekTo)
  47. name:VLCPlaybackControllerPlaybackPositionUpdated
  48. object:nil];
  49. APLog(@"web socket did open");
  50. [super didOpen];
  51. }
  52. - (void)didReceiveMessage:(NSString *)msg
  53. {
  54. NSError *error;
  55. NSDictionary *receivedDict = [NSJSONSerialization JSONObjectWithData:[msg dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];
  56. if (error != nil) {
  57. APLog(@"JSON deserialization failed for %@", msg);
  58. return;
  59. }
  60. NSString *type = receivedDict[@"type"];
  61. if (!type) {
  62. APLog(@"No type in received JSON dict %@", receivedDict);
  63. }
  64. if ([type isEqualToString:@"playing"]) {
  65. [self _respondToPlaying];
  66. } else if ([type isEqualToString:@"play"]) {
  67. [self _respondToPlay];
  68. } else if ([type isEqualToString:@"pause"]) {
  69. [self _respondToPause];
  70. } else if ([type isEqualToString:@"ended"]) {
  71. [self _respondToEnded];
  72. } else if ([type isEqualToString:@"seekTo"]) {
  73. [self _respondToSeek:receivedDict];
  74. } else if ([type isEqualToString:@"volume"]) {
  75. [self sendMessage:@"VOLUME CONTROL NOT SUPPORTED ON THIS DEVICE"];
  76. } else
  77. [self sendMessage:@"INVALID REQUEST!"];
  78. }
  79. #ifndef NDEBUG
  80. - (void)didClose
  81. {
  82. APLog(@"web socket did close");
  83. [super didClose];
  84. }
  85. #endif
  86. - (void)_respondToPlaying
  87. {
  88. /* JSON response
  89. {
  90. "type": "playing",
  91. "currentTime": 42,
  92. "media": {
  93. "id": "some id",
  94. "title": "some title",
  95. "duration": 120000
  96. }
  97. }
  98. */
  99. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  100. NSDictionary *returnDict;
  101. if (vpc.activePlaybackSession) {
  102. VLCMediaPlayer *player = vpc.mediaPlayer;
  103. if (player) {
  104. VLCMedia *media = player.media;
  105. if (media) {
  106. NSURL *url = media.url;
  107. NSString *mediaTitle = vpc.mediaTitle;
  108. if (!mediaTitle)
  109. mediaTitle = url.lastPathComponent;
  110. NSDictionary *mediaDict = @{ @"id" : url.absoluteString,
  111. @"title" : mediaTitle,
  112. @"duration" : @(media.length.intValue)};
  113. returnDict = @{ @"currentTime" : @(player.time.intValue),
  114. @"type" : @"playing",
  115. @"media" : mediaDict };
  116. }
  117. }
  118. }
  119. if (!returnDict) {
  120. returnDict = [NSDictionary dictionary];
  121. }
  122. NSError *error;
  123. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  124. if (error != nil) {
  125. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  126. }
  127. [self sendData:returnData];
  128. }
  129. #pragma mark - play
  130. - (void)_respondToPlay
  131. {
  132. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  133. VLCMediaListPlayer *listPlayer = vpc.listPlayer;
  134. if (listPlayer) {
  135. [listPlayer play];
  136. }
  137. }
  138. - (void)playbackStarted
  139. {
  140. /*
  141. {
  142. "type": "play",
  143. "currentTime": 42
  144. }
  145. */
  146. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  147. VLCMediaPlayer *player = vpc.mediaPlayer;
  148. if (player) {
  149. VLCMedia *media = player.media;
  150. if (media) {
  151. NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
  152. @"type" : @"play" };
  153. NSError *error;
  154. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  155. if (error != nil) {
  156. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  157. }
  158. [self sendData:returnData];
  159. }
  160. }
  161. }
  162. #pragma mark - pause
  163. - (void)_respondToPause
  164. {
  165. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  166. VLCMediaListPlayer *listPlayer = vpc.listPlayer;
  167. if (listPlayer) {
  168. [listPlayer pause];
  169. }
  170. }
  171. - (void)playbackPaused
  172. {
  173. /*
  174. {
  175. "type": "pause",
  176. "currentTime": 42,
  177. }
  178. */
  179. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  180. VLCMediaPlayer *player = vpc.mediaPlayer;
  181. if (player) {
  182. VLCMedia *media = player.media;
  183. if (media) {
  184. NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
  185. @"type" : @"pause" };
  186. NSError *error;
  187. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  188. if (error != nil) {
  189. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  190. }
  191. [self sendData:returnData];
  192. }
  193. }
  194. }
  195. #pragma mark - ended
  196. - (void)_respondToEnded
  197. {
  198. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  199. [vpc stopPlayback];
  200. }
  201. - (void)playbackEnded
  202. {
  203. /*
  204. {
  205. "type": "ended"
  206. }
  207. */
  208. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  209. VLCMediaPlayer *player = vpc.mediaPlayer;
  210. if (player) {
  211. VLCMedia *media = player.media;
  212. if (media) {
  213. NSDictionary *returnDict = @{ @"type" : @"ended" };
  214. NSError *error;
  215. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  216. if (error != nil) {
  217. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  218. }
  219. [self sendData:returnData];
  220. }
  221. }
  222. }
  223. #pragma mark - seek
  224. - (void)_respondToSeek:(NSDictionary *)dictionary
  225. {
  226. /*
  227. {
  228. "currentTime" = 12514;
  229. "type" = seekTo;
  230. }
  231. */
  232. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  233. VLCMediaPlayer *player = vpc.mediaPlayer;
  234. if (!player)
  235. return;
  236. VLCMedia *media = player.media;
  237. if (!media)
  238. return;
  239. player.position = [dictionary[@"currentTime"] floatValue] / (CGFloat)media.length.intValue;
  240. }
  241. - (void)playbackSeekTo
  242. {
  243. /*
  244. {
  245. "type": "seekTo",
  246. "currentTime": 42,
  247. "media": {
  248. "id": 42
  249. }
  250. }
  251. */
  252. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  253. VLCMediaPlayer *player = vpc.mediaPlayer;
  254. if (player) {
  255. VLCMedia *media = player.media;
  256. if (media) {
  257. NSDictionary *mediaDict = @{ @"id" : media.url.absoluteString};
  258. NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
  259. @"type" : @"seekTo",
  260. @"media" : mediaDict };
  261. NSError *error;
  262. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  263. if (error != nil) {
  264. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  265. }
  266. [self sendData:returnData];
  267. }
  268. }
  269. }
  270. @end