blob: 112dac0764f3b4528b457a0b391e9d5400f25ae9 [file] [log] [blame]
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001#include <stddef.h>
Zhiqiang Han620b9252021-11-09 14:23:20 +08002#include <unistd.h>
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003#include <stdlib.h>
4#include <pthread.h>
5#include <string.h>
6#include <time.h>
7#include <errno.h>
Zhiqiang Han18f42c82021-08-11 17:13:28 +08008#include <sys/time.h>
9#include <time.h>
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080010
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 Han2d8cd822020-03-16 13:58:10 +080020
21#include "dvr_wrapper.h"
22
Wentao MA96f68962022-06-15 19:45:35 +080023#define WRAPPER_LOG_TAG "libdvr-wrapper"
24#define DVR_WRAPPER_DEBUG(...) DVR_LOG_PRINT(LOG_LV_DEBUG, WRAPPER_LOG_TAG, __VA_ARGS__)
25#define DVR_WRAPPER_INFO(...) DVR_LOG_PRINT(LOG_LV_INFO, WRAPPER_LOG_TAG, __VA_ARGS__)
26#define DVR_WRAPPER_WARN(...) DVR_LOG_PRINT(LOG_LV_WARN, WRAPPER_LOG_TAG, __VA_ARGS__)
27#define DVR_WRAPPER_ERROR(...) DVR_LOG_PRINT(LOG_LV_ERROR, WRAPPER_LOG_TAG, __VA_ARGS__)
28#define DVR_WRAPPER_FATAL(...) DVR_LOG_PRINT(LOG_LV_FATAL, WRAPPER_LOG_TAG, __VA_ARGS__)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080029
30/*duration of data to resume if paused with EVT_REACHED_END in timeshifting*/
hualing chen2932d372020-04-29 13:44:00 +080031#define TIMESHIFT_DATA_DURATION_TO_RESUME (600)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080032/*a tolerant gap*/
33#define DVR_PLAYBACK_END_GAP (1000)
34
Wentao MA96f68962022-06-15 19:45:35 +080035int g_dvr_log_level = LOG_LV_DEFAULT;
36
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080037enum {
38 W_REC = 1,
39 W_PLAYBACK = 2,
40};
41
42enum {
43 U_PIDS = 0x01,
44 U_STAT = 0x02,
45 U_ALL = U_PIDS | U_STAT,
46};
47
48typedef struct {
Gong Kefdb31922022-06-17 17:11:16 +080049 pthread_mutex_t lock;
50 pthread_cond_t cond;
51 int inited;
52 int locked;
53} DVR_WrapperMutex_t;
54
55static void
56wrapper_mutex_init (DVR_WrapperMutex_t *lock)
57{
58 pthread_condattr_t cattr;
59
Zhiqiang Han2259da32022-07-07 15:52:58 +080060 if (lock->inited)
Gong Kefdb31922022-06-17 17:11:16 +080061 return;
62
63 pthread_mutex_init(&lock->lock, NULL);
64
65 pthread_condattr_init(&cattr);
66 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
67 pthread_cond_init(&lock->cond, &cattr);
68 pthread_condattr_destroy(&cattr);
69
70 lock->locked = 0;
71 lock->inited = 1;
72}
73
74static int
75wrapper_mutex_lock (DVR_WrapperMutex_t *lock)
76{
77 pthread_mutex_lock(&lock->lock);
78 while (lock->locked) {
79 pthread_cond_wait(&lock->cond, &lock->lock);
80 }
81 lock->locked = 1;
82 pthread_mutex_unlock(&lock->lock);
83
84 return 0;
85}
86
87static int
88wrapper_mutex_unlock (DVR_WrapperMutex_t *lock)
89{
90 pthread_mutex_lock(&lock->lock);
91 lock->locked = 0;
92 pthread_mutex_unlock(&lock->lock);
93 pthread_cond_signal(&lock->cond);
94
95 return 0;
96}
97
98static int
99wrapper_mutex_timedlock (DVR_WrapperMutex_t *lock, struct timespec *tv)
100{
101 int r = 0;
102
103 pthread_mutex_lock(&lock->lock);
104 if (lock->locked) {
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800105 //DVR_WRAPPER_DEBUG("Enter cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800106 r = pthread_cond_timedwait(&lock->cond, &lock->lock, tv);
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800107 //DVR_WRAPPER_DEBUG("Leave cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800108 }
109 if (r == 0) {
110 if (!lock->locked) {
111 lock->locked = 1;
112 } else {
113 r = ETIMEDOUT;
114 }
115 }
116 pthread_mutex_unlock(&lock->lock);
117
118 return r;
119}
120
121#define WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(expr, lock)\
122 do {\
123 if (!(expr)) {\
124 DVR_INFO("%s-%d failed", __func__, __LINE__);\
125 wrapper_mutex_unlock(lock);\
126 return DVR_FAILURE;\
127 }\
128 } while (0);
129
130
131typedef struct {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800132 /*make lock the 1st item in the structure*/
Gong Kefdb31922022-06-17 17:11:16 +0800133 DVR_WrapperMutex_t wrapper_lock;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800134
135 /*rec or play*/
136 int type;
137
138 /*valid if (sn != 0)*/
139 unsigned long sn;
140 unsigned long sn_linked;
141
142 struct list_head segments; /**<head-add list*/
143 uint64_t current_segment_id; /**<id of the current segment*/
144
145 union {
146 struct {
147 DVR_WrapperRecordOpenParams_t param_open;
148 DVR_RecordStartParams_t param_start;
149 DVR_RecordStartParams_t param_update;
150 DVR_RecordHandle_t recorder;
151 DVR_RecordEventFunction_t event_fn;
152 void *event_userdata;
153
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800154 /*total status = seg_status + status + obsolete*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800155 DVR_RecordStatus_t seg_status; /**<status of current segment*/
156 DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
157 uint64_t next_segment_id;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800158
159 DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800160 } record;
161
162 struct {
163 DVR_WrapperPlaybackOpenParams_t param_open;
164 DVR_PlaybackHandle_t player;
165 DVR_PlaybackEventFunction_t event_fn;
166 void *event_userdata;
167
168 /*total status = seg_status + status*/
169 DVR_PlaybackStatus_t seg_status;
170 DVR_WrapperPlaybackStatus_t status;
171 DVR_PlaybackPids_t pids_req;
172 DVR_PlaybackEvent_t last_event;
Zhiqiang Han3eb75f92020-04-08 10:07:55 +0800173 float speed;
hualing chenb5cd42e2020-04-15 17:03:34 +0800174 DVR_Bool_t reach_end;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800175
176 DVR_WrapperInfo_t obsolete;
hualing chen56c0a162022-01-27 17:01:50 +0800177 DVR_Bool_t tf_full;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800178 } playback;
179 };
180} DVR_WrapperCtx_t;
181
182typedef struct {
183 struct list_head head;
184 unsigned long sn;
185
186 /* rec or playback */
187 int type;
188
189 union {
190 struct {
191 DVR_RecordEvent_t event;
192 DVR_RecordStatus_t status;
193 } record;
194 struct {
195 DVR_PlaybackEvent_t event;
196 DVR_Play_Notify_t status;
197 } playback;
198 };
199} DVR_WrapperEventCtx_t;
200
201typedef struct {
202 pthread_mutex_t lock;
203 char *name;
204 int running;
205 pthread_cond_t cond;
206 pthread_t thread;
207 int type;
208} DVR_WrapperThreadCtx_t;
209
210typedef struct {
211 struct list_head head;
212
213 DVR_RecordSegmentInfo_t seg_info;
214 DVR_PlaybackSegmentInfo_t playback_info;
215} DVR_WrapperPlaybackSegmentInfo_t;
216
217typedef struct {
218 struct list_head head;
219
220 DVR_RecordSegmentInfo_t info;
221} DVR_WrapperRecordSegmentInfo_t;
222
223/* serial num generater */
224static unsigned long sn = 1;
225static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER;
226
227static inline unsigned long get_sn()
228{
hualing chenab0d1262021-09-26 15:22:50 +0800229 unsigned long no = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800230
231 pthread_mutex_lock(&sn_lock);
232 no = sn++;
233 if (!no)
234 no = sn++;
235 pthread_mutex_unlock(&sn_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800236 return no;
237}
238
239/* entity ctx */
240#define DVR_WRAPPER_MAX 10
241
242static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] =
243{
244 [0 ... (DVR_WRAPPER_MAX - 1)] =
245 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800246 .type = W_REC,
247 }
248};
249
250static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] =
251{
252 [0 ... (DVR_WRAPPER_MAX - 1)] =
253 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800254 .type = W_PLAYBACK,
255 }
256};
257
258/* events lists */
259static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list);
260static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list);
261
262static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
263static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
264
265static DVR_WrapperThreadCtx_t wrapper_thread[2] =
266{
267 [0] =
268 {
269 .lock = PTHREAD_MUTEX_INITIALIZER,
270 .running = 0,
271 .name = "record",
272 .type = W_REC,
273 },
274 [1] =
275 {
276 .lock = PTHREAD_MUTEX_INITIALIZER,
277 .running = 0,
278 .name = "playback",
279 .type = W_PLAYBACK,
280 },
281};
282
283/*now only support one timeshift now*/
284static unsigned long sn_timeshift_record;
285static unsigned long sn_timeshift_playback;
286
287static void *wrapper_task(void *arg);
288static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx);
289
290static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata);
291static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata);
292
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800293static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status);
294static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800295
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800296static int get_timespec_timeout(int timeout, struct timespec *ts)
297{
298 struct timespec ots;
299 int left, diff;
300
301 clock_gettime(CLOCK_MONOTONIC, &ots);
302
303 ts->tv_sec = ots.tv_sec + timeout / 1000;
304 ts->tv_nsec = ots.tv_nsec;
305
306 left = timeout % 1000;
307 left *= 1000000;
308 diff = 1000000000 - ots.tv_nsec;
309
310 if (diff <= left) {
311 ts->tv_sec++;
312 ts->tv_nsec = left-diff;
313 } else {
314 ts->tv_nsec += left;
315 }
316
317 return 0;
318}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800319
320static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock)
321{
Wentao MA270dc0f2022-08-23 13:17:26 +0800322 DVR_WrapperEventCtx_t *p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800323
324 pthread_mutex_lock(list_lock);
325 if (list_empty(list))
Wentao MA270dc0f2022-08-23 13:17:26 +0800326 p_evt = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800327 else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800328 p_evt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
329 list_del(&p_evt->head);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800330 }
331 pthread_mutex_unlock(list_lock);
332
Wentao MA270dc0f2022-08-23 13:17:26 +0800333 return p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800334}
335
336static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent()
337{
338 return ctx_getEvent(&record_evt_list, &record_evt_list_lock);
339}
340
341static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent()
342{
343 return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock);
344}
345
346static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt)
347{
348 DVR_WrapperEventCtx_t *padd;
349 padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t));
350 DVR_RETURN_IF_FALSE(padd);
351
352 *padd = *evt;
353 pthread_mutex_lock(lock);
354 list_add_tail(&padd->head, list);
355 pthread_mutex_unlock(lock);
356 return DVR_SUCCESS;
357}
358
359static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt)
360{
361 free(evt);
362}
363
364/*useless*/
365static void ctx_cleanOutdatedEvents(struct list_head *evt_list,
366 pthread_mutex_t *evt_list_lock,
367 DVR_WrapperCtx_t *list)
368{
Wentao MA270dc0f2022-08-23 13:17:26 +0800369 DVR_WrapperEventCtx_t *p_evt, *p_evt_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800370 unsigned long sns[DVR_WRAPPER_MAX];
371 int cnt = 0;
372 int i;
373 int found = 0;
374
375 /*copy all valid sns*/
376 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
377 sns[cnt] = list[i].sn;
378 if (!sns[cnt])
379 cnt++;
380 }
381
382 /*free evts that not belong to any valid sns*/
383 pthread_mutex_lock(evt_list_lock);
Wentao MA270dc0f2022-08-23 13:17:26 +0800384 list_for_each_entry_safe(p_evt, p_evt_tmp, evt_list, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800385 for (i = 0; i < cnt; i++) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800386 if (p_evt->sn == sns[i]) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800387 found = 1;
388 break;
389 }
390 }
391 if (!found) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800392 list_del(&p_evt->head);
393 ctx_freeEvent(p_evt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800394 }
395 }
396 pthread_mutex_unlock(evt_list_lock);
397}
398
399static inline void ctx_cleanOutdatedRecordEvents()
400{
401 ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list);
402}
403
404static inline void ctx_cleanOutdatedPlaybackEvents()
405{
406 ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list);
407}
408
hualing chenb9b358a2021-08-17 15:06:36 +0800409//check this play is recording file
410//return 0 if not the recording
411//else return record id
412static inline int ctx_isPlay_recording(char *play_location)
413{
414 int i;
415 DVR_WrapperCtx_t *cnt;
416
417 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
418 cnt = &record_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800419 //DVR_WRAPPER_INFO("[%d]sn[%d]R:[%s]P:[%s] ...\n", i, cnt->sn, cnt->record.param_open.location, play_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800420 if (!strcmp(cnt->record.param_open.location, play_location)) {
Wentao MA96f68962022-06-15 19:45:35 +0800421 DVR_WRAPPER_INFO("[%d]sn[%d]R:[%s]P:[%s] .found..\n", i, cnt->sn, cnt->record.param_open.location, play_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800422 return cnt->sn;
423 }
424 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800425 DVR_WRAPPER_INFO(" not found any play is in recording [%d]", DVR_WRAPPER_MAX);
hualing chenb9b358a2021-08-17 15:06:36 +0800426 return 0;
427}
428//check this record is playing file
429//return 0 if not the playing
430//else return playback id
431static inline int ctx_isRecord_playing(char *rec_location)
432{
433 int i;
434 DVR_WrapperCtx_t *cnt;
435 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
436 cnt = &playback_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800437 //DVR_WRAPPER_INFO("[%d]sn[%d]P[%s]R[%s] ...\n", i, cnt->sn, cnt->playback.param_open.location, rec_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800438 if (!strcmp(cnt->playback.param_open.location, rec_location)) {
Wentao MA96f68962022-06-15 19:45:35 +0800439 DVR_WRAPPER_INFO("[%d]sn[%d]P[%s]R[%s] ..found.\n",i, cnt->sn, cnt->playback.param_open.location, rec_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800440 return cnt->sn;
441 }
442 }
Wentao MA96f68962022-06-15 19:45:35 +0800443 DVR_WRAPPER_INFO(" not found rec is playing [%d]", DVR_WRAPPER_MAX);
hualing chenb9b358a2021-08-17 15:06:36 +0800444 return 0;
445}
446
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800447static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list)
448{
449 int i;
450 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
Gong Kefdb31922022-06-17 17:11:16 +0800451 if (list[i].sn == sn) {
452 wrapper_mutex_init(&list[i].wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800453 return &list[i];
Gong Kefdb31922022-06-17 17:11:16 +0800454 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800455 }
456 return NULL;
457}
458
459static inline void ctx_reset(DVR_WrapperCtx_t *ctx)
460{
461 memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn),
462 0,
463 sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn));
464}
465
466static inline int ctx_valid(DVR_WrapperCtx_t *ctx)
467{
468 return (ctx->sn != 0);
469}
470
471static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn)
472{
473 return ctx_get(sn, record_list);
474}
475
476static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn)
477{
478 return ctx_get(sn, playback_list);
479}
480
481static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *))
482{
483 pthread_mutex_lock(&ctx->lock);
484 if (ctx->running == 0) {
485 pthread_condattr_t attr;
486 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
487 pthread_cond_init(&ctx->cond, &attr);
488 pthread_condattr_destroy(&attr);
Wentao MA96f68962022-06-15 19:45:35 +0800489 DVR_WRAPPER_INFO("start wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800490 pthread_create(&ctx->thread, NULL, thread_fn, ctx);
Wentao MA96f68962022-06-15 19:45:35 +0800491 DVR_WRAPPER_INFO("wrapper thread(%s) started\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800492 }
493 ctx->running++;
494 pthread_mutex_unlock(&ctx->lock);
495 return 0;
496}
497
498static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx)
499{
500 pthread_mutex_lock(&ctx->lock);
501 ctx->running--;
502 if (!ctx->running) {
503 pthread_cond_broadcast(&ctx->cond);
504 pthread_mutex_unlock(&ctx->lock);
505
Wentao MA96f68962022-06-15 19:45:35 +0800506 DVR_WRAPPER_INFO("stop wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800507 pthread_join(ctx->thread, NULL);
Wentao MA96f68962022-06-15 19:45:35 +0800508 DVR_WRAPPER_INFO("wrapper thread(%s) stopped\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800509
510 pthread_mutex_lock(&ctx->lock);
511 if (!ctx->running) /*protect*/
512 pthread_cond_destroy(&ctx->cond);
513 }
514 pthread_mutex_unlock(&ctx->lock);
515 return 0;
516}
517
518#define WRAPPER_THREAD_RECORD (&wrapper_thread[0])
519#define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1])
520
521static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx)
522{
523 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
524 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
525 return wrapper_requestThread(thread_ctx, wrapper_task);
526}
527
528static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx)
529{
530 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
531 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
532 return wrapper_releaseThread(thread_ctx);
533}
534
535static inline int wrapper_releaseThreadForType(int type)
536{
537 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)?
538 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
539 return wrapper_releaseThread(thread_ctx);
540}
541
542static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx)
543{
544 pthread_cond_signal(&thread_ctx->cond);
545}
546
547static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx)
548{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800549 struct timespec rt;
550 get_timespec_timeout(200, &rt);
551 pthread_cond_timedwait(&thread_ctx->cond, &thread_ctx->lock, &rt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800552 return 0;
553}
554
555static inline void wrapper_threadSignalForType(int type)
556{
557 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ?
558 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
559 wrapper_threadSignal(thread_ctx);
560}
561
562static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx)
563{
564 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
565 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
566 wrapper_threadSignal(thread_ctx);
567}
568
569static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx)
570{
571 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
572 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
573 wrapper_threadWait(thread_ctx);
574 return 0;
575}
576
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800577/*return condition, locked if condition == true*/
Gong Kefdb31922022-06-17 17:11:16 +0800578static int wrapper_mutex_lock_if(DVR_WrapperMutex_t *lock, int *condition)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800579{
580 int r2;
581 do {
582 struct timespec rt2;
Gong Kefdb31922022-06-17 17:11:16 +0800583 get_timespec_timeout(10, &rt2);
584 r2 = wrapper_mutex_timedlock(lock, &rt2);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800585 } while (*condition && (r2 == ETIMEDOUT));
586
587 if (!(*condition) && (r2 == 0))
Gong Kefdb31922022-06-17 17:11:16 +0800588 wrapper_mutex_unlock(lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800589
590 return *condition;
591}
592
593static void *wrapper_task(void *arg)
594{
Wentao MA270dc0f2022-08-23 13:17:26 +0800595 DVR_WrapperThreadCtx_t *thread_ctx = (DVR_WrapperThreadCtx_t *)arg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800596 DVR_WrapperEventCtx_t *evt;
597
Wentao MA270dc0f2022-08-23 13:17:26 +0800598 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800599
Wentao MA270dc0f2022-08-23 13:17:26 +0800600 while (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800601 {
602 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800603
Wentao MA270dc0f2022-08-23 13:17:26 +0800604 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800605 if (!evt)
Wentao MA270dc0f2022-08-23 13:17:26 +0800606 ret = wrapper_threadWait(thread_ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800607 }
608
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800609 while (evt) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800610 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
611 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chenbc0aec92021-03-18 14:52:40 +0800612 if (ctx == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800613 DVR_WRAPPER_INFO("warp not get ctx.free event..\n");
hualing chenbc0aec92021-03-18 14:52:40 +0800614 goto processed;
615 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800616 DVR_WRAPPER_INFO("start name(%s) sn(%d) running(%d) type(%d)\n", thread_ctx->name, (int)ctx->sn, thread_ctx->running, thread_ctx->type);
617 if (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800618 /*
619 continue not break,
620 make all events consumed, or mem leak
621 */
Wentao MA270dc0f2022-08-23 13:17:26 +0800622 if (!wrapper_mutex_lock_if(&ctx->wrapper_lock, &thread_ctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800623 goto processed;
624
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800625 if (ctx_valid(ctx)) {
626 /*double check after lock*/
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800627 if (evt->sn == ctx->sn) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800628 pthread_mutex_unlock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800629 process_handleEvents(evt, ctx);
Wentao MA270dc0f2022-08-23 13:17:26 +0800630 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800631 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800632 }
Gong Kefdb31922022-06-17 17:11:16 +0800633 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800634 }
635
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800636processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800637 ctx_freeEvent(evt);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800638
Wentao MA270dc0f2022-08-23 13:17:26 +0800639 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800640 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800641 DVR_WRAPPER_INFO("start name(%s) running(%d) type(%d) con...\n", thread_ctx->name, thread_ctx->running, thread_ctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800642 }
643
Wentao MA270dc0f2022-08-23 13:17:26 +0800644 pthread_mutex_unlock(&thread_ctx->lock);
645 DVR_WRAPPER_INFO("end name(%s) running(%d) type(%d) end...\n", thread_ctx->name, thread_ctx->running, thread_ctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800646 return NULL;
647}
648
649static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
650{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800651 pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800652 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
653 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800654 pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800655 return 0;
656}
657
658static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
659{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800660 pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800661 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
662 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800663 pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800664 return 0;
665}
666
667static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
668{
Wentao MA270dc0f2022-08-23 13:17:26 +0800669 DVR_WrapperPlaybackSegmentInfo_t *p_seg, *p_seg_tmp;
670 list_for_each_entry_safe(p_seg, p_seg_tmp, &ctx->segments, head) {
671 list_del(&p_seg->head);
672 free(p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800673 }
674}
675
Wentao MA270dc0f2022-08-23 13:17:26 +0800676static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800677 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
678{
679 (void)ctx;
680 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800681 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800682 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800683 p_seg->seg_info.nb_pids = seg_info->nb_pids;
684 memcpy(p_seg->seg_info.pids, seg_info->pids, sizeof(p_seg->seg_info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800685 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800686 p_seg->seg_info.duration = seg_info->duration;
687 p_seg->seg_info.size = seg_info->size;
688 p_seg->seg_info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800689 }
hualing chen03fd4942021-07-15 15:56:41 +0800690 //update current segment duration on timeshift mode
hualing chenb9b358a2021-08-17 15:06:36 +0800691 if (ctx->playback.param_open.is_timeshift
692 || ctx_isPlay_recording(ctx->playback.param_open.location))
Wentao MA270dc0f2022-08-23 13:17:26 +0800693 dvr_playback_update_duration(ctx->playback.player,p_seg->seg_info.id,p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800694 /*no changes
695 DVR_PlaybackSegmentFlag_t flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800696 p_seg->playback_info.segment_id = p_seg->seg_info.id;
697 strncpy(p_seg->playback_info.location,
698 ctx->playback.param_open.location, sizeof(p_seg->playback_info.location));
699 p_seg->playback_info.pids = ctx->playback.pids_req;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800700 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
701 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
702 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
Wentao MA270dc0f2022-08-23 13:17:26 +0800703 p_seg->playback_info.flags = flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800704 */
705}
706
707static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
708{
Wentao MA270dc0f2022-08-23 13:17:26 +0800709 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800710
Wentao MA96f68962022-06-15 19:45:35 +0800711 DVR_WRAPPER_INFO("timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800712 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
713
714 if (list_empty(&ctx->segments)) {
Wentao MA96f68962022-06-15 19:45:35 +0800715 DVR_WRAPPER_INFO("timeshift, update while no segment exists, ignore\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800716 return DVR_SUCCESS;
717 }
718
719 /*normally, the last segment added will be updated*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800720 p_seg =
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800721 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800722 if (p_seg->seg_info.id == seg_info->id) {
723 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800724 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800725 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
726 if (p_seg->seg_info.id == seg_info->id) {
727 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800728 break;
729 }
730 }
731 }
732
733 /*need to notify the dvr_playback*/
hualing chenb9b358a2021-08-17 15:06:36 +0800734 if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/
735 || ctx_isPlay_recording(ctx->playback.param_open.location))
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800736 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
737 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
738 if (
739 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
740 (ctx->playback.seg_status.segment_id == seg_info->id
741 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
742 ||
743 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
744 (ctx->playback.seg_status.segment_id != seg_info->id
745 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
746 )
747 {
748 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800749 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800750 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800751 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800752
753 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800754 DVR_WRAPPER_INFO("timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800755 ctx->sn, error,
756 seg_info->id, seg_info->duration,
757 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
758 }
759 }
760
761 return DVR_SUCCESS;
762}
763
Wentao MA270dc0f2022-08-23 13:17:26 +0800764static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800765 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
766{
767 (void)ctx;
768 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800769 p_seg->info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800770 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800771 p_seg->info.nb_pids = seg_info->nb_pids;
772 memcpy(p_seg->info.pids, seg_info->pids, sizeof(p_seg->info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800773 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800774 p_seg->info.duration = seg_info->duration;
775 p_seg->info.size = seg_info->size;
776 p_seg->info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800777 }
778}
779
780static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
781{
Wentao MA270dc0f2022-08-23 13:17:26 +0800782 DVR_WrapperRecordSegmentInfo_t *p_seg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800783
784 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800785 if (!list_empty(&ctx->segments)) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800786 p_seg =
hualing chen266b9502020-04-04 17:39:39 +0800787 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800788 if (p_seg->info.id == seg_info->id) {
789 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800790 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800791 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
792 if (p_seg->info.id == seg_info->id) {
793 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800794 break;
795 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800796 }
797 }
798 }
799
800 /*timeshift, update the segment for playback*/
801 /*
802 the playback should grab the segment info other than the id,
803 and the id will be updated by each segment-add during the recording
804 */
805 /*
806 the playback paused if no data been checked from recording,
807 should resume the player later when there's more data
808 */
hualing chenb9b358a2021-08-17 15:06:36 +0800809 int sn = 0;
810 if (ctx->record.param_open.is_timeshift ||
811 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
812 DVR_WrapperCtx_t *ctx_playback;
813 if (ctx->record.param_open.is_timeshift)
814 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
815 else
816 ctx_playback = ctx_getPlayback(sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800817
818 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800819 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800820 if (ctx_valid(ctx_playback)
hualing chenb9b358a2021-08-17 15:06:36 +0800821 && (ctx_playback->sn == sn_timeshift_playback ||
822 ctx_playback->sn == sn)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800823 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
824 }
Gong Kefdb31922022-06-17 17:11:16 +0800825 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800826 }
827 }
828
829 return DVR_SUCCESS;
830}
831
832static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
833 DVR_RecordSegmentInfo_t *seg_info,
834 DVR_PlaybackPids_t *p_pids,
835 DVR_PlaybackSegmentFlag_t flags)
836{
Wentao MA270dc0f2022-08-23 13:17:26 +0800837 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800838 int error;
839
840 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +0800841 p_seg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
842 if (!p_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800843 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +0800844 DVR_WRAPPER_INFO("memory fail\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800845 return error;
846 }
847
Wentao MA270dc0f2022-08-23 13:17:26 +0800848 /*copy the original segment info*/
849 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800850 /*generate the segment info used in playback*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800851 p_seg->playback_info.segment_id = p_seg->seg_info.id;
Wentao MAe88ad702022-09-02 10:35:00 +0800852 const int len = strlen(ctx->playback.param_open.location);
853 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
854 DVR_WRAPPER_ERROR("Invalid playback.param_open.location length %d", len);
Wentao MA4d85ff32022-09-23 11:36:18 +0800855 free(p_seg);
856 p_seg = NULL;
Wentao MAe88ad702022-09-02 10:35:00 +0800857 return DVR_FAILURE;
858 }
859 strncpy(p_seg->playback_info.location, ctx->playback.param_open.location, len+1);
Wentao MA270dc0f2022-08-23 13:17:26 +0800860 p_seg->playback_info.pids = *p_pids;
861 p_seg->playback_info.flags = flags;
862 list_add(&p_seg->head, &ctx->segments);
863 DVR_WRAPPER_INFO("start to add segment %lld\n", p_seg->playback_info.segment_id);
864 p_seg->playback_info.duration = p_seg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800865
Wentao MA270dc0f2022-08-23 13:17:26 +0800866 error = dvr_playback_add_segment(ctx->playback.player, &p_seg->playback_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800867 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800868 DVR_WRAPPER_INFO("fail to add segment %lld (%d)\n", p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800869 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800870 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
871 ctx->playback.status.info_full.size += p_seg->seg_info.size;
872 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800873 }
874
875 return error;
876}
877
878static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
879{
Wentao MA270dc0f2022-08-23 13:17:26 +0800880 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MA16f870e2022-09-09 11:00:22 +0800881 int error = DVR_SUCCESS;
hualing chenab0d1262021-09-26 15:22:50 +0800882 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800883
Wentao MA270dc0f2022-08-23 13:17:26 +0800884 p_seg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
885 if (!p_seg) {
Wentao MA16f870e2022-09-09 11:00:22 +0800886 DVR_WRAPPER_ERROR("memory allocation failed");
887 return DVR_FAILURE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800888 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800889 p_seg->info = *seg_info;
890 list_add(&p_seg->head, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800891
hualing chenb9b358a2021-08-17 15:06:36 +0800892 if (ctx->record.param_open.is_timeshift ||
893 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
894
895 DVR_WrapperCtx_t *ctx_playback;
896 if (ctx->record.param_open.is_timeshift)
897 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
898 else
899 ctx_playback = ctx_getPlayback(sn);
900
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800901 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) add seg\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800902
903 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800904 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800905 if (ctx_valid(ctx_playback)) {
906 DVR_PlaybackSegmentFlag_t flags;
907
908 /*only if playback has started, the previous segments have been loaded*/
909 if (!list_empty(&ctx_playback->segments)) {
910 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800911 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800912 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
913 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
hualing chen451c8f72022-03-09 13:05:52 +0800914 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800915 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) list empty\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800916 }
hualing chenb9b358a2021-08-17 15:06:36 +0800917 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800918 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800919 }
Gong Kefdb31922022-06-17 17:11:16 +0800920 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800921 }
hualing chen451c8f72022-03-09 13:05:52 +0800922 else
923 {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800924 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid 2\n", ctx->sn, sn);
925 }
926
927 /*if it is not a timeshift recording, but a playing recording,
928 do not forget to obey the recording rule: link the segment!*/
929 if (!ctx->record.param_open.is_timeshift) {
930 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
931 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
hualing chen451c8f72022-03-09 13:05:52 +0800932 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800933 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800934 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
Wentao MAe8ba5172022-08-09 11:18:17 +0800935 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800936 }
937
938 return error;
939}
940
941static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
942{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800943 int error = -1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800944 DVR_WrapperPlaybackSegmentInfo_t *p_seg = NULL, *p_seg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800945 uint32_t off_set = 0;
Wentao MA96f68962022-06-15 19:45:35 +0800946 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800947
Wentao MA270dc0f2022-08-23 13:17:26 +0800948 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
949 if (p_seg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800950
951 if (ctx->current_segment_id == seg_info->id) {
952 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
953
954 /*drive the player out of this will-be-deleted segment*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800955 next_seg = list_prev_entry(p_seg, head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800956
957 if (ctx->playback.speed != 100.0f) {
958 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800959 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800960 }
hualing chenb9a1a2c2021-12-31 11:27:59 +0800961 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
962 off_set = 10 * 1000;
963 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800964 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), seek(seg:%llu 0) from new start (%d)\n", ctx->sn, next_seg->seg_info.id, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800965
966 if (ctx->playback.speed == 0.0f) {
967 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +0800968 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), keep last paused from new start (%d)\n", ctx->sn, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800969 } else if (ctx->playback.speed != 100.0f) {
970 DVR_PlaybackSpeed_t dvr_speed = {
971 .speed = { ctx->playback.speed },
972 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
973 };
974 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
Wentao MA96f68962022-06-15 19:45:35 +0800975 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), keep last speed(x%f) from new start (%d)\n", ctx->sn,ctx->playback.speed, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800976 }
977 }
978
979 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
980 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800981 /*remove playback segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +0800982 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), failed to remove segment(%llu) (%d)\n", ctx->sn, seg_info->id, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800983 }
984
Wentao MA270dc0f2022-08-23 13:17:26 +0800985 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800986
987 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800988 ctx->playback.obsolete.time += p_seg->seg_info.duration;
989 ctx->playback.obsolete.size += p_seg->seg_info.size;
990 ctx->playback.obsolete.pkts += p_seg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +0800991 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ..obs(%d).\n", ctx->sn, seg_info->id, ctx->playback.obsolete.time);
hualing chen03fd4942021-07-15 15:56:41 +0800992 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Wentao MA270dc0f2022-08-23 13:17:26 +0800993 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800994 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800995 }
996 }
997
Wentao MA96f68962022-06-15 19:45:35 +0800998 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800999
1000 return error;
1001}
1002
1003static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
1004{
1005 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08001006 DVR_WrapperRecordSegmentInfo_t *p_seg, *p_seg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001007
Wentao MAf4072032022-06-30 13:50:45 +08001008 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1009 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001010
1011 /*if timeshifting, notify the playback first, then deal with record*/
1012 if (ctx->record.param_open.is_timeshift) {
1013 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1014
1015 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001016 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001017 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1018 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001019 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1020 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1021 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001022 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001023 return DVR_SUCCESS;
1024 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001025 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1026 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1027 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001028 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001029 if (ctx_valid(ctx_playback)
1030 && ctx_playback->sn == sn_timeshift_playback
1031 && !list_empty(&ctx_playback->segments)) {
1032 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
Wentao MA07d3d742022-09-06 09:58:05 +08001033 if (error != DVR_SUCCESS) {
1034 DVR_WRAPPER_ERROR("wrapper_removePlaybackSegment failed with return value %d",error);
1035 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001036 }
hualing chen56c0a162022-01-27 17:01:50 +08001037 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001038 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001039 }
1040 }
1041
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001042 uint64_t id = seg_info->info.id;
1043
Wentao MA270dc0f2022-08-23 13:17:26 +08001044 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
1045 if (p_seg->info.id == id) {
1046 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001047
1048 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001049 ctx->record.obsolete.time += p_seg->info.duration;
1050 ctx->record.obsolete.size += p_seg->info.size;
1051 ctx->record.obsolete.pkts += p_seg->info.nb_packets;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001052
Wentao MA270dc0f2022-08-23 13:17:26 +08001053 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001054 break;
1055 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001056 }
1057
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001058 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001059
Wentao MAf4072032022-06-30 13:50:45 +08001060 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1061 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001062
1063 return error;
1064}
1065
1066int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1067{
1068 int error;
1069 DVR_WrapperCtx_t *ctx;
1070 DVR_RecordOpenParams_t open_param;
1071
1072 DVR_RETURN_IF_FALSE(rec);
1073 DVR_RETURN_IF_FALSE(params);
1074
1075 /*get a free ctx*/
1076 ctx = ctx_getRecord(0);
1077 DVR_RETURN_IF_FALSE(ctx);
1078
Gong Kefdb31922022-06-17 17:11:16 +08001079 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001080
Wentao MA9a164002022-08-29 11:20:24 +08001081 DVR_WRAPPER_INFO("open record(dmx:%d) .is_tf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
hualing chen51652f02020-12-29 16:59:31 +08001082 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001083
1084 ctx_reset(ctx);
1085
1086 ctx->record.param_open = *params;
1087 ctx->record.event_fn = params->event_fn;
1088 ctx->record.event_userdata = params->event_userdata;
1089 ctx->record.next_segment_id = 0;
1090 ctx->current_segment_id = 0;
1091 INIT_LIST_HEAD(&ctx->segments);
1092 ctx->sn = get_sn();
1093
1094 wrapper_requestThreadFor(ctx);
1095
hualing chen266b9502020-04-04 17:39:39 +08001096 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001097 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001098 open_param.dmx_dev_id = params->dmx_dev_id;
1099 open_param.data_from_memory = 0;
1100 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001101 if (params->flush_size) {
1102 open_param.notification_size = params->flush_size;
1103 } else {
1104 open_param.notification_size = 64*1024;
1105 }
hualing chen002e5b92022-02-23 17:51:21 +08001106 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001107 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001108 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001109 open_param.event_fn = wrapper_record_event_handler;
1110 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001111 if (params->keylen) {
1112 open_param.clearkey = params->clearkey;
1113 open_param.cleariv = params->cleariv;
1114 open_param.keylen = params->keylen;
1115 }
wentao.ma35a69d42022-03-10 18:08:40 +08001116 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001117 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001118
1119 error = dvr_record_open(&ctx->record.recorder, &open_param);
1120 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001121 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001122 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001123 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001124 wrapper_releaseThreadForType(ctx->type);
1125 return DVR_FAILURE;
1126 }
1127 if (params->is_timeshift)
1128 sn_timeshift_record = ctx->sn;
1129
Wentao MA96f68962022-06-15 19:45:35 +08001130 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001131
Yahui Han1fbf3292021-11-08 18:17:19 +08001132 if (params->crypto_fn) {
1133 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1134 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001135 DVR_WRAPPER_INFO("record(dmx:%d) set encrypt callback fail(error:%d).\n", params->dmx_dev_id, error);
Yahui Han1fbf3292021-11-08 18:17:19 +08001136 }
hualing chen266b9502020-04-04 17:39:39 +08001137 }
1138
Gong Kefdb31922022-06-17 17:11:16 +08001139 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001140
1141 *rec = (DVR_WrapperRecord_t)ctx->sn;
1142 return DVR_SUCCESS;
1143}
1144
1145int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1146{
1147 DVR_WrapperCtx_t *ctx;
1148 DVR_RecordSegmentInfo_t seg_info;
1149 int error;
1150
1151 DVR_RETURN_IF_FALSE(rec);
1152
1153 ctx = ctx_getRecord((unsigned long)rec);
1154 DVR_RETURN_IF_FALSE(ctx);
1155
Gong Kefdb31922022-06-17 17:11:16 +08001156 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001157 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001158 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001159
1160 memset(&seg_info, 0, sizeof(seg_info));
1161 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1162
1163 error = dvr_record_close(ctx->record.recorder);
1164
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001165 if (ctx->record.param_open.is_timeshift)
1166 sn_timeshift_record = 0;
1167
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001168 ctx_freeSegments(ctx);
1169
Wentao MA96f68962022-06-15 19:45:35 +08001170 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001171 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001172 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001173
1174 wrapper_releaseThreadForType(ctx->type);
1175
1176 return error;
1177}
1178
1179int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1180{
1181 DVR_WrapperCtx_t *ctx;
1182 DVR_RecordStartParams_t *start_param;
1183 int i;
1184 int error;
1185
1186 DVR_RETURN_IF_FALSE(rec);
1187 DVR_RETURN_IF_FALSE(params);
1188
1189 ctx = ctx_getRecord((unsigned long)rec);
1190 DVR_RETURN_IF_FALSE(ctx);
1191
Gong Kefdb31922022-06-17 17:11:16 +08001192 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001193 DVR_WRAPPER_INFO("start record(sn:%ld, location:%s) save(%d)...\n", ctx->sn, ctx->record.param_open.location, params->save_rec_file);
Gong Kefdb31922022-06-17 17:11:16 +08001194 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001195
1196 start_param = &ctx->record.param_start;
1197 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001198 const int len = strlen(ctx->record.param_open.location);
1199 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1200 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1201 pthread_mutex_unlock(&ctx->wrapper_lock);
1202 return DVR_FAILURE;
1203 }
1204 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001205 start_param->segment.segment_id = ctx->record.next_segment_id++;
1206 start_param->segment.nb_pids = params->pids_info.nb_pids;
1207 for (i = 0; i < params->pids_info.nb_pids; i++) {
1208 start_param->segment.pids[i] = params->pids_info.pids[i];
1209 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1210 }
hualing chena5f03222021-12-02 11:22:35 +08001211 if (params->save_rec_file == 0)//default is not save
1212 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001213 {
1214 /*sync to update for further use*/
1215 DVR_RecordStartParams_t *update_param;
1216 update_param = &ctx->record.param_update;
1217 memcpy(update_param, start_param, sizeof(*update_param));
1218 for (i = 0; i < update_param->segment.nb_pids; i++)
1219 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1220 }
1221
1222 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1223 {
1224 DVR_RecordSegmentInfo_t new_seg_info =
1225 { .id = start_param->segment.segment_id, };
1226 wrapper_addRecordSegment(ctx, &new_seg_info);
1227 }
1228
Wentao MA96f68962022-06-15 19:45:35 +08001229 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001230
Gong Kefdb31922022-06-17 17:11:16 +08001231 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001232
1233 return error;
1234}
1235
1236int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1237{
1238 DVR_WrapperCtx_t *ctx;
1239 DVR_RecordSegmentInfo_t seg_info;
1240 int error;
1241
1242 DVR_RETURN_IF_FALSE(rec);
1243
1244 ctx = ctx_getRecord((unsigned long)rec);
1245 DVR_RETURN_IF_FALSE(ctx);
1246
Gong Kefdb31922022-06-17 17:11:16 +08001247 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001248 DVR_WRAPPER_INFO("stop record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001249 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001250
1251 memset(&seg_info, 0, sizeof(seg_info));
1252 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1253 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1254
Wentao MA96f68962022-06-15 19:45:35 +08001255 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001256 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001257
1258 return error;
1259}
1260
hualing chen03fd4942021-07-15 15:56:41 +08001261int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1262{
1263 DVR_WrapperCtx_t *ctx;
1264 int error;
1265
1266 DVR_RETURN_IF_FALSE(rec);
1267
1268 ctx = ctx_getRecord((unsigned long)rec);
1269 DVR_RETURN_IF_FALSE(ctx);
1270
Gong Kefdb31922022-06-17 17:11:16 +08001271 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001272 DVR_WRAPPER_INFO("pause record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001273 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001274
1275 error = dvr_record_pause(ctx->record.recorder);
1276
Wentao MA96f68962022-06-15 19:45:35 +08001277 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001278 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001279
1280 return error;
1281}
1282
1283int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1284{
1285 DVR_WrapperCtx_t *ctx;
1286 int error;
1287
1288 DVR_RETURN_IF_FALSE(rec);
1289
1290 ctx = ctx_getRecord((unsigned long)rec);
1291 DVR_RETURN_IF_FALSE(ctx);
1292
Gong Kefdb31922022-06-17 17:11:16 +08001293 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001294 DVR_WRAPPER_INFO("resume record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001295 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001296
1297 error = dvr_record_resume(ctx->record.recorder);
1298
Wentao MA96f68962022-06-15 19:45:35 +08001299 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001300 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001301
1302 return error;
1303}
1304
Wentao MAcdea4762022-04-26 13:28:56 +08001305/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001306static DVR_Bool_t pids_test_include(
1307 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1308 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001309{
Wentao MAcdea4762022-04-26 13:28:56 +08001310 DVR_Bool_t ret = DVR_TRUE;
1311 for (int i=0;i<size2;i++)
1312 { // iterate all elements in arr2 to check if they exist in arr1
1313 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001314
1315 if (act2[i] == DVR_RECORD_PID_CLOSE)
1316 continue;
1317
Wentao MAcdea4762022-04-26 13:28:56 +08001318 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001319 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001320 if (act1[j] != DVR_RECORD_PID_CLOSE
1321 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001322 {
1323 found=DVR_TRUE;
1324 break;
1325 }
1326 }
1327 if (found == DVR_FALSE)
1328 {
Wentao MAcdea4762022-04-26 13:28:56 +08001329 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001330 break;
1331 }
1332 }
Wentao MAcdea4762022-04-26 13:28:56 +08001333 return ret;
1334}
1335
1336static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1337 const DVR_WrapperUpdatePidsParams_t* p2)
1338{
1339 int i=0;
1340 char buf[128]={0};
1341 int cnt=0;
1342 int chars=0;
1343
1344 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1345 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1346
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001347 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1348 p2->pids,p2->pid_action,p2->nb_pids);
1349 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1350 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001351 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001352 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001353
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001354 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001355 for (i=0;i<p1->nb_pids;i++)
1356 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001357 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1358 removed++;
1359 continue;
1360 }
Wentao MAcdea4762022-04-26 13:28:56 +08001361 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1362 if (chars<0)
1363 {
1364 break;
1365 }
1366 cnt += chars;
1367 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001368 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001369 memset(buf,0,sizeof(buf));
1370
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001371 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001372 for (i=0,cnt=0;i<p2->nb_pids;i++)
1373 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001374 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1375 removed++;
1376 continue;
1377 }
Wentao MAcdea4762022-04-26 13:28:56 +08001378 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1379 if (chars<0)
1380 {
1381 break;
1382 }
1383 cnt += chars;
1384 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001385 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001386 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001387 return is_equal;
1388}
1389
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001390int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1391{
1392 DVR_WrapperCtx_t *ctx;
1393 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001394 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001395 int i;
1396 int error;
1397
1398 DVR_RETURN_IF_FALSE(rec);
1399 DVR_RETURN_IF_FALSE(params);
1400
1401 ctx = ctx_getRecord((unsigned long)rec);
1402 DVR_RETURN_IF_FALSE(ctx);
1403
Gong Kefdb31922022-06-17 17:11:16 +08001404 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001405 DVR_WRAPPER_INFO("update record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001406 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001407
1408 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001409 if (pids_equal(&(start_param->segment),params))
1410 {
Gong Kefdb31922022-06-17 17:11:16 +08001411 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001412 return DVR_TRUE;
1413 }
1414
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001415 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001416 const int len = strlen(ctx->record.param_open.location);
1417 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1418 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1419 pthread_mutex_unlock(&ctx->wrapper_lock);
1420 return DVR_FAILURE;
1421 }
1422 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001423 start_param->segment.segment_id = ctx->record.next_segment_id++;
1424 start_param->segment.nb_pids = params->nb_pids;
1425 for (i = 0; i < params->nb_pids; i++) {
1426 start_param->segment.pids[i] = params->pids[i];
1427 start_param->segment.pid_action[i] = params->pid_action[i];
1428 }
1429 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1430 {
1431 DVR_RecordSegmentInfo_t new_seg_info =
1432 { .id = start_param->segment.segment_id, };
1433 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1434 wrapper_addRecordSegment(ctx, &new_seg_info);
1435 }
1436
Wentao MA96f68962022-06-15 19:45:35 +08001437 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001438 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001439
1440 return error;
1441}
1442
1443int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1444{
1445 DVR_WrapperCtx_t *ctx;
1446 DVR_WrapperRecordStatus_t s;
1447 int error;
1448
1449 DVR_RETURN_IF_FALSE(rec);
1450 DVR_RETURN_IF_FALSE(status);
1451
1452 ctx = ctx_getRecord((unsigned long)rec);
1453 DVR_RETURN_IF_FALSE(ctx);
1454
Gong Kefdb31922022-06-17 17:11:16 +08001455 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001456
Wentao MA96f68962022-06-15 19:45:35 +08001457 DVR_WRAPPER_INFO("get record(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001458 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001459
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001460 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001461
Wentao MA96f68962022-06-15 19:45:35 +08001462 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001463 ctx->sn,
1464 s.state,
1465 s.info.time,
1466 s.info.size,
1467 s.info.pkts,
1468 error);
1469
1470 *status = s;
1471
Gong Kefdb31922022-06-17 17:11:16 +08001472 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001473
1474 return error;
1475}
1476
hualing chen4fe3bee2020-10-23 13:58:52 +08001477int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1478{
1479 DVR_WrapperCtx_t *ctx;
1480 int error;
1481
1482 DVR_RETURN_IF_FALSE(rec);
1483
1484 ctx = ctx_getRecord((unsigned long)rec);
1485 DVR_RETURN_IF_FALSE(ctx);
1486
Gong Kefdb31922022-06-17 17:11:16 +08001487 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001488 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001489 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001490 return error;
1491}
1492
hualing chen266b9502020-04-04 17:39:39 +08001493int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1494{
1495 DVR_WrapperCtx_t *ctx;
1496 int error;
1497
1498 DVR_RETURN_IF_FALSE(rec);
1499 DVR_RETURN_IF_FALSE(p_secure_buf);
1500
1501 ctx = ctx_getRecord((unsigned long)rec);
1502 DVR_RETURN_IF_FALSE(ctx);
1503
Gong Kefdb31922022-06-17 17:11:16 +08001504 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001505 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001506 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001507 return error;
1508}
1509
1510int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1511{
1512 DVR_WrapperCtx_t *ctx;
1513 int error;
1514
1515 DVR_RETURN_IF_FALSE(rec);
1516 DVR_RETURN_IF_FALSE(func);
1517
1518 ctx = ctx_getRecord((unsigned long)rec);
1519 DVR_RETURN_IF_FALSE(ctx);
1520
Gong Kefdb31922022-06-17 17:11:16 +08001521 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001522 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001523 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001524 return error;
1525}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001526
1527
1528int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1529{
1530 DVR_WrapperCtx_t *ctx;
1531 DVR_PlaybackOpenParams_t open_param;
1532 int error;
1533
1534 DVR_RETURN_IF_FALSE(playback);
1535 DVR_RETURN_IF_FALSE(params);
1536 DVR_RETURN_IF_FALSE(params->playback_handle);
1537
1538 /*get a free ctx*/
1539 ctx = ctx_getPlayback(0);
1540 DVR_RETURN_IF_FALSE(ctx);
1541
Gong Kefdb31922022-06-17 17:11:16 +08001542 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001543
Wentao MA96f68962022-06-15 19:45:35 +08001544 DVR_WRAPPER_INFO("open playback(dmx:%d) ..vendor[%d]params->block_size[%d].\n", params->dmx_dev_id, params->vendor, params->block_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001545
1546 ctx_reset(ctx);
1547
1548 ctx->playback.param_open = *params;
1549 ctx->playback.event_fn = params->event_fn;
1550 ctx->playback.event_userdata = params->event_userdata;
1551 ctx->current_segment_id = 0;
1552 INIT_LIST_HEAD(&ctx->segments);
1553 ctx->sn = get_sn();
1554
1555 wrapper_requestThreadFor(ctx);
1556
hualing chen266b9502020-04-04 17:39:39 +08001557 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001558 open_param.dmx_dev_id = params->dmx_dev_id;
1559 open_param.block_size = params->block_size;
1560 open_param.is_timeshift = params->is_timeshift;
1561 //open_param.notification_size = 10*1024; //not supported
1562 open_param.event_fn = wrapper_playback_event_handler;
1563 open_param.event_userdata = (void*)ctx->sn;
1564 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001565 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001566 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001567 open_param.vendor = params->vendor;
1568
Yahui Han1fbf3292021-11-08 18:17:19 +08001569 if (params->keylen) {
1570 open_param.clearkey = params->clearkey;
1571 open_param.cleariv = params->cleariv;
1572 open_param.keylen = params->keylen;
1573 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001574
1575 error = dvr_playback_open(&ctx->playback.player, &open_param);
1576 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001577 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001578 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001579 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001580 wrapper_releaseThreadForType(ctx->type);
1581 return DVR_FAILURE;
1582 }
1583 if (params->is_timeshift)
1584 sn_timeshift_playback = ctx->sn;
1585
Wentao MA270dc0f2022-08-23 13:17:26 +08001586 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001587 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1588 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001589 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001590 }
Gong Kefdb31922022-06-17 17:11:16 +08001591 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001592
1593 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1594 return DVR_SUCCESS;
1595}
1596
1597int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1598{
1599 DVR_WrapperCtx_t *ctx;
1600 int error;
1601
1602 DVR_RETURN_IF_FALSE(playback);
1603
1604 ctx = ctx_getPlayback((unsigned long)playback);
1605 DVR_RETURN_IF_FALSE(ctx);
1606
Gong Kefdb31922022-06-17 17:11:16 +08001607 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001608 DVR_WRAPPER_INFO("close playback(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001609 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001610
1611 if (ctx->playback.param_open.is_timeshift)
1612 sn_timeshift_playback = 0;
1613
1614 /*try stop first*/
1615 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1616
1617 {
1618 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001619 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001620
Wentao MA270dc0f2022-08-23 13:17:26 +08001621 list_for_each_entry(p_seg, &ctx->segments, head) {
1622 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001623 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001624 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001625 }
1626 ctx_freeSegments(ctx);
1627 }
1628
1629 error = dvr_playback_close(ctx->playback.player);
1630
Wentao MA96f68962022-06-15 19:45:35 +08001631 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001632 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001633 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001634
1635 wrapper_releaseThreadForType(ctx->type);
1636
1637 return error;
1638}
1639
1640int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1641{
1642 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001643 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001644 uint64_t *p_segment_ids;
1645 uint32_t segment_nb;
1646 uint32_t i;
1647 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001648 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001649 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001650 DVR_Bool_t is_timeshift = DVR_FALSE;
Wentao MA9628de82022-09-13 14:19:37 +08001651 DVR_PlaybackSegmentFlag_t segflags = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001652
1653 DVR_RETURN_IF_FALSE(playback);
1654 DVR_RETURN_IF_FALSE(p_pids);
1655
hualing chenc110f952021-01-18 11:25:37 +08001656 ctx_record = NULL;
1657
1658 /*lock the recorder to avoid changing the recording segments*/
1659 ctx_record = ctx_getRecord(sn_timeshift_record);
1660
1661 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001662 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001663 if (!ctx_valid(ctx_record)
1664 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001665 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001666 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001667 is_timeshift = DVR_FALSE;
1668 } else {
1669 is_timeshift = DVR_TRUE;
1670 }
1671 }
1672
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001673 ctx = ctx_getPlayback((unsigned long)playback);
1674 DVR_RETURN_IF_FALSE(ctx);
1675
Gong Kefdb31922022-06-17 17:11:16 +08001676 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001677
Wentao MA96f68962022-06-15 19:45:35 +08001678 DVR_WRAPPER_INFO("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",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001679 ctx->sn,
1680 ctx->playback.param_open.location,
1681 flags,
1682 p_pids->video.pid, p_pids->video.format,
1683 p_pids->audio.pid, p_pids->audio.format,
1684 p_pids->ad.pid, p_pids->ad.format,
1685 p_pids->subtitle.pid, p_pids->subtitle.format,
1686 p_pids->pcr.pid);
1687
Gong Kefdb31922022-06-17 17:11:16 +08001688 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001689
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001690 if (ctx->playback.param_open.is_timeshift) {
1691 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001692 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001693 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001694 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001695 return DVR_FAILURE;
1696 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001697 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001698 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001699 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001700 }
1701
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001702 /*obtain all segments in a list*/
1703 segment_nb = 0;
1704 p_segment_ids = NULL;
1705 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001706 if (!error) {
1707 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001708 struct list_head info_list; /**< segment list head*/
1709 INIT_LIST_HEAD(&info_list);
1710
Wentao MA96f68962022-06-15 19:45:35 +08001711 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001712 //we need free info list buf when we used end.
1713 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001714 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001715 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001716 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1717 ctx->playback.param_open.location, error);
hualing chenb9a02922021-12-14 11:29:47 +08001718 for (i = 0; i < segment_nb; i++) {
1719 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001720 memset((void*)&seg_info,0,sizeof(seg_info));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001721
hualing chenb9a02922021-12-14 11:29:47 +08001722 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1723 if (error) {
1724 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001725 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001726 ctx->playback.param_open.location, p_segment_ids[i], error);
1727 break;
1728 }
1729 //add check if has audio or video pid. if not exist. not add segment to playback
1730 int ii = 0;
1731 int has_av = 0;
1732 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1733 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1734 if (type == DVR_STREAM_TYPE_VIDEO ||
1735 type == DVR_STREAM_TYPE_AUDIO ||
1736 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001737 DVR_WRAPPER_INFO("success to get seg av info \n");
1738 DVR_WRAPPER_INFO("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,
hualing chenb9a02922021-12-14 11:29:47 +08001739 DVR_STREAM_TYPE_VIDEO,
1740 DVR_STREAM_TYPE_AUDIO,
1741 DVR_STREAM_TYPE_AD);
1742 has_av = 1;
1743 //break;
1744 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001745 DVR_WRAPPER_INFO("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,
hualing chenb9a02922021-12-14 11:29:47 +08001746 DVR_STREAM_TYPE_VIDEO,
1747 DVR_STREAM_TYPE_AUDIO,
1748 DVR_STREAM_TYPE_AD);
1749 }
1750 }
1751 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001752 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001753 continue;
1754 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001755 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001756 }
Wentao MA9628de82022-09-13 14:19:37 +08001757 segflags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1758 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, segflags);
hualing chenb9a02922021-12-14 11:29:47 +08001759 if (error)
1760 break;
1761 /*copy the 1st segment*/
1762 if (got_1st_seg == 0) {
1763 seg_info_1st = seg_info;
1764 got_1st_seg = 1;
1765 }
1766 }
1767 } else {
1768 for (i = 0; i < segment_nb; i++) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001769 DVR_RecordSegmentInfo_t *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001770 int found = 0;
Wentao MA4d85ff32022-09-23 11:36:18 +08001771 list_for_each_entry(p_seg_info, &info_list, head)
hualing chenb9a02922021-12-14 11:29:47 +08001772 {
Wentao MA4d85ff32022-09-23 11:36:18 +08001773 if (p_seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001774 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001775 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001776 break;
1777 }
1778 }
1779 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001780 //last info is not found if when recording occured power off.
1781 if (p_segment_ids[i] == segment_nb - 1) {
1782 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001783 memset((void*)&seg_info,0,sizeof(seg_info));
hualing chen8aed9582021-12-24 17:59:56 +08001784
1785 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1786 if (error) {
1787 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001788 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001789 ctx->playback.param_open.location, p_segment_ids[i], error);
1790 break;
1791 }
1792 //
1793 //add check if has audio or video pid. if not exist. not add segment to playback
1794 int ii = 0;
1795 int has_av = 0;
1796 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1797 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1798 if (type == DVR_STREAM_TYPE_VIDEO ||
1799 type == DVR_STREAM_TYPE_AUDIO ||
1800 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001801 DVR_WRAPPER_INFO("success to get seg av info \n");
1802 DVR_WRAPPER_INFO("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,
hualing chen8aed9582021-12-24 17:59:56 +08001803 DVR_STREAM_TYPE_VIDEO,
1804 DVR_STREAM_TYPE_AUDIO,
1805 DVR_STREAM_TYPE_AD);
1806 has_av = 1;
1807 //break;
1808 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001809 DVR_WRAPPER_INFO("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,
hualing chen8aed9582021-12-24 17:59:56 +08001810 DVR_STREAM_TYPE_VIDEO,
1811 DVR_STREAM_TYPE_AUDIO,
1812 DVR_STREAM_TYPE_AD);
1813 }
1814 }
1815 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001816 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001817 continue;
1818 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001819 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001820 }
Wentao MA9628de82022-09-13 14:19:37 +08001821 segflags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1822 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, segflags);
hualing chen8aed9582021-12-24 17:59:56 +08001823 if (error)
1824 break;
1825 }
hualing chenb9a02922021-12-14 11:29:47 +08001826 continue;
1827 }
1828
1829 //add check if has audio or video pid. if not exist. not add segment to playback
1830 int ii = 0;
1831 int has_av = 0;
Wentao MA4d85ff32022-09-23 11:36:18 +08001832 for (ii = 0; ii < p_seg_info->nb_pids; ii++) {
1833 int type = (p_seg_info->pids[ii].type >> 24) & 0x0f;
hualing chenb9a02922021-12-14 11:29:47 +08001834 if (type == DVR_STREAM_TYPE_VIDEO ||
1835 type == DVR_STREAM_TYPE_AUDIO ||
1836 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001837 DVR_WRAPPER_INFO("success to get seg av info \n");
Wentao MA4d85ff32022-09-23 11:36:18 +08001838 DVR_WRAPPER_INFO("success to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(p_seg_info->pids[ii].type >> 24)&0x0f,p_seg_info->pids[ii].pid,
hualing chenb9a02922021-12-14 11:29:47 +08001839 DVR_STREAM_TYPE_VIDEO,
1840 DVR_STREAM_TYPE_AUDIO,
1841 DVR_STREAM_TYPE_AD);
1842 has_av = 1;
1843 //break;
1844 } else {
Wentao MA4d85ff32022-09-23 11:36:18 +08001845 DVR_WRAPPER_INFO("error to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(p_seg_info->pids[ii].type >> 24)&0x0f,p_seg_info->pids[ii].pid,
hualing chenb9a02922021-12-14 11:29:47 +08001846 DVR_STREAM_TYPE_VIDEO,
1847 DVR_STREAM_TYPE_AUDIO,
1848 DVR_STREAM_TYPE_AD);
1849 }
1850 }
1851 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001852 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001853 continue;
1854 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001855 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001856 }
Wentao MA9628de82022-09-13 14:19:37 +08001857 segflags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Wentao MA4d85ff32022-09-23 11:36:18 +08001858 error = wrapper_addPlaybackSegment(ctx, p_seg_info, p_pids, segflags);
hualing chenb9a02922021-12-14 11:29:47 +08001859 if (error)
1860 break;
1861
1862 /*copy the 1st segment*/
1863 if (got_1st_seg == 0) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001864 seg_info_1st = *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001865 got_1st_seg = 1;
1866 }
hualing chen92f3a142020-07-08 20:59:33 +08001867 }
hualing chenb9a02922021-12-14 11:29:47 +08001868 //free list
1869 DVR_RecordSegmentInfo_t *segment = NULL;
1870 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1871 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1872 {
1873 if (segment) {
1874 list_del(&segment->head);
1875 free(segment);
1876 }
1877 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001878 }
hualing chenb9a02922021-12-14 11:29:47 +08001879
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001880 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001881
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001882 /* return if no segment or fail to add */
1883 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001884
Wentao MA270dc0f2022-08-23 13:17:26 +08001885 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001886 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1887 ctx->playback.obsolete = ctx_record->record.obsolete;
1888 }
1889
Wentao MA96f68962022-06-15 19:45:35 +08001890 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001891
1892 ctx->playback.reach_end = DVR_FALSE;
1893 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1894 ctx->playback.speed = 0.0f;
1895 else
1896 ctx->playback.speed = 100.0f;
1897
1898 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001899 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001900 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001901 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001902 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001903 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001904 error = dvr_playback_start(ctx->playback.player, flags);
1905 } else {
1906 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1907 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001908 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001909 ctx->sn, seg_info_1st.id, error);
1910 }
Wentao MA96f68962022-06-15 19:45:35 +08001911 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001912 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001913 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)got_1st_seg:%d\n", ctx->sn, error, got_1st_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001914 }
1915 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001916
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001917 if (ctx->playback.param_open.is_timeshift) {
1918 /*unlock the recorder locked above*/
1919 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001920 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001921 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001922 ctx->sn, ctx_record->sn);
1923 }
1924 }
Gong Kefdb31922022-06-17 17:11:16 +08001925 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001926
1927 return error;
1928}
hualing chen002e5b92022-02-23 17:51:21 +08001929//stop record and playback
1930int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1931{
1932 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1933 int error;
Wentao MA96f68962022-06-15 19:45:35 +08001934 DVR_WRAPPER_INFO("stop timeshift record\n");
hualing chen002e5b92022-02-23 17:51:21 +08001935
1936 //stop timeshift record
1937 ctx_record = ctx_getRecord(sn_timeshift_record);
1938 error = dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
1939
Wentao MA96f68962022-06-15 19:45:35 +08001940 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08001941 //stop play
1942 error = dvr_wrapper_stop_playback(playback);
1943 //del timeshift file
1944 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001945 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08001946 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
1947 }
1948 return error;
1949}
1950//start record and start playback
1951int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1952{
1953 DVR_WrapperCtx_t *ctx;
1954 DVR_RecordStartParams_t *start_param;
1955 int error;
1956
1957 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
1958 DVR_RETURN_IF_FALSE(ctx);
1959
Gong Kefdb31922022-06-17 17:11:16 +08001960 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001961 DVR_WRAPPER_INFO("restart record(sn:%ld, location:%s)...\n", ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08001962 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08001963
hualing chen451c8f72022-03-09 13:05:52 +08001964 {
1965 //clear old record status
1966 // struct {
1967 // DVR_WrapperRecordOpenParams_t param_open;
1968 // DVR_RecordStartParams_t param_start;
1969 // DVR_RecordStartParams_t param_update;
1970 // DVR_RecordHandle_t recorder;
1971 // DVR_RecordEventFunction_t event_fn;
1972 // void *event_userdata;
1973
1974 // /*total status = seg_status + status + obsolete*/
1975 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
1976 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
1977 // uint64_t next_segment_id;
1978
1979 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
1980 // } record;
1981 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
1982 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
1983 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
1984 }
1985
hualing chen002e5b92022-02-23 17:51:21 +08001986 start_param = &ctx->record.param_start;
1987
1988 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1989 {
1990 DVR_RecordSegmentInfo_t new_seg_info =
1991 { .id = start_param->segment.segment_id, };
1992 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08001993 DVR_WRAPPER_INFO("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);
hualing chen451c8f72022-03-09 13:05:52 +08001994 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
1995 DVR_RecordStartParams_t *update_param;
1996 update_param = &ctx->record.param_update;
1997 memcpy(update_param, start_param, sizeof(*update_param));
1998 int i = 0;
1999 for (i = 0; i < update_param->segment.nb_pids; i++)
2000 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08002001 }
2002
Wentao MA96f68962022-06-15 19:45:35 +08002003 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08002004
Gong Kefdb31922022-06-17 17:11:16 +08002005 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002006
2007 //start play
Wentao MA96f68962022-06-15 19:45:35 +08002008 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08002009 //clear play statue
2010 ctx = ctx_getPlayback((unsigned long)playback);
2011 if (ctx) {
Wentao MA4d85ff32022-09-23 11:36:18 +08002012 //reset old playback status
hualing chen451c8f72022-03-09 13:05:52 +08002013 // struct {
2014 // DVR_WrapperPlaybackOpenParams_t param_open;
2015 // DVR_PlaybackHandle_t player;
2016 // DVR_PlaybackEventFunction_t event_fn;
2017 // void *event_userdata;
2018
2019 // /*total status = seg_status + status*/
2020 // DVR_PlaybackStatus_t seg_status;
2021 // DVR_WrapperPlaybackStatus_t status;
2022 // DVR_PlaybackPids_t pids_req;
2023 // DVR_PlaybackEvent_t last_event;
2024 // float speed;
2025 // DVR_Bool_t reach_end;
2026
2027 // DVR_WrapperInfo_t obsolete;
2028 // DVR_Bool_t tf_full;
2029 // } playback;
Wentao MA4d85ff32022-09-23 11:36:18 +08002030 ctx->playback.tf_full = DVR_FALSE;
2031 ctx->playback.reach_end = DVR_FALSE;
hualing chen451c8f72022-03-09 13:05:52 +08002032 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2033 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2034 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2035 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2036 }
hualing chen002e5b92022-02-23 17:51:21 +08002037 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2038 return error;
2039}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002040
2041int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2042{
2043 DVR_WrapperCtx_t *ctx;
2044 int error;
2045
2046 DVR_RETURN_IF_FALSE(playback);
2047
2048 ctx = ctx_getPlayback((unsigned long)playback);
2049 DVR_RETURN_IF_FALSE(ctx);
2050
Gong Kefdb31922022-06-17 17:11:16 +08002051 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002052 DVR_WRAPPER_INFO("stop playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002053 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002054
2055 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2056
2057 {
2058 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002059 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002060
Wentao MA270dc0f2022-08-23 13:17:26 +08002061 list_for_each_entry(p_seg, &ctx->segments, head) {
2062 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002063 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002064 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002065 }
2066 ctx_freeSegments(ctx);
2067 }
2068
Wentao MA96f68962022-06-15 19:45:35 +08002069 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002070 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002071
2072 return error;
2073}
2074
2075int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2076{
2077 DVR_WrapperCtx_t *ctx;
2078 int error;
2079
2080 DVR_RETURN_IF_FALSE(playback);
2081
2082 ctx = ctx_getPlayback((unsigned long)playback);
2083 DVR_RETURN_IF_FALSE(ctx);
2084
Gong Kefdb31922022-06-17 17:11:16 +08002085 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002086 DVR_WRAPPER_INFO("pause playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002087 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002088 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002089 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002090 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002091
2092 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2093
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002094 ctx->playback.speed = 0.0f;
2095
Wentao MA96f68962022-06-15 19:45:35 +08002096 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002097 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002098
2099 return error;
2100}
2101
2102int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2103{
2104 DVR_WrapperCtx_t *ctx;
2105 int error;
2106
2107 DVR_RETURN_IF_FALSE(playback);
2108
2109 ctx = ctx_getPlayback((unsigned long)playback);
2110 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002111 //if set limit.we need check if seek to valid data when resume
2112 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2113 if (dvr_playback_check_limit(ctx->playback.player)) {
2114 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2115 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002116 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002117 ctx->sn, time_offset, expired);
2118 time_offset = expired;
2119 dvr_wrapper_seek_playback(playback, time_offset);
2120 }
2121 }
Gong Kefdb31922022-06-17 17:11:16 +08002122 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002123 DVR_WRAPPER_INFO("resume playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002124 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002125
2126 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002127 ctx->playback.speed = 100.0f;
2128
Wentao MA96f68962022-06-15 19:45:35 +08002129 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002130 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002131
2132 return error;
2133}
2134
2135int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2136{
2137 DVR_WrapperCtx_t *ctx;
2138 int error;
2139 DVR_PlaybackSpeed_t dvr_speed = {
2140 .speed = { speed },
2141 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2142 };
2143
2144 DVR_RETURN_IF_FALSE(playback);
2145
2146 ctx = ctx_getPlayback((unsigned long)playback);
2147 DVR_RETURN_IF_FALSE(ctx);
2148
Gong Kefdb31922022-06-17 17:11:16 +08002149 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002150 DVR_WRAPPER_INFO("speed playback(sn:%ld) (x%f) .(x%f)..\n", ctx->sn, speed, ctx->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08002151 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002152
2153 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2154
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002155 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2156 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2157 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002158 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002159 error = dvr_playback_resume(ctx->playback.player);
2160 } else if (ctx->playback.speed == 0.0f
2161 && speed != 0.0f
2162 && speed != 100.0f) {
2163 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002164 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002165 error = dvr_playback_resume(ctx->playback.player);
2166 }
2167
2168 ctx->playback.speed = speed;
2169
Wentao MA96f68962022-06-15 19:45:35 +08002170 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002171 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002172 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002173
2174 return error;
2175}
2176
hualing chen03fd4942021-07-15 15:56:41 +08002177int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2178{
2179 DVR_WrapperCtx_t *ctx;
2180 int error;
2181
2182 DVR_RETURN_IF_FALSE(playback);
2183
2184 ctx = ctx_getPlayback((unsigned long)playback);
2185 DVR_RETURN_IF_FALSE(ctx);
2186
Gong Kefdb31922022-06-17 17:11:16 +08002187 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002188
Wentao MAe8ba5172022-08-09 11:18:17 +08002189 DVR_WRAPPER_INFO("set_limit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002190 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002191
2192 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002193 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002194
Gong Kefdb31922022-06-17 17:11:16 +08002195 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002196
2197 return error;
2198}
2199
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002200int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2201{
2202 DVR_WrapperCtx_t *ctx;
2203 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002204 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002205 uint64_t segment_id;
2206 uint32_t off;
2207 uint64_t last_segment_id;
2208 uint32_t pre_off;
2209
2210 DVR_RETURN_IF_FALSE(playback);
2211
2212 ctx = ctx_getPlayback((unsigned long)playback);
2213 DVR_RETURN_IF_FALSE(ctx);
2214
Gong Kefdb31922022-06-17 17:11:16 +08002215 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002216
Wentao MA96f68962022-06-15 19:45:35 +08002217 DVR_WRAPPER_INFO("seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002218 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002219
2220 off = 0;
2221 segment_id = 0;
2222 pre_off = 0;
2223 last_segment_id = 0;
2224
hualing chen03fd4942021-07-15 15:56:41 +08002225 //if set limit info we need check ts data is
2226 //expired when seek
2227 if (dvr_playback_check_limit(ctx->playback.player)) {
2228 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2229 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002230 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002231 ctx->sn, time_offset, expired);
2232 time_offset = expired;
2233 }
2234 }
2235
Wentao MA270dc0f2022-08-23 13:17:26 +08002236 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2237 segment_id = p_seg->seg_info.id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002238
Wentao MA270dc0f2022-08-23 13:17:26 +08002239 if ((ctx->playback.obsolete.time + pre_off + p_seg->seg_info.duration) > time_offset)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002240 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002241
Wentao MA270dc0f2022-08-23 13:17:26 +08002242 last_segment_id = p_seg->seg_info.id;
2243 pre_off += p_seg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002244 }
2245
2246 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002247 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2248 off = time_offset;
2249 } else if (ctx->playback.obsolete.time >= time_offset) {
2250 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002251 } else {
hualing chenda76fc52020-05-28 14:56:42 +08002252 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002253 }
2254
Wentao MA96f68962022-06-15 19:45:35 +08002255 DVR_WRAPPER_INFO("seek playback(sn:%ld) (seg:%lld, off:%d)\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002256 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002257 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
Wentao MA96f68962022-06-15 19:45:35 +08002258 DVR_WRAPPER_INFO("playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002259
Gong Kefdb31922022-06-17 17:11:16 +08002260 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002261
2262 return error;
2263}
2264
2265int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2266{
2267 DVR_WrapperCtx_t *ctx;
2268 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002269 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002270
2271 DVR_RETURN_IF_FALSE(playback);
2272 DVR_RETURN_IF_FALSE(p_pids);
2273
2274 ctx = ctx_getPlayback((unsigned long)playback);
2275 DVR_RETURN_IF_FALSE(ctx);
2276
Gong Kefdb31922022-06-17 17:11:16 +08002277 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002278
Wentao MA96f68962022-06-15 19:45:35 +08002279 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002280 ctx->sn,
2281 p_pids->video.pid, p_pids->video.format,
2282 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002283 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002284
2285 ctx->playback.pids_req = *p_pids;
2286
2287 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +08002288 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002289 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002290 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2291 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2292 /*check update for pids*/
2293 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2294 p_seg->playback_info.pids = *p_pids;
2295 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002296 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002297 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002298 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002299 /*do not break, let list updated*/
2300 }
2301 }
2302 }
2303 /*break;*/
2304 }
2305 }
2306
Wentao MA96f68962022-06-15 19:45:35 +08002307 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002308 ctx->sn,
2309 p_pids->video.pid, p_pids->video.format,
2310 p_pids->audio.pid, p_pids->audio.format,
2311 error);
2312
Gong Kefdb31922022-06-17 17:11:16 +08002313 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002314
2315 return error;
2316}
2317
hualing chena5f03222021-12-02 11:22:35 +08002318int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2319{
2320 DVR_WrapperCtx_t *ctx;
2321 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002322 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002323
2324 DVR_RETURN_IF_FALSE(playback);
2325 DVR_RETURN_IF_FALSE(p_pids);
2326
2327 ctx = ctx_getPlayback((unsigned long)playback);
2328 DVR_RETURN_IF_FALSE(ctx);
2329
Gong Kefdb31922022-06-17 17:11:16 +08002330 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002331
Wentao MA96f68962022-06-15 19:45:35 +08002332 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
hualing chena5f03222021-12-02 11:22:35 +08002333 ctx->sn,
2334 p_pids->video.pid, p_pids->video.format,
2335 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002336 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002337
2338 ctx->playback.pids_req = *p_pids;
2339
2340 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +08002341 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002342 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002343 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2344 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2345 /*check update for pids*/
2346 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2347 p_seg->playback_info.pids = *p_pids;
2348 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002349 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002350 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002351 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002352 /*do not break, let list updated*/
2353 }
2354 }
2355 }
2356 /*break;*/
2357 }
2358 }
2359
Wentao MA96f68962022-06-15 19:45:35 +08002360 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002361 ctx->sn,
2362 p_pids->video.pid, p_pids->video.format,
2363 p_pids->audio.pid, p_pids->audio.format,
2364 error);
2365
Gong Kefdb31922022-06-17 17:11:16 +08002366 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002367
2368 return error;
2369}
2370
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002371int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2372{
2373 DVR_WrapperCtx_t *ctx;
2374 DVR_WrapperPlaybackStatus_t s;
2375 DVR_PlaybackStatus_t play_status;
2376 int error;
2377
2378 DVR_RETURN_IF_FALSE(playback);
2379 DVR_RETURN_IF_FALSE(status);
2380
2381 ctx = ctx_getPlayback((unsigned long)playback);
2382 DVR_RETURN_IF_FALSE(ctx);
2383
Gong Kefdb31922022-06-17 17:11:16 +08002384 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002385
Wentao MA96f68962022-06-15 19:45:35 +08002386 DVR_WRAPPER_INFO("get playback(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002387 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002388
2389 error = dvr_playback_get_status(ctx->playback.player, &play_status);
Wentao MA96f68962022-06-15 19:45:35 +08002390 DVR_WRAPPER_INFO("playback(sn:%ld) get status (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002391
2392 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002393 error = process_generatePlaybackStatus(ctx, &s);
2394
hualing chenb5cd42e2020-04-15 17:03:34 +08002395 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2396 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002397 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002398 s.info_cur.time = s.info_full.time;
2399 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002400 DVR_WRAPPER_INFO("playback(sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002401 ctx->sn,
2402 s.state,
2403 s.info_cur.time,
2404 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002405 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002406 error);
2407
2408 *status = s;
2409
Gong Kefdb31922022-06-17 17:11:16 +08002410 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002411
2412 return error;
2413}
2414
hualing chen266b9502020-04-04 17:39:39 +08002415int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2416{
2417 DVR_WrapperCtx_t *ctx;
2418 int error;
2419
2420 DVR_RETURN_IF_FALSE(playback);
2421 DVR_RETURN_IF_FALSE(p_secure_buf);
2422
2423 ctx = ctx_getPlayback((unsigned long)playback);
2424 DVR_RETURN_IF_FALSE(ctx);
2425
Gong Kefdb31922022-06-17 17:11:16 +08002426 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002427 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002428 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002429 return error;
2430}
2431
2432int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2433{
2434 DVR_WrapperCtx_t *ctx;
2435 int error;
2436
2437 DVR_RETURN_IF_FALSE(playback);
2438 DVR_RETURN_IF_FALSE(func);
2439
2440 ctx = ctx_getPlayback((unsigned long)playback);
2441 DVR_RETURN_IF_FALSE(ctx);
2442
Gong Kefdb31922022-06-17 17:11:16 +08002443 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002444 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002445 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002446 return error;
2447}
2448
Zhiqiang Han620b9252021-11-09 14:23:20 +08002449int dvr_wrapper_segment_del_by_location (const char *location)
2450{
2451 char fpath[DVR_MAX_LOCATION_SIZE];
2452
2453 DVR_RETURN_IF_FALSE(location);
2454
2455 /*del the stats file*/
2456 sprintf(fpath, "%s.stats", location);
2457 unlink(fpath);
2458
2459 return dvr_segment_del_by_location(location);
2460}
2461
2462int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2463{
2464 FILE *fp;
2465 char fpath[DVR_MAX_LOCATION_SIZE];
2466
2467 DVR_RETURN_IF_FALSE(location);
2468 DVR_RETURN_IF_FALSE(p_info);
2469
2470 if (p_info)
2471 memset(p_info, 0, sizeof(p_info[0]));
2472
2473 memset(fpath, 0, sizeof(fpath));
2474 sprintf(fpath, "%s.stats", location);
2475
2476 /*stats file exists*/
2477 if ((fp = fopen(fpath, "r"))) {
2478 char buf[256];
2479
2480 if (fgets(buf, sizeof(buf), fp) != NULL
2481 && (sscanf(buf, ":%llu:%lu:%u",
2482 &p_info->size,
2483 &p_info->time,
2484 &p_info->pkts) == 3)) {
2485 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002486 DVR_WRAPPER_INFO("rec(%s) t/s/p:(%lu/%llu/%u)\n", location, p_info->time, p_info->size, p_info->pkts);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002487 return DVR_SUCCESS;
2488 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002489 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002490 }
2491
2492 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002493 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002494
2495 int error;
2496 uint32_t n_ids;
2497 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002498
hualing chen8aed9582021-12-24 17:59:56 +08002499 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002500
Zhiqiang Han620b9252021-11-09 14:23:20 +08002501 if (!error) {
2502 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002503 struct list_head info_list; /**< segment list head*/
2504 INIT_LIST_HEAD(&info_list);
2505
2506 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002507 error = dvr_segment_get_allInfo(location, &info_list);
2508 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08002509 DVR_RecordSegmentInfo_t info;
2510
2511 memset(&info, 0, sizeof(info));
2512 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002513 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002514 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002515
2516 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002517 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002518 if (!error) {
2519 p_info->size += info.size;
2520 p_info->time += info.duration;
2521 p_info->pkts += info.nb_packets;
2522 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002523 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002524 break;
2525 }
2526 }
2527 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002528 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002529 for (i = 0; i < n_ids; i++) {
2530
2531 DVR_RecordSegmentInfo_t *seg_info;
2532 DVR_PlaybackSegmentFlag_t flags;
2533 int found = 0;
2534 list_for_each_entry(seg_info, &info_list, head)
2535 {
hualing chen8aed9582021-12-24 17:59:56 +08002536 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002537 found = 1;
2538 break;
2539 }
2540 }
2541 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002542 DVR_WRAPPER_INFO("get segment info::%d [%d]n_ids[%d]error", i, p_ids[i], n_ids);
hualing chen8aed9582021-12-24 17:59:56 +08002543 if (p_ids[i] == n_ids - 1) {
2544 DVR_RecordSegmentInfo_t info;
Wentao MA96f68962022-06-15 19:45:35 +08002545 DVR_WRAPPER_INFO("get last segment info::%d [%d]n_ids[%d] from subfile", i, p_ids[i], n_ids);
hualing chen8aed9582021-12-24 17:59:56 +08002546 error = dvr_segment_get_info(location, p_ids[i], &info);
2547 if (!error) {
2548 p_info->size += info.size;
2549 p_info->time += info.duration;
2550 p_info->pkts += info.nb_packets;
2551 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002552 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002553 break;
2554 }
2555 }
hualing chenb9a02922021-12-14 11:29:47 +08002556 continue;
2557 }
2558
2559 if (!error) {
2560 p_info->size += seg_info->size;
2561 p_info->time += seg_info->duration;
2562 p_info->pkts += seg_info->nb_packets;
2563 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002564 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002565 break;
2566 }
2567 }
2568 //free list
2569 DVR_RecordSegmentInfo_t *segment = NULL;
2570 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2571 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2572 {
2573 if (segment) {
2574 list_del(&segment->head);
2575 free(segment);
2576 }
2577 }
2578 }
2579 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002580 } else {
2581 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002582 }
Wentao MA96f68962022-06-15 19:45:35 +08002583 DVR_WRAPPER_INFO("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 Han620b9252021-11-09 14:23:20 +08002584
2585 return (error)? DVR_FAILURE : DVR_SUCCESS;
2586}
2587
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002588static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2589{
2590 DVR_WrapperEventCtx_t evt;
2591
2592 DVR_RETURN_IF_FALSE(userdata);
2593
2594 evt.sn = (unsigned long)userdata;
2595 evt.type = W_REC;
2596 evt.record.event = event;
2597 evt.record.status = *(DVR_RecordStatus_t *)params;
Wentao MA96f68962022-06-15 19:45:35 +08002598 DVR_WRAPPER_INFO("evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002599 return ctx_addRecordEvent(&evt);
2600}
2601
2602static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2603{
2604 DVR_WrapperEventCtx_t evt;
2605
2606 DVR_RETURN_IF_FALSE(userdata);
2607
2608 evt.sn = (unsigned long)userdata;
2609 evt.type = W_PLAYBACK;
2610 evt.playback.event = event;
2611 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA9a164002022-08-29 11:20:24 +08002612 DVR_WRAPPER_INFO("evt[sn:%ld, playback, evt:0x%x]\n", evt.sn, evt.playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002613 return ctx_addPlaybackEvent(&evt);
2614}
2615
2616static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2617{
Wentao MA270dc0f2022-08-23 13:17:26 +08002618 DVR_WRAPPER_INFO("notify(sn:%ld) evt(0x%x) statistic:time/size/pkts(%ld/%lld/%u) obsolete:(%ld/%llu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002619 ctx->sn,
2620 evt,
2621 status->info.time,
2622 status->info.size,
2623 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002624 status->info_obsolete.time,
2625 status->info_obsolete.size,
2626 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002627
2628 if (ctx->record.event_fn)
2629 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2630 return 0;
2631}
2632
Zhiqiang Han620b9252021-11-09 14:23:20 +08002633static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2634{
2635 FILE *fp;
2636 char fpath[DVR_MAX_LOCATION_SIZE];
2637
2638 DVR_RETURN_IF_FALSE(location);
2639 DVR_RETURN_IF_FALSE(p_status);
2640
2641 sprintf(fpath, "%s.stats", location);
2642
2643 /*stats file*/
2644 if ((fp = fopen(fpath, "w"))) {
2645 char buf[256];
2646 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2647 p_status->info.size - p_status->info_obsolete.size,
2648 p_status->info.time - p_status->info_obsolete.time,
2649 p_status->info.pkts - p_status->info_obsolete.pkts);
2650 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002651 fflush(fp);
2652 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002653 fclose(fp);
2654 return DVR_SUCCESS;
2655 }
2656
2657 return DVR_FAILURE;
2658}
2659
2660
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002661static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2662{
2663 DVR_RecordStartParams_t param;
2664 DVR_RecordSegmentInfo_t seg_info;
2665 int i;
2666 int error;
2667
2668 memcpy(&param, &ctx->record.param_update, sizeof(param));
2669 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2670 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2671 for (i = 0; i < param.segment.nb_pids; i++) {
2672 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2673 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2674 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2675 ctx->record.param_update.segment.nb_pids++;
2676 }
2677 }
2678 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2679 {
2680 DVR_RecordSegmentInfo_t new_seg_info =
2681 { .id = ctx->record.param_update.segment.segment_id, };
2682 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2683 wrapper_addRecordSegment(ctx, &new_seg_info);
2684 }
2685
Wentao MA96f68962022-06-15 19:45:35 +08002686 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002687 return error;
2688}
2689
Wentao MA270dc0f2022-08-23 13:17:26 +08002690static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002691{
Wentao MA270dc0f2022-08-23 13:17:26 +08002692 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002693}
2694
2695/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002696static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002697{
2698 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002699 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002700
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002701 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002702 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2703
2704 ctx->record.status.state = ctx->record.seg_status.state;
2705 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2706 memcpy(ctx->record.status.pids.pids,
2707 ctx->record.seg_status.info.pids,
2708 sizeof(ctx->record.status.pids.pids));
2709 ctx->current_segment_id = ctx->record.seg_status.info.id;
2710
Wentao MA270dc0f2022-08-23 13:17:26 +08002711 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2712 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2713 ctx->record.status.info.time += p_seg->info.duration;
2714 ctx->record.status.info.size += p_seg->info.size;
2715 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002716 }
2717 }
2718
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002719 ctx->record.status.info_obsolete = ctx->record.obsolete;
2720
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002721 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002722
2723 if (status) {
2724 *status = ctx->record.status;
2725 status->info.time += ctx->record.seg_status.info.duration;
2726 status->info.size += ctx->record.seg_status.info.size;
2727 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2728 }
2729
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002730 return DVR_SUCCESS;
2731}
2732
2733
2734static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2735{
2736 DVR_WrapperRecordStatus_t status;
2737
2738 memset(&status, 0, sizeof(status));
2739
Wentao MA96f68962022-06-15 19:45:35 +08002740 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002741 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002742 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002743 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002744 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002745 return 0;
2746 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002747 switch (evt->record.event)
2748 {
2749 case DVR_RECORD_EVENT_STATUS:
2750 {
2751 switch (evt->record.status.state)
2752 {
2753 case DVR_RECORD_STATE_OPENED:
2754 case DVR_RECORD_STATE_CLOSED:
2755 {
2756 ctx->record.seg_status = evt->record.status;
2757
2758 status.state = evt->record.status.state;
2759 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002760 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002761 } break;
2762 case DVR_RECORD_STATE_STARTED:
2763 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002764 ctx->record.seg_status = evt->record.status;
2765
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002766 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002767 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002768 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002769
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002770 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002771 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002772 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002773 DVR_WRAPPER_INFO("start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002774 ctx->sn,
2775 evt->record.status.info.size,
2776 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002777 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2778 /*should notify the recording's stop*/
2779 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002780 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002781 ctx->sn, error);
2782 status.state = DVR_RECORD_STATE_CLOSED;
2783 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002784 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002785 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002786 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002787
2788 if (ctx->record.param_open.is_timeshift
2789 && ctx->record.param_open.max_time
2790 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002791 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002792
2793 /*as the player do not support null playlist,
2794 there must be one segment existed at any time,
2795 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002796 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2797 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002798 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002799 DVR_WRAPPER_INFO("warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002800 status.info.size,
2801 ctx->record.param_open.max_time,
2802 ctx->record.param_open.segment_size);
2803 } else {
2804 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002805 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002806
2807 process_generateRecordStatus(ctx, &status);
2808 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002809 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002810 }
2811 }
2812
Wentao MAf4072032022-06-30 13:50:45 +08002813 const loff_t actual_size = status.info.size;
2814 const loff_t max_size = ctx->record.param_open.max_size;
2815 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002816
Wentao MAf4072032022-06-30 13:50:45 +08002817 if (ctx->record.param_open.is_timeshift && max_size) {
2818 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002819 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002820 /*as the player do not support null playlist,
2821 there must be one segment existed at any time,
2822 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002823 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2824 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002825 /*only one segment, waiting for more*/
2826 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2827 actual_size, segment_size);
2828 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002829 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002830
2831 process_generateRecordStatus(ctx, &status);
2832 process_notifyRecord(ctx, evt->record.event, &status);
2833 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2834 }
2835 if (actual_size >= max_size + segment_size/2) {
2836 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2837 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002838 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002839 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002840 }
2841 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002842 } break;
2843 case DVR_RECORD_STATE_STOPPED:
2844 {
2845 ctx->record.seg_status = evt->record.status;
2846
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002847 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002848 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002849 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002850 } break;
2851 default:
2852 break;
2853 }
2854 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002855 case DVR_RECORD_EVENT_WRITE_ERROR: {
2856 ctx->record.seg_status = evt->record.status;
2857 status.state = evt->record.status.state;
2858 process_notifyRecord(ctx, evt->record.event, &status);
2859 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002860 default:
2861 break;
2862 }
2863 return DVR_SUCCESS;
2864}
2865
2866static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2867{
Wentao MA270dc0f2022-08-23 13:17:26 +08002868 DVR_WRAPPER_INFO("notify(sn:%ld) evt(0x%x) statistics:state/cur/full/obsolete(%d/%ld/%ld/%ld)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002869 ctx->sn,
2870 evt,
2871 status->state,
2872 status->info_cur.time,
2873 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002874 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002875
2876 if (ctx->playback.event_fn)
2877 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
2878 return 0;
2879}
2880
2881/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002882static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002883{
2884 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002885 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002886
2887 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2888 ctx->playback.status.pids = ctx->playback.pids_req;
2889
2890 ctx->playback.status.state = ctx->playback.seg_status.state;
2891 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2892 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2893 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2894
Wentao MA270dc0f2022-08-23 13:17:26 +08002895 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2896 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
2897 DVR_WRAPPER_INFO("calculate cur time :[%lld]time[%d]\n", p_seg->seg_info.id, p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002898 break;
hualing chen451c8f72022-03-09 13:05:52 +08002899 }
2900
Wentao MA270dc0f2022-08-23 13:17:26 +08002901 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
2902 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
2903 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002904 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002905 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2906 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
2907 ctx->playback.status.info_full.size += p_seg->seg_info.size;
2908 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
2909 DVR_WRAPPER_INFO("calculate full time :[%lld]time[%d]\n", p_seg->seg_info.id, p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002910 }
2911
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002912 if (status) {
2913 *status = ctx->playback.status;
2914 /*deal with current, lack size and pkts with the current*/
2915 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08002916 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08002917 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08002918
Wentao MA270dc0f2022-08-23 13:17:26 +08002919 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2920 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08002921 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
2922 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08002923 DVR_WRAPPER_INFO("warning change start time :id[%lld] [%d]cur[%d]\n", p_seg->info.id, status->info_obsolete.time, status->info_cur.time);
hualing chen56c0a162022-01-27 17:01:50 +08002924 }
2925 else
2926 {
Wentao MA270dc0f2022-08-23 13:17:26 +08002927 DVR_WRAPPER_INFO("warning not change start time :ctx->playback.tf_full[%d]id[%lld] [%lld] cur[%d]\n",ctx->playback.tf_full , p_seg->info.id, ctx->current_segment_id, status->info_cur.time);
hualing chen56c0a162022-01-27 17:01:50 +08002928 status->info_obsolete.time = ctx->playback.obsolete.time;
2929 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
2930 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002931 }
2932
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002933 return DVR_SUCCESS;
2934}
2935
2936static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2937{
Wentao MA96f68962022-06-15 19:45:35 +08002938 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002939 evt->sn, evt->playback.event,
2940 evt->playback.status.play_status.state,
2941 evt->playback.status.play_status.segment_id,
2942 evt->playback.status.play_status.time_cur,
2943 evt->playback.status.play_status.time_end);
2944
2945 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002946 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2947 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2948 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2949 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002950 ctx->playback.last_event = evt->playback.event;
2951
2952 switch (evt->playback.event)
2953 {
2954 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2955 case DVR_PLAYBACK_EVENT_REACHED_END:
2956 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2957 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002958 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002959 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002960 case DVR_PLAYBACK_EVENT_NODATA:
2961 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002962 {
2963 DVR_WrapperPlaybackStatus_t status;
2964
2965 /*copy status of segment*/
2966 ctx->playback.seg_status = evt->playback.status.play_status;
2967
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002968 /*generate status of the whole playback*/
2969 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002970
2971 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08002972 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08002973 if (ctx->playback.param_open.is_timeshift
2974 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002975 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08002976 }
2977 /*trust the low level, make NO check.
2978 As this evt is changed to only once due to some operations(paused) in low level.
2979 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002980 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08002981 }
2982 */
2983 else {
2984 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002985 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002986 }
hualing chenf291cf32020-06-18 10:50:30 +08002987 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002988 process_notifyPlayback(ctx, evt->playback.event, &status);
2989 }
2990 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002991 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2992 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2993 case DVR_PLAYBACK_EVENT_NO_KEY:
2994 {
Wentao MA96f68962022-06-15 19:45:35 +08002995 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002996 } break;
2997 default:
2998 {
Wentao MA96f68962022-06-15 19:45:35 +08002999 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003000 } break;
3001 }
3002 return 0;
3003}
3004
3005static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3006{
3007 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
3008}
3009
Wentao MA96f68962022-06-15 19:45:35 +08003010int dvr_wrapper_set_log_level (int level)
3011{
3012 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3013 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3014 return DVR_FAILURE;
3015 }
3016 g_dvr_log_level = level;
3017 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3018 return DVR_SUCCESS;
3019}
3020
Wentao MA5629ad82022-08-24 10:03:02 +08003021int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3022{
3023 DVR_WrapperCtx_t *ctx;
3024 int error;
3025
3026 DVR_RETURN_IF_FALSE(playback==NULL);
3027
3028 ctx = ctx_getPlayback((unsigned long)playback);
3029 DVR_RETURN_IF_FALSE(ctx);
3030
3031 wrapper_mutex_lock(&ctx->wrapper_lock);
3032
3033 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3034
3035 DVR_WRAPPER_INFO("set ac4 preselection id: %d", presel_id);
3036 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3037
3038 wrapper_mutex_unlock(&ctx->wrapper_lock);
3039
3040 return error;
3041}
3042