You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
8.6KB

  1. /*
  2. * Unbuffered io for ffmpeg system
  3. * Copyright (c) 2001 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /* needed for usleep() */
  22. #define _XOPEN_SOURCE 600
  23. #include <unistd.h>
  24. #include "libavutil/avstring.h"
  25. #include "libavcodec/opt.h"
  26. #include "os_support.h"
  27. #include "avformat.h"
  28. #if CONFIG_NETWORK
  29. #include "network.h"
  30. #endif
  31. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  32. /** @name Logging context. */
  33. /*@{*/
  34. static const char *urlcontext_to_name(void *ptr)
  35. {
  36. URLContext *h = (URLContext *)ptr;
  37. if(h->prot) return h->prot->name;
  38. else return "NULL";
  39. }
  40. static const AVOption options[] = {{NULL}};
  41. static const AVClass urlcontext_class =
  42. { "URLContext", urlcontext_to_name, options, LIBAVUTIL_VERSION_INT };
  43. /*@}*/
  44. #endif
  45. static int default_interrupt_cb(void);
  46. URLProtocol *first_protocol = NULL;
  47. URLInterruptCB *url_interrupt_cb = default_interrupt_cb;
  48. URLProtocol *av_protocol_next(URLProtocol *p)
  49. {
  50. if(p) return p->next;
  51. else return first_protocol;
  52. }
  53. int av_register_protocol2(URLProtocol *protocol, int size)
  54. {
  55. URLProtocol **p;
  56. if (size < sizeof(URLProtocol)) {
  57. URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
  58. memcpy(temp, protocol, size);
  59. protocol = temp;
  60. }
  61. p = &first_protocol;
  62. while (*p != NULL) p = &(*p)->next;
  63. *p = protocol;
  64. protocol->next = NULL;
  65. return 0;
  66. }
  67. #if LIBAVFORMAT_VERSION_MAJOR < 53
  68. /* The layout of URLProtocol as of when major was bumped to 52 */
  69. struct URLProtocol_compat {
  70. const char *name;
  71. int (*url_open)(URLContext *h, const char *filename, int flags);
  72. int (*url_read)(URLContext *h, unsigned char *buf, int size);
  73. int (*url_write)(URLContext *h, unsigned char *buf, int size);
  74. int64_t (*url_seek)(URLContext *h, int64_t pos, int whence);
  75. int (*url_close)(URLContext *h);
  76. struct URLProtocol *next;
  77. };
  78. int av_register_protocol(URLProtocol *protocol)
  79. {
  80. return av_register_protocol2(protocol, sizeof(struct URLProtocol_compat));
  81. }
  82. int register_protocol(URLProtocol *protocol)
  83. {
  84. return av_register_protocol(protocol);
  85. }
  86. #endif
  87. static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
  88. const char *filename, int flags)
  89. {
  90. URLContext *uc;
  91. int err;
  92. #if CONFIG_NETWORK
  93. if (!ff_network_init())
  94. return AVERROR(EIO);
  95. #endif
  96. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  97. if (!uc) {
  98. err = AVERROR(ENOMEM);
  99. goto fail;
  100. }
  101. #if LIBAVFORMAT_VERSION_MAJOR >= 53
  102. uc->av_class = &urlcontext_class;
  103. #endif
  104. uc->filename = (char *) &uc[1];
  105. strcpy(uc->filename, filename);
  106. uc->prot = up;
  107. uc->flags = flags;
  108. uc->is_streamed = 0; /* default = not streamed */
  109. uc->max_packet_size = 0; /* default: stream file */
  110. if (up->priv_data_size) {
  111. uc->priv_data = av_mallocz(up->priv_data_size);
  112. if (up->priv_data_class) {
  113. *(const AVClass**)uc->priv_data = up->priv_data_class;
  114. av_opt_set_defaults(uc->priv_data);
  115. }
  116. }
  117. *puc = uc;
  118. return 0;
  119. fail:
  120. *puc = NULL;
  121. #if CONFIG_NETWORK
  122. ff_network_close();
  123. #endif
  124. return err;
  125. }
  126. int url_connect(URLContext* uc)
  127. {
  128. int err = uc->prot->url_open(uc, uc->filename, uc->flags);
  129. if (err)
  130. return err;
  131. uc->is_connected = 1;
  132. //We must be careful here as url_seek() could be slow, for example for http
  133. if( (uc->flags & (URL_WRONLY | URL_RDWR))
  134. || !strcmp(uc->prot->name, "file"))
  135. if(!uc->is_streamed && url_seek(uc, 0, SEEK_SET) < 0)
  136. uc->is_streamed= 1;
  137. return 0;
  138. }
  139. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  140. const char *filename, int flags)
  141. {
  142. int ret;
  143. ret = url_alloc_for_protocol(puc, up, filename, flags);
  144. if (ret)
  145. goto fail;
  146. ret = url_connect(*puc);
  147. if (!ret)
  148. return 0;
  149. fail:
  150. url_close(*puc);
  151. *puc = NULL;
  152. return ret;
  153. }
  154. #define URL_SCHEME_CHARS \
  155. "abcdefghijklmnopqrstuvwxyz" \
  156. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  157. "0123456789+-."
  158. int url_alloc(URLContext **puc, const char *filename, int flags)
  159. {
  160. URLProtocol *up;
  161. char proto_str[128];
  162. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  163. if (filename[proto_len] != ':' || is_dos_path(filename))
  164. strcpy(proto_str, "file");
  165. else
  166. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  167. up = first_protocol;
  168. while (up != NULL) {
  169. if (!strcmp(proto_str, up->name))
  170. return url_alloc_for_protocol (puc, up, filename, flags);
  171. up = up->next;
  172. }
  173. *puc = NULL;
  174. return AVERROR(ENOENT);
  175. }
  176. int url_open(URLContext **puc, const char *filename, int flags)
  177. {
  178. int ret = url_alloc(puc, filename, flags);
  179. if (ret)
  180. return ret;
  181. ret = url_connect(*puc);
  182. if (!ret)
  183. return 0;
  184. url_close(*puc);
  185. *puc = NULL;
  186. return ret;
  187. }
  188. int url_read(URLContext *h, unsigned char *buf, int size)
  189. {
  190. int ret;
  191. if (h->flags & URL_WRONLY)
  192. return AVERROR(EIO);
  193. ret = h->prot->url_read(h, buf, size);
  194. return ret;
  195. }
  196. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  197. {
  198. int ret, len;
  199. int fast_retries = 5;
  200. len = 0;
  201. while (len < size) {
  202. ret = url_read(h, buf+len, size-len);
  203. if (ret == AVERROR(EAGAIN)) {
  204. ret = 0;
  205. if (fast_retries)
  206. fast_retries--;
  207. else
  208. usleep(1000);
  209. } else if (ret < 1)
  210. return ret < 0 ? ret : len;
  211. if (ret)
  212. fast_retries = FFMAX(fast_retries, 2);
  213. len += ret;
  214. }
  215. return len;
  216. }
  217. int url_write(URLContext *h, const unsigned char *buf, int size)
  218. {
  219. int ret;
  220. if (!(h->flags & (URL_WRONLY | URL_RDWR)))
  221. return AVERROR(EIO);
  222. /* avoid sending too big packets */
  223. if (h->max_packet_size && size > h->max_packet_size)
  224. return AVERROR(EIO);
  225. ret = h->prot->url_write(h, buf, size);
  226. return ret;
  227. }
  228. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  229. {
  230. int64_t ret;
  231. if (!h->prot->url_seek)
  232. return AVERROR(ENOSYS);
  233. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  234. return ret;
  235. }
  236. int url_close(URLContext *h)
  237. {
  238. int ret = 0;
  239. if (!h) return 0; /* can happen when url_open fails */
  240. if (h->is_connected && h->prot->url_close)
  241. ret = h->prot->url_close(h);
  242. #if CONFIG_NETWORK
  243. ff_network_close();
  244. #endif
  245. if (h->prot->priv_data_size)
  246. av_free(h->priv_data);
  247. av_free(h);
  248. return ret;
  249. }
  250. int url_exist(const char *filename)
  251. {
  252. URLContext *h;
  253. if (url_open(&h, filename, URL_RDONLY) < 0)
  254. return 0;
  255. url_close(h);
  256. return 1;
  257. }
  258. int64_t url_filesize(URLContext *h)
  259. {
  260. int64_t pos, size;
  261. size= url_seek(h, 0, AVSEEK_SIZE);
  262. if(size<0){
  263. pos = url_seek(h, 0, SEEK_CUR);
  264. if ((size = url_seek(h, -1, SEEK_END)) < 0)
  265. return size;
  266. size++;
  267. url_seek(h, pos, SEEK_SET);
  268. }
  269. return size;
  270. }
  271. int url_get_file_handle(URLContext *h)
  272. {
  273. if (!h->prot->url_get_file_handle)
  274. return -1;
  275. return h->prot->url_get_file_handle(h);
  276. }
  277. int url_get_max_packet_size(URLContext *h)
  278. {
  279. return h->max_packet_size;
  280. }
  281. void url_get_filename(URLContext *h, char *buf, int buf_size)
  282. {
  283. av_strlcpy(buf, h->filename, buf_size);
  284. }
  285. static int default_interrupt_cb(void)
  286. {
  287. return 0;
  288. }
  289. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  290. {
  291. if (!interrupt_cb)
  292. interrupt_cb = default_interrupt_cb;
  293. url_interrupt_cb = interrupt_cb;
  294. }
  295. int av_url_read_pause(URLContext *h, int pause)
  296. {
  297. if (!h->prot->url_read_pause)
  298. return AVERROR(ENOSYS);
  299. return h->prot->url_read_pause(h, pause);
  300. }
  301. int64_t av_url_read_seek(URLContext *h,
  302. int stream_index, int64_t timestamp, int flags)
  303. {
  304. if (!h->prot->url_read_seek)
  305. return AVERROR(ENOSYS);
  306. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  307. }