blob: 0fb942f18c750cb36b732ba8a7928b51a2e2c78d [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;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800879 int error;
hualing chenab0d1262021-09-26 15:22:50 +0800880 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800881
882 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +0800883 p_seg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
884 if (!p_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800885 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +0800886 DVR_WRAPPER_INFO("memory fail\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800887 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800888 p_seg->info = *seg_info;
889 list_add(&p_seg->head, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800890
hualing chenb9b358a2021-08-17 15:06:36 +0800891 if (ctx->record.param_open.is_timeshift ||
892 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
893
894 DVR_WrapperCtx_t *ctx_playback;
895 if (ctx->record.param_open.is_timeshift)
896 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
897 else
898 ctx_playback = ctx_getPlayback(sn);
899
Wentao MA96f68962022-06-15 19:45:35 +0800900 DVR_WRAPPER_INFO("ctx_playback ---- add segment\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800901
902 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800903 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800904 if (ctx_valid(ctx_playback)) {
905 DVR_PlaybackSegmentFlag_t flags;
906
907 /*only if playback has started, the previous segments have been loaded*/
908 if (!list_empty(&ctx_playback->segments)) {
909 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800910 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800911 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
912 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
hualing chen451c8f72022-03-09 13:05:52 +0800913 } else {
Wentao MA96f68962022-06-15 19:45:35 +0800914 DVR_WRAPPER_INFO("ctx_playback list_empty(&ctx_playback->segments) true\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800915 }
hualing chenb9b358a2021-08-17 15:06:36 +0800916 } else {
Wentao MA96f68962022-06-15 19:45:35 +0800917 DVR_WRAPPER_INFO("ctx_playback ---- not valid\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800918 }
Gong Kefdb31922022-06-17 17:11:16 +0800919 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800920 }
hualing chen451c8f72022-03-09 13:05:52 +0800921 else
922 {
Wentao MA96f68962022-06-15 19:45:35 +0800923 DVR_WRAPPER_INFO("ctx_playback ---- not valid 1\n");
hualing chen451c8f72022-03-09 13:05:52 +0800924 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800925 } else {
Wentao MA96f68962022-06-15 19:45:35 +0800926 DVR_WRAPPER_INFO("ctx_playback -sn[%d]-\n", sn);
Wentao MAe8ba5172022-08-09 11:18:17 +0800927 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800928 }
929
930 return error;
931}
932
933static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
934{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800935 int error = -1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800936 DVR_WrapperPlaybackSegmentInfo_t *p_seg = NULL, *p_seg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800937 uint32_t off_set = 0;
Wentao MA96f68962022-06-15 19:45:35 +0800938 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800939
Wentao MA270dc0f2022-08-23 13:17:26 +0800940 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
941 if (p_seg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800942
943 if (ctx->current_segment_id == seg_info->id) {
944 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
945
946 /*drive the player out of this will-be-deleted segment*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800947 next_seg = list_prev_entry(p_seg, head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800948
949 if (ctx->playback.speed != 100.0f) {
950 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800951 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800952 }
hualing chenb9a1a2c2021-12-31 11:27:59 +0800953 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
954 off_set = 10 * 1000;
955 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800956 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 +0800957
958 if (ctx->playback.speed == 0.0f) {
959 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +0800960 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 +0800961 } else if (ctx->playback.speed != 100.0f) {
962 DVR_PlaybackSpeed_t dvr_speed = {
963 .speed = { ctx->playback.speed },
964 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
965 };
966 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
Wentao MA96f68962022-06-15 19:45:35 +0800967 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 +0800968 }
969 }
970
971 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
972 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800973 /*remove playback segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +0800974 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 +0800975 }
976
Wentao MA270dc0f2022-08-23 13:17:26 +0800977 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800978
979 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800980 ctx->playback.obsolete.time += p_seg->seg_info.duration;
981 ctx->playback.obsolete.size += p_seg->seg_info.size;
982 ctx->playback.obsolete.pkts += p_seg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +0800983 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 +0800984 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Wentao MA270dc0f2022-08-23 13:17:26 +0800985 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800986 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800987 }
988 }
989
Wentao MA96f68962022-06-15 19:45:35 +0800990 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 +0800991
992 return error;
993}
994
995static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
996{
997 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +0800998 DVR_WrapperRecordSegmentInfo_t *p_seg, *p_seg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800999
Wentao MAf4072032022-06-30 13:50:45 +08001000 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1001 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001002
1003 /*if timeshifting, notify the playback first, then deal with record*/
1004 if (ctx->record.param_open.is_timeshift) {
1005 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1006
1007 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001008 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001009 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1010 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001011 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1012 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1013 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001014 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001015 return DVR_SUCCESS;
1016 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001017 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1018 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1019 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001020 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001021 if (ctx_valid(ctx_playback)
1022 && ctx_playback->sn == sn_timeshift_playback
1023 && !list_empty(&ctx_playback->segments)) {
1024 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
1025 }
hualing chen56c0a162022-01-27 17:01:50 +08001026 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001027 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001028 }
1029 }
1030
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001031 uint64_t id = seg_info->info.id;
1032
Wentao MA270dc0f2022-08-23 13:17:26 +08001033 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
1034 if (p_seg->info.id == id) {
1035 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001036
1037 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001038 ctx->record.obsolete.time += p_seg->info.duration;
1039 ctx->record.obsolete.size += p_seg->info.size;
1040 ctx->record.obsolete.pkts += p_seg->info.nb_packets;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001041
Wentao MA270dc0f2022-08-23 13:17:26 +08001042 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001043 break;
1044 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001045 }
1046
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001047 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001048
Wentao MAf4072032022-06-30 13:50:45 +08001049 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1050 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001051
1052 return error;
1053}
1054
1055int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1056{
1057 int error;
1058 DVR_WrapperCtx_t *ctx;
1059 DVR_RecordOpenParams_t open_param;
1060
1061 DVR_RETURN_IF_FALSE(rec);
1062 DVR_RETURN_IF_FALSE(params);
1063
1064 /*get a free ctx*/
1065 ctx = ctx_getRecord(0);
1066 DVR_RETURN_IF_FALSE(ctx);
1067
Gong Kefdb31922022-06-17 17:11:16 +08001068 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001069
Wentao MA9a164002022-08-29 11:20:24 +08001070 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 +08001071 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001072
1073 ctx_reset(ctx);
1074
1075 ctx->record.param_open = *params;
1076 ctx->record.event_fn = params->event_fn;
1077 ctx->record.event_userdata = params->event_userdata;
1078 ctx->record.next_segment_id = 0;
1079 ctx->current_segment_id = 0;
1080 INIT_LIST_HEAD(&ctx->segments);
1081 ctx->sn = get_sn();
1082
1083 wrapper_requestThreadFor(ctx);
1084
hualing chen266b9502020-04-04 17:39:39 +08001085 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001086 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001087 open_param.dmx_dev_id = params->dmx_dev_id;
1088 open_param.data_from_memory = 0;
1089 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001090 if (params->flush_size) {
1091 open_param.notification_size = params->flush_size;
1092 } else {
1093 open_param.notification_size = 64*1024;
1094 }
hualing chen002e5b92022-02-23 17:51:21 +08001095 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001096 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001097 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001098 open_param.event_fn = wrapper_record_event_handler;
1099 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001100 if (params->keylen) {
1101 open_param.clearkey = params->clearkey;
1102 open_param.cleariv = params->cleariv;
1103 open_param.keylen = params->keylen;
1104 }
wentao.ma35a69d42022-03-10 18:08:40 +08001105 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001106 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001107
1108 error = dvr_record_open(&ctx->record.recorder, &open_param);
1109 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001110 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001111 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001112 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001113 wrapper_releaseThreadForType(ctx->type);
1114 return DVR_FAILURE;
1115 }
1116 if (params->is_timeshift)
1117 sn_timeshift_record = ctx->sn;
1118
Wentao MA96f68962022-06-15 19:45:35 +08001119 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001120
Yahui Han1fbf3292021-11-08 18:17:19 +08001121 if (params->crypto_fn) {
1122 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1123 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001124 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 +08001125 }
hualing chen266b9502020-04-04 17:39:39 +08001126 }
1127
Gong Kefdb31922022-06-17 17:11:16 +08001128 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001129
1130 *rec = (DVR_WrapperRecord_t)ctx->sn;
1131 return DVR_SUCCESS;
1132}
1133
1134int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1135{
1136 DVR_WrapperCtx_t *ctx;
1137 DVR_RecordSegmentInfo_t seg_info;
1138 int error;
1139
1140 DVR_RETURN_IF_FALSE(rec);
1141
1142 ctx = ctx_getRecord((unsigned long)rec);
1143 DVR_RETURN_IF_FALSE(ctx);
1144
Gong Kefdb31922022-06-17 17:11:16 +08001145 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001146 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001147 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001148
1149 memset(&seg_info, 0, sizeof(seg_info));
1150 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1151
1152 error = dvr_record_close(ctx->record.recorder);
1153
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001154 if (ctx->record.param_open.is_timeshift)
1155 sn_timeshift_record = 0;
1156
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001157 ctx_freeSegments(ctx);
1158
Wentao MA96f68962022-06-15 19:45:35 +08001159 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001160 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001161 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001162
1163 wrapper_releaseThreadForType(ctx->type);
1164
1165 return error;
1166}
1167
1168int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1169{
1170 DVR_WrapperCtx_t *ctx;
1171 DVR_RecordStartParams_t *start_param;
1172 int i;
1173 int error;
1174
1175 DVR_RETURN_IF_FALSE(rec);
1176 DVR_RETURN_IF_FALSE(params);
1177
1178 ctx = ctx_getRecord((unsigned long)rec);
1179 DVR_RETURN_IF_FALSE(ctx);
1180
Gong Kefdb31922022-06-17 17:11:16 +08001181 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001182 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 +08001183 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001184
1185 start_param = &ctx->record.param_start;
1186 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001187 const int len = strlen(ctx->record.param_open.location);
1188 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1189 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1190 pthread_mutex_unlock(&ctx->wrapper_lock);
1191 return DVR_FAILURE;
1192 }
1193 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001194 start_param->segment.segment_id = ctx->record.next_segment_id++;
1195 start_param->segment.nb_pids = params->pids_info.nb_pids;
1196 for (i = 0; i < params->pids_info.nb_pids; i++) {
1197 start_param->segment.pids[i] = params->pids_info.pids[i];
1198 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1199 }
hualing chena5f03222021-12-02 11:22:35 +08001200 if (params->save_rec_file == 0)//default is not save
1201 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001202 {
1203 /*sync to update for further use*/
1204 DVR_RecordStartParams_t *update_param;
1205 update_param = &ctx->record.param_update;
1206 memcpy(update_param, start_param, sizeof(*update_param));
1207 for (i = 0; i < update_param->segment.nb_pids; i++)
1208 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1209 }
1210
1211 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1212 {
1213 DVR_RecordSegmentInfo_t new_seg_info =
1214 { .id = start_param->segment.segment_id, };
1215 wrapper_addRecordSegment(ctx, &new_seg_info);
1216 }
1217
Wentao MA96f68962022-06-15 19:45:35 +08001218 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001219
Gong Kefdb31922022-06-17 17:11:16 +08001220 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001221
1222 return error;
1223}
1224
1225int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1226{
1227 DVR_WrapperCtx_t *ctx;
1228 DVR_RecordSegmentInfo_t seg_info;
1229 int error;
1230
1231 DVR_RETURN_IF_FALSE(rec);
1232
1233 ctx = ctx_getRecord((unsigned long)rec);
1234 DVR_RETURN_IF_FALSE(ctx);
1235
Gong Kefdb31922022-06-17 17:11:16 +08001236 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001237 DVR_WRAPPER_INFO("stop record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001238 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001239
1240 memset(&seg_info, 0, sizeof(seg_info));
1241 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1242 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1243
Wentao MA96f68962022-06-15 19:45:35 +08001244 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001245 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001246
1247 return error;
1248}
1249
hualing chen03fd4942021-07-15 15:56:41 +08001250int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1251{
1252 DVR_WrapperCtx_t *ctx;
1253 int error;
1254
1255 DVR_RETURN_IF_FALSE(rec);
1256
1257 ctx = ctx_getRecord((unsigned long)rec);
1258 DVR_RETURN_IF_FALSE(ctx);
1259
Gong Kefdb31922022-06-17 17:11:16 +08001260 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001261 DVR_WRAPPER_INFO("pause record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001262 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001263
1264 error = dvr_record_pause(ctx->record.recorder);
1265
Wentao MA96f68962022-06-15 19:45:35 +08001266 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001267 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001268
1269 return error;
1270}
1271
1272int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1273{
1274 DVR_WrapperCtx_t *ctx;
1275 int error;
1276
1277 DVR_RETURN_IF_FALSE(rec);
1278
1279 ctx = ctx_getRecord((unsigned long)rec);
1280 DVR_RETURN_IF_FALSE(ctx);
1281
Gong Kefdb31922022-06-17 17:11:16 +08001282 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001283 DVR_WRAPPER_INFO("resume record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001284 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001285
1286 error = dvr_record_resume(ctx->record.recorder);
1287
Wentao MA96f68962022-06-15 19:45:35 +08001288 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001289 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001290
1291 return error;
1292}
1293
Wentao MAcdea4762022-04-26 13:28:56 +08001294/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001295static DVR_Bool_t pids_test_include(
1296 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1297 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001298{
Wentao MAcdea4762022-04-26 13:28:56 +08001299 DVR_Bool_t ret = DVR_TRUE;
1300 for (int i=0;i<size2;i++)
1301 { // iterate all elements in arr2 to check if they exist in arr1
1302 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001303
1304 if (act2[i] == DVR_RECORD_PID_CLOSE)
1305 continue;
1306
Wentao MAcdea4762022-04-26 13:28:56 +08001307 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001308 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001309 if (act1[j] != DVR_RECORD_PID_CLOSE
1310 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001311 {
1312 found=DVR_TRUE;
1313 break;
1314 }
1315 }
1316 if (found == DVR_FALSE)
1317 {
Wentao MAcdea4762022-04-26 13:28:56 +08001318 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001319 break;
1320 }
1321 }
Wentao MAcdea4762022-04-26 13:28:56 +08001322 return ret;
1323}
1324
1325static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1326 const DVR_WrapperUpdatePidsParams_t* p2)
1327{
1328 int i=0;
1329 char buf[128]={0};
1330 int cnt=0;
1331 int chars=0;
1332
1333 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1334 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1335
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001336 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1337 p2->pids,p2->pid_action,p2->nb_pids);
1338 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1339 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001340 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001341 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001342
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001343 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001344 for (i=0;i<p1->nb_pids;i++)
1345 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001346 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1347 removed++;
1348 continue;
1349 }
Wentao MAcdea4762022-04-26 13:28:56 +08001350 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1351 if (chars<0)
1352 {
1353 break;
1354 }
1355 cnt += chars;
1356 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001357 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001358 memset(buf,0,sizeof(buf));
1359
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001360 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001361 for (i=0,cnt=0;i<p2->nb_pids;i++)
1362 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001363 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1364 removed++;
1365 continue;
1366 }
Wentao MAcdea4762022-04-26 13:28:56 +08001367 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1368 if (chars<0)
1369 {
1370 break;
1371 }
1372 cnt += chars;
1373 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001374 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001375 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001376 return is_equal;
1377}
1378
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001379int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1380{
1381 DVR_WrapperCtx_t *ctx;
1382 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001383 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001384 int i;
1385 int error;
1386
1387 DVR_RETURN_IF_FALSE(rec);
1388 DVR_RETURN_IF_FALSE(params);
1389
1390 ctx = ctx_getRecord((unsigned long)rec);
1391 DVR_RETURN_IF_FALSE(ctx);
1392
Gong Kefdb31922022-06-17 17:11:16 +08001393 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001394 DVR_WRAPPER_INFO("update record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001395 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001396
1397 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001398 if (pids_equal(&(start_param->segment),params))
1399 {
Gong Kefdb31922022-06-17 17:11:16 +08001400 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001401 return DVR_TRUE;
1402 }
1403
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001404 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001405 const int len = strlen(ctx->record.param_open.location);
1406 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1407 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1408 pthread_mutex_unlock(&ctx->wrapper_lock);
1409 return DVR_FAILURE;
1410 }
1411 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001412 start_param->segment.segment_id = ctx->record.next_segment_id++;
1413 start_param->segment.nb_pids = params->nb_pids;
1414 for (i = 0; i < params->nb_pids; i++) {
1415 start_param->segment.pids[i] = params->pids[i];
1416 start_param->segment.pid_action[i] = params->pid_action[i];
1417 }
1418 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1419 {
1420 DVR_RecordSegmentInfo_t new_seg_info =
1421 { .id = start_param->segment.segment_id, };
1422 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1423 wrapper_addRecordSegment(ctx, &new_seg_info);
1424 }
1425
Wentao MA96f68962022-06-15 19:45:35 +08001426 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001427 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001428
1429 return error;
1430}
1431
1432int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1433{
1434 DVR_WrapperCtx_t *ctx;
1435 DVR_WrapperRecordStatus_t s;
1436 int error;
1437
1438 DVR_RETURN_IF_FALSE(rec);
1439 DVR_RETURN_IF_FALSE(status);
1440
1441 ctx = ctx_getRecord((unsigned long)rec);
1442 DVR_RETURN_IF_FALSE(ctx);
1443
Gong Kefdb31922022-06-17 17:11:16 +08001444 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001445
Wentao MA96f68962022-06-15 19:45:35 +08001446 DVR_WRAPPER_INFO("get record(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001447 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001448
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001449 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001450
Wentao MA96f68962022-06-15 19:45:35 +08001451 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001452 ctx->sn,
1453 s.state,
1454 s.info.time,
1455 s.info.size,
1456 s.info.pkts,
1457 error);
1458
1459 *status = s;
1460
Gong Kefdb31922022-06-17 17:11:16 +08001461 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001462
1463 return error;
1464}
1465
hualing chen4fe3bee2020-10-23 13:58:52 +08001466int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1467{
1468 DVR_WrapperCtx_t *ctx;
1469 int error;
1470
1471 DVR_RETURN_IF_FALSE(rec);
1472
1473 ctx = ctx_getRecord((unsigned long)rec);
1474 DVR_RETURN_IF_FALSE(ctx);
1475
Gong Kefdb31922022-06-17 17:11:16 +08001476 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001477 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001478 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001479 return error;
1480}
1481
hualing chen266b9502020-04-04 17:39:39 +08001482int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1483{
1484 DVR_WrapperCtx_t *ctx;
1485 int error;
1486
1487 DVR_RETURN_IF_FALSE(rec);
1488 DVR_RETURN_IF_FALSE(p_secure_buf);
1489
1490 ctx = ctx_getRecord((unsigned long)rec);
1491 DVR_RETURN_IF_FALSE(ctx);
1492
Gong Kefdb31922022-06-17 17:11:16 +08001493 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001494 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001495 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001496 return error;
1497}
1498
1499int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1500{
1501 DVR_WrapperCtx_t *ctx;
1502 int error;
1503
1504 DVR_RETURN_IF_FALSE(rec);
1505 DVR_RETURN_IF_FALSE(func);
1506
1507 ctx = ctx_getRecord((unsigned long)rec);
1508 DVR_RETURN_IF_FALSE(ctx);
1509
Gong Kefdb31922022-06-17 17:11:16 +08001510 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001511 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001512 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001513 return error;
1514}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001515
1516
1517int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1518{
1519 DVR_WrapperCtx_t *ctx;
1520 DVR_PlaybackOpenParams_t open_param;
1521 int error;
1522
1523 DVR_RETURN_IF_FALSE(playback);
1524 DVR_RETURN_IF_FALSE(params);
1525 DVR_RETURN_IF_FALSE(params->playback_handle);
1526
1527 /*get a free ctx*/
1528 ctx = ctx_getPlayback(0);
1529 DVR_RETURN_IF_FALSE(ctx);
1530
Gong Kefdb31922022-06-17 17:11:16 +08001531 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001532
Wentao MA96f68962022-06-15 19:45:35 +08001533 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 +08001534
1535 ctx_reset(ctx);
1536
1537 ctx->playback.param_open = *params;
1538 ctx->playback.event_fn = params->event_fn;
1539 ctx->playback.event_userdata = params->event_userdata;
1540 ctx->current_segment_id = 0;
1541 INIT_LIST_HEAD(&ctx->segments);
1542 ctx->sn = get_sn();
1543
1544 wrapper_requestThreadFor(ctx);
1545
hualing chen266b9502020-04-04 17:39:39 +08001546 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001547 open_param.dmx_dev_id = params->dmx_dev_id;
1548 open_param.block_size = params->block_size;
1549 open_param.is_timeshift = params->is_timeshift;
1550 //open_param.notification_size = 10*1024; //not supported
1551 open_param.event_fn = wrapper_playback_event_handler;
1552 open_param.event_userdata = (void*)ctx->sn;
1553 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001554 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001555 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001556 open_param.vendor = params->vendor;
1557
Yahui Han1fbf3292021-11-08 18:17:19 +08001558 if (params->keylen) {
1559 open_param.clearkey = params->clearkey;
1560 open_param.cleariv = params->cleariv;
1561 open_param.keylen = params->keylen;
1562 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001563
1564 error = dvr_playback_open(&ctx->playback.player, &open_param);
1565 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001566 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001567 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001568 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001569 wrapper_releaseThreadForType(ctx->type);
1570 return DVR_FAILURE;
1571 }
1572 if (params->is_timeshift)
1573 sn_timeshift_playback = ctx->sn;
1574
Wentao MA270dc0f2022-08-23 13:17:26 +08001575 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001576 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1577 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001578 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001579 }
Gong Kefdb31922022-06-17 17:11:16 +08001580 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001581
1582 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1583 return DVR_SUCCESS;
1584}
1585
1586int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1587{
1588 DVR_WrapperCtx_t *ctx;
1589 int error;
1590
1591 DVR_RETURN_IF_FALSE(playback);
1592
1593 ctx = ctx_getPlayback((unsigned long)playback);
1594 DVR_RETURN_IF_FALSE(ctx);
1595
Gong Kefdb31922022-06-17 17:11:16 +08001596 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001597 DVR_WRAPPER_INFO("close playback(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001598 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001599
1600 if (ctx->playback.param_open.is_timeshift)
1601 sn_timeshift_playback = 0;
1602
1603 /*try stop first*/
1604 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1605
1606 {
1607 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001608 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001609
Wentao MA270dc0f2022-08-23 13:17:26 +08001610 list_for_each_entry(p_seg, &ctx->segments, head) {
1611 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001612 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001613 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001614 }
1615 ctx_freeSegments(ctx);
1616 }
1617
1618 error = dvr_playback_close(ctx->playback.player);
1619
Wentao MA96f68962022-06-15 19:45:35 +08001620 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001621 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001622 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001623
1624 wrapper_releaseThreadForType(ctx->type);
1625
1626 return error;
1627}
1628
1629int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1630{
1631 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001632 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001633 uint64_t *p_segment_ids;
1634 uint32_t segment_nb;
1635 uint32_t i;
1636 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001637 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001638 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001639 DVR_Bool_t is_timeshift = DVR_FALSE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001640
1641 DVR_RETURN_IF_FALSE(playback);
1642 DVR_RETURN_IF_FALSE(p_pids);
1643
hualing chenc110f952021-01-18 11:25:37 +08001644 ctx_record = NULL;
1645
1646 /*lock the recorder to avoid changing the recording segments*/
1647 ctx_record = ctx_getRecord(sn_timeshift_record);
1648
1649 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001650 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001651 if (!ctx_valid(ctx_record)
1652 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001653 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001654 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001655 is_timeshift = DVR_FALSE;
1656 } else {
1657 is_timeshift = DVR_TRUE;
1658 }
1659 }
1660
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001661 ctx = ctx_getPlayback((unsigned long)playback);
1662 DVR_RETURN_IF_FALSE(ctx);
1663
Gong Kefdb31922022-06-17 17:11:16 +08001664 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001665
Wentao MA96f68962022-06-15 19:45:35 +08001666 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 +08001667 ctx->sn,
1668 ctx->playback.param_open.location,
1669 flags,
1670 p_pids->video.pid, p_pids->video.format,
1671 p_pids->audio.pid, p_pids->audio.format,
1672 p_pids->ad.pid, p_pids->ad.format,
1673 p_pids->subtitle.pid, p_pids->subtitle.format,
1674 p_pids->pcr.pid);
1675
Gong Kefdb31922022-06-17 17:11:16 +08001676 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001677
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001678 if (ctx->playback.param_open.is_timeshift) {
1679 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001680 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001681 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001682 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001683 return DVR_FAILURE;
1684 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001685 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001686 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001687 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001688 }
1689
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001690 /*obtain all segments in a list*/
1691 segment_nb = 0;
1692 p_segment_ids = NULL;
1693 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001694 if (!error) {
1695 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001696 struct list_head info_list; /**< segment list head*/
1697 INIT_LIST_HEAD(&info_list);
1698
Wentao MA96f68962022-06-15 19:45:35 +08001699 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001700 //we need free info list buf when we used end.
1701 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001702 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001703 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001704 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1705 ctx->playback.param_open.location, error);
hualing chenb9a02922021-12-14 11:29:47 +08001706 for (i = 0; i < segment_nb; i++) {
1707 DVR_RecordSegmentInfo_t seg_info;
1708 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001709
hualing chenb9a02922021-12-14 11:29:47 +08001710 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1711 if (error) {
1712 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001713 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001714 ctx->playback.param_open.location, p_segment_ids[i], error);
1715 break;
1716 }
1717 //add check if has audio or video pid. if not exist. not add segment to playback
1718 int ii = 0;
1719 int has_av = 0;
1720 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1721 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1722 if (type == DVR_STREAM_TYPE_VIDEO ||
1723 type == DVR_STREAM_TYPE_AUDIO ||
1724 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001725 DVR_WRAPPER_INFO("success to get seg av info \n");
1726 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 +08001727 DVR_STREAM_TYPE_VIDEO,
1728 DVR_STREAM_TYPE_AUDIO,
1729 DVR_STREAM_TYPE_AD);
1730 has_av = 1;
1731 //break;
1732 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001733 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 +08001734 DVR_STREAM_TYPE_VIDEO,
1735 DVR_STREAM_TYPE_AUDIO,
1736 DVR_STREAM_TYPE_AD);
1737 }
1738 }
1739 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001740 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001741 continue;
1742 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001743 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001744 }
1745 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1746 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1747 if (error)
1748 break;
1749 /*copy the 1st segment*/
1750 if (got_1st_seg == 0) {
1751 seg_info_1st = seg_info;
1752 got_1st_seg = 1;
1753 }
1754 }
1755 } else {
1756 for (i = 0; i < segment_nb; i++) {
1757 DVR_RecordSegmentInfo_t *seg_info;
1758 DVR_PlaybackSegmentFlag_t flags;
1759 int found = 0;
1760 list_for_each_entry(seg_info, &info_list, head)
1761 {
hualing chen8aed9582021-12-24 17:59:56 +08001762 if (seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001763 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001764 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001765 break;
1766 }
1767 }
1768 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001769 //last info is not found if when recording occured power off.
1770 if (p_segment_ids[i] == segment_nb - 1) {
1771 DVR_RecordSegmentInfo_t seg_info;
1772 DVR_PlaybackSegmentFlag_t flags;
1773
1774 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1775 if (error) {
1776 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001777 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001778 ctx->playback.param_open.location, p_segment_ids[i], error);
1779 break;
1780 }
1781 //
1782 //add check if has audio or video pid. if not exist. not add segment to playback
1783 int ii = 0;
1784 int has_av = 0;
1785 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1786 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1787 if (type == DVR_STREAM_TYPE_VIDEO ||
1788 type == DVR_STREAM_TYPE_AUDIO ||
1789 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001790 DVR_WRAPPER_INFO("success to get seg av info \n");
1791 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 +08001792 DVR_STREAM_TYPE_VIDEO,
1793 DVR_STREAM_TYPE_AUDIO,
1794 DVR_STREAM_TYPE_AD);
1795 has_av = 1;
1796 //break;
1797 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001798 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 +08001799 DVR_STREAM_TYPE_VIDEO,
1800 DVR_STREAM_TYPE_AUDIO,
1801 DVR_STREAM_TYPE_AD);
1802 }
1803 }
1804 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001805 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001806 continue;
1807 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001808 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001809 }
1810 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1811 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1812 if (error)
1813 break;
1814 }
hualing chenb9a02922021-12-14 11:29:47 +08001815 continue;
1816 }
1817
1818 //add check if has audio or video pid. if not exist. not add segment to playback
1819 int ii = 0;
1820 int has_av = 0;
1821 for (ii = 0; ii < seg_info->nb_pids; ii++) {
1822 int type = (seg_info->pids[ii].type >> 24) & 0x0f;
1823 if (type == DVR_STREAM_TYPE_VIDEO ||
1824 type == DVR_STREAM_TYPE_AUDIO ||
1825 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001826 DVR_WRAPPER_INFO("success to get seg av info \n");
1827 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 +08001828 DVR_STREAM_TYPE_VIDEO,
1829 DVR_STREAM_TYPE_AUDIO,
1830 DVR_STREAM_TYPE_AD);
1831 has_av = 1;
1832 //break;
1833 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001834 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 +08001835 DVR_STREAM_TYPE_VIDEO,
1836 DVR_STREAM_TYPE_AUDIO,
1837 DVR_STREAM_TYPE_AD);
1838 }
1839 }
1840 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001841 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001842 continue;
1843 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001844 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001845 }
1846 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1847 error = wrapper_addPlaybackSegment(ctx, seg_info, p_pids, flags);
1848 if (error)
1849 break;
1850
1851 /*copy the 1st segment*/
1852 if (got_1st_seg == 0) {
1853 seg_info_1st = *seg_info;
1854 got_1st_seg = 1;
1855 }
hualing chen92f3a142020-07-08 20:59:33 +08001856 }
hualing chenb9a02922021-12-14 11:29:47 +08001857 //free list
1858 DVR_RecordSegmentInfo_t *segment = NULL;
1859 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1860 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1861 {
1862 if (segment) {
1863 list_del(&segment->head);
1864 free(segment);
1865 }
1866 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001867 }
hualing chenb9a02922021-12-14 11:29:47 +08001868
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001869 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001870
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001871 /* return if no segment or fail to add */
1872 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001873
Wentao MA270dc0f2022-08-23 13:17:26 +08001874 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001875 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1876 ctx->playback.obsolete = ctx_record->record.obsolete;
1877 }
1878
Wentao MA96f68962022-06-15 19:45:35 +08001879 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001880
1881 ctx->playback.reach_end = DVR_FALSE;
1882 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1883 ctx->playback.speed = 0.0f;
1884 else
1885 ctx->playback.speed = 100.0f;
1886
1887 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001888 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001889 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001890 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001891 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001892 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001893 error = dvr_playback_start(ctx->playback.player, flags);
1894 } else {
1895 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1896 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001897 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001898 ctx->sn, seg_info_1st.id, error);
1899 }
Wentao MA96f68962022-06-15 19:45:35 +08001900 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001901 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001902 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 +08001903 }
1904 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001905
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001906 if (ctx->playback.param_open.is_timeshift) {
1907 /*unlock the recorder locked above*/
1908 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001909 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001910 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001911 ctx->sn, ctx_record->sn);
1912 }
1913 }
Gong Kefdb31922022-06-17 17:11:16 +08001914 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001915
1916 return error;
1917}
hualing chen002e5b92022-02-23 17:51:21 +08001918//stop record and playback
1919int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1920{
1921 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1922 int error;
Wentao MA96f68962022-06-15 19:45:35 +08001923 DVR_WRAPPER_INFO("stop timeshift record\n");
hualing chen002e5b92022-02-23 17:51:21 +08001924
1925 //stop timeshift record
1926 ctx_record = ctx_getRecord(sn_timeshift_record);
1927 error = dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
1928
Wentao MA96f68962022-06-15 19:45:35 +08001929 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08001930 //stop play
1931 error = dvr_wrapper_stop_playback(playback);
1932 //del timeshift file
1933 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001934 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08001935 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
1936 }
1937 return error;
1938}
1939//start record and start playback
1940int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1941{
1942 DVR_WrapperCtx_t *ctx;
1943 DVR_RecordStartParams_t *start_param;
1944 int error;
1945
1946 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
1947 DVR_RETURN_IF_FALSE(ctx);
1948
Gong Kefdb31922022-06-17 17:11:16 +08001949 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001950 DVR_WRAPPER_INFO("restart record(sn:%ld, location:%s)...\n", ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08001951 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08001952
hualing chen451c8f72022-03-09 13:05:52 +08001953 {
1954 //clear old record status
1955 // struct {
1956 // DVR_WrapperRecordOpenParams_t param_open;
1957 // DVR_RecordStartParams_t param_start;
1958 // DVR_RecordStartParams_t param_update;
1959 // DVR_RecordHandle_t recorder;
1960 // DVR_RecordEventFunction_t event_fn;
1961 // void *event_userdata;
1962
1963 // /*total status = seg_status + status + obsolete*/
1964 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
1965 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
1966 // uint64_t next_segment_id;
1967
1968 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
1969 // } record;
1970 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
1971 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
1972 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
1973 }
1974
hualing chen002e5b92022-02-23 17:51:21 +08001975 start_param = &ctx->record.param_start;
1976
1977 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1978 {
1979 DVR_RecordSegmentInfo_t new_seg_info =
1980 { .id = start_param->segment.segment_id, };
1981 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08001982 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 +08001983 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
1984 DVR_RecordStartParams_t *update_param;
1985 update_param = &ctx->record.param_update;
1986 memcpy(update_param, start_param, sizeof(*update_param));
1987 int i = 0;
1988 for (i = 0; i < update_param->segment.nb_pids; i++)
1989 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08001990 }
1991
Wentao MA96f68962022-06-15 19:45:35 +08001992 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08001993
Gong Kefdb31922022-06-17 17:11:16 +08001994 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08001995
1996 //start play
Wentao MA96f68962022-06-15 19:45:35 +08001997 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08001998 //clear play statue
1999 ctx = ctx_getPlayback((unsigned long)playback);
2000 if (ctx) {
2001 //clear old playback status
2002 // struct {
2003 // DVR_WrapperPlaybackOpenParams_t param_open;
2004 // DVR_PlaybackHandle_t player;
2005 // DVR_PlaybackEventFunction_t event_fn;
2006 // void *event_userdata;
2007
2008 // /*total status = seg_status + status*/
2009 // DVR_PlaybackStatus_t seg_status;
2010 // DVR_WrapperPlaybackStatus_t status;
2011 // DVR_PlaybackPids_t pids_req;
2012 // DVR_PlaybackEvent_t last_event;
2013 // float speed;
2014 // DVR_Bool_t reach_end;
2015
2016 // DVR_WrapperInfo_t obsolete;
2017 // DVR_Bool_t tf_full;
2018 // } playback;
2019 ctx->playback.tf_full == DVR_FALSE;
2020 ctx->playback.reach_end == DVR_FALSE;
2021 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2022 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2023 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2024 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2025 }
hualing chen002e5b92022-02-23 17:51:21 +08002026 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2027 return error;
2028}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002029
2030int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2031{
2032 DVR_WrapperCtx_t *ctx;
2033 int error;
2034
2035 DVR_RETURN_IF_FALSE(playback);
2036
2037 ctx = ctx_getPlayback((unsigned long)playback);
2038 DVR_RETURN_IF_FALSE(ctx);
2039
Gong Kefdb31922022-06-17 17:11:16 +08002040 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002041 DVR_WRAPPER_INFO("stop playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002042 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002043
2044 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2045
2046 {
2047 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002048 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002049
Wentao MA270dc0f2022-08-23 13:17:26 +08002050 list_for_each_entry(p_seg, &ctx->segments, head) {
2051 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002052 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002053 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002054 }
2055 ctx_freeSegments(ctx);
2056 }
2057
Wentao MA96f68962022-06-15 19:45:35 +08002058 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002059 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002060
2061 return error;
2062}
2063
2064int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2065{
2066 DVR_WrapperCtx_t *ctx;
2067 int error;
2068
2069 DVR_RETURN_IF_FALSE(playback);
2070
2071 ctx = ctx_getPlayback((unsigned long)playback);
2072 DVR_RETURN_IF_FALSE(ctx);
2073
Gong Kefdb31922022-06-17 17:11:16 +08002074 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002075 DVR_WRAPPER_INFO("pause playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002076 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002077 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002078 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002079 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002080
2081 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2082
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002083 ctx->playback.speed = 0.0f;
2084
Wentao MA96f68962022-06-15 19:45:35 +08002085 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002086 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002087
2088 return error;
2089}
2090
2091int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2092{
2093 DVR_WrapperCtx_t *ctx;
2094 int error;
2095
2096 DVR_RETURN_IF_FALSE(playback);
2097
2098 ctx = ctx_getPlayback((unsigned long)playback);
2099 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002100 //if set limit.we need check if seek to valid data when resume
2101 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2102 if (dvr_playback_check_limit(ctx->playback.player)) {
2103 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2104 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002105 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002106 ctx->sn, time_offset, expired);
2107 time_offset = expired;
2108 dvr_wrapper_seek_playback(playback, time_offset);
2109 }
2110 }
Gong Kefdb31922022-06-17 17:11:16 +08002111 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002112 DVR_WRAPPER_INFO("resume playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002113 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002114
2115 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002116 ctx->playback.speed = 100.0f;
2117
Wentao MA96f68962022-06-15 19:45:35 +08002118 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002119 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002120
2121 return error;
2122}
2123
2124int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2125{
2126 DVR_WrapperCtx_t *ctx;
2127 int error;
2128 DVR_PlaybackSpeed_t dvr_speed = {
2129 .speed = { speed },
2130 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2131 };
2132
2133 DVR_RETURN_IF_FALSE(playback);
2134
2135 ctx = ctx_getPlayback((unsigned long)playback);
2136 DVR_RETURN_IF_FALSE(ctx);
2137
Gong Kefdb31922022-06-17 17:11:16 +08002138 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002139 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 +08002140 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002141
2142 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2143
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002144 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2145 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2146 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002147 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002148 error = dvr_playback_resume(ctx->playback.player);
2149 } else if (ctx->playback.speed == 0.0f
2150 && speed != 0.0f
2151 && speed != 100.0f) {
2152 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002153 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002154 error = dvr_playback_resume(ctx->playback.player);
2155 }
2156
2157 ctx->playback.speed = speed;
2158
Wentao MA96f68962022-06-15 19:45:35 +08002159 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002160 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002161 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002162
2163 return error;
2164}
2165
hualing chen03fd4942021-07-15 15:56:41 +08002166int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2167{
2168 DVR_WrapperCtx_t *ctx;
2169 int error;
2170
2171 DVR_RETURN_IF_FALSE(playback);
2172
2173 ctx = ctx_getPlayback((unsigned long)playback);
2174 DVR_RETURN_IF_FALSE(ctx);
2175
Gong Kefdb31922022-06-17 17:11:16 +08002176 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002177
Wentao MAe8ba5172022-08-09 11:18:17 +08002178 DVR_WRAPPER_INFO("set_limit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002179 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002180
2181 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002182 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002183
Gong Kefdb31922022-06-17 17:11:16 +08002184 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002185
2186 return error;
2187}
2188
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002189int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2190{
2191 DVR_WrapperCtx_t *ctx;
2192 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002193 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002194 uint64_t segment_id;
2195 uint32_t off;
2196 uint64_t last_segment_id;
2197 uint32_t pre_off;
2198
2199 DVR_RETURN_IF_FALSE(playback);
2200
2201 ctx = ctx_getPlayback((unsigned long)playback);
2202 DVR_RETURN_IF_FALSE(ctx);
2203
Gong Kefdb31922022-06-17 17:11:16 +08002204 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002205
Wentao MA96f68962022-06-15 19:45:35 +08002206 DVR_WRAPPER_INFO("seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002207 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002208
2209 off = 0;
2210 segment_id = 0;
2211 pre_off = 0;
2212 last_segment_id = 0;
2213
hualing chen03fd4942021-07-15 15:56:41 +08002214 //if set limit info we need check ts data is
2215 //expired when seek
2216 if (dvr_playback_check_limit(ctx->playback.player)) {
2217 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2218 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002219 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002220 ctx->sn, time_offset, expired);
2221 time_offset = expired;
2222 }
2223 }
2224
Wentao MA270dc0f2022-08-23 13:17:26 +08002225 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2226 segment_id = p_seg->seg_info.id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002227
Wentao MA270dc0f2022-08-23 13:17:26 +08002228 if ((ctx->playback.obsolete.time + pre_off + p_seg->seg_info.duration) > time_offset)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002229 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002230
Wentao MA270dc0f2022-08-23 13:17:26 +08002231 last_segment_id = p_seg->seg_info.id;
2232 pre_off += p_seg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002233 }
2234
2235 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002236 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2237 off = time_offset;
2238 } else if (ctx->playback.obsolete.time >= time_offset) {
2239 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002240 } else {
hualing chenda76fc52020-05-28 14:56:42 +08002241 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002242 }
2243
Wentao MA96f68962022-06-15 19:45:35 +08002244 DVR_WRAPPER_INFO("seek playback(sn:%ld) (seg:%lld, off:%d)\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002245 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002246 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
Wentao MA96f68962022-06-15 19:45:35 +08002247 DVR_WRAPPER_INFO("playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002248
Gong Kefdb31922022-06-17 17:11:16 +08002249 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002250
2251 return error;
2252}
2253
2254int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2255{
2256 DVR_WrapperCtx_t *ctx;
2257 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002258 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002259
2260 DVR_RETURN_IF_FALSE(playback);
2261 DVR_RETURN_IF_FALSE(p_pids);
2262
2263 ctx = ctx_getPlayback((unsigned long)playback);
2264 DVR_RETURN_IF_FALSE(ctx);
2265
Gong Kefdb31922022-06-17 17:11:16 +08002266 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002267
Wentao MA96f68962022-06-15 19:45:35 +08002268 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002269 ctx->sn,
2270 p_pids->video.pid, p_pids->video.format,
2271 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002272 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002273
2274 ctx->playback.pids_req = *p_pids;
2275
2276 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +08002277 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002278 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002279 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2280 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2281 /*check update for pids*/
2282 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2283 p_seg->playback_info.pids = *p_pids;
2284 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002285 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002286 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002287 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002288 /*do not break, let list updated*/
2289 }
2290 }
2291 }
2292 /*break;*/
2293 }
2294 }
2295
Wentao MA96f68962022-06-15 19:45:35 +08002296 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002297 ctx->sn,
2298 p_pids->video.pid, p_pids->video.format,
2299 p_pids->audio.pid, p_pids->audio.format,
2300 error);
2301
Gong Kefdb31922022-06-17 17:11:16 +08002302 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002303
2304 return error;
2305}
2306
hualing chena5f03222021-12-02 11:22:35 +08002307int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2308{
2309 DVR_WrapperCtx_t *ctx;
2310 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002311 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002312
2313 DVR_RETURN_IF_FALSE(playback);
2314 DVR_RETURN_IF_FALSE(p_pids);
2315
2316 ctx = ctx_getPlayback((unsigned long)playback);
2317 DVR_RETURN_IF_FALSE(ctx);
2318
Gong Kefdb31922022-06-17 17:11:16 +08002319 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002320
Wentao MA96f68962022-06-15 19:45:35 +08002321 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
hualing chena5f03222021-12-02 11:22:35 +08002322 ctx->sn,
2323 p_pids->video.pid, p_pids->video.format,
2324 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002325 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002326
2327 ctx->playback.pids_req = *p_pids;
2328
2329 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +08002330 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002331 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002332 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2333 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2334 /*check update for pids*/
2335 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2336 p_seg->playback_info.pids = *p_pids;
2337 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002338 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002339 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002340 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002341 /*do not break, let list updated*/
2342 }
2343 }
2344 }
2345 /*break;*/
2346 }
2347 }
2348
Wentao MA96f68962022-06-15 19:45:35 +08002349 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002350 ctx->sn,
2351 p_pids->video.pid, p_pids->video.format,
2352 p_pids->audio.pid, p_pids->audio.format,
2353 error);
2354
Gong Kefdb31922022-06-17 17:11:16 +08002355 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002356
2357 return error;
2358}
2359
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002360int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2361{
2362 DVR_WrapperCtx_t *ctx;
2363 DVR_WrapperPlaybackStatus_t s;
2364 DVR_PlaybackStatus_t play_status;
2365 int error;
2366
2367 DVR_RETURN_IF_FALSE(playback);
2368 DVR_RETURN_IF_FALSE(status);
2369
2370 ctx = ctx_getPlayback((unsigned long)playback);
2371 DVR_RETURN_IF_FALSE(ctx);
2372
Gong Kefdb31922022-06-17 17:11:16 +08002373 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002374
Wentao MA96f68962022-06-15 19:45:35 +08002375 DVR_WRAPPER_INFO("get playback(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002376 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002377
2378 error = dvr_playback_get_status(ctx->playback.player, &play_status);
Wentao MA96f68962022-06-15 19:45:35 +08002379 DVR_WRAPPER_INFO("playback(sn:%ld) get status (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002380
2381 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002382 error = process_generatePlaybackStatus(ctx, &s);
2383
hualing chenb5cd42e2020-04-15 17:03:34 +08002384 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2385 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002386 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002387 s.info_cur.time = s.info_full.time;
2388 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002389 DVR_WRAPPER_INFO("playback(sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002390 ctx->sn,
2391 s.state,
2392 s.info_cur.time,
2393 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002394 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002395 error);
2396
2397 *status = s;
2398
Gong Kefdb31922022-06-17 17:11:16 +08002399 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002400
2401 return error;
2402}
2403
hualing chen266b9502020-04-04 17:39:39 +08002404int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2405{
2406 DVR_WrapperCtx_t *ctx;
2407 int error;
2408
2409 DVR_RETURN_IF_FALSE(playback);
2410 DVR_RETURN_IF_FALSE(p_secure_buf);
2411
2412 ctx = ctx_getPlayback((unsigned long)playback);
2413 DVR_RETURN_IF_FALSE(ctx);
2414
Gong Kefdb31922022-06-17 17:11:16 +08002415 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002416 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002417 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002418 return error;
2419}
2420
2421int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2422{
2423 DVR_WrapperCtx_t *ctx;
2424 int error;
2425
2426 DVR_RETURN_IF_FALSE(playback);
2427 DVR_RETURN_IF_FALSE(func);
2428
2429 ctx = ctx_getPlayback((unsigned long)playback);
2430 DVR_RETURN_IF_FALSE(ctx);
2431
Gong Kefdb31922022-06-17 17:11:16 +08002432 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002433 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002434 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002435 return error;
2436}
2437
Zhiqiang Han620b9252021-11-09 14:23:20 +08002438int dvr_wrapper_segment_del_by_location (const char *location)
2439{
2440 char fpath[DVR_MAX_LOCATION_SIZE];
2441
2442 DVR_RETURN_IF_FALSE(location);
2443
2444 /*del the stats file*/
2445 sprintf(fpath, "%s.stats", location);
2446 unlink(fpath);
2447
2448 return dvr_segment_del_by_location(location);
2449}
2450
2451int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2452{
2453 FILE *fp;
2454 char fpath[DVR_MAX_LOCATION_SIZE];
2455
2456 DVR_RETURN_IF_FALSE(location);
2457 DVR_RETURN_IF_FALSE(p_info);
2458
2459 if (p_info)
2460 memset(p_info, 0, sizeof(p_info[0]));
2461
2462 memset(fpath, 0, sizeof(fpath));
2463 sprintf(fpath, "%s.stats", location);
2464
2465 /*stats file exists*/
2466 if ((fp = fopen(fpath, "r"))) {
2467 char buf[256];
2468
2469 if (fgets(buf, sizeof(buf), fp) != NULL
2470 && (sscanf(buf, ":%llu:%lu:%u",
2471 &p_info->size,
2472 &p_info->time,
2473 &p_info->pkts) == 3)) {
2474 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002475 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 +08002476 return DVR_SUCCESS;
2477 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002478 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002479 }
2480
2481 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002482 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002483
2484 int error;
2485 uint32_t n_ids;
2486 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002487
hualing chen8aed9582021-12-24 17:59:56 +08002488 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002489
Zhiqiang Han620b9252021-11-09 14:23:20 +08002490 if (!error) {
2491 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002492 struct list_head info_list; /**< segment list head*/
2493 INIT_LIST_HEAD(&info_list);
2494
2495 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002496 error = dvr_segment_get_allInfo(location, &info_list);
2497 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08002498 DVR_RecordSegmentInfo_t info;
2499
2500 memset(&info, 0, sizeof(info));
2501 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002502 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002503 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002504
2505 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002506 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002507 if (!error) {
2508 p_info->size += info.size;
2509 p_info->time += info.duration;
2510 p_info->pkts += info.nb_packets;
2511 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002512 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002513 break;
2514 }
2515 }
2516 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002517 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002518 for (i = 0; i < n_ids; i++) {
2519
2520 DVR_RecordSegmentInfo_t *seg_info;
2521 DVR_PlaybackSegmentFlag_t flags;
2522 int found = 0;
2523 list_for_each_entry(seg_info, &info_list, head)
2524 {
hualing chen8aed9582021-12-24 17:59:56 +08002525 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002526 found = 1;
2527 break;
2528 }
2529 }
2530 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002531 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 +08002532 if (p_ids[i] == n_ids - 1) {
2533 DVR_RecordSegmentInfo_t info;
Wentao MA96f68962022-06-15 19:45:35 +08002534 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 +08002535 error = dvr_segment_get_info(location, p_ids[i], &info);
2536 if (!error) {
2537 p_info->size += info.size;
2538 p_info->time += info.duration;
2539 p_info->pkts += info.nb_packets;
2540 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002541 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002542 break;
2543 }
2544 }
hualing chenb9a02922021-12-14 11:29:47 +08002545 continue;
2546 }
2547
2548 if (!error) {
2549 p_info->size += seg_info->size;
2550 p_info->time += seg_info->duration;
2551 p_info->pkts += seg_info->nb_packets;
2552 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002553 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002554 break;
2555 }
2556 }
2557 //free list
2558 DVR_RecordSegmentInfo_t *segment = NULL;
2559 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2560 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2561 {
2562 if (segment) {
2563 list_del(&segment->head);
2564 free(segment);
2565 }
2566 }
2567 }
2568 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002569 } else {
2570 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002571 }
Wentao MA96f68962022-06-15 19:45:35 +08002572 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 +08002573
2574 return (error)? DVR_FAILURE : DVR_SUCCESS;
2575}
2576
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002577static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2578{
2579 DVR_WrapperEventCtx_t evt;
2580
2581 DVR_RETURN_IF_FALSE(userdata);
2582
2583 evt.sn = (unsigned long)userdata;
2584 evt.type = W_REC;
2585 evt.record.event = event;
2586 evt.record.status = *(DVR_RecordStatus_t *)params;
Wentao MA96f68962022-06-15 19:45:35 +08002587 DVR_WRAPPER_INFO("evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002588 return ctx_addRecordEvent(&evt);
2589}
2590
2591static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2592{
2593 DVR_WrapperEventCtx_t evt;
2594
2595 DVR_RETURN_IF_FALSE(userdata);
2596
2597 evt.sn = (unsigned long)userdata;
2598 evt.type = W_PLAYBACK;
2599 evt.playback.event = event;
2600 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA9a164002022-08-29 11:20:24 +08002601 DVR_WRAPPER_INFO("evt[sn:%ld, playback, evt:0x%x]\n", evt.sn, evt.playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002602 return ctx_addPlaybackEvent(&evt);
2603}
2604
2605static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2606{
Wentao MA270dc0f2022-08-23 13:17:26 +08002607 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 +08002608 ctx->sn,
2609 evt,
2610 status->info.time,
2611 status->info.size,
2612 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002613 status->info_obsolete.time,
2614 status->info_obsolete.size,
2615 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002616
2617 if (ctx->record.event_fn)
2618 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2619 return 0;
2620}
2621
Zhiqiang Han620b9252021-11-09 14:23:20 +08002622static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2623{
2624 FILE *fp;
2625 char fpath[DVR_MAX_LOCATION_SIZE];
2626
2627 DVR_RETURN_IF_FALSE(location);
2628 DVR_RETURN_IF_FALSE(p_status);
2629
2630 sprintf(fpath, "%s.stats", location);
2631
2632 /*stats file*/
2633 if ((fp = fopen(fpath, "w"))) {
2634 char buf[256];
2635 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2636 p_status->info.size - p_status->info_obsolete.size,
2637 p_status->info.time - p_status->info_obsolete.time,
2638 p_status->info.pkts - p_status->info_obsolete.pkts);
2639 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002640 fflush(fp);
2641 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002642 fclose(fp);
2643 return DVR_SUCCESS;
2644 }
2645
2646 return DVR_FAILURE;
2647}
2648
2649
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002650static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2651{
2652 DVR_RecordStartParams_t param;
2653 DVR_RecordSegmentInfo_t seg_info;
2654 int i;
2655 int error;
2656
2657 memcpy(&param, &ctx->record.param_update, sizeof(param));
2658 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2659 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2660 for (i = 0; i < param.segment.nb_pids; i++) {
2661 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2662 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2663 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2664 ctx->record.param_update.segment.nb_pids++;
2665 }
2666 }
2667 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2668 {
2669 DVR_RecordSegmentInfo_t new_seg_info =
2670 { .id = ctx->record.param_update.segment.segment_id, };
2671 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2672 wrapper_addRecordSegment(ctx, &new_seg_info);
2673 }
2674
Wentao MA96f68962022-06-15 19:45:35 +08002675 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002676 return error;
2677}
2678
Wentao MA270dc0f2022-08-23 13:17:26 +08002679static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002680{
Wentao MA270dc0f2022-08-23 13:17:26 +08002681 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002682}
2683
2684/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002685static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002686{
2687 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002688 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002689
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002690 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002691 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2692
2693 ctx->record.status.state = ctx->record.seg_status.state;
2694 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2695 memcpy(ctx->record.status.pids.pids,
2696 ctx->record.seg_status.info.pids,
2697 sizeof(ctx->record.status.pids.pids));
2698 ctx->current_segment_id = ctx->record.seg_status.info.id;
2699
Wentao MA270dc0f2022-08-23 13:17:26 +08002700 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2701 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2702 ctx->record.status.info.time += p_seg->info.duration;
2703 ctx->record.status.info.size += p_seg->info.size;
2704 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002705 }
2706 }
2707
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002708 ctx->record.status.info_obsolete = ctx->record.obsolete;
2709
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002710 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002711
2712 if (status) {
2713 *status = ctx->record.status;
2714 status->info.time += ctx->record.seg_status.info.duration;
2715 status->info.size += ctx->record.seg_status.info.size;
2716 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2717 }
2718
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002719 return DVR_SUCCESS;
2720}
2721
2722
2723static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2724{
2725 DVR_WrapperRecordStatus_t status;
2726
2727 memset(&status, 0, sizeof(status));
2728
Wentao MA96f68962022-06-15 19:45:35 +08002729 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002730 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002731 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002732 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002733 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002734 return 0;
2735 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002736 switch (evt->record.event)
2737 {
2738 case DVR_RECORD_EVENT_STATUS:
2739 {
2740 switch (evt->record.status.state)
2741 {
2742 case DVR_RECORD_STATE_OPENED:
2743 case DVR_RECORD_STATE_CLOSED:
2744 {
2745 ctx->record.seg_status = evt->record.status;
2746
2747 status.state = evt->record.status.state;
2748 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002749 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002750 } break;
2751 case DVR_RECORD_STATE_STARTED:
2752 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002753 ctx->record.seg_status = evt->record.status;
2754
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002755 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002756 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002757 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002758
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002759 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002760 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002761 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002762 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 +08002763 ctx->sn,
2764 evt->record.status.info.size,
2765 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002766 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2767 /*should notify the recording's stop*/
2768 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002769 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002770 ctx->sn, error);
2771 status.state = DVR_RECORD_STATE_CLOSED;
2772 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002773 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002774 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002775 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002776
2777 if (ctx->record.param_open.is_timeshift
2778 && ctx->record.param_open.max_time
2779 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002780 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002781
2782 /*as the player do not support null playlist,
2783 there must be one segment existed at any time,
2784 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002785 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2786 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002787 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002788 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 +08002789 status.info.size,
2790 ctx->record.param_open.max_time,
2791 ctx->record.param_open.segment_size);
2792 } else {
2793 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002794 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002795
2796 process_generateRecordStatus(ctx, &status);
2797 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002798 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002799 }
2800 }
2801
Wentao MAf4072032022-06-30 13:50:45 +08002802 const loff_t actual_size = status.info.size;
2803 const loff_t max_size = ctx->record.param_open.max_size;
2804 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002805
Wentao MAf4072032022-06-30 13:50:45 +08002806 if (ctx->record.param_open.is_timeshift && max_size) {
2807 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002808 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002809 /*as the player do not support null playlist,
2810 there must be one segment existed at any time,
2811 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002812 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2813 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002814 /*only one segment, waiting for more*/
2815 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2816 actual_size, segment_size);
2817 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002818 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002819
2820 process_generateRecordStatus(ctx, &status);
2821 process_notifyRecord(ctx, evt->record.event, &status);
2822 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2823 }
2824 if (actual_size >= max_size + segment_size/2) {
2825 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2826 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002827 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002828 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002829 }
2830 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002831 } break;
2832 case DVR_RECORD_STATE_STOPPED:
2833 {
2834 ctx->record.seg_status = evt->record.status;
2835
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002836 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002837 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002838 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002839 } break;
2840 default:
2841 break;
2842 }
2843 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002844 case DVR_RECORD_EVENT_WRITE_ERROR: {
2845 ctx->record.seg_status = evt->record.status;
2846 status.state = evt->record.status.state;
2847 process_notifyRecord(ctx, evt->record.event, &status);
2848 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002849 default:
2850 break;
2851 }
2852 return DVR_SUCCESS;
2853}
2854
2855static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2856{
Wentao MA270dc0f2022-08-23 13:17:26 +08002857 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 +08002858 ctx->sn,
2859 evt,
2860 status->state,
2861 status->info_cur.time,
2862 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002863 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002864
2865 if (ctx->playback.event_fn)
2866 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
2867 return 0;
2868}
2869
2870/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002871static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002872{
2873 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002874 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002875
2876 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2877 ctx->playback.status.pids = ctx->playback.pids_req;
2878
2879 ctx->playback.status.state = ctx->playback.seg_status.state;
2880 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2881 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2882 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2883
Wentao MA270dc0f2022-08-23 13:17:26 +08002884 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2885 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
2886 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 +08002887 break;
hualing chen451c8f72022-03-09 13:05:52 +08002888 }
2889
Wentao MA270dc0f2022-08-23 13:17:26 +08002890 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
2891 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
2892 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002893 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002894 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2895 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
2896 ctx->playback.status.info_full.size += p_seg->seg_info.size;
2897 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
2898 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 +08002899 }
2900
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002901 if (status) {
2902 *status = ctx->playback.status;
2903 /*deal with current, lack size and pkts with the current*/
2904 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08002905 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08002906 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08002907
Wentao MA270dc0f2022-08-23 13:17:26 +08002908 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2909 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08002910 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
2911 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08002912 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 +08002913 }
2914 else
2915 {
Wentao MA270dc0f2022-08-23 13:17:26 +08002916 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 +08002917 status->info_obsolete.time = ctx->playback.obsolete.time;
2918 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
2919 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002920 }
2921
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002922 return DVR_SUCCESS;
2923}
2924
2925static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2926{
Wentao MA96f68962022-06-15 19:45:35 +08002927 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002928 evt->sn, evt->playback.event,
2929 evt->playback.status.play_status.state,
2930 evt->playback.status.play_status.segment_id,
2931 evt->playback.status.play_status.time_cur,
2932 evt->playback.status.play_status.time_end);
2933
2934 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002935 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2936 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2937 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2938 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002939 ctx->playback.last_event = evt->playback.event;
2940
2941 switch (evt->playback.event)
2942 {
2943 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2944 case DVR_PLAYBACK_EVENT_REACHED_END:
2945 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2946 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002947 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002948 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002949 case DVR_PLAYBACK_EVENT_NODATA:
2950 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002951 {
2952 DVR_WrapperPlaybackStatus_t status;
2953
2954 /*copy status of segment*/
2955 ctx->playback.seg_status = evt->playback.status.play_status;
2956
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002957 /*generate status of the whole playback*/
2958 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002959
2960 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08002961 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08002962 if (ctx->playback.param_open.is_timeshift
2963 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002964 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08002965 }
2966 /*trust the low level, make NO check.
2967 As this evt is changed to only once due to some operations(paused) in low level.
2968 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002969 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08002970 }
2971 */
2972 else {
2973 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002974 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002975 }
hualing chenf291cf32020-06-18 10:50:30 +08002976 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002977 process_notifyPlayback(ctx, evt->playback.event, &status);
2978 }
2979 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002980 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2981 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2982 case DVR_PLAYBACK_EVENT_NO_KEY:
2983 {
Wentao MA96f68962022-06-15 19:45:35 +08002984 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002985 } break;
2986 default:
2987 {
Wentao MA96f68962022-06-15 19:45:35 +08002988 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002989 } break;
2990 }
2991 return 0;
2992}
2993
2994static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2995{
2996 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2997}
2998
Wentao MA96f68962022-06-15 19:45:35 +08002999int dvr_wrapper_set_log_level (int level)
3000{
3001 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3002 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3003 return DVR_FAILURE;
3004 }
3005 g_dvr_log_level = level;
3006 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3007 return DVR_SUCCESS;
3008}
3009
Wentao MA5629ad82022-08-24 10:03:02 +08003010int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3011{
3012 DVR_WrapperCtx_t *ctx;
3013 int error;
3014
3015 DVR_RETURN_IF_FALSE(playback==NULL);
3016
3017 ctx = ctx_getPlayback((unsigned long)playback);
3018 DVR_RETURN_IF_FALSE(ctx);
3019
3020 wrapper_mutex_lock(&ctx->wrapper_lock);
3021
3022 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3023
3024 DVR_WRAPPER_INFO("set ac4 preselection id: %d", presel_id);
3025 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3026
3027 wrapper_mutex_unlock(&ctx->wrapper_lock);
3028
3029 return error;
3030}
3031