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