playback.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #include "playback.h"
  2. #include <pthread.h>
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5. /* ============================================================
  6. * 内部数据结构
  7. * ============================================================ */
  8. #define MAX_ACTIVE_PLAYBACKS 32
  9. static PlaybackSession* g_active_playbacks[MAX_ACTIVE_PLAYBACKS];
  10. static int g_playback_count = 0;
  11. static int g_playback_id_counter = 0;
  12. static pthread_mutex_t g_playback_mutex = PTHREAD_MUTEX_INITIALIZER;
  13. /* ============================================================
  14. * 内部函数
  15. * ============================================================ */
  16. static PlaybackSession* find_playback(int playback_id) {
  17. for (int i = 0; i < g_playback_count; i++) {
  18. if (g_active_playbacks[i] != NULL && g_active_playbacks[i]->playback_id == playback_id) {
  19. return g_active_playbacks[i];
  20. }
  21. }
  22. return NULL;
  23. }
  24. static int add_playback(PlaybackSession* session) {
  25. pthread_mutex_lock(&g_playback_mutex);
  26. if (g_playback_count >= MAX_ACTIVE_PLAYBACKS) {
  27. pthread_mutex_unlock(&g_playback_mutex);
  28. LOG_ERROR("Maximum playback sessions reached");
  29. return FAILURE;
  30. }
  31. session->playback_id = ++g_playback_id_counter;
  32. session->state = PLAYBACK_STATE_IDLE;
  33. session->is_running = 0;
  34. session->frame_count = 0;
  35. pthread_mutex_init(&session->lock, NULL);
  36. g_active_playbacks[g_playback_count++] = session;
  37. pthread_mutex_unlock(&g_playback_mutex);
  38. LOG_INFO("Playback session added: ID=%d, Device=%s, Channel=%d",
  39. session->playback_id, session->device_id, session->channel);
  40. return SUCCESS;
  41. }
  42. static void remove_playback(int playback_id) {
  43. pthread_mutex_lock(&g_playback_mutex);
  44. for (int i = 0; i < g_playback_count; i++) {
  45. if (g_active_playbacks[i] != NULL && g_active_playbacks[i]->playback_id == playback_id) {
  46. pthread_mutex_destroy(&g_active_playbacks[i]->lock);
  47. g_active_playbacks[i] = NULL;
  48. g_playback_count--;
  49. /* 压缩数组 */
  50. int write_idx = 0;
  51. for (int read_idx = 0; read_idx < g_playback_count; read_idx++) {
  52. if (g_active_playbacks[read_idx] != NULL) {
  53. g_active_playbacks[write_idx++] = g_active_playbacks[read_idx];
  54. }
  55. }
  56. for (int j = write_idx; j < g_playback_count; j++) {
  57. g_active_playbacks[j] = NULL;
  58. }
  59. LOG_INFO("Playback session removed: ID=%d", playback_id);
  60. break;
  61. }
  62. }
  63. pthread_mutex_unlock(&g_playback_mutex);
  64. }
  65. /* ============================================================
  66. * 公共接口实现
  67. * ============================================================ */
  68. int playback_init(void) {
  69. LOG_INFO("Initializing playback module...");
  70. g_playback_count = 0;
  71. memset(g_active_playbacks, 0, sizeof(g_active_playbacks));
  72. /* 确保录像目录存在 */
  73. /* 目录由配置决定 */
  74. LOG_INFO("Playback module initialized");
  75. return SUCCESS;
  76. }
  77. void playback_cleanup(void) {
  78. LOG_INFO("Cleaning up playback module...");
  79. playback_stop_all();
  80. for (int i = 0; i < MAX_ACTIVE_PLAYBACKS; i++) {
  81. if (g_active_playbacks[i] != NULL) {
  82. pthread_mutex_destroy(&g_active_playbacks[i]->lock);
  83. if (g_active_playbacks[i]->stream) {
  84. stream_destroy_session(g_active_playbacks[i]->stream);
  85. }
  86. free(g_active_playbacks[i]);
  87. g_active_playbacks[i] = NULL;
  88. }
  89. }
  90. g_playback_count = 0;
  91. LOG_INFO("Playback module cleaned up");
  92. }
  93. PlaybackSession* playback_start(const char* device_id, int channel,
  94. time_t start_time, time_t end_time,
  95. int speed,
  96. PlaybackDataCB data_cb,
  97. PlaybackStatusCB status_cb,
  98. void* user_data) {
  99. if (device_id == NULL || channel < 0) {
  100. LOG_ERROR("Invalid parameters");
  101. return NULL;
  102. }
  103. LOG_INFO("Starting playback: Device=%s, Channel=%d, Start=%ld, End=%ld, Speed=%d",
  104. device_id, channel, (long)start_time, (long)end_time, speed);
  105. PlaybackSession* session = (PlaybackSession*)calloc(1, sizeof(PlaybackSession));
  106. if (session == NULL) {
  107. LOG_ERROR("Failed to allocate playback session");
  108. return NULL;
  109. }
  110. safe_strcpy(session->device_id, device_id, MAX_DEVICE_ID);
  111. session->channel = channel;
  112. session->start_time = start_time;
  113. session->end_time = end_time;
  114. session->speed = speed;
  115. session->state = PLAYBACK_STATE_STARTING;
  116. /* 创建关联的流会话用于接收回放数据 */
  117. session->stream = stream_create_session(device_id, channel, STREAM_TYPE_SUB,
  118. CODEC_TYPE_H264, 1920, 1080, 25, 2048000);
  119. if (session->stream == NULL) {
  120. LOG_ERROR("Failed to create stream session for playback");
  121. free(session);
  122. return NULL;
  123. }
  124. /* 设置回放数据回调 */
  125. if (data_cb != NULL) {
  126. /* 在实际实现中,这里会注册到流媒体库的回调 */
  127. session->stream->internal_handle = (void*)data_cb;
  128. }
  129. if (add_playback(session) != SUCCESS) {
  130. stream_destroy_session(session->stream);
  131. free(session);
  132. return NULL;
  133. }
  134. /* 调用HCNetSDK启动回放
  135. * 完整流程:
  136. * 1. NET_ESTREAM_StartListenPlayBack - 开启SMS回放监听
  137. * 2. NET_ESTREAM_SetExceptionCallBack - 注册异常回调
  138. * 3. NET_ECMS_StartPlayBack - 向设备发送回放开始请求
  139. * 4. NET_ESTREAM_SetPlayBackDataCB - 注册回放数据回调
  140. * 5. NET_ECMS_StartPushPlayBack - 发送码流传输请求(可选)
  141. */
  142. /* 步骤1: 开启SMS回放监听 */
  143. stream_start_playback_listen("0.0.0.0", 7660, NULL, NULL);
  144. /* 步骤2: 向设备发送回放开始请求 */
  145. /* NET_ECMS_StartPlayBack(user_id, dev_ip, dev_port, sms_ip, sms_port, channel) */
  146. session->state = PLAYBACK_STATE_PLAYING;
  147. session->is_running = 1;
  148. /* 触发状态回调 */
  149. if (status_cb) {
  150. status_cb(session, PLAYBACK_STATE_PLAYING, user_data);
  151. }
  152. LOG_INFO("Playback started: ID=%d, Device=%s, Channel=%d",
  153. session->playback_id, device_id, channel);
  154. return session;
  155. }
  156. int playback_stop(PlaybackSession* session) {
  157. if (session == NULL) return FAILURE;
  158. LOG_INFO("Stopping playback: ID=%d, Device=%s, Channel=%d",
  159. session->playback_id, session->device_id, session->channel);
  160. session->is_running = 0;
  161. session->state = PLAYBACK_STATE_STOPPING;
  162. /* 步骤7: 停止SMS码流转发 */
  163. /* NET_ESTREAM_StopPlayBack() */
  164. /* 步骤8: 向设备发送回放停止请求 */
  165. /* NET_ECMS_StopPlayBack(user_id) */
  166. /* 停止关联的流 */
  167. if (session->stream) {
  168. stream_stop_push(session->stream);
  169. stream_destroy_session(session->stream);
  170. session->stream = NULL;
  171. }
  172. /* 步骤9: 停止SMS回放监听(如果没有其他回放在使用) */
  173. /* NET_ESTREAM_StopListenPlayBack() */
  174. remove_playback(session->playback_id);
  175. free(session);
  176. LOG_INFO("Playback stopped: ID=%d", session->playback_id);
  177. return SUCCESS;
  178. }
  179. int playback_pause(PlaybackSession* session) {
  180. if (session == NULL) return FAILURE;
  181. if (session->state != PLAYBACK_STATE_PLAYING) {
  182. return FAILURE;
  183. }
  184. LOG_INFO("Pausing playback: ID=%d", session->playback_id);
  185. session->state = PLAYBACK_STATE_PAUSED;
  186. /* 在实际实现中,这里会暂停码流转发 */
  187. return SUCCESS;
  188. }
  189. int playback_resume(PlaybackSession* session) {
  190. if (session == NULL) return FAILURE;
  191. if (session->state != PLAYBACK_STATE_PAUSED) {
  192. return FAILURE;
  193. }
  194. LOG_INFO("Resuming playback: ID=%d", session->playback_id);
  195. session->state = PLAYBACK_STATE_PLAYING;
  196. /* 在实际实现中,这里会恢复码流转发 */
  197. return SUCCESS;
  198. }
  199. int playback_get_file_list(const char* device_id, int channel,
  200. time_t start_time, time_t end_time,
  201. char files[][MAX_PATH_SIZE], int max_files) {
  202. if (device_id == NULL || files == NULL) {
  203. return FAILURE;
  204. }
  205. LOG_INFO("Getting file list: Device=%s, Channel=%d, Start=%ld, End=%ld",
  206. device_id, channel, (long)start_time, (long)end_time);
  207. /* 在实际实现中,这里会调用HCNetSDK查询录像文件
  208. * 例如: NET_DVR_FindFirstFile / NET_DVR_FindNextFile / NET_DVR_FindClose
  209. */
  210. /* 模拟实现: 返回空列表 */
  211. (void)start_time;
  212. (void)end_time;
  213. (void)max_files;
  214. return 0;
  215. }
  216. int playback_download_file(const char* device_id, int channel,
  217. const char* file_name, const char* save_path) {
  218. if (device_id == NULL || file_name == NULL || save_path == NULL) {
  219. return FAILURE;
  220. }
  221. LOG_INFO("Downloading file: Device=%s, Channel=%d, File=%s, Path=%s",
  222. device_id, channel, file_name, save_path);
  223. /* 在实际实现中,这里会调用HCNetSDK下载录像文件
  224. * 例如: NET_DVR_PlayBackByTime / NET_DVR_SaveRealData
  225. */
  226. return SUCCESS;
  227. }
  228. int playback_get_active_sessions(PlaybackSession* sessions[], int max_sessions) {
  229. int count = 0;
  230. pthread_mutex_lock(&g_playback_mutex);
  231. for (int i = 0; i < g_playback_count && count < max_sessions; i++) {
  232. if (g_active_playbacks[i] != NULL) {
  233. sessions[count++] = g_active_playbacks[i];
  234. }
  235. }
  236. pthread_mutex_unlock(&g_playback_mutex);
  237. return count;
  238. }
  239. void playback_stop_all(void) {
  240. pthread_mutex_lock(&g_playback_mutex);
  241. for (int i = 0; i < g_playback_count; i++) {
  242. if (g_active_playbacks[i] != NULL) {
  243. g_active_playbacks[i]->is_running = 0;
  244. if (g_active_playbacks[i]->stream) {
  245. stream_stop_push(g_active_playbacks[i]->stream);
  246. }
  247. }
  248. }
  249. pthread_mutex_unlock(&g_playback_mutex);
  250. }
  251. PlaybackState playback_get_state(PlaybackSession* session) {
  252. if (session == NULL) return PLAYBACK_STATE_ERROR;
  253. return session->state;
  254. }