caching_stream.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * This file is part of the FreeStreamer project,
  3. * (C)Copyright 2011-2018 Matias Muhonen <mmu@iki.fi> 穆马帝
  4. * See the file ''LICENSE'' for using the code.
  5. *
  6. * https://github.com/muhku/FreeStreamer
  7. */
  8. #include "caching_stream.h"
  9. #include "file_output.h"
  10. #include "stream_configuration.h"
  11. #include "file_stream.h"
  12. //#define CS_DEBUG 1
  13. #if !defined (CS_DEBUG)
  14. #define CS_TRACE(...) do {} while (0)
  15. #define CS_TRACE_CFSTRING(X) do {} while (0)
  16. #define CS_TRACE_CFURL(X) do {} while (0)
  17. #else
  18. #define CS_TRACE(...) printf(__VA_ARGS__)
  19. #define CS_TRACE_CFSTRING(X) CS_TRACE("%s\n", CFStringGetCStringPtr(X, kCFStringEncodingMacRoman))
  20. #define CS_TRACE_CFURL(X) CS_TRACE_CFSTRING(CFURLGetString(X))
  21. #endif
  22. namespace astreamer {
  23. Caching_Stream::Caching_Stream(Input_Stream *target) :
  24. m_target(target),
  25. m_fileOutput(0),
  26. m_fileStream(new File_Stream()),
  27. m_cacheable(false),
  28. m_writable(false),
  29. m_useCache(false),
  30. m_cacheMetaDataWritten(false),
  31. m_cacheIdentifier(0),
  32. m_fileUrl(0),
  33. m_metaDataUrl(0)
  34. {
  35. m_target->m_delegate = this;
  36. m_fileStream->m_delegate = this;
  37. }
  38. Caching_Stream::~Caching_Stream()
  39. {
  40. if (m_target) {
  41. delete m_target;
  42. m_target = 0;
  43. }
  44. if (m_fileOutput) {
  45. delete m_fileOutput;
  46. m_fileOutput = 0;
  47. }
  48. if (m_fileStream) {
  49. delete m_fileStream;
  50. m_fileStream = 0;
  51. }
  52. if (m_cacheIdentifier) {
  53. CFRelease(m_cacheIdentifier);
  54. m_cacheIdentifier = 0;
  55. }
  56. if (m_fileUrl) {
  57. CFRelease(m_fileUrl);
  58. m_fileUrl = 0;
  59. }
  60. if (m_metaDataUrl) {
  61. CFRelease(m_metaDataUrl);
  62. m_fileUrl = 0;
  63. }
  64. }
  65. CFURLRef Caching_Stream::createFileURLWithPath(CFStringRef path)
  66. {
  67. CFURLRef fileUrl = NULL;
  68. if (!path) {
  69. return fileUrl;
  70. }
  71. CFStringRef escapedPath = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, path, NULL, NULL, kCFStringEncodingUTF8);
  72. CFURLRef regularUrl = CFURLCreateWithString(kCFAllocatorDefault, (escapedPath ? escapedPath : path), NULL);
  73. if (regularUrl) {
  74. fileUrl = CFURLCreateFilePathURL(kCFAllocatorDefault, regularUrl, NULL);
  75. CFRelease(regularUrl);
  76. }
  77. if (escapedPath) {
  78. CFRelease(escapedPath);
  79. }
  80. return fileUrl;
  81. }
  82. void Caching_Stream::readMetaData()
  83. {
  84. if (!m_metaDataUrl) {
  85. return;
  86. }
  87. CFReadStreamRef readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, m_metaDataUrl);
  88. if (readStream) {
  89. if (CFReadStreamOpen(readStream)) {
  90. UInt8 buf[1024];
  91. CFIndex bytesRead = CFReadStreamRead(readStream, buf, 1024);
  92. if (bytesRead > 0) {
  93. CFStringRef contentType = CFStringCreateWithBytes(kCFAllocatorDefault, buf, bytesRead, kCFStringEncodingUTF8, false);
  94. if (contentType) {
  95. if (m_fileStream) {
  96. CS_TRACE("Setting the content type of the file stream based on the meta data\n");
  97. CS_TRACE_CFSTRING(contentType);
  98. m_fileStream->setContentType(contentType);
  99. }
  100. CFRelease(contentType);
  101. }
  102. }
  103. CFReadStreamClose(readStream);
  104. }
  105. CFRelease(readStream);
  106. }
  107. }
  108. Input_Stream_Position Caching_Stream::position()
  109. {
  110. if (m_useCache) {
  111. return m_fileStream->position();
  112. } else {
  113. return m_target->position();
  114. }
  115. }
  116. CFStringRef Caching_Stream::contentType()
  117. {
  118. if (m_useCache) {
  119. return m_fileStream->contentType();
  120. } else {
  121. return m_target->contentType();
  122. }
  123. }
  124. size_t Caching_Stream::contentLength()
  125. {
  126. if (m_useCache) {
  127. return m_fileStream->contentLength();
  128. } else {
  129. return m_target->contentLength();
  130. }
  131. }
  132. bool Caching_Stream::open()
  133. {
  134. bool status;
  135. if (CFURLResourceIsReachable(m_metaDataUrl, NULL) &&
  136. CFURLResourceIsReachable(m_fileUrl, NULL)) {
  137. m_cacheable = false;
  138. m_writable = false;
  139. m_useCache = true;
  140. m_cacheMetaDataWritten = false;
  141. readMetaData();
  142. CS_TRACE("Playing file from cache\n");
  143. CS_TRACE_CFURL(m_fileUrl);
  144. status = m_fileStream->open();
  145. } else {
  146. m_cacheable = true;
  147. m_writable = false;
  148. m_useCache = false;
  149. m_cacheMetaDataWritten = false;
  150. CS_TRACE("File not cached\n");
  151. status = m_target->open();
  152. }
  153. return status;
  154. }
  155. bool Caching_Stream::open(const Input_Stream_Position& position)
  156. {
  157. bool status;
  158. if (CFURLResourceIsReachable(m_metaDataUrl, NULL) &&
  159. CFURLResourceIsReachable(m_fileUrl, NULL)) {
  160. m_cacheable = false;
  161. m_writable = false;
  162. m_useCache = true;
  163. m_cacheMetaDataWritten = false;
  164. readMetaData();
  165. CS_TRACE("Playing file from cache\n");
  166. CS_TRACE_CFURL(m_fileUrl);
  167. status = m_fileStream->open(position);
  168. } else {
  169. m_cacheable = false;
  170. m_writable = false;
  171. m_useCache = false;
  172. m_cacheMetaDataWritten = false;
  173. CS_TRACE("File not cached\n");
  174. status = m_target->open(position);
  175. }
  176. return status;
  177. }
  178. void Caching_Stream::close()
  179. {
  180. m_fileStream->close();
  181. m_target->close();
  182. }
  183. void Caching_Stream::setScheduledInRunLoop(bool scheduledInRunLoop)
  184. {
  185. if (m_useCache) {
  186. m_fileStream->setScheduledInRunLoop(scheduledInRunLoop);
  187. } else {
  188. m_target->setScheduledInRunLoop(scheduledInRunLoop);
  189. }
  190. }
  191. void Caching_Stream::setUrl(CFURLRef url)
  192. {
  193. m_target->setUrl(url);
  194. }
  195. void Caching_Stream::setCacheIdentifier(CFStringRef cacheIdentifier)
  196. {
  197. m_cacheIdentifier = CFStringCreateCopy(kCFAllocatorDefault, cacheIdentifier);
  198. if (m_fileOutput) {
  199. delete m_fileOutput;
  200. m_fileOutput = 0;
  201. }
  202. Stream_Configuration *config = Stream_Configuration::configuration();
  203. CFStringRef filePath = CFStringCreateWithFormat(NULL, NULL, CFSTR("file://%@/%@"), config->cacheDirectory, m_cacheIdentifier);
  204. CFStringRef metaDataPath = CFStringCreateWithFormat(NULL, NULL, CFSTR("file://%@/%@.metadata"), config->cacheDirectory, m_cacheIdentifier);
  205. if (m_fileUrl) {
  206. CFRelease(m_fileUrl);
  207. m_fileUrl = 0;
  208. }
  209. if (m_metaDataUrl) {
  210. CFRelease(m_metaDataUrl);
  211. m_metaDataUrl = 0;
  212. }
  213. m_fileUrl = createFileURLWithPath(filePath);
  214. m_metaDataUrl = createFileURLWithPath(metaDataPath);
  215. m_fileStream->setUrl(m_fileUrl);
  216. CFRelease(filePath);
  217. CFRelease(metaDataPath);
  218. }
  219. bool Caching_Stream::canHandleUrl(CFURLRef url)
  220. {
  221. if (!url) {
  222. return false;
  223. }
  224. CFStringRef scheme = CFURLCopyScheme(url);
  225. if (scheme) {
  226. if (CFStringCompare(scheme, CFSTR("http"), 0) == kCFCompareEqualTo) {
  227. CFRelease(scheme);
  228. // Using cache makes only sense for HTTP
  229. return true;
  230. }
  231. CFRelease(scheme);
  232. }
  233. // Nothing else to server
  234. return false;
  235. }
  236. /* ID3_Parser_Delegate */
  237. void Caching_Stream::id3metaDataAvailable(std::map<CFStringRef,CFStringRef> metaData)
  238. {
  239. if (m_delegate) {
  240. m_delegate->streamMetaDataAvailable(metaData);
  241. }
  242. }
  243. void Caching_Stream::id3tagSizeAvailable(UInt32 tagSize)
  244. {
  245. if (m_delegate) {
  246. m_delegate->streamMetaDataByteSizeAvailable(tagSize);
  247. }
  248. }
  249. /* Input_Stream_Delegate */
  250. void Caching_Stream::streamIsReadyRead()
  251. {
  252. if (m_cacheable) {
  253. // If the stream is cacheable (not seeked from some position)
  254. // Check if the stream has a length. If there is no length,
  255. // it is a continuous stream and thus cannot be cached.
  256. m_cacheable = (m_target->contentLength() > 0);
  257. }
  258. #if CS_DEBUG
  259. if (m_cacheable) CS_TRACE("Stream can be cached!\n");
  260. else CS_TRACE("Stream cannot be cached\n");
  261. #endif
  262. if (m_delegate) {
  263. m_delegate->streamIsReadyRead();
  264. }
  265. }
  266. void Caching_Stream::streamHasBytesAvailable(UInt8 *data, UInt32 numBytes)
  267. {
  268. if (m_cacheable) {
  269. if (numBytes > 0) {
  270. if (!m_fileOutput) {
  271. if (m_fileUrl) {
  272. CS_TRACE("Caching started for stream\n");
  273. m_fileOutput = new File_Output(m_fileUrl);
  274. m_writable = true;
  275. }
  276. }
  277. if (m_writable && m_fileOutput) {
  278. m_writable &= (m_fileOutput->write(data, numBytes) > 0);
  279. }
  280. }
  281. }
  282. if (m_delegate) {
  283. m_delegate->streamHasBytesAvailable(data, numBytes);
  284. }
  285. }
  286. void Caching_Stream::streamEndEncountered()
  287. {
  288. if (m_fileOutput) {
  289. delete m_fileOutput;
  290. m_fileOutput = 0;
  291. }
  292. if (m_cacheable) {
  293. if (m_writable) {
  294. CS_TRACE("Successfully cached the stream\n");
  295. CS_TRACE_CFURL(m_fileUrl);
  296. // We only write the meta data if the stream was successfully streamed.
  297. // In that way we can use the meta data as an indicator that there is a file to stream.
  298. if (!m_cacheMetaDataWritten) {
  299. CFWriteStreamRef writeStream = CFWriteStreamCreateWithFile(kCFAllocatorDefault, m_metaDataUrl);
  300. if (writeStream) {
  301. if (CFWriteStreamOpen(writeStream)) {
  302. CFStringRef contentType = m_target->contentType();
  303. UInt8 buf[1024];
  304. CFIndex usedBytes = 0;
  305. if (contentType) {
  306. // It is possible that some streams don't provide a content type
  307. CFStringGetBytes(contentType,
  308. CFRangeMake(0, CFStringGetLength(contentType)),
  309. kCFStringEncodingUTF8,
  310. '?',
  311. false,
  312. buf,
  313. 1024,
  314. &usedBytes);
  315. }
  316. if (usedBytes > 0) {
  317. CS_TRACE("Writing the meta data\n");
  318. CS_TRACE_CFSTRING(contentType);
  319. CFWriteStreamWrite(writeStream, buf, usedBytes);
  320. }
  321. CFWriteStreamClose(writeStream);
  322. }
  323. CFRelease(writeStream);
  324. }
  325. m_cacheable = false;
  326. m_writable = false;
  327. m_useCache = true;
  328. m_cacheMetaDataWritten = true;
  329. }
  330. }
  331. }
  332. if (m_delegate) {
  333. m_delegate->streamEndEncountered();
  334. }
  335. }
  336. void Caching_Stream::streamErrorOccurred(CFStringRef errorDesc)
  337. {
  338. if (m_delegate) {
  339. m_delegate->streamErrorOccurred(errorDesc);
  340. }
  341. }
  342. void Caching_Stream::streamMetaDataAvailable(std::map<CFStringRef,CFStringRef> metaData)
  343. {
  344. if (m_delegate) {
  345. m_delegate->streamMetaDataAvailable(metaData);
  346. }
  347. }
  348. void Caching_Stream::streamMetaDataByteSizeAvailable(UInt32 sizeInBytes)
  349. {
  350. if (m_delegate) {
  351. m_delegate->streamMetaDataByteSizeAvailable(sizeInBytes);
  352. }
  353. }
  354. } // namespace astreamer