rtp.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @file
  3. * RTP client/server module
  4. *
  5. */
  6. /*
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  21. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  23. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  26. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. * OF SUCH DAMAGE.
  28. *
  29. * This file is part of the lwIP TCP/IP stack.
  30. *
  31. */
  32. #include "lwip/opt.h"
  33. #if LWIP_SOCKET && LWIP_IGMP /* don't build if not configured for use in lwipopts.h */
  34. #include "lwip/sys.h"
  35. #include "lwip/sockets.h"
  36. #include "rtp.h"
  37. #include "rtpdata.h"
  38. #include <string.h>
  39. /** This is an example of a "RTP" client/server based on a MPEG4 bitstream (with socket API).
  40. */
  41. /**
  42. * RTP_DEBUG: Enable debugging for RTP.
  43. */
  44. #ifndef RTP_DEBUG
  45. #define RTP_DEBUG LWIP_DBG_ON
  46. #endif
  47. /** RTP stream port */
  48. #ifndef RTP_STREAM_PORT
  49. #define RTP_STREAM_PORT 4000
  50. #endif
  51. /** RTP stream multicast address as IPv4 address in "u32_t" format */
  52. #ifndef RTP_STREAM_ADDRESS
  53. #define RTP_STREAM_ADDRESS inet_addr("232.0.0.0")
  54. #endif
  55. /** RTP send delay - in milliseconds */
  56. #ifndef RTP_SEND_DELAY
  57. #define RTP_SEND_DELAY 40
  58. #endif
  59. /** RTP receive timeout - in milliseconds */
  60. #ifndef RTP_RECV_TIMEOUT
  61. #define RTP_RECV_TIMEOUT 2000
  62. #endif
  63. /** RTP stats display period - in received packets */
  64. #ifndef RTP_RECV_STATS
  65. #define RTP_RECV_STATS 50
  66. #endif
  67. /** RTP macro to let the application process the data */
  68. #ifndef RTP_RECV_PROCESSING
  69. #define RTP_RECV_PROCESSING(p,s)
  70. #endif
  71. /** RTP packet/payload size */
  72. #define RTP_PACKET_SIZE 1500
  73. #define RTP_PAYLOAD_SIZE 1024
  74. /** RTP header constants */
  75. #define RTP_VERSION 0x80
  76. #define RTP_TIMESTAMP_INCREMENT 3600
  77. #define RTP_SSRC 0
  78. #define RTP_PAYLOADTYPE 96
  79. #define RTP_MARKER_MASK 0x80
  80. /** RTP message header */
  81. #ifdef PACK_STRUCT_USE_INCLUDES
  82. # include "arch/bpstruct.h"
  83. #endif
  84. PACK_STRUCT_BEGIN
  85. struct rtp_hdr {
  86. PACK_STRUCT_FLD_8(u8_t version);
  87. PACK_STRUCT_FLD_8(u8_t payloadtype);
  88. PACK_STRUCT_FIELD(u16_t seqNum);
  89. PACK_STRUCT_FIELD(u32_t timestamp);
  90. PACK_STRUCT_FIELD(u32_t ssrc);
  91. } PACK_STRUCT_STRUCT;
  92. PACK_STRUCT_END
  93. #ifdef PACK_STRUCT_USE_INCLUDES
  94. # include "arch/epstruct.h"
  95. #endif
  96. /** RTP packets */
  97. static u8_t rtp_send_packet[RTP_PACKET_SIZE];
  98. static u8_t rtp_recv_packet[RTP_PACKET_SIZE];
  99. /**
  100. * RTP send packets
  101. */
  102. static void
  103. rtp_send_packets( int sock, struct sockaddr_in* to)
  104. {
  105. struct rtp_hdr* rtphdr;
  106. u8_t* rtp_payload;
  107. size_t rtp_payload_size;
  108. size_t rtp_data_index;
  109. /* prepare RTP packet */
  110. rtphdr = (struct rtp_hdr*)rtp_send_packet;
  111. rtphdr->version = RTP_VERSION;
  112. rtphdr->payloadtype = 0;
  113. rtphdr->ssrc = PP_HTONL(RTP_SSRC);
  114. rtphdr->timestamp = lwip_htonl(lwip_ntohl(rtphdr->timestamp) + RTP_TIMESTAMP_INCREMENT);
  115. /* send RTP stream packets */
  116. rtp_data_index = 0;
  117. do {
  118. rtp_payload = rtp_send_packet+sizeof(struct rtp_hdr);
  119. rtp_payload_size = LWIP_MIN(RTP_PAYLOAD_SIZE, sizeof(rtp_data) - rtp_data_index);
  120. MEMCPY(rtp_payload, rtp_data + rtp_data_index, rtp_payload_size);
  121. /* set MARKER bit in RTP header on the last packet of an image */
  122. if ((rtp_data_index + rtp_payload_size) >= sizeof(rtp_data)) {
  123. rtphdr->payloadtype = RTP_PAYLOADTYPE | RTP_MARKER_MASK;
  124. } else {
  125. rtphdr->payloadtype = RTP_PAYLOADTYPE;
  126. }
  127. /* send RTP stream packet */
  128. if (lwip_sendto(sock, rtp_send_packet, sizeof(struct rtp_hdr) + rtp_payload_size,
  129. 0, (struct sockaddr *)to, sizeof(struct sockaddr)) >= 0) {
  130. rtphdr->seqNum = lwip_htons((u16_t)(lwip_ntohs(rtphdr->seqNum) + 1));
  131. rtp_data_index += rtp_payload_size;
  132. } else {
  133. LWIP_DEBUGF(RTP_DEBUG, ("rtp_sender: not sendto==%i\n", errno));
  134. }
  135. }while (rtp_data_index < sizeof(rtp_data));
  136. }
  137. /**
  138. * RTP send thread
  139. */
  140. static void
  141. rtp_send_thread(void *arg)
  142. {
  143. int sock;
  144. struct sockaddr_in local;
  145. struct sockaddr_in to;
  146. u32_t rtp_stream_address;
  147. LWIP_UNUSED_ARG(arg);
  148. /* initialize RTP stream address */
  149. rtp_stream_address = RTP_STREAM_ADDRESS;
  150. /* if we got a valid RTP stream address... */
  151. if (rtp_stream_address != 0) {
  152. /* create new socket */
  153. sock = lwip_socket(AF_INET, SOCK_DGRAM, 0);
  154. if (sock >= 0) {
  155. /* prepare local address */
  156. memset(&local, 0, sizeof(local));
  157. local.sin_family = AF_INET;
  158. local.sin_port = PP_HTONS(INADDR_ANY);
  159. local.sin_addr.s_addr = PP_HTONL(INADDR_ANY);
  160. /* bind to local address */
  161. if (lwip_bind(sock, (struct sockaddr *)&local, sizeof(local)) == 0) {
  162. /* prepare RTP stream address */
  163. memset(&to, 0, sizeof(to));
  164. to.sin_family = AF_INET;
  165. to.sin_port = PP_HTONS(RTP_STREAM_PORT);
  166. to.sin_addr.s_addr = rtp_stream_address;
  167. /* send RTP packets */
  168. memset(rtp_send_packet, 0, sizeof(rtp_send_packet));
  169. while (1) {
  170. rtp_send_packets( sock, &to);
  171. sys_msleep(RTP_SEND_DELAY);
  172. }
  173. }
  174. /* close the socket */
  175. lwip_close(sock);
  176. }
  177. }
  178. }
  179. /**
  180. * RTP recv thread
  181. */
  182. static void
  183. rtp_recv_thread(void *arg)
  184. {
  185. int sock;
  186. struct sockaddr_in local;
  187. struct sockaddr_in from;
  188. int fromlen;
  189. struct ip_mreq ipmreq;
  190. struct rtp_hdr* rtphdr;
  191. u32_t rtp_stream_address;
  192. int timeout;
  193. int result;
  194. int recvrtppackets = 0;
  195. int lostrtppackets = 0;
  196. u16_t lastrtpseq = 0;
  197. LWIP_UNUSED_ARG(arg);
  198. /* initialize RTP stream address */
  199. rtp_stream_address = RTP_STREAM_ADDRESS;
  200. /* if we got a valid RTP stream address... */
  201. if (rtp_stream_address != 0) {
  202. /* create new socket */
  203. sock = lwip_socket(AF_INET, SOCK_DGRAM, 0);
  204. if (sock >= 0) {
  205. /* prepare local address */
  206. memset(&local, 0, sizeof(local));
  207. local.sin_family = AF_INET;
  208. local.sin_port = PP_HTONS(RTP_STREAM_PORT);
  209. local.sin_addr.s_addr = PP_HTONL(INADDR_ANY);
  210. /* bind to local address */
  211. if (lwip_bind(sock, (struct sockaddr *)&local, sizeof(local)) == 0) {
  212. /* set recv timeout */
  213. timeout = RTP_RECV_TIMEOUT;
  214. result = lwip_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  215. if (result) {
  216. LWIP_DEBUGF(RTP_DEBUG, ("rtp_recv_thread: setsockopt(SO_RCVTIMEO) failed: errno=%d\n", errno));
  217. }
  218. /* prepare multicast "ip_mreq" struct */
  219. ipmreq.imr_multiaddr.s_addr = rtp_stream_address;
  220. ipmreq.imr_interface.s_addr = PP_HTONL(INADDR_ANY);
  221. /* join multicast group */
  222. if (lwip_setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &ipmreq, sizeof(ipmreq)) == 0) {
  223. /* receive RTP packets */
  224. while(1) {
  225. fromlen = sizeof(from);
  226. result = lwip_recvfrom(sock, rtp_recv_packet, sizeof(rtp_recv_packet), 0,
  227. (struct sockaddr *)&from, (socklen_t *)&fromlen);
  228. if ((result > 0) && ((size_t)result >= sizeof(struct rtp_hdr))) {
  229. size_t recved = (size_t)result;
  230. rtphdr = (struct rtp_hdr *)rtp_recv_packet;
  231. recvrtppackets++;
  232. if ((lastrtpseq == 0) || ((lastrtpseq + 1) == lwip_ntohs(rtphdr->seqNum))) {
  233. RTP_RECV_PROCESSING((rtp_recv_packet + sizeof(rtp_hdr)), (recved-sizeof(rtp_hdr)));
  234. LWIP_UNUSED_ARG(recved); /* just in case... */
  235. } else {
  236. lostrtppackets++;
  237. }
  238. lastrtpseq = lwip_ntohs(rtphdr->seqNum);
  239. if ((recvrtppackets % RTP_RECV_STATS) == 0) {
  240. LWIP_DEBUGF(RTP_DEBUG, ("rtp_recv_thread: recv %6i packet(s) / lost %4i packet(s) (%.4f%%)...\n", recvrtppackets, lostrtppackets, (lostrtppackets*100.0)/recvrtppackets));
  241. }
  242. } else {
  243. LWIP_DEBUGF(RTP_DEBUG, ("rtp_recv_thread: recv timeout...\n"));
  244. }
  245. }
  246. /* leave multicast group */
  247. /* TODO: this code is never reached
  248. result = lwip_setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, &ipmreq, sizeof(ipmreq));
  249. if (result) {
  250. LWIP_DEBUGF(RTP_DEBUG, ("rtp_recv_thread: setsockopt(IP_DROP_MEMBERSHIP) failed: errno=%d\n", errno));
  251. }*/
  252. }
  253. }
  254. /* close the socket */
  255. lwip_close(sock);
  256. }
  257. }
  258. }
  259. void
  260. rtp_init(void)
  261. {
  262. sys_thread_new("rtp_send_thread", rtp_send_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  263. sys_thread_new("rtp_recv_thread", rtp_recv_thread, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
  264. }
  265. #endif /* LWIP_SOCKET && LWIP_IGMP */