mp3stream.c 7.6 KB

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