httpstream.c 6.7 KB

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