avio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * unbuffered I/O
  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. #include <unistd.h>
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/dict.h"
  24. #include "libavutil/opt.h"
  25. #include "os_support.h"
  26. #include "avformat.h"
  27. #if CONFIG_NETWORK
  28. #include "network.h"
  29. #endif
  30. #include "url.h"
  31. static URLProtocol *first_protocol = NULL;
  32. URLProtocol *ffurl_protocol_next(URLProtocol *prev)
  33. {
  34. return prev ? prev->next : first_protocol;
  35. }
  36. /** @name Logging context. */
  37. /*@{*/
  38. static const char *urlcontext_to_name(void *ptr)
  39. {
  40. URLContext *h = (URLContext *)ptr;
  41. if(h->prot) return h->prot->name;
  42. else return "NULL";
  43. }
  44. static void *urlcontext_child_next(void *obj, void *prev)
  45. {
  46. URLContext *h = obj;
  47. if (!prev && h->priv_data && h->prot->priv_data_class)
  48. return h->priv_data;
  49. return NULL;
  50. }
  51. static const AVClass *urlcontext_child_class_next(const AVClass *prev)
  52. {
  53. URLProtocol *p = NULL;
  54. /* find the protocol that corresponds to prev */
  55. while (prev && (p = ffurl_protocol_next(p)))
  56. if (p->priv_data_class == prev)
  57. break;
  58. /* find next protocol with priv options */
  59. while (p = ffurl_protocol_next(p))
  60. if (p->priv_data_class)
  61. return p->priv_data_class;
  62. return NULL;
  63. }
  64. static const AVOption options[] = {{NULL}};
  65. const AVClass ffurl_context_class = {
  66. .class_name = "URLContext",
  67. .item_name = urlcontext_to_name,
  68. .option = options,
  69. .version = LIBAVUTIL_VERSION_INT,
  70. .child_next = urlcontext_child_next,
  71. .child_class_next = urlcontext_child_class_next,
  72. };
  73. /*@}*/
  74. #if FF_API_OLD_INTERRUPT_CB
  75. static int default_interrupt_cb(void);
  76. int (*url_interrupt_cb)(void) = default_interrupt_cb;
  77. #endif
  78. URLProtocol *av_protocol_next(URLProtocol *p)
  79. {
  80. return ffurl_protocol_next(p);
  81. }
  82. const char *avio_enum_protocols(void **opaque, int output)
  83. {
  84. URLProtocol **p = opaque;
  85. *p = ffurl_protocol_next(*p);
  86. if (!*p) return NULL;
  87. if ((output && (*p)->url_write) || (!output && (*p)->url_read))
  88. return (*p)->name;
  89. return avio_enum_protocols(opaque, output);
  90. }
  91. int ffurl_register_protocol(URLProtocol *protocol, int size)
  92. {
  93. URLProtocol **p;
  94. if (size < sizeof(URLProtocol)) {
  95. URLProtocol* temp = av_mallocz(sizeof(URLProtocol));
  96. memcpy(temp, protocol, size);
  97. protocol = temp;
  98. }
  99. p = &first_protocol;
  100. while (*p != NULL) p = &(*p)->next;
  101. *p = protocol;
  102. protocol->next = NULL;
  103. return 0;
  104. }
  105. static int url_alloc_for_protocol (URLContext **puc, struct URLProtocol *up,
  106. const char *filename, int flags,
  107. const AVIOInterruptCB *int_cb)
  108. {
  109. URLContext *uc;
  110. int err;
  111. #if CONFIG_NETWORK
  112. if (up->flags & URL_PROTOCOL_FLAG_NETWORK && !ff_network_init())
  113. return AVERROR(EIO);
  114. #endif
  115. uc = av_mallocz(sizeof(URLContext) + strlen(filename) + 1);
  116. if (!uc) {
  117. err = AVERROR(ENOMEM);
  118. goto fail;
  119. }
  120. uc->av_class = &ffurl_context_class;
  121. uc->filename = (char *) &uc[1];
  122. strcpy(uc->filename, filename);
  123. uc->prot = up;
  124. uc->flags = flags;
  125. uc->is_streamed = 0; /* default = not streamed */
  126. uc->max_packet_size = 0; /* default: stream file */
  127. if (up->priv_data_size) {
  128. uc->priv_data = av_mallocz(up->priv_data_size);
  129. if (up->priv_data_class) {
  130. int proto_len= strlen(up->name);
  131. char *start = strchr(uc->filename, ',');
  132. *(const AVClass**)uc->priv_data = up->priv_data_class;
  133. av_opt_set_defaults(uc->priv_data);
  134. if(!strncmp(up->name, uc->filename, proto_len) && uc->filename + proto_len == start){
  135. int ret= 0;
  136. char *p= start;
  137. char sep= *++p;
  138. char *key, *val;
  139. p++;
  140. while(ret >= 0 && (key= strchr(p, sep)) && p<key && (val = strchr(key+1, sep))){
  141. *val= *key= 0;
  142. ret= av_opt_set(uc->priv_data, p, key+1, 0);
  143. if (ret == AVERROR_OPTION_NOT_FOUND)
  144. av_log(uc, AV_LOG_ERROR, "Key '%s' not found.\n", p);
  145. *val= *key= sep;
  146. p= val+1;
  147. }
  148. if(ret<0 || p!=key){
  149. av_log(uc, AV_LOG_ERROR, "Error parsing options string %s\n", start);
  150. av_freep(&uc->priv_data);
  151. av_freep(&uc);
  152. goto fail;
  153. }
  154. memmove(start, key+1, strlen(key));
  155. }
  156. }
  157. }
  158. if (int_cb)
  159. uc->interrupt_callback = *int_cb;
  160. *puc = uc;
  161. return 0;
  162. fail:
  163. *puc = NULL;
  164. #if CONFIG_NETWORK
  165. if (up->flags & URL_PROTOCOL_FLAG_NETWORK)
  166. ff_network_close();
  167. #endif
  168. return err;
  169. }
  170. int ffurl_connect(URLContext* uc, AVDictionary **options)
  171. {
  172. int err =
  173. #if !FF_API_OLD_AVIO
  174. uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
  175. #endif
  176. uc->prot->url_open(uc, uc->filename, uc->flags);
  177. if (err)
  178. return err;
  179. uc->is_connected = 1;
  180. //We must be careful here as ffurl_seek() could be slow, for example for http
  181. if( (uc->flags & AVIO_FLAG_WRITE)
  182. || !strcmp(uc->prot->name, "file"))
  183. if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  184. uc->is_streamed= 1;
  185. return 0;
  186. }
  187. #if FF_API_OLD_AVIO
  188. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  189. const char *filename, int flags)
  190. {
  191. int ret;
  192. ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
  193. if (ret)
  194. goto fail;
  195. ret = ffurl_connect(*puc, NULL);
  196. if (!ret)
  197. return 0;
  198. fail:
  199. ffurl_close(*puc);
  200. *puc = NULL;
  201. return ret;
  202. }
  203. int url_alloc(URLContext **puc, const char *filename, int flags)
  204. {
  205. return ffurl_alloc(puc, filename, flags, NULL);
  206. }
  207. int url_connect(URLContext* uc)
  208. {
  209. return ffurl_connect(uc, NULL);
  210. }
  211. int url_open(URLContext **puc, const char *filename, int flags)
  212. {
  213. return ffurl_open(puc, filename, flags, NULL, NULL);
  214. }
  215. int url_read(URLContext *h, unsigned char *buf, int size)
  216. {
  217. return ffurl_read(h, buf, size);
  218. }
  219. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  220. {
  221. return ffurl_read_complete(h, buf, size);
  222. }
  223. int url_write(URLContext *h, const unsigned char *buf, int size)
  224. {
  225. return ffurl_write(h, buf, size);
  226. }
  227. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  228. {
  229. return ffurl_seek(h, pos, whence);
  230. }
  231. int url_close(URLContext *h)
  232. {
  233. return ffurl_close(h);
  234. }
  235. int64_t url_filesize(URLContext *h)
  236. {
  237. return ffurl_size(h);
  238. }
  239. int url_get_file_handle(URLContext *h)
  240. {
  241. return ffurl_get_file_handle(h);
  242. }
  243. int url_get_max_packet_size(URLContext *h)
  244. {
  245. return h->max_packet_size;
  246. }
  247. void url_get_filename(URLContext *h, char *buf, int buf_size)
  248. {
  249. av_strlcpy(buf, h->filename, buf_size);
  250. }
  251. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  252. {
  253. avio_set_interrupt_cb(interrupt_cb);
  254. }
  255. int av_register_protocol2(URLProtocol *protocol, int size)
  256. {
  257. return ffurl_register_protocol(protocol, size);
  258. }
  259. #endif
  260. #define URL_SCHEME_CHARS \
  261. "abcdefghijklmnopqrstuvwxyz" \
  262. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  263. "0123456789+-."
  264. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  265. const AVIOInterruptCB *int_cb)
  266. {
  267. URLProtocol *up = NULL;
  268. char proto_str[128], proto_nested[128], *ptr;
  269. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  270. if (!first_protocol) {
  271. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  272. "Missing call to av_register_all()?\n");
  273. }
  274. if (filename[proto_len] != ':' &&
  275. (filename[proto_len] != ',' || !strchr(filename + proto_len + 1, ':')) ||
  276. is_dos_path(filename))
  277. strcpy(proto_str, "file");
  278. else
  279. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  280. if ((ptr = strchr(proto_str, ',')))
  281. *ptr = '\0';
  282. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  283. if ((ptr = strchr(proto_nested, '+')))
  284. *ptr = '\0';
  285. while (up = ffurl_protocol_next(up)) {
  286. if (!strcmp(proto_str, up->name))
  287. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  288. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  289. !strcmp(proto_nested, up->name))
  290. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  291. }
  292. *puc = NULL;
  293. return AVERROR(ENOENT);
  294. }
  295. int ffurl_open(URLContext **puc, const char *filename, int flags,
  296. const AVIOInterruptCB *int_cb, AVDictionary **options)
  297. {
  298. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  299. if (ret)
  300. return ret;
  301. if (options && (*puc)->prot->priv_data_class &&
  302. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  303. goto fail;
  304. ret = ffurl_connect(*puc, options);
  305. if (!ret)
  306. return 0;
  307. fail:
  308. ffurl_close(*puc);
  309. *puc = NULL;
  310. return ret;
  311. }
  312. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  313. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  314. {
  315. int ret, len;
  316. int fast_retries = 5;
  317. len = 0;
  318. while (len < size_min) {
  319. ret = transfer_func(h, buf+len, size-len);
  320. if (ret == AVERROR(EINTR))
  321. continue;
  322. if (h->flags & AVIO_FLAG_NONBLOCK)
  323. return ret;
  324. if (ret == AVERROR(EAGAIN)) {
  325. ret = 0;
  326. if (fast_retries)
  327. fast_retries--;
  328. else
  329. usleep(1000);
  330. } else if (ret < 1)
  331. return (ret < 0 && ret != AVERROR_EOF) ? ret : len;
  332. if (ret)
  333. fast_retries = FFMAX(fast_retries, 2);
  334. len += ret;
  335. if (len < size && ff_check_interrupt(&h->interrupt_callback))
  336. return AVERROR_EXIT;
  337. }
  338. return len;
  339. }
  340. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  341. {
  342. if (!(h->flags & AVIO_FLAG_READ))
  343. return AVERROR(EIO);
  344. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  345. }
  346. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  347. {
  348. if (!(h->flags & AVIO_FLAG_READ))
  349. return AVERROR(EIO);
  350. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  351. }
  352. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  353. {
  354. if (!(h->flags & AVIO_FLAG_WRITE))
  355. return AVERROR(EIO);
  356. /* avoid sending too big packets */
  357. if (h->max_packet_size && size > h->max_packet_size)
  358. return AVERROR(EIO);
  359. return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
  360. }
  361. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  362. {
  363. int64_t ret;
  364. if (!h->prot->url_seek)
  365. return AVERROR(ENOSYS);
  366. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  367. return ret;
  368. }
  369. int ffurl_close(URLContext *h)
  370. {
  371. int ret = 0;
  372. if (!h) return 0; /* can happen when ffurl_open fails */
  373. if (h->is_connected && h->prot->url_close)
  374. ret = h->prot->url_close(h);
  375. #if CONFIG_NETWORK
  376. if (h->prot->flags & URL_PROTOCOL_FLAG_NETWORK)
  377. ff_network_close();
  378. #endif
  379. if (h->prot->priv_data_size) {
  380. if (h->prot->priv_data_class)
  381. av_opt_free(h->priv_data);
  382. av_free(h->priv_data);
  383. }
  384. av_free(h);
  385. return ret;
  386. }
  387. #if FF_API_OLD_AVIO
  388. int url_exist(const char *filename)
  389. {
  390. URLContext *h;
  391. if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
  392. return 0;
  393. ffurl_close(h);
  394. return 1;
  395. }
  396. #endif
  397. int avio_check(const char *url, int flags)
  398. {
  399. URLContext *h;
  400. int ret = ffurl_alloc(&h, url, flags, NULL);
  401. if (ret)
  402. return ret;
  403. if (h->prot->url_check) {
  404. ret = h->prot->url_check(h, flags);
  405. } else {
  406. ret = ffurl_connect(h, NULL);
  407. if (ret >= 0)
  408. ret = flags;
  409. }
  410. ffurl_close(h);
  411. return ret;
  412. }
  413. int64_t ffurl_size(URLContext *h)
  414. {
  415. int64_t pos, size;
  416. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  417. if(size<0){
  418. pos = ffurl_seek(h, 0, SEEK_CUR);
  419. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  420. return size;
  421. size++;
  422. ffurl_seek(h, pos, SEEK_SET);
  423. }
  424. return size;
  425. }
  426. int ffurl_get_file_handle(URLContext *h)
  427. {
  428. if (!h->prot->url_get_file_handle)
  429. return -1;
  430. return h->prot->url_get_file_handle(h);
  431. }
  432. #if FF_API_OLD_INTERRUPT_CB
  433. static int default_interrupt_cb(void)
  434. {
  435. return 0;
  436. }
  437. void avio_set_interrupt_cb(int (*interrupt_cb)(void))
  438. {
  439. if (!interrupt_cb)
  440. interrupt_cb = default_interrupt_cb;
  441. url_interrupt_cb = interrupt_cb;
  442. }
  443. #endif
  444. int ff_check_interrupt(AVIOInterruptCB *cb)
  445. {
  446. int ret;
  447. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  448. return ret;
  449. #if FF_API_OLD_INTERRUPT_CB
  450. return url_interrupt_cb();
  451. #else
  452. return 0;
  453. #endif
  454. }
  455. #if FF_API_OLD_AVIO
  456. int av_url_read_pause(URLContext *h, int pause)
  457. {
  458. if (!h->prot->url_read_pause)
  459. return AVERROR(ENOSYS);
  460. return h->prot->url_read_pause(h, pause);
  461. }
  462. int64_t av_url_read_seek(URLContext *h,
  463. int stream_index, int64_t timestamp, int flags)
  464. {
  465. if (!h->prot->url_read_seek)
  466. return AVERROR(ENOSYS);
  467. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  468. }
  469. #endif