mp3stream.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. static char VS_StreamInfo[17] = " No info ";
  31. FILE *stream;
  32. TCPSOCKET *socket;
  33. bool connectToStream(char *ipaddr, u_short port, char *radioUrl)
  34. {
  35. if (stream_connected == true)
  36. return false;
  37. stream_connected = false;
  38. bool result = true;
  39. char* data;
  40. socket = NutTcpCreateSocket();
  41. printf("Connecting to %s:%d%s\r\n", ipaddr, port, radioUrl);
  42. if (NutTcpConnect(socket, inet_addr(ipaddr), port))
  43. {
  44. // An error has occurred.
  45. printf("ConnectToStream: Error creating tcp socket.\n");
  46. NutSleep(5000);
  47. result = false;
  48. return result;
  49. }
  50. stream = _fdopen((int)socket, "r+b");
  51. fprintf(stream, "GET %s HTTP/1.0\r\n", radioUrl);
  52. fprintf(stream, "Host: %s\r\n", ipaddr);
  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. void 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. }
  107. void volumeDown(void)
  108. {
  109. if (VS_volume <= 0)
  110. return VS_volume;
  111. //else:
  112. --VS_volume;
  113. VS_volume = VS_volume % 17;
  114. setVolume();
  115. }
  116. void setVolumeManual(char level){
  117. u_char v_level = level * 16 / 100;
  118. VS_volume = v_level % 17;
  119. setVolume();
  120. setCurrentDisplay(DISPLAY_Volume, 5);
  121. }
  122. void setVolume(void){
  123. u_char volumeToSet = (128 - (VS_volume * 8)) % 129;
  124. VsSetVolume(volumeToSet, volumeToSet);
  125. printf("- VS_volume level: %d/16\n", VS_volume);
  126. }
  127. u_char getVolume(void){
  128. return VS_volume;
  129. }
  130. char* getStreamInfo(void){
  131. return VS_StreamInfo;
  132. }
  133. void killPlayerThread(void)
  134. {
  135. printf("Signal to stop the stream sent.\n");
  136. stream_stopped = true;
  137. }
  138. THREAD(Mp3Player, args)
  139. //void function(void* args) //TODO: REMOVE THIS, THIS IS ONLY TO TRICK CLION!
  140. {
  141. // Unpack the args passed to the thread initializer.
  142. TStreamArgs *streamArgs = (TStreamArgs *)args;
  143. FILE *stream = streamArgs->stream;
  144. int result = NOK;
  145. size_t rbytes = 0;
  146. char *mp3buf;
  147. u_char ief;
  148. int got = 0;
  149. u_long mp3left = metaInt;
  150. // Init MP3-buffer. NutSegBuf is a global system buffer.
  151. if (0 != NutSegBufInit(NUTSEGBUFFER)){
  152. // Reset the global buffer.
  153. ief = VsPlayerInterrupts(0);
  154. NutSegBufReset();
  155. VsPlayerInterrupts(ief);
  156. result = OK;
  157. }
  158. // Init the VS1003b hardware.
  159. if (result == OK){
  160. if (-1 == VsPlayerInit()) {
  161. if (-1 == VsPlayerReset(0)){
  162. result = NOK;
  163. }
  164. }
  165. }
  166. // Set the volume to the correct level
  167. setVolume();
  168. for(;;)
  169. {
  170. /*
  171. * Query the number of bytes available in the MP3 buffer.
  172. */
  173. ief = VsPlayerInterrupts(0);
  174. mp3buf = NutSegBufWriteRequest(&rbytes);
  175. VsPlayerInterrupts(ief);
  176. /*
  177. * If the player is not running, kick it.
  178. * This should only occur once.
  179. */
  180. if (VsGetStatus() != VS_STATUS_RUNNING) {
  181. puts("Player not running.");
  182. if (rbytes < 1024){
  183. puts("Kick player in 3 2 1..:");
  184. VsPlayerKick();
  185. }
  186. }
  187. /*
  188. * Do not read pass metadata.
  189. * This causes ugly hiccups.
  190. */
  191. if (metaInt && rbytes > mp3left){
  192. rbytes = mp3left;
  193. }
  194. /*
  195. * Read directly into the MP3 buffer.
  196. */
  197. while (rbytes) {
  198. if (stream_stopped == true) {
  199. stopMp3PlayerThread();
  200. }
  201. if ((got = fread(mp3buf, 1, rbytes, stream)) > 0) {
  202. ief = VsPlayerInterrupts(0);
  203. mp3buf = NutSegBufWriteCommit(got);
  204. VsPlayerInterrupts(ief);
  205. if (metaInt) {
  206. mp3left -= got;
  207. if (mp3left == 0) {
  208. ProcessStreamMetaData(stream);
  209. mp3left = metaInt;
  210. }
  211. }
  212. if (got < rbytes && got < 512) {
  213. printf("%lu buffered\n", NutSegBufUsed());
  214. NutSleep(250);
  215. }
  216. else {
  217. NutThreadYield();
  218. }
  219. } else {
  220. break;
  221. }
  222. rbytes -= got;
  223. }
  224. if (got <= 0) break;
  225. } // end for(;;)
  226. while (NutSegBufUsed() > 10){
  227. NutSleep(250);
  228. }
  229. stopMp3PlayerThread();
  230. }
  231. void stopMp3PlayerThread(void)
  232. {
  233. printf("Signal to stop the stream recieved\n.");
  234. stream_connected = false;
  235. stream_isplaying = false;
  236. VsPlayerStop();
  237. NutTcpCloseSocket(socket);
  238. NutThreadExit();
  239. }
  240. int ProcessStreamMetaData(FILE *stream)
  241. {
  242. u_char blks = 0;
  243. u_short cnt;
  244. int got;
  245. int rc = 0;
  246. u_char *mbuf;
  247. /*
  248. * Wait for the lenght byte.
  249. */
  250. got = fread(&blks, 1, 1, stream);
  251. if(got != 1) {
  252. return -1;
  253. }
  254. if (blks) {
  255. if (blks > 32) {
  256. printf("Error: Metadata too large, %u blocks\n", blks);
  257. return -1;
  258. }
  259. cnt = blks * 16;
  260. if ((mbuf = malloc(cnt + 1)) == 0) {
  261. printf("Can't malloc memory for metadata parsing\n");
  262. return -1;
  263. }
  264. /*
  265. * Receive the metadata block.
  266. */
  267. for (;;) {
  268. if ((got = fread(mbuf + rc, 1, cnt, stream)) <= 0) {
  269. return -1;
  270. }
  271. if ((cnt -= got) == 0) {
  272. break;
  273. }
  274. rc += got;
  275. mbuf[rc] = 0;
  276. }
  277. printf("================================================ Func: ProcessStreamMetaData\n");
  278. printf("\nMeta='%s'\n", mbuf);
  279. char* found = strstr(mbuf, "StreamTitle=");
  280. if (found != 0){
  281. char* first = strstr(mbuf, "'") + 1;
  282. char* last = strstr(first, "'");
  283. size_t diff = last - first;
  284. if (diff > 16){ diff = 16; }
  285. strncpy(VS_StreamInfo, first, diff);
  286. VS_StreamInfo[16] = '\0';
  287. printf("Found: %s\n\n", VS_StreamInfo);
  288. }
  289. setCurrentDisplay(DISPLAY_StreamInfo, 6);
  290. free(mbuf);
  291. }
  292. return 0;
  293. }