blob: f7a6ebbe77affc770c241fbc8a5335bea8e07be5 [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{
322 DVR_WrapperEventCtx_t *pevt;
323
324 pthread_mutex_lock(list_lock);
325 if (list_empty(list))
326 pevt = NULL;
327 else {
328 pevt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
329 list_del(&pevt->head);
330 }
331 pthread_mutex_unlock(list_lock);
332
333 return pevt;
334}
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{
369 DVR_WrapperEventCtx_t *pevt, *pevt_tmp;
370 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);
384 list_for_each_entry_safe(pevt, pevt_tmp, evt_list, head) {
385 for (i = 0; i < cnt; i++) {
386 if (pevt->sn == sns[i]) {
387 found = 1;
388 break;
389 }
390 }
391 if (!found) {
392 list_del(&pevt->head);
393 ctx_freeEvent(pevt);
394 }
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 MA96f68962022-06-15 19:45:35 +0800425 DVR_WRAPPER_INFO(" not found play is recing [%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{
595 DVR_WrapperThreadCtx_t *tctx = (DVR_WrapperThreadCtx_t *)arg;
596 DVR_WrapperEventCtx_t *evt;
597
598 pthread_mutex_lock(&tctx->lock);
599
600 while (tctx->running) {
601 {
602 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800603
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800604 evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
605 if (!evt)
606 ret = wrapper_threadWait(tctx);
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 MA96f68962022-06-15 19:45:35 +0800616 DVR_WRAPPER_INFO("start name(%s) sn(%d) running(%d) type(%d)\n", tctx->name, (int)ctx->sn, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800617 if (tctx->running) {
618 /*
619 continue not break,
620 make all events consumed, or mem leak
621 */
Gong Kefdb31922022-06-17 17:11:16 +0800622 if (!wrapper_mutex_lock_if(&ctx->wrapper_lock, &tctx->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) {
628 pthread_mutex_unlock(&tctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800629 process_handleEvents(evt, ctx);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800630 pthread_mutex_lock(&tctx->lock);
631 }
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
639 evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800640 }
Wentao MA96f68962022-06-15 19:45:35 +0800641 DVR_WRAPPER_INFO("start name(%s) running(%d) type(%d) con...\n", tctx->name, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800642 }
643
644 pthread_mutex_unlock(&tctx->lock);
Wentao MA96f68962022-06-15 19:45:35 +0800645 DVR_WRAPPER_INFO("end name(%s) running(%d) type(%d) end...\n", tctx->name, tctx->running, tctx->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{
669 DVR_WrapperPlaybackSegmentInfo_t *pseg, *pseg_tmp;
670 list_for_each_entry_safe(pseg, pseg_tmp, &ctx->segments, head) {
671 list_del(&pseg->head);
672 free(pseg);
673 }
674}
675
676static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *pseg,
677 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))
681 pseg->seg_info = *seg_info;
682 else if (update_flags & U_PIDS) {
683 pseg->seg_info.nb_pids = seg_info->nb_pids;
684 memcpy(pseg->seg_info.pids, seg_info->pids, sizeof(pseg->seg_info.pids));
685 } else if (update_flags & U_STAT) {
686 pseg->seg_info.duration = seg_info->duration;
687 pseg->seg_info.size = seg_info->size;
688 pseg->seg_info.nb_packets = seg_info->nb_packets;
689 }
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))
hualing chen03fd4942021-07-15 15:56:41 +0800693 dvr_playback_update_duration(ctx->playback.player,pseg->seg_info.id,pseg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800694 /*no changes
695 DVR_PlaybackSegmentFlag_t flags;
696 pseg->playback_info.segment_id = pseg->seg_info.id;
697 strncpy(pseg->playback_info.location,
698 ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
699 pseg->playback_info.pids = ctx->playback.pids_req;
700 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;
703 pseg->playback_info.flags = flags;
704 */
705}
706
707static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
708{
709 DVR_WrapperPlaybackSegmentInfo_t *pseg;
710
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*/
720 pseg =
721 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
722 if (pseg->seg_info.id == seg_info->id) {
723 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
724 } else {
725 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
726 if (pseg->seg_info.id == seg_info->id) {
727 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
728 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
764static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *pseg,
765 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))
769 pseg->info = *seg_info;
770 else if (update_flags & U_PIDS) {
771 pseg->info.nb_pids = seg_info->nb_pids;
772 memcpy(pseg->info.pids, seg_info->pids, sizeof(pseg->info.pids));
773 } else if (update_flags & U_STAT) {
774 pseg->info.duration = seg_info->duration;
775 pseg->info.size = seg_info->size;
776 pseg->info.nb_packets = seg_info->nb_packets;
777 }
778}
779
780static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
781{
hualing chen266b9502020-04-04 17:39:39 +0800782 DVR_WrapperRecordSegmentInfo_t *pseg = 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)) {
786 pseg =
787 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
788 if (pseg->info.id == seg_info->id) {
789 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
790 } else {
791 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
792 if (pseg->info.id == seg_info->id) {
793 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
794 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{
837 DVR_WrapperPlaybackSegmentInfo_t *pseg;
838 int error;
839
840 error = 0;
841 pseg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
842 if (!pseg) {
843 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
848 /*copy the orignal segment info*/
849 pseg->seg_info = *seg_info;
850 /*generate the segment info used in playback*/
851 pseg->playback_info.segment_id = pseg->seg_info.id;
852 strncpy(pseg->playback_info.location, ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
853 pseg->playback_info.pids = *p_pids;
854 pseg->playback_info.flags = flags;
855 list_add(&pseg->head, &ctx->segments);
Wentao MA96f68962022-06-15 19:45:35 +0800856 DVR_WRAPPER_INFO("start to add segment %lld\n", pseg->playback_info.segment_id);
hualing chen03fd4942021-07-15 15:56:41 +0800857 pseg->playback_info.duration = pseg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800858
859 error = dvr_playback_add_segment(ctx->playback.player, &pseg->playback_info);
860 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +0800861 DVR_WRAPPER_INFO("fail to add segment %lld (%d)\n", pseg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800862 } else {
863 ctx->playback.status.info_full.time += pseg->seg_info.duration;
864 ctx->playback.status.info_full.size += pseg->seg_info.size;
865 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
866 }
867
868 return error;
869}
870
871static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
872{
873 DVR_WrapperRecordSegmentInfo_t *pseg;
874 int error;
hualing chenab0d1262021-09-26 15:22:50 +0800875 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800876
877 error = 0;
878 pseg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
879 if (!pseg) {
880 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +0800881 DVR_WRAPPER_INFO("memory fail\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800882 }
883 pseg->info = *seg_info;
884 list_add(&pseg->head, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800885
hualing chenb9b358a2021-08-17 15:06:36 +0800886 if (ctx->record.param_open.is_timeshift ||
887 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
888
889 DVR_WrapperCtx_t *ctx_playback;
890 if (ctx->record.param_open.is_timeshift)
891 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
892 else
893 ctx_playback = ctx_getPlayback(sn);
894
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800895 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) add seg\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800896
897 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800898 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800899 if (ctx_valid(ctx_playback)) {
900 DVR_PlaybackSegmentFlag_t flags;
901
902 /*only if playback has started, the previous segments have been loaded*/
903 if (!list_empty(&ctx_playback->segments)) {
904 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800905 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800906 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
907 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
hualing chen451c8f72022-03-09 13:05:52 +0800908 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800909 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) list empty\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800910 }
hualing chenb9b358a2021-08-17 15:06:36 +0800911 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800912 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800913 }
Gong Kefdb31922022-06-17 17:11:16 +0800914 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800915 }
hualing chen451c8f72022-03-09 13:05:52 +0800916 else
917 {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800918 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid 2\n", ctx->sn, sn);
919 }
920
921 /*if it is not a timeshift recording, but a playing recording,
922 do not forget to obey the recording rule: link the segment!*/
923 if (!ctx->record.param_open.is_timeshift) {
924 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
925 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
hualing chen451c8f72022-03-09 13:05:52 +0800926 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800927 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800928 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
Wentao MAe8ba5172022-08-09 11:18:17 +0800929 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800930 }
931
932 return error;
933}
934
935static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
936{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800937 int error = -1;
938 DVR_WrapperPlaybackSegmentInfo_t *pseg = NULL, *pseg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800939 uint32_t off_set = 0;
Wentao MA96f68962022-06-15 19:45:35 +0800940 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800941
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800942 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800943 if (pseg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800944
945 if (ctx->current_segment_id == seg_info->id) {
946 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
947
948 /*drive the player out of this will-be-deleted segment*/
949 next_seg = list_prev_entry(pseg, head);
950
951 if (ctx->playback.speed != 100.0f) {
952 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800953 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800954 }
hualing chenb9a1a2c2021-12-31 11:27:59 +0800955 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
956 off_set = 10 * 1000;
957 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800958 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 +0800959
960 if (ctx->playback.speed == 0.0f) {
961 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +0800962 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 +0800963 } else if (ctx->playback.speed != 100.0f) {
964 DVR_PlaybackSpeed_t dvr_speed = {
965 .speed = { ctx->playback.speed },
966 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
967 };
968 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
Wentao MA96f68962022-06-15 19:45:35 +0800969 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 +0800970 }
971 }
972
973 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
974 if (error) {
975 /*remove playack segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +0800976 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 +0800977 }
978
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800979 list_del(&pseg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800980
981 /*record the obsolete*/
982 ctx->playback.obsolete.time += pseg->seg_info.duration;
983 ctx->playback.obsolete.size += pseg->seg_info.size;
984 ctx->playback.obsolete.pkts += pseg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +0800985 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 +0800986 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800987 free(pseg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800988 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800989 }
990 }
991
Wentao MA96f68962022-06-15 19:45:35 +0800992 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 +0800993
994 return error;
995}
996
997static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
998{
999 int error;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001000 DVR_WrapperRecordSegmentInfo_t *pseg, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001001
Wentao MAf4072032022-06-30 13:50:45 +08001002 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1003 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001004
1005 /*if timeshifting, notify the playback first, then deal with record*/
1006 if (ctx->record.param_open.is_timeshift) {
1007 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1008
1009 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001010 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001011 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1012 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001013 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1014 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1015 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001016 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001017 return DVR_SUCCESS;
1018 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001019 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1020 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1021 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001022 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001023 if (ctx_valid(ctx_playback)
1024 && ctx_playback->sn == sn_timeshift_playback
1025 && !list_empty(&ctx_playback->segments)) {
1026 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
1027 }
hualing chen56c0a162022-01-27 17:01:50 +08001028 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001029 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001030 }
1031 }
1032
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001033 uint64_t id = seg_info->info.id;
1034
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001035 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001036 if (pseg->info.id == id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001037 list_del(&pseg->head);
1038
1039 /*record the obsolete*/
1040 ctx->record.obsolete.time += pseg->info.duration;
1041 ctx->record.obsolete.size += pseg->info.size;
1042 ctx->record.obsolete.pkts += pseg->info.nb_packets;
1043
1044 free(pseg);
1045 break;
1046 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001047 }
1048
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001049 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001050
Wentao MAf4072032022-06-30 13:50:45 +08001051 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1052 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001053
1054 return error;
1055}
1056
1057int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1058{
1059 int error;
1060 DVR_WrapperCtx_t *ctx;
1061 DVR_RecordOpenParams_t open_param;
1062
1063 DVR_RETURN_IF_FALSE(rec);
1064 DVR_RETURN_IF_FALSE(params);
1065
1066 /*get a free ctx*/
1067 ctx = ctx_getRecord(0);
1068 DVR_RETURN_IF_FALSE(ctx);
1069
Gong Kefdb31922022-06-17 17:11:16 +08001070 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001071
Wentao MA96f68962022-06-15 19:45:35 +08001072 DVR_WRAPPER_INFO("open record(dmx:%d) .istf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
hualing chen51652f02020-12-29 16:59:31 +08001073 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001074
1075 ctx_reset(ctx);
1076
1077 ctx->record.param_open = *params;
1078 ctx->record.event_fn = params->event_fn;
1079 ctx->record.event_userdata = params->event_userdata;
1080 ctx->record.next_segment_id = 0;
1081 ctx->current_segment_id = 0;
1082 INIT_LIST_HEAD(&ctx->segments);
1083 ctx->sn = get_sn();
1084
1085 wrapper_requestThreadFor(ctx);
1086
hualing chen266b9502020-04-04 17:39:39 +08001087 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001088 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001089 open_param.dmx_dev_id = params->dmx_dev_id;
1090 open_param.data_from_memory = 0;
1091 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001092 if (params->flush_size) {
1093 open_param.notification_size = params->flush_size;
1094 } else {
1095 open_param.notification_size = 64*1024;
1096 }
hualing chen002e5b92022-02-23 17:51:21 +08001097 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001098 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001099 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001100 open_param.event_fn = wrapper_record_event_handler;
1101 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001102 if (params->keylen) {
1103 open_param.clearkey = params->clearkey;
1104 open_param.cleariv = params->cleariv;
1105 open_param.keylen = params->keylen;
1106 }
wentao.ma35a69d42022-03-10 18:08:40 +08001107 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001108 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001109
1110 error = dvr_record_open(&ctx->record.recorder, &open_param);
1111 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001112 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001113 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001114 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001115 wrapper_releaseThreadForType(ctx->type);
1116 return DVR_FAILURE;
1117 }
1118 if (params->is_timeshift)
1119 sn_timeshift_record = ctx->sn;
1120
Wentao MA96f68962022-06-15 19:45:35 +08001121 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001122
Yahui Han1fbf3292021-11-08 18:17:19 +08001123 if (params->crypto_fn) {
1124 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1125 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001126 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 +08001127 }
hualing chen266b9502020-04-04 17:39:39 +08001128 }
1129
Gong Kefdb31922022-06-17 17:11:16 +08001130 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001131
1132 *rec = (DVR_WrapperRecord_t)ctx->sn;
1133 return DVR_SUCCESS;
1134}
1135
1136int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1137{
1138 DVR_WrapperCtx_t *ctx;
1139 DVR_RecordSegmentInfo_t seg_info;
1140 int error;
1141
1142 DVR_RETURN_IF_FALSE(rec);
1143
1144 ctx = ctx_getRecord((unsigned long)rec);
1145 DVR_RETURN_IF_FALSE(ctx);
1146
Gong Kefdb31922022-06-17 17:11:16 +08001147 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001148 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001149 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001150
1151 memset(&seg_info, 0, sizeof(seg_info));
1152 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1153
1154 error = dvr_record_close(ctx->record.recorder);
1155
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001156 if (ctx->record.param_open.is_timeshift)
1157 sn_timeshift_record = 0;
1158
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001159 ctx_freeSegments(ctx);
1160
Wentao MA96f68962022-06-15 19:45:35 +08001161 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001162 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001163 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001164
1165 wrapper_releaseThreadForType(ctx->type);
1166
1167 return error;
1168}
1169
1170int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1171{
1172 DVR_WrapperCtx_t *ctx;
1173 DVR_RecordStartParams_t *start_param;
1174 int i;
1175 int error;
1176
1177 DVR_RETURN_IF_FALSE(rec);
1178 DVR_RETURN_IF_FALSE(params);
1179
1180 ctx = ctx_getRecord((unsigned long)rec);
1181 DVR_RETURN_IF_FALSE(ctx);
1182
Gong Kefdb31922022-06-17 17:11:16 +08001183 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001184 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 +08001185 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001186
1187 start_param = &ctx->record.param_start;
1188 memset(start_param, 0, sizeof(*start_param));
1189 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1190 start_param->segment.segment_id = ctx->record.next_segment_id++;
1191 start_param->segment.nb_pids = params->pids_info.nb_pids;
1192 for (i = 0; i < params->pids_info.nb_pids; i++) {
1193 start_param->segment.pids[i] = params->pids_info.pids[i];
1194 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1195 }
hualing chena5f03222021-12-02 11:22:35 +08001196 if (params->save_rec_file == 0)//default is not save
1197 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001198 {
1199 /*sync to update for further use*/
1200 DVR_RecordStartParams_t *update_param;
1201 update_param = &ctx->record.param_update;
1202 memcpy(update_param, start_param, sizeof(*update_param));
1203 for (i = 0; i < update_param->segment.nb_pids; i++)
1204 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1205 }
1206
1207 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1208 {
1209 DVR_RecordSegmentInfo_t new_seg_info =
1210 { .id = start_param->segment.segment_id, };
1211 wrapper_addRecordSegment(ctx, &new_seg_info);
1212 }
1213
Wentao MA96f68962022-06-15 19:45:35 +08001214 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001215
Gong Kefdb31922022-06-17 17:11:16 +08001216 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001217
1218 return error;
1219}
1220
1221int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1222{
1223 DVR_WrapperCtx_t *ctx;
1224 DVR_RecordSegmentInfo_t seg_info;
1225 int error;
1226
1227 DVR_RETURN_IF_FALSE(rec);
1228
1229 ctx = ctx_getRecord((unsigned long)rec);
1230 DVR_RETURN_IF_FALSE(ctx);
1231
Gong Kefdb31922022-06-17 17:11:16 +08001232 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001233 DVR_WRAPPER_INFO("stop record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001234 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001235
1236 memset(&seg_info, 0, sizeof(seg_info));
1237 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1238 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1239
Wentao MA96f68962022-06-15 19:45:35 +08001240 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001241 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001242
1243 return error;
1244}
1245
hualing chen03fd4942021-07-15 15:56:41 +08001246int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1247{
1248 DVR_WrapperCtx_t *ctx;
1249 int error;
1250
1251 DVR_RETURN_IF_FALSE(rec);
1252
1253 ctx = ctx_getRecord((unsigned long)rec);
1254 DVR_RETURN_IF_FALSE(ctx);
1255
Gong Kefdb31922022-06-17 17:11:16 +08001256 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001257 DVR_WRAPPER_INFO("pause record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001258 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001259
1260 error = dvr_record_pause(ctx->record.recorder);
1261
Wentao MA96f68962022-06-15 19:45:35 +08001262 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001263 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001264
1265 return error;
1266}
1267
1268int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1269{
1270 DVR_WrapperCtx_t *ctx;
1271 int error;
1272
1273 DVR_RETURN_IF_FALSE(rec);
1274
1275 ctx = ctx_getRecord((unsigned long)rec);
1276 DVR_RETURN_IF_FALSE(ctx);
1277
Gong Kefdb31922022-06-17 17:11:16 +08001278 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001279 DVR_WRAPPER_INFO("resume record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001280 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001281
1282 error = dvr_record_resume(ctx->record.recorder);
1283
Wentao MA96f68962022-06-15 19:45:35 +08001284 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001285 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001286
1287 return error;
1288}
1289
Wentao MAcdea4762022-04-26 13:28:56 +08001290/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001291static DVR_Bool_t pids_test_include(
1292 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1293 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001294{
Wentao MAcdea4762022-04-26 13:28:56 +08001295 DVR_Bool_t ret = DVR_TRUE;
1296 for (int i=0;i<size2;i++)
1297 { // iterate all elements in arr2 to check if they exist in arr1
1298 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001299
1300 if (act2[i] == DVR_RECORD_PID_CLOSE)
1301 continue;
1302
Wentao MAcdea4762022-04-26 13:28:56 +08001303 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001304 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001305 if (act1[j] != DVR_RECORD_PID_CLOSE
1306 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001307 {
1308 found=DVR_TRUE;
1309 break;
1310 }
1311 }
1312 if (found == DVR_FALSE)
1313 {
Wentao MAcdea4762022-04-26 13:28:56 +08001314 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001315 break;
1316 }
1317 }
Wentao MAcdea4762022-04-26 13:28:56 +08001318 return ret;
1319}
1320
1321static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1322 const DVR_WrapperUpdatePidsParams_t* p2)
1323{
1324 int i=0;
1325 char buf[128]={0};
1326 int cnt=0;
1327 int chars=0;
1328
1329 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1330 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1331
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001332 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1333 p2->pids,p2->pid_action,p2->nb_pids);
1334 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1335 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001336 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001337 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001338
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001339 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001340 for (i=0;i<p1->nb_pids;i++)
1341 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001342 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1343 removed++;
1344 continue;
1345 }
Wentao MAcdea4762022-04-26 13:28:56 +08001346 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1347 if (chars<0)
1348 {
1349 break;
1350 }
1351 cnt += chars;
1352 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001353 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001354 memset(buf,0,sizeof(buf));
1355
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001356 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001357 for (i=0,cnt=0;i<p2->nb_pids;i++)
1358 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001359 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1360 removed++;
1361 continue;
1362 }
Wentao MAcdea4762022-04-26 13:28:56 +08001363 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1364 if (chars<0)
1365 {
1366 break;
1367 }
1368 cnt += chars;
1369 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001370 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001371 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001372 return is_equal;
1373}
1374
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001375int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1376{
1377 DVR_WrapperCtx_t *ctx;
1378 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001379 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001380 int i;
1381 int error;
1382
1383 DVR_RETURN_IF_FALSE(rec);
1384 DVR_RETURN_IF_FALSE(params);
1385
1386 ctx = ctx_getRecord((unsigned long)rec);
1387 DVR_RETURN_IF_FALSE(ctx);
1388
Gong Kefdb31922022-06-17 17:11:16 +08001389 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001390 DVR_WRAPPER_INFO("update record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001391 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001392
1393 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001394 if (pids_equal(&(start_param->segment),params))
1395 {
Gong Kefdb31922022-06-17 17:11:16 +08001396 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001397 return DVR_TRUE;
1398 }
1399
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001400 memset(start_param, 0, sizeof(*start_param));
1401 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1402 start_param->segment.segment_id = ctx->record.next_segment_id++;
1403 start_param->segment.nb_pids = params->nb_pids;
1404 for (i = 0; i < params->nb_pids; i++) {
1405 start_param->segment.pids[i] = params->pids[i];
1406 start_param->segment.pid_action[i] = params->pid_action[i];
1407 }
1408 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1409 {
1410 DVR_RecordSegmentInfo_t new_seg_info =
1411 { .id = start_param->segment.segment_id, };
1412 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1413 wrapper_addRecordSegment(ctx, &new_seg_info);
1414 }
1415
Wentao MA96f68962022-06-15 19:45:35 +08001416 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001417 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001418
1419 return error;
1420}
1421
1422int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1423{
1424 DVR_WrapperCtx_t *ctx;
1425 DVR_WrapperRecordStatus_t s;
1426 int error;
1427
1428 DVR_RETURN_IF_FALSE(rec);
1429 DVR_RETURN_IF_FALSE(status);
1430
1431 ctx = ctx_getRecord((unsigned long)rec);
1432 DVR_RETURN_IF_FALSE(ctx);
1433
Gong Kefdb31922022-06-17 17:11:16 +08001434 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001435
Wentao MA96f68962022-06-15 19:45:35 +08001436 DVR_WRAPPER_INFO("get record(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001437 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001438
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001439 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001440
Wentao MA96f68962022-06-15 19:45:35 +08001441 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001442 ctx->sn,
1443 s.state,
1444 s.info.time,
1445 s.info.size,
1446 s.info.pkts,
1447 error);
1448
1449 *status = s;
1450
Gong Kefdb31922022-06-17 17:11:16 +08001451 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001452
1453 return error;
1454}
1455
hualing chen4fe3bee2020-10-23 13:58:52 +08001456int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1457{
1458 DVR_WrapperCtx_t *ctx;
1459 int error;
1460
1461 DVR_RETURN_IF_FALSE(rec);
1462
1463 ctx = ctx_getRecord((unsigned long)rec);
1464 DVR_RETURN_IF_FALSE(ctx);
1465
Gong Kefdb31922022-06-17 17:11:16 +08001466 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001467 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001468 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001469 return error;
1470}
1471
hualing chen266b9502020-04-04 17:39:39 +08001472int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1473{
1474 DVR_WrapperCtx_t *ctx;
1475 int error;
1476
1477 DVR_RETURN_IF_FALSE(rec);
1478 DVR_RETURN_IF_FALSE(p_secure_buf);
1479
1480 ctx = ctx_getRecord((unsigned long)rec);
1481 DVR_RETURN_IF_FALSE(ctx);
1482
Gong Kefdb31922022-06-17 17:11:16 +08001483 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001484 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001485 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001486 return error;
1487}
1488
1489int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1490{
1491 DVR_WrapperCtx_t *ctx;
1492 int error;
1493
1494 DVR_RETURN_IF_FALSE(rec);
1495 DVR_RETURN_IF_FALSE(func);
1496
1497 ctx = ctx_getRecord((unsigned long)rec);
1498 DVR_RETURN_IF_FALSE(ctx);
1499
Gong Kefdb31922022-06-17 17:11:16 +08001500 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001501 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001502 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001503 return error;
1504}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001505
1506
1507int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1508{
1509 DVR_WrapperCtx_t *ctx;
1510 DVR_PlaybackOpenParams_t open_param;
1511 int error;
1512
1513 DVR_RETURN_IF_FALSE(playback);
1514 DVR_RETURN_IF_FALSE(params);
1515 DVR_RETURN_IF_FALSE(params->playback_handle);
1516
1517 /*get a free ctx*/
1518 ctx = ctx_getPlayback(0);
1519 DVR_RETURN_IF_FALSE(ctx);
1520
Gong Kefdb31922022-06-17 17:11:16 +08001521 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001522
Wentao MA96f68962022-06-15 19:45:35 +08001523 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 +08001524
1525 ctx_reset(ctx);
1526
1527 ctx->playback.param_open = *params;
1528 ctx->playback.event_fn = params->event_fn;
1529 ctx->playback.event_userdata = params->event_userdata;
1530 ctx->current_segment_id = 0;
1531 INIT_LIST_HEAD(&ctx->segments);
1532 ctx->sn = get_sn();
1533
1534 wrapper_requestThreadFor(ctx);
1535
hualing chen266b9502020-04-04 17:39:39 +08001536 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001537 open_param.dmx_dev_id = params->dmx_dev_id;
1538 open_param.block_size = params->block_size;
1539 open_param.is_timeshift = params->is_timeshift;
1540 //open_param.notification_size = 10*1024; //not supported
1541 open_param.event_fn = wrapper_playback_event_handler;
1542 open_param.event_userdata = (void*)ctx->sn;
1543 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001544 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001545 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001546 open_param.vendor = params->vendor;
1547
Yahui Han1fbf3292021-11-08 18:17:19 +08001548 if (params->keylen) {
1549 open_param.clearkey = params->clearkey;
1550 open_param.cleariv = params->cleariv;
1551 open_param.keylen = params->keylen;
1552 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001553
1554 error = dvr_playback_open(&ctx->playback.player, &open_param);
1555 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001556 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001557 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001558 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001559 wrapper_releaseThreadForType(ctx->type);
1560 return DVR_FAILURE;
1561 }
1562 if (params->is_timeshift)
1563 sn_timeshift_playback = ctx->sn;
1564
Wentao MA96f68962022-06-15 19:45:35 +08001565 DVR_WRAPPER_INFO("hanyh: playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001566 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1567 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001568 DVR_WRAPPER_INFO("playback set deccrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001569 }
Gong Kefdb31922022-06-17 17:11:16 +08001570 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001571
1572 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1573 return DVR_SUCCESS;
1574}
1575
1576int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1577{
1578 DVR_WrapperCtx_t *ctx;
1579 int error;
1580
1581 DVR_RETURN_IF_FALSE(playback);
1582
1583 ctx = ctx_getPlayback((unsigned long)playback);
1584 DVR_RETURN_IF_FALSE(ctx);
1585
Gong Kefdb31922022-06-17 17:11:16 +08001586 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001587 DVR_WRAPPER_INFO("close playback(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001588 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001589
1590 if (ctx->playback.param_open.is_timeshift)
1591 sn_timeshift_playback = 0;
1592
1593 /*try stop first*/
1594 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1595
1596 {
1597 /*remove all segments*/
1598 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1599
1600 list_for_each_entry(pseg, &ctx->segments, head) {
1601 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001602 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001603 ctx->sn, pseg->playback_info.segment_id, error);
1604 }
1605 ctx_freeSegments(ctx);
1606 }
1607
1608 error = dvr_playback_close(ctx->playback.player);
1609
Wentao MA96f68962022-06-15 19:45:35 +08001610 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001611 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001612 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001613
1614 wrapper_releaseThreadForType(ctx->type);
1615
1616 return error;
1617}
1618
1619int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1620{
1621 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001622 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001623 uint64_t *p_segment_ids;
1624 uint32_t segment_nb;
1625 uint32_t i;
1626 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001627 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001628 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001629 DVR_Bool_t is_timeshift = DVR_FALSE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001630
1631 DVR_RETURN_IF_FALSE(playback);
1632 DVR_RETURN_IF_FALSE(p_pids);
1633
hualing chenc110f952021-01-18 11:25:37 +08001634 ctx_record = NULL;
1635
1636 /*lock the recorder to avoid changing the recording segments*/
1637 ctx_record = ctx_getRecord(sn_timeshift_record);
1638
1639 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001640 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001641 if (!ctx_valid(ctx_record)
1642 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001643 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001644 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001645 is_timeshift = DVR_FALSE;
1646 } else {
1647 is_timeshift = DVR_TRUE;
1648 }
1649 }
1650
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001651 ctx = ctx_getPlayback((unsigned long)playback);
1652 DVR_RETURN_IF_FALSE(ctx);
1653
Gong Kefdb31922022-06-17 17:11:16 +08001654 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001655
Wentao MA96f68962022-06-15 19:45:35 +08001656 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 +08001657 ctx->sn,
1658 ctx->playback.param_open.location,
1659 flags,
1660 p_pids->video.pid, p_pids->video.format,
1661 p_pids->audio.pid, p_pids->audio.format,
1662 p_pids->ad.pid, p_pids->ad.format,
1663 p_pids->subtitle.pid, p_pids->subtitle.format,
1664 p_pids->pcr.pid);
1665
Gong Kefdb31922022-06-17 17:11:16 +08001666 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001667
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001668 if (ctx->playback.param_open.is_timeshift) {
1669 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001670 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001671 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001672 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001673 return DVR_FAILURE;
1674 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001675 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001676 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001677 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001678 }
1679
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001680 /*obtain all segments in a list*/
1681 segment_nb = 0;
1682 p_segment_ids = NULL;
1683 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001684 if (!error) {
1685 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001686 struct list_head info_list; /**< segment list head*/
1687 INIT_LIST_HEAD(&info_list);
1688
Wentao MA96f68962022-06-15 19:45:35 +08001689 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001690 //we need free info list buf when we used end.
1691 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001692 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001693 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001694 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1695 ctx->playback.param_open.location, error);
hualing chenb9a02922021-12-14 11:29:47 +08001696 for (i = 0; i < segment_nb; i++) {
1697 DVR_RecordSegmentInfo_t seg_info;
1698 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001699
hualing chenb9a02922021-12-14 11:29:47 +08001700 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1701 if (error) {
1702 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001703 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001704 ctx->playback.param_open.location, p_segment_ids[i], error);
1705 break;
1706 }
1707 //add check if has audio or video pid. if not exist. not add segment to playback
1708 int ii = 0;
1709 int has_av = 0;
1710 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1711 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1712 if (type == DVR_STREAM_TYPE_VIDEO ||
1713 type == DVR_STREAM_TYPE_AUDIO ||
1714 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001715 DVR_WRAPPER_INFO("success to get seg av info \n");
1716 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 +08001717 DVR_STREAM_TYPE_VIDEO,
1718 DVR_STREAM_TYPE_AUDIO,
1719 DVR_STREAM_TYPE_AD);
1720 has_av = 1;
1721 //break;
1722 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001723 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 +08001724 DVR_STREAM_TYPE_VIDEO,
1725 DVR_STREAM_TYPE_AUDIO,
1726 DVR_STREAM_TYPE_AD);
1727 }
1728 }
1729 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001730 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001731 continue;
1732 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001733 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001734 }
1735 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1736 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1737 if (error)
1738 break;
1739 /*copy the 1st segment*/
1740 if (got_1st_seg == 0) {
1741 seg_info_1st = seg_info;
1742 got_1st_seg = 1;
1743 }
1744 }
1745 } else {
1746 for (i = 0; i < segment_nb; i++) {
1747 DVR_RecordSegmentInfo_t *seg_info;
1748 DVR_PlaybackSegmentFlag_t flags;
1749 int found = 0;
1750 list_for_each_entry(seg_info, &info_list, head)
1751 {
hualing chen8aed9582021-12-24 17:59:56 +08001752 if (seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001753 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001754 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001755 break;
1756 }
1757 }
1758 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001759 //last info is not found if when recording occured power off.
1760 if (p_segment_ids[i] == segment_nb - 1) {
1761 DVR_RecordSegmentInfo_t seg_info;
1762 DVR_PlaybackSegmentFlag_t flags;
1763
1764 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1765 if (error) {
1766 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001767 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001768 ctx->playback.param_open.location, p_segment_ids[i], error);
1769 break;
1770 }
1771 //
1772 //add check if has audio or video pid. if not exist. not add segment to playback
1773 int ii = 0;
1774 int has_av = 0;
1775 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1776 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1777 if (type == DVR_STREAM_TYPE_VIDEO ||
1778 type == DVR_STREAM_TYPE_AUDIO ||
1779 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001780 DVR_WRAPPER_INFO("success to get seg av info \n");
1781 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 +08001782 DVR_STREAM_TYPE_VIDEO,
1783 DVR_STREAM_TYPE_AUDIO,
1784 DVR_STREAM_TYPE_AD);
1785 has_av = 1;
1786 //break;
1787 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001788 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 +08001789 DVR_STREAM_TYPE_VIDEO,
1790 DVR_STREAM_TYPE_AUDIO,
1791 DVR_STREAM_TYPE_AD);
1792 }
1793 }
1794 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001795 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001796 continue;
1797 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001798 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001799 }
1800 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1801 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1802 if (error)
1803 break;
1804 }
hualing chenb9a02922021-12-14 11:29:47 +08001805 continue;
1806 }
1807
1808 //add check if has audio or video pid. if not exist. not add segment to playback
1809 int ii = 0;
1810 int has_av = 0;
1811 for (ii = 0; ii < seg_info->nb_pids; ii++) {
1812 int type = (seg_info->pids[ii].type >> 24) & 0x0f;
1813 if (type == DVR_STREAM_TYPE_VIDEO ||
1814 type == DVR_STREAM_TYPE_AUDIO ||
1815 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001816 DVR_WRAPPER_INFO("success to get seg av info \n");
1817 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 +08001818 DVR_STREAM_TYPE_VIDEO,
1819 DVR_STREAM_TYPE_AUDIO,
1820 DVR_STREAM_TYPE_AD);
1821 has_av = 1;
1822 //break;
1823 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001824 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 +08001825 DVR_STREAM_TYPE_VIDEO,
1826 DVR_STREAM_TYPE_AUDIO,
1827 DVR_STREAM_TYPE_AD);
1828 }
1829 }
1830 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001831 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001832 continue;
1833 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001834 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001835 }
1836 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1837 error = wrapper_addPlaybackSegment(ctx, seg_info, p_pids, flags);
1838 if (error)
1839 break;
1840
1841 /*copy the 1st segment*/
1842 if (got_1st_seg == 0) {
1843 seg_info_1st = *seg_info;
1844 got_1st_seg = 1;
1845 }
hualing chen92f3a142020-07-08 20:59:33 +08001846 }
hualing chenb9a02922021-12-14 11:29:47 +08001847 //free list
1848 DVR_RecordSegmentInfo_t *segment = NULL;
1849 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1850 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1851 {
1852 if (segment) {
1853 list_del(&segment->head);
1854 free(segment);
1855 }
1856 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001857 }
hualing chenb9a02922021-12-14 11:29:47 +08001858
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001859 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001860
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001861 /* return if no segment or fail to add */
1862 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001863
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001864 /*copy the obsolete infomation, must for timeshifting*/
1865 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1866 ctx->playback.obsolete = ctx_record->record.obsolete;
1867 }
1868
Wentao MA96f68962022-06-15 19:45:35 +08001869 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001870
1871 ctx->playback.reach_end = DVR_FALSE;
1872 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1873 ctx->playback.speed = 0.0f;
1874 else
1875 ctx->playback.speed = 100.0f;
1876
1877 ctx->playback.pids_req = *p_pids;
hualing chen03fd4942021-07-15 15:56:41 +08001878 //calualte segment id and pos
1879 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001880 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001881 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001882 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001883 error = dvr_playback_start(ctx->playback.player, flags);
1884 } else {
1885 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1886 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001887 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001888 ctx->sn, seg_info_1st.id, error);
1889 }
Wentao MA96f68962022-06-15 19:45:35 +08001890 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001891 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001892 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 +08001893 }
1894 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001895
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001896 if (ctx->playback.param_open.is_timeshift) {
1897 /*unlock the recorder locked above*/
1898 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001899 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001900 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001901 ctx->sn, ctx_record->sn);
1902 }
1903 }
Gong Kefdb31922022-06-17 17:11:16 +08001904 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001905
1906 return error;
1907}
hualing chen002e5b92022-02-23 17:51:21 +08001908//stop record and playback
1909int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1910{
1911 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1912 int error;
Wentao MA96f68962022-06-15 19:45:35 +08001913 DVR_WRAPPER_INFO("stop timeshift record\n");
hualing chen002e5b92022-02-23 17:51:21 +08001914
1915 //stop timeshift record
1916 ctx_record = ctx_getRecord(sn_timeshift_record);
1917 error = dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
1918
Wentao MA96f68962022-06-15 19:45:35 +08001919 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08001920 //stop play
1921 error = dvr_wrapper_stop_playback(playback);
1922 //del timeshift file
1923 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001924 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08001925 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
1926 }
1927 return error;
1928}
1929//start record and start playback
1930int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1931{
1932 DVR_WrapperCtx_t *ctx;
1933 DVR_RecordStartParams_t *start_param;
1934 int error;
1935
1936 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
1937 DVR_RETURN_IF_FALSE(ctx);
1938
Gong Kefdb31922022-06-17 17:11:16 +08001939 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001940 DVR_WRAPPER_INFO("restart record(sn:%ld, location:%s)...\n", ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08001941 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08001942
hualing chen451c8f72022-03-09 13:05:52 +08001943 {
1944 //clear old record status
1945 // struct {
1946 // DVR_WrapperRecordOpenParams_t param_open;
1947 // DVR_RecordStartParams_t param_start;
1948 // DVR_RecordStartParams_t param_update;
1949 // DVR_RecordHandle_t recorder;
1950 // DVR_RecordEventFunction_t event_fn;
1951 // void *event_userdata;
1952
1953 // /*total status = seg_status + status + obsolete*/
1954 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
1955 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
1956 // uint64_t next_segment_id;
1957
1958 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
1959 // } record;
1960 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
1961 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
1962 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
1963 }
1964
hualing chen002e5b92022-02-23 17:51:21 +08001965 start_param = &ctx->record.param_start;
1966
1967 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1968 {
1969 DVR_RecordSegmentInfo_t new_seg_info =
1970 { .id = start_param->segment.segment_id, };
1971 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08001972 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 +08001973 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
1974 DVR_RecordStartParams_t *update_param;
1975 update_param = &ctx->record.param_update;
1976 memcpy(update_param, start_param, sizeof(*update_param));
1977 int i = 0;
1978 for (i = 0; i < update_param->segment.nb_pids; i++)
1979 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08001980 }
1981
Wentao MA96f68962022-06-15 19:45:35 +08001982 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08001983
Gong Kefdb31922022-06-17 17:11:16 +08001984 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08001985
1986 //start play
Wentao MA96f68962022-06-15 19:45:35 +08001987 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08001988 //clear play statue
1989 ctx = ctx_getPlayback((unsigned long)playback);
1990 if (ctx) {
1991 //clear old playback status
1992 // struct {
1993 // DVR_WrapperPlaybackOpenParams_t param_open;
1994 // DVR_PlaybackHandle_t player;
1995 // DVR_PlaybackEventFunction_t event_fn;
1996 // void *event_userdata;
1997
1998 // /*total status = seg_status + status*/
1999 // DVR_PlaybackStatus_t seg_status;
2000 // DVR_WrapperPlaybackStatus_t status;
2001 // DVR_PlaybackPids_t pids_req;
2002 // DVR_PlaybackEvent_t last_event;
2003 // float speed;
2004 // DVR_Bool_t reach_end;
2005
2006 // DVR_WrapperInfo_t obsolete;
2007 // DVR_Bool_t tf_full;
2008 // } playback;
2009 ctx->playback.tf_full == DVR_FALSE;
2010 ctx->playback.reach_end == DVR_FALSE;
2011 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2012 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2013 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2014 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2015 }
hualing chen002e5b92022-02-23 17:51:21 +08002016 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2017 return error;
2018}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002019
2020int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2021{
2022 DVR_WrapperCtx_t *ctx;
2023 int error;
2024
2025 DVR_RETURN_IF_FALSE(playback);
2026
2027 ctx = ctx_getPlayback((unsigned long)playback);
2028 DVR_RETURN_IF_FALSE(ctx);
2029
Gong Kefdb31922022-06-17 17:11:16 +08002030 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002031 DVR_WRAPPER_INFO("stop playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002032 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002033
2034 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2035
2036 {
2037 /*remove all segments*/
2038 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2039
2040 list_for_each_entry(pseg, &ctx->segments, head) {
2041 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002042 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002043 ctx->sn, pseg->playback_info.segment_id, error);
2044 }
2045 ctx_freeSegments(ctx);
2046 }
2047
Wentao MA96f68962022-06-15 19:45:35 +08002048 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002049 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002050
2051 return error;
2052}
2053
2054int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2055{
2056 DVR_WrapperCtx_t *ctx;
2057 int error;
2058
2059 DVR_RETURN_IF_FALSE(playback);
2060
2061 ctx = ctx_getPlayback((unsigned long)playback);
2062 DVR_RETURN_IF_FALSE(ctx);
2063
Gong Kefdb31922022-06-17 17:11:16 +08002064 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002065 DVR_WRAPPER_INFO("pause playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002066 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002067 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002068 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002069 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002070
2071 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2072
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002073 ctx->playback.speed = 0.0f;
2074
Wentao MA96f68962022-06-15 19:45:35 +08002075 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002076 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002077
2078 return error;
2079}
2080
2081int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2082{
2083 DVR_WrapperCtx_t *ctx;
2084 int error;
2085
2086 DVR_RETURN_IF_FALSE(playback);
2087
2088 ctx = ctx_getPlayback((unsigned long)playback);
2089 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002090 //if set limit.we need check if seek to valid data when resume
2091 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2092 if (dvr_playback_check_limit(ctx->playback.player)) {
2093 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2094 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002095 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002096 ctx->sn, time_offset, expired);
2097 time_offset = expired;
2098 dvr_wrapper_seek_playback(playback, time_offset);
2099 }
2100 }
Gong Kefdb31922022-06-17 17:11:16 +08002101 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002102 DVR_WRAPPER_INFO("resume playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002103 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002104
2105 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002106 ctx->playback.speed = 100.0f;
2107
Wentao MA96f68962022-06-15 19:45:35 +08002108 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002109 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002110
2111 return error;
2112}
2113
2114int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2115{
2116 DVR_WrapperCtx_t *ctx;
2117 int error;
2118 DVR_PlaybackSpeed_t dvr_speed = {
2119 .speed = { speed },
2120 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2121 };
2122
2123 DVR_RETURN_IF_FALSE(playback);
2124
2125 ctx = ctx_getPlayback((unsigned long)playback);
2126 DVR_RETURN_IF_FALSE(ctx);
2127
Gong Kefdb31922022-06-17 17:11:16 +08002128 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002129 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 +08002130 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002131
2132 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2133
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002134 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2135 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2136 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002137 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002138 error = dvr_playback_resume(ctx->playback.player);
2139 } else if (ctx->playback.speed == 0.0f
2140 && speed != 0.0f
2141 && speed != 100.0f) {
2142 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002143 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002144 error = dvr_playback_resume(ctx->playback.player);
2145 }
2146
2147 ctx->playback.speed = speed;
2148
Wentao MA96f68962022-06-15 19:45:35 +08002149 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002150 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002151 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002152
2153 return error;
2154}
2155
hualing chen03fd4942021-07-15 15:56:41 +08002156int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2157{
2158 DVR_WrapperCtx_t *ctx;
2159 int error;
2160
2161 DVR_RETURN_IF_FALSE(playback);
2162
2163 ctx = ctx_getPlayback((unsigned long)playback);
2164 DVR_RETURN_IF_FALSE(ctx);
2165
Gong Kefdb31922022-06-17 17:11:16 +08002166 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002167
Wentao MAe8ba5172022-08-09 11:18:17 +08002168 DVR_WRAPPER_INFO("set_limit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002169 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002170
2171 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002172 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002173
Gong Kefdb31922022-06-17 17:11:16 +08002174 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002175
2176 return error;
2177}
2178
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002179int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2180{
2181 DVR_WrapperCtx_t *ctx;
2182 int error;
2183 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2184 uint64_t segment_id;
2185 uint32_t off;
2186 uint64_t last_segment_id;
2187 uint32_t pre_off;
2188
2189 DVR_RETURN_IF_FALSE(playback);
2190
2191 ctx = ctx_getPlayback((unsigned long)playback);
2192 DVR_RETURN_IF_FALSE(ctx);
2193
Gong Kefdb31922022-06-17 17:11:16 +08002194 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002195
Wentao MA96f68962022-06-15 19:45:35 +08002196 DVR_WRAPPER_INFO("seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002197 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002198
2199 off = 0;
2200 segment_id = 0;
2201 pre_off = 0;
2202 last_segment_id = 0;
2203
hualing chen03fd4942021-07-15 15:56:41 +08002204 //if set limit info we need check ts data is
2205 //expired when seek
2206 if (dvr_playback_check_limit(ctx->playback.player)) {
2207 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2208 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002209 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002210 ctx->sn, time_offset, expired);
2211 time_offset = expired;
2212 }
2213 }
2214
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002215 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2216 segment_id = pseg->seg_info.id;
2217
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002218 if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset)
2219 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002220
2221 last_segment_id = pseg->seg_info.id;
2222 pre_off += pseg->seg_info.duration;
2223 }
2224
2225 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002226 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2227 off = time_offset;
2228 } else if (ctx->playback.obsolete.time >= time_offset) {
2229 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002230 } else {
hualing chenda76fc52020-05-28 14:56:42 +08002231 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002232 }
2233
Wentao MA96f68962022-06-15 19:45:35 +08002234 DVR_WRAPPER_INFO("seek playback(sn:%ld) (seg:%lld, off:%d)\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002235 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002236 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
Wentao MA96f68962022-06-15 19:45:35 +08002237 DVR_WRAPPER_INFO("playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002238
Gong Kefdb31922022-06-17 17:11:16 +08002239 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002240
2241 return error;
2242}
2243
2244int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2245{
2246 DVR_WrapperCtx_t *ctx;
2247 int error;
2248 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2249
2250 DVR_RETURN_IF_FALSE(playback);
2251 DVR_RETURN_IF_FALSE(p_pids);
2252
2253 ctx = ctx_getPlayback((unsigned long)playback);
2254 DVR_RETURN_IF_FALSE(ctx);
2255
Gong Kefdb31922022-06-17 17:11:16 +08002256 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002257
Wentao MA96f68962022-06-15 19:45:35 +08002258 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002259 ctx->sn,
2260 p_pids->video.pid, p_pids->video.format,
2261 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002262 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002263
2264 ctx->playback.pids_req = *p_pids;
2265
2266 error = 0;
2267 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2268 /*should update the whole list of segments*/
2269 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
2270 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
2271 /*check udpate for pids*/
2272 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2273 pseg->playback_info.pids = *p_pids;
2274 error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
2275 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002276 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002277 ctx->sn, pseg->seg_info.id, error);
2278 /*do not break, let list updated*/
2279 }
2280 }
2281 }
2282 /*break;*/
2283 }
2284 }
2285
Wentao MA96f68962022-06-15 19:45:35 +08002286 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002287 ctx->sn,
2288 p_pids->video.pid, p_pids->video.format,
2289 p_pids->audio.pid, p_pids->audio.format,
2290 error);
2291
Gong Kefdb31922022-06-17 17:11:16 +08002292 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002293
2294 return error;
2295}
2296
hualing chena5f03222021-12-02 11:22:35 +08002297int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2298{
2299 DVR_WrapperCtx_t *ctx;
2300 int error;
2301 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2302
2303 DVR_RETURN_IF_FALSE(playback);
2304 DVR_RETURN_IF_FALSE(p_pids);
2305
2306 ctx = ctx_getPlayback((unsigned long)playback);
2307 DVR_RETURN_IF_FALSE(ctx);
2308
Gong Kefdb31922022-06-17 17:11:16 +08002309 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002310
Wentao MA96f68962022-06-15 19:45:35 +08002311 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
hualing chena5f03222021-12-02 11:22:35 +08002312 ctx->sn,
2313 p_pids->video.pid, p_pids->video.format,
2314 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002315 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002316
2317 ctx->playback.pids_req = *p_pids;
2318
2319 error = 0;
2320 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2321 /*should update the whole list of segments*/
2322 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
2323 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
2324 /*check udpate for pids*/
2325 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2326 pseg->playback_info.pids = *p_pids;
2327 error = dvr_playback_only_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
2328 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002329 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002330 ctx->sn, pseg->seg_info.id, error);
2331 /*do not break, let list updated*/
2332 }
2333 }
2334 }
2335 /*break;*/
2336 }
2337 }
2338
Wentao MA96f68962022-06-15 19:45:35 +08002339 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002340 ctx->sn,
2341 p_pids->video.pid, p_pids->video.format,
2342 p_pids->audio.pid, p_pids->audio.format,
2343 error);
2344
Gong Kefdb31922022-06-17 17:11:16 +08002345 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002346
2347 return error;
2348}
2349
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002350int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2351{
2352 DVR_WrapperCtx_t *ctx;
2353 DVR_WrapperPlaybackStatus_t s;
2354 DVR_PlaybackStatus_t play_status;
2355 int error;
2356
2357 DVR_RETURN_IF_FALSE(playback);
2358 DVR_RETURN_IF_FALSE(status);
2359
2360 ctx = ctx_getPlayback((unsigned long)playback);
2361 DVR_RETURN_IF_FALSE(ctx);
2362
Gong Kefdb31922022-06-17 17:11:16 +08002363 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002364
Wentao MA96f68962022-06-15 19:45:35 +08002365 DVR_WRAPPER_INFO("get playback(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002366 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002367
2368 error = dvr_playback_get_status(ctx->playback.player, &play_status);
Wentao MA96f68962022-06-15 19:45:35 +08002369 DVR_WRAPPER_INFO("playback(sn:%ld) get status (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002370
2371 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002372 error = process_generatePlaybackStatus(ctx, &s);
2373
hualing chenb5cd42e2020-04-15 17:03:34 +08002374 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2375 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002376 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002377 s.info_cur.time = s.info_full.time;
2378 }
Wentao MA96f68962022-06-15 19:45:35 +08002379 DVR_WRAPPER_INFO("playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002380 ctx->sn,
2381 s.state,
2382 s.info_cur.time,
2383 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002384 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002385 error);
2386
2387 *status = s;
2388
Gong Kefdb31922022-06-17 17:11:16 +08002389 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002390
2391 return error;
2392}
2393
hualing chen266b9502020-04-04 17:39:39 +08002394int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2395{
2396 DVR_WrapperCtx_t *ctx;
2397 int error;
2398
2399 DVR_RETURN_IF_FALSE(playback);
2400 DVR_RETURN_IF_FALSE(p_secure_buf);
2401
2402 ctx = ctx_getPlayback((unsigned long)playback);
2403 DVR_RETURN_IF_FALSE(ctx);
2404
Gong Kefdb31922022-06-17 17:11:16 +08002405 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002406 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002407 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002408 return error;
2409}
2410
2411int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2412{
2413 DVR_WrapperCtx_t *ctx;
2414 int error;
2415
2416 DVR_RETURN_IF_FALSE(playback);
2417 DVR_RETURN_IF_FALSE(func);
2418
2419 ctx = ctx_getPlayback((unsigned long)playback);
2420 DVR_RETURN_IF_FALSE(ctx);
2421
Gong Kefdb31922022-06-17 17:11:16 +08002422 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002423 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002424 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002425 return error;
2426}
2427
Zhiqiang Han620b9252021-11-09 14:23:20 +08002428int dvr_wrapper_segment_del_by_location (const char *location)
2429{
2430 char fpath[DVR_MAX_LOCATION_SIZE];
2431
2432 DVR_RETURN_IF_FALSE(location);
2433
2434 /*del the stats file*/
2435 sprintf(fpath, "%s.stats", location);
2436 unlink(fpath);
2437
2438 return dvr_segment_del_by_location(location);
2439}
2440
2441int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2442{
2443 FILE *fp;
2444 char fpath[DVR_MAX_LOCATION_SIZE];
2445
2446 DVR_RETURN_IF_FALSE(location);
2447 DVR_RETURN_IF_FALSE(p_info);
2448
2449 if (p_info)
2450 memset(p_info, 0, sizeof(p_info[0]));
2451
2452 memset(fpath, 0, sizeof(fpath));
2453 sprintf(fpath, "%s.stats", location);
2454
2455 /*stats file exists*/
2456 if ((fp = fopen(fpath, "r"))) {
2457 char buf[256];
2458
2459 if (fgets(buf, sizeof(buf), fp) != NULL
2460 && (sscanf(buf, ":%llu:%lu:%u",
2461 &p_info->size,
2462 &p_info->time,
2463 &p_info->pkts) == 3)) {
2464 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002465 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 +08002466 return DVR_SUCCESS;
2467 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002468 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002469 }
2470
2471 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002472 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002473
2474 int error;
2475 uint32_t n_ids;
2476 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002477
hualing chen8aed9582021-12-24 17:59:56 +08002478 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002479
Zhiqiang Han620b9252021-11-09 14:23:20 +08002480 if (!error) {
2481 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002482 struct list_head info_list; /**< segment list head*/
2483 INIT_LIST_HEAD(&info_list);
2484
2485 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002486 error = dvr_segment_get_allInfo(location, &info_list);
2487 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08002488 DVR_RecordSegmentInfo_t info;
2489
2490 memset(&info, 0, sizeof(info));
2491 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002492 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002493 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002494
2495 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002496 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002497 if (!error) {
2498 p_info->size += info.size;
2499 p_info->time += info.duration;
2500 p_info->pkts += info.nb_packets;
2501 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002502 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002503 break;
2504 }
2505 }
2506 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002507 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002508 for (i = 0; i < n_ids; i++) {
2509
2510 DVR_RecordSegmentInfo_t *seg_info;
2511 DVR_PlaybackSegmentFlag_t flags;
2512 int found = 0;
2513 list_for_each_entry(seg_info, &info_list, head)
2514 {
hualing chen8aed9582021-12-24 17:59:56 +08002515 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002516 found = 1;
2517 break;
2518 }
2519 }
2520 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002521 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 +08002522 if (p_ids[i] == n_ids - 1) {
2523 DVR_RecordSegmentInfo_t info;
Wentao MA96f68962022-06-15 19:45:35 +08002524 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 +08002525 error = dvr_segment_get_info(location, p_ids[i], &info);
2526 if (!error) {
2527 p_info->size += info.size;
2528 p_info->time += info.duration;
2529 p_info->pkts += info.nb_packets;
2530 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002531 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002532 break;
2533 }
2534 }
hualing chenb9a02922021-12-14 11:29:47 +08002535 continue;
2536 }
2537
2538 if (!error) {
2539 p_info->size += seg_info->size;
2540 p_info->time += seg_info->duration;
2541 p_info->pkts += seg_info->nb_packets;
2542 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002543 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002544 break;
2545 }
2546 }
2547 //free list
2548 DVR_RecordSegmentInfo_t *segment = NULL;
2549 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2550 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2551 {
2552 if (segment) {
2553 list_del(&segment->head);
2554 free(segment);
2555 }
2556 }
2557 }
2558 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002559 } else {
2560 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002561 }
Wentao MA96f68962022-06-15 19:45:35 +08002562 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 +08002563
2564 return (error)? DVR_FAILURE : DVR_SUCCESS;
2565}
2566
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002567static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2568{
2569 DVR_WrapperEventCtx_t evt;
2570
2571 DVR_RETURN_IF_FALSE(userdata);
2572
2573 evt.sn = (unsigned long)userdata;
2574 evt.type = W_REC;
2575 evt.record.event = event;
2576 evt.record.status = *(DVR_RecordStatus_t *)params;
Wentao MA96f68962022-06-15 19:45:35 +08002577 DVR_WRAPPER_INFO("evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002578 return ctx_addRecordEvent(&evt);
2579}
2580
2581static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2582{
2583 DVR_WrapperEventCtx_t evt;
2584
2585 DVR_RETURN_IF_FALSE(userdata);
2586
2587 evt.sn = (unsigned long)userdata;
2588 evt.type = W_PLAYBACK;
2589 evt.playback.event = event;
2590 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA96f68962022-06-15 19:45:35 +08002591 DVR_WRAPPER_INFO("evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002592 return ctx_addPlaybackEvent(&evt);
2593}
2594
2595static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2596{
Wentao MA96f68962022-06-15 19:45:35 +08002597 DVR_WRAPPER_INFO("notify(sn:%ld) evt(0x%x) statistic:time/size/pkts(%ld/%lld/%u) obsl:(%ld/%llu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002598 ctx->sn,
2599 evt,
2600 status->info.time,
2601 status->info.size,
2602 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002603 status->info_obsolete.time,
2604 status->info_obsolete.size,
2605 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002606
2607 if (ctx->record.event_fn)
2608 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2609 return 0;
2610}
2611
Zhiqiang Han620b9252021-11-09 14:23:20 +08002612static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2613{
2614 FILE *fp;
2615 char fpath[DVR_MAX_LOCATION_SIZE];
2616
2617 DVR_RETURN_IF_FALSE(location);
2618 DVR_RETURN_IF_FALSE(p_status);
2619
2620 sprintf(fpath, "%s.stats", location);
2621
2622 /*stats file*/
2623 if ((fp = fopen(fpath, "w"))) {
2624 char buf[256];
2625 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2626 p_status->info.size - p_status->info_obsolete.size,
2627 p_status->info.time - p_status->info_obsolete.time,
2628 p_status->info.pkts - p_status->info_obsolete.pkts);
2629 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002630 fflush(fp);
2631 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002632 fclose(fp);
2633 return DVR_SUCCESS;
2634 }
2635
2636 return DVR_FAILURE;
2637}
2638
2639
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002640static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2641{
2642 DVR_RecordStartParams_t param;
2643 DVR_RecordSegmentInfo_t seg_info;
2644 int i;
2645 int error;
2646
2647 memcpy(&param, &ctx->record.param_update, sizeof(param));
2648 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2649 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2650 for (i = 0; i < param.segment.nb_pids; i++) {
2651 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2652 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2653 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2654 ctx->record.param_update.segment.nb_pids++;
2655 }
2656 }
2657 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2658 {
2659 DVR_RecordSegmentInfo_t new_seg_info =
2660 { .id = ctx->record.param_update.segment.segment_id, };
2661 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2662 wrapper_addRecordSegment(ctx, &new_seg_info);
2663 }
2664
Wentao MA96f68962022-06-15 19:45:35 +08002665 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002666 return error;
2667}
2668
2669static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg)
2670{
2671 return wrapper_removeRecordSegment(ctx, pseg);
2672}
2673
2674/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002675static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002676{
2677 /*the current seg is not covered in the statistics*/
2678 DVR_WrapperRecordSegmentInfo_t *pseg;
2679
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002680 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002681 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2682
2683 ctx->record.status.state = ctx->record.seg_status.state;
2684 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2685 memcpy(ctx->record.status.pids.pids,
2686 ctx->record.seg_status.info.pids,
2687 sizeof(ctx->record.status.pids.pids));
2688 ctx->current_segment_id = ctx->record.seg_status.info.id;
2689
2690 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2691 if (pseg->info.id != ctx->record.seg_status.info.id) {
2692 ctx->record.status.info.time += pseg->info.duration;
2693 ctx->record.status.info.size += pseg->info.size;
2694 ctx->record.status.info.pkts += pseg->info.nb_packets;
2695 }
2696 }
2697
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002698 ctx->record.status.info_obsolete = ctx->record.obsolete;
2699
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002700 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002701
2702 if (status) {
2703 *status = ctx->record.status;
2704 status->info.time += ctx->record.seg_status.info.duration;
2705 status->info.size += ctx->record.seg_status.info.size;
2706 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2707 }
2708
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002709 return DVR_SUCCESS;
2710}
2711
2712
2713static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2714{
2715 DVR_WrapperRecordStatus_t status;
2716
2717 memset(&status, 0, sizeof(status));
2718
Wentao MA96f68962022-06-15 19:45:35 +08002719 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002720 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002721 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002722 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002723 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002724 return 0;
2725 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002726 switch (evt->record.event)
2727 {
2728 case DVR_RECORD_EVENT_STATUS:
2729 {
2730 switch (evt->record.status.state)
2731 {
2732 case DVR_RECORD_STATE_OPENED:
2733 case DVR_RECORD_STATE_CLOSED:
2734 {
2735 ctx->record.seg_status = evt->record.status;
2736
2737 status.state = evt->record.status.state;
2738 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002739 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002740 } break;
2741 case DVR_RECORD_STATE_STARTED:
2742 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002743 ctx->record.seg_status = evt->record.status;
2744
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002745 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002746 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002747 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002748
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002749 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002750 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002751 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002752 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 +08002753 ctx->sn,
2754 evt->record.status.info.size,
2755 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002756 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2757 /*should notify the recording's stop*/
2758 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002759 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002760 ctx->sn, error);
2761 status.state = DVR_RECORD_STATE_CLOSED;
2762 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002763 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002764 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002765 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002766
2767 if (ctx->record.param_open.is_timeshift
2768 && ctx->record.param_open.max_time
2769 && status.info.time >= ctx->record.param_open.max_time) {
2770 DVR_WrapperRecordSegmentInfo_t *pseg;
2771
2772 /*as the player do not support null playlist,
2773 there must be one segment existed at any time,
2774 we have to keep two segments before remove one*/
2775 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2776 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
2777 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002778 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 +08002779 status.info.size,
2780 ctx->record.param_open.max_time,
2781 ctx->record.param_open.segment_size);
2782 } else {
2783 /*timeshifting, remove the 1st segment and notify the player*/
2784 record_removeSegment(ctx, pseg);
2785
2786 process_generateRecordStatus(ctx, &status);
2787 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002788 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002789 }
2790 }
2791
Wentao MAf4072032022-06-30 13:50:45 +08002792 const loff_t actual_size = status.info.size;
2793 const loff_t max_size = ctx->record.param_open.max_size;
2794 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002795
Wentao MAf4072032022-06-30 13:50:45 +08002796 if (ctx->record.param_open.is_timeshift && max_size) {
2797 if (actual_size >= max_size) {
2798 DVR_WrapperRecordSegmentInfo_t *pseg;
2799 /*as the player do not support null playlist,
2800 there must be one segment existed at any time,
2801 we have to keep two segments before remove one*/
2802 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2803 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
2804 /*only one segment, waiting for more*/
2805 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2806 actual_size, segment_size);
2807 } else {
2808 record_removeSegment(ctx, pseg);
2809
2810 process_generateRecordStatus(ctx, &status);
2811 process_notifyRecord(ctx, evt->record.event, &status);
2812 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2813 }
2814 if (actual_size >= max_size + segment_size/2) {
2815 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2816 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002817 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002818 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002819 }
2820 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002821 } break;
2822 case DVR_RECORD_STATE_STOPPED:
2823 {
2824 ctx->record.seg_status = evt->record.status;
2825
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002826 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002827 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002828 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002829 } break;
2830 default:
2831 break;
2832 }
2833 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002834 case DVR_RECORD_EVENT_WRITE_ERROR: {
2835 ctx->record.seg_status = evt->record.status;
2836 status.state = evt->record.status.state;
2837 process_notifyRecord(ctx, evt->record.event, &status);
2838 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002839 default:
2840 break;
2841 }
2842 return DVR_SUCCESS;
2843}
2844
2845static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2846{
Wentao MA96f68962022-06-15 19:45:35 +08002847 DVR_WRAPPER_INFO("notify(sn:%ld) evt(0x%x) statistics:state/cur/full/obsl(%d/%ld/%ld/%ld)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002848 ctx->sn,
2849 evt,
2850 status->state,
2851 status->info_cur.time,
2852 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002853 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002854
2855 if (ctx->playback.event_fn)
2856 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
2857 return 0;
2858}
2859
2860/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002861static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002862{
2863 /*the current seg is not covered in the statistics*/
2864 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2865
2866 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2867 ctx->playback.status.pids = ctx->playback.pids_req;
2868
2869 ctx->playback.status.state = ctx->playback.seg_status.state;
2870 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2871 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2872 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2873
2874 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
hualing chen451c8f72022-03-09 13:05:52 +08002875 if (pseg->seg_info.id == ctx->playback.seg_status.segment_id) {
Wentao MA96f68962022-06-15 19:45:35 +08002876 DVR_WRAPPER_INFO("caulate cur time :[%lld]time[%d]\n", pseg->seg_info.id, pseg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002877 break;
hualing chen451c8f72022-03-09 13:05:52 +08002878 }
2879
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002880 ctx->playback.status.info_cur.time += pseg->seg_info.duration;
2881 ctx->playback.status.info_cur.size += pseg->seg_info.size;
2882 ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets;
2883 }
2884 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2885 ctx->playback.status.info_full.time += pseg->seg_info.duration;
2886 ctx->playback.status.info_full.size += pseg->seg_info.size;
2887 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +08002888 DVR_WRAPPER_INFO("caulate full time :[%lld]time[%d]\n", pseg->seg_info.id, pseg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002889 }
2890
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002891 if (status) {
2892 *status = ctx->playback.status;
2893 /*deal with current, lack size and pkts with the current*/
2894 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08002895 //get last segment id
2896 DVR_WrapperRecordSegmentInfo_t *pseg;
2897
2898 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2899 if (ctx->playback.tf_full == DVR_TRUE && pseg->info.id == ctx->current_segment_id) {
2900 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
2901 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA96f68962022-06-15 19:45:35 +08002902 DVR_WRAPPER_INFO("warning change start time :id[%lld] [%d]cur[%d]\n", pseg->info.id, status->info_obsolete.time, status->info_cur.time);
hualing chen56c0a162022-01-27 17:01:50 +08002903 }
2904 else
2905 {
Wentao MA96f68962022-06-15 19:45:35 +08002906 DVR_WRAPPER_INFO("warning not change start time :ctx->playback.tf_full[%d]id[%lld] [%lld] cur[%d]\n",ctx->playback.tf_full , pseg->info.id, ctx->current_segment_id, status->info_cur.time);
hualing chen56c0a162022-01-27 17:01:50 +08002907 status->info_obsolete.time = ctx->playback.obsolete.time;
2908 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
2909 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002910 }
2911
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002912 return DVR_SUCCESS;
2913}
2914
2915static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2916{
Wentao MA96f68962022-06-15 19:45:35 +08002917 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002918 evt->sn, evt->playback.event,
2919 evt->playback.status.play_status.state,
2920 evt->playback.status.play_status.segment_id,
2921 evt->playback.status.play_status.time_cur,
2922 evt->playback.status.play_status.time_end);
2923
2924 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002925 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2926 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2927 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2928 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002929 ctx->playback.last_event = evt->playback.event;
2930
2931 switch (evt->playback.event)
2932 {
2933 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2934 case DVR_PLAYBACK_EVENT_REACHED_END:
2935 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2936 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002937 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002938 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002939 case DVR_PLAYBACK_EVENT_NODATA:
2940 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002941 {
2942 DVR_WrapperPlaybackStatus_t status;
2943
2944 /*copy status of segment*/
2945 ctx->playback.seg_status = evt->playback.status.play_status;
2946
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002947 /*generate status of the whole playback*/
2948 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002949
2950 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08002951 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08002952 if (ctx->playback.param_open.is_timeshift
2953 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002954 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08002955 }
2956 /*trust the low level, make NO check.
2957 As this evt is changed to only once due to some operations(paused) in low level.
2958 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002959 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08002960 }
2961 */
2962 else {
2963 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002964 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002965 }
hualing chenf291cf32020-06-18 10:50:30 +08002966 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002967 process_notifyPlayback(ctx, evt->playback.event, &status);
2968 }
2969 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002970 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2971 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2972 case DVR_PLAYBACK_EVENT_NO_KEY:
2973 {
Wentao MA96f68962022-06-15 19:45:35 +08002974 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002975 } break;
2976 default:
2977 {
Wentao MA96f68962022-06-15 19:45:35 +08002978 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002979 } break;
2980 }
2981 return 0;
2982}
2983
2984static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2985{
2986 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2987}
2988
Wentao MA96f68962022-06-15 19:45:35 +08002989int dvr_wrapper_set_log_level (int level)
2990{
2991 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
2992 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
2993 return DVR_FAILURE;
2994 }
2995 g_dvr_log_level = level;
2996 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
2997 return DVR_SUCCESS;
2998}
2999