mp3stream.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // Created by Jordy Sipkema on 28/03/16.
  3. //
  4. #include "mp3stream.h"
  5. #include <arpa/inet.h>
  6. #include <sys/bankmem.h>
  7. #include <sys/heap.h>
  8. #include <sys/thread.h>
  9. #include <sys/timer.h>
  10. #include "vs10xx.h"
  11. #define OK 1
  12. #define NOK 0
  13. #define MSIZE 1024
  14. typedef struct _StreamArgs {
  15. FILE *stream;
  16. } TStreamArgs;
  17. // Prototypes - Functions for internal use only! (They wont be visible outside this file!)
  18. THREAD(Mp3Player, args);
  19. void setVolume(void);
  20. int ProcessStreamMetaData(FILE *stream);
  21. // Variables
  22. static bool stream_connected = false;
  23. static bool stream_stopped = false;
  24. static u_char VS_volume = 7; //[0-16]; (Default volume = 7/16
  25. static u_long metaInt = 0;
  26. FILE *stream;
  27. bool connectToStream(u_long ipAddressStream, u_short port, char *radioUrl
  28. )
  29. {
  30. TCPSOCKET *socket;
  31. stream_connected = false;
  32. bool result = true;
  33. char* data;
  34. socket = NutTcpCreateSocket();
  35. if (NutTcpConnect(socket, ipAddressStream, port))
  36. {
  37. // An error has occurred.
  38. printf("ConnectToSteam: Error creating tcp socket.");
  39. NutSleep(5000);
  40. result = false;
  41. return result;
  42. }
  43. stream = _fdopen((int)socket, "r+b");
  44. // Tell the console what we are doing.
  45. printf("GET %s HTTP/1.0\r\n", radioUrl);
  46. fprintf(stream, "GET %s HTTP/1.0\r\n", radioUrl);
  47. fprintf(stream, "Host: %s\r\n", inet_ntoa(ipAddressStream));
  48. fprintf(stream, "User-Agent: Ethernut\r\n");
  49. fprintf(stream, "Accept: */*\r\n");
  50. fprintf(stream, "Icy-MetaData: 1\r\n");
  51. fprintf(stream, "Connection: close\r\n\r\n");
  52. fflush(stream);
  53. // Server will respond with a HTTP-header. Fetch it to the buffer.
  54. stream_connected = true;
  55. data = (char *)malloc(MSIZE * sizeof(char));
  56. while (fgets(data, MSIZE, stream))
  57. {
  58. /*
  59. * Chop off the carriage return at the end of the line. If none
  60. * was found, then this line was probably too large for our buffer.
  61. */
  62. char *cp = strchr(data, '\r');
  63. if (cp == 0) continue; // Input buffer overflow.
  64. *cp = 0;
  65. /*
  66. * The header is terminated by an empty line.
  67. */
  68. if (*data == 0) break;
  69. if (strncmp(data, "icy-metaint:", 12) == 0)
  70. metaInt = atol(data + 12);
  71. printf("%s\n", data);
  72. }
  73. free(data);
  74. return result;
  75. }
  76. bool play()
  77. {
  78. if (stream_connected == false)
  79. return false;
  80. // else:
  81. TStreamArgs *streamArgs = &(TStreamArgs){
  82. .stream = stream,
  83. //.metaint = metaInt
  84. };
  85. NutThreadCreate("Mp3Player", Mp3Player, streamArgs, 512);
  86. printf("Mp3Player thread created. Device should start playing the stream.\n");
  87. return true;
  88. }
  89. u_char volumeUp(void)
  90. {
  91. if (VS_volume >= 16)
  92. return VS_volume;
  93. //else:
  94. ++VS_volume;
  95. VS_volume = VS_volume % 17;
  96. setVolume();
  97. return VS_volume;
  98. }
  99. u_char volumeDown(void)
  100. {
  101. if (VS_volume <= 0)
  102. return VS_volume;
  103. //else:
  104. --VS_volume;
  105. VS_volume = VS_volume % 17;
  106. setVolume();
  107. return VS_volume;
  108. }
  109. void setVolume(void){
  110. u_char volumeToSet = (127 - (VS_volume * 8)) % 128;
  111. VsSetVolume(volumeToSet, volumeToSet);
  112. }
  113. void killPlayerThread(void)
  114. {
  115. stream_stopped = true;
  116. }
  117. THREAD(Mp3Player, args)
  118. //void function(void* args) //TODO: REMOVE THIS, THIS IS ONLY TO TRICK CLION!
  119. {
  120. // Unpack the args passed to the thread initializer.
  121. TStreamArgs *streamArgs = (TStreamArgs *)args;
  122. FILE *stream = streamArgs->stream;
  123. int result = NOK;
  124. size_t rbytes = 0;
  125. char *mp3buf;
  126. u_char ief;
  127. int got = 0;
  128. u_long mp3left = metaInt;
  129. // Init MP3-buffer. NutSegBuf is a global system buffer.
  130. if (0 != NutSegBufInit(8192)){
  131. // Reset the global buffer.
  132. ief = VsPlayerInterrupts(0);
  133. NutSegBufReset();
  134. VsPlayerInterrupts(ief);
  135. result = OK;
  136. }
  137. // Init the VS1003b hardware.
  138. if (result == OK){
  139. if (-1 == VsPlayerInit()) {
  140. if (-1 == VsPlayerReset(0)){
  141. result = NOK;
  142. }
  143. }
  144. }
  145. for(;;)
  146. {
  147. /*
  148. * Query the number of bytes available in the MP3 buffer.
  149. */
  150. ief = VsPlayerInterrupts(0);
  151. mp3buf = NutSegBufWriteRequest(&rbytes);
  152. VsPlayerInterrupts(ief);
  153. /*
  154. * If the player is not running, kick it.
  155. * This should only occur once.
  156. */
  157. if (VsGetStatus() != VS_STATUS_RUNNING) {
  158. puts("Player not running.");
  159. if (rbytes < 1024){
  160. puts("Kick player in 3 2 1..:");
  161. VsPlayerKick();
  162. }
  163. }
  164. /*
  165. * Do not read pass metadata.
  166. * This causes ugly hiccups.
  167. */
  168. if (metaInt && rbytes > mp3left){
  169. rbytes = mp3left;
  170. }
  171. /*
  172. * Read directly into the MP3 buffer.
  173. */
  174. while (rbytes) {
  175. if (stream_stopped == true) {
  176. VsPlayerStop();
  177. NutThreadExit();
  178. }
  179. if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) {
  180. ief = VsPlayerInterrupts(0);
  181. mp3buf = NutSegBufWriteCommit(got);
  182. VsPlayerInterrupts(ief);
  183. if (metaInt) {
  184. mp3left -= got;
  185. if (mp3left == 0) {
  186. ProcessStreamMetaData(stream);
  187. mp3left = metaInt;
  188. }
  189. }
  190. if (got < rbytes && got < 512) {
  191. printf("%lu buffered\n", NutSegBufUsed());
  192. NutSleep(250);
  193. }
  194. else {
  195. NutThreadYield();
  196. }
  197. } else {
  198. break;
  199. }
  200. rbytes -= got;
  201. }
  202. if (got <= 0) break;
  203. } // end for(;;)
  204. }
  205. int ProcessStreamMetaData(FILE *stream)
  206. {
  207. u_char blks = 0;
  208. u_short cnt;
  209. int got;
  210. int rc = 0;
  211. u_char *mbuf;
  212. /*
  213. * Wait for the lenght byte.
  214. */
  215. got = fread(&blks, 1, 1, stream);
  216. if(got != 1) {
  217. return -1;
  218. }
  219. if (blks) {
  220. if (blks > 32) {
  221. printf("Error: Metadata too large, %u blocks\n", blks);
  222. return -1;
  223. }
  224. cnt = blks * 16;
  225. if ((mbuf = malloc(cnt + 1)) == 0) {
  226. printf("Can't malloc memory for metadata parsing\n");
  227. return -1;
  228. }
  229. /*
  230. * Receive the metadata block.
  231. */
  232. for (;;) {
  233. if ((got = fread(mbuf + rc, 1, cnt, stream)) <= 0) {
  234. return -1;
  235. }
  236. if ((cnt -= got) == 0) {
  237. break;
  238. }
  239. rc += got;
  240. mbuf[rc] = 0;
  241. }
  242. printf("\nMeta='%s'\n", mbuf);
  243. free(mbuf);
  244. }
  245. return 0;
  246. }