player_bg.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "player_bg.h"
  2. #include "player_ui.h"
  3. #include "mp3.h"
  4. #include <string.h>
  5. rt_bool_t is_playing = RT_FALSE;
  6. static rt_mq_t player_thread_mq;
  7. static struct rt_thread player_thread_tid;
  8. static rt_uint8_t player_thread_stack[0x400];
  9. rt_bool_t player_is_playing()
  10. {
  11. return is_playing;
  12. }
  13. void player_play_req(const char* fn)
  14. {
  15. struct player_request request;
  16. request.type = PLAYER_REQUEST_PLAY_SINGLE_FILE;
  17. strncpy(request.fn, fn, sizeof(request.fn));
  18. /* send to message queue */
  19. rt_mq_send(player_thread_mq, (void*)&request, sizeof(struct player_request));
  20. }
  21. void player_stop_req()
  22. {
  23. is_playing = RT_FALSE;
  24. }
  25. void player_thread(void* parameter)
  26. {
  27. rt_err_t result;
  28. struct player_request request;
  29. while(1)
  30. {
  31. /* get request from message queue */
  32. result = rt_mq_recv(player_thread_mq, (void*)&request,
  33. sizeof(struct player_request), RT_WAITING_FOREVER);
  34. if (result == RT_EOK)
  35. {
  36. switch (request.type)
  37. {
  38. case PLAYER_REQUEST_PLAY_SINGLE_FILE:
  39. if ((strstr(request.fn, ".mp3") != RT_NULL) ||
  40. (strstr(request.fn, ".MP3") != RT_NULL))
  41. {
  42. is_playing = RT_TRUE;
  43. player_notify_play();
  44. /* get music tag information */
  45. mp3(request.fn);
  46. player_notify_stop();
  47. is_playing = RT_FALSE;
  48. }
  49. else if ((strstr(request.fn, ".wav") != RT_NULL) ||
  50. (strstr(request.fn, ".WAV") != RT_NULL))
  51. {
  52. is_playing = RT_TRUE;
  53. player_notify_play();
  54. wav(request.fn);
  55. player_notify_stop();
  56. is_playing = RT_FALSE;
  57. }
  58. else if ((strstr(request.fn, "http://") == request.fn ||
  59. (strstr(request.fn, "HTTP://") == request.fn )))
  60. {
  61. is_playing = RT_TRUE;
  62. player_notify_play();
  63. ice_mp3(request.fn);
  64. /* notfiy net buffer worker to stop */
  65. net_buf_stop_job();
  66. player_notify_stop();
  67. is_playing = RT_FALSE;
  68. }
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. void player_init()
  75. {
  76. rt_err_t result;
  77. /* create player thread */
  78. player_thread_mq = rt_mq_create("player", sizeof(struct player_request),
  79. 8, RT_IPC_FLAG_FIFO);
  80. RT_ASSERT(player_thread_mq != RT_NULL);
  81. result = rt_thread_init(&player_thread_tid, "ply_bg", player_thread, RT_NULL,
  82. player_thread_stack, sizeof(player_thread_stack),
  83. 20, 5);
  84. if (result != RT_EOK) rt_kprintf("player thread init failed\n");
  85. else
  86. {
  87. rt_thread_startup(&player_thread_tid);
  88. player_ui_init();
  89. }
  90. }