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.

224 lines
5.3KB

  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_NOTSUPP;
  123. /* seeks are in milliseconds */
  124. timestamp = av_rescale(timestamp, AV_TIME_BASE, 1000);
  125. if (!RTMP_SendSeek(r, timestamp))
  126. return -1;
  127. return timestamp;
  128. }
  129. static int rtmp_get_file_handle(URLContext *s)
  130. {
  131. RTMP *r = s->priv_data;
  132. return r->m_sb.sb_socket;
  133. }
  134. URLProtocol rtmp_protocol = {
  135. "rtmp",
  136. rtmp_open,
  137. rtmp_read,
  138. rtmp_write,
  139. NULL, /* seek */
  140. rtmp_close,
  141. NULL, /* next */
  142. rtmp_read_pause,
  143. rtmp_read_seek,
  144. rtmp_get_file_handle
  145. };
  146. URLProtocol rtmpt_protocol = {
  147. "rtmpt",
  148. rtmp_open,
  149. rtmp_read,
  150. rtmp_write,
  151. NULL, /* seek */
  152. rtmp_close,
  153. NULL, /* next */
  154. rtmp_read_pause,
  155. rtmp_read_seek,
  156. rtmp_get_file_handle
  157. };
  158. URLProtocol rtmpe_protocol = {
  159. "rtmpe",
  160. rtmp_open,
  161. rtmp_read,
  162. rtmp_write,
  163. NULL, /* seek */
  164. rtmp_close,
  165. NULL, /* next */
  166. rtmp_read_pause,
  167. rtmp_read_seek,
  168. rtmp_get_file_handle
  169. };
  170. URLProtocol rtmpte_protocol = {
  171. "rtmpte",
  172. rtmp_open,
  173. rtmp_read,
  174. rtmp_write,
  175. NULL, /* seek */
  176. rtmp_close,
  177. NULL, /* next */
  178. rtmp_read_pause,
  179. rtmp_read_seek,
  180. rtmp_get_file_handle
  181. };
  182. URLProtocol rtmps_protocol = {
  183. "rtmps",
  184. rtmp_open,
  185. rtmp_read,
  186. rtmp_write,
  187. NULL, /* seek */
  188. rtmp_close,
  189. NULL, /* next */
  190. rtmp_read_pause,
  191. rtmp_read_seek,
  192. rtmp_get_file_handle
  193. };