player_bg.c 2.5 KB

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