librtmp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * RTMP network protocol
  3. * Copyright (c) 2010 Howard Chu
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * RTMP protocol based on http://rtmpdump.mplayerhq.hu/ librtmp
  24. */
  25. #include "avformat.h"
  26. #include <librtmp/rtmp.h>
  27. #include <librtmp/log.h>
  28. static void rtmp_log(int level, const char *fmt, va_list args)
  29. {
  30. switch (level) {
  31. default:
  32. case RTMP_LOGCRIT: level = AV_LOG_FATAL; break;
  33. case RTMP_LOGERROR: level = AV_LOG_ERROR; break;
  34. case RTMP_LOGWARNING: level = AV_LOG_WARNING; break;
  35. case RTMP_LOGINFO: level = AV_LOG_INFO; break;
  36. case RTMP_LOGDEBUG: level = AV_LOG_VERBOSE; break;
  37. case RTMP_LOGDEBUG2: level = AV_LOG_DEBUG; break;
  38. }
  39. av_vlog(NULL, level, fmt, args);
  40. av_log(NULL, level, "\n");
  41. }
  42. static int rtmp_close(URLContext *s)
  43. {
  44. RTMP *r = s->priv_data;
  45. RTMP_Close(r);
  46. av_free(r);
  47. return 0;
  48. }
  49. /**
  50. * Open RTMP connection and verify that the stream can be played.
  51. *
  52. * URL syntax: rtmp://server[:port][/app][/playpath][ keyword=value]...
  53. * where 'app' is first one or two directories in the path
  54. * (e.g. /ondemand/, /flash/live/, etc.)
  55. * and 'playpath' is a file name (the rest of the path,
  56. * may be prefixed with "mp4:")
  57. *
  58. * Additional RTMP library options may be appended as
  59. * space-separated key-value pairs.
  60. */
  61. static int rtmp_open(URLContext *s, const char *uri, int flags)
  62. {
  63. RTMP *r;
  64. int rc;
  65. r = av_mallocz(sizeof(RTMP));
  66. if (!r)
  67. return AVERROR(ENOMEM);
  68. switch (av_log_get_level()) {
  69. default:
  70. case AV_LOG_FATAL: rc = RTMP_LOGCRIT; break;
  71. case AV_LOG_ERROR: rc = RTMP_LOGERROR; break;
  72. case AV_LOG_WARNING: rc = RTMP_LOGWARNING; break;
  73. case AV_LOG_INFO: rc = RTMP_LOGINFO; break;
  74. case AV_LOG_VERBOSE: rc = RTMP_LOGDEBUG; break;
  75. case AV_LOG_DEBUG: rc = RTMP_LOGDEBUG2; break;
  76. }
  77. RTMP_LogSetLevel(rc);
  78. RTMP_LogSetCallback(rtmp_log);
  79. RTMP_Init(r);
  80. if (!RTMP_SetupURL(r, s->filename)) {
  81. rc = -1;
  82. goto fail;
  83. }
  84. if (flags & URL_WRONLY)
  85. RTMP_EnableWrite(r);
  86. if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0)) {
  87. rc = -1;
  88. goto fail;
  89. }
  90. s->priv_data = r;
  91. s->is_streamed = 1;
  92. return 0;
  93. fail:
  94. av_free(r);
  95. return rc;
  96. }
  97. static int rtmp_write(URLContext *s, const uint8_t *buf, int size)
  98. {
  99. RTMP *r = s->priv_data;
  100. return RTMP_Write(r, buf, size);
  101. }
  102. static int rtmp_read(URLContext *s, uint8_t *buf, int size)
  103. {
  104. RTMP *r = s->priv_data;
  105. return RTMP_Read(r, buf, size);
  106. }
  107. static int rtmp_read_pause(URLContext *s, int pause)
  108. {
  109. RTMP *r = s->priv_data;
  110. if (!RTMP_Pause(r, pause))
  111. return -1;
  112. return 0;
  113. }
  114. static int64_t rtmp_read_seek(URLContext *s, int stream_index,
  115. int64_t timestamp, int flags)
  116. {
  117. RTMP *r = s->priv_data;
  118. if (flags & AVSEEK_FLAG_BYTE)
  119. return AVERROR(ENOSYS);
  120. /* seeks are in milliseconds */
  121. if (stream_index < 0)
  122. timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
  123. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  124. if (!RTMP_SendSeek(r, timestamp))
  125. return -1;
  126. return timestamp;
  127. }
  128. static int rtmp_get_file_handle(URLContext *s)
  129. {
  130. RTMP *r = s->priv_data;
  131. return RTMP_Socket(r);
  132. }
  133. URLProtocol ff_rtmp_protocol = {
  134. "rtmp",
  135. rtmp_open,
  136. rtmp_read,
  137. rtmp_write,
  138. NULL, /* seek */
  139. rtmp_close,
  140. NULL, /* next */
  141. rtmp_read_pause,
  142. rtmp_read_seek,
  143. rtmp_get_file_handle
  144. };
  145. URLProtocol ff_rtmpt_protocol = {
  146. "rtmpt",
  147. rtmp_open,
  148. rtmp_read,
  149. rtmp_write,
  150. NULL, /* seek */
  151. rtmp_close,
  152. NULL, /* next */
  153. rtmp_read_pause,
  154. rtmp_read_seek,
  155. rtmp_get_file_handle
  156. };
  157. URLProtocol ff_rtmpe_protocol = {
  158. "rtmpe",
  159. rtmp_open,
  160. rtmp_read,
  161. rtmp_write,
  162. NULL, /* seek */
  163. rtmp_close,
  164. NULL, /* next */
  165. rtmp_read_pause,
  166. rtmp_read_seek,
  167. rtmp_get_file_handle
  168. };
  169. URLProtocol ff_rtmpte_protocol = {
  170. "rtmpte",
  171. rtmp_open,
  172. rtmp_read,
  173. rtmp_write,
  174. NULL, /* seek */
  175. rtmp_close,
  176. NULL, /* next */
  177. rtmp_read_pause,
  178. rtmp_read_seek,
  179. rtmp_get_file_handle
  180. };
  181. URLProtocol ff_rtmps_protocol = {
  182. "rtmps",
  183. rtmp_open,
  184. rtmp_read,
  185. rtmp_write,
  186. NULL, /* seek */
  187. rtmp_close,
  188. NULL, /* next */
  189. rtmp_read_pause,
  190. rtmp_read_seek,
  191. rtmp_get_file_handle
  192. };