http_stream.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef ASTREAMER_HTTP_STREAM_H
  7. #define ASTREAMER_HTTP_STREAM_H
  8. #import <CFNetwork/CFNetwork.h>
  9. #import <string>
  10. #import <vector>
  11. #import <map>
  12. #import "id3_parser.h"
  13. namespace astreamer {
  14. class HTTP_Stream_Delegate;
  15. struct HTTP_Stream_Position {
  16. size_t start;
  17. size_t end;
  18. };
  19. class HTTP_Stream : public ID3_Parser_Delegate {
  20. private:
  21. HTTP_Stream(const HTTP_Stream&);
  22. HTTP_Stream& operator=(const HTTP_Stream&);
  23. static const size_t STREAM_BUFSIZ;
  24. static CFStringRef httpRequestMethod;
  25. static CFStringRef httpUserAgentHeader;
  26. static CFStringRef httpUserAgentValue;
  27. static CFStringRef httpRangeHeader;
  28. static CFStringRef icyMetaDataHeader;
  29. static CFStringRef icyMetaDataValue;
  30. CFURLRef m_url;
  31. CFReadStreamRef m_readStream;
  32. bool m_scheduledInRunLoop;
  33. HTTP_Stream_Position m_position;
  34. /* HTTP headers */
  35. bool m_httpHeadersParsed;
  36. std::string m_contentType;
  37. size_t m_contentLength;
  38. /* ICY protocol */
  39. bool m_icyStream;
  40. bool m_icyHeaderCR;
  41. bool m_icyHeadersRead;
  42. bool m_icyHeadersParsed;
  43. std::vector<std::string> m_icyHeaderLines;
  44. size_t m_icyMetaDataInterval;
  45. size_t m_dataByteReadCount;
  46. size_t m_metaDataBytesRemaining;
  47. std::vector<UInt8> m_icyMetaData;
  48. /* Read buffers */
  49. UInt8 *m_httpReadBuffer;
  50. UInt8 *m_icyReadBuffer;
  51. ID3_Parser *m_id3Parser;
  52. CFReadStreamRef createReadStream(CFURLRef url);
  53. void parseHttpHeadersIfNeeded(UInt8 *buf, CFIndex bufSize);
  54. void parseICYStream(UInt8 *buf, CFIndex bufSize);
  55. CFStringRef createMetaDataStringWithMostReasonableEncoding(const UInt8 *bytes, CFIndex numBytes);
  56. static void readCallBack(CFReadStreamRef stream, CFStreamEventType eventType, void *clientCallBackInfo);
  57. public:
  58. HTTP_Stream_Delegate *m_delegate;
  59. HTTP_Stream();
  60. virtual ~HTTP_Stream();
  61. HTTP_Stream_Position position();
  62. std::string contentType();
  63. size_t contentLength();
  64. bool open();
  65. bool open(const HTTP_Stream_Position& position);
  66. void close();
  67. void setScheduledInRunLoop(bool scheduledInRunLoop);
  68. void setUrl(CFURLRef url);
  69. /* ID3_Parser_Delegate */
  70. void id3metaDataAvailable(std::map<CFStringRef,CFStringRef> metaData);
  71. };
  72. class HTTP_Stream_Delegate {
  73. public:
  74. virtual void streamIsReadyRead() = 0;
  75. virtual void streamHasBytesAvailable(UInt8 *data, UInt32 numBytes) = 0;
  76. virtual void streamEndEncountered() = 0;
  77. virtual void streamErrorOccurred() = 0;
  78. virtual void streamMetaDataAvailable(std::map<CFStringRef,CFStringRef> metaData) = 0;
  79. };
  80. } // namespace astreamer
  81. #endif // ASTREAMER_HTTP_STREAM_H