mp3stream.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. void 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. }
  105. void volumeDown(void)
  106. {
  107. if (VS_volume <= 0)
  108. return VS_volume;
  109. //else:
  110. --VS_volume;
  111. VS_volume = VS_volume % 17;
  112. setVolume();
  113. }
  114. void setVolume(void){
  115. u_char volumeToSet = (128 - (VS_volume * 8)) % 129;
  116. VsSetVolume(volumeToSet, volumeToSet);
  117. printf("- VS_volume level: %d/16\n", VS_volume);
  118. }
  119. u_char getVolume(void){
  120. return VS_volume;
  121. }
  122. void killPlayerThread(void)
  123. {
  124. printf("Signal to stop the stream sent.\n");
  125. stream_stopped = true;
  126. }
  127. THREAD(Mp3Player, args)
  128. //void function(void* args) //TODO: REMOVE THIS, THIS IS ONLY TO TRICK CLION!
  129. {
  130. // Unpack the args passed to the thread initializer.
  131. TStreamArgs *streamArgs = (TStreamArgs *)args;
  132. FILE *stream = streamArgs->stream;
  133. int result = NOK;
  134. size_t rbytes = 0;
  135. char *mp3buf;
  136. u_char ief;
  137. int got = 0;
  138. u_long mp3left = metaInt;
  139. // Init MP3-buffer. NutSegBuf is a global system buffer.
  140. if (0 != NutSegBufInit(NUTSEGBUFFER)){
  141. // Reset the global buffer.
  142. ief = VsPlayerInterrupts(0);
  143. NutSegBufReset();
  144. VsPlayerInterrupts(ief);
  145. result = OK;
  146. }
  147. // Init the VS1003b hardware.
  148. if (result == OK){
  149. if (-1 == VsPlayerInit()) {
  150. if (-1 == VsPlayerReset(0)){
  151. result = NOK;
  152. }
  153. }
  154. }
  155. // Set the volume to the correct level
  156. setVolume();
  157. for(;;)
  158. {
  159. /*
  160. * Query the number of bytes available in the MP3 buffer.
  161. */
  162. ief = VsPlayerInterrupts(0);
  163. mp3buf = NutSegBufWriteRequest(&rbytes);
  164. VsPlayerInterrupts(ief);
  165. /*
  166. * If the player is not running, kick it.
  167. * This should only occur once.
  168. */
  169. if (VsGetStatus() != VS_STATUS_RUNNING) {
  170. puts("Player not running.");
  171. if (rbytes < 1024){
  172. puts("Kick player in 3 2 1..:");
  173. VsPlayerKick();
  174. }
  175. }
  176. /*
  177. * Do not read pass metadata.
  178. * This causes ugly hiccups.
  179. */
  180. if (metaInt && rbytes > mp3left){
  181. rbytes = mp3left;
  182. }
  183. /*
  184. * Read directly into the MP3 buffer.
  185. */
  186. while (rbytes) {
  187. if (stream_stopped == true) {
  188. stopMp3PlayerThread();
  189. }
  190. if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) {
  191. ief = VsPlayerInterrupts(0);
  192. mp3buf = NutSegBufWriteCommit(got);
  193. VsPlayerInterrupts(ief);
  194. if (metaInt) {
  195. mp3left -= got;
  196. if (mp3left == 0) {
  197. ProcessStreamMetaData(stream);
  198. mp3left = metaInt;
  199. }
  200. }
  201. if (got < rbytes && got < 512) {
  202. printf("%lu buffered\n", NutSegBufUsed());
  203. NutSleep(250);
  204. }
  205. else {
  206. NutThreadYield();
  207. }
  208. } else {
  209. break;
  210. }
  211. rbytes -= got;
  212. }
  213. if (got <= 0) break;
  214. } // end for(;;)
  215. while (NutSegBufUsed() > 10){
  216. NutSleep(250);
  217. }
  218. stopMp3PlayerThread();
  219. }
  220. void stopMp3PlayerThread(void)
  221. {
  222. printf("Signal to stop the stream recieved\n.");
  223. stream_connected = false;
  224. stream_isplaying = false;
  225. VsPlayerStop();
  226. NutTcpCloseSocket(socket);
  227. NutThreadExit();
  228. }
  229. int ProcessStreamMetaData(FILE *stream)
  230. {
  231. u_char blks = 0;
  232. u_short cnt;
  233. int got;
  234. int rc = 0;
  235. u_char *mbuf;
  236. /*
  237. * Wait for the lenght byte.
  238. */
  239. got = fread(&blks, 1, 1, stream);
  240. if(got != 1) {
  241. return -1;
  242. }
  243. if (blks) {
  244. if (blks > 32) {
  245. printf("Error: Metadata too large, %u blocks\n", blks);
  246. return -1;
  247. }
  248. cnt = blks * 16;
  249. if ((mbuf = malloc(cnt + 1)) == 0) {
  250. printf("Can't malloc memory for metadata parsing\n");
  251. return -1;
  252. }
  253. /*
  254. * Receive the metadata block.
  255. */
  256. for (;;) {
  257. if ((got = fread(mbuf + rc, 1, cnt, stream)) <= 0) {
  258. return -1;
  259. }
  260. if ((cnt -= got) == 0) {
  261. break;
  262. }
  263. rc += got;
  264. mbuf[rc] = 0;
  265. }
  266. printf("\nMeta='%s'\n", mbuf);
  267. free(mbuf);
  268. }
  269. return 0;
  270. }