video_player.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "video_player.h"
  2. #include "video_player_hal.h"
  3. #include "init_deinit.h"
  4. #include <video_player_icons.h>
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <cli/cli.h>
  8. #include <gui/gui.h>
  9. void draw_callback(Canvas* canvas, void* ctx) {
  10. PlayerViewModel* model = (PlayerViewModel*)ctx;
  11. VideoPlayerApp* player = (VideoPlayerApp*)(model->player);
  12. canvas_draw_xbm(canvas, 0, 0, player->width, player->height, player->image_buffer);
  13. }
  14. bool input_callback(InputEvent* input_event, void* ctx) {
  15. // Проверяем, что контекст не нулевой
  16. furi_assert(ctx);
  17. PlayerView* player_view = (PlayerView*)ctx;
  18. VideoPlayerApp* player = (VideoPlayerApp*)(player_view->context);
  19. bool consumed = false;
  20. VideoPlayerEvent event = {.type = EventTypeInput, .input = *input_event};
  21. furi_message_queue_put(player->event_queue, &event, FuriWaitForever);
  22. consumed = true;
  23. return consumed;
  24. }
  25. void direct_input_callback(const void* value, void* ctx) {
  26. // Проверяем, что контекст не нулевой
  27. furi_assert(ctx);
  28. const InputEvent* input_event = value;
  29. VideoPlayerApp* player = (VideoPlayerApp*)(ctx);
  30. VideoPlayerEvent event = {.type = EventTypeInput, .input = *input_event};
  31. furi_message_queue_put(player->event_queue, &event, FuriWaitForever);
  32. }
  33. void player_view_free(PlayerView* player_view) {
  34. furi_assert(player_view);
  35. view_free(player_view->view);
  36. free(player_view);
  37. }
  38. bool open_file_stream(Stream* stream) {
  39. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  40. bool result = false;
  41. FuriString* path;
  42. path = furi_string_alloc();
  43. furi_string_set(path, VIDEO_PLAYER_FOLDER);
  44. DialogsFileBrowserOptions browser_options;
  45. dialog_file_browser_set_basic_options(&browser_options, ".bnd", &I_vid_logo);
  46. browser_options.base_path = VIDEO_PLAYER_FOLDER;
  47. browser_options.hide_ext = false;
  48. bool ret = dialog_file_browser_show(dialogs, path, path, &browser_options);
  49. furi_record_close(RECORD_DIALOGS);
  50. if(ret) {
  51. if(!file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  52. FURI_LOG_E("FUCK! ", "Cannot open file \"%s\"", furi_string_get_cstr(path));
  53. } else {
  54. result = true;
  55. }
  56. }
  57. furi_string_free(path);
  58. return result;
  59. }
  60. int32_t video_player_app(void* p) {
  61. UNUSED(p);
  62. Storage* storage = furi_record_open(RECORD_STORAGE);
  63. bool st = storage_simply_mkdir(storage, APPSDATA_FOLDER);
  64. st = storage_simply_mkdir(storage, VIDEO_PLAYER_FOLDER);
  65. UNUSED(st);
  66. furi_record_close(RECORD_STORAGE);
  67. VideoPlayerApp* player = init_player();
  68. if(open_file_stream(player->stream)) {
  69. }
  70. else {
  71. player->quit = true;
  72. //goto end;
  73. }
  74. if(!(player->quit)) {
  75. char header[8];
  76. header[7] = '\0';
  77. stream_read(player->stream, (uint8_t*)header, 7);
  78. if(strcmp(header, "BND!VID") != 0) {
  79. player->quit = true;
  80. //goto end;
  81. }
  82. stream_read(player->stream, (uint8_t*)&player->version, sizeof(player->version));
  83. stream_read(player->stream, (uint8_t*)&player->num_frames, sizeof(player->num_frames));
  84. stream_read(
  85. player->stream, (uint8_t*)&player->audio_chunk_size, sizeof(player->audio_chunk_size));
  86. stream_read(player->stream, (uint8_t*)&player->sample_rate, sizeof(player->sample_rate));
  87. stream_read(player->stream, &player->height, sizeof(player->height));
  88. stream_read(player->stream, &player->width, sizeof(player->width));
  89. player->buffer = (uint8_t*)malloc(
  90. player->audio_chunk_size * 2 + (uint32_t)player->height * (uint32_t)player->width / 8);
  91. memset(
  92. player->buffer,
  93. 0,
  94. player->audio_chunk_size * 2 + (uint32_t)player->height * (uint32_t)player->width / 8);
  95. player->image_buffer_length = (uint32_t)player->height * (uint32_t)player->width / 8;
  96. player->audio_buffer = (uint8_t*)&player->buffer[player->image_buffer_length];
  97. player->image_buffer = player->buffer;
  98. }
  99. if(furi_hal_speaker_acquire(1000)) {
  100. if(!(player->quit)) {
  101. player_init_hardware_and_play(player);
  102. }
  103. // Текущее событие типа кастомного типа VideoPlayerEvent
  104. VideoPlayerEvent event;
  105. //view_dispatcher_switch_to_view(player->view_dispatcher, VIEW_PLAYER);
  106. //switch from view dispatcher to direct draw
  107. view_dispatcher_remove_view(player->view_dispatcher, VIEW_PLAYER);
  108. view_dispatcher_free(player->view_dispatcher);
  109. player_view_free(player->player_view);
  110. furi_record_close(RECORD_GUI);
  111. player->input = furi_record_open(RECORD_INPUT_EVENTS);
  112. player->gui = furi_record_open(RECORD_GUI);
  113. player->canvas = gui_direct_draw_acquire(player->gui);
  114. player->input_subscription =
  115. furi_pubsub_subscribe(player->input, direct_input_callback, player);
  116. if(player->quit) {
  117. deinit_player(player);
  118. player_deinit_hardware();
  119. return 0;
  120. }
  121. player->playing = true;
  122. vTaskPrioritySet(furi_thread_get_current_id(), FuriThreadPriorityIdle);
  123. while(!(player->quit)) {
  124. furi_check(
  125. furi_message_queue_get(player->event_queue, &event, FuriWaitForever) ==
  126. FuriStatusOk);
  127. if(event.type == EventTypeInput) {
  128. if(event.input.key == InputKeyBack) {
  129. player->quit = true;
  130. }
  131. if(event.input.key == InputKeyOk) {
  132. player->playing = !player->playing;
  133. }
  134. if(player->playing) {
  135. player_start();
  136. }
  137. else {
  138. player_stop();
  139. }
  140. }
  141. if(event.type == EventType1stHalf) {
  142. //reading image+sound data in one pass since in this case image buffer and first part of audio buffer are continuous chunk of memory; should probably improve FPS
  143. stream_read(
  144. player->stream,
  145. player->image_buffer,
  146. player->image_buffer_length + player->audio_chunk_size);
  147. player->frames_played++;
  148. canvas_reset(player->canvas);
  149. canvas_draw_xbm(
  150. player->canvas, 0, 0, player->width, player->height, player->image_buffer);
  151. canvas_commit(player->canvas);
  152. }
  153. if(event.type == EventType2ndHalf) {
  154. uint8_t* audio_buffer = &player->audio_buffer[player->audio_chunk_size];
  155. stream_read(player->stream, player->image_buffer, player->image_buffer_length);
  156. stream_read(player->stream, audio_buffer, player->audio_chunk_size);
  157. player->frames_played++;
  158. canvas_reset(player->canvas);
  159. canvas_draw_xbm(
  160. player->canvas, 0, 0, player->width, player->height, player->image_buffer);
  161. canvas_commit(player->canvas);
  162. }
  163. if(player->frames_played == player->num_frames) {
  164. player->quit = true;
  165. }
  166. furi_thread_yield();
  167. }
  168. }
  169. deinit_player(player);
  170. player_deinit_hardware();
  171. return 0;
  172. }