httpstream.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. while(fgets(line, MAX_HEADERLINE, stream)) {
  95. /*
  96. * Chop off the carriage return at the end of the line. If none
  97. * was found, then this line was probably too large for our buffer.
  98. */
  99. cp = strchr(line, '\r');
  100. if(cp == 0) {
  101. printf("Warning: Input buffer overflow");
  102. continue;
  103. }
  104. *cp = 0;
  105. /*
  106. * The header is terminated by an empty line.
  107. */
  108. if(*line == 0) {
  109. break;
  110. }
  111. if(strncmp(line, "icy-metaint:", 12) == 0) {
  112. *metaint = atol(line + 12);
  113. }
  114. printf("%s\n", line);
  115. }
  116. putchar('\n');
  117. free(line);
  118. }
  119. int ProcessMetaData(FILE *stream)
  120. {
  121. u_char blks = 0;
  122. u_short cnt;
  123. int got;
  124. int rc = 0;
  125. u_char *mbuf;
  126. /*
  127. * Wait for the lenght byte.
  128. */
  129. got = fread(&blks, 1, 1, stream);
  130. if(got != 1) {
  131. return -1;
  132. }
  133. if (blks) {
  134. if (blks > 32) {
  135. printf("Error: Metadata too large, %u blocks\n", blks);
  136. return -1;
  137. }
  138. cnt = blks * 16;
  139. if ((mbuf = malloc(cnt + 1)) == 0) {
  140. return -1;
  141. }
  142. /*
  143. * Receive the metadata block.
  144. */
  145. for (;;) {
  146. if ((got = fread(mbuf + rc, 1, cnt, stream)) <= 0) {
  147. return -1;
  148. }
  149. if ((cnt -= got) == 0) {
  150. break;
  151. }
  152. rc += got;
  153. mbuf[rc] = 0;
  154. }
  155. printf("\nMeta='%s'\n", mbuf);
  156. free(mbuf);
  157. }
  158. return 0;
  159. }
  160. void PlayMp3Stream(FILE *stream, u_long metaint)
  161. {
  162. size_t rbytes = 0;
  163. u_char *mp3buf;
  164. u_char ief;
  165. int got = 0;
  166. u_long last;
  167. u_long mp3left = metaint;
  168. /*
  169. * Initialize the MP3 buffer. The NutSegBuf routines provide a global
  170. * system buffer, which works with banked and non-banked systems.
  171. */
  172. if (NutSegBufInit(8192) == 0) {
  173. puts("Error: MP3 buffer init failed");
  174. return;
  175. }
  176. /*
  177. * Initialize the MP3 decoder hardware.
  178. */
  179. if (VsPlayerReset(0)) {
  180. puts("Error: MP3 hardware init failed");
  181. return;
  182. }
  183. /*
  184. * Reset the MP3 buffer.
  185. */
  186. ief = VsPlayerInterrupts(0);
  187. NutSegBufReset();
  188. VsPlayerInterrupts(ief);
  189. last = NutGetSeconds();
  190. while (isStreaming == true) {
  191. /*
  192. * Query number of byte available in MP3 buffer.
  193. */
  194. ief = VsPlayerInterrupts(0);
  195. mp3buf = NutSegBufWriteRequest(&rbytes);
  196. VsPlayerInterrupts(ief);
  197. /*
  198. * If the player is not running, kick it.
  199. */
  200. if (VsGetStatus() != VS_STATUS_RUNNING) {
  201. puts("Not running");
  202. if(rbytes < 1024 || NutGetSeconds() - last > 4UL) {
  203. last = NutGetSeconds();
  204. puts("Kick player");
  205. VsPlayerKick();
  206. }
  207. }
  208. /*
  209. * Do not read pass metadata.
  210. */
  211. if (metaint && rbytes > mp3left) {
  212. rbytes = mp3left;
  213. }
  214. /*
  215. * Read data directly into the MP3 buffer.
  216. */
  217. while (rbytes && (isStreaming == true)) {
  218. if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) {
  219. ief = VsPlayerInterrupts(0);
  220. mp3buf = NutSegBufWriteCommit(got);
  221. VsPlayerInterrupts(ief);
  222. if (metaint) {
  223. mp3left -= got;
  224. if (mp3left == 0) {
  225. ProcessMetaData(stream);
  226. mp3left = metaint;
  227. }
  228. }
  229. if(got < rbytes && got < 512) {
  230. printf("%lu buffered\n", NutSegBufUsed());
  231. NutSleep(250);
  232. }
  233. else {
  234. NutThreadYield();
  235. }
  236. } else {
  237. break;
  238. }
  239. rbytes -= got;
  240. }
  241. if(got <= 0) {
  242. break;
  243. }
  244. }
  245. }