InputMedia.cpp
上传用户:lusi_8715
上传日期:2007-01-08
资源大小:199k
文件大小:6k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

Visual C++

  1. /**************************************************************************************
  2.  *                                                                                    *
  3.  * This application contains code from OpenDivX and is released as a "Larger Work"    *
  4.  * under that license. Consistant with that license, this application is released     *
  5.  * under the GNU General Public License.                                              *
  6.  *                                                                                    *
  7.  * The OpenDivX license can be found at: http://www.projectmayo.com/opendivx/docs.php *
  8.  * The GPL can be found at: http://www.gnu.org/copyleft/gpl.html                      *
  9.  *                                                                                    *
  10.  * Copyright (c) 2001 - Project Mayo                                                  *
  11.  *                                                                                    *
  12.  * Authors: Damien Chavarria <adrc at projectmayo.com>                                *
  13.  *                                                                                    *
  14.  **************************************************************************************/
  15. #include "InputMedia.h"
  16. /*
  17.  * HTTP filling thread
  18.  */
  19. DWORD WINAPI httpThreadFunc( LPVOID lpData)
  20. {
  21. InputMedia *input = (InputMedia *) lpData;
  22. DWORD read;
  23. char  buffer[4096];
  24. while(input->running) {
  25. Sleep(5);
  26. if(input->buffering) {
  27. read = 0;
  28. InternetReadFile(input->http, (void *) buffer, 4096, &read);
  29. fwrite(buffer, 1, read, input->dnld);
  30. input->downloaded += read;
  31. }
  32. }
  33. return 0;
  34. }
  35. /*
  36.  * The main class
  37.  */
  38. InputMedia::InputMedia() 
  39. {
  40. this->file       = NULL;
  41. this->dnld       = NULL;
  42. this->hInternet  = NULL;
  43. this->http       = NULL;
  44. this->buffering  = 0;
  45. this->mode       = -1;
  46. this->downloaded = 0;
  47. }
  48. int InputMedia::Open(char *lpFilename, int mode, int type) 
  49. {
  50. if(lpFilename) {
  51. this->file       = NULL;
  52. this->dnld       = NULL;
  53. this->hInternet  = NULL;
  54. this->http       = NULL;
  55. this->buffering  = 0;
  56. this->mode       = -1;
  57. this->downloaded = 0;
  58. switch(type) {
  59. case INPUT_TYPE_FILE:
  60. switch(mode) {
  61. case INPUT_OPEN_BINARY:
  62. this->file = fopen(lpFilename, "rb");
  63. break;
  64. case INPUT_OPEN_ASCII:
  65. this->file = fopen(lpFilename, "rt");
  66. break;
  67. default:
  68. this->file = fopen(lpFilename, "rb");
  69. break;
  70. }
  71. if(this->file == NULL) {
  72. return 0;
  73. }
  74. this->mode = INPUT_TYPE_FILE;
  75. this->filename  = lpFilename;
  76. fseek(this->file, 0, SEEK_END);
  77. this->file_size = ftell(this->file);
  78. fseek(this->file, 0, SEEK_SET);
  79. return 1;
  80. break;
  81. case INPUT_TYPE_HTTP:
  82. this->hInternet = InternetOpen("The Playa - OpenDivX Viewer",
  83.    INTERNET_OPEN_TYPE_DIRECT,
  84.    NULL, NULL, 0);
  85. if(this->hInternet == NULL) {
  86. MessageBox(NULL, "Couldn't open an Internet connection", "", MB_OK);
  87. return 0;
  88. }
  89. this->http = InternetOpenUrl(this->hInternet, lpFilename, NULL, -1, 0, 0);
  90. if(this->http == NULL) {
  91. return 0;
  92. }
  93. /*
  94.  * Here we can create the temp file 
  95.  * and start the filling thread
  96.  */
  97. this->dnld = fopen("c:\temp.avi", "w+b");
  98. if(this->dnld == NULL) {
  99. MessageBox(NULL, "Could not create temp file, check your settings.", "Error", MB_OK);
  100. this->file = NULL;
  101. return 0;
  102. }
  103. this->downloaded  = 0;
  104. this->lastReadPos = 0;
  105. this->ioMutex = CreateMutex (NULL, FALSE, NULL);
  106. this->buffering     = 1;
  107. this->running       = 1;
  108. this->eof           = 0;
  109. this->mode = INPUT_TYPE_HTTP;
  110. this->httpThread    = CreateThread(NULL, 0, httpThreadFunc, (LPVOID) this, 0, &this->httpId);
  111. while(this->downloaded < HTTP_BUFFER_SIZE && !this->eof) {
  112. Sleep(5);
  113. }
  114. this->file = fopen("c:\temp.avi", "rb");
  115. if(this->file == NULL) {
  116. MessageBox(NULL, "Could not read temp file, check your settings.", "Error", MB_OK);
  117. return 0;
  118. }
  119. return 1;
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125. return 0;
  126. }
  127. InputMedia::~InputMedia() 
  128. {
  129. if(this->file) {
  130. fclose(this->file);
  131. }
  132. }
  133. int InputMedia::isOK() {
  134. return (this->file != NULL);
  135. }
  136. int InputMedia::getBufferState() {
  137. switch(this->mode) {
  138. case INPUT_TYPE_HTTP:
  139. if(this->http) {
  140. if(this->downloaded <= HTTP_BUFFER_SIZE) {
  141. return ((this->downloaded * 100) / HTTP_BUFFER_SIZE);
  142. }
  143. else 
  144. return 100;
  145. }
  146. break;
  147. default:
  148. break;
  149. }
  150. return 0;
  151. }
  152. char *InputMedia::getFilename()
  153. {
  154. return this->filename;
  155. return NULL;
  156. }
  157. DWORD InputMedia::getSize()
  158. {
  159. if(this->file) {
  160. return this->file_size;
  161. }
  162. return 0;
  163. }
  164. int InputMedia::Read(char *data, unsigned int size)
  165. {
  166. switch(this->mode) {
  167. case INPUT_TYPE_FILE:
  168. if(this->file) {
  169. return fread( (void *) data, 1, size, this->file);
  170. }
  171. MessageBox(NULL, "Problem reading from source!", "", MB_OK);
  172. break;
  173. case INPUT_TYPE_HTTP:
  174. if(this->http) {
  175. if(this->file) {
  176. return fread( (void *) data, 1, size, this->file);
  177. }
  178. MessageBox(NULL, "Problem reading from source!", "", MB_OK);
  179. }
  180. break;
  181. default:
  182. break;
  183. }
  184. MessageBox(NULL, "Problem reading from source!", "", MB_OK);
  185. return 0;
  186. }
  187. int InputMedia::ScanF(const char *format, void *data) {
  188. switch(mode) {
  189. case INPUT_TYPE_FILE:
  190. case INPUT_TYPE_HTTP:
  191. if(this->file) {
  192. fscanf(this->file, format, data);
  193. return 1;
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. return 0;
  200. }
  201. int InputMedia::Seek(int size, unsigned int method)
  202. {
  203. switch(mode) {
  204. case INPUT_TYPE_FILE:
  205. case INPUT_TYPE_HTTP:
  206. if(this->file) {
  207. switch(method)
  208. {
  209. case INPUT_SEEK_SET:
  210. return fseek( this->file, size, SEEK_SET);
  211. break;
  212. case INPUT_SEEK_CUR:
  213. if(size == 0) {
  214. return ftell(this->file);
  215. }
  216. else {
  217. return fseek( this->file, size, SEEK_CUR);
  218. }
  219. break;
  220. case INPUT_SEEK_END:
  221. return fseek( this->file, size, SEEK_END);
  222. break;
  223. }
  224. }
  225. break;
  226. default:
  227. break;
  228. }
  229. return 0;
  230. }
  231. int InputMedia::Close()
  232. {
  233. if(this->file) {
  234. fclose(this->file);
  235. }
  236. if(this->http) {
  237. InternetCloseHandle(this->http);
  238. }
  239. if(this->hInternet) {
  240. InternetCloseHandle(this->hInternet);
  241. }
  242. return 1;
  243. }