blob: 77418c648122a82348fa35ba1295b80d873d8cda [file] [log] [blame]
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001#include <stddef.h>
Zhiqiang Han620b9252021-11-09 14:23:20 +08002#include <unistd.h>
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003#include <stdlib.h>
4#include <pthread.h>
5#include <string.h>
6#include <time.h>
7#include <errno.h>
Zhiqiang Han18f42c82021-08-11 17:13:28 +08008#include <sys/time.h>
9#include <time.h>
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080010
11#include "dvr_types.h"
12#include "dvr_record.h"
13#include "dvr_crypto.h"
14#include "dvr_playback.h"
15#include "dvr_segment.h"
16
17#include "AmTsPlayer.h"
18
19#include "list.h"
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080020
21#include "dvr_wrapper.h"
22
Wentao MA96f68962022-06-15 19:45:35 +080023#define WRAPPER_LOG_TAG "libdvr-wrapper"
24#define DVR_WRAPPER_DEBUG(...) DVR_LOG_PRINT(LOG_LV_DEBUG, WRAPPER_LOG_TAG, __VA_ARGS__)
25#define DVR_WRAPPER_INFO(...) DVR_LOG_PRINT(LOG_LV_INFO, WRAPPER_LOG_TAG, __VA_ARGS__)
26#define DVR_WRAPPER_WARN(...) DVR_LOG_PRINT(LOG_LV_WARN, WRAPPER_LOG_TAG, __VA_ARGS__)
27#define DVR_WRAPPER_ERROR(...) DVR_LOG_PRINT(LOG_LV_ERROR, WRAPPER_LOG_TAG, __VA_ARGS__)
28#define DVR_WRAPPER_FATAL(...) DVR_LOG_PRINT(LOG_LV_FATAL, WRAPPER_LOG_TAG, __VA_ARGS__)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080029
30/*duration of data to resume if paused with EVT_REACHED_END in timeshifting*/
hualing chen2932d372020-04-29 13:44:00 +080031#define TIMESHIFT_DATA_DURATION_TO_RESUME (600)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080032/*a tolerant gap*/
33#define DVR_PLAYBACK_END_GAP (1000)
34
Wentao MA96f68962022-06-15 19:45:35 +080035int g_dvr_log_level = LOG_LV_DEFAULT;
36
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080037enum {
38 W_REC = 1,
39 W_PLAYBACK = 2,
40};
41
42enum {
43 U_PIDS = 0x01,
44 U_STAT = 0x02,
45 U_ALL = U_PIDS | U_STAT,
46};
47
48typedef struct {
Gong Kefdb31922022-06-17 17:11:16 +080049 pthread_mutex_t lock;
50 pthread_cond_t cond;
51 int inited;
52 int locked;
53} DVR_WrapperMutex_t;
54
55static void
56wrapper_mutex_init (DVR_WrapperMutex_t *lock)
57{
58 pthread_condattr_t cattr;
59
Zhiqiang Han2259da32022-07-07 15:52:58 +080060 if (lock->inited)
Gong Kefdb31922022-06-17 17:11:16 +080061 return;
62
63 pthread_mutex_init(&lock->lock, NULL);
64
65 pthread_condattr_init(&cattr);
66 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
67 pthread_cond_init(&lock->cond, &cattr);
68 pthread_condattr_destroy(&cattr);
69
70 lock->locked = 0;
71 lock->inited = 1;
72}
73
74static int
75wrapper_mutex_lock (DVR_WrapperMutex_t *lock)
76{
77 pthread_mutex_lock(&lock->lock);
78 while (lock->locked) {
79 pthread_cond_wait(&lock->cond, &lock->lock);
80 }
81 lock->locked = 1;
82 pthread_mutex_unlock(&lock->lock);
83
84 return 0;
85}
86
87static int
88wrapper_mutex_unlock (DVR_WrapperMutex_t *lock)
89{
90 pthread_mutex_lock(&lock->lock);
91 lock->locked = 0;
92 pthread_mutex_unlock(&lock->lock);
93 pthread_cond_signal(&lock->cond);
94
95 return 0;
96}
97
98static int
99wrapper_mutex_timedlock (DVR_WrapperMutex_t *lock, struct timespec *tv)
100{
101 int r = 0;
102
103 pthread_mutex_lock(&lock->lock);
104 if (lock->locked) {
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800105 //DVR_WRAPPER_DEBUG("Enter cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800106 r = pthread_cond_timedwait(&lock->cond, &lock->lock, tv);
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800107 //DVR_WRAPPER_DEBUG("Leave cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800108 }
109 if (r == 0) {
110 if (!lock->locked) {
111 lock->locked = 1;
112 } else {
113 r = ETIMEDOUT;
114 }
115 }
116 pthread_mutex_unlock(&lock->lock);
117
118 return r;
119}
120
121#define WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(expr, lock)\
122 do {\
123 if (!(expr)) {\
124 DVR_INFO("%s-%d failed", __func__, __LINE__);\
125 wrapper_mutex_unlock(lock);\
126 return DVR_FAILURE;\
127 }\
128 } while (0);
129
130
131typedef struct {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800132 /*make lock the 1st item in the structure*/
Gong Kefdb31922022-06-17 17:11:16 +0800133 DVR_WrapperMutex_t wrapper_lock;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800134
135 /*rec or play*/
136 int type;
137
138 /*valid if (sn != 0)*/
139 unsigned long sn;
140 unsigned long sn_linked;
141
142 struct list_head segments; /**<head-add list*/
143 uint64_t current_segment_id; /**<id of the current segment*/
144
145 union {
146 struct {
147 DVR_WrapperRecordOpenParams_t param_open;
148 DVR_RecordStartParams_t param_start;
149 DVR_RecordStartParams_t param_update;
150 DVR_RecordHandle_t recorder;
151 DVR_RecordEventFunction_t event_fn;
152 void *event_userdata;
153
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800154 /*total status = seg_status + status + obsolete*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800155 DVR_RecordStatus_t seg_status; /**<status of current segment*/
156 DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
157 uint64_t next_segment_id;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800158
159 DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800160 } record;
161
162 struct {
163 DVR_WrapperPlaybackOpenParams_t param_open;
164 DVR_PlaybackHandle_t player;
165 DVR_PlaybackEventFunction_t event_fn;
166 void *event_userdata;
167
168 /*total status = seg_status + status*/
169 DVR_PlaybackStatus_t seg_status;
170 DVR_WrapperPlaybackStatus_t status;
171 DVR_PlaybackPids_t pids_req;
172 DVR_PlaybackEvent_t last_event;
Zhiqiang Han3eb75f92020-04-08 10:07:55 +0800173 float speed;
hualing chenb5cd42e2020-04-15 17:03:34 +0800174 DVR_Bool_t reach_end;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800175
176 DVR_WrapperInfo_t obsolete;
hualing chen56c0a162022-01-27 17:01:50 +0800177 DVR_Bool_t tf_full;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800178 } playback;
179 };
180} DVR_WrapperCtx_t;
181
182typedef struct {
183 struct list_head head;
184 unsigned long sn;
185
186 /* rec or playback */
187 int type;
188
189 union {
190 struct {
191 DVR_RecordEvent_t event;
192 DVR_RecordStatus_t status;
193 } record;
194 struct {
195 DVR_PlaybackEvent_t event;
196 DVR_Play_Notify_t status;
197 } playback;
198 };
199} DVR_WrapperEventCtx_t;
200
201typedef struct {
202 pthread_mutex_t lock;
203 char *name;
204 int running;
205 pthread_cond_t cond;
206 pthread_t thread;
207 int type;
208} DVR_WrapperThreadCtx_t;
209
210typedef struct {
211 struct list_head head;
212
213 DVR_RecordSegmentInfo_t seg_info;
214 DVR_PlaybackSegmentInfo_t playback_info;
215} DVR_WrapperPlaybackSegmentInfo_t;
216
217typedef struct {
218 struct list_head head;
219
220 DVR_RecordSegmentInfo_t info;
221} DVR_WrapperRecordSegmentInfo_t;
222
223/* serial num generater */
224static unsigned long sn = 1;
225static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER;
226
227static inline unsigned long get_sn()
228{
hualing chenab0d1262021-09-26 15:22:50 +0800229 unsigned long no = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800230
231 pthread_mutex_lock(&sn_lock);
232 no = sn++;
233 if (!no)
234 no = sn++;
235 pthread_mutex_unlock(&sn_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800236 return no;
237}
238
239/* entity ctx */
240#define DVR_WRAPPER_MAX 10
241
242static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] =
243{
244 [0 ... (DVR_WRAPPER_MAX - 1)] =
245 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800246 .type = W_REC,
247 }
248};
249
250static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] =
251{
252 [0 ... (DVR_WRAPPER_MAX - 1)] =
253 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800254 .type = W_PLAYBACK,
255 }
256};
257
258/* events lists */
259static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list);
260static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list);
261
262static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
263static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
264
265static DVR_WrapperThreadCtx_t wrapper_thread[2] =
266{
267 [0] =
268 {
269 .lock = PTHREAD_MUTEX_INITIALIZER,
270 .running = 0,
271 .name = "record",
272 .type = W_REC,
273 },
274 [1] =
275 {
276 .lock = PTHREAD_MUTEX_INITIALIZER,
277 .running = 0,
278 .name = "playback",
279 .type = W_PLAYBACK,
280 },
281};
282
283/*now only support one timeshift now*/
284static unsigned long sn_timeshift_record;
285static unsigned long sn_timeshift_playback;
286
287static void *wrapper_task(void *arg);
288static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx);
289
290static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata);
291static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata);
292
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800293static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status);
294static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800295
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800296static int get_timespec_timeout(int timeout, struct timespec *ts)
297{
298 struct timespec ots;
299 int left, diff;
300
301 clock_gettime(CLOCK_MONOTONIC, &ots);
302
303 ts->tv_sec = ots.tv_sec + timeout / 1000;
304 ts->tv_nsec = ots.tv_nsec;
305
306 left = timeout % 1000;
307 left *= 1000000;
308 diff = 1000000000 - ots.tv_nsec;
309
310 if (diff <= left) {
311 ts->tv_sec++;
312 ts->tv_nsec = left-diff;
313 } else {
314 ts->tv_nsec += left;
315 }
316
317 return 0;
318}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800319
320static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock)
321{
Wentao MA270dc0f2022-08-23 13:17:26 +0800322 DVR_WrapperEventCtx_t *p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800323
324 pthread_mutex_lock(list_lock);
325 if (list_empty(list))
Wentao MA270dc0f2022-08-23 13:17:26 +0800326 p_evt = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800327 else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800328 p_evt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
329 list_del(&p_evt->head);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800330 }
331 pthread_mutex_unlock(list_lock);
332
Wentao MA270dc0f2022-08-23 13:17:26 +0800333 return p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800334}
335
336static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent()
337{
338 return ctx_getEvent(&record_evt_list, &record_evt_list_lock);
339}
340
341static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent()
342{
343 return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock);
344}
345
346static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt)
347{
348 DVR_WrapperEventCtx_t *padd;
349 padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t));
350 DVR_RETURN_IF_FALSE(padd);
351
352 *padd = *evt;
353 pthread_mutex_lock(lock);
354 list_add_tail(&padd->head, list);
355 pthread_mutex_unlock(lock);
356 return DVR_SUCCESS;
357}
358
359static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt)
360{
361 free(evt);
362}
363
364/*useless*/
365static void ctx_cleanOutdatedEvents(struct list_head *evt_list,
366 pthread_mutex_t *evt_list_lock,
367 DVR_WrapperCtx_t *list)
368{
Wentao MA270dc0f2022-08-23 13:17:26 +0800369 DVR_WrapperEventCtx_t *p_evt, *p_evt_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800370 unsigned long sns[DVR_WRAPPER_MAX];
371 int cnt = 0;
372 int i;
373 int found = 0;
374
375 /*copy all valid sns*/
376 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
377 sns[cnt] = list[i].sn;
378 if (!sns[cnt])
379 cnt++;
380 }
381
382 /*free evts that not belong to any valid sns*/
383 pthread_mutex_lock(evt_list_lock);
Wentao MA270dc0f2022-08-23 13:17:26 +0800384 list_for_each_entry_safe(p_evt, p_evt_tmp, evt_list, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800385 for (i = 0; i < cnt; i++) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800386 if (p_evt->sn == sns[i]) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800387 found = 1;
388 break;
389 }
390 }
391 if (!found) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800392 list_del(&p_evt->head);
393 ctx_freeEvent(p_evt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800394 }
395 }
396 pthread_mutex_unlock(evt_list_lock);
397}
398
399static inline void ctx_cleanOutdatedRecordEvents()
400{
401 ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list);
402}
403
404static inline void ctx_cleanOutdatedPlaybackEvents()
405{
406 ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list);
407}
408
hualing chenb9b358a2021-08-17 15:06:36 +0800409//check this play is recording file
410//return 0 if not the recording
411//else return record id
412static inline int ctx_isPlay_recording(char *play_location)
413{
414 int i;
415 DVR_WrapperCtx_t *cnt;
416
417 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
418 cnt = &record_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800419 //DVR_WRAPPER_INFO("[%d]sn[%d]R:[%s]P:[%s] ...\n", i, cnt->sn, cnt->record.param_open.location, play_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800420 if (!strcmp(cnt->record.param_open.location, play_location)) {
Wentao MA96f68962022-06-15 19:45:35 +0800421 DVR_WRAPPER_INFO("[%d]sn[%d]R:[%s]P:[%s] .found..\n", i, cnt->sn, cnt->record.param_open.location, play_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800422 return cnt->sn;
423 }
424 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800425 DVR_WRAPPER_INFO(" not found any play is in recording [%d]", DVR_WRAPPER_MAX);
hualing chenb9b358a2021-08-17 15:06:36 +0800426 return 0;
427}
428//check this record is playing file
429//return 0 if not the playing
430//else return playback id
431static inline int ctx_isRecord_playing(char *rec_location)
432{
433 int i;
434 DVR_WrapperCtx_t *cnt;
435 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
436 cnt = &playback_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800437 //DVR_WRAPPER_INFO("[%d]sn[%d]P[%s]R[%s] ...\n", i, cnt->sn, cnt->playback.param_open.location, rec_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800438 if (!strcmp(cnt->playback.param_open.location, rec_location)) {
Wentao MA96f68962022-06-15 19:45:35 +0800439 DVR_WRAPPER_INFO("[%d]sn[%d]P[%s]R[%s] ..found.\n",i, cnt->sn, cnt->playback.param_open.location, rec_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800440 return cnt->sn;
441 }
442 }
Wentao MA96f68962022-06-15 19:45:35 +0800443 DVR_WRAPPER_INFO(" not found rec is playing [%d]", DVR_WRAPPER_MAX);
hualing chenb9b358a2021-08-17 15:06:36 +0800444 return 0;
445}
446
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800447static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list)
448{
449 int i;
450 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
Gong Kefdb31922022-06-17 17:11:16 +0800451 if (list[i].sn == sn) {
452 wrapper_mutex_init(&list[i].wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800453 return &list[i];
Gong Kefdb31922022-06-17 17:11:16 +0800454 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800455 }
456 return NULL;
457}
458
459static inline void ctx_reset(DVR_WrapperCtx_t *ctx)
460{
461 memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn),
462 0,
463 sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn));
464}
465
466static inline int ctx_valid(DVR_WrapperCtx_t *ctx)
467{
468 return (ctx->sn != 0);
469}
470
471static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn)
472{
473 return ctx_get(sn, record_list);
474}
475
476static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn)
477{
478 return ctx_get(sn, playback_list);
479}
480
481static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *))
482{
483 pthread_mutex_lock(&ctx->lock);
484 if (ctx->running == 0) {
485 pthread_condattr_t attr;
486 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
487 pthread_cond_init(&ctx->cond, &attr);
488 pthread_condattr_destroy(&attr);
Wentao MA96f68962022-06-15 19:45:35 +0800489 DVR_WRAPPER_INFO("start wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800490 pthread_create(&ctx->thread, NULL, thread_fn, ctx);
Wentao MA96f68962022-06-15 19:45:35 +0800491 DVR_WRAPPER_INFO("wrapper thread(%s) started\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800492 }
493 ctx->running++;
494 pthread_mutex_unlock(&ctx->lock);
495 return 0;
496}
497
498static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx)
499{
500 pthread_mutex_lock(&ctx->lock);
501 ctx->running--;
502 if (!ctx->running) {
503 pthread_cond_broadcast(&ctx->cond);
504 pthread_mutex_unlock(&ctx->lock);
505
Wentao MA96f68962022-06-15 19:45:35 +0800506 DVR_WRAPPER_INFO("stop wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800507 pthread_join(ctx->thread, NULL);
Wentao MA96f68962022-06-15 19:45:35 +0800508 DVR_WRAPPER_INFO("wrapper thread(%s) stopped\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800509
510 pthread_mutex_lock(&ctx->lock);
511 if (!ctx->running) /*protect*/
512 pthread_cond_destroy(&ctx->cond);
513 }
514 pthread_mutex_unlock(&ctx->lock);
515 return 0;
516}
517
518#define WRAPPER_THREAD_RECORD (&wrapper_thread[0])
519#define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1])
520
521static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx)
522{
523 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
524 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
525 return wrapper_requestThread(thread_ctx, wrapper_task);
526}
527
528static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx)
529{
530 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
531 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
532 return wrapper_releaseThread(thread_ctx);
533}
534
535static inline int wrapper_releaseThreadForType(int type)
536{
537 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)?
538 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
539 return wrapper_releaseThread(thread_ctx);
540}
541
542static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx)
543{
544 pthread_cond_signal(&thread_ctx->cond);
545}
546
547static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx)
548{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800549 struct timespec rt;
550 get_timespec_timeout(200, &rt);
551 pthread_cond_timedwait(&thread_ctx->cond, &thread_ctx->lock, &rt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800552 return 0;
553}
554
555static inline void wrapper_threadSignalForType(int type)
556{
557 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ?
558 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
559 wrapper_threadSignal(thread_ctx);
560}
561
562static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx)
563{
564 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
565 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
566 wrapper_threadSignal(thread_ctx);
567}
568
569static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx)
570{
571 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
572 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
573 wrapper_threadWait(thread_ctx);
574 return 0;
575}
576
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800577/*return condition, locked if condition == true*/
Gong Kefdb31922022-06-17 17:11:16 +0800578static int wrapper_mutex_lock_if(DVR_WrapperMutex_t *lock, int *condition)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800579{
580 int r2;
581 do {
582 struct timespec rt2;
Gong Kefdb31922022-06-17 17:11:16 +0800583 get_timespec_timeout(10, &rt2);
584 r2 = wrapper_mutex_timedlock(lock, &rt2);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800585 } while (*condition && (r2 == ETIMEDOUT));
586
587 if (!(*condition) && (r2 == 0))
Gong Kefdb31922022-06-17 17:11:16 +0800588 wrapper_mutex_unlock(lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800589
590 return *condition;
591}
592
593static void *wrapper_task(void *arg)
594{
Wentao MA270dc0f2022-08-23 13:17:26 +0800595 DVR_WrapperThreadCtx_t *thread_ctx = (DVR_WrapperThreadCtx_t *)arg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800596 DVR_WrapperEventCtx_t *evt;
597
Wentao MA270dc0f2022-08-23 13:17:26 +0800598 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800599
Wentao MA270dc0f2022-08-23 13:17:26 +0800600 while (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800601 {
602 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800603
Wentao MA270dc0f2022-08-23 13:17:26 +0800604 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800605 if (!evt)
Wentao MA270dc0f2022-08-23 13:17:26 +0800606 ret = wrapper_threadWait(thread_ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800607 }
608
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800609 while (evt) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800610 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
611 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chenbc0aec92021-03-18 14:52:40 +0800612 if (ctx == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800613 DVR_WRAPPER_INFO("warp not get ctx.free event..\n");
hualing chenbc0aec92021-03-18 14:52:40 +0800614 goto processed;
615 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800616 DVR_WRAPPER_INFO("start name(%s) sn(%d) running(%d) type(%d)\n", thread_ctx->name, (int)ctx->sn, thread_ctx->running, thread_ctx->type);
617 if (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800618 /*
619 continue not break,
620 make all events consumed, or mem leak
621 */
Wentao MA270dc0f2022-08-23 13:17:26 +0800622 if (!wrapper_mutex_lock_if(&ctx->wrapper_lock, &thread_ctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800623 goto processed;
624
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800625 if (ctx_valid(ctx)) {
626 /*double check after lock*/
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800627 if (evt->sn == ctx->sn) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800628 pthread_mutex_unlock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800629 process_handleEvents(evt, ctx);
Wentao MA270dc0f2022-08-23 13:17:26 +0800630 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800631 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800632 }
Gong Kefdb31922022-06-17 17:11:16 +0800633 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800634 }
635
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800636processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800637 ctx_freeEvent(evt);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800638
Wentao MA270dc0f2022-08-23 13:17:26 +0800639 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800640 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800641 DVR_WRAPPER_INFO("start name(%s) running(%d) type(%d) con...\n", thread_ctx->name, thread_ctx->running, thread_ctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800642 }
643
Wentao MA270dc0f2022-08-23 13:17:26 +0800644 pthread_mutex_unlock(&thread_ctx->lock);
645 DVR_WRAPPER_INFO("end name(%s) running(%d) type(%d) end...\n", thread_ctx->name, thread_ctx->running, thread_ctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800646 return NULL;
647}
648
649static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
650{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800651 pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800652 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
653 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800654 pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800655 return 0;
656}
657
658static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
659{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800660 pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800661 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
662 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800663 pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800664 return 0;
665}
666
667static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
668{
Wentao MA270dc0f2022-08-23 13:17:26 +0800669 DVR_WrapperPlaybackSegmentInfo_t *p_seg, *p_seg_tmp;
670 list_for_each_entry_safe(p_seg, p_seg_tmp, &ctx->segments, head) {
671 list_del(&p_seg->head);
672 free(p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800673 }
674}
675
Wentao MA270dc0f2022-08-23 13:17:26 +0800676static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800677 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
678{
679 (void)ctx;
680 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800681 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800682 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800683 p_seg->seg_info.nb_pids = seg_info->nb_pids;
684 memcpy(p_seg->seg_info.pids, seg_info->pids, sizeof(p_seg->seg_info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800685 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800686 p_seg->seg_info.duration = seg_info->duration;
687 p_seg->seg_info.size = seg_info->size;
688 p_seg->seg_info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800689 }
hualing chen03fd4942021-07-15 15:56:41 +0800690 //update current segment duration on timeshift mode
hualing chenb9b358a2021-08-17 15:06:36 +0800691 if (ctx->playback.param_open.is_timeshift
692 || ctx_isPlay_recording(ctx->playback.param_open.location))
Wentao MA270dc0f2022-08-23 13:17:26 +0800693 dvr_playback_update_duration(ctx->playback.player,p_seg->seg_info.id,p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800694 /*no changes
695 DVR_PlaybackSegmentFlag_t flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800696 p_seg->playback_info.segment_id = p_seg->seg_info.id;
697 strncpy(p_seg->playback_info.location,
698 ctx->playback.param_open.location, sizeof(p_seg->playback_info.location));
699 p_seg->playback_info.pids = ctx->playback.pids_req;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800700 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
701 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
702 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
Wentao MA270dc0f2022-08-23 13:17:26 +0800703 p_seg->playback_info.flags = flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800704 */
705}
706
707static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
708{
Wentao MA270dc0f2022-08-23 13:17:26 +0800709 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800710
Wentao MA96f68962022-06-15 19:45:35 +0800711 DVR_WRAPPER_INFO("timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800712 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
713
714 if (list_empty(&ctx->segments)) {
Wentao MA96f68962022-06-15 19:45:35 +0800715 DVR_WRAPPER_INFO("timeshift, update while no segment exists, ignore\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800716 return DVR_SUCCESS;
717 }
718
719 /*normally, the last segment added will be updated*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800720 p_seg =
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800721 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800722 if (p_seg->seg_info.id == seg_info->id) {
723 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800724 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800725 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
726 if (p_seg->seg_info.id == seg_info->id) {
727 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800728 break;
729 }
730 }
731 }
732
733 /*need to notify the dvr_playback*/
hualing chenb9b358a2021-08-17 15:06:36 +0800734 if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/
735 || ctx_isPlay_recording(ctx->playback.param_open.location))
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800736 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
737 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
738 if (
739 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
740 (ctx->playback.seg_status.segment_id == seg_info->id
741 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
742 ||
743 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
744 (ctx->playback.seg_status.segment_id != seg_info->id
745 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
746 )
747 {
748 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800749 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800750 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800751 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800752
753 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800754 DVR_WRAPPER_INFO("timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800755 ctx->sn, error,
756 seg_info->id, seg_info->duration,
757 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
758 }
759 }
760
761 return DVR_SUCCESS;
762}
763
Wentao MA270dc0f2022-08-23 13:17:26 +0800764static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800765 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
766{
767 (void)ctx;
768 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800769 p_seg->info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800770 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800771 p_seg->info.nb_pids = seg_info->nb_pids;
772 memcpy(p_seg->info.pids, seg_info->pids, sizeof(p_seg->info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800773 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800774 p_seg->info.duration = seg_info->duration;
775 p_seg->info.size = seg_info->size;
776 p_seg->info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800777 }
778}
779
780static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
781{
Wentao MA270dc0f2022-08-23 13:17:26 +0800782 DVR_WrapperRecordSegmentInfo_t *p_seg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800783
784 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800785 if (!list_empty(&ctx->segments)) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800786 p_seg =
hualing chen266b9502020-04-04 17:39:39 +0800787 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800788 if (p_seg->info.id == seg_info->id) {
789 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800790 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800791 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
792 if (p_seg->info.id == seg_info->id) {
793 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800794 break;
795 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800796 }
797 }
798 }
799
800 /*timeshift, update the segment for playback*/
801 /*
802 the playback should grab the segment info other than the id,
803 and the id will be updated by each segment-add during the recording
804 */
805 /*
806 the playback paused if no data been checked from recording,
807 should resume the player later when there's more data
808 */
hualing chenb9b358a2021-08-17 15:06:36 +0800809 int sn = 0;
810 if (ctx->record.param_open.is_timeshift ||
811 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
812 DVR_WrapperCtx_t *ctx_playback;
813 if (ctx->record.param_open.is_timeshift)
814 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
815 else
816 ctx_playback = ctx_getPlayback(sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800817
818 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800819 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800820 if (ctx_valid(ctx_playback)
hualing chenb9b358a2021-08-17 15:06:36 +0800821 && (ctx_playback->sn == sn_timeshift_playback ||
822 ctx_playback->sn == sn)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800823 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
824 }
Gong Kefdb31922022-06-17 17:11:16 +0800825 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800826 }
827 }
828
829 return DVR_SUCCESS;
830}
831
832static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
833 DVR_RecordSegmentInfo_t *seg_info,
834 DVR_PlaybackPids_t *p_pids,
835 DVR_PlaybackSegmentFlag_t flags)
836{
Wentao MA270dc0f2022-08-23 13:17:26 +0800837 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800838 int error;
839
840 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +0800841 p_seg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
842 if (!p_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800843 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +0800844 DVR_WRAPPER_INFO("memory fail\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800845 return error;
846 }
847
Wentao MA270dc0f2022-08-23 13:17:26 +0800848 /*copy the original segment info*/
849 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800850 /*generate the segment info used in playback*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800851 p_seg->playback_info.segment_id = p_seg->seg_info.id;
Wentao MAe88ad702022-09-02 10:35:00 +0800852 const int len = strlen(ctx->playback.param_open.location);
853 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
854 DVR_WRAPPER_ERROR("Invalid playback.param_open.location length %d", len);
855 return DVR_FAILURE;
856 }
857 strncpy(p_seg->playback_info.location, ctx->playback.param_open.location, len+1);
Wentao MA270dc0f2022-08-23 13:17:26 +0800858 p_seg->playback_info.pids = *p_pids;
859 p_seg->playback_info.flags = flags;
860 list_add(&p_seg->head, &ctx->segments);
861 DVR_WRAPPER_INFO("start to add segment %lld\n", p_seg->playback_info.segment_id);
862 p_seg->playback_info.duration = p_seg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800863
Wentao MA270dc0f2022-08-23 13:17:26 +0800864 error = dvr_playback_add_segment(ctx->playback.player, &p_seg->playback_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800865 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800866 DVR_WRAPPER_INFO("fail to add segment %lld (%d)\n", p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800867 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800868 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
869 ctx->playback.status.info_full.size += p_seg->seg_info.size;
870 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800871 }
872
873 return error;
874}
875
876static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
877{
Wentao MA270dc0f2022-08-23 13:17:26 +0800878 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MA16f870e2022-09-09 11:00:22 +0800879 int error = DVR_SUCCESS;
hualing chenab0d1262021-09-26 15:22:50 +0800880 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800881
Wentao MA270dc0f2022-08-23 13:17:26 +0800882 p_seg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
883 if (!p_seg) {
Wentao MA16f870e2022-09-09 11:00:22 +0800884 DVR_WRAPPER_ERROR("memory allocation failed");
885 return DVR_FAILURE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800886 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800887 p_seg->info = *seg_info;
888 list_add(&p_seg->head, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800889
hualing chenb9b358a2021-08-17 15:06:36 +0800890 if (ctx->record.param_open.is_timeshift ||
891 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
892
893 DVR_WrapperCtx_t *ctx_playback;
894 if (ctx->record.param_open.is_timeshift)
895 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
896 else
897 ctx_playback = ctx_getPlayback(sn);
898
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800899 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) add seg\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800900
901 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800902 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800903 if (ctx_valid(ctx_playback)) {
904 DVR_PlaybackSegmentFlag_t flags;
905
906 /*only if playback has started, the previous segments have been loaded*/
907 if (!list_empty(&ctx_playback->segments)) {
908 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800909 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800910 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
911 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
hualing chen451c8f72022-03-09 13:05:52 +0800912 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800913 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) list empty\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800914 }
hualing chenb9b358a2021-08-17 15:06:36 +0800915 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800916 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800917 }
Gong Kefdb31922022-06-17 17:11:16 +0800918 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800919 }
hualing chen451c8f72022-03-09 13:05:52 +0800920 else
921 {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800922 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid 2\n", ctx->sn, sn);
923 }
924
925 /*if it is not a timeshift recording, but a playing recording,
926 do not forget to obey the recording rule: link the segment!*/
927 if (!ctx->record.param_open.is_timeshift) {
928 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
929 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
hualing chen451c8f72022-03-09 13:05:52 +0800930 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800931 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800932 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
Wentao MAe8ba5172022-08-09 11:18:17 +0800933 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800934 }
935
936 return error;
937}
938
939static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
940{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800941 int error = -1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800942 DVR_WrapperPlaybackSegmentInfo_t *p_seg = NULL, *p_seg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800943 uint32_t off_set = 0;
Wentao MA96f68962022-06-15 19:45:35 +0800944 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800945
Wentao MA270dc0f2022-08-23 13:17:26 +0800946 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
947 if (p_seg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800948
949 if (ctx->current_segment_id == seg_info->id) {
950 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
951
952 /*drive the player out of this will-be-deleted segment*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800953 next_seg = list_prev_entry(p_seg, head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800954
955 if (ctx->playback.speed != 100.0f) {
956 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800957 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800958 }
hualing chenb9a1a2c2021-12-31 11:27:59 +0800959 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
960 off_set = 10 * 1000;
961 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800962 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 +0800963
964 if (ctx->playback.speed == 0.0f) {
965 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +0800966 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 +0800967 } else if (ctx->playback.speed != 100.0f) {
968 DVR_PlaybackSpeed_t dvr_speed = {
969 .speed = { ctx->playback.speed },
970 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
971 };
972 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
Wentao MA96f68962022-06-15 19:45:35 +0800973 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 +0800974 }
975 }
976
977 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
978 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800979 /*remove playback segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +0800980 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 +0800981 }
982
Wentao MA270dc0f2022-08-23 13:17:26 +0800983 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800984
985 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800986 ctx->playback.obsolete.time += p_seg->seg_info.duration;
987 ctx->playback.obsolete.size += p_seg->seg_info.size;
988 ctx->playback.obsolete.pkts += p_seg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +0800989 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 +0800990 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Wentao MA270dc0f2022-08-23 13:17:26 +0800991 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800992 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800993 }
994 }
995
Wentao MA96f68962022-06-15 19:45:35 +0800996 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 +0800997
998 return error;
999}
1000
1001static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
1002{
1003 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08001004 DVR_WrapperRecordSegmentInfo_t *p_seg, *p_seg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001005
Wentao MAf4072032022-06-30 13:50:45 +08001006 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1007 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001008
1009 /*if timeshifting, notify the playback first, then deal with record*/
1010 if (ctx->record.param_open.is_timeshift) {
1011 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1012
1013 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001014 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001015 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1016 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001017 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1018 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1019 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001020 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001021 return DVR_SUCCESS;
1022 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001023 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1024 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1025 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001026 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001027 if (ctx_valid(ctx_playback)
1028 && ctx_playback->sn == sn_timeshift_playback
1029 && !list_empty(&ctx_playback->segments)) {
1030 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
Wentao MA07d3d742022-09-06 09:58:05 +08001031 if (error != DVR_SUCCESS) {
1032 DVR_WRAPPER_ERROR("wrapper_removePlaybackSegment failed with return value %d",error);
1033 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001034 }
hualing chen56c0a162022-01-27 17:01:50 +08001035 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001036 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001037 }
1038 }
1039
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001040 uint64_t id = seg_info->info.id;
1041
Wentao MA270dc0f2022-08-23 13:17:26 +08001042 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
1043 if (p_seg->info.id == id) {
1044 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001045
1046 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001047 ctx->record.obsolete.time += p_seg->info.duration;
1048 ctx->record.obsolete.size += p_seg->info.size;
1049 ctx->record.obsolete.pkts += p_seg->info.nb_packets;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001050
Wentao MA270dc0f2022-08-23 13:17:26 +08001051 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001052 break;
1053 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001054 }
1055
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001056 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001057
Wentao MAf4072032022-06-30 13:50:45 +08001058 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1059 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001060
1061 return error;
1062}
1063
1064int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1065{
1066 int error;
1067 DVR_WrapperCtx_t *ctx;
1068 DVR_RecordOpenParams_t open_param;
1069
1070 DVR_RETURN_IF_FALSE(rec);
1071 DVR_RETURN_IF_FALSE(params);
1072
1073 /*get a free ctx*/
1074 ctx = ctx_getRecord(0);
1075 DVR_RETURN_IF_FALSE(ctx);
1076
Gong Kefdb31922022-06-17 17:11:16 +08001077 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001078
Wentao MA9a164002022-08-29 11:20:24 +08001079 DVR_WRAPPER_INFO("open record(dmx:%d) .is_tf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
hualing chen51652f02020-12-29 16:59:31 +08001080 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001081
1082 ctx_reset(ctx);
1083
1084 ctx->record.param_open = *params;
1085 ctx->record.event_fn = params->event_fn;
1086 ctx->record.event_userdata = params->event_userdata;
1087 ctx->record.next_segment_id = 0;
1088 ctx->current_segment_id = 0;
1089 INIT_LIST_HEAD(&ctx->segments);
1090 ctx->sn = get_sn();
1091
1092 wrapper_requestThreadFor(ctx);
1093
hualing chen266b9502020-04-04 17:39:39 +08001094 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001095 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001096 open_param.dmx_dev_id = params->dmx_dev_id;
1097 open_param.data_from_memory = 0;
1098 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001099 if (params->flush_size) {
1100 open_param.notification_size = params->flush_size;
1101 } else {
1102 open_param.notification_size = 64*1024;
1103 }
hualing chen002e5b92022-02-23 17:51:21 +08001104 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001105 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001106 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001107 open_param.event_fn = wrapper_record_event_handler;
1108 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001109 if (params->keylen) {
1110 open_param.clearkey = params->clearkey;
1111 open_param.cleariv = params->cleariv;
1112 open_param.keylen = params->keylen;
1113 }
wentao.ma35a69d42022-03-10 18:08:40 +08001114 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001115 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001116
1117 error = dvr_record_open(&ctx->record.recorder, &open_param);
1118 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001119 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001120 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001121 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001122 wrapper_releaseThreadForType(ctx->type);
1123 return DVR_FAILURE;
1124 }
1125 if (params->is_timeshift)
1126 sn_timeshift_record = ctx->sn;
1127
Wentao MA96f68962022-06-15 19:45:35 +08001128 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001129
Yahui Han1fbf3292021-11-08 18:17:19 +08001130 if (params->crypto_fn) {
1131 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1132 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001133 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 +08001134 }
hualing chen266b9502020-04-04 17:39:39 +08001135 }
1136
Gong Kefdb31922022-06-17 17:11:16 +08001137 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001138
1139 *rec = (DVR_WrapperRecord_t)ctx->sn;
1140 return DVR_SUCCESS;
1141}
1142
1143int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1144{
1145 DVR_WrapperCtx_t *ctx;
1146 DVR_RecordSegmentInfo_t seg_info;
1147 int error;
1148
1149 DVR_RETURN_IF_FALSE(rec);
1150
1151 ctx = ctx_getRecord((unsigned long)rec);
1152 DVR_RETURN_IF_FALSE(ctx);
1153
Gong Kefdb31922022-06-17 17:11:16 +08001154 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001155 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001156 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001157
1158 memset(&seg_info, 0, sizeof(seg_info));
1159 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1160
1161 error = dvr_record_close(ctx->record.recorder);
1162
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001163 if (ctx->record.param_open.is_timeshift)
1164 sn_timeshift_record = 0;
1165
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001166 ctx_freeSegments(ctx);
1167
Wentao MA96f68962022-06-15 19:45:35 +08001168 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001169 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001170 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001171
1172 wrapper_releaseThreadForType(ctx->type);
1173
1174 return error;
1175}
1176
1177int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1178{
1179 DVR_WrapperCtx_t *ctx;
1180 DVR_RecordStartParams_t *start_param;
1181 int i;
1182 int error;
1183
1184 DVR_RETURN_IF_FALSE(rec);
1185 DVR_RETURN_IF_FALSE(params);
1186
1187 ctx = ctx_getRecord((unsigned long)rec);
1188 DVR_RETURN_IF_FALSE(ctx);
1189
Gong Kefdb31922022-06-17 17:11:16 +08001190 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001191 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 +08001192 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001193
1194 start_param = &ctx->record.param_start;
1195 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001196 const int len = strlen(ctx->record.param_open.location);
1197 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1198 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1199 pthread_mutex_unlock(&ctx->wrapper_lock);
1200 return DVR_FAILURE;
1201 }
1202 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001203 start_param->segment.segment_id = ctx->record.next_segment_id++;
1204 start_param->segment.nb_pids = params->pids_info.nb_pids;
1205 for (i = 0; i < params->pids_info.nb_pids; i++) {
1206 start_param->segment.pids[i] = params->pids_info.pids[i];
1207 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1208 }
hualing chena5f03222021-12-02 11:22:35 +08001209 if (params->save_rec_file == 0)//default is not save
1210 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001211 {
1212 /*sync to update for further use*/
1213 DVR_RecordStartParams_t *update_param;
1214 update_param = &ctx->record.param_update;
1215 memcpy(update_param, start_param, sizeof(*update_param));
1216 for (i = 0; i < update_param->segment.nb_pids; i++)
1217 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1218 }
1219
1220 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1221 {
1222 DVR_RecordSegmentInfo_t new_seg_info =
1223 { .id = start_param->segment.segment_id, };
1224 wrapper_addRecordSegment(ctx, &new_seg_info);
1225 }
1226
Wentao MA96f68962022-06-15 19:45:35 +08001227 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001228
Gong Kefdb31922022-06-17 17:11:16 +08001229 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001230
1231 return error;
1232}
1233
1234int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1235{
1236 DVR_WrapperCtx_t *ctx;
1237 DVR_RecordSegmentInfo_t seg_info;
1238 int error;
1239
1240 DVR_RETURN_IF_FALSE(rec);
1241
1242 ctx = ctx_getRecord((unsigned long)rec);
1243 DVR_RETURN_IF_FALSE(ctx);
1244
Gong Kefdb31922022-06-17 17:11:16 +08001245 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001246 DVR_WRAPPER_INFO("stop record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001247 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001248
1249 memset(&seg_info, 0, sizeof(seg_info));
1250 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1251 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1252
Wentao MA96f68962022-06-15 19:45:35 +08001253 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001254 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001255
1256 return error;
1257}
1258
hualing chen03fd4942021-07-15 15:56:41 +08001259int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1260{
1261 DVR_WrapperCtx_t *ctx;
1262 int error;
1263
1264 DVR_RETURN_IF_FALSE(rec);
1265
1266 ctx = ctx_getRecord((unsigned long)rec);
1267 DVR_RETURN_IF_FALSE(ctx);
1268
Gong Kefdb31922022-06-17 17:11:16 +08001269 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001270 DVR_WRAPPER_INFO("pause record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001271 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001272
1273 error = dvr_record_pause(ctx->record.recorder);
1274
Wentao MA96f68962022-06-15 19:45:35 +08001275 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001276 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001277
1278 return error;
1279}
1280
1281int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1282{
1283 DVR_WrapperCtx_t *ctx;
1284 int error;
1285
1286 DVR_RETURN_IF_FALSE(rec);
1287
1288 ctx = ctx_getRecord((unsigned long)rec);
1289 DVR_RETURN_IF_FALSE(ctx);
1290
Gong Kefdb31922022-06-17 17:11:16 +08001291 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001292 DVR_WRAPPER_INFO("resume record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001293 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001294
1295 error = dvr_record_resume(ctx->record.recorder);
1296
Wentao MA96f68962022-06-15 19:45:35 +08001297 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001298 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001299
1300 return error;
1301}
1302
Wentao MAcdea4762022-04-26 13:28:56 +08001303/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001304static DVR_Bool_t pids_test_include(
1305 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1306 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001307{
Wentao MAcdea4762022-04-26 13:28:56 +08001308 DVR_Bool_t ret = DVR_TRUE;
1309 for (int i=0;i<size2;i++)
1310 { // iterate all elements in arr2 to check if they exist in arr1
1311 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001312
1313 if (act2[i] == DVR_RECORD_PID_CLOSE)
1314 continue;
1315
Wentao MAcdea4762022-04-26 13:28:56 +08001316 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001317 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001318 if (act1[j] != DVR_RECORD_PID_CLOSE
1319 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001320 {
1321 found=DVR_TRUE;
1322 break;
1323 }
1324 }
1325 if (found == DVR_FALSE)
1326 {
Wentao MAcdea4762022-04-26 13:28:56 +08001327 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001328 break;
1329 }
1330 }
Wentao MAcdea4762022-04-26 13:28:56 +08001331 return ret;
1332}
1333
1334static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1335 const DVR_WrapperUpdatePidsParams_t* p2)
1336{
1337 int i=0;
1338 char buf[128]={0};
1339 int cnt=0;
1340 int chars=0;
1341
1342 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1343 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1344
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001345 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1346 p2->pids,p2->pid_action,p2->nb_pids);
1347 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1348 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001349 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001350 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001351
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001352 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001353 for (i=0;i<p1->nb_pids;i++)
1354 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001355 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1356 removed++;
1357 continue;
1358 }
Wentao MAcdea4762022-04-26 13:28:56 +08001359 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1360 if (chars<0)
1361 {
1362 break;
1363 }
1364 cnt += chars;
1365 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001366 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001367 memset(buf,0,sizeof(buf));
1368
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001369 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001370 for (i=0,cnt=0;i<p2->nb_pids;i++)
1371 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001372 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1373 removed++;
1374 continue;
1375 }
Wentao MAcdea4762022-04-26 13:28:56 +08001376 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1377 if (chars<0)
1378 {
1379 break;
1380 }
1381 cnt += chars;
1382 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001383 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001384 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001385 return is_equal;
1386}
1387
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001388int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1389{
1390 DVR_WrapperCtx_t *ctx;
1391 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001392 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001393 int i;
1394 int error;
1395
1396 DVR_RETURN_IF_FALSE(rec);
1397 DVR_RETURN_IF_FALSE(params);
1398
1399 ctx = ctx_getRecord((unsigned long)rec);
1400 DVR_RETURN_IF_FALSE(ctx);
1401
Gong Kefdb31922022-06-17 17:11:16 +08001402 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001403 DVR_WRAPPER_INFO("update record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001404 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001405
1406 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001407 if (pids_equal(&(start_param->segment),params))
1408 {
Gong Kefdb31922022-06-17 17:11:16 +08001409 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001410 return DVR_TRUE;
1411 }
1412
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001413 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001414 const int len = strlen(ctx->record.param_open.location);
1415 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1416 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1417 pthread_mutex_unlock(&ctx->wrapper_lock);
1418 return DVR_FAILURE;
1419 }
1420 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001421 start_param->segment.segment_id = ctx->record.next_segment_id++;
1422 start_param->segment.nb_pids = params->nb_pids;
1423 for (i = 0; i < params->nb_pids; i++) {
1424 start_param->segment.pids[i] = params->pids[i];
1425 start_param->segment.pid_action[i] = params->pid_action[i];
1426 }
1427 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1428 {
1429 DVR_RecordSegmentInfo_t new_seg_info =
1430 { .id = start_param->segment.segment_id, };
1431 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1432 wrapper_addRecordSegment(ctx, &new_seg_info);
1433 }
1434
Wentao MA96f68962022-06-15 19:45:35 +08001435 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001436 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001437
1438 return error;
1439}
1440
1441int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1442{
1443 DVR_WrapperCtx_t *ctx;
1444 DVR_WrapperRecordStatus_t s;
1445 int error;
1446
1447 DVR_RETURN_IF_FALSE(rec);
1448 DVR_RETURN_IF_FALSE(status);
1449
1450 ctx = ctx_getRecord((unsigned long)rec);
1451 DVR_RETURN_IF_FALSE(ctx);
1452
Gong Kefdb31922022-06-17 17:11:16 +08001453 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001454
Wentao MA96f68962022-06-15 19:45:35 +08001455 DVR_WRAPPER_INFO("get record(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001456 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001457
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001458 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001459
Wentao MA96f68962022-06-15 19:45:35 +08001460 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001461 ctx->sn,
1462 s.state,
1463 s.info.time,
1464 s.info.size,
1465 s.info.pkts,
1466 error);
1467
1468 *status = s;
1469
Gong Kefdb31922022-06-17 17:11:16 +08001470 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001471
1472 return error;
1473}
1474
hualing chen4fe3bee2020-10-23 13:58:52 +08001475int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1476{
1477 DVR_WrapperCtx_t *ctx;
1478 int error;
1479
1480 DVR_RETURN_IF_FALSE(rec);
1481
1482 ctx = ctx_getRecord((unsigned long)rec);
1483 DVR_RETURN_IF_FALSE(ctx);
1484
Gong Kefdb31922022-06-17 17:11:16 +08001485 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001486 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001487 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001488 return error;
1489}
1490
hualing chen266b9502020-04-04 17:39:39 +08001491int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1492{
1493 DVR_WrapperCtx_t *ctx;
1494 int error;
1495
1496 DVR_RETURN_IF_FALSE(rec);
1497 DVR_RETURN_IF_FALSE(p_secure_buf);
1498
1499 ctx = ctx_getRecord((unsigned long)rec);
1500 DVR_RETURN_IF_FALSE(ctx);
1501
Gong Kefdb31922022-06-17 17:11:16 +08001502 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001503 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001504 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001505 return error;
1506}
1507
1508int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1509{
1510 DVR_WrapperCtx_t *ctx;
1511 int error;
1512
1513 DVR_RETURN_IF_FALSE(rec);
1514 DVR_RETURN_IF_FALSE(func);
1515
1516 ctx = ctx_getRecord((unsigned long)rec);
1517 DVR_RETURN_IF_FALSE(ctx);
1518
Gong Kefdb31922022-06-17 17:11:16 +08001519 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001520 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001521 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001522 return error;
1523}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001524
1525
1526int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1527{
1528 DVR_WrapperCtx_t *ctx;
1529 DVR_PlaybackOpenParams_t open_param;
1530 int error;
1531
1532 DVR_RETURN_IF_FALSE(playback);
1533 DVR_RETURN_IF_FALSE(params);
1534 DVR_RETURN_IF_FALSE(params->playback_handle);
1535
1536 /*get a free ctx*/
1537 ctx = ctx_getPlayback(0);
1538 DVR_RETURN_IF_FALSE(ctx);
1539
Gong Kefdb31922022-06-17 17:11:16 +08001540 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001541
Wentao MA96f68962022-06-15 19:45:35 +08001542 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 +08001543
1544 ctx_reset(ctx);
1545
1546 ctx->playback.param_open = *params;
1547 ctx->playback.event_fn = params->event_fn;
1548 ctx->playback.event_userdata = params->event_userdata;
1549 ctx->current_segment_id = 0;
1550 INIT_LIST_HEAD(&ctx->segments);
1551 ctx->sn = get_sn();
1552
1553 wrapper_requestThreadFor(ctx);
1554
hualing chen266b9502020-04-04 17:39:39 +08001555 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001556 open_param.dmx_dev_id = params->dmx_dev_id;
1557 open_param.block_size = params->block_size;
1558 open_param.is_timeshift = params->is_timeshift;
1559 //open_param.notification_size = 10*1024; //not supported
1560 open_param.event_fn = wrapper_playback_event_handler;
1561 open_param.event_userdata = (void*)ctx->sn;
1562 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001563 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001564 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001565 open_param.vendor = params->vendor;
1566
Yahui Han1fbf3292021-11-08 18:17:19 +08001567 if (params->keylen) {
1568 open_param.clearkey = params->clearkey;
1569 open_param.cleariv = params->cleariv;
1570 open_param.keylen = params->keylen;
1571 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001572
1573 error = dvr_playback_open(&ctx->playback.player, &open_param);
1574 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001575 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001576 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001577 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001578 wrapper_releaseThreadForType(ctx->type);
1579 return DVR_FAILURE;
1580 }
1581 if (params->is_timeshift)
1582 sn_timeshift_playback = ctx->sn;
1583
Wentao MA270dc0f2022-08-23 13:17:26 +08001584 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001585 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1586 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001587 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001588 }
Gong Kefdb31922022-06-17 17:11:16 +08001589 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001590
1591 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1592 return DVR_SUCCESS;
1593}
1594
1595int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1596{
1597 DVR_WrapperCtx_t *ctx;
1598 int error;
1599
1600 DVR_RETURN_IF_FALSE(playback);
1601
1602 ctx = ctx_getPlayback((unsigned long)playback);
1603 DVR_RETURN_IF_FALSE(ctx);
1604
Gong Kefdb31922022-06-17 17:11:16 +08001605 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001606 DVR_WRAPPER_INFO("close playback(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001607 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001608
1609 if (ctx->playback.param_open.is_timeshift)
1610 sn_timeshift_playback = 0;
1611
1612 /*try stop first*/
1613 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1614
1615 {
1616 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001617 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001618
Wentao MA270dc0f2022-08-23 13:17:26 +08001619 list_for_each_entry(p_seg, &ctx->segments, head) {
1620 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001621 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001622 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001623 }
1624 ctx_freeSegments(ctx);
1625 }
1626
1627 error = dvr_playback_close(ctx->playback.player);
1628
Wentao MA96f68962022-06-15 19:45:35 +08001629 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001630 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001631 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001632
1633 wrapper_releaseThreadForType(ctx->type);
1634
1635 return error;
1636}
1637
1638int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1639{
1640 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001641 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001642 uint64_t *p_segment_ids;
1643 uint32_t segment_nb;
1644 uint32_t i;
1645 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001646 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001647 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001648 DVR_Bool_t is_timeshift = DVR_FALSE;
Wentao MA9628de82022-09-13 14:19:37 +08001649 DVR_PlaybackSegmentFlag_t segflags = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001650
1651 DVR_RETURN_IF_FALSE(playback);
1652 DVR_RETURN_IF_FALSE(p_pids);
1653
hualing chenc110f952021-01-18 11:25:37 +08001654 ctx_record = NULL;
1655
1656 /*lock the recorder to avoid changing the recording segments*/
1657 ctx_record = ctx_getRecord(sn_timeshift_record);
1658
1659 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001660 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001661 if (!ctx_valid(ctx_record)
1662 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001663 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001664 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001665 is_timeshift = DVR_FALSE;
1666 } else {
1667 is_timeshift = DVR_TRUE;
1668 }
1669 }
1670
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001671 ctx = ctx_getPlayback((unsigned long)playback);
1672 DVR_RETURN_IF_FALSE(ctx);
1673
Gong Kefdb31922022-06-17 17:11:16 +08001674 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001675
Wentao MA96f68962022-06-15 19:45:35 +08001676 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 +08001677 ctx->sn,
1678 ctx->playback.param_open.location,
1679 flags,
1680 p_pids->video.pid, p_pids->video.format,
1681 p_pids->audio.pid, p_pids->audio.format,
1682 p_pids->ad.pid, p_pids->ad.format,
1683 p_pids->subtitle.pid, p_pids->subtitle.format,
1684 p_pids->pcr.pid);
1685
Gong Kefdb31922022-06-17 17:11:16 +08001686 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001687
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001688 if (ctx->playback.param_open.is_timeshift) {
1689 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001690 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001691 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001692 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001693 return DVR_FAILURE;
1694 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001695 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001696 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001697 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001698 }
1699
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001700 /*obtain all segments in a list*/
1701 segment_nb = 0;
1702 p_segment_ids = NULL;
1703 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001704 if (!error) {
1705 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001706 struct list_head info_list; /**< segment list head*/
1707 INIT_LIST_HEAD(&info_list);
1708
Wentao MA96f68962022-06-15 19:45:35 +08001709 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001710 //we need free info list buf when we used end.
1711 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001712 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001713 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001714 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1715 ctx->playback.param_open.location, error);
hualing chenb9a02922021-12-14 11:29:47 +08001716 for (i = 0; i < segment_nb; i++) {
1717 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001718
hualing chenb9a02922021-12-14 11:29:47 +08001719 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1720 if (error) {
1721 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001722 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001723 ctx->playback.param_open.location, p_segment_ids[i], error);
1724 break;
1725 }
1726 //add check if has audio or video pid. if not exist. not add segment to playback
1727 int ii = 0;
1728 int has_av = 0;
1729 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1730 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1731 if (type == DVR_STREAM_TYPE_VIDEO ||
1732 type == DVR_STREAM_TYPE_AUDIO ||
1733 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001734 DVR_WRAPPER_INFO("success to get seg av info \n");
1735 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 +08001736 DVR_STREAM_TYPE_VIDEO,
1737 DVR_STREAM_TYPE_AUDIO,
1738 DVR_STREAM_TYPE_AD);
1739 has_av = 1;
1740 //break;
1741 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001742 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 +08001743 DVR_STREAM_TYPE_VIDEO,
1744 DVR_STREAM_TYPE_AUDIO,
1745 DVR_STREAM_TYPE_AD);
1746 }
1747 }
1748 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001749 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001750 continue;
1751 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001752 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001753 }
Wentao MA9628de82022-09-13 14:19:37 +08001754 segflags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1755 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, segflags);
hualing chenb9a02922021-12-14 11:29:47 +08001756 if (error)
1757 break;
1758 /*copy the 1st segment*/
1759 if (got_1st_seg == 0) {
1760 seg_info_1st = seg_info;
1761 got_1st_seg = 1;
1762 }
1763 }
1764 } else {
1765 for (i = 0; i < segment_nb; i++) {
1766 DVR_RecordSegmentInfo_t *seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001767 int found = 0;
1768 list_for_each_entry(seg_info, &info_list, head)
1769 {
hualing chen8aed9582021-12-24 17:59:56 +08001770 if (seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001771 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001772 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001773 break;
1774 }
1775 }
1776 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001777 //last info is not found if when recording occured power off.
1778 if (p_segment_ids[i] == segment_nb - 1) {
1779 DVR_RecordSegmentInfo_t seg_info;
hualing chen8aed9582021-12-24 17:59:56 +08001780
1781 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1782 if (error) {
1783 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001784 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001785 ctx->playback.param_open.location, p_segment_ids[i], error);
1786 break;
1787 }
1788 //
1789 //add check if has audio or video pid. if not exist. not add segment to playback
1790 int ii = 0;
1791 int has_av = 0;
1792 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1793 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1794 if (type == DVR_STREAM_TYPE_VIDEO ||
1795 type == DVR_STREAM_TYPE_AUDIO ||
1796 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001797 DVR_WRAPPER_INFO("success to get seg av info \n");
1798 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 +08001799 DVR_STREAM_TYPE_VIDEO,
1800 DVR_STREAM_TYPE_AUDIO,
1801 DVR_STREAM_TYPE_AD);
1802 has_av = 1;
1803 //break;
1804 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001805 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 +08001806 DVR_STREAM_TYPE_VIDEO,
1807 DVR_STREAM_TYPE_AUDIO,
1808 DVR_STREAM_TYPE_AD);
1809 }
1810 }
1811 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001812 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001813 continue;
1814 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001815 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001816 }
Wentao MA9628de82022-09-13 14:19:37 +08001817 segflags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1818 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, segflags);
hualing chen8aed9582021-12-24 17:59:56 +08001819 if (error)
1820 break;
1821 }
hualing chenb9a02922021-12-14 11:29:47 +08001822 continue;
1823 }
1824
1825 //add check if has audio or video pid. if not exist. not add segment to playback
1826 int ii = 0;
1827 int has_av = 0;
1828 for (ii = 0; ii < seg_info->nb_pids; ii++) {
1829 int type = (seg_info->pids[ii].type >> 24) & 0x0f;
1830 if (type == DVR_STREAM_TYPE_VIDEO ||
1831 type == DVR_STREAM_TYPE_AUDIO ||
1832 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001833 DVR_WRAPPER_INFO("success to get seg av info \n");
1834 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 +08001835 DVR_STREAM_TYPE_VIDEO,
1836 DVR_STREAM_TYPE_AUDIO,
1837 DVR_STREAM_TYPE_AD);
1838 has_av = 1;
1839 //break;
1840 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001841 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 +08001842 DVR_STREAM_TYPE_VIDEO,
1843 DVR_STREAM_TYPE_AUDIO,
1844 DVR_STREAM_TYPE_AD);
1845 }
1846 }
1847 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001848 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001849 continue;
1850 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001851 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001852 }
Wentao MA9628de82022-09-13 14:19:37 +08001853 segflags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1854 error = wrapper_addPlaybackSegment(ctx, seg_info, p_pids, segflags);
hualing chenb9a02922021-12-14 11:29:47 +08001855 if (error)
1856 break;
1857
1858 /*copy the 1st segment*/
1859 if (got_1st_seg == 0) {
1860 seg_info_1st = *seg_info;
1861 got_1st_seg = 1;
1862 }
hualing chen92f3a142020-07-08 20:59:33 +08001863 }
hualing chenb9a02922021-12-14 11:29:47 +08001864 //free list
1865 DVR_RecordSegmentInfo_t *segment = NULL;
1866 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1867 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1868 {
1869 if (segment) {
1870 list_del(&segment->head);
1871 free(segment);
1872 }
1873 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001874 }
hualing chenb9a02922021-12-14 11:29:47 +08001875
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001876 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001877
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001878 /* return if no segment or fail to add */
1879 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001880
Wentao MA270dc0f2022-08-23 13:17:26 +08001881 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001882 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1883 ctx->playback.obsolete = ctx_record->record.obsolete;
1884 }
1885
Wentao MA96f68962022-06-15 19:45:35 +08001886 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001887
1888 ctx->playback.reach_end = DVR_FALSE;
1889 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1890 ctx->playback.speed = 0.0f;
1891 else
1892 ctx->playback.speed = 100.0f;
1893
1894 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001895 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001896 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001897 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001898 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001899 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001900 error = dvr_playback_start(ctx->playback.player, flags);
1901 } else {
1902 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1903 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001904 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001905 ctx->sn, seg_info_1st.id, error);
1906 }
Wentao MA96f68962022-06-15 19:45:35 +08001907 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001908 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001909 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 +08001910 }
1911 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001912
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001913 if (ctx->playback.param_open.is_timeshift) {
1914 /*unlock the recorder locked above*/
1915 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001916 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001917 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001918 ctx->sn, ctx_record->sn);
1919 }
1920 }
Gong Kefdb31922022-06-17 17:11:16 +08001921 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001922
1923 return error;
1924}
hualing chen002e5b92022-02-23 17:51:21 +08001925//stop record and playback
1926int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1927{
1928 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1929 int error;
Wentao MA96f68962022-06-15 19:45:35 +08001930 DVR_WRAPPER_INFO("stop timeshift record\n");
hualing chen002e5b92022-02-23 17:51:21 +08001931
1932 //stop timeshift record
1933 ctx_record = ctx_getRecord(sn_timeshift_record);
1934 error = dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
1935
Wentao MA96f68962022-06-15 19:45:35 +08001936 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08001937 //stop play
1938 error = dvr_wrapper_stop_playback(playback);
1939 //del timeshift file
1940 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001941 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08001942 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
1943 }
1944 return error;
1945}
1946//start record and start playback
1947int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1948{
1949 DVR_WrapperCtx_t *ctx;
1950 DVR_RecordStartParams_t *start_param;
1951 int error;
1952
1953 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
1954 DVR_RETURN_IF_FALSE(ctx);
1955
Gong Kefdb31922022-06-17 17:11:16 +08001956 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001957 DVR_WRAPPER_INFO("restart record(sn:%ld, location:%s)...\n", ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08001958 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08001959
hualing chen451c8f72022-03-09 13:05:52 +08001960 {
1961 //clear old record status
1962 // struct {
1963 // DVR_WrapperRecordOpenParams_t param_open;
1964 // DVR_RecordStartParams_t param_start;
1965 // DVR_RecordStartParams_t param_update;
1966 // DVR_RecordHandle_t recorder;
1967 // DVR_RecordEventFunction_t event_fn;
1968 // void *event_userdata;
1969
1970 // /*total status = seg_status + status + obsolete*/
1971 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
1972 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
1973 // uint64_t next_segment_id;
1974
1975 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
1976 // } record;
1977 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
1978 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
1979 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
1980 }
1981
hualing chen002e5b92022-02-23 17:51:21 +08001982 start_param = &ctx->record.param_start;
1983
1984 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1985 {
1986 DVR_RecordSegmentInfo_t new_seg_info =
1987 { .id = start_param->segment.segment_id, };
1988 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08001989 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 +08001990 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
1991 DVR_RecordStartParams_t *update_param;
1992 update_param = &ctx->record.param_update;
1993 memcpy(update_param, start_param, sizeof(*update_param));
1994 int i = 0;
1995 for (i = 0; i < update_param->segment.nb_pids; i++)
1996 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08001997 }
1998
Wentao MA96f68962022-06-15 19:45:35 +08001999 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08002000
Gong Kefdb31922022-06-17 17:11:16 +08002001 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002002
2003 //start play
Wentao MA96f68962022-06-15 19:45:35 +08002004 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08002005 //clear play statue
2006 ctx = ctx_getPlayback((unsigned long)playback);
2007 if (ctx) {
2008 //clear old playback status
2009 // struct {
2010 // DVR_WrapperPlaybackOpenParams_t param_open;
2011 // DVR_PlaybackHandle_t player;
2012 // DVR_PlaybackEventFunction_t event_fn;
2013 // void *event_userdata;
2014
2015 // /*total status = seg_status + status*/
2016 // DVR_PlaybackStatus_t seg_status;
2017 // DVR_WrapperPlaybackStatus_t status;
2018 // DVR_PlaybackPids_t pids_req;
2019 // DVR_PlaybackEvent_t last_event;
2020 // float speed;
2021 // DVR_Bool_t reach_end;
2022
2023 // DVR_WrapperInfo_t obsolete;
2024 // DVR_Bool_t tf_full;
2025 // } playback;
2026 ctx->playback.tf_full == DVR_FALSE;
2027 ctx->playback.reach_end == DVR_FALSE;
2028 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2029 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2030 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2031 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2032 }
hualing chen002e5b92022-02-23 17:51:21 +08002033 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2034 return error;
2035}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002036
2037int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2038{
2039 DVR_WrapperCtx_t *ctx;
2040 int error;
2041
2042 DVR_RETURN_IF_FALSE(playback);
2043
2044 ctx = ctx_getPlayback((unsigned long)playback);
2045 DVR_RETURN_IF_FALSE(ctx);
2046
Gong Kefdb31922022-06-17 17:11:16 +08002047 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002048 DVR_WRAPPER_INFO("stop playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002049 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002050
2051 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2052
2053 {
2054 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002055 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002056
Wentao MA270dc0f2022-08-23 13:17:26 +08002057 list_for_each_entry(p_seg, &ctx->segments, head) {
2058 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002059 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002060 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002061 }
2062 ctx_freeSegments(ctx);
2063 }
2064
Wentao MA96f68962022-06-15 19:45:35 +08002065 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002066 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002067
2068 return error;
2069}
2070
2071int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2072{
2073 DVR_WrapperCtx_t *ctx;
2074 int error;
2075
2076 DVR_RETURN_IF_FALSE(playback);
2077
2078 ctx = ctx_getPlayback((unsigned long)playback);
2079 DVR_RETURN_IF_FALSE(ctx);
2080
Gong Kefdb31922022-06-17 17:11:16 +08002081 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002082 DVR_WRAPPER_INFO("pause playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002083 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002084 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002085 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002086 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002087
2088 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2089
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002090 ctx->playback.speed = 0.0f;
2091
Wentao MA96f68962022-06-15 19:45:35 +08002092 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002093 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002094
2095 return error;
2096}
2097
2098int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2099{
2100 DVR_WrapperCtx_t *ctx;
2101 int error;
2102
2103 DVR_RETURN_IF_FALSE(playback);
2104
2105 ctx = ctx_getPlayback((unsigned long)playback);
2106 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002107 //if set limit.we need check if seek to valid data when resume
2108 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2109 if (dvr_playback_check_limit(ctx->playback.player)) {
2110 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2111 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002112 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002113 ctx->sn, time_offset, expired);
2114 time_offset = expired;
2115 dvr_wrapper_seek_playback(playback, time_offset);
2116 }
2117 }
Gong Kefdb31922022-06-17 17:11:16 +08002118 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002119 DVR_WRAPPER_INFO("resume playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002120 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002121
2122 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002123 ctx->playback.speed = 100.0f;
2124
Wentao MA96f68962022-06-15 19:45:35 +08002125 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002126 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002127
2128 return error;
2129}
2130
2131int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2132{
2133 DVR_WrapperCtx_t *ctx;
2134 int error;
2135 DVR_PlaybackSpeed_t dvr_speed = {
2136 .speed = { speed },
2137 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2138 };
2139
2140 DVR_RETURN_IF_FALSE(playback);
2141
2142 ctx = ctx_getPlayback((unsigned long)playback);
2143 DVR_RETURN_IF_FALSE(ctx);
2144
Gong Kefdb31922022-06-17 17:11:16 +08002145 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002146 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 +08002147 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002148
2149 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2150
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002151 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2152 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2153 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002154 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002155 error = dvr_playback_resume(ctx->playback.player);
2156 } else if (ctx->playback.speed == 0.0f
2157 && speed != 0.0f
2158 && speed != 100.0f) {
2159 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002160 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002161 error = dvr_playback_resume(ctx->playback.player);
2162 }
2163
2164 ctx->playback.speed = speed;
2165
Wentao MA96f68962022-06-15 19:45:35 +08002166 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002167 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002168 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002169
2170 return error;
2171}
2172
hualing chen03fd4942021-07-15 15:56:41 +08002173int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2174{
2175 DVR_WrapperCtx_t *ctx;
2176 int error;
2177
2178 DVR_RETURN_IF_FALSE(playback);
2179
2180 ctx = ctx_getPlayback((unsigned long)playback);
2181 DVR_RETURN_IF_FALSE(ctx);
2182
Gong Kefdb31922022-06-17 17:11:16 +08002183 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002184
Wentao MAe8ba5172022-08-09 11:18:17 +08002185 DVR_WRAPPER_INFO("set_limit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002186 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002187
2188 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002189 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002190
Gong Kefdb31922022-06-17 17:11:16 +08002191 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002192
2193 return error;
2194}
2195
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002196int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2197{
2198 DVR_WrapperCtx_t *ctx;
2199 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002200 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002201 uint64_t segment_id;
2202 uint32_t off;
2203 uint64_t last_segment_id;
2204 uint32_t pre_off;
2205
2206 DVR_RETURN_IF_FALSE(playback);
2207
2208 ctx = ctx_getPlayback((unsigned long)playback);
2209 DVR_RETURN_IF_FALSE(ctx);
2210
Gong Kefdb31922022-06-17 17:11:16 +08002211 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002212
Wentao MA96f68962022-06-15 19:45:35 +08002213 DVR_WRAPPER_INFO("seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002214 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002215
2216 off = 0;
2217 segment_id = 0;
2218 pre_off = 0;
2219 last_segment_id = 0;
2220
hualing chen03fd4942021-07-15 15:56:41 +08002221 //if set limit info we need check ts data is
2222 //expired when seek
2223 if (dvr_playback_check_limit(ctx->playback.player)) {
2224 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2225 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002226 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002227 ctx->sn, time_offset, expired);
2228 time_offset = expired;
2229 }
2230 }
2231
Wentao MA270dc0f2022-08-23 13:17:26 +08002232 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2233 segment_id = p_seg->seg_info.id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002234
Wentao MA270dc0f2022-08-23 13:17:26 +08002235 if ((ctx->playback.obsolete.time + pre_off + p_seg->seg_info.duration) > time_offset)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002236 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002237
Wentao MA270dc0f2022-08-23 13:17:26 +08002238 last_segment_id = p_seg->seg_info.id;
2239 pre_off += p_seg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002240 }
2241
2242 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002243 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2244 off = time_offset;
2245 } else if (ctx->playback.obsolete.time >= time_offset) {
2246 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002247 } else {
hualing chenda76fc52020-05-28 14:56:42 +08002248 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002249 }
2250
Wentao MA96f68962022-06-15 19:45:35 +08002251 DVR_WRAPPER_INFO("seek playback(sn:%ld) (seg:%lld, off:%d)\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002252 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002253 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
Wentao MA96f68962022-06-15 19:45:35 +08002254 DVR_WRAPPER_INFO("playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002255
Gong Kefdb31922022-06-17 17:11:16 +08002256 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002257
2258 return error;
2259}
2260
2261int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2262{
2263 DVR_WrapperCtx_t *ctx;
2264 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002265 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002266
2267 DVR_RETURN_IF_FALSE(playback);
2268 DVR_RETURN_IF_FALSE(p_pids);
2269
2270 ctx = ctx_getPlayback((unsigned long)playback);
2271 DVR_RETURN_IF_FALSE(ctx);
2272
Gong Kefdb31922022-06-17 17:11:16 +08002273 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002274
Wentao MA96f68962022-06-15 19:45:35 +08002275 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002276 ctx->sn,
2277 p_pids->video.pid, p_pids->video.format,
2278 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002279 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002280
2281 ctx->playback.pids_req = *p_pids;
2282
2283 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +08002284 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002285 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002286 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2287 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2288 /*check update for pids*/
2289 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2290 p_seg->playback_info.pids = *p_pids;
2291 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002292 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002293 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002294 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002295 /*do not break, let list updated*/
2296 }
2297 }
2298 }
2299 /*break;*/
2300 }
2301 }
2302
Wentao MA96f68962022-06-15 19:45:35 +08002303 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002304 ctx->sn,
2305 p_pids->video.pid, p_pids->video.format,
2306 p_pids->audio.pid, p_pids->audio.format,
2307 error);
2308
Gong Kefdb31922022-06-17 17:11:16 +08002309 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002310
2311 return error;
2312}
2313
hualing chena5f03222021-12-02 11:22:35 +08002314int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2315{
2316 DVR_WrapperCtx_t *ctx;
2317 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002318 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002319
2320 DVR_RETURN_IF_FALSE(playback);
2321 DVR_RETURN_IF_FALSE(p_pids);
2322
2323 ctx = ctx_getPlayback((unsigned long)playback);
2324 DVR_RETURN_IF_FALSE(ctx);
2325
Gong Kefdb31922022-06-17 17:11:16 +08002326 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002327
Wentao MA96f68962022-06-15 19:45:35 +08002328 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
hualing chena5f03222021-12-02 11:22:35 +08002329 ctx->sn,
2330 p_pids->video.pid, p_pids->video.format,
2331 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002332 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002333
2334 ctx->playback.pids_req = *p_pids;
2335
2336 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +08002337 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002338 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002339 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2340 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2341 /*check update for pids*/
2342 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2343 p_seg->playback_info.pids = *p_pids;
2344 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002345 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002346 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002347 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002348 /*do not break, let list updated*/
2349 }
2350 }
2351 }
2352 /*break;*/
2353 }
2354 }
2355
Wentao MA96f68962022-06-15 19:45:35 +08002356 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002357 ctx->sn,
2358 p_pids->video.pid, p_pids->video.format,
2359 p_pids->audio.pid, p_pids->audio.format,
2360 error);
2361
Gong Kefdb31922022-06-17 17:11:16 +08002362 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002363
2364 return error;
2365}
2366
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002367int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2368{
2369 DVR_WrapperCtx_t *ctx;
2370 DVR_WrapperPlaybackStatus_t s;
2371 DVR_PlaybackStatus_t play_status;
2372 int error;
2373
2374 DVR_RETURN_IF_FALSE(playback);
2375 DVR_RETURN_IF_FALSE(status);
2376
2377 ctx = ctx_getPlayback((unsigned long)playback);
2378 DVR_RETURN_IF_FALSE(ctx);
2379
Gong Kefdb31922022-06-17 17:11:16 +08002380 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002381
Wentao MA96f68962022-06-15 19:45:35 +08002382 DVR_WRAPPER_INFO("get playback(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002383 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002384
2385 error = dvr_playback_get_status(ctx->playback.player, &play_status);
Wentao MA96f68962022-06-15 19:45:35 +08002386 DVR_WRAPPER_INFO("playback(sn:%ld) get status (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002387
2388 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002389 error = process_generatePlaybackStatus(ctx, &s);
2390
hualing chenb5cd42e2020-04-15 17:03:34 +08002391 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2392 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002393 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002394 s.info_cur.time = s.info_full.time;
2395 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002396 DVR_WRAPPER_INFO("playback(sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002397 ctx->sn,
2398 s.state,
2399 s.info_cur.time,
2400 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002401 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002402 error);
2403
2404 *status = s;
2405
Gong Kefdb31922022-06-17 17:11:16 +08002406 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002407
2408 return error;
2409}
2410
hualing chen266b9502020-04-04 17:39:39 +08002411int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2412{
2413 DVR_WrapperCtx_t *ctx;
2414 int error;
2415
2416 DVR_RETURN_IF_FALSE(playback);
2417 DVR_RETURN_IF_FALSE(p_secure_buf);
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_secure_buffer(ctx->playback.player, p_secure_buf, len);
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
2428int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2429{
2430 DVR_WrapperCtx_t *ctx;
2431 int error;
2432
2433 DVR_RETURN_IF_FALSE(playback);
2434 DVR_RETURN_IF_FALSE(func);
2435
2436 ctx = ctx_getPlayback((unsigned long)playback);
2437 DVR_RETURN_IF_FALSE(ctx);
2438
Gong Kefdb31922022-06-17 17:11:16 +08002439 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002440 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002441 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002442 return error;
2443}
2444
Zhiqiang Han620b9252021-11-09 14:23:20 +08002445int dvr_wrapper_segment_del_by_location (const char *location)
2446{
2447 char fpath[DVR_MAX_LOCATION_SIZE];
2448
2449 DVR_RETURN_IF_FALSE(location);
2450
2451 /*del the stats file*/
2452 sprintf(fpath, "%s.stats", location);
2453 unlink(fpath);
2454
2455 return dvr_segment_del_by_location(location);
2456}
2457
2458int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2459{
2460 FILE *fp;
2461 char fpath[DVR_MAX_LOCATION_SIZE];
2462
2463 DVR_RETURN_IF_FALSE(location);
2464 DVR_RETURN_IF_FALSE(p_info);
2465
2466 if (p_info)
2467 memset(p_info, 0, sizeof(p_info[0]));
2468
2469 memset(fpath, 0, sizeof(fpath));
2470 sprintf(fpath, "%s.stats", location);
2471
2472 /*stats file exists*/
2473 if ((fp = fopen(fpath, "r"))) {
2474 char buf[256];
2475
2476 if (fgets(buf, sizeof(buf), fp) != NULL
2477 && (sscanf(buf, ":%llu:%lu:%u",
2478 &p_info->size,
2479 &p_info->time,
2480 &p_info->pkts) == 3)) {
2481 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002482 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 +08002483 return DVR_SUCCESS;
2484 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002485 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002486 }
2487
2488 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002489 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002490
2491 int error;
2492 uint32_t n_ids;
2493 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002494
hualing chen8aed9582021-12-24 17:59:56 +08002495 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002496
Zhiqiang Han620b9252021-11-09 14:23:20 +08002497 if (!error) {
2498 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002499 struct list_head info_list; /**< segment list head*/
2500 INIT_LIST_HEAD(&info_list);
2501
2502 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002503 error = dvr_segment_get_allInfo(location, &info_list);
2504 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08002505 DVR_RecordSegmentInfo_t info;
2506
2507 memset(&info, 0, sizeof(info));
2508 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002509 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002510 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002511
2512 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002513 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002514 if (!error) {
2515 p_info->size += info.size;
2516 p_info->time += info.duration;
2517 p_info->pkts += info.nb_packets;
2518 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002519 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002520 break;
2521 }
2522 }
2523 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002524 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002525 for (i = 0; i < n_ids; i++) {
2526
2527 DVR_RecordSegmentInfo_t *seg_info;
2528 DVR_PlaybackSegmentFlag_t flags;
2529 int found = 0;
2530 list_for_each_entry(seg_info, &info_list, head)
2531 {
hualing chen8aed9582021-12-24 17:59:56 +08002532 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002533 found = 1;
2534 break;
2535 }
2536 }
2537 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002538 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 +08002539 if (p_ids[i] == n_ids - 1) {
2540 DVR_RecordSegmentInfo_t info;
Wentao MA96f68962022-06-15 19:45:35 +08002541 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 +08002542 error = dvr_segment_get_info(location, p_ids[i], &info);
2543 if (!error) {
2544 p_info->size += info.size;
2545 p_info->time += info.duration;
2546 p_info->pkts += info.nb_packets;
2547 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002548 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002549 break;
2550 }
2551 }
hualing chenb9a02922021-12-14 11:29:47 +08002552 continue;
2553 }
2554
2555 if (!error) {
2556 p_info->size += seg_info->size;
2557 p_info->time += seg_info->duration;
2558 p_info->pkts += seg_info->nb_packets;
2559 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002560 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002561 break;
2562 }
2563 }
2564 //free list
2565 DVR_RecordSegmentInfo_t *segment = NULL;
2566 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2567 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2568 {
2569 if (segment) {
2570 list_del(&segment->head);
2571 free(segment);
2572 }
2573 }
2574 }
2575 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002576 } else {
2577 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002578 }
Wentao MA96f68962022-06-15 19:45:35 +08002579 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 +08002580
2581 return (error)? DVR_FAILURE : DVR_SUCCESS;
2582}
2583
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002584static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2585{
2586 DVR_WrapperEventCtx_t evt;
2587
2588 DVR_RETURN_IF_FALSE(userdata);
2589
2590 evt.sn = (unsigned long)userdata;
2591 evt.type = W_REC;
2592 evt.record.event = event;
2593 evt.record.status = *(DVR_RecordStatus_t *)params;
Wentao MA96f68962022-06-15 19:45:35 +08002594 DVR_WRAPPER_INFO("evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002595 return ctx_addRecordEvent(&evt);
2596}
2597
2598static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2599{
2600 DVR_WrapperEventCtx_t evt;
2601
2602 DVR_RETURN_IF_FALSE(userdata);
2603
2604 evt.sn = (unsigned long)userdata;
2605 evt.type = W_PLAYBACK;
2606 evt.playback.event = event;
2607 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA9a164002022-08-29 11:20:24 +08002608 DVR_WRAPPER_INFO("evt[sn:%ld, playback, evt:0x%x]\n", evt.sn, evt.playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002609 return ctx_addPlaybackEvent(&evt);
2610}
2611
2612static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2613{
Wentao MA270dc0f2022-08-23 13:17:26 +08002614 DVR_WRAPPER_INFO("notify(sn:%ld) evt(0x%x) statistic:time/size/pkts(%ld/%lld/%u) obsolete:(%ld/%llu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002615 ctx->sn,
2616 evt,
2617 status->info.time,
2618 status->info.size,
2619 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002620 status->info_obsolete.time,
2621 status->info_obsolete.size,
2622 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002623
2624 if (ctx->record.event_fn)
2625 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2626 return 0;
2627}
2628
Zhiqiang Han620b9252021-11-09 14:23:20 +08002629static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2630{
2631 FILE *fp;
2632 char fpath[DVR_MAX_LOCATION_SIZE];
2633
2634 DVR_RETURN_IF_FALSE(location);
2635 DVR_RETURN_IF_FALSE(p_status);
2636
2637 sprintf(fpath, "%s.stats", location);
2638
2639 /*stats file*/
2640 if ((fp = fopen(fpath, "w"))) {
2641 char buf[256];
2642 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2643 p_status->info.size - p_status->info_obsolete.size,
2644 p_status->info.time - p_status->info_obsolete.time,
2645 p_status->info.pkts - p_status->info_obsolete.pkts);
2646 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002647 fflush(fp);
2648 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002649 fclose(fp);
2650 return DVR_SUCCESS;
2651 }
2652
2653 return DVR_FAILURE;
2654}
2655
2656
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002657static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2658{
2659 DVR_RecordStartParams_t param;
2660 DVR_RecordSegmentInfo_t seg_info;
2661 int i;
2662 int error;
2663
2664 memcpy(&param, &ctx->record.param_update, sizeof(param));
2665 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2666 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2667 for (i = 0; i < param.segment.nb_pids; i++) {
2668 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2669 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2670 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2671 ctx->record.param_update.segment.nb_pids++;
2672 }
2673 }
2674 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2675 {
2676 DVR_RecordSegmentInfo_t new_seg_info =
2677 { .id = ctx->record.param_update.segment.segment_id, };
2678 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2679 wrapper_addRecordSegment(ctx, &new_seg_info);
2680 }
2681
Wentao MA96f68962022-06-15 19:45:35 +08002682 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002683 return error;
2684}
2685
Wentao MA270dc0f2022-08-23 13:17:26 +08002686static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002687{
Wentao MA270dc0f2022-08-23 13:17:26 +08002688 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002689}
2690
2691/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002692static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002693{
2694 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002695 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002696
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002697 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002698 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2699
2700 ctx->record.status.state = ctx->record.seg_status.state;
2701 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2702 memcpy(ctx->record.status.pids.pids,
2703 ctx->record.seg_status.info.pids,
2704 sizeof(ctx->record.status.pids.pids));
2705 ctx->current_segment_id = ctx->record.seg_status.info.id;
2706
Wentao MA270dc0f2022-08-23 13:17:26 +08002707 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2708 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2709 ctx->record.status.info.time += p_seg->info.duration;
2710 ctx->record.status.info.size += p_seg->info.size;
2711 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002712 }
2713 }
2714
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002715 ctx->record.status.info_obsolete = ctx->record.obsolete;
2716
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002717 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002718
2719 if (status) {
2720 *status = ctx->record.status;
2721 status->info.time += ctx->record.seg_status.info.duration;
2722 status->info.size += ctx->record.seg_status.info.size;
2723 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2724 }
2725
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002726 return DVR_SUCCESS;
2727}
2728
2729
2730static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2731{
2732 DVR_WrapperRecordStatus_t status;
2733
2734 memset(&status, 0, sizeof(status));
2735
Wentao MA96f68962022-06-15 19:45:35 +08002736 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002737 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002738 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002739 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002740 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002741 return 0;
2742 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002743 switch (evt->record.event)
2744 {
2745 case DVR_RECORD_EVENT_STATUS:
2746 {
2747 switch (evt->record.status.state)
2748 {
2749 case DVR_RECORD_STATE_OPENED:
2750 case DVR_RECORD_STATE_CLOSED:
2751 {
2752 ctx->record.seg_status = evt->record.status;
2753
2754 status.state = evt->record.status.state;
2755 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002756 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002757 } break;
2758 case DVR_RECORD_STATE_STARTED:
2759 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002760 ctx->record.seg_status = evt->record.status;
2761
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002762 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002763 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002764 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002765
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002766 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002767 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002768 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002769 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 +08002770 ctx->sn,
2771 evt->record.status.info.size,
2772 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002773 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2774 /*should notify the recording's stop*/
2775 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002776 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002777 ctx->sn, error);
2778 status.state = DVR_RECORD_STATE_CLOSED;
2779 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002780 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002781 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002782 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002783
2784 if (ctx->record.param_open.is_timeshift
2785 && ctx->record.param_open.max_time
2786 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002787 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002788
2789 /*as the player do not support null playlist,
2790 there must be one segment existed at any time,
2791 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002792 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2793 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002794 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002795 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 +08002796 status.info.size,
2797 ctx->record.param_open.max_time,
2798 ctx->record.param_open.segment_size);
2799 } else {
2800 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002801 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002802
2803 process_generateRecordStatus(ctx, &status);
2804 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002805 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002806 }
2807 }
2808
Wentao MAf4072032022-06-30 13:50:45 +08002809 const loff_t actual_size = status.info.size;
2810 const loff_t max_size = ctx->record.param_open.max_size;
2811 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002812
Wentao MAf4072032022-06-30 13:50:45 +08002813 if (ctx->record.param_open.is_timeshift && max_size) {
2814 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002815 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002816 /*as the player do not support null playlist,
2817 there must be one segment existed at any time,
2818 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002819 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2820 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002821 /*only one segment, waiting for more*/
2822 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2823 actual_size, segment_size);
2824 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002825 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002826
2827 process_generateRecordStatus(ctx, &status);
2828 process_notifyRecord(ctx, evt->record.event, &status);
2829 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2830 }
2831 if (actual_size >= max_size + segment_size/2) {
2832 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2833 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002834 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002835 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002836 }
2837 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002838 } break;
2839 case DVR_RECORD_STATE_STOPPED:
2840 {
2841 ctx->record.seg_status = evt->record.status;
2842
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002843 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002844 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002845 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002846 } break;
2847 default:
2848 break;
2849 }
2850 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002851 case DVR_RECORD_EVENT_WRITE_ERROR: {
2852 ctx->record.seg_status = evt->record.status;
2853 status.state = evt->record.status.state;
2854 process_notifyRecord(ctx, evt->record.event, &status);
2855 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002856 default:
2857 break;
2858 }
2859 return DVR_SUCCESS;
2860}
2861
2862static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2863{
Wentao MA270dc0f2022-08-23 13:17:26 +08002864 DVR_WRAPPER_INFO("notify(sn:%ld) evt(0x%x) statistics:state/cur/full/obsolete(%d/%ld/%ld/%ld)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002865 ctx->sn,
2866 evt,
2867 status->state,
2868 status->info_cur.time,
2869 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002870 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002871
2872 if (ctx->playback.event_fn)
2873 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
2874 return 0;
2875}
2876
2877/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002878static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002879{
2880 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002881 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002882
2883 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2884 ctx->playback.status.pids = ctx->playback.pids_req;
2885
2886 ctx->playback.status.state = ctx->playback.seg_status.state;
2887 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2888 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2889 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2890
Wentao MA270dc0f2022-08-23 13:17:26 +08002891 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2892 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
2893 DVR_WRAPPER_INFO("calculate cur time :[%lld]time[%d]\n", p_seg->seg_info.id, p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002894 break;
hualing chen451c8f72022-03-09 13:05:52 +08002895 }
2896
Wentao MA270dc0f2022-08-23 13:17:26 +08002897 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
2898 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
2899 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002900 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002901 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2902 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
2903 ctx->playback.status.info_full.size += p_seg->seg_info.size;
2904 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
2905 DVR_WRAPPER_INFO("calculate full time :[%lld]time[%d]\n", p_seg->seg_info.id, p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002906 }
2907
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002908 if (status) {
2909 *status = ctx->playback.status;
2910 /*deal with current, lack size and pkts with the current*/
2911 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08002912 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08002913 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08002914
Wentao MA270dc0f2022-08-23 13:17:26 +08002915 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2916 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08002917 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
2918 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08002919 DVR_WRAPPER_INFO("warning change start time :id[%lld] [%d]cur[%d]\n", p_seg->info.id, status->info_obsolete.time, status->info_cur.time);
hualing chen56c0a162022-01-27 17:01:50 +08002920 }
2921 else
2922 {
Wentao MA270dc0f2022-08-23 13:17:26 +08002923 DVR_WRAPPER_INFO("warning not change start time :ctx->playback.tf_full[%d]id[%lld] [%lld] cur[%d]\n",ctx->playback.tf_full , p_seg->info.id, ctx->current_segment_id, status->info_cur.time);
hualing chen56c0a162022-01-27 17:01:50 +08002924 status->info_obsolete.time = ctx->playback.obsolete.time;
2925 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
2926 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002927 }
2928
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002929 return DVR_SUCCESS;
2930}
2931
2932static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2933{
Wentao MA96f68962022-06-15 19:45:35 +08002934 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002935 evt->sn, evt->playback.event,
2936 evt->playback.status.play_status.state,
2937 evt->playback.status.play_status.segment_id,
2938 evt->playback.status.play_status.time_cur,
2939 evt->playback.status.play_status.time_end);
2940
2941 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002942 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2943 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2944 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2945 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002946 ctx->playback.last_event = evt->playback.event;
2947
2948 switch (evt->playback.event)
2949 {
2950 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2951 case DVR_PLAYBACK_EVENT_REACHED_END:
2952 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2953 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002954 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002955 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002956 case DVR_PLAYBACK_EVENT_NODATA:
2957 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002958 {
2959 DVR_WrapperPlaybackStatus_t status;
2960
2961 /*copy status of segment*/
2962 ctx->playback.seg_status = evt->playback.status.play_status;
2963
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002964 /*generate status of the whole playback*/
2965 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002966
2967 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08002968 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08002969 if (ctx->playback.param_open.is_timeshift
2970 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002971 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08002972 }
2973 /*trust the low level, make NO check.
2974 As this evt is changed to only once due to some operations(paused) in low level.
2975 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002976 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08002977 }
2978 */
2979 else {
2980 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002981 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002982 }
hualing chenf291cf32020-06-18 10:50:30 +08002983 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002984 process_notifyPlayback(ctx, evt->playback.event, &status);
2985 }
2986 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002987 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2988 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2989 case DVR_PLAYBACK_EVENT_NO_KEY:
2990 {
Wentao MA96f68962022-06-15 19:45:35 +08002991 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002992 } break;
2993 default:
2994 {
Wentao MA96f68962022-06-15 19:45:35 +08002995 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002996 } break;
2997 }
2998 return 0;
2999}
3000
3001static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3002{
3003 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
3004}
3005
Wentao MA96f68962022-06-15 19:45:35 +08003006int dvr_wrapper_set_log_level (int level)
3007{
3008 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3009 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3010 return DVR_FAILURE;
3011 }
3012 g_dvr_log_level = level;
3013 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3014 return DVR_SUCCESS;
3015}
3016
Wentao MA5629ad82022-08-24 10:03:02 +08003017int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3018{
3019 DVR_WrapperCtx_t *ctx;
3020 int error;
3021
3022 DVR_RETURN_IF_FALSE(playback==NULL);
3023
3024 ctx = ctx_getPlayback((unsigned long)playback);
3025 DVR_RETURN_IF_FALSE(ctx);
3026
3027 wrapper_mutex_lock(&ctx->wrapper_lock);
3028
3029 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3030
3031 DVR_WRAPPER_INFO("set ac4 preselection id: %d", presel_id);
3032 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3033
3034 wrapper_mutex_unlock(&ctx->wrapper_lock);
3035
3036 return error;
3037}
3038