VLCPlayerControlWebSocket.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. NSString *mediaTitle = vpc.mediaTitle;
  107. if (!mediaTitle)
  108. mediaTitle = @"";
  109. NSDictionary *mediaDict = @{ @"id" : media.url.absoluteString,
  110. @"title" : mediaTitle,
  111. @"duration" : @(media.length.intValue)};
  112. returnDict = @{ @"currentTime" : @(player.time.intValue),
  113. @"type" : @"playing",
  114. @"media" : mediaDict };
  115. }
  116. }
  117. }
  118. if (!returnDict) {
  119. returnDict = [NSDictionary dictionary];
  120. }
  121. NSError *error;
  122. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  123. if (error != nil) {
  124. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  125. }
  126. [self sendData:returnData];
  127. }
  128. #pragma mark - play
  129. - (void)_respondToPlay
  130. {
  131. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  132. VLCMediaListPlayer *listPlayer = vpc.listPlayer;
  133. if (listPlayer) {
  134. [listPlayer play];
  135. }
  136. }
  137. - (void)playbackStarted
  138. {
  139. /*
  140. {
  141. "type": "play",
  142. "currentTime": 42
  143. }
  144. */
  145. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  146. VLCMediaPlayer *player = vpc.mediaPlayer;
  147. if (player) {
  148. VLCMedia *media = player.media;
  149. if (media) {
  150. NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
  151. @"type" : @"play" };
  152. NSError *error;
  153. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  154. if (error != nil) {
  155. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  156. }
  157. [self sendData:returnData];
  158. }
  159. }
  160. }
  161. #pragma mark - pause
  162. - (void)_respondToPause
  163. {
  164. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  165. VLCMediaListPlayer *listPlayer = vpc.listPlayer;
  166. if (listPlayer) {
  167. [listPlayer pause];
  168. }
  169. }
  170. - (void)playbackPaused
  171. {
  172. /*
  173. {
  174. "type": "pause",
  175. "currentTime": 42,
  176. }
  177. */
  178. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  179. VLCMediaPlayer *player = vpc.mediaPlayer;
  180. if (player) {
  181. VLCMedia *media = player.media;
  182. if (media) {
  183. NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
  184. @"type" : @"pause" };
  185. NSError *error;
  186. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  187. if (error != nil) {
  188. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  189. }
  190. [self sendData:returnData];
  191. }
  192. }
  193. }
  194. #pragma mark - ended
  195. - (void)_respondToEnded
  196. {
  197. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  198. [vpc stopPlayback];
  199. }
  200. - (void)playbackEnded
  201. {
  202. /*
  203. {
  204. "type": "ended"
  205. }
  206. */
  207. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  208. VLCMediaPlayer *player = vpc.mediaPlayer;
  209. if (player) {
  210. VLCMedia *media = player.media;
  211. if (media) {
  212. NSDictionary *returnDict = @{ @"type" : @"ended" };
  213. NSError *error;
  214. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  215. if (error != nil) {
  216. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  217. }
  218. [self sendData:returnData];
  219. }
  220. }
  221. }
  222. #pragma mark - seek
  223. - (void)_respondToSeek:(NSDictionary *)dictionary
  224. {
  225. /*
  226. {
  227. "currentTime" = 12514;
  228. "type" = seekTo;
  229. }
  230. */
  231. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  232. VLCMediaPlayer *player = vpc.mediaPlayer;
  233. if (!player)
  234. return;
  235. VLCMedia *media = player.media;
  236. if (!media)
  237. return;
  238. player.position = [dictionary[@"currentTime"] floatValue] / (CGFloat)media.length.intValue;
  239. }
  240. - (void)playbackSeekTo
  241. {
  242. /*
  243. {
  244. "type": "seekTo",
  245. "currentTime": 42,
  246. "media": {
  247. "id": 42
  248. }
  249. }
  250. */
  251. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  252. VLCMediaPlayer *player = vpc.mediaPlayer;
  253. if (player) {
  254. VLCMedia *media = player.media;
  255. if (media) {
  256. NSDictionary *mediaDict = @{ @"id" : media.url.absoluteString};
  257. NSDictionary *returnDict = @{ @"currentTime" : @(player.time.intValue),
  258. @"type" : @"seekTo",
  259. @"media" : mediaDict };
  260. NSError *error;
  261. NSData *returnData = [NSJSONSerialization dataWithJSONObject:returnDict options:0 error:&error];
  262. if (error != nil) {
  263. APLog(@"%s: JSON serialization failed %@", __PRETTY_FUNCTION__, error);
  264. }
  265. [self sendData:returnData];
  266. }
  267. }
  268. }
  269. @end