mp3stream.c 7.5 KB

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