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.

227 lines
5.4KB

  1. /*
  2. * RTMP network protocol
  3. * Copyright (c) 2010 Howard Chu
  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. /**
  22. * @file libavformat/librtmp.c
  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. * Opens RTMP connection and verifies 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. r->Link.protocol |= RTMP_FEATURE_WRITE;
  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, 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 (pause)
  111. r->m_pauseStamp =
  112. r->m_channelTimestamp[r->m_mediaChannel];
  113. if (!RTMP_SendPause(r, pause, r->m_pauseStamp))
  114. return -1;
  115. return 0;
  116. }
  117. static int64_t rtmp_read_seek(URLContext *s, int stream_index,
  118. int64_t timestamp, int flags)
  119. {
  120. RTMP *r = s->priv_data;
  121. if (flags & AVSEEK_FLAG_BYTE)
  122. return AVERROR(ENOSYS);
  123. /* seeks are in milliseconds */
  124. if (stream_index < 0)
  125. timestamp = av_rescale_rnd(timestamp, 1000, AV_TIME_BASE,
  126. flags & AVSEEK_FLAG_BACKWARD ? AV_ROUND_DOWN : AV_ROUND_UP);
  127. if (!RTMP_SendSeek(r, timestamp))
  128. return -1;
  129. return timestamp;
  130. }
  131. static int rtmp_get_file_handle(URLContext *s)
  132. {
  133. RTMP *r = s->priv_data;
  134. return r->m_sb.sb_socket;
  135. }
  136. URLProtocol rtmp_protocol = {
  137. "rtmp",
  138. rtmp_open,
  139. rtmp_read,
  140. rtmp_write,
  141. NULL, /* seek */
  142. rtmp_close,
  143. NULL, /* next */
  144. rtmp_read_pause,
  145. rtmp_read_seek,
  146. rtmp_get_file_handle
  147. };
  148. URLProtocol rtmpt_protocol = {
  149. "rtmpt",
  150. rtmp_open,
  151. rtmp_read,
  152. rtmp_write,
  153. NULL, /* seek */
  154. rtmp_close,
  155. NULL, /* next */
  156. rtmp_read_pause,
  157. rtmp_read_seek,
  158. rtmp_get_file_handle
  159. };
  160. URLProtocol rtmpe_protocol = {
  161. "rtmpe",
  162. rtmp_open,
  163. rtmp_read,
  164. rtmp_write,
  165. NULL, /* seek */
  166. rtmp_close,
  167. NULL, /* next */
  168. rtmp_read_pause,
  169. rtmp_read_seek,
  170. rtmp_get_file_handle
  171. };
  172. URLProtocol rtmpte_protocol = {
  173. "rtmpte",
  174. rtmp_open,
  175. rtmp_read,
  176. rtmp_write,
  177. NULL, /* seek */
  178. rtmp_close,
  179. NULL, /* next */
  180. rtmp_read_pause,
  181. rtmp_read_seek,
  182. rtmp_get_file_handle
  183. };
  184. URLProtocol rtmps_protocol = {
  185. "rtmps",
  186. rtmp_open,
  187. rtmp_read,
  188. rtmp_write,
  189. NULL, /* seek */
  190. rtmp_close,
  191. NULL, /* next */
  192. rtmp_read_pause,
  193. rtmp_read_seek,
  194. rtmp_get_file_handle
  195. };