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.

517 lines
14KB

  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 (!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. ff_network_close();
  166. #endif
  167. return err;
  168. }
  169. int ffurl_connect(URLContext* uc, AVDictionary **options)
  170. {
  171. int err =
  172. #if !FF_API_OLD_AVIO
  173. uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags, options) :
  174. #endif
  175. uc->prot->url_open(uc, uc->filename, uc->flags);
  176. if (err)
  177. return err;
  178. uc->is_connected = 1;
  179. //We must be careful here as ffurl_seek() could be slow, for example for http
  180. if( (uc->flags & AVIO_FLAG_WRITE)
  181. || !strcmp(uc->prot->name, "file"))
  182. if(!uc->is_streamed && ffurl_seek(uc, 0, SEEK_SET) < 0)
  183. uc->is_streamed= 1;
  184. return 0;
  185. }
  186. #if FF_API_OLD_AVIO
  187. int url_open_protocol (URLContext **puc, struct URLProtocol *up,
  188. const char *filename, int flags)
  189. {
  190. int ret;
  191. ret = url_alloc_for_protocol(puc, up, filename, flags, NULL);
  192. if (ret)
  193. goto fail;
  194. ret = ffurl_connect(*puc, NULL);
  195. if (!ret)
  196. return 0;
  197. fail:
  198. ffurl_close(*puc);
  199. *puc = NULL;
  200. return ret;
  201. }
  202. int url_alloc(URLContext **puc, const char *filename, int flags)
  203. {
  204. return ffurl_alloc(puc, filename, flags, NULL);
  205. }
  206. int url_connect(URLContext* uc)
  207. {
  208. return ffurl_connect(uc, NULL);
  209. }
  210. int url_open(URLContext **puc, const char *filename, int flags)
  211. {
  212. return ffurl_open(puc, filename, flags, NULL, NULL);
  213. }
  214. int url_read(URLContext *h, unsigned char *buf, int size)
  215. {
  216. return ffurl_read(h, buf, size);
  217. }
  218. int url_read_complete(URLContext *h, unsigned char *buf, int size)
  219. {
  220. return ffurl_read_complete(h, buf, size);
  221. }
  222. int url_write(URLContext *h, const unsigned char *buf, int size)
  223. {
  224. return ffurl_write(h, buf, size);
  225. }
  226. int64_t url_seek(URLContext *h, int64_t pos, int whence)
  227. {
  228. return ffurl_seek(h, pos, whence);
  229. }
  230. int url_close(URLContext *h)
  231. {
  232. return ffurl_close(h);
  233. }
  234. int64_t url_filesize(URLContext *h)
  235. {
  236. return ffurl_size(h);
  237. }
  238. int url_get_file_handle(URLContext *h)
  239. {
  240. return ffurl_get_file_handle(h);
  241. }
  242. int url_get_max_packet_size(URLContext *h)
  243. {
  244. return h->max_packet_size;
  245. }
  246. void url_get_filename(URLContext *h, char *buf, int buf_size)
  247. {
  248. av_strlcpy(buf, h->filename, buf_size);
  249. }
  250. void url_set_interrupt_cb(URLInterruptCB *interrupt_cb)
  251. {
  252. avio_set_interrupt_cb(interrupt_cb);
  253. }
  254. int av_register_protocol2(URLProtocol *protocol, int size)
  255. {
  256. return ffurl_register_protocol(protocol, size);
  257. }
  258. #endif
  259. #define URL_SCHEME_CHARS \
  260. "abcdefghijklmnopqrstuvwxyz" \
  261. "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  262. "0123456789+-."
  263. int ffurl_alloc(URLContext **puc, const char *filename, int flags,
  264. const AVIOInterruptCB *int_cb)
  265. {
  266. URLProtocol *up = NULL;
  267. char proto_str[128], proto_nested[128], *ptr;
  268. size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
  269. if (!first_protocol) {
  270. av_log(NULL, AV_LOG_WARNING, "No URL Protocols are registered. "
  271. "Missing call to av_register_all()?\n");
  272. }
  273. if (filename[proto_len] != ':' && filename[proto_len] != ',' || is_dos_path(filename))
  274. strcpy(proto_str, "file");
  275. else
  276. av_strlcpy(proto_str, filename, FFMIN(proto_len+1, sizeof(proto_str)));
  277. if ((ptr = strchr(proto_str, ',')))
  278. *ptr = '\0';
  279. av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
  280. if ((ptr = strchr(proto_nested, '+')))
  281. *ptr = '\0';
  282. while (up = ffurl_protocol_next(up)) {
  283. if (!strcmp(proto_str, up->name))
  284. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  285. if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
  286. !strcmp(proto_nested, up->name))
  287. return url_alloc_for_protocol (puc, up, filename, flags, int_cb);
  288. }
  289. *puc = NULL;
  290. return AVERROR(ENOENT);
  291. }
  292. int ffurl_open(URLContext **puc, const char *filename, int flags,
  293. const AVIOInterruptCB *int_cb, AVDictionary **options)
  294. {
  295. int ret = ffurl_alloc(puc, filename, flags, int_cb);
  296. if (ret)
  297. return ret;
  298. if (options && (*puc)->prot->priv_data_class &&
  299. (ret = av_opt_set_dict((*puc)->priv_data, options)) < 0)
  300. goto fail;
  301. ret = ffurl_connect(*puc, options);
  302. if (!ret)
  303. return 0;
  304. fail:
  305. ffurl_close(*puc);
  306. *puc = NULL;
  307. return ret;
  308. }
  309. static inline int retry_transfer_wrapper(URLContext *h, unsigned char *buf, int size, int size_min,
  310. int (*transfer_func)(URLContext *h, unsigned char *buf, int size))
  311. {
  312. int ret, len;
  313. int fast_retries = 5;
  314. len = 0;
  315. while (len < size_min) {
  316. ret = transfer_func(h, buf+len, size-len);
  317. if (ret == AVERROR(EINTR))
  318. continue;
  319. if (h->flags & AVIO_FLAG_NONBLOCK)
  320. return ret;
  321. if (ret == AVERROR(EAGAIN)) {
  322. ret = 0;
  323. if (fast_retries)
  324. fast_retries--;
  325. else
  326. usleep(1000);
  327. } else if (ret < 1)
  328. return ret < 0 ? ret : len;
  329. if (ret)
  330. fast_retries = FFMAX(fast_retries, 2);
  331. len += ret;
  332. if (len < size && ff_check_interrupt(&h->interrupt_callback))
  333. return AVERROR_EXIT;
  334. }
  335. return len;
  336. }
  337. int ffurl_read(URLContext *h, unsigned char *buf, int size)
  338. {
  339. if (!(h->flags & AVIO_FLAG_READ))
  340. return AVERROR(EIO);
  341. return retry_transfer_wrapper(h, buf, size, 1, h->prot->url_read);
  342. }
  343. int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
  344. {
  345. if (!(h->flags & AVIO_FLAG_READ))
  346. return AVERROR(EIO);
  347. return retry_transfer_wrapper(h, buf, size, size, h->prot->url_read);
  348. }
  349. int ffurl_write(URLContext *h, const unsigned char *buf, int size)
  350. {
  351. if (!(h->flags & AVIO_FLAG_WRITE))
  352. return AVERROR(EIO);
  353. /* avoid sending too big packets */
  354. if (h->max_packet_size && size > h->max_packet_size)
  355. return AVERROR(EIO);
  356. return retry_transfer_wrapper(h, buf, size, size, (void*)h->prot->url_write);
  357. }
  358. int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
  359. {
  360. int64_t ret;
  361. if (!h->prot->url_seek)
  362. return AVERROR(ENOSYS);
  363. ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
  364. return ret;
  365. }
  366. int ffurl_close(URLContext *h)
  367. {
  368. int ret = 0;
  369. if (!h) return 0; /* can happen when ffurl_open fails */
  370. if (h->is_connected && h->prot->url_close)
  371. ret = h->prot->url_close(h);
  372. #if CONFIG_NETWORK
  373. ff_network_close();
  374. #endif
  375. if (h->prot->priv_data_size) {
  376. if (h->prot->priv_data_class)
  377. av_opt_free(h->priv_data);
  378. av_free(h->priv_data);
  379. }
  380. av_free(h);
  381. return ret;
  382. }
  383. #if FF_API_OLD_AVIO
  384. int url_exist(const char *filename)
  385. {
  386. URLContext *h;
  387. if (ffurl_open(&h, filename, AVIO_FLAG_READ, NULL, NULL) < 0)
  388. return 0;
  389. ffurl_close(h);
  390. return 1;
  391. }
  392. #endif
  393. int avio_check(const char *url, int flags)
  394. {
  395. URLContext *h;
  396. int ret = ffurl_alloc(&h, url, flags, NULL);
  397. if (ret)
  398. return ret;
  399. if (h->prot->url_check) {
  400. ret = h->prot->url_check(h, flags);
  401. } else {
  402. ret = ffurl_connect(h, NULL);
  403. if (ret >= 0)
  404. ret = flags;
  405. }
  406. ffurl_close(h);
  407. return ret;
  408. }
  409. int64_t ffurl_size(URLContext *h)
  410. {
  411. int64_t pos, size;
  412. size= ffurl_seek(h, 0, AVSEEK_SIZE);
  413. if(size<0){
  414. pos = ffurl_seek(h, 0, SEEK_CUR);
  415. if ((size = ffurl_seek(h, -1, SEEK_END)) < 0)
  416. return size;
  417. size++;
  418. ffurl_seek(h, pos, SEEK_SET);
  419. }
  420. return size;
  421. }
  422. int ffurl_get_file_handle(URLContext *h)
  423. {
  424. if (!h->prot->url_get_file_handle)
  425. return -1;
  426. return h->prot->url_get_file_handle(h);
  427. }
  428. #if FF_API_OLD_INTERRUPT_CB
  429. static int default_interrupt_cb(void)
  430. {
  431. return 0;
  432. }
  433. void avio_set_interrupt_cb(int (*interrupt_cb)(void))
  434. {
  435. if (!interrupt_cb)
  436. interrupt_cb = default_interrupt_cb;
  437. url_interrupt_cb = interrupt_cb;
  438. }
  439. #endif
  440. int ff_check_interrupt(AVIOInterruptCB *cb)
  441. {
  442. int ret;
  443. if (cb && cb->callback && (ret = cb->callback(cb->opaque)))
  444. return ret;
  445. #if FF_API_OLD_INTERRUPT_CB
  446. return url_interrupt_cb();
  447. #else
  448. return 0;
  449. #endif
  450. }
  451. #if FF_API_OLD_AVIO
  452. int av_url_read_pause(URLContext *h, int pause)
  453. {
  454. if (!h->prot->url_read_pause)
  455. return AVERROR(ENOSYS);
  456. return h->prot->url_read_pause(h, pause);
  457. }
  458. int64_t av_url_read_seek(URLContext *h,
  459. int stream_index, int64_t timestamp, int flags)
  460. {
  461. if (!h->prot->url_read_seek)
  462. return AVERROR(ENOSYS);
  463. return h->prot->url_read_seek(h, stream_index, timestamp, flags);
  464. }
  465. #endif