httpstream.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // Created by janco on 10-3-16.
  3. //
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/thread.h>
  7. #include <sys/timer.h>
  8. #include <sys/version.h>
  9. #include <dev/irqreg.h>
  10. #include "vs10xx.h"
  11. #include <sys/socket.h>
  12. #include <netinet/tcp.h>
  13. #include <sys/confnet.h>
  14. #include <arpa/inet.h>
  15. #include <net/route.h>
  16. #include <dev/board.h>
  17. #include <pro/httpd.h>
  18. #include <pro/dhcp.h>
  19. #include <pro/asp.h>
  20. #include <pro/discover.h>
  21. #include <dev/nicrtl.h>
  22. #include "ntp.h"
  23. #include "httpstream.h"
  24. #define MAX_HEADERLINE 512
  25. bool isStreaming;
  26. TCPSOCKET *sock;
  27. u_short mss = 1460;
  28. u_long rx_to = 3000;
  29. u_short tcpbufsiz = 8760;
  30. FILE *stream;
  31. u_long metaint;
  32. void playStream(char *ipaddr, u_short port, char *radiourl){
  33. isStreaming = true;
  34. ConnectStation(sock, inet_addr(ipaddr), port, radiourl, &metaint);
  35. if(stream) {
  36. PlayMp3Stream(stream, metaint);
  37. }
  38. stopStream();
  39. }
  40. void stopStream(){
  41. isStreaming = false;
  42. fclose(stream);
  43. NutTcpCloseSocket(sock);
  44. }
  45. bool HttpIsStreaming(){
  46. return isStreaming;
  47. }
  48. void ConnectStation(TCPSOCKET *sock, u_long ip, u_short port, char *radiourl, u_long *metaint){
  49. int rc;
  50. u_char *line;
  51. u_char *cp;
  52. /*
  53. * Connect the TCP server.
  54. */
  55. if ((sock = NutTcpCreateSocket()) == 0)
  56. printf("Probleem bij het creereen van tcp socket");
  57. if (NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss)))
  58. printf("Probleem bij het creereen van tcp socket");
  59. if (NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to)))
  60. printf("Probleem bij het creereen van tcp socket");
  61. if (NutTcpSetSockOpt(sock, SO_RCVBUF, &tcpbufsiz, sizeof(tcpbufsiz)))
  62. printf("Probleem bij het creereen van tcp socket");
  63. printf("Connecting %s:%u...", inet_ntoa(ip), port);
  64. if ((rc = NutTcpConnect(sock, ip, port))) {
  65. printf("Error: Connect failed with %d\n", ip);
  66. return 0;
  67. }
  68. puts("OK");
  69. if ((stream = _fdopen((int) sock, "r+b")) == 0) {
  70. printf("Error: Can't create stream");
  71. return 0;
  72. }
  73. /*
  74. * Send the HTTP request.
  75. */
  76. printf("GET %s HTTP/1.0\n\n", radiourl);
  77. fprintf(stream, "GET %s HTTP/1.0\r\n", radiourl);
  78. fprintf(stream, "Host: %s\r\n", inet_ntoa(ip));
  79. fprintf(stream, "User-Agent: Ethernut\r\n");
  80. fprintf(stream, "Accept: */*\r\n");
  81. fprintf(stream, "Icy-MetaData: 1\r\n");
  82. fprintf(stream, "Connection: close\r\n");
  83. fputs("\r\n", stream);
  84. fflush(stream);
  85. /*
  86. * Receive the HTTP header.
  87. */
  88. line = malloc(MAX_HEADERLINE);
  89. while(fgets(line, MAX_HEADERLINE, stream)) {
  90. /*
  91. * Chop off the carriage return at the end of the line. If none
  92. * was found, then this line was probably too large for our buffer.
  93. */
  94. cp = strchr(line, '\r');
  95. if(cp == 0) {
  96. printf("Warning: Input buffer overflow");
  97. continue;
  98. }
  99. *cp = 0;
  100. /*
  101. * The header is terminated by an empty line.
  102. */
  103. if(*line == 0) {
  104. break;
  105. }
  106. if(strncmp(line, "icy-metaint:", 12) == 0) {
  107. *metaint = atol(line + 12);
  108. }
  109. printf("%s\n", line);
  110. }
  111. putchar('\n');
  112. free(line);
  113. }
  114. int ProcessMetaData(FILE *stream)
  115. {
  116. u_char blks = 0;
  117. u_short cnt;
  118. int got;
  119. int rc = 0;
  120. u_char *mbuf;
  121. /*
  122. * Wait for the lenght byte.
  123. */
  124. got = fread(&blks, 1, 1, stream);
  125. if(got != 1) {
  126. return -1;
  127. }
  128. if (blks) {
  129. if (blks > 32) {
  130. printf("Error: Metadata too large, %u blocks\n", blks);
  131. return -1;
  132. }
  133. cnt = blks * 16;
  134. if ((mbuf = malloc(cnt + 1)) == 0) {
  135. return -1;
  136. }
  137. /*
  138. * Receive the metadata block.
  139. */
  140. for (;;) {
  141. if ((got = fread(mbuf + rc, 1, cnt, stream)) <= 0) {
  142. return -1;
  143. }
  144. if ((cnt -= got) == 0) {
  145. break;
  146. }
  147. rc += got;
  148. mbuf[rc] = 0;
  149. }
  150. printf("\nMeta='%s'\n", mbuf);
  151. free(mbuf);
  152. }
  153. return 0;
  154. }
  155. void PlayMp3Stream(FILE *stream, u_long metaint)
  156. {
  157. size_t rbytes;
  158. u_char *mp3buf;
  159. u_char ief;
  160. int got = 0;
  161. u_long last;
  162. u_long mp3left = metaint;
  163. /*
  164. * Initialize the MP3 buffer. The NutSegBuf routines provide a global
  165. * system buffer, which works with banked and non-banked systems.
  166. */
  167. if (NutSegBufInit(8192) == 0) {
  168. puts("Error: MP3 buffer init failed");
  169. return;
  170. }
  171. /*
  172. * Initialize the MP3 decoder hardware.
  173. */
  174. if (VsPlayerReset(0)) {
  175. puts("Error: MP3 hardware init failed");
  176. return;
  177. }
  178. /*
  179. * Reset the MP3 buffer.
  180. */
  181. ief = VsPlayerInterrupts(0);
  182. NutSegBufReset();
  183. VsPlayerInterrupts(ief);
  184. last = NutGetSeconds();
  185. while (isStreaming == true) {
  186. /*
  187. * Query number of byte available in MP3 buffer.
  188. */
  189. ief = VsPlayerInterrupts(0);
  190. mp3buf = NutSegBufWriteRequest(&rbytes);
  191. VsPlayerInterrupts(ief);
  192. /*
  193. * If the player is not running, kick it.
  194. */
  195. if (VsGetStatus() != VS_STATUS_RUNNING) {
  196. puts("Not running");
  197. if(rbytes < 1024 || NutGetSeconds() - last > 4UL) {
  198. last = NutGetSeconds();
  199. puts("Kick player");
  200. VsPlayerKick();
  201. }
  202. }
  203. /*
  204. * Do not read pass metadata.
  205. */
  206. if (metaint && rbytes > mp3left) {
  207. rbytes = mp3left;
  208. }
  209. /*
  210. * Read data directly into the MP3 buffer.
  211. */
  212. while (rbytes && (isStreaming == true)) {
  213. if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) {
  214. ief = VsPlayerInterrupts(0);
  215. mp3buf = NutSegBufWriteCommit(got);
  216. VsPlayerInterrupts(ief);
  217. if (metaint) {
  218. mp3left -= got;
  219. if (mp3left == 0) {
  220. ProcessMetaData(stream);
  221. mp3left = metaint;
  222. }
  223. }
  224. if(got < rbytes && got < 512) {
  225. printf("%lu buffered\n", NutSegBufUsed());
  226. NutSleep(250);
  227. }
  228. else {
  229. NutThreadYield();
  230. }
  231. } else {
  232. break;
  233. }
  234. rbytes -= got;
  235. }
  236. if(got <= 0) {
  237. break;
  238. }
  239. }
  240. }