http_stream.h 2.5 KB

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