Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1 | #include <stddef.h> |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2 | #include <unistd.h> |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <pthread.h> |
| 5 | #include <string.h> |
| 6 | #include <time.h> |
| 7 | #include <errno.h> |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 8 | #include <sys/time.h> |
| 9 | #include <time.h> |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 10 | |
| 11 | #include "dvr_types.h" |
| 12 | #include "dvr_record.h" |
| 13 | #include "dvr_crypto.h" |
| 14 | #include "dvr_playback.h" |
| 15 | #include "dvr_segment.h" |
| 16 | |
| 17 | #include "AmTsPlayer.h" |
| 18 | |
| 19 | #include "list.h" |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 20 | |
| 21 | #include "dvr_wrapper.h" |
| 22 | |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 23 | #define DVR_WRAPPER_DEBUG(_level, _fmt...) \ |
| 24 | DVR_DEBUG_FL(_level, "wrapper", _fmt) |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 25 | |
| 26 | /*duration of data to resume if paused with EVT_REACHED_END in timeshifting*/ |
hualing chen | 2932d37 | 2020-04-29 13:44:00 +0800 | [diff] [blame] | 27 | #define TIMESHIFT_DATA_DURATION_TO_RESUME (600) |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 28 | /*a tolerant gap*/ |
| 29 | #define DVR_PLAYBACK_END_GAP (1000) |
| 30 | |
| 31 | enum { |
| 32 | W_REC = 1, |
| 33 | W_PLAYBACK = 2, |
| 34 | }; |
| 35 | |
| 36 | enum { |
| 37 | U_PIDS = 0x01, |
| 38 | U_STAT = 0x02, |
| 39 | U_ALL = U_PIDS | U_STAT, |
| 40 | }; |
| 41 | |
| 42 | typedef struct { |
| 43 | /*make lock the 1st item in the structure*/ |
| 44 | pthread_mutex_t lock; |
| 45 | |
| 46 | /*rec or play*/ |
| 47 | int type; |
| 48 | |
| 49 | /*valid if (sn != 0)*/ |
| 50 | unsigned long sn; |
| 51 | unsigned long sn_linked; |
| 52 | |
| 53 | struct list_head segments; /**<head-add list*/ |
| 54 | uint64_t current_segment_id; /**<id of the current segment*/ |
| 55 | |
| 56 | union { |
| 57 | struct { |
| 58 | DVR_WrapperRecordOpenParams_t param_open; |
| 59 | DVR_RecordStartParams_t param_start; |
| 60 | DVR_RecordStartParams_t param_update; |
| 61 | DVR_RecordHandle_t recorder; |
| 62 | DVR_RecordEventFunction_t event_fn; |
| 63 | void *event_userdata; |
| 64 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 65 | /*total status = seg_status + status + obsolete*/ |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 66 | DVR_RecordStatus_t seg_status; /**<status of current segment*/ |
| 67 | DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/ |
| 68 | uint64_t next_segment_id; |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 69 | |
| 70 | DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/ |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 71 | } record; |
| 72 | |
| 73 | struct { |
| 74 | DVR_WrapperPlaybackOpenParams_t param_open; |
| 75 | DVR_PlaybackHandle_t player; |
| 76 | DVR_PlaybackEventFunction_t event_fn; |
| 77 | void *event_userdata; |
| 78 | |
| 79 | /*total status = seg_status + status*/ |
| 80 | DVR_PlaybackStatus_t seg_status; |
| 81 | DVR_WrapperPlaybackStatus_t status; |
| 82 | DVR_PlaybackPids_t pids_req; |
| 83 | DVR_PlaybackEvent_t last_event; |
Zhiqiang Han | 3eb75f9 | 2020-04-08 10:07:55 +0800 | [diff] [blame] | 84 | float speed; |
hualing chen | b5cd42e | 2020-04-15 17:03:34 +0800 | [diff] [blame] | 85 | DVR_Bool_t reach_end; |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 86 | |
| 87 | DVR_WrapperInfo_t obsolete; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 88 | } playback; |
| 89 | }; |
| 90 | } DVR_WrapperCtx_t; |
| 91 | |
| 92 | typedef struct { |
| 93 | struct list_head head; |
| 94 | unsigned long sn; |
| 95 | |
| 96 | /* rec or playback */ |
| 97 | int type; |
| 98 | |
| 99 | union { |
| 100 | struct { |
| 101 | DVR_RecordEvent_t event; |
| 102 | DVR_RecordStatus_t status; |
| 103 | } record; |
| 104 | struct { |
| 105 | DVR_PlaybackEvent_t event; |
| 106 | DVR_Play_Notify_t status; |
| 107 | } playback; |
| 108 | }; |
| 109 | } DVR_WrapperEventCtx_t; |
| 110 | |
| 111 | typedef struct { |
| 112 | pthread_mutex_t lock; |
| 113 | char *name; |
| 114 | int running; |
| 115 | pthread_cond_t cond; |
| 116 | pthread_t thread; |
| 117 | int type; |
| 118 | } DVR_WrapperThreadCtx_t; |
| 119 | |
| 120 | typedef struct { |
| 121 | struct list_head head; |
| 122 | |
| 123 | DVR_RecordSegmentInfo_t seg_info; |
| 124 | DVR_PlaybackSegmentInfo_t playback_info; |
| 125 | } DVR_WrapperPlaybackSegmentInfo_t; |
| 126 | |
| 127 | typedef struct { |
| 128 | struct list_head head; |
| 129 | |
| 130 | DVR_RecordSegmentInfo_t info; |
| 131 | } DVR_WrapperRecordSegmentInfo_t; |
| 132 | |
| 133 | /* serial num generater */ |
| 134 | static unsigned long sn = 1; |
| 135 | static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER; |
| 136 | |
| 137 | static inline unsigned long get_sn() |
| 138 | { |
hualing chen | ab0d126 | 2021-09-26 15:22:50 +0800 | [diff] [blame] | 139 | unsigned long no = 0; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 140 | |
| 141 | pthread_mutex_lock(&sn_lock); |
| 142 | no = sn++; |
| 143 | if (!no) |
| 144 | no = sn++; |
| 145 | pthread_mutex_unlock(&sn_lock); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 146 | return no; |
| 147 | } |
| 148 | |
| 149 | /* entity ctx */ |
| 150 | #define DVR_WRAPPER_MAX 10 |
| 151 | |
| 152 | static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] = |
| 153 | { |
| 154 | [0 ... (DVR_WRAPPER_MAX - 1)] = |
| 155 | { |
| 156 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 157 | .type = W_REC, |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] = |
| 162 | { |
| 163 | [0 ... (DVR_WRAPPER_MAX - 1)] = |
| 164 | { |
| 165 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 166 | .type = W_PLAYBACK, |
| 167 | } |
| 168 | }; |
| 169 | |
| 170 | /* events lists */ |
| 171 | static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list); |
| 172 | static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list); |
| 173 | |
| 174 | static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER; |
| 175 | static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER; |
| 176 | |
| 177 | static DVR_WrapperThreadCtx_t wrapper_thread[2] = |
| 178 | { |
| 179 | [0] = |
| 180 | { |
| 181 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 182 | .running = 0, |
| 183 | .name = "record", |
| 184 | .type = W_REC, |
| 185 | }, |
| 186 | [1] = |
| 187 | { |
| 188 | .lock = PTHREAD_MUTEX_INITIALIZER, |
| 189 | .running = 0, |
| 190 | .name = "playback", |
| 191 | .type = W_PLAYBACK, |
| 192 | }, |
| 193 | }; |
| 194 | |
| 195 | /*now only support one timeshift now*/ |
| 196 | static unsigned long sn_timeshift_record; |
| 197 | static unsigned long sn_timeshift_playback; |
| 198 | |
| 199 | static void *wrapper_task(void *arg); |
| 200 | static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx); |
| 201 | |
| 202 | static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata); |
| 203 | static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata); |
| 204 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 205 | static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status); |
| 206 | static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 207 | |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 208 | static int get_timespec_timeout(int timeout, struct timespec *ts) |
| 209 | { |
| 210 | struct timespec ots; |
| 211 | int left, diff; |
| 212 | |
| 213 | clock_gettime(CLOCK_MONOTONIC, &ots); |
| 214 | |
| 215 | ts->tv_sec = ots.tv_sec + timeout / 1000; |
| 216 | ts->tv_nsec = ots.tv_nsec; |
| 217 | |
| 218 | left = timeout % 1000; |
| 219 | left *= 1000000; |
| 220 | diff = 1000000000 - ots.tv_nsec; |
| 221 | |
| 222 | if (diff <= left) { |
| 223 | ts->tv_sec++; |
| 224 | ts->tv_nsec = left-diff; |
| 225 | } else { |
| 226 | ts->tv_nsec += left; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 231 | |
| 232 | static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock) |
| 233 | { |
| 234 | DVR_WrapperEventCtx_t *pevt; |
| 235 | |
| 236 | pthread_mutex_lock(list_lock); |
| 237 | if (list_empty(list)) |
| 238 | pevt = NULL; |
| 239 | else { |
| 240 | pevt = list_first_entry(list, DVR_WrapperEventCtx_t, head); |
| 241 | list_del(&pevt->head); |
| 242 | } |
| 243 | pthread_mutex_unlock(list_lock); |
| 244 | |
| 245 | return pevt; |
| 246 | } |
| 247 | |
| 248 | static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent() |
| 249 | { |
| 250 | return ctx_getEvent(&record_evt_list, &record_evt_list_lock); |
| 251 | } |
| 252 | |
| 253 | static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent() |
| 254 | { |
| 255 | return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock); |
| 256 | } |
| 257 | |
| 258 | static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt) |
| 259 | { |
| 260 | DVR_WrapperEventCtx_t *padd; |
| 261 | padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t)); |
| 262 | DVR_RETURN_IF_FALSE(padd); |
| 263 | |
| 264 | *padd = *evt; |
| 265 | pthread_mutex_lock(lock); |
| 266 | list_add_tail(&padd->head, list); |
| 267 | pthread_mutex_unlock(lock); |
| 268 | return DVR_SUCCESS; |
| 269 | } |
| 270 | |
| 271 | static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt) |
| 272 | { |
| 273 | free(evt); |
| 274 | } |
| 275 | |
| 276 | /*useless*/ |
| 277 | static void ctx_cleanOutdatedEvents(struct list_head *evt_list, |
| 278 | pthread_mutex_t *evt_list_lock, |
| 279 | DVR_WrapperCtx_t *list) |
| 280 | { |
| 281 | DVR_WrapperEventCtx_t *pevt, *pevt_tmp; |
| 282 | unsigned long sns[DVR_WRAPPER_MAX]; |
| 283 | int cnt = 0; |
| 284 | int i; |
| 285 | int found = 0; |
| 286 | |
| 287 | /*copy all valid sns*/ |
| 288 | for (i = 0; i < DVR_WRAPPER_MAX; i++) { |
| 289 | sns[cnt] = list[i].sn; |
| 290 | if (!sns[cnt]) |
| 291 | cnt++; |
| 292 | } |
| 293 | |
| 294 | /*free evts that not belong to any valid sns*/ |
| 295 | pthread_mutex_lock(evt_list_lock); |
| 296 | list_for_each_entry_safe(pevt, pevt_tmp, evt_list, head) { |
| 297 | for (i = 0; i < cnt; i++) { |
| 298 | if (pevt->sn == sns[i]) { |
| 299 | found = 1; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (!found) { |
| 304 | list_del(&pevt->head); |
| 305 | ctx_freeEvent(pevt); |
| 306 | } |
| 307 | } |
| 308 | pthread_mutex_unlock(evt_list_lock); |
| 309 | } |
| 310 | |
| 311 | static inline void ctx_cleanOutdatedRecordEvents() |
| 312 | { |
| 313 | ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list); |
| 314 | } |
| 315 | |
| 316 | static inline void ctx_cleanOutdatedPlaybackEvents() |
| 317 | { |
| 318 | ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list); |
| 319 | } |
| 320 | |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 321 | //check this play is recording file |
| 322 | //return 0 if not the recording |
| 323 | //else return record id |
| 324 | static inline int ctx_isPlay_recording(char *play_location) |
| 325 | { |
| 326 | int i; |
| 327 | DVR_WrapperCtx_t *cnt; |
| 328 | |
| 329 | for (i = 0; i < DVR_WRAPPER_MAX; i++) { |
| 330 | cnt = &record_list[i]; |
| 331 | //DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]R:[%s]P:[%s] ...\n", i, cnt->sn, cnt->record.param_open.location, play_location); |
| 332 | if (!strcmp(cnt->record.param_open.location, play_location)) { |
| 333 | DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]R:[%s]P:[%s] .found..\n", i, cnt->sn, cnt->record.param_open.location, play_location); |
| 334 | return cnt->sn; |
| 335 | } |
| 336 | } |
| 337 | DVR_WRAPPER_DEBUG(1, " not found play is recing [%d]", DVR_WRAPPER_MAX); |
| 338 | return 0; |
| 339 | } |
| 340 | //check this record is playing file |
| 341 | //return 0 if not the playing |
| 342 | //else return playback id |
| 343 | static inline int ctx_isRecord_playing(char *rec_location) |
| 344 | { |
| 345 | int i; |
| 346 | DVR_WrapperCtx_t *cnt; |
| 347 | for (i = 0; i < DVR_WRAPPER_MAX; i++) { |
| 348 | cnt = &playback_list[i]; |
| 349 | //DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]P[%s]R[%s] ...\n", i, cnt->sn, cnt->playback.param_open.location, rec_location); |
| 350 | if (!strcmp(cnt->playback.param_open.location, rec_location)) { |
| 351 | DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]P[%s]R[%s] ..found.\n",i, cnt->sn, cnt->playback.param_open.location, rec_location); |
| 352 | return cnt->sn; |
| 353 | } |
| 354 | } |
| 355 | DVR_WRAPPER_DEBUG(1, " not found rec is playing [%d]", DVR_WRAPPER_MAX); |
| 356 | return 0; |
| 357 | } |
| 358 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 359 | static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list) |
| 360 | { |
| 361 | int i; |
| 362 | for (i = 0; i < DVR_WRAPPER_MAX; i++) { |
| 363 | if (list[i].sn == sn) |
| 364 | return &list[i]; |
| 365 | } |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | static inline void ctx_reset(DVR_WrapperCtx_t *ctx) |
| 370 | { |
| 371 | memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn), |
| 372 | 0, |
| 373 | sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn)); |
| 374 | } |
| 375 | |
| 376 | static inline int ctx_valid(DVR_WrapperCtx_t *ctx) |
| 377 | { |
| 378 | return (ctx->sn != 0); |
| 379 | } |
| 380 | |
| 381 | static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn) |
| 382 | { |
| 383 | return ctx_get(sn, record_list); |
| 384 | } |
| 385 | |
| 386 | static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn) |
| 387 | { |
| 388 | return ctx_get(sn, playback_list); |
| 389 | } |
| 390 | |
| 391 | static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *)) |
| 392 | { |
| 393 | pthread_mutex_lock(&ctx->lock); |
| 394 | if (ctx->running == 0) { |
| 395 | pthread_condattr_t attr; |
| 396 | pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); |
| 397 | pthread_cond_init(&ctx->cond, &attr); |
| 398 | pthread_condattr_destroy(&attr); |
| 399 | DVR_WRAPPER_DEBUG(1, "start wrapper thread(%s) ...\n", ctx->name); |
| 400 | pthread_create(&ctx->thread, NULL, thread_fn, ctx); |
| 401 | DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) started\n", ctx->name); |
| 402 | } |
| 403 | ctx->running++; |
| 404 | pthread_mutex_unlock(&ctx->lock); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx) |
| 409 | { |
| 410 | pthread_mutex_lock(&ctx->lock); |
| 411 | ctx->running--; |
| 412 | if (!ctx->running) { |
| 413 | pthread_cond_broadcast(&ctx->cond); |
| 414 | pthread_mutex_unlock(&ctx->lock); |
| 415 | |
| 416 | DVR_WRAPPER_DEBUG(1, "stop wrapper thread(%s) ...\n", ctx->name); |
| 417 | pthread_join(ctx->thread, NULL); |
| 418 | DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) stopped\n", ctx->name); |
| 419 | |
| 420 | pthread_mutex_lock(&ctx->lock); |
| 421 | if (!ctx->running) /*protect*/ |
| 422 | pthread_cond_destroy(&ctx->cond); |
| 423 | } |
| 424 | pthread_mutex_unlock(&ctx->lock); |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | #define WRAPPER_THREAD_RECORD (&wrapper_thread[0]) |
| 429 | #define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1]) |
| 430 | |
| 431 | static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx) |
| 432 | { |
| 433 | DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)? |
| 434 | WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK; |
| 435 | return wrapper_requestThread(thread_ctx, wrapper_task); |
| 436 | } |
| 437 | |
| 438 | static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx) |
| 439 | { |
| 440 | DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)? |
| 441 | WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK; |
| 442 | return wrapper_releaseThread(thread_ctx); |
| 443 | } |
| 444 | |
| 445 | static inline int wrapper_releaseThreadForType(int type) |
| 446 | { |
| 447 | DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)? |
| 448 | WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK; |
| 449 | return wrapper_releaseThread(thread_ctx); |
| 450 | } |
| 451 | |
| 452 | static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx) |
| 453 | { |
| 454 | pthread_cond_signal(&thread_ctx->cond); |
| 455 | } |
| 456 | |
| 457 | static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx) |
| 458 | { |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 459 | struct timespec rt; |
| 460 | get_timespec_timeout(200, &rt); |
| 461 | pthread_cond_timedwait(&thread_ctx->cond, &thread_ctx->lock, &rt); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static inline void wrapper_threadSignalForType(int type) |
| 466 | { |
| 467 | DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ? |
| 468 | WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK; |
| 469 | wrapper_threadSignal(thread_ctx); |
| 470 | } |
| 471 | |
| 472 | static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx) |
| 473 | { |
| 474 | DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ? |
| 475 | WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK; |
| 476 | wrapper_threadSignal(thread_ctx); |
| 477 | } |
| 478 | |
| 479 | static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx) |
| 480 | { |
| 481 | DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ? |
| 482 | WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK; |
| 483 | wrapper_threadWait(thread_ctx); |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static void get_timeout_real(int timeout, struct timespec *ts) |
| 488 | { |
| 489 | struct timespec ots; |
| 490 | int left, diff; |
| 491 | |
| 492 | clock_gettime(CLOCK_REALTIME, &ots); |
| 493 | |
| 494 | ts->tv_sec = ots.tv_sec + timeout/1000; |
| 495 | ts->tv_nsec = ots.tv_nsec; |
| 496 | |
| 497 | left = timeout % 1000; |
| 498 | left *= 1000000; |
| 499 | diff = 1000000000-ots.tv_nsec; |
| 500 | |
| 501 | if (diff <= left) { |
| 502 | ts->tv_sec++; |
| 503 | ts->tv_nsec = left-diff; |
| 504 | } else { |
| 505 | ts->tv_nsec += left; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /*return condition, locked if condition == true*/ |
| 510 | static int wrapper_mutex_lock_if(pthread_mutex_t *lock, int *condition) |
| 511 | { |
| 512 | int r2; |
| 513 | do { |
| 514 | struct timespec rt2; |
| 515 | /*android use real time for mutex timedlock*/ |
| 516 | get_timeout_real(10, &rt2); |
| 517 | r2 = pthread_mutex_timedlock(lock, &rt2); |
| 518 | } while (*condition && (r2 == ETIMEDOUT)); |
| 519 | |
| 520 | if (!(*condition) && (r2 == 0)) |
| 521 | pthread_mutex_unlock(lock); |
| 522 | |
| 523 | return *condition; |
| 524 | } |
| 525 | |
| 526 | static void *wrapper_task(void *arg) |
| 527 | { |
| 528 | DVR_WrapperThreadCtx_t *tctx = (DVR_WrapperThreadCtx_t *)arg; |
| 529 | DVR_WrapperEventCtx_t *evt; |
| 530 | |
| 531 | pthread_mutex_lock(&tctx->lock); |
| 532 | |
| 533 | while (tctx->running) { |
| 534 | { |
| 535 | int ret; |
hualing chen | e3797f0 | 2021-01-13 14:53:28 +0800 | [diff] [blame] | 536 | |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 537 | evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent(); |
| 538 | if (!evt) |
| 539 | ret = wrapper_threadWait(tctx); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 540 | } |
| 541 | |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 542 | while (evt) { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 543 | DVR_WrapperCtx_t *ctx = (evt->type == W_REC)? |
| 544 | ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn); |
hualing chen | bc0aec9 | 2021-03-18 14:52:40 +0800 | [diff] [blame] | 545 | if (ctx == NULL) { |
| 546 | DVR_WRAPPER_DEBUG(1, "warp not get ctx.free event..\n"); |
| 547 | goto processed; |
| 548 | } |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 549 | DVR_WRAPPER_DEBUG(1, "start name(%s) sn(%d) running(%d) type(%d)\n", tctx->name, (int)ctx->sn, tctx->running, tctx->type); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 550 | if (tctx->running) { |
| 551 | /* |
| 552 | continue not break, |
| 553 | make all events consumed, or mem leak |
| 554 | */ |
| 555 | if (!wrapper_mutex_lock_if(&ctx->lock, &tctx->running)) |
Zhiqiang Han | ef61c0a | 2020-04-13 15:49:24 +0800 | [diff] [blame] | 556 | goto processed; |
| 557 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 558 | if (ctx_valid(ctx)) { |
| 559 | /*double check after lock*/ |
Zhiqiang Han | 3b9c908 | 2021-11-10 10:41:09 +0800 | [diff] [blame] | 560 | if (evt->sn == ctx->sn) { |
| 561 | pthread_mutex_unlock(&tctx->lock); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 562 | process_handleEvents(evt, ctx); |
Zhiqiang Han | 3b9c908 | 2021-11-10 10:41:09 +0800 | [diff] [blame] | 563 | pthread_mutex_lock(&tctx->lock); |
| 564 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 565 | } |
| 566 | pthread_mutex_unlock(&ctx->lock); |
| 567 | } |
| 568 | |
Zhiqiang Han | ef61c0a | 2020-04-13 15:49:24 +0800 | [diff] [blame] | 569 | processed: |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 570 | ctx_freeEvent(evt); |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 571 | |
| 572 | evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent(); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 573 | } |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 574 | DVR_WRAPPER_DEBUG(1, "start name(%s) running(%d) type(%d) con...\n", tctx->name, tctx->running, tctx->type); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | pthread_mutex_unlock(&tctx->lock); |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 578 | DVR_WRAPPER_DEBUG(1, "end name(%s) running(%d) type(%d) end...\n", tctx->name, tctx->running, tctx->type); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 579 | return NULL; |
| 580 | } |
| 581 | |
| 582 | static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt) |
| 583 | { |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 584 | pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 585 | if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0) |
| 586 | wrapper_threadSignalForType(evt->type); |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 587 | pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt) |
| 592 | { |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 593 | pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 594 | if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0) |
| 595 | wrapper_threadSignalForType(evt->type); |
Zhiqiang Han | 18f42c8 | 2021-08-11 17:13:28 +0800 | [diff] [blame] | 596 | pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx) |
| 601 | { |
| 602 | DVR_WrapperPlaybackSegmentInfo_t *pseg, *pseg_tmp; |
| 603 | list_for_each_entry_safe(pseg, pseg_tmp, &ctx->segments, head) { |
| 604 | list_del(&pseg->head); |
| 605 | free(pseg); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *pseg, |
| 610 | DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx) |
| 611 | { |
| 612 | (void)ctx; |
| 613 | if ((update_flags & U_PIDS) && (update_flags & U_STAT)) |
| 614 | pseg->seg_info = *seg_info; |
| 615 | else if (update_flags & U_PIDS) { |
| 616 | pseg->seg_info.nb_pids = seg_info->nb_pids; |
| 617 | memcpy(pseg->seg_info.pids, seg_info->pids, sizeof(pseg->seg_info.pids)); |
| 618 | } else if (update_flags & U_STAT) { |
| 619 | pseg->seg_info.duration = seg_info->duration; |
| 620 | pseg->seg_info.size = seg_info->size; |
| 621 | pseg->seg_info.nb_packets = seg_info->nb_packets; |
| 622 | } |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 623 | //update current segment duration on timeshift mode |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 624 | if (ctx->playback.param_open.is_timeshift |
| 625 | || ctx_isPlay_recording(ctx->playback.param_open.location)) |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 626 | dvr_playback_update_duration(ctx->playback.player,pseg->seg_info.id,pseg->seg_info.duration); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 627 | /*no changes |
| 628 | DVR_PlaybackSegmentFlag_t flags; |
| 629 | pseg->playback_info.segment_id = pseg->seg_info.id; |
| 630 | strncpy(pseg->playback_info.location, |
| 631 | ctx->playback.param_open.location, sizeof(pseg->playback_info.location)); |
| 632 | pseg->playback_info.pids = ctx->playback.pids_req; |
| 633 | flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS; |
| 634 | if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED) |
| 635 | flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED; |
| 636 | pseg->playback_info.flags = flags; |
| 637 | */ |
| 638 | } |
| 639 | |
| 640 | static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags) |
| 641 | { |
| 642 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 643 | |
| 644 | DVR_WRAPPER_DEBUG(1, "timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n", |
| 645 | seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets); |
| 646 | |
| 647 | if (list_empty(&ctx->segments)) { |
| 648 | DVR_WRAPPER_DEBUG(1, "timeshift, update while no segment exists, ignore\n"); |
| 649 | return DVR_SUCCESS; |
| 650 | } |
| 651 | |
| 652 | /*normally, the last segment added will be updated*/ |
| 653 | pseg = |
| 654 | list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head); |
| 655 | if (pseg->seg_info.id == seg_info->id) { |
| 656 | _updatePlaybackSegment(pseg, seg_info, update_flags, ctx); |
| 657 | } else { |
| 658 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 659 | if (pseg->seg_info.id == seg_info->id) { |
| 660 | _updatePlaybackSegment(pseg, seg_info, update_flags, ctx); |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /*need to notify the dvr_playback*/ |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 667 | if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/ |
| 668 | || ctx_isPlay_recording(ctx->playback.param_open.location)) |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 669 | && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END |
| 670 | && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) { |
| 671 | if ( |
| 672 | /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/ |
| 673 | (ctx->playback.seg_status.segment_id == seg_info->id |
| 674 | && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME))) |
| 675 | || |
| 676 | /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/ |
| 677 | (ctx->playback.seg_status.segment_id != seg_info->id |
| 678 | && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME)) |
| 679 | ) |
| 680 | { |
| 681 | int error; |
hualing chen | 36e0dfd | 2020-05-02 16:33:06 +0800 | [diff] [blame] | 682 | //clear end event |
Zhiqiang Han | b723cdb | 2020-05-09 11:10:29 +0800 | [diff] [blame] | 683 | if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END) |
hualing chen | 36e0dfd | 2020-05-02 16:33:06 +0800 | [diff] [blame] | 684 | ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 685 | |
| 686 | error = dvr_playback_resume(ctx->playback.player); |
| 687 | DVR_WRAPPER_DEBUG(1, "timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n", |
| 688 | ctx->sn, error, |
| 689 | seg_info->id, seg_info->duration, |
| 690 | ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | return DVR_SUCCESS; |
| 695 | } |
| 696 | |
| 697 | static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *pseg, |
| 698 | DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx) |
| 699 | { |
| 700 | (void)ctx; |
| 701 | if ((update_flags & U_PIDS) && (update_flags & U_STAT)) |
| 702 | pseg->info = *seg_info; |
| 703 | else if (update_flags & U_PIDS) { |
| 704 | pseg->info.nb_pids = seg_info->nb_pids; |
| 705 | memcpy(pseg->info.pids, seg_info->pids, sizeof(pseg->info.pids)); |
| 706 | } else if (update_flags & U_STAT) { |
| 707 | pseg->info.duration = seg_info->duration; |
| 708 | pseg->info.size = seg_info->size; |
| 709 | pseg->info.nb_packets = seg_info->nb_packets; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags) |
| 714 | { |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 715 | DVR_WrapperRecordSegmentInfo_t *pseg = NULL; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 716 | |
| 717 | /*normally, the last segment added will be updated*/ |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 718 | if (!list_empty(&ctx->segments)) { |
| 719 | pseg = |
| 720 | list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head); |
| 721 | if (pseg->info.id == seg_info->id) { |
| 722 | _updateRecordSegment(pseg, seg_info, update_flags, ctx); |
| 723 | } else { |
| 724 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 725 | if (pseg->info.id == seg_info->id) { |
| 726 | _updateRecordSegment(pseg, seg_info, update_flags, ctx); |
| 727 | break; |
| 728 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /*timeshift, update the segment for playback*/ |
| 734 | /* |
| 735 | the playback should grab the segment info other than the id, |
| 736 | and the id will be updated by each segment-add during the recording |
| 737 | */ |
| 738 | /* |
| 739 | the playback paused if no data been checked from recording, |
| 740 | should resume the player later when there's more data |
| 741 | */ |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 742 | int sn = 0; |
| 743 | if (ctx->record.param_open.is_timeshift || |
| 744 | (sn = ctx_isRecord_playing(ctx->record.param_open.location))) { |
| 745 | DVR_WrapperCtx_t *ctx_playback; |
| 746 | if (ctx->record.param_open.is_timeshift) |
| 747 | ctx_playback = ctx_getPlayback(sn_timeshift_playback); |
| 748 | else |
| 749 | ctx_playback = ctx_getPlayback(sn); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 750 | |
| 751 | if (ctx_playback) { |
| 752 | pthread_mutex_lock(&ctx_playback->lock); |
| 753 | if (ctx_valid(ctx_playback) |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 754 | && (ctx_playback->sn == sn_timeshift_playback || |
| 755 | ctx_playback->sn == sn)) { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 756 | wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags); |
| 757 | } |
| 758 | pthread_mutex_unlock(&ctx_playback->lock); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | return DVR_SUCCESS; |
| 763 | } |
| 764 | |
| 765 | static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx, |
| 766 | DVR_RecordSegmentInfo_t *seg_info, |
| 767 | DVR_PlaybackPids_t *p_pids, |
| 768 | DVR_PlaybackSegmentFlag_t flags) |
| 769 | { |
| 770 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 771 | int error; |
| 772 | |
| 773 | error = 0; |
| 774 | pseg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t)); |
| 775 | if (!pseg) { |
| 776 | error = DVR_FAILURE; |
| 777 | DVR_WRAPPER_DEBUG(1, "memory fail\n"); |
| 778 | return error; |
| 779 | } |
| 780 | |
| 781 | /*copy the orignal segment info*/ |
| 782 | pseg->seg_info = *seg_info; |
| 783 | /*generate the segment info used in playback*/ |
| 784 | pseg->playback_info.segment_id = pseg->seg_info.id; |
| 785 | strncpy(pseg->playback_info.location, ctx->playback.param_open.location, sizeof(pseg->playback_info.location)); |
| 786 | pseg->playback_info.pids = *p_pids; |
| 787 | pseg->playback_info.flags = flags; |
| 788 | list_add(&pseg->head, &ctx->segments); |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 789 | pseg->playback_info.duration = pseg->seg_info.duration; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 790 | |
| 791 | error = dvr_playback_add_segment(ctx->playback.player, &pseg->playback_info); |
| 792 | if (error) { |
| 793 | DVR_WRAPPER_DEBUG(1, "fail to add segment %lld (%d)\n", pseg->playback_info.segment_id, error); |
| 794 | } else { |
| 795 | ctx->playback.status.info_full.time += pseg->seg_info.duration; |
| 796 | ctx->playback.status.info_full.size += pseg->seg_info.size; |
| 797 | ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets; |
| 798 | } |
| 799 | |
| 800 | return error; |
| 801 | } |
| 802 | |
| 803 | static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info) |
| 804 | { |
| 805 | DVR_WrapperRecordSegmentInfo_t *pseg; |
| 806 | int error; |
hualing chen | ab0d126 | 2021-09-26 15:22:50 +0800 | [diff] [blame] | 807 | int sn = 0; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 808 | |
| 809 | error = 0; |
| 810 | pseg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t)); |
| 811 | if (!pseg) { |
| 812 | error = DVR_FAILURE; |
| 813 | DVR_WRAPPER_DEBUG(1, "memory fail\n"); |
| 814 | } |
| 815 | pseg->info = *seg_info; |
| 816 | list_add(&pseg->head, &ctx->segments); |
hualing chen | ab0d126 | 2021-09-26 15:22:50 +0800 | [diff] [blame] | 817 | |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 818 | if (ctx->record.param_open.is_timeshift || |
| 819 | (sn = ctx_isRecord_playing(ctx->record.param_open.location))) { |
| 820 | |
| 821 | DVR_WrapperCtx_t *ctx_playback; |
| 822 | if (ctx->record.param_open.is_timeshift) |
| 823 | ctx_playback = ctx_getPlayback(sn_timeshift_playback); |
| 824 | else |
| 825 | ctx_playback = ctx_getPlayback(sn); |
| 826 | |
| 827 | DVR_WRAPPER_DEBUG(1, "ctx_playback ---- add segment\n"); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 828 | |
| 829 | if (ctx_playback) { |
| 830 | pthread_mutex_lock(&ctx_playback->lock); |
| 831 | if (ctx_valid(ctx_playback)) { |
| 832 | DVR_PlaybackSegmentFlag_t flags; |
| 833 | |
| 834 | /*only if playback has started, the previous segments have been loaded*/ |
| 835 | if (!list_empty(&ctx_playback->segments)) { |
| 836 | flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS; |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 837 | if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED) |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 838 | flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED; |
| 839 | wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags); |
| 840 | } |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 841 | } else { |
| 842 | DVR_WRAPPER_DEBUG(1, "ctx_playback ---- not valid\n"); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 843 | } |
| 844 | pthread_mutex_unlock(&ctx_playback->lock); |
| 845 | } |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame] | 846 | } else { |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 847 | DVR_WRAPPER_DEBUG(1, "ctx_playback -sn[%d]-\n", sn); |
Zhiqiang Han | e0a1c38 | 2021-06-08 11:28:05 +0800 | [diff] [blame] | 848 | dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, LSEG_OP_ADD); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return error; |
| 852 | } |
| 853 | |
| 854 | static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info) |
| 855 | { |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 856 | int error = -1; |
| 857 | DVR_WrapperPlaybackSegmentInfo_t *pseg = NULL, *pseg_tmp; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 858 | |
| 859 | DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id); |
| 860 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 861 | list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 862 | if (pseg->seg_info.id == seg_info->id) { |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 863 | |
| 864 | if (ctx->current_segment_id == seg_info->id) { |
| 865 | DVR_WrapperPlaybackSegmentInfo_t *next_seg; |
| 866 | |
| 867 | /*drive the player out of this will-be-deleted segment*/ |
| 868 | next_seg = list_prev_entry(pseg, head); |
| 869 | |
| 870 | if (ctx->playback.speed != 100.0f) { |
| 871 | error = dvr_playback_resume(ctx->playback.player); |
| 872 | DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error); |
| 873 | } |
| 874 | |
| 875 | error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, 0); |
| 876 | DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), seek(seg:%llu 0) from new start (%d)\n", ctx->sn, next_seg->seg_info.id, error); |
| 877 | |
| 878 | if (ctx->playback.speed == 0.0f) { |
| 879 | error = dvr_playback_pause(ctx->playback.player, DVR_FALSE); |
| 880 | DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last paused from new start (%d)\n", ctx->sn, error); |
| 881 | } else if (ctx->playback.speed != 100.0f) { |
| 882 | DVR_PlaybackSpeed_t dvr_speed = { |
| 883 | .speed = { ctx->playback.speed }, |
| 884 | .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD |
| 885 | }; |
| 886 | error = dvr_playback_set_speed(ctx->playback.player, dvr_speed); |
| 887 | DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last speed(x%f) from new start (%d)\n", ctx->sn,ctx->playback.speed, error); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id); |
| 892 | if (error) { |
| 893 | /*remove playack segment fail*/ |
| 894 | DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), failed to remove segment(%llu) (%d)\n", ctx->sn, seg_info->id, error); |
| 895 | } |
| 896 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 897 | list_del(&pseg->head); |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 898 | |
| 899 | /*record the obsolete*/ |
| 900 | ctx->playback.obsolete.time += pseg->seg_info.duration; |
| 901 | ctx->playback.obsolete.size += pseg->seg_info.size; |
| 902 | ctx->playback.obsolete.pkts += pseg->seg_info.nb_packets; |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 903 | dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 904 | free(pseg); |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 905 | break; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
| 909 | DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->id, error); |
| 910 | |
| 911 | return error; |
| 912 | } |
| 913 | |
| 914 | static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info) |
| 915 | { |
| 916 | int error; |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 917 | DVR_WrapperRecordSegmentInfo_t *pseg, *pseg_tmp; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 918 | |
| 919 | DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->info.id); |
| 920 | |
| 921 | /*if timeshifting, notify the playback first, then deal with record*/ |
| 922 | if (ctx->record.param_open.is_timeshift) { |
| 923 | DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback); |
| 924 | |
| 925 | if (ctx_playback) { |
| 926 | pthread_mutex_lock(&ctx_playback->lock); |
| 927 | if (ctx_valid(ctx_playback) |
| 928 | && ctx_playback->sn == sn_timeshift_playback |
| 929 | && !list_empty(&ctx_playback->segments)) { |
| 930 | error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info); |
| 931 | } |
| 932 | pthread_mutex_unlock(&ctx_playback->lock); |
| 933 | } |
| 934 | } |
| 935 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 936 | list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) { |
| 937 | if (pseg->info.id == seg_info->info.id) { |
| 938 | list_del(&pseg->head); |
| 939 | |
| 940 | /*record the obsolete*/ |
| 941 | ctx->record.obsolete.time += pseg->info.duration; |
| 942 | ctx->record.obsolete.size += pseg->info.size; |
| 943 | ctx->record.obsolete.pkts += pseg->info.nb_packets; |
| 944 | |
| 945 | free(pseg); |
| 946 | break; |
| 947 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | error = dvr_segment_delete(ctx->record.param_open.location, seg_info->info.id); |
| 951 | |
| 952 | DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->info.id, error); |
| 953 | |
| 954 | return error; |
| 955 | } |
| 956 | |
| 957 | int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params) |
| 958 | { |
| 959 | int error; |
| 960 | DVR_WrapperCtx_t *ctx; |
| 961 | DVR_RecordOpenParams_t open_param; |
| 962 | |
| 963 | DVR_RETURN_IF_FALSE(rec); |
| 964 | DVR_RETURN_IF_FALSE(params); |
| 965 | |
| 966 | /*get a free ctx*/ |
| 967 | ctx = ctx_getRecord(0); |
| 968 | DVR_RETURN_IF_FALSE(ctx); |
| 969 | |
| 970 | pthread_mutex_lock(&ctx->lock); |
| 971 | |
hualing chen | 51652f0 | 2020-12-29 16:59:31 +0800 | [diff] [blame] | 972 | DVR_WRAPPER_DEBUG(1, "open record(dmx:%d) .istf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n", |
| 973 | params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 974 | |
| 975 | ctx_reset(ctx); |
| 976 | |
| 977 | ctx->record.param_open = *params; |
| 978 | ctx->record.event_fn = params->event_fn; |
| 979 | ctx->record.event_userdata = params->event_userdata; |
| 980 | ctx->record.next_segment_id = 0; |
| 981 | ctx->current_segment_id = 0; |
| 982 | INIT_LIST_HEAD(&ctx->segments); |
| 983 | ctx->sn = get_sn(); |
| 984 | |
| 985 | wrapper_requestThreadFor(ctx); |
| 986 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 987 | memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t)); |
Yahui Han | ce15e9c | 2020-12-08 18:08:32 +0800 | [diff] [blame] | 988 | open_param.fend_dev_id = params->fend_dev_id; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 989 | open_param.dmx_dev_id = params->dmx_dev_id; |
| 990 | open_param.data_from_memory = 0; |
| 991 | open_param.flags = params->flags; |
Yahui Han | 15a00f1 | 2021-11-15 19:44:39 +0800 | [diff] [blame] | 992 | if (params->flush_size) { |
| 993 | open_param.notification_size = params->flush_size; |
| 994 | } else { |
| 995 | open_param.notification_size = 64*1024; |
| 996 | } |
Zhiqiang Han | 3150545 | 2020-05-06 15:08:10 +0800 | [diff] [blame] | 997 | open_param.flush_size = params->flush_size; |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 998 | open_param.ringbuf_size = params->ringbuf_size; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 999 | open_param.event_fn = wrapper_record_event_handler; |
| 1000 | open_param.event_userdata = (void*)ctx->sn; |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 1001 | if (params->keylen) { |
| 1002 | open_param.clearkey = params->clearkey; |
| 1003 | open_param.cleariv = params->cleariv; |
| 1004 | open_param.keylen = params->keylen; |
| 1005 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1006 | |
| 1007 | error = dvr_record_open(&ctx->record.recorder, &open_param); |
| 1008 | if (error) { |
| 1009 | DVR_WRAPPER_DEBUG(1, "record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error); |
| 1010 | ctx_reset(ctx); |
| 1011 | pthread_mutex_unlock(&ctx->lock); |
| 1012 | wrapper_releaseThreadForType(ctx->type); |
| 1013 | return DVR_FAILURE; |
| 1014 | } |
| 1015 | if (params->is_timeshift) |
| 1016 | sn_timeshift_record = ctx->sn; |
| 1017 | |
| 1018 | DVR_WRAPPER_DEBUG(1, "record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn); |
| 1019 | |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 1020 | if (params->crypto_fn) { |
| 1021 | error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data); |
| 1022 | if (error) { |
| 1023 | DVR_WRAPPER_DEBUG(1, "record(dmx:%d) set encrypt callback fail(error:%d).\n", params->dmx_dev_id, error); |
| 1024 | } |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1027 | pthread_mutex_unlock(&ctx->lock); |
| 1028 | |
| 1029 | *rec = (DVR_WrapperRecord_t)ctx->sn; |
| 1030 | return DVR_SUCCESS; |
| 1031 | } |
| 1032 | |
| 1033 | int dvr_wrapper_close_record (DVR_WrapperRecord_t rec) |
| 1034 | { |
| 1035 | DVR_WrapperCtx_t *ctx; |
| 1036 | DVR_RecordSegmentInfo_t seg_info; |
| 1037 | int error; |
| 1038 | |
| 1039 | DVR_RETURN_IF_FALSE(rec); |
| 1040 | |
| 1041 | ctx = ctx_getRecord((unsigned long)rec); |
| 1042 | DVR_RETURN_IF_FALSE(ctx); |
| 1043 | |
| 1044 | pthread_mutex_lock(&ctx->lock); |
| 1045 | DVR_WRAPPER_DEBUG(1, "close record(sn:%ld)\n", ctx->sn); |
| 1046 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1047 | |
| 1048 | memset(&seg_info, 0, sizeof(seg_info)); |
| 1049 | error = dvr_record_stop_segment(ctx->record.recorder, &seg_info); |
| 1050 | |
| 1051 | error = dvr_record_close(ctx->record.recorder); |
| 1052 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1053 | if (ctx->record.param_open.is_timeshift) |
| 1054 | sn_timeshift_record = 0; |
| 1055 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1056 | ctx_freeSegments(ctx); |
| 1057 | |
| 1058 | DVR_WRAPPER_DEBUG(1, "record(sn:%ld) closed = (%d).\n", ctx->sn, error); |
| 1059 | ctx_reset(ctx); |
| 1060 | pthread_mutex_unlock(&ctx->lock); |
| 1061 | |
| 1062 | wrapper_releaseThreadForType(ctx->type); |
| 1063 | |
| 1064 | return error; |
| 1065 | } |
| 1066 | |
| 1067 | int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params) |
| 1068 | { |
| 1069 | DVR_WrapperCtx_t *ctx; |
| 1070 | DVR_RecordStartParams_t *start_param; |
| 1071 | int i; |
| 1072 | int error; |
| 1073 | |
| 1074 | DVR_RETURN_IF_FALSE(rec); |
| 1075 | DVR_RETURN_IF_FALSE(params); |
| 1076 | |
| 1077 | ctx = ctx_getRecord((unsigned long)rec); |
| 1078 | DVR_RETURN_IF_FALSE(ctx); |
| 1079 | |
| 1080 | pthread_mutex_lock(&ctx->lock); |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 1081 | DVR_WRAPPER_DEBUG(1, "start record(sn:%ld, location:%s) save(%d)...\n", ctx->sn, ctx->record.param_open.location, params->save_rec_file); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1082 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1083 | |
| 1084 | start_param = &ctx->record.param_start; |
| 1085 | memset(start_param, 0, sizeof(*start_param)); |
| 1086 | strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location)); |
| 1087 | start_param->segment.segment_id = ctx->record.next_segment_id++; |
| 1088 | start_param->segment.nb_pids = params->pids_info.nb_pids; |
| 1089 | for (i = 0; i < params->pids_info.nb_pids; i++) { |
| 1090 | start_param->segment.pids[i] = params->pids_info.pids[i]; |
| 1091 | start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE; |
| 1092 | } |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 1093 | if (params->save_rec_file == 0)//default is not save |
| 1094 | dvr_segment_del_by_location(start_param->location); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1095 | { |
| 1096 | /*sync to update for further use*/ |
| 1097 | DVR_RecordStartParams_t *update_param; |
| 1098 | update_param = &ctx->record.param_update; |
| 1099 | memcpy(update_param, start_param, sizeof(*update_param)); |
| 1100 | for (i = 0; i < update_param->segment.nb_pids; i++) |
| 1101 | update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP; |
| 1102 | } |
| 1103 | |
| 1104 | error = dvr_record_start_segment(ctx->record.recorder, start_param); |
| 1105 | { |
| 1106 | DVR_RecordSegmentInfo_t new_seg_info = |
| 1107 | { .id = start_param->segment.segment_id, }; |
| 1108 | wrapper_addRecordSegment(ctx, &new_seg_info); |
| 1109 | } |
| 1110 | |
| 1111 | DVR_WRAPPER_DEBUG(1, "record(sn:%ld) started = (%d)\n", ctx->sn, error); |
| 1112 | |
| 1113 | pthread_mutex_unlock(&ctx->lock); |
| 1114 | |
| 1115 | return error; |
| 1116 | } |
| 1117 | |
| 1118 | int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec) |
| 1119 | { |
| 1120 | DVR_WrapperCtx_t *ctx; |
| 1121 | DVR_RecordSegmentInfo_t seg_info; |
| 1122 | int error; |
| 1123 | |
| 1124 | DVR_RETURN_IF_FALSE(rec); |
| 1125 | |
| 1126 | ctx = ctx_getRecord((unsigned long)rec); |
| 1127 | DVR_RETURN_IF_FALSE(ctx); |
| 1128 | |
| 1129 | pthread_mutex_lock(&ctx->lock); |
| 1130 | DVR_WRAPPER_DEBUG(1, "stop record(sn:%ld) ...\n", ctx->sn); |
| 1131 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1132 | |
| 1133 | memset(&seg_info, 0, sizeof(seg_info)); |
| 1134 | error = dvr_record_stop_segment(ctx->record.recorder, &seg_info); |
| 1135 | wrapper_updateRecordSegment(ctx, &seg_info, U_ALL); |
| 1136 | |
| 1137 | DVR_WRAPPER_DEBUG(1, "record(sn:%ld) stopped = (%d)\n", ctx->sn, error); |
| 1138 | pthread_mutex_unlock(&ctx->lock); |
| 1139 | |
| 1140 | return error; |
| 1141 | } |
| 1142 | |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 1143 | int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec) |
| 1144 | { |
| 1145 | DVR_WrapperCtx_t *ctx; |
| 1146 | int error; |
| 1147 | |
| 1148 | DVR_RETURN_IF_FALSE(rec); |
| 1149 | |
| 1150 | ctx = ctx_getRecord((unsigned long)rec); |
| 1151 | DVR_RETURN_IF_FALSE(ctx); |
| 1152 | |
| 1153 | pthread_mutex_lock(&ctx->lock); |
| 1154 | DVR_WRAPPER_DEBUG(1, "pause record(sn:%ld) ...\n", ctx->sn); |
| 1155 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1156 | |
| 1157 | error = dvr_record_pause(ctx->record.recorder); |
| 1158 | |
| 1159 | DVR_WRAPPER_DEBUG(1, "record(sn:%ld) pauseed = (%d)\n", ctx->sn, error); |
| 1160 | pthread_mutex_unlock(&ctx->lock); |
| 1161 | |
| 1162 | return error; |
| 1163 | } |
| 1164 | |
| 1165 | int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec) |
| 1166 | { |
| 1167 | DVR_WrapperCtx_t *ctx; |
| 1168 | int error; |
| 1169 | |
| 1170 | DVR_RETURN_IF_FALSE(rec); |
| 1171 | |
| 1172 | ctx = ctx_getRecord((unsigned long)rec); |
| 1173 | DVR_RETURN_IF_FALSE(ctx); |
| 1174 | |
| 1175 | pthread_mutex_lock(&ctx->lock); |
| 1176 | DVR_WRAPPER_DEBUG(1, "resume record(sn:%ld) ...\n", ctx->sn); |
| 1177 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1178 | |
| 1179 | error = dvr_record_resume(ctx->record.recorder); |
| 1180 | |
| 1181 | DVR_WRAPPER_DEBUG(1, "record(sn:%ld) resumed = (%d)\n", ctx->sn, error); |
| 1182 | pthread_mutex_unlock(&ctx->lock); |
| 1183 | |
| 1184 | return error; |
| 1185 | } |
| 1186 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1187 | int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params) |
| 1188 | { |
| 1189 | DVR_WrapperCtx_t *ctx; |
| 1190 | DVR_RecordStartParams_t *start_param; |
| 1191 | DVR_RecordSegmentInfo_t seg_info;; |
| 1192 | int i; |
| 1193 | int error; |
| 1194 | |
| 1195 | DVR_RETURN_IF_FALSE(rec); |
| 1196 | DVR_RETURN_IF_FALSE(params); |
| 1197 | |
| 1198 | ctx = ctx_getRecord((unsigned long)rec); |
| 1199 | DVR_RETURN_IF_FALSE(ctx); |
| 1200 | |
| 1201 | pthread_mutex_lock(&ctx->lock); |
| 1202 | DVR_WRAPPER_DEBUG(1, "update record(sn:%ld)\n", ctx->sn); |
| 1203 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1204 | |
| 1205 | start_param = &ctx->record.param_update; |
| 1206 | memset(start_param, 0, sizeof(*start_param)); |
| 1207 | strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location)); |
| 1208 | start_param->segment.segment_id = ctx->record.next_segment_id++; |
| 1209 | start_param->segment.nb_pids = params->nb_pids; |
| 1210 | for (i = 0; i < params->nb_pids; i++) { |
| 1211 | start_param->segment.pids[i] = params->pids[i]; |
| 1212 | start_param->segment.pid_action[i] = params->pid_action[i]; |
| 1213 | } |
| 1214 | error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info); |
| 1215 | { |
| 1216 | DVR_RecordSegmentInfo_t new_seg_info = |
| 1217 | { .id = start_param->segment.segment_id, }; |
| 1218 | wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS); |
| 1219 | wrapper_addRecordSegment(ctx, &new_seg_info); |
| 1220 | } |
| 1221 | |
| 1222 | DVR_WRAPPER_DEBUG(1, "record(sn:%ld) updated = (%d)\n", ctx->sn, error); |
| 1223 | pthread_mutex_unlock(&ctx->lock); |
| 1224 | |
| 1225 | return error; |
| 1226 | } |
| 1227 | |
| 1228 | int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status) |
| 1229 | { |
| 1230 | DVR_WrapperCtx_t *ctx; |
| 1231 | DVR_WrapperRecordStatus_t s; |
| 1232 | int error; |
| 1233 | |
| 1234 | DVR_RETURN_IF_FALSE(rec); |
| 1235 | DVR_RETURN_IF_FALSE(status); |
| 1236 | |
| 1237 | ctx = ctx_getRecord((unsigned long)rec); |
| 1238 | DVR_RETURN_IF_FALSE(ctx); |
| 1239 | |
| 1240 | pthread_mutex_lock(&ctx->lock); |
| 1241 | |
| 1242 | DVR_WRAPPER_DEBUG(1, "get record(sn:%ld) status ...\n", ctx->sn); |
| 1243 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1244 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1245 | error = process_generateRecordStatus(ctx, &s); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1246 | |
| 1247 | DVR_WRAPPER_DEBUG(1, "record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n", |
| 1248 | ctx->sn, |
| 1249 | s.state, |
| 1250 | s.info.time, |
| 1251 | s.info.size, |
| 1252 | s.info.pkts, |
| 1253 | error); |
| 1254 | |
| 1255 | *status = s; |
| 1256 | |
| 1257 | pthread_mutex_unlock(&ctx->lock); |
| 1258 | |
| 1259 | return error; |
| 1260 | } |
| 1261 | |
hualing chen | 4fe3bee | 2020-10-23 13:58:52 +0800 | [diff] [blame] | 1262 | int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec) |
| 1263 | { |
| 1264 | DVR_WrapperCtx_t *ctx; |
| 1265 | int error; |
| 1266 | |
| 1267 | DVR_RETURN_IF_FALSE(rec); |
| 1268 | |
| 1269 | ctx = ctx_getRecord((unsigned long)rec); |
| 1270 | DVR_RETURN_IF_FALSE(ctx); |
| 1271 | |
| 1272 | pthread_mutex_lock(&ctx->lock); |
| 1273 | error = dvr_record_is_secure_mode(ctx->record.recorder); |
| 1274 | pthread_mutex_unlock(&ctx->lock); |
| 1275 | return error; |
| 1276 | } |
| 1277 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 1278 | int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len) |
| 1279 | { |
| 1280 | DVR_WrapperCtx_t *ctx; |
| 1281 | int error; |
| 1282 | |
| 1283 | DVR_RETURN_IF_FALSE(rec); |
| 1284 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 1285 | |
| 1286 | ctx = ctx_getRecord((unsigned long)rec); |
| 1287 | DVR_RETURN_IF_FALSE(ctx); |
| 1288 | |
| 1289 | pthread_mutex_lock(&ctx->lock); |
| 1290 | error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len); |
| 1291 | pthread_mutex_unlock(&ctx->lock); |
| 1292 | return error; |
| 1293 | } |
| 1294 | |
| 1295 | int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata) |
| 1296 | { |
| 1297 | DVR_WrapperCtx_t *ctx; |
| 1298 | int error; |
| 1299 | |
| 1300 | DVR_RETURN_IF_FALSE(rec); |
| 1301 | DVR_RETURN_IF_FALSE(func); |
| 1302 | |
| 1303 | ctx = ctx_getRecord((unsigned long)rec); |
| 1304 | DVR_RETURN_IF_FALSE(ctx); |
| 1305 | |
| 1306 | pthread_mutex_lock(&ctx->lock); |
| 1307 | error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata); |
| 1308 | pthread_mutex_unlock(&ctx->lock); |
| 1309 | return error; |
| 1310 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1311 | |
| 1312 | |
| 1313 | int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params) |
| 1314 | { |
| 1315 | DVR_WrapperCtx_t *ctx; |
| 1316 | DVR_PlaybackOpenParams_t open_param; |
| 1317 | int error; |
| 1318 | |
| 1319 | DVR_RETURN_IF_FALSE(playback); |
| 1320 | DVR_RETURN_IF_FALSE(params); |
| 1321 | DVR_RETURN_IF_FALSE(params->playback_handle); |
| 1322 | |
| 1323 | /*get a free ctx*/ |
| 1324 | ctx = ctx_getPlayback(0); |
| 1325 | DVR_RETURN_IF_FALSE(ctx); |
| 1326 | |
| 1327 | pthread_mutex_lock(&ctx->lock); |
| 1328 | |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 1329 | DVR_WRAPPER_DEBUG(1, "open playback(dmx:%d) ..vendor[%d]params->block_size[%d].\n", params->dmx_dev_id, params->vendor, params->block_size); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1330 | |
| 1331 | ctx_reset(ctx); |
| 1332 | |
| 1333 | ctx->playback.param_open = *params; |
| 1334 | ctx->playback.event_fn = params->event_fn; |
| 1335 | ctx->playback.event_userdata = params->event_userdata; |
| 1336 | ctx->current_segment_id = 0; |
| 1337 | INIT_LIST_HEAD(&ctx->segments); |
| 1338 | ctx->sn = get_sn(); |
| 1339 | |
| 1340 | wrapper_requestThreadFor(ctx); |
| 1341 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 1342 | memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t)); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1343 | open_param.dmx_dev_id = params->dmx_dev_id; |
| 1344 | open_param.block_size = params->block_size; |
| 1345 | open_param.is_timeshift = params->is_timeshift; |
| 1346 | //open_param.notification_size = 10*1024; //not supported |
| 1347 | open_param.event_fn = wrapper_playback_event_handler; |
| 1348 | open_param.event_userdata = (void*)ctx->sn; |
| 1349 | /*open_param.has_pids = 0;*/ |
hualing chen | e3797f0 | 2021-01-13 14:53:28 +0800 | [diff] [blame] | 1350 | open_param.is_notify_time = params->is_notify_time; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1351 | open_param.player_handle = (am_tsplayer_handle)params->playback_handle; |
hualing chen | 90b3ae6 | 2021-03-30 10:49:28 +0800 | [diff] [blame] | 1352 | open_param.vendor = params->vendor; |
| 1353 | |
Yahui Han | 1fbf329 | 2021-11-08 18:17:19 +0800 | [diff] [blame] | 1354 | if (params->keylen) { |
| 1355 | open_param.clearkey = params->clearkey; |
| 1356 | open_param.cleariv = params->cleariv; |
| 1357 | open_param.keylen = params->keylen; |
| 1358 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1359 | |
| 1360 | error = dvr_playback_open(&ctx->playback.player, &open_param); |
| 1361 | if (error) { |
| 1362 | DVR_WRAPPER_DEBUG(1, "playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error); |
| 1363 | ctx_reset(ctx); |
| 1364 | pthread_mutex_unlock(&ctx->lock); |
| 1365 | wrapper_releaseThreadForType(ctx->type); |
| 1366 | return DVR_FAILURE; |
| 1367 | } |
| 1368 | if (params->is_timeshift) |
| 1369 | sn_timeshift_playback = ctx->sn; |
| 1370 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 1371 | DVR_WRAPPER_DEBUG(1, "hanyh: playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn); |
| 1372 | error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data); |
| 1373 | if (error) { |
| 1374 | DVR_WRAPPER_DEBUG(1, "playback set deccrypt callback fail(error:%d).\n", error); |
| 1375 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1376 | pthread_mutex_unlock(&ctx->lock); |
| 1377 | |
| 1378 | *playback = (DVR_WrapperPlayback_t)ctx->sn; |
| 1379 | return DVR_SUCCESS; |
| 1380 | } |
| 1381 | |
| 1382 | int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback) |
| 1383 | { |
| 1384 | DVR_WrapperCtx_t *ctx; |
| 1385 | int error; |
| 1386 | |
| 1387 | DVR_RETURN_IF_FALSE(playback); |
| 1388 | |
| 1389 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1390 | DVR_RETURN_IF_FALSE(ctx); |
| 1391 | |
| 1392 | pthread_mutex_lock(&ctx->lock); |
| 1393 | DVR_WRAPPER_DEBUG(1, "close playback(sn:%ld)\n", ctx->sn); |
| 1394 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1395 | |
| 1396 | if (ctx->playback.param_open.is_timeshift) |
| 1397 | sn_timeshift_playback = 0; |
| 1398 | |
| 1399 | /*try stop first*/ |
| 1400 | error = dvr_playback_stop(ctx->playback.player, DVR_TRUE); |
| 1401 | |
| 1402 | { |
| 1403 | /*remove all segments*/ |
| 1404 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 1405 | |
| 1406 | list_for_each_entry(pseg, &ctx->segments, head) { |
| 1407 | error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id); |
| 1408 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n", |
| 1409 | ctx->sn, pseg->playback_info.segment_id, error); |
| 1410 | } |
| 1411 | ctx_freeSegments(ctx); |
| 1412 | } |
| 1413 | |
| 1414 | error = dvr_playback_close(ctx->playback.player); |
| 1415 | |
| 1416 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) closed.\n", ctx->sn); |
| 1417 | ctx_reset(ctx); |
| 1418 | pthread_mutex_unlock(&ctx->lock); |
| 1419 | |
| 1420 | wrapper_releaseThreadForType(ctx->type); |
| 1421 | |
| 1422 | return error; |
| 1423 | } |
| 1424 | |
| 1425 | int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids) |
| 1426 | { |
| 1427 | DVR_WrapperCtx_t *ctx; |
Wentao MA | 9aa0aa0 | 2021-12-23 18:30:17 +0800 | [diff] [blame] | 1428 | int error=0; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1429 | uint64_t *p_segment_ids; |
| 1430 | uint32_t segment_nb; |
| 1431 | uint32_t i; |
| 1432 | DVR_RecordSegmentInfo_t seg_info_1st; |
Wentao MA | 9aa0aa0 | 2021-12-23 18:30:17 +0800 | [diff] [blame] | 1433 | int got_1st_seg=0; |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1434 | DVR_WrapperCtx_t *ctx_record;/*for timeshift*/ |
hualing chen | c110f95 | 2021-01-18 11:25:37 +0800 | [diff] [blame] | 1435 | DVR_Bool_t is_timeshift = DVR_FALSE; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1436 | |
| 1437 | DVR_RETURN_IF_FALSE(playback); |
| 1438 | DVR_RETURN_IF_FALSE(p_pids); |
| 1439 | |
hualing chen | c110f95 | 2021-01-18 11:25:37 +0800 | [diff] [blame] | 1440 | ctx_record = NULL; |
| 1441 | |
| 1442 | /*lock the recorder to avoid changing the recording segments*/ |
| 1443 | ctx_record = ctx_getRecord(sn_timeshift_record); |
| 1444 | |
| 1445 | if (ctx_record) { |
| 1446 | pthread_mutex_lock(&ctx_record->lock); |
| 1447 | if (!ctx_valid(ctx_record) |
| 1448 | || ctx_record->sn != sn_timeshift_record) { |
| 1449 | DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error found\n"); |
| 1450 | pthread_mutex_unlock(&ctx_record->lock); |
| 1451 | is_timeshift = DVR_FALSE; |
| 1452 | } else { |
| 1453 | is_timeshift = DVR_TRUE; |
| 1454 | } |
| 1455 | } |
| 1456 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1457 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1458 | DVR_RETURN_IF_FALSE(ctx); |
| 1459 | |
| 1460 | pthread_mutex_lock(&ctx->lock); |
| 1461 | |
| 1462 | DVR_WRAPPER_DEBUG(1, "start playback(sn:%ld) (%s)\n\t flags(0x%x) v/a/ad/sub/pcr(%d:%d %d:%d %d:%d %d:%d %d)\n", |
| 1463 | ctx->sn, |
| 1464 | ctx->playback.param_open.location, |
| 1465 | flags, |
| 1466 | p_pids->video.pid, p_pids->video.format, |
| 1467 | p_pids->audio.pid, p_pids->audio.format, |
| 1468 | p_pids->ad.pid, p_pids->ad.format, |
| 1469 | p_pids->subtitle.pid, p_pids->subtitle.format, |
| 1470 | p_pids->pcr.pid); |
| 1471 | |
| 1472 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1473 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1474 | if (ctx->playback.param_open.is_timeshift) { |
| 1475 | /*lock the recorder to avoid changing the recording segments*/ |
hualing chen | c110f95 | 2021-01-18 11:25:37 +0800 | [diff] [blame] | 1476 | if (is_timeshift == DVR_FALSE) { |
| 1477 | DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error return\n"); |
| 1478 | pthread_mutex_unlock(&ctx->lock); |
| 1479 | return DVR_FAILURE; |
| 1480 | } else { |
| 1481 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n", |
| 1482 | ctx->sn, ctx_record->sn); |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1483 | } |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1484 | } |
| 1485 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1486 | /*obtain all segments in a list*/ |
| 1487 | segment_nb = 0; |
| 1488 | p_segment_ids = NULL; |
| 1489 | error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids); |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1490 | if (!error) { |
| 1491 | got_1st_seg = 0; |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1492 | struct list_head info_list; /**< segment list head*/ |
| 1493 | INIT_LIST_HEAD(&info_list); |
| 1494 | |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 1495 | DVR_WRAPPER_DEBUG(1, "get list segment_nb::%d",segment_nb); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1496 | //we need free info list buf when we used end. |
| 1497 | error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list); |
hualing chen | 926a8ec | 2021-12-20 20:38:24 +0800 | [diff] [blame] | 1498 | if (error == DVR_FAILURE) { |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1499 | error = DVR_FAILURE; |
| 1500 | DVR_WRAPPER_DEBUG(1, "fail to get all seg info (location:%s, seg:%llu), (error:%d)\n", |
| 1501 | ctx->playback.param_open.location, p_segment_ids[i], error); |
| 1502 | for (i = 0; i < segment_nb; i++) { |
| 1503 | DVR_RecordSegmentInfo_t seg_info; |
| 1504 | DVR_PlaybackSegmentFlag_t flags; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1505 | |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1506 | error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info); |
| 1507 | if (error) { |
| 1508 | error = DVR_FAILURE; |
| 1509 | DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n", |
| 1510 | ctx->playback.param_open.location, p_segment_ids[i], error); |
| 1511 | break; |
| 1512 | } |
| 1513 | //add check if has audio or video pid. if not exist. not add segment to playback |
| 1514 | int ii = 0; |
| 1515 | int has_av = 0; |
| 1516 | for (ii = 0; ii < seg_info.nb_pids; ii++) { |
| 1517 | int type = (seg_info.pids[ii].type >> 24) & 0x0f; |
| 1518 | if (type == DVR_STREAM_TYPE_VIDEO || |
| 1519 | type == DVR_STREAM_TYPE_AUDIO || |
| 1520 | type == DVR_STREAM_TYPE_AD) { |
| 1521 | DVR_WRAPPER_DEBUG(1, "success to get seg av info \n"); |
| 1522 | DVR_WRAPPER_DEBUG(1, "success to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid, |
| 1523 | DVR_STREAM_TYPE_VIDEO, |
| 1524 | DVR_STREAM_TYPE_AUDIO, |
| 1525 | DVR_STREAM_TYPE_AD); |
| 1526 | has_av = 1; |
| 1527 | //break; |
| 1528 | } else { |
| 1529 | DVR_WRAPPER_DEBUG(1, "error to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid, |
| 1530 | DVR_STREAM_TYPE_VIDEO, |
| 1531 | DVR_STREAM_TYPE_AUDIO, |
| 1532 | DVR_STREAM_TYPE_AD); |
| 1533 | } |
| 1534 | } |
| 1535 | if (has_av == 0) { |
| 1536 | DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n"); |
| 1537 | continue; |
| 1538 | } else { |
| 1539 | DVR_WRAPPER_DEBUG(1, "success to get seg av info \n"); |
| 1540 | } |
| 1541 | flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS; |
| 1542 | error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags); |
| 1543 | if (error) |
| 1544 | break; |
| 1545 | /*copy the 1st segment*/ |
| 1546 | if (got_1st_seg == 0) { |
| 1547 | seg_info_1st = seg_info; |
| 1548 | got_1st_seg = 1; |
| 1549 | } |
| 1550 | } |
| 1551 | } else { |
| 1552 | for (i = 0; i < segment_nb; i++) { |
| 1553 | DVR_RecordSegmentInfo_t *seg_info; |
| 1554 | DVR_PlaybackSegmentFlag_t flags; |
| 1555 | int found = 0; |
| 1556 | list_for_each_entry(seg_info, &info_list, head) |
| 1557 | { |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 1558 | if (seg_info->id == p_segment_ids[i]) { |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1559 | found = 1; |
| 1560 | DVR_WRAPPER_DEBUG(1, "get segment info::%d", i); |
| 1561 | break; |
| 1562 | } |
| 1563 | } |
| 1564 | if (!found) { |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 1565 | //last info is not found if when recording occured power off. |
| 1566 | if (p_segment_ids[i] == segment_nb - 1) { |
| 1567 | DVR_RecordSegmentInfo_t seg_info; |
| 1568 | DVR_PlaybackSegmentFlag_t flags; |
| 1569 | |
| 1570 | error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info); |
| 1571 | if (error) { |
| 1572 | error = DVR_FAILURE; |
| 1573 | DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n", |
| 1574 | ctx->playback.param_open.location, p_segment_ids[i], error); |
| 1575 | break; |
| 1576 | } |
| 1577 | // |
| 1578 | //add check if has audio or video pid. if not exist. not add segment to playback |
| 1579 | int ii = 0; |
| 1580 | int has_av = 0; |
| 1581 | for (ii = 0; ii < seg_info.nb_pids; ii++) { |
| 1582 | int type = (seg_info.pids[ii].type >> 24) & 0x0f; |
| 1583 | if (type == DVR_STREAM_TYPE_VIDEO || |
| 1584 | type == DVR_STREAM_TYPE_AUDIO || |
| 1585 | type == DVR_STREAM_TYPE_AD) { |
| 1586 | DVR_WRAPPER_DEBUG(1, "success to get seg av info \n"); |
| 1587 | DVR_WRAPPER_DEBUG(1, "success to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid, |
| 1588 | DVR_STREAM_TYPE_VIDEO, |
| 1589 | DVR_STREAM_TYPE_AUDIO, |
| 1590 | DVR_STREAM_TYPE_AD); |
| 1591 | has_av = 1; |
| 1592 | //break; |
| 1593 | } else { |
| 1594 | DVR_WRAPPER_DEBUG(1, "error to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid, |
| 1595 | DVR_STREAM_TYPE_VIDEO, |
| 1596 | DVR_STREAM_TYPE_AUDIO, |
| 1597 | DVR_STREAM_TYPE_AD); |
| 1598 | } |
| 1599 | } |
| 1600 | if (has_av == 0) { |
| 1601 | DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n"); |
| 1602 | continue; |
| 1603 | } else { |
| 1604 | DVR_WRAPPER_DEBUG(1, "success to get seg av info \n"); |
| 1605 | } |
| 1606 | flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS; |
| 1607 | error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags); |
| 1608 | if (error) |
| 1609 | break; |
| 1610 | } |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1611 | continue; |
| 1612 | } |
| 1613 | |
| 1614 | //add check if has audio or video pid. if not exist. not add segment to playback |
| 1615 | int ii = 0; |
| 1616 | int has_av = 0; |
| 1617 | for (ii = 0; ii < seg_info->nb_pids; ii++) { |
| 1618 | int type = (seg_info->pids[ii].type >> 24) & 0x0f; |
| 1619 | if (type == DVR_STREAM_TYPE_VIDEO || |
| 1620 | type == DVR_STREAM_TYPE_AUDIO || |
| 1621 | type == DVR_STREAM_TYPE_AD) { |
| 1622 | DVR_WRAPPER_DEBUG(1, "success to get seg av info \n"); |
| 1623 | DVR_WRAPPER_DEBUG(1, "success to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info->pids[ii].type >> 24)&0x0f,seg_info->pids[ii].pid, |
| 1624 | DVR_STREAM_TYPE_VIDEO, |
| 1625 | DVR_STREAM_TYPE_AUDIO, |
| 1626 | DVR_STREAM_TYPE_AD); |
| 1627 | has_av = 1; |
| 1628 | //break; |
| 1629 | } else { |
| 1630 | DVR_WRAPPER_DEBUG(1, "error to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info->pids[ii].type >> 24)&0x0f,seg_info->pids[ii].pid, |
| 1631 | DVR_STREAM_TYPE_VIDEO, |
| 1632 | DVR_STREAM_TYPE_AUDIO, |
| 1633 | DVR_STREAM_TYPE_AD); |
| 1634 | } |
| 1635 | } |
| 1636 | if (has_av == 0) { |
| 1637 | DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n"); |
| 1638 | continue; |
| 1639 | } else { |
| 1640 | DVR_WRAPPER_DEBUG(1, "success to get seg av info \n"); |
| 1641 | } |
| 1642 | flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS; |
| 1643 | error = wrapper_addPlaybackSegment(ctx, seg_info, p_pids, flags); |
| 1644 | if (error) |
| 1645 | break; |
| 1646 | |
| 1647 | /*copy the 1st segment*/ |
| 1648 | if (got_1st_seg == 0) { |
| 1649 | seg_info_1st = *seg_info; |
| 1650 | got_1st_seg = 1; |
| 1651 | } |
hualing chen | 92f3a14 | 2020-07-08 20:59:33 +0800 | [diff] [blame] | 1652 | } |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1653 | //free list |
| 1654 | DVR_RecordSegmentInfo_t *segment = NULL; |
| 1655 | DVR_RecordSegmentInfo_t *segment_tmp = NULL; |
| 1656 | list_for_each_entry_safe(segment, segment_tmp, &info_list, head) |
| 1657 | { |
| 1658 | if (segment) { |
| 1659 | list_del(&segment->head); |
| 1660 | free(segment); |
| 1661 | } |
| 1662 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1663 | } |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 1664 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1665 | free(p_segment_ids); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1666 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1667 | /* return if no segment or fail to add */ |
| 1668 | if (!error && got_1st_seg) { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1669 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1670 | /*copy the obsolete infomation, must for timeshifting*/ |
| 1671 | if (ctx->playback.param_open.is_timeshift && ctx_record) { |
| 1672 | ctx->playback.obsolete = ctx_record->record.obsolete; |
| 1673 | } |
| 1674 | |
| 1675 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) (%d) segments added\n", ctx->sn, i); |
| 1676 | |
| 1677 | ctx->playback.reach_end = DVR_FALSE; |
| 1678 | if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE) |
| 1679 | ctx->playback.speed = 0.0f; |
| 1680 | else |
| 1681 | ctx->playback.speed = 100.0f; |
| 1682 | |
| 1683 | ctx->playback.pids_req = *p_pids; |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 1684 | //calualte segment id and pos |
| 1685 | if (dvr_playback_check_limit(ctx->playback.player)) { |
| 1686 | pthread_mutex_unlock(&ctx->lock); |
| 1687 | dvr_wrapper_seek_playback(playback, 0); |
| 1688 | pthread_mutex_lock(&ctx->lock); |
| 1689 | error = dvr_playback_start(ctx->playback.player, flags); |
| 1690 | } else { |
| 1691 | error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0); |
| 1692 | error = dvr_playback_start(ctx->playback.player, flags); |
| 1693 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n", |
| 1694 | ctx->sn, seg_info_1st.id, error); |
| 1695 | } |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1696 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)\n", ctx->sn, error); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1697 | } |
| 1698 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1699 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1700 | if (ctx->playback.param_open.is_timeshift) { |
| 1701 | /*unlock the recorder locked above*/ |
| 1702 | if (ctx_record && ctx_valid(ctx_record)) { |
| 1703 | pthread_mutex_unlock(&ctx_record->lock); |
| 1704 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n", |
| 1705 | ctx->sn, ctx_record->sn); |
| 1706 | } |
| 1707 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1708 | pthread_mutex_unlock(&ctx->lock); |
| 1709 | |
| 1710 | return error; |
| 1711 | } |
| 1712 | |
| 1713 | int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback) |
| 1714 | { |
| 1715 | DVR_WrapperCtx_t *ctx; |
| 1716 | int error; |
| 1717 | |
| 1718 | DVR_RETURN_IF_FALSE(playback); |
| 1719 | |
| 1720 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1721 | DVR_RETURN_IF_FALSE(ctx); |
| 1722 | |
| 1723 | pthread_mutex_lock(&ctx->lock); |
| 1724 | DVR_WRAPPER_DEBUG(1, "stop playback(sn:%ld) ...\n", ctx->sn); |
| 1725 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1726 | |
| 1727 | error = dvr_playback_stop(ctx->playback.player, DVR_TRUE); |
| 1728 | |
| 1729 | { |
| 1730 | /*remove all segments*/ |
| 1731 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 1732 | |
| 1733 | list_for_each_entry(pseg, &ctx->segments, head) { |
| 1734 | error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id); |
| 1735 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n", |
| 1736 | ctx->sn, pseg->playback_info.segment_id, error); |
| 1737 | } |
| 1738 | ctx_freeSegments(ctx); |
| 1739 | } |
| 1740 | |
| 1741 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) stopped (%d)\n", ctx->sn, error); |
| 1742 | pthread_mutex_unlock(&ctx->lock); |
| 1743 | |
| 1744 | return error; |
| 1745 | } |
| 1746 | |
| 1747 | int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback) |
| 1748 | { |
| 1749 | DVR_WrapperCtx_t *ctx; |
| 1750 | int error; |
| 1751 | |
| 1752 | DVR_RETURN_IF_FALSE(playback); |
| 1753 | |
| 1754 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1755 | DVR_RETURN_IF_FALSE(ctx); |
| 1756 | |
| 1757 | pthread_mutex_lock(&ctx->lock); |
| 1758 | DVR_WRAPPER_DEBUG(1, "pause playback(sn:%ld) ...\n", ctx->sn); |
| 1759 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
hualing chen | 36e0dfd | 2020-05-02 16:33:06 +0800 | [diff] [blame] | 1760 | //clear end event |
Zhiqiang Han | b723cdb | 2020-05-09 11:10:29 +0800 | [diff] [blame] | 1761 | if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END) |
hualing chen | 36e0dfd | 2020-05-02 16:33:06 +0800 | [diff] [blame] | 1762 | ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1763 | |
| 1764 | error = dvr_playback_pause(ctx->playback.player, DVR_FALSE); |
| 1765 | |
Zhiqiang Han | 3eb75f9 | 2020-04-08 10:07:55 +0800 | [diff] [blame] | 1766 | ctx->playback.speed = 0.0f; |
| 1767 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1768 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) paused (%d)\n", ctx->sn, error); |
| 1769 | pthread_mutex_unlock(&ctx->lock); |
| 1770 | |
| 1771 | return error; |
| 1772 | } |
| 1773 | |
| 1774 | int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback) |
| 1775 | { |
| 1776 | DVR_WrapperCtx_t *ctx; |
| 1777 | int error; |
| 1778 | |
| 1779 | DVR_RETURN_IF_FALSE(playback); |
| 1780 | |
| 1781 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1782 | DVR_RETURN_IF_FALSE(ctx); |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 1783 | //if set limit.we need check if seek to valid data when resume |
| 1784 | uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time; |
| 1785 | if (dvr_playback_check_limit(ctx->playback.player)) { |
| 1786 | int expired = dvr_playback_calculate_expiredlen(ctx->playback.player); |
| 1787 | if (expired > time_offset) { |
| 1788 | DVR_WRAPPER_DEBUG(1, "seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n", |
| 1789 | ctx->sn, time_offset, expired); |
| 1790 | time_offset = expired; |
| 1791 | dvr_wrapper_seek_playback(playback, time_offset); |
| 1792 | } |
| 1793 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1794 | pthread_mutex_lock(&ctx->lock); |
| 1795 | DVR_WRAPPER_DEBUG(1, "resume playback(sn:%ld) ...\n", ctx->sn); |
| 1796 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1797 | |
| 1798 | error = dvr_playback_resume(ctx->playback.player); |
Zhiqiang Han | 3eb75f9 | 2020-04-08 10:07:55 +0800 | [diff] [blame] | 1799 | ctx->playback.speed = 100.0f; |
| 1800 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1801 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) resumed (%d)\n", ctx->sn, error); |
| 1802 | pthread_mutex_unlock(&ctx->lock); |
| 1803 | |
| 1804 | return error; |
| 1805 | } |
| 1806 | |
| 1807 | int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed) |
| 1808 | { |
| 1809 | DVR_WrapperCtx_t *ctx; |
| 1810 | int error; |
| 1811 | DVR_PlaybackSpeed_t dvr_speed = { |
| 1812 | .speed = { speed }, |
| 1813 | .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD |
| 1814 | }; |
| 1815 | |
| 1816 | DVR_RETURN_IF_FALSE(playback); |
| 1817 | |
| 1818 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1819 | DVR_RETURN_IF_FALSE(ctx); |
| 1820 | |
| 1821 | pthread_mutex_lock(&ctx->lock); |
hualing chen | c70a8df | 2020-05-12 19:23:11 +0800 | [diff] [blame] | 1822 | DVR_WRAPPER_DEBUG(1, "speed playback(sn:%ld) (x%f) .(x%f)..\n", ctx->sn, speed, ctx->playback.speed); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1823 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1824 | |
| 1825 | error = dvr_playback_set_speed(ctx->playback.player, dvr_speed); |
| 1826 | |
Zhiqiang Han | 3eb75f9 | 2020-04-08 10:07:55 +0800 | [diff] [blame] | 1827 | if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f |
| 1828 | && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN |
| 1829 | && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) { |
| 1830 | DVR_WRAPPER_DEBUG(1, "x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed); |
| 1831 | error = dvr_playback_resume(ctx->playback.player); |
| 1832 | } else if (ctx->playback.speed == 0.0f |
| 1833 | && speed != 0.0f |
| 1834 | && speed != 100.0f) { |
| 1835 | /*libdvr do not support pause with speed=0, will not be here*/ |
| 1836 | DVR_WRAPPER_DEBUG(1, "x%f -> x%f, do resume first\n", ctx->playback.speed, speed); |
| 1837 | error = dvr_playback_resume(ctx->playback.player); |
| 1838 | } |
| 1839 | |
| 1840 | ctx->playback.speed = speed; |
| 1841 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1842 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) speeded(x%f) (%d)\n", |
| 1843 | ctx->sn, speed, error); |
| 1844 | pthread_mutex_unlock(&ctx->lock); |
| 1845 | |
| 1846 | return error; |
| 1847 | } |
| 1848 | |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 1849 | int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit) |
| 1850 | { |
| 1851 | DVR_WrapperCtx_t *ctx; |
| 1852 | int error; |
| 1853 | |
| 1854 | DVR_RETURN_IF_FALSE(playback); |
| 1855 | |
| 1856 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1857 | DVR_RETURN_IF_FALSE(ctx); |
| 1858 | |
| 1859 | pthread_mutex_lock(&ctx->lock); |
| 1860 | |
| 1861 | DVR_WRAPPER_DEBUG(1, "setlimit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit); |
| 1862 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1863 | |
| 1864 | error = dvr_playback_setlimit(ctx->playback.player, time, limit); |
| 1865 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) setlimit(time:%lld limit:%d) ...\n", ctx->sn, time, limit); |
| 1866 | |
| 1867 | pthread_mutex_unlock(&ctx->lock); |
| 1868 | |
| 1869 | return error; |
| 1870 | } |
| 1871 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1872 | int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset) |
| 1873 | { |
| 1874 | DVR_WrapperCtx_t *ctx; |
| 1875 | int error; |
| 1876 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 1877 | uint64_t segment_id; |
| 1878 | uint32_t off; |
| 1879 | uint64_t last_segment_id; |
| 1880 | uint32_t pre_off; |
| 1881 | |
| 1882 | DVR_RETURN_IF_FALSE(playback); |
| 1883 | |
| 1884 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1885 | DVR_RETURN_IF_FALSE(ctx); |
| 1886 | |
| 1887 | pthread_mutex_lock(&ctx->lock); |
| 1888 | |
| 1889 | DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset); |
| 1890 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1891 | |
| 1892 | off = 0; |
| 1893 | segment_id = 0; |
| 1894 | pre_off = 0; |
| 1895 | last_segment_id = 0; |
| 1896 | |
hualing chen | 03fd494 | 2021-07-15 15:56:41 +0800 | [diff] [blame] | 1897 | //if set limit info we need check ts data is |
| 1898 | //expired when seek |
| 1899 | if (dvr_playback_check_limit(ctx->playback.player)) { |
| 1900 | int expired = dvr_playback_calculate_expiredlen(ctx->playback.player); |
| 1901 | if (expired > time_offset) { |
| 1902 | DVR_WRAPPER_DEBUG(1, "seek reset offset playback(sn:%ld) (off:%d expired:%d)\n", |
| 1903 | ctx->sn, time_offset, expired); |
| 1904 | time_offset = expired; |
| 1905 | } |
| 1906 | } |
| 1907 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1908 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 1909 | segment_id = pseg->seg_info.id; |
| 1910 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1911 | if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset) |
| 1912 | break; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1913 | |
| 1914 | last_segment_id = pseg->seg_info.id; |
| 1915 | pre_off += pseg->seg_info.duration; |
| 1916 | } |
| 1917 | |
| 1918 | if (last_segment_id == segment_id) { |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1919 | /*1.only one seg with id:0, 2.offset exceeds the total duration*/ |
| 1920 | off = time_offset; |
| 1921 | } else if (ctx->playback.obsolete.time >= time_offset) { |
| 1922 | off = 0; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1923 | } else { |
hualing chen | da76fc5 | 2020-05-28 14:56:42 +0800 | [diff] [blame] | 1924 | off = time_offset - pre_off - ctx->playback.obsolete.time; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1925 | } |
| 1926 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 1927 | DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (seg:%lld, off:%d)\n", |
| 1928 | ctx->sn, segment_id, off); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 1929 | error = dvr_playback_seek(ctx->playback.player, segment_id, off); |
| 1930 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error); |
| 1931 | |
| 1932 | pthread_mutex_unlock(&ctx->lock); |
| 1933 | |
| 1934 | return error; |
| 1935 | } |
| 1936 | |
| 1937 | int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids) |
| 1938 | { |
| 1939 | DVR_WrapperCtx_t *ctx; |
| 1940 | int error; |
| 1941 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 1942 | |
| 1943 | DVR_RETURN_IF_FALSE(playback); |
| 1944 | DVR_RETURN_IF_FALSE(p_pids); |
| 1945 | |
| 1946 | ctx = ctx_getPlayback((unsigned long)playback); |
| 1947 | DVR_RETURN_IF_FALSE(ctx); |
| 1948 | |
| 1949 | pthread_mutex_lock(&ctx->lock); |
| 1950 | |
| 1951 | DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n", |
| 1952 | ctx->sn, |
| 1953 | p_pids->video.pid, p_pids->video.format, |
| 1954 | p_pids->audio.pid, p_pids->audio.format); |
| 1955 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 1956 | |
| 1957 | ctx->playback.pids_req = *p_pids; |
| 1958 | |
| 1959 | error = 0; |
| 1960 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 1961 | /*should update the whole list of segments*/ |
| 1962 | /*if (pseg->seg_info.id == ctx->current_segment_id)*/ { |
| 1963 | /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ { |
| 1964 | /*check udpate for pids*/ |
| 1965 | if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) { |
| 1966 | pseg->playback_info.pids = *p_pids; |
| 1967 | error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids); |
| 1968 | if (error) { |
| 1969 | DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n", |
| 1970 | ctx->sn, pseg->seg_info.id, error); |
| 1971 | /*do not break, let list updated*/ |
| 1972 | } |
| 1973 | } |
| 1974 | } |
| 1975 | /*break;*/ |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n", |
| 1980 | ctx->sn, |
| 1981 | p_pids->video.pid, p_pids->video.format, |
| 1982 | p_pids->audio.pid, p_pids->audio.format, |
| 1983 | error); |
| 1984 | |
| 1985 | pthread_mutex_unlock(&ctx->lock); |
| 1986 | |
| 1987 | return error; |
| 1988 | } |
| 1989 | |
hualing chen | a5f0322 | 2021-12-02 11:22:35 +0800 | [diff] [blame] | 1990 | int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids) |
| 1991 | { |
| 1992 | DVR_WrapperCtx_t *ctx; |
| 1993 | int error; |
| 1994 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 1995 | |
| 1996 | DVR_RETURN_IF_FALSE(playback); |
| 1997 | DVR_RETURN_IF_FALSE(p_pids); |
| 1998 | |
| 1999 | ctx = ctx_getPlayback((unsigned long)playback); |
| 2000 | DVR_RETURN_IF_FALSE(ctx); |
| 2001 | |
| 2002 | pthread_mutex_lock(&ctx->lock); |
| 2003 | |
| 2004 | DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n", |
| 2005 | ctx->sn, |
| 2006 | p_pids->video.pid, p_pids->video.format, |
| 2007 | p_pids->audio.pid, p_pids->audio.format); |
| 2008 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 2009 | |
| 2010 | ctx->playback.pids_req = *p_pids; |
| 2011 | |
| 2012 | error = 0; |
| 2013 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 2014 | /*should update the whole list of segments*/ |
| 2015 | /*if (pseg->seg_info.id == ctx->current_segment_id)*/ { |
| 2016 | /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ { |
| 2017 | /*check udpate for pids*/ |
| 2018 | if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) { |
| 2019 | pseg->playback_info.pids = *p_pids; |
| 2020 | error = dvr_playback_only_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids); |
| 2021 | if (error) { |
| 2022 | DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n", |
| 2023 | ctx->sn, pseg->seg_info.id, error); |
| 2024 | /*do not break, let list updated*/ |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | /*break;*/ |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n", |
| 2033 | ctx->sn, |
| 2034 | p_pids->video.pid, p_pids->video.format, |
| 2035 | p_pids->audio.pid, p_pids->audio.format, |
| 2036 | error); |
| 2037 | |
| 2038 | pthread_mutex_unlock(&ctx->lock); |
| 2039 | |
| 2040 | return error; |
| 2041 | } |
| 2042 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2043 | int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status) |
| 2044 | { |
| 2045 | DVR_WrapperCtx_t *ctx; |
| 2046 | DVR_WrapperPlaybackStatus_t s; |
| 2047 | DVR_PlaybackStatus_t play_status; |
| 2048 | int error; |
| 2049 | |
| 2050 | DVR_RETURN_IF_FALSE(playback); |
| 2051 | DVR_RETURN_IF_FALSE(status); |
| 2052 | |
| 2053 | ctx = ctx_getPlayback((unsigned long)playback); |
| 2054 | DVR_RETURN_IF_FALSE(ctx); |
| 2055 | |
| 2056 | pthread_mutex_lock(&ctx->lock); |
| 2057 | |
| 2058 | DVR_WRAPPER_DEBUG(1, "get playback(sn:%ld) status ...\n", ctx->sn); |
| 2059 | DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock); |
| 2060 | |
| 2061 | error = dvr_playback_get_status(ctx->playback.player, &play_status); |
| 2062 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) get status (%d)\n", ctx->sn, error); |
| 2063 | |
| 2064 | ctx->playback.seg_status = play_status; |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2065 | error = process_generatePlaybackStatus(ctx, &s); |
| 2066 | |
hualing chen | b5cd42e | 2020-04-15 17:03:34 +0800 | [diff] [blame] | 2067 | if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) { |
| 2068 | //reach end need set full time to cur.so app can exist playback. |
| 2069 | DVR_WRAPPER_DEBUG(1, "set cur time to full time, reach end occur"); |
| 2070 | s.info_cur.time = s.info_full.time; |
| 2071 | } |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2072 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n", |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2073 | ctx->sn, |
| 2074 | s.state, |
| 2075 | s.info_cur.time, |
| 2076 | s.info_full.time, |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2077 | s.info_obsolete.time, |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2078 | error); |
| 2079 | |
| 2080 | *status = s; |
| 2081 | |
| 2082 | pthread_mutex_unlock(&ctx->lock); |
| 2083 | |
| 2084 | return error; |
| 2085 | } |
| 2086 | |
hualing chen | 266b950 | 2020-04-04 17:39:39 +0800 | [diff] [blame] | 2087 | int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len) |
| 2088 | { |
| 2089 | DVR_WrapperCtx_t *ctx; |
| 2090 | int error; |
| 2091 | |
| 2092 | DVR_RETURN_IF_FALSE(playback); |
| 2093 | DVR_RETURN_IF_FALSE(p_secure_buf); |
| 2094 | |
| 2095 | ctx = ctx_getPlayback((unsigned long)playback); |
| 2096 | DVR_RETURN_IF_FALSE(ctx); |
| 2097 | |
| 2098 | pthread_mutex_lock(&ctx->lock); |
| 2099 | error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len); |
| 2100 | pthread_mutex_unlock(&ctx->lock); |
| 2101 | return error; |
| 2102 | } |
| 2103 | |
| 2104 | int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata) |
| 2105 | { |
| 2106 | DVR_WrapperCtx_t *ctx; |
| 2107 | int error; |
| 2108 | |
| 2109 | DVR_RETURN_IF_FALSE(playback); |
| 2110 | DVR_RETURN_IF_FALSE(func); |
| 2111 | |
| 2112 | ctx = ctx_getPlayback((unsigned long)playback); |
| 2113 | DVR_RETURN_IF_FALSE(ctx); |
| 2114 | |
| 2115 | pthread_mutex_lock(&ctx->lock); |
| 2116 | error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata); |
| 2117 | pthread_mutex_unlock(&ctx->lock); |
| 2118 | return error; |
| 2119 | } |
| 2120 | |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2121 | int dvr_wrapper_segment_del_by_location (const char *location) |
| 2122 | { |
| 2123 | char fpath[DVR_MAX_LOCATION_SIZE]; |
| 2124 | |
| 2125 | DVR_RETURN_IF_FALSE(location); |
| 2126 | |
| 2127 | /*del the stats file*/ |
| 2128 | sprintf(fpath, "%s.stats", location); |
| 2129 | unlink(fpath); |
| 2130 | |
| 2131 | return dvr_segment_del_by_location(location); |
| 2132 | } |
| 2133 | |
| 2134 | int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info) |
| 2135 | { |
| 2136 | FILE *fp; |
| 2137 | char fpath[DVR_MAX_LOCATION_SIZE]; |
| 2138 | |
| 2139 | DVR_RETURN_IF_FALSE(location); |
| 2140 | DVR_RETURN_IF_FALSE(p_info); |
| 2141 | |
| 2142 | if (p_info) |
| 2143 | memset(p_info, 0, sizeof(p_info[0])); |
| 2144 | |
| 2145 | memset(fpath, 0, sizeof(fpath)); |
| 2146 | sprintf(fpath, "%s.stats", location); |
| 2147 | |
| 2148 | /*stats file exists*/ |
| 2149 | if ((fp = fopen(fpath, "r"))) { |
| 2150 | char buf[256]; |
| 2151 | |
| 2152 | if (fgets(buf, sizeof(buf), fp) != NULL |
| 2153 | && (sscanf(buf, ":%llu:%lu:%u", |
| 2154 | &p_info->size, |
| 2155 | &p_info->time, |
| 2156 | &p_info->pkts) == 3)) { |
| 2157 | fclose(fp); |
| 2158 | DVR_WRAPPER_DEBUG(1, "rec(%s) t/s/p:(%lu/%llu/%u)\n", location, p_info->time, p_info->size, p_info->pkts); |
| 2159 | return DVR_SUCCESS; |
| 2160 | } |
Zhiqiang Han | b978592 | 2021-11-26 18:47:39 +0800 | [diff] [blame] | 2161 | fclose(fp); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | /*fallback, slow on mass files*/ |
Zhiqiang Han | b978592 | 2021-11-26 18:47:39 +0800 | [diff] [blame] | 2165 | DVR_WRAPPER_DEBUG(1, "rec '%s.stats' invalid.\n", location); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2166 | |
| 2167 | int error; |
| 2168 | uint32_t n_ids; |
| 2169 | uint64_t *p_ids; |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2170 | |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2171 | error = dvr_segment_get_list(location, &n_ids, &p_ids); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2172 | |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2173 | if (!error) { |
| 2174 | int i; |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2175 | struct list_head info_list; /**< segment list head*/ |
| 2176 | INIT_LIST_HEAD(&info_list); |
| 2177 | |
| 2178 | //we need free info list buf when we used end. |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2179 | error = dvr_segment_get_allInfo(location, &info_list); |
| 2180 | if (error == DVR_FAILURE) { |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2181 | DVR_RecordSegmentInfo_t info; |
| 2182 | |
| 2183 | memset(&info, 0, sizeof(info)); |
| 2184 | error = DVR_FAILURE; |
| 2185 | DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n", |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2186 | location, 0, error); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2187 | |
| 2188 | for (i = 0; i < n_ids; i++) { |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2189 | error = dvr_segment_get_info(location, p_ids[i], &info); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2190 | if (!error) { |
| 2191 | p_info->size += info.size; |
| 2192 | p_info->time += info.duration; |
| 2193 | p_info->pkts += info.nb_packets; |
| 2194 | } else { |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2195 | DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2196 | break; |
| 2197 | } |
| 2198 | } |
| 2199 | } else { |
| 2200 | DVR_WRAPPER_DEBUG(1, "get list segment_nb::%d",n_ids); |
| 2201 | for (i = 0; i < n_ids; i++) { |
| 2202 | |
| 2203 | DVR_RecordSegmentInfo_t *seg_info; |
| 2204 | DVR_PlaybackSegmentFlag_t flags; |
| 2205 | int found = 0; |
| 2206 | list_for_each_entry(seg_info, &info_list, head) |
| 2207 | { |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2208 | if (seg_info->id == p_ids[i]) { |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2209 | found = 1; |
| 2210 | break; |
| 2211 | } |
| 2212 | } |
| 2213 | if (!found) { |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2214 | DVR_WRAPPER_DEBUG(1, "get segment info::%d [%d]n_ids[%d]error", i, p_ids[i], n_ids); |
| 2215 | if (p_ids[i] == n_ids - 1) { |
| 2216 | DVR_RecordSegmentInfo_t info; |
| 2217 | DVR_WRAPPER_DEBUG(1, "get last segment info::%d [%d]n_ids[%d] from subfile", i, p_ids[i], n_ids); |
| 2218 | error = dvr_segment_get_info(location, p_ids[i], &info); |
| 2219 | if (!error) { |
| 2220 | p_info->size += info.size; |
| 2221 | p_info->time += info.duration; |
| 2222 | p_info->pkts += info.nb_packets; |
| 2223 | } else { |
| 2224 | DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]); |
| 2225 | break; |
| 2226 | } |
| 2227 | } |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2228 | continue; |
| 2229 | } |
| 2230 | |
| 2231 | if (!error) { |
| 2232 | p_info->size += seg_info->size; |
| 2233 | p_info->time += seg_info->duration; |
| 2234 | p_info->pkts += seg_info->nb_packets; |
| 2235 | } else { |
hualing chen | 8aed958 | 2021-12-24 17:59:56 +0800 | [diff] [blame] | 2236 | DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]); |
hualing chen | b9a0292 | 2021-12-14 11:29:47 +0800 | [diff] [blame] | 2237 | break; |
| 2238 | } |
| 2239 | } |
| 2240 | //free list |
| 2241 | DVR_RecordSegmentInfo_t *segment = NULL; |
| 2242 | DVR_RecordSegmentInfo_t *segment_tmp = NULL; |
| 2243 | list_for_each_entry_safe(segment, segment_tmp, &info_list, head) |
| 2244 | { |
| 2245 | if (segment) { |
| 2246 | list_del(&segment->head); |
| 2247 | free(segment); |
| 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | free(p_ids); |
Zhiqiang Han | b978592 | 2021-11-26 18:47:39 +0800 | [diff] [blame] | 2252 | } else { |
| 2253 | n_ids = 0; |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2254 | } |
Zhiqiang Han | b978592 | 2021-11-26 18:47:39 +0800 | [diff] [blame] | 2255 | DVR_WRAPPER_DEBUG(1, "rec(%s)... t/s/p:(%lu/%llu/%u) segs(%u) error(%d)\n", location, p_info->time, p_info->size, p_info->pkts, n_ids, error); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2256 | |
| 2257 | return (error)? DVR_FAILURE : DVR_SUCCESS; |
| 2258 | } |
| 2259 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2260 | static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata) |
| 2261 | { |
| 2262 | DVR_WrapperEventCtx_t evt; |
| 2263 | |
| 2264 | DVR_RETURN_IF_FALSE(userdata); |
| 2265 | |
| 2266 | evt.sn = (unsigned long)userdata; |
| 2267 | evt.type = W_REC; |
| 2268 | evt.record.event = event; |
| 2269 | evt.record.status = *(DVR_RecordStatus_t *)params; |
| 2270 | DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event); |
| 2271 | return ctx_addRecordEvent(&evt); |
| 2272 | } |
| 2273 | |
| 2274 | static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata) |
| 2275 | { |
| 2276 | DVR_WrapperEventCtx_t evt; |
| 2277 | |
| 2278 | DVR_RETURN_IF_FALSE(userdata); |
| 2279 | |
| 2280 | evt.sn = (unsigned long)userdata; |
| 2281 | evt.type = W_PLAYBACK; |
| 2282 | evt.playback.event = event; |
| 2283 | evt.playback.status = *(DVR_Play_Notify_t *)params; |
| 2284 | DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event); |
| 2285 | return ctx_addPlaybackEvent(&evt); |
| 2286 | } |
| 2287 | |
| 2288 | static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status) |
| 2289 | { |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2290 | DVR_WRAPPER_DEBUG(1, "notify(sn:%ld) evt(0x%x) statistic:time/size/pkts(%ld/%lld/%u) obsl:(%ld/%llu/%u)\n", |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2291 | ctx->sn, |
| 2292 | evt, |
| 2293 | status->info.time, |
| 2294 | status->info.size, |
| 2295 | status->info.pkts, |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2296 | status->info_obsolete.time, |
| 2297 | status->info_obsolete.size, |
| 2298 | status->info_obsolete.pkts); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2299 | |
| 2300 | if (ctx->record.event_fn) |
| 2301 | return ctx->record.event_fn(evt, status, ctx->record.event_userdata); |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2305 | static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status) |
| 2306 | { |
| 2307 | FILE *fp; |
| 2308 | char fpath[DVR_MAX_LOCATION_SIZE]; |
| 2309 | |
| 2310 | DVR_RETURN_IF_FALSE(location); |
| 2311 | DVR_RETURN_IF_FALSE(p_status); |
| 2312 | |
| 2313 | sprintf(fpath, "%s.stats", location); |
| 2314 | |
| 2315 | /*stats file*/ |
| 2316 | if ((fp = fopen(fpath, "w"))) { |
| 2317 | char buf[256]; |
| 2318 | snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n", |
| 2319 | p_status->info.size - p_status->info_obsolete.size, |
| 2320 | p_status->info.time - p_status->info_obsolete.time, |
| 2321 | p_status->info.pkts - p_status->info_obsolete.pkts); |
| 2322 | fputs(buf, fp); |
| 2323 | fclose(fp); |
| 2324 | return DVR_SUCCESS; |
| 2325 | } |
| 2326 | |
| 2327 | return DVR_FAILURE; |
| 2328 | } |
| 2329 | |
| 2330 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2331 | static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx) |
| 2332 | { |
| 2333 | DVR_RecordStartParams_t param; |
| 2334 | DVR_RecordSegmentInfo_t seg_info; |
| 2335 | int i; |
| 2336 | int error; |
| 2337 | |
| 2338 | memcpy(¶m, &ctx->record.param_update, sizeof(param)); |
| 2339 | memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment)); |
| 2340 | ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++; |
| 2341 | for (i = 0; i < param.segment.nb_pids; i++) { |
| 2342 | if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) { |
| 2343 | ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i]; |
| 2344 | ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP; |
| 2345 | ctx->record.param_update.segment.nb_pids++; |
| 2346 | } |
| 2347 | } |
| 2348 | error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info); |
| 2349 | { |
| 2350 | DVR_RecordSegmentInfo_t new_seg_info = |
| 2351 | { .id = ctx->record.param_update.segment.segment_id, }; |
| 2352 | wrapper_updateRecordSegment(ctx, &seg_info, U_ALL); |
| 2353 | wrapper_addRecordSegment(ctx, &new_seg_info); |
| 2354 | } |
| 2355 | |
Zhiqiang Han | d977e97 | 2020-05-11 11:30:47 +0800 | [diff] [blame] | 2356 | DVR_WRAPPER_DEBUG(1, "record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2357 | return error; |
| 2358 | } |
| 2359 | |
| 2360 | static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg) |
| 2361 | { |
| 2362 | return wrapper_removeRecordSegment(ctx, pseg); |
| 2363 | } |
| 2364 | |
| 2365 | /*should run periodically to update the current status*/ |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2366 | static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status) |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2367 | { |
| 2368 | /*the current seg is not covered in the statistics*/ |
| 2369 | DVR_WrapperRecordSegmentInfo_t *pseg; |
| 2370 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2371 | /*re-calculate the all segments*/ |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2372 | memset(&ctx->record.status, 0, sizeof(ctx->record.status)); |
| 2373 | |
| 2374 | ctx->record.status.state = ctx->record.seg_status.state; |
| 2375 | ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids; |
| 2376 | memcpy(ctx->record.status.pids.pids, |
| 2377 | ctx->record.seg_status.info.pids, |
| 2378 | sizeof(ctx->record.status.pids.pids)); |
| 2379 | ctx->current_segment_id = ctx->record.seg_status.info.id; |
| 2380 | |
| 2381 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 2382 | if (pseg->info.id != ctx->record.seg_status.info.id) { |
| 2383 | ctx->record.status.info.time += pseg->info.duration; |
| 2384 | ctx->record.status.info.size += pseg->info.size; |
| 2385 | ctx->record.status.info.pkts += pseg->info.nb_packets; |
| 2386 | } |
| 2387 | } |
| 2388 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2389 | ctx->record.status.info_obsolete = ctx->record.obsolete; |
| 2390 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2391 | wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL); |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2392 | |
| 2393 | if (status) { |
| 2394 | *status = ctx->record.status; |
| 2395 | status->info.time += ctx->record.seg_status.info.duration; |
| 2396 | status->info.size += ctx->record.seg_status.info.size; |
| 2397 | status->info.pkts += ctx->record.seg_status.info.nb_packets; |
| 2398 | } |
| 2399 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2400 | return DVR_SUCCESS; |
| 2401 | } |
| 2402 | |
| 2403 | |
| 2404 | static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx) |
| 2405 | { |
| 2406 | DVR_WrapperRecordStatus_t status; |
| 2407 | |
| 2408 | memset(&status, 0, sizeof(status)); |
| 2409 | |
| 2410 | DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d)\n", |
| 2411 | evt->sn, evt->record.event, evt->record.status.state); |
hualing chen | d3b55ab | 2021-05-06 09:56:27 +0800 | [diff] [blame] | 2412 | if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) { |
| 2413 | DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) cur id:0x%x (event id:%d)\n", |
Gong Ke | 2a0ebbe | 2021-05-25 15:22:50 +0800 | [diff] [blame] | 2414 | evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id); |
hualing chen | d3b55ab | 2021-05-06 09:56:27 +0800 | [diff] [blame] | 2415 | return 0; |
| 2416 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2417 | switch (evt->record.event) |
| 2418 | { |
| 2419 | case DVR_RECORD_EVENT_STATUS: |
| 2420 | { |
| 2421 | switch (evt->record.status.state) |
| 2422 | { |
| 2423 | case DVR_RECORD_STATE_OPENED: |
| 2424 | case DVR_RECORD_STATE_CLOSED: |
| 2425 | { |
| 2426 | ctx->record.seg_status = evt->record.status; |
| 2427 | |
| 2428 | status.state = evt->record.status.state; |
| 2429 | process_notifyRecord(ctx, evt->record.event, &status); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2430 | wrapper_saveRecordStatistics(ctx->record.param_open.location, &status); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2431 | } break; |
| 2432 | case DVR_RECORD_STATE_STARTED: |
| 2433 | { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2434 | ctx->record.seg_status = evt->record.status; |
| 2435 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2436 | process_generateRecordStatus(ctx, &status); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2437 | process_notifyRecord(ctx, evt->record.event, &status); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2438 | wrapper_saveRecordStatistics(ctx->record.param_open.location, &status); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2439 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2440 | /*restart to next segment*/ |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2441 | if (ctx->record.param_open.segment_size |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2442 | && evt->record.status.info.size >= ctx->record.param_open.segment_size) { |
| 2443 | DVR_WRAPPER_DEBUG(1, "start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n", |
| 2444 | ctx->sn, |
| 2445 | evt->record.status.info.size, |
| 2446 | ctx->record.param_open.segment_size); |
Zhiqiang Han | d977e97 | 2020-05-11 11:30:47 +0800 | [diff] [blame] | 2447 | if (record_startNextSegment(ctx) != DVR_SUCCESS) { |
| 2448 | /*should notify the recording's stop*/ |
| 2449 | int error = dvr_record_close(ctx->record.recorder); |
| 2450 | DVR_WRAPPER_DEBUG(1, "stop record(%lu)=%d, failed to start new segment for recording.", |
| 2451 | ctx->sn, error); |
| 2452 | status.state = DVR_RECORD_STATE_CLOSED; |
| 2453 | process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2454 | wrapper_saveRecordStatistics(ctx->record.param_open.location, &status); |
Zhiqiang Han | d977e97 | 2020-05-11 11:30:47 +0800 | [diff] [blame] | 2455 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2456 | } |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2457 | |
| 2458 | if (ctx->record.param_open.is_timeshift |
| 2459 | && ctx->record.param_open.max_time |
| 2460 | && status.info.time >= ctx->record.param_open.max_time) { |
| 2461 | DVR_WrapperRecordSegmentInfo_t *pseg; |
| 2462 | |
| 2463 | /*as the player do not support null playlist, |
| 2464 | there must be one segment existed at any time, |
| 2465 | we have to keep two segments before remove one*/ |
| 2466 | pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head); |
| 2467 | if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) { |
| 2468 | /*only one segment, waiting for more*/ |
| 2469 | DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n", |
| 2470 | status.info.size, |
| 2471 | ctx->record.param_open.max_time, |
| 2472 | ctx->record.param_open.segment_size); |
| 2473 | } else { |
| 2474 | /*timeshifting, remove the 1st segment and notify the player*/ |
| 2475 | record_removeSegment(ctx, pseg); |
| 2476 | |
| 2477 | process_generateRecordStatus(ctx, &status); |
| 2478 | process_notifyRecord(ctx, evt->record.event, &status); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2479 | wrapper_saveRecordStatistics(ctx->record.param_open.location, &status); |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | if (ctx->record.param_open.is_timeshift |
| 2484 | && ctx->record.param_open.max_size |
| 2485 | && status.info.size >= ctx->record.param_open.max_size) { |
| 2486 | DVR_WrapperRecordSegmentInfo_t *pseg; |
| 2487 | |
| 2488 | /*as the player do not support null playlist, |
| 2489 | there must be one segment existed at any time, |
| 2490 | we have to keep two segments before remove one*/ |
| 2491 | pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head); |
| 2492 | if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) { |
| 2493 | /*only one segment, waiting for more*/ |
| 2494 | DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of record < max size of segment(%lld)\n", |
| 2495 | status.info.size, |
| 2496 | ctx->record.param_open.segment_size); |
| 2497 | } else { |
| 2498 | record_removeSegment(ctx, pseg); |
| 2499 | |
| 2500 | process_generateRecordStatus(ctx, &status); |
| 2501 | process_notifyRecord(ctx, evt->record.event, &status); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2502 | wrapper_saveRecordStatistics(ctx->record.param_open.location, &status); |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2503 | } |
| 2504 | } |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2505 | } break; |
| 2506 | case DVR_RECORD_STATE_STOPPED: |
| 2507 | { |
| 2508 | ctx->record.seg_status = evt->record.status; |
| 2509 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2510 | process_generateRecordStatus(ctx, &status); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2511 | process_notifyRecord(ctx, evt->record.event, &status); |
Zhiqiang Han | 620b925 | 2021-11-09 14:23:20 +0800 | [diff] [blame] | 2512 | wrapper_saveRecordStatistics(ctx->record.param_open.location, &status); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2513 | } break; |
| 2514 | default: |
| 2515 | break; |
| 2516 | } |
| 2517 | } break; |
hualing chen | 4b7c15d | 2020-04-07 16:13:48 +0800 | [diff] [blame] | 2518 | case DVR_RECORD_EVENT_WRITE_ERROR: { |
| 2519 | ctx->record.seg_status = evt->record.status; |
| 2520 | status.state = evt->record.status.state; |
| 2521 | process_notifyRecord(ctx, evt->record.event, &status); |
| 2522 | }break; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2523 | default: |
| 2524 | break; |
| 2525 | } |
| 2526 | return DVR_SUCCESS; |
| 2527 | } |
| 2528 | |
| 2529 | static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status) |
| 2530 | { |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2531 | DVR_WRAPPER_DEBUG(1, "notify(sn:%ld) evt(0x%x) statistics:state/cur/full/obsl(%d/%ld/%ld/%ld)\n", |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2532 | ctx->sn, |
| 2533 | evt, |
| 2534 | status->state, |
| 2535 | status->info_cur.time, |
| 2536 | status->info_full.time, |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2537 | status->info_obsolete.time); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2538 | |
| 2539 | if (ctx->playback.event_fn) |
| 2540 | return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata); |
| 2541 | return 0; |
| 2542 | } |
| 2543 | |
| 2544 | /*should run periodically to update the current status*/ |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2545 | static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status) |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2546 | { |
| 2547 | /*the current seg is not covered in the statistics*/ |
| 2548 | DVR_WrapperPlaybackSegmentInfo_t *pseg; |
| 2549 | |
| 2550 | memset(&ctx->playback.status, 0, sizeof(ctx->playback.status)); |
| 2551 | ctx->playback.status.pids = ctx->playback.pids_req; |
| 2552 | |
| 2553 | ctx->playback.status.state = ctx->playback.seg_status.state; |
| 2554 | ctx->playback.status.speed = ctx->playback.seg_status.speed; |
| 2555 | ctx->playback.status.flags = ctx->playback.seg_status.flags; |
| 2556 | ctx->current_segment_id = ctx->playback.seg_status.segment_id; |
| 2557 | |
| 2558 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 2559 | if (pseg->seg_info.id == ctx->playback.seg_status.segment_id) |
| 2560 | break; |
| 2561 | ctx->playback.status.info_cur.time += pseg->seg_info.duration; |
| 2562 | ctx->playback.status.info_cur.size += pseg->seg_info.size; |
| 2563 | ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets; |
| 2564 | } |
| 2565 | list_for_each_entry_reverse(pseg, &ctx->segments, head) { |
| 2566 | ctx->playback.status.info_full.time += pseg->seg_info.duration; |
| 2567 | ctx->playback.status.info_full.size += pseg->seg_info.size; |
| 2568 | ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets; |
| 2569 | } |
| 2570 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2571 | if (status) { |
| 2572 | *status = ctx->playback.status; |
| 2573 | /*deal with current, lack size and pkts with the current*/ |
| 2574 | status->info_cur.time += ctx->playback.seg_status.time_cur; |
| 2575 | status->info_obsolete.time = ctx->playback.obsolete.time; |
| 2576 | } |
| 2577 | |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2578 | return DVR_SUCCESS; |
| 2579 | } |
| 2580 | |
| 2581 | static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx) |
| 2582 | { |
| 2583 | DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n", |
| 2584 | evt->sn, evt->playback.event, |
| 2585 | evt->playback.status.play_status.state, |
| 2586 | evt->playback.status.play_status.segment_id, |
| 2587 | evt->playback.status.play_status.time_cur, |
| 2588 | evt->playback.status.play_status.time_end); |
| 2589 | |
| 2590 | /*evt PLAYTIME will break the last logic, do not save*/ |
hualing chen | e3797f0 | 2021-01-13 14:53:28 +0800 | [diff] [blame] | 2591 | if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME |
| 2592 | && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA |
| 2593 | && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME |
| 2594 | ) |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2595 | ctx->playback.last_event = evt->playback.event; |
| 2596 | |
| 2597 | switch (evt->playback.event) |
| 2598 | { |
| 2599 | case DVR_PLAYBACK_EVENT_FIRST_FRAME: |
| 2600 | case DVR_PLAYBACK_EVENT_REACHED_END: |
| 2601 | case DVR_PLAYBACK_EVENT_TRANSITION_OK: |
| 2602 | case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME: |
hualing chen | b5cd42e | 2020-04-15 17:03:34 +0800 | [diff] [blame] | 2603 | case DVR_PLAYBACK_EVENT_ERROR: |
hualing chen | f291cf3 | 2020-06-18 10:50:30 +0800 | [diff] [blame] | 2604 | case DVR_PLAYBACK_EVENT_REACHED_BEGIN: |
hualing chen | e3797f0 | 2021-01-13 14:53:28 +0800 | [diff] [blame] | 2605 | case DVR_PLAYBACK_EVENT_NODATA: |
| 2606 | case DVR_PLAYBACK_EVENT_DATARESUME: |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2607 | { |
| 2608 | DVR_WrapperPlaybackStatus_t status; |
| 2609 | |
| 2610 | /*copy status of segment*/ |
| 2611 | ctx->playback.seg_status = evt->playback.status.play_status; |
| 2612 | |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 2613 | /*generate status of the whole playback*/ |
| 2614 | process_generatePlaybackStatus(ctx, &status); |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2615 | |
| 2616 | if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) { |
Zhiqiang Han | 3184600 | 2021-11-04 10:49:06 +0800 | [diff] [blame] | 2617 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event); |
hualing chen | b9b358a | 2021-08-17 15:06:36 +0800 | [diff] [blame] | 2618 | if (ctx->playback.param_open.is_timeshift |
| 2619 | || ctx_isPlay_recording(ctx->playback.param_open.location)) { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2620 | /*wait for more data in recording*/ |
Zhiqiang Han | 3184600 | 2021-11-04 10:49:06 +0800 | [diff] [blame] | 2621 | } |
| 2622 | /*trust the low level, make NO check. |
| 2623 | As this evt is changed to only once due to some operations(paused) in low level. |
| 2624 | else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2625 | process_notifyPlayback(ctx, evt->playback.event, &status); |
Zhiqiang Han | 3184600 | 2021-11-04 10:49:06 +0800 | [diff] [blame] | 2626 | } |
| 2627 | */ |
| 2628 | else { |
| 2629 | process_notifyPlayback(ctx, evt->playback.event, &status); |
hualing chen | b5cd42e | 2020-04-15 17:03:34 +0800 | [diff] [blame] | 2630 | ctx->playback.reach_end = DVR_TRUE; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2631 | } |
hualing chen | f291cf3 | 2020-06-18 10:50:30 +0800 | [diff] [blame] | 2632 | } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) { |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2633 | process_notifyPlayback(ctx, evt->playback.event, &status); |
| 2634 | } |
| 2635 | } break; |
Zhiqiang Han | 2d8cd82 | 2020-03-16 13:58:10 +0800 | [diff] [blame] | 2636 | case DVR_PLAYBACK_EVENT_TRANSITION_FAILED: |
| 2637 | case DVR_PLAYBACK_EVENT_KEY_FAILURE: |
| 2638 | case DVR_PLAYBACK_EVENT_NO_KEY: |
| 2639 | { |
| 2640 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event); |
| 2641 | } break; |
| 2642 | default: |
| 2643 | { |
| 2644 | DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event); |
| 2645 | } break; |
| 2646 | } |
| 2647 | return 0; |
| 2648 | } |
| 2649 | |
| 2650 | static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx) |
| 2651 | { |
| 2652 | return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx); |
| 2653 | } |
| 2654 | |