mp3stream.c 6.8 KB

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