FSAudioController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * This file is part of the FreeStreamer project,
  3. * (C)Copyright 2011-2013 Matias Muhonen.
  4. * See the file ''LICENSE'' for using the code.
  5. */
  6. #import "FSAudioController.h"
  7. #import "FSAudioStream.h"
  8. #import "FSPlaylistItem.h"
  9. #import "FSCheckContentTypeRequest.h"
  10. #import "FSParsePlaylistRequest.h"
  11. #import "FSParseRssPodcastFeedRequest.h"
  12. @interface FSAudioController ()
  13. @property (readonly) FSAudioStream *audioStream;
  14. @property (readonly) FSCheckContentTypeRequest *checkContentTypeRequest;
  15. @property (readonly) FSParsePlaylistRequest *parsePlaylistRequest;
  16. @property (readonly) FSParseRssPodcastFeedRequest *parseRssPodcastFeedRequest;
  17. @property (nonatomic,assign) BOOL readyToPlay;
  18. @property (nonatomic,assign) NSUInteger currentPlaylistItemIndex;
  19. @property (nonatomic,strong) NSMutableArray *playlistItems;
  20. @end
  21. @implementation FSAudioController
  22. @synthesize readyToPlay;
  23. @synthesize currentPlaylistItemIndex;
  24. @synthesize playlistItems;
  25. -(id)init
  26. {
  27. if (self = [super init]) {
  28. _url = nil;
  29. _audioStream = [[FSAudioStream alloc] init];
  30. _checkContentTypeRequest = nil;
  31. _parsePlaylistRequest = nil;
  32. _readyToPlay = NO;
  33. }
  34. return self;
  35. }
  36. - (id)initWithUrl:(NSString *)url
  37. {
  38. if (self = [self init]) {
  39. self.url = url;
  40. }
  41. return self;
  42. }
  43. - (void)dealloc
  44. {
  45. [_audioStream stop];
  46. if (_checkContentTypeRequest) {
  47. [_checkContentTypeRequest cancel];
  48. }
  49. if (_parsePlaylistRequest) {
  50. [_parsePlaylistRequest cancel];
  51. }
  52. if (_parseRssPodcastFeedRequest) {
  53. [_parseRssPodcastFeedRequest cancel];
  54. }
  55. }
  56. /*
  57. * =======================================
  58. * Properties
  59. * =======================================
  60. */
  61. - (FSAudioStream *)audioStream
  62. {
  63. return _audioStream;
  64. }
  65. - (FSCheckContentTypeRequest *)checkContentTypeRequest
  66. {
  67. return _checkContentTypeRequest;
  68. }
  69. - (FSParsePlaylistRequest *)parsePlaylistRequest
  70. {
  71. return _parsePlaylistRequest;
  72. }
  73. - (FSParseRssPodcastFeedRequest *)parseRssPodcastFeedRequest
  74. {
  75. return _parseRssPodcastFeedRequest;
  76. }
  77. - (BOOL)isPlaying
  78. {
  79. return [_audioStream isPlaying];
  80. }
  81. /*
  82. * =======================================
  83. * Public interface
  84. * =======================================
  85. */
  86. - (void)play
  87. {
  88. @synchronized (self) {
  89. if (self.readyToPlay) {
  90. if ([self.playlistItems count] > 0) {
  91. FSPlaylistItem *playlistItem = (self.playlistItems)[self.currentPlaylistItemIndex];
  92. _audioStream.url = playlistItem.nsURL;
  93. }
  94. [self.audioStream play];
  95. return;
  96. }
  97. __weak FSAudioController *weakSelf = self;
  98. /*
  99. * Handle playlists
  100. */
  101. _parsePlaylistRequest = [[FSParsePlaylistRequest alloc] init];
  102. _parsePlaylistRequest.url = self.url;
  103. _parsePlaylistRequest.onCompletion = ^() {
  104. if ([weakSelf.parsePlaylistRequest.playlistItems count] > 0) {
  105. weakSelf.playlistItems = weakSelf.parsePlaylistRequest.playlistItems;
  106. weakSelf.readyToPlay = YES;
  107. weakSelf.audioStream.onCompletion = ^() {
  108. if (weakSelf.currentPlaylistItemIndex + 1 < [weakSelf.playlistItems count]) {
  109. weakSelf.currentPlaylistItemIndex = weakSelf.currentPlaylistItemIndex + 1;
  110. [weakSelf play];
  111. }
  112. };
  113. [weakSelf play];
  114. }
  115. };
  116. _parsePlaylistRequest.onFailure = ^() {
  117. // Failed to parse the playlist; try playing anyway
  118. weakSelf.readyToPlay = YES;
  119. [weakSelf.audioStream play];
  120. };
  121. /*
  122. * Handle RSS feed parsing
  123. */
  124. _parseRssPodcastFeedRequest = [[FSParseRssPodcastFeedRequest alloc] init];
  125. _parseRssPodcastFeedRequest.url = self.url;
  126. _parseRssPodcastFeedRequest.onCompletion = ^() {
  127. if ([weakSelf.parseRssPodcastFeedRequest.playlistItems count] > 0) {
  128. weakSelf.playlistItems = weakSelf.parseRssPodcastFeedRequest.playlistItems;
  129. weakSelf.readyToPlay = YES;
  130. weakSelf.audioStream.onCompletion = ^() {
  131. if (weakSelf.currentPlaylistItemIndex + 1 < [weakSelf.playlistItems count]) {
  132. weakSelf.currentPlaylistItemIndex = weakSelf.currentPlaylistItemIndex + 1;
  133. [weakSelf play];
  134. }
  135. };
  136. [weakSelf play];
  137. }
  138. };
  139. _parseRssPodcastFeedRequest.onFailure = ^() {
  140. // Failed to parse the XML file; try playing anyway
  141. weakSelf.readyToPlay = YES;
  142. [weakSelf.audioStream play];
  143. };
  144. /*
  145. * Handle content type check
  146. */
  147. _checkContentTypeRequest = [[FSCheckContentTypeRequest alloc] init];
  148. _checkContentTypeRequest.url = self.url;
  149. _checkContentTypeRequest.onCompletion = ^() {
  150. if (weakSelf.checkContentTypeRequest.playlist) {
  151. // The URL is a playlist; retrieve the contents
  152. [weakSelf.parsePlaylistRequest start];
  153. } else if (weakSelf.checkContentTypeRequest.xml) {
  154. // The URL may be an RSS feed, check the contents
  155. [weakSelf.parseRssPodcastFeedRequest start];
  156. } else {
  157. // Not a playlist; try directly playing the URL
  158. weakSelf.readyToPlay = YES;
  159. [weakSelf.audioStream play];
  160. }
  161. };
  162. _checkContentTypeRequest.onFailure = ^() {
  163. // Failed to check the format; try playing anyway
  164. weakSelf.readyToPlay = YES;
  165. [weakSelf.audioStream play];
  166. };
  167. [_checkContentTypeRequest start];
  168. NSDictionary *userInfo = @{FSAudioStreamNotificationKey_State: @(kFsAudioStreamRetrievingURL)};
  169. NSNotification *notification = [NSNotification notificationWithName:FSAudioStreamStateChangeNotification object:nil userInfo:userInfo];
  170. [[NSNotificationCenter defaultCenter] postNotification:notification];
  171. }
  172. }
  173. - (void)playFromURL:(NSString*)url
  174. {
  175. self.url = url;
  176. [self play];
  177. }
  178. - (void)stop
  179. {
  180. [_audioStream stop];
  181. self.readyToPlay = NO;
  182. }
  183. - (void)pause
  184. {
  185. [_audioStream pause];
  186. }
  187. /*
  188. * =======================================
  189. * Properties
  190. * =======================================
  191. */
  192. - (void)setUrl:(NSString *)url
  193. {
  194. @synchronized (self) {
  195. _url = nil;
  196. self.currentPlaylistItemIndex = 0;
  197. if (url && ![url isEqual:_url]) {
  198. [_checkContentTypeRequest cancel], _checkContentTypeRequest = nil;
  199. [_parsePlaylistRequest cancel], _parsePlaylistRequest = nil;
  200. NSString *copyOfURL = [url copy];
  201. _url = copyOfURL;
  202. /* Since the stream URL changed, the content may have changed */
  203. self.readyToPlay = NO;
  204. self.playlistItems = [[NSMutableArray alloc] init];
  205. }
  206. self.audioStream.url = [NSURL URLWithString:_url];
  207. }
  208. }
  209. - (NSString*)url
  210. {
  211. if (!_url) {
  212. return nil;
  213. }
  214. NSString *copyOfURL = [_url copy];
  215. return copyOfURL;
  216. }
  217. - (FSAudioStream *)stream
  218. {
  219. return _audioStream;
  220. }
  221. @end