localfile.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include "main.h"
  2. #include "../nu/AutoChar.h"
  3. extern CfgInt cfg_fullbuf;
  4. int VorbisFile::_f_close(void *) {return 0;}
  5. int VorbisFile::_f_seek(void* rs,__int64 offset,int whence)
  6. {
  7. return ((VorbisFile*)rs)->f_seek(offset,whence);
  8. }
  9. size_t VorbisFile::_f_read(void* ptr,size_t size,size_t nmemb,void * rs)
  10. {
  11. return ((VorbisFile*)rs)->f_read((UINT)(size*nmemb),ptr);
  12. }
  13. long VorbisFile::_f_tell(void* rs)
  14. {
  15. return ((VorbisFile*)rs)->f_tell();
  16. }
  17. ov_callbacks VorbisFile::oc={_f_read,_f_seek,_f_close,_f_tell};
  18. static __int64 Seek64(HANDLE hf, __int64 distance, DWORD MoveMethod)
  19. {
  20. LARGE_INTEGER li;
  21. li.QuadPart = distance;
  22. li.LowPart = SetFilePointer (hf, li.LowPart, &li.HighPart, MoveMethod);
  23. if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR)
  24. {
  25. li.QuadPart = -1;
  26. }
  27. return li.QuadPart;
  28. }
  29. static __int64 FileSize64(HANDLE file)
  30. {
  31. LARGE_INTEGER position;
  32. position.QuadPart=0;
  33. position.LowPart = GetFileSize(file, (LPDWORD)&position.HighPart);
  34. if (position.LowPart == INVALID_FILE_SIZE && GetLastError() != NO_ERROR)
  35. return INVALID_FILE_SIZE;
  36. else
  37. return position.QuadPart;
  38. }
  39. class VorbisFile_Local : public VorbisFile
  40. {
  41. private:
  42. HANDLE hFile;
  43. protected:
  44. int f_seek(__int64 offset,int whence)
  45. {
  46. if(whence==SEEK_SET) offset+=baseoffs;
  47. if (Seek64(hFile,offset,whence) != INVALID_SET_FILE_POINTER) return 0;
  48. else return -1;
  49. }
  50. size_t f_read(UINT siz,void * ptr)
  51. {
  52. DWORD bw=0;
  53. ReadFile(hFile,ptr,siz,&bw,0);
  54. return bw;
  55. }
  56. UINT f_tell()
  57. {
  58. return (UINT)(SetFilePointer(hFile,0,0,FILE_CURRENT)-baseoffs);
  59. }
  60. UINT FileSize()
  61. {
  62. return (UINT)(FileSize64(hFile)-baseoffs);
  63. }
  64. public:
  65. virtual int GetType() {return TYPE_LOCAL;}
  66. VorbisFile_Local(HANDLE f,const wchar_t * u,bool is_info) : VorbisFile(u,is_info) {hFile=f;}
  67. ~VorbisFile_Local() {CloseHandle(hFile);}
  68. };
  69. class VorbisFile_Mem : public VorbisFile
  70. {
  71. BYTE * block;
  72. UINT size,ptr;
  73. protected:
  74. int f_seek(__int64 offset,int whence)
  75. {
  76. switch(whence)
  77. {
  78. case SEEK_SET:
  79. ptr=(UINT)(offset+baseoffs);
  80. break;
  81. case SEEK_CUR:
  82. ptr+=(UINT)offset;
  83. break;
  84. case SEEK_END:
  85. ptr=size+whence;
  86. break;
  87. }
  88. if (ptr<=size) return 0;
  89. else {ptr=size;return -1;}
  90. }
  91. size_t f_read(UINT siz,void * out)
  92. {
  93. UINT d=size-ptr;
  94. if (d>siz) d=siz;
  95. memcpy(out,block+ptr,d);
  96. ptr+=d;
  97. return d;
  98. }
  99. UINT f_tell()
  100. {
  101. return (UINT)(ptr-baseoffs);
  102. }
  103. UINT FileSize()
  104. {
  105. return (UINT)(size-baseoffs);
  106. }
  107. public:
  108. virtual int GetType() {return TYPE_LOCAL;}
  109. VorbisFile_Mem(HANDLE f,const wchar_t * u,bool is_info) : VorbisFile(u,is_info)
  110. {
  111. size=GetFileSize(f,0);
  112. ptr=0;
  113. block=(BYTE*)malloc(size);
  114. DWORD br = 0;
  115. ReadFile(f,block,size,&br,0);
  116. CloseHandle(f);
  117. }
  118. ~VorbisFile_Mem() {free(block);}
  119. };
  120. VorbisFile * VorbisFile::Create(const wchar_t *url, bool is_info)
  121. {
  122. VorbisFile * r;
  123. if (PathIsURLW(url))
  124. {
  125. if (is_info) return 0;
  126. r=Create_HTTP(AutoChar(url),is_info);
  127. }
  128. else
  129. {
  130. __int64 baseoffs=0;
  131. HANDLE f=CreateFileW(url,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
  132. if (f==INVALID_HANDLE_VALUE) return 0;
  133. {
  134. DWORD dw = 0, br = 0;
  135. ReadFile(f,&dw,4,&br,0);
  136. if(br==4 && dw=='SggO')
  137. {
  138. SetFilePointer(f,0,0,FILE_BEGIN);
  139. }
  140. else if(br==4 && dw=='FFIR')
  141. {
  142. //RIFF file
  143. DWORD wavhdr = 0, nb = 0;
  144. SetFilePointer(f,4,0,FILE_CURRENT);
  145. ReadFile(f,&wavhdr,4,&nb,0);
  146. if(nb!=4 || wavhdr!='EVAW')
  147. {
  148. goto abort;
  149. }
  150. //find data starting point
  151. char tmp[1024] = {0};
  152. ReadFile(f,&tmp,1024,&nb,0);
  153. for(int i=0;i<1020;i++)
  154. if(tmp[i]=='d'&&tmp[i+1]=='a'&&tmp[i+2]=='t'&&tmp[i+3]=='a')
  155. {
  156. baseoffs=i+12+8;
  157. Seek64(f, baseoffs, FILE_BEGIN);
  158. }
  159. if(!baseoffs) goto abort;
  160. }
  161. else
  162. {
  163. abort:
  164. CloseHandle(f);
  165. return 0;
  166. }
  167. }
  168. r=cfg_fullbuf ? (VorbisFile*)new VorbisFile_Mem(f,url,is_info) : (VorbisFile*)new VorbisFile_Local(f,url,is_info);
  169. r->setBaseOffset(baseoffs);
  170. }
  171. if (r && !r->init())
  172. {
  173. delete r;
  174. r=0;
  175. }
  176. return r;
  177. }
  178. bool VorbisFile::init()
  179. {
  180. if (ov_open_callbacks(this,&vf,0,0,oc)) return 0;
  181. //TODO bitrate
  182. UINT siz=FileSize();
  183. double len=Length();
  184. if (siz>0 && len>0)
  185. {
  186. UINT divisor = (UINT)(len*125.0);
  187. if (divisor)
  188. avg_kbps=siz/divisor;
  189. }
  190. post_init();
  191. return 1;
  192. }
  193. int is_http(const char* url)
  194. {
  195. return (!_strnicmp(url,"http://",7) || !_strnicmp(url,"https://",8));
  196. }
  197. void VorbisFile::set_meta(const vorbis_comment * vc,int links)
  198. {
  199. if (links == vf.links)
  200. {
  201. int n;
  202. for(n=0;n<links;n++)
  203. {
  204. vorbis_comment_clear(vf.vc+n);
  205. /*
  206. extern void vorbis_comment_init(vorbis_comment *vc);
  207. extern void vorbis_comment_add(vorbis_comment *vc, char *comment);
  208. extern void vorbis_comment_add_tag(vorbis_comment *vc,char *tag, char *contents);
  209. extern char *vorbis_comment_query(vorbis_comment *vc, char *tag, int count);
  210. extern int vorbis_comment_query_count(vorbis_comment *vc, char *tag);
  211. extern void vorbis_comment_clear(vorbis_comment *vc);
  212. */
  213. }
  214. _ogg_free(vf.vc);
  215. vf.vc = (vorbis_comment*) _ogg_calloc(links,sizeof(vorbis_comment));
  216. for(n=0;n<links;n++)
  217. {
  218. vorbis_comment_init(vf.vc+n);
  219. int c;
  220. for(c=0;c<vc[n].comments;c++)
  221. {
  222. vorbis_comment_add(vf.vc+n,vc[n].user_comments[c]);
  223. }
  224. vf.vc[n].vendor = _strdup(vc[n].vendor);
  225. }
  226. }
  227. }