httpstream.c 6.6 KB

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