mp3stream.c 7.4 KB

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