blob: af45b8b3be114dfa0f42fe74f273b537ad2b52b8 [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"
wentao.ma9009aaa2022-10-25 14:26:33 +080016#include "dvr_utils.h"
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080017
18#include "AmTsPlayer.h"
19
20#include "list.h"
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080021
22#include "dvr_wrapper.h"
23
Wentao MA96f68962022-06-15 19:45:35 +080024#define WRAPPER_LOG_TAG "libdvr-wrapper"
25#define DVR_WRAPPER_DEBUG(...) DVR_LOG_PRINT(LOG_LV_DEBUG, WRAPPER_LOG_TAG, __VA_ARGS__)
26#define DVR_WRAPPER_INFO(...) DVR_LOG_PRINT(LOG_LV_INFO, WRAPPER_LOG_TAG, __VA_ARGS__)
27#define DVR_WRAPPER_WARN(...) DVR_LOG_PRINT(LOG_LV_WARN, WRAPPER_LOG_TAG, __VA_ARGS__)
28#define DVR_WRAPPER_ERROR(...) DVR_LOG_PRINT(LOG_LV_ERROR, WRAPPER_LOG_TAG, __VA_ARGS__)
29#define DVR_WRAPPER_FATAL(...) DVR_LOG_PRINT(LOG_LV_FATAL, WRAPPER_LOG_TAG, __VA_ARGS__)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080030
31/*duration of data to resume if paused with EVT_REACHED_END in timeshifting*/
hualing chen2932d372020-04-29 13:44:00 +080032#define TIMESHIFT_DATA_DURATION_TO_RESUME (600)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080033/*a tolerant gap*/
34#define DVR_PLAYBACK_END_GAP (1000)
35
Wentao MA96f68962022-06-15 19:45:35 +080036int g_dvr_log_level = LOG_LV_DEFAULT;
37
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080038enum {
39 W_REC = 1,
40 W_PLAYBACK = 2,
41};
42
43enum {
44 U_PIDS = 0x01,
45 U_STAT = 0x02,
46 U_ALL = U_PIDS | U_STAT,
47};
48
49typedef struct {
Gong Kefdb31922022-06-17 17:11:16 +080050 pthread_mutex_t lock;
51 pthread_cond_t cond;
52 int inited;
53 int locked;
54} DVR_WrapperMutex_t;
55
56static void
57wrapper_mutex_init (DVR_WrapperMutex_t *lock)
58{
59 pthread_condattr_t cattr;
60
Zhiqiang Han2259da32022-07-07 15:52:58 +080061 if (lock->inited)
Gong Kefdb31922022-06-17 17:11:16 +080062 return;
63
64 pthread_mutex_init(&lock->lock, NULL);
65
66 pthread_condattr_init(&cattr);
67 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
68 pthread_cond_init(&lock->cond, &cattr);
69 pthread_condattr_destroy(&cattr);
70
wentao.maa22bc852022-10-13 12:18:06 +080071 // It is not necessary to protect code block below with
72 // DVR_WrapperMutex_t.lock, so the following annotation
wentao.mafd5283f2022-10-14 09:51:13 +080073 // is given to suppress related Coverity complaint.
wentao.maa22bc852022-10-13 12:18:06 +080074 // coverity[missing_lock]
Gong Kefdb31922022-06-17 17:11:16 +080075 lock->locked = 0;
76 lock->inited = 1;
77}
78
79static int
80wrapper_mutex_lock (DVR_WrapperMutex_t *lock)
81{
82 pthread_mutex_lock(&lock->lock);
wentao.maa22bc852022-10-13 12:18:06 +080083 // This couldn't be a infinite loop as Coverity reported.
84 // Loop can finish when another thread calls wrapper_mutex_unlock.
85 // coverity[loop_condition]
Gong Kefdb31922022-06-17 17:11:16 +080086 while (lock->locked) {
87 pthread_cond_wait(&lock->cond, &lock->lock);
88 }
89 lock->locked = 1;
90 pthread_mutex_unlock(&lock->lock);
91
92 return 0;
93}
94
95static int
96wrapper_mutex_unlock (DVR_WrapperMutex_t *lock)
97{
98 pthread_mutex_lock(&lock->lock);
99 lock->locked = 0;
100 pthread_mutex_unlock(&lock->lock);
101 pthread_cond_signal(&lock->cond);
102
103 return 0;
104}
105
106static int
107wrapper_mutex_timedlock (DVR_WrapperMutex_t *lock, struct timespec *tv)
108{
109 int r = 0;
110
111 pthread_mutex_lock(&lock->lock);
112 if (lock->locked) {
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800113 //DVR_WRAPPER_DEBUG("Enter cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800114 r = pthread_cond_timedwait(&lock->cond, &lock->lock, tv);
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800115 //DVR_WRAPPER_DEBUG("Leave cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800116 }
117 if (r == 0) {
118 if (!lock->locked) {
119 lock->locked = 1;
120 } else {
121 r = ETIMEDOUT;
122 }
123 }
124 pthread_mutex_unlock(&lock->lock);
125
126 return r;
127}
128
129#define WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(expr, lock)\
130 do {\
131 if (!(expr)) {\
132 DVR_INFO("%s-%d failed", __func__, __LINE__);\
133 wrapper_mutex_unlock(lock);\
134 return DVR_FAILURE;\
135 }\
136 } while (0);
137
138
139typedef struct {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800140 /*make lock the 1st item in the structure*/
Gong Kefdb31922022-06-17 17:11:16 +0800141 DVR_WrapperMutex_t wrapper_lock;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800142
143 /*rec or play*/
144 int type;
145
146 /*valid if (sn != 0)*/
147 unsigned long sn;
148 unsigned long sn_linked;
149
150 struct list_head segments; /**<head-add list*/
151 uint64_t current_segment_id; /**<id of the current segment*/
152
153 union {
154 struct {
155 DVR_WrapperRecordOpenParams_t param_open;
156 DVR_RecordStartParams_t param_start;
157 DVR_RecordStartParams_t param_update;
158 DVR_RecordHandle_t recorder;
159 DVR_RecordEventFunction_t event_fn;
160 void *event_userdata;
161
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800162 /*total status = seg_status + status + obsolete*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800163 DVR_RecordStatus_t seg_status; /**<status of current segment*/
164 DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
165 uint64_t next_segment_id;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800166
167 DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800168 } record;
169
170 struct {
171 DVR_WrapperPlaybackOpenParams_t param_open;
172 DVR_PlaybackHandle_t player;
173 DVR_PlaybackEventFunction_t event_fn;
174 void *event_userdata;
175
176 /*total status = seg_status + status*/
177 DVR_PlaybackStatus_t seg_status;
178 DVR_WrapperPlaybackStatus_t status;
179 DVR_PlaybackPids_t pids_req;
180 DVR_PlaybackEvent_t last_event;
Zhiqiang Han3eb75f92020-04-08 10:07:55 +0800181 float speed;
hualing chenb5cd42e2020-04-15 17:03:34 +0800182 DVR_Bool_t reach_end;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800183
184 DVR_WrapperInfo_t obsolete;
hualing chen56c0a162022-01-27 17:01:50 +0800185 DVR_Bool_t tf_full;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800186 } playback;
187 };
188} DVR_WrapperCtx_t;
189
190typedef struct {
191 struct list_head head;
192 unsigned long sn;
193
194 /* rec or playback */
195 int type;
196
197 union {
198 struct {
199 DVR_RecordEvent_t event;
200 DVR_RecordStatus_t status;
201 } record;
202 struct {
203 DVR_PlaybackEvent_t event;
204 DVR_Play_Notify_t status;
205 } playback;
206 };
207} DVR_WrapperEventCtx_t;
208
209typedef struct {
210 pthread_mutex_t lock;
211 char *name;
212 int running;
213 pthread_cond_t cond;
214 pthread_t thread;
215 int type;
216} DVR_WrapperThreadCtx_t;
217
218typedef struct {
219 struct list_head head;
220
221 DVR_RecordSegmentInfo_t seg_info;
222 DVR_PlaybackSegmentInfo_t playback_info;
223} DVR_WrapperPlaybackSegmentInfo_t;
224
225typedef struct {
226 struct list_head head;
227
228 DVR_RecordSegmentInfo_t info;
229} DVR_WrapperRecordSegmentInfo_t;
230
231/* serial num generater */
232static unsigned long sn = 1;
233static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER;
234
235static inline unsigned long get_sn()
236{
hualing chenab0d1262021-09-26 15:22:50 +0800237 unsigned long no = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800238
239 pthread_mutex_lock(&sn_lock);
240 no = sn++;
241 if (!no)
242 no = sn++;
243 pthread_mutex_unlock(&sn_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800244 return no;
245}
246
247/* entity ctx */
248#define DVR_WRAPPER_MAX 10
249
250static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] =
251{
252 [0 ... (DVR_WRAPPER_MAX - 1)] =
253 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800254 .type = W_REC,
255 }
256};
257
258static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] =
259{
260 [0 ... (DVR_WRAPPER_MAX - 1)] =
261 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800262 .type = W_PLAYBACK,
263 }
264};
265
266/* events lists */
267static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list);
268static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list);
269
270static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
271static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
272
273static DVR_WrapperThreadCtx_t wrapper_thread[2] =
274{
275 [0] =
276 {
277 .lock = PTHREAD_MUTEX_INITIALIZER,
278 .running = 0,
279 .name = "record",
280 .type = W_REC,
281 },
282 [1] =
283 {
284 .lock = PTHREAD_MUTEX_INITIALIZER,
285 .running = 0,
286 .name = "playback",
287 .type = W_PLAYBACK,
288 },
289};
290
291/*now only support one timeshift now*/
292static unsigned long sn_timeshift_record;
293static unsigned long sn_timeshift_playback;
294
295static void *wrapper_task(void *arg);
296static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx);
297
298static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata);
299static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata);
300
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800301static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status);
302static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800303
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800304static int get_timespec_timeout(int timeout, struct timespec *ts)
305{
306 struct timespec ots;
307 int left, diff;
308
309 clock_gettime(CLOCK_MONOTONIC, &ots);
310
311 ts->tv_sec = ots.tv_sec + timeout / 1000;
312 ts->tv_nsec = ots.tv_nsec;
313
314 left = timeout % 1000;
315 left *= 1000000;
316 diff = 1000000000 - ots.tv_nsec;
317
318 if (diff <= left) {
319 ts->tv_sec++;
320 ts->tv_nsec = left-diff;
321 } else {
322 ts->tv_nsec += left;
323 }
324
325 return 0;
326}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800327
328static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock)
329{
Wentao MA270dc0f2022-08-23 13:17:26 +0800330 DVR_WrapperEventCtx_t *p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800331
332 pthread_mutex_lock(list_lock);
333 if (list_empty(list))
Wentao MA270dc0f2022-08-23 13:17:26 +0800334 p_evt = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800335 else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800336 p_evt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
337 list_del(&p_evt->head);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800338 }
339 pthread_mutex_unlock(list_lock);
340
Wentao MA270dc0f2022-08-23 13:17:26 +0800341 return p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800342}
343
344static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent()
345{
346 return ctx_getEvent(&record_evt_list, &record_evt_list_lock);
347}
348
349static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent()
350{
351 return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock);
352}
353
354static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt)
355{
356 DVR_WrapperEventCtx_t *padd;
357 padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t));
358 DVR_RETURN_IF_FALSE(padd);
359
360 *padd = *evt;
361 pthread_mutex_lock(lock);
wentao.maa22bc852022-10-13 12:18:06 +0800362 list_add_tail(padd, list);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800363 pthread_mutex_unlock(lock);
364 return DVR_SUCCESS;
365}
366
367static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt)
368{
369 free(evt);
370}
371
372/*useless*/
373static void ctx_cleanOutdatedEvents(struct list_head *evt_list,
374 pthread_mutex_t *evt_list_lock,
375 DVR_WrapperCtx_t *list)
376{
Wentao MA270dc0f2022-08-23 13:17:26 +0800377 DVR_WrapperEventCtx_t *p_evt, *p_evt_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800378 unsigned long sns[DVR_WRAPPER_MAX];
379 int cnt = 0;
380 int i;
381 int found = 0;
382
383 /*copy all valid sns*/
384 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
385 sns[cnt] = list[i].sn;
386 if (!sns[cnt])
387 cnt++;
388 }
389
390 /*free evts that not belong to any valid sns*/
391 pthread_mutex_lock(evt_list_lock);
Wentao MA270dc0f2022-08-23 13:17:26 +0800392 list_for_each_entry_safe(p_evt, p_evt_tmp, evt_list, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800393 for (i = 0; i < cnt; i++) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800394 if (p_evt->sn == sns[i]) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800395 found = 1;
396 break;
397 }
398 }
399 if (!found) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800400 list_del(&p_evt->head);
401 ctx_freeEvent(p_evt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800402 }
403 }
404 pthread_mutex_unlock(evt_list_lock);
405}
406
407static inline void ctx_cleanOutdatedRecordEvents()
408{
409 ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list);
410}
411
412static inline void ctx_cleanOutdatedPlaybackEvents()
413{
414 ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list);
415}
416
hualing chenb9b358a2021-08-17 15:06:36 +0800417//check this play is recording file
418//return 0 if not the recording
419//else return record id
420static inline int ctx_isPlay_recording(char *play_location)
421{
422 int i;
423 DVR_WrapperCtx_t *cnt;
424
425 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
426 cnt = &record_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800427 //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 +0800428 if (!strcmp(cnt->record.param_open.location, play_location)) {
Wentao MA96f68962022-06-15 19:45:35 +0800429 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 +0800430 return cnt->sn;
431 }
432 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800433 DVR_WRAPPER_INFO(" not found any play is in recording [%d]", DVR_WRAPPER_MAX);
hualing chenb9b358a2021-08-17 15:06:36 +0800434 return 0;
435}
436//check this record is playing file
437//return 0 if not the playing
438//else return playback id
439static inline int ctx_isRecord_playing(char *rec_location)
440{
441 int i;
442 DVR_WrapperCtx_t *cnt;
443 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
444 cnt = &playback_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800445 //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 +0800446 if (!strcmp(cnt->playback.param_open.location, rec_location)) {
Wentao MA96f68962022-06-15 19:45:35 +0800447 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 +0800448 return cnt->sn;
449 }
450 }
Wentao MA96f68962022-06-15 19:45:35 +0800451 DVR_WRAPPER_INFO(" not found rec is playing [%d]", DVR_WRAPPER_MAX);
hualing chenb9b358a2021-08-17 15:06:36 +0800452 return 0;
453}
454
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800455static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list)
456{
457 int i;
458 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
Gong Kefdb31922022-06-17 17:11:16 +0800459 if (list[i].sn == sn) {
460 wrapper_mutex_init(&list[i].wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800461 return &list[i];
Gong Kefdb31922022-06-17 17:11:16 +0800462 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800463 }
464 return NULL;
465}
466
467static inline void ctx_reset(DVR_WrapperCtx_t *ctx)
468{
469 memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn),
470 0,
471 sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn));
472}
473
474static inline int ctx_valid(DVR_WrapperCtx_t *ctx)
475{
476 return (ctx->sn != 0);
477}
478
479static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn)
480{
481 return ctx_get(sn, record_list);
482}
483
484static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn)
485{
486 return ctx_get(sn, playback_list);
487}
488
489static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *))
490{
491 pthread_mutex_lock(&ctx->lock);
492 if (ctx->running == 0) {
wentao.maa210e5e2022-10-12 16:10:03 +0800493 pthread_condattr_t attr = {0};
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800494 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
495 pthread_cond_init(&ctx->cond, &attr);
496 pthread_condattr_destroy(&attr);
Wentao MA96f68962022-06-15 19:45:35 +0800497 DVR_WRAPPER_INFO("start wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800498 pthread_create(&ctx->thread, NULL, thread_fn, ctx);
Wentao MA96f68962022-06-15 19:45:35 +0800499 DVR_WRAPPER_INFO("wrapper thread(%s) started\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800500 }
501 ctx->running++;
502 pthread_mutex_unlock(&ctx->lock);
503 return 0;
504}
505
506static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx)
507{
508 pthread_mutex_lock(&ctx->lock);
509 ctx->running--;
510 if (!ctx->running) {
511 pthread_cond_broadcast(&ctx->cond);
512 pthread_mutex_unlock(&ctx->lock);
513
Wentao MA96f68962022-06-15 19:45:35 +0800514 DVR_WRAPPER_INFO("stop wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800515 pthread_join(ctx->thread, NULL);
Wentao MA96f68962022-06-15 19:45:35 +0800516 DVR_WRAPPER_INFO("wrapper thread(%s) stopped\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800517
518 pthread_mutex_lock(&ctx->lock);
519 if (!ctx->running) /*protect*/
520 pthread_cond_destroy(&ctx->cond);
521 }
522 pthread_mutex_unlock(&ctx->lock);
523 return 0;
524}
525
526#define WRAPPER_THREAD_RECORD (&wrapper_thread[0])
527#define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1])
528
529static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx)
530{
531 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
532 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
533 return wrapper_requestThread(thread_ctx, wrapper_task);
534}
535
536static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx)
537{
538 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
539 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
540 return wrapper_releaseThread(thread_ctx);
541}
542
543static inline int wrapper_releaseThreadForType(int type)
544{
545 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)?
546 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
547 return wrapper_releaseThread(thread_ctx);
548}
549
550static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx)
551{
552 pthread_cond_signal(&thread_ctx->cond);
553}
554
555static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx)
556{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800557 struct timespec rt;
558 get_timespec_timeout(200, &rt);
559 pthread_cond_timedwait(&thread_ctx->cond, &thread_ctx->lock, &rt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800560 return 0;
561}
562
563static inline void wrapper_threadSignalForType(int type)
564{
565 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ?
566 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
567 wrapper_threadSignal(thread_ctx);
568}
569
570static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx)
571{
572 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
573 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
574 wrapper_threadSignal(thread_ctx);
575}
576
577static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx)
578{
579 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
580 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
581 wrapper_threadWait(thread_ctx);
582 return 0;
583}
584
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800585/*return condition, locked if condition == true*/
Gong Kefdb31922022-06-17 17:11:16 +0800586static int wrapper_mutex_lock_if(DVR_WrapperMutex_t *lock, int *condition)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800587{
588 int r2;
589 do {
590 struct timespec rt2;
Gong Kefdb31922022-06-17 17:11:16 +0800591 get_timespec_timeout(10, &rt2);
592 r2 = wrapper_mutex_timedlock(lock, &rt2);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800593 } while (*condition && (r2 == ETIMEDOUT));
594
595 if (!(*condition) && (r2 == 0))
Gong Kefdb31922022-06-17 17:11:16 +0800596 wrapper_mutex_unlock(lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800597
598 return *condition;
599}
600
601static void *wrapper_task(void *arg)
602{
Wentao MA270dc0f2022-08-23 13:17:26 +0800603 DVR_WrapperThreadCtx_t *thread_ctx = (DVR_WrapperThreadCtx_t *)arg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800604 DVR_WrapperEventCtx_t *evt;
605
Wentao MA270dc0f2022-08-23 13:17:26 +0800606 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800607
Wentao MA270dc0f2022-08-23 13:17:26 +0800608 while (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800609 {
610 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800611
Wentao MA270dc0f2022-08-23 13:17:26 +0800612 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800613 if (!evt)
Wentao MA270dc0f2022-08-23 13:17:26 +0800614 ret = wrapper_threadWait(thread_ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800615 }
616
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800617 while (evt) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800618 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
619 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chenbc0aec92021-03-18 14:52:40 +0800620 if (ctx == NULL) {
Wentao MA96f68962022-06-15 19:45:35 +0800621 DVR_WRAPPER_INFO("warp not get ctx.free event..\n");
hualing chenbc0aec92021-03-18 14:52:40 +0800622 goto processed;
623 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800624 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);
625 if (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800626 /*
627 continue not break,
628 make all events consumed, or mem leak
629 */
Wentao MA270dc0f2022-08-23 13:17:26 +0800630 if (!wrapper_mutex_lock_if(&ctx->wrapper_lock, &thread_ctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800631 goto processed;
632
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800633 if (ctx_valid(ctx)) {
634 /*double check after lock*/
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800635 if (evt->sn == ctx->sn) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800636 pthread_mutex_unlock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800637 process_handleEvents(evt, ctx);
Wentao MA270dc0f2022-08-23 13:17:26 +0800638 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800639 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800640 }
Gong Kefdb31922022-06-17 17:11:16 +0800641 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800642 }
643
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800644processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800645 ctx_freeEvent(evt);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800646
Wentao MA270dc0f2022-08-23 13:17:26 +0800647 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800648 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800649 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 +0800650 }
651
Wentao MA270dc0f2022-08-23 13:17:26 +0800652 pthread_mutex_unlock(&thread_ctx->lock);
653 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 +0800654 return NULL;
655}
656
657static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
658{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800659 pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800660 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
661 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800662 pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800663 return 0;
664}
665
666static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
667{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800668 pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800669 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
670 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800671 pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800672 return 0;
673}
674
675static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
676{
Wentao MA270dc0f2022-08-23 13:17:26 +0800677 DVR_WrapperPlaybackSegmentInfo_t *p_seg, *p_seg_tmp;
678 list_for_each_entry_safe(p_seg, p_seg_tmp, &ctx->segments, head) {
679 list_del(&p_seg->head);
680 free(p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800681 }
682}
683
Wentao MA270dc0f2022-08-23 13:17:26 +0800684static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800685 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
686{
687 (void)ctx;
688 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800689 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800690 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800691 p_seg->seg_info.nb_pids = seg_info->nb_pids;
692 memcpy(p_seg->seg_info.pids, seg_info->pids, sizeof(p_seg->seg_info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800693 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800694 p_seg->seg_info.duration = seg_info->duration;
695 p_seg->seg_info.size = seg_info->size;
696 p_seg->seg_info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800697 }
hualing chen03fd4942021-07-15 15:56:41 +0800698 //update current segment duration on timeshift mode
hualing chenb9b358a2021-08-17 15:06:36 +0800699 if (ctx->playback.param_open.is_timeshift
700 || ctx_isPlay_recording(ctx->playback.param_open.location))
Wentao MA270dc0f2022-08-23 13:17:26 +0800701 dvr_playback_update_duration(ctx->playback.player,p_seg->seg_info.id,p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800702 /*no changes
703 DVR_PlaybackSegmentFlag_t flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800704 p_seg->playback_info.segment_id = p_seg->seg_info.id;
705 strncpy(p_seg->playback_info.location,
706 ctx->playback.param_open.location, sizeof(p_seg->playback_info.location));
707 p_seg->playback_info.pids = ctx->playback.pids_req;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800708 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
709 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
710 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
Wentao MA270dc0f2022-08-23 13:17:26 +0800711 p_seg->playback_info.flags = flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800712 */
713}
714
715static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
716{
Wentao MA270dc0f2022-08-23 13:17:26 +0800717 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800718
Wentao MA96f68962022-06-15 19:45:35 +0800719 DVR_WRAPPER_INFO("timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800720 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
721
722 if (list_empty(&ctx->segments)) {
Wentao MA96f68962022-06-15 19:45:35 +0800723 DVR_WRAPPER_INFO("timeshift, update while no segment exists, ignore\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800724 return DVR_SUCCESS;
725 }
726
727 /*normally, the last segment added will be updated*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800728 p_seg =
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800729 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800730 if (p_seg->seg_info.id == seg_info->id) {
731 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800732 } else {
wentao.mafd5283f2022-10-14 09:51:13 +0800733 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +0800734 // prefetch() here incurring self_assign is used to avoid some compiling
735 // warnings.
736 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +0800737 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
738 if (p_seg->seg_info.id == seg_info->id) {
739 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800740 break;
741 }
742 }
743 }
744
745 /*need to notify the dvr_playback*/
hualing chenb9b358a2021-08-17 15:06:36 +0800746 if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/
747 || ctx_isPlay_recording(ctx->playback.param_open.location))
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800748 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
749 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
750 if (
751 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
752 (ctx->playback.seg_status.segment_id == seg_info->id
753 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
754 ||
755 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
756 (ctx->playback.seg_status.segment_id != seg_info->id
757 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
758 )
759 {
760 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800761 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800762 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800763 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800764
765 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800766 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 +0800767 ctx->sn, error,
768 seg_info->id, seg_info->duration,
769 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
770 }
771 }
772
773 return DVR_SUCCESS;
774}
775
Wentao MA270dc0f2022-08-23 13:17:26 +0800776static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800777 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
778{
779 (void)ctx;
780 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800781 p_seg->info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800782 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800783 p_seg->info.nb_pids = seg_info->nb_pids;
784 memcpy(p_seg->info.pids, seg_info->pids, sizeof(p_seg->info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800785 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800786 p_seg->info.duration = seg_info->duration;
787 p_seg->info.size = seg_info->size;
788 p_seg->info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800789 }
790}
791
792static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
793{
Wentao MA270dc0f2022-08-23 13:17:26 +0800794 DVR_WrapperRecordSegmentInfo_t *p_seg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800795
796 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800797 if (!list_empty(&ctx->segments)) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800798 p_seg =
hualing chen266b9502020-04-04 17:39:39 +0800799 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800800 if (p_seg->info.id == seg_info->id) {
801 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800802 } else {
wentao.mafd5283f2022-10-14 09:51:13 +0800803 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +0800804 // prefetch() here incurring self_assign is used to avoid some compiling
805 // warnings.
806 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +0800807 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
808 if (p_seg->info.id == seg_info->id) {
809 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800810 break;
811 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800812 }
813 }
814 }
815
816 /*timeshift, update the segment for playback*/
817 /*
818 the playback should grab the segment info other than the id,
819 and the id will be updated by each segment-add during the recording
820 */
821 /*
822 the playback paused if no data been checked from recording,
823 should resume the player later when there's more data
824 */
hualing chenb9b358a2021-08-17 15:06:36 +0800825 int sn = 0;
826 if (ctx->record.param_open.is_timeshift ||
827 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
828 DVR_WrapperCtx_t *ctx_playback;
829 if (ctx->record.param_open.is_timeshift)
830 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
831 else
832 ctx_playback = ctx_getPlayback(sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800833
834 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800835 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800836 if (ctx_valid(ctx_playback)
hualing chenb9b358a2021-08-17 15:06:36 +0800837 && (ctx_playback->sn == sn_timeshift_playback ||
838 ctx_playback->sn == sn)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800839 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
840 }
Gong Kefdb31922022-06-17 17:11:16 +0800841 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800842 }
843 }
844
845 return DVR_SUCCESS;
846}
847
848static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
849 DVR_RecordSegmentInfo_t *seg_info,
850 DVR_PlaybackPids_t *p_pids,
851 DVR_PlaybackSegmentFlag_t flags)
852{
Wentao MA270dc0f2022-08-23 13:17:26 +0800853 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800854 int error;
855
856 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +0800857 p_seg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
858 if (!p_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800859 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +0800860 DVR_WRAPPER_INFO("memory fail\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800861 return error;
862 }
863
Wentao MA270dc0f2022-08-23 13:17:26 +0800864 /*copy the original segment info*/
865 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800866 /*generate the segment info used in playback*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800867 p_seg->playback_info.segment_id = p_seg->seg_info.id;
Wentao MAe88ad702022-09-02 10:35:00 +0800868 const int len = strlen(ctx->playback.param_open.location);
869 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
870 DVR_WRAPPER_ERROR("Invalid playback.param_open.location length %d", len);
Wentao MA4d85ff32022-09-23 11:36:18 +0800871 free(p_seg);
Wentao MAe88ad702022-09-02 10:35:00 +0800872 return DVR_FAILURE;
873 }
874 strncpy(p_seg->playback_info.location, ctx->playback.param_open.location, len+1);
Wentao MA270dc0f2022-08-23 13:17:26 +0800875 p_seg->playback_info.pids = *p_pids;
876 p_seg->playback_info.flags = flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800877 p_seg->playback_info.duration = p_seg->seg_info.duration;
wentao.maa22bc852022-10-13 12:18:06 +0800878 list_add(p_seg, &ctx->segments);
879 DVR_WRAPPER_INFO("start to add segment %lld\n", p_seg->playback_info.segment_id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800880
Wentao MA270dc0f2022-08-23 13:17:26 +0800881 error = dvr_playback_add_segment(ctx->playback.player, &p_seg->playback_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800882 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800883 DVR_WRAPPER_INFO("fail to add segment %lld (%d)\n", p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800884 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800885 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
886 ctx->playback.status.info_full.size += p_seg->seg_info.size;
887 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800888 }
889
890 return error;
891}
892
893static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
894{
Wentao MA270dc0f2022-08-23 13:17:26 +0800895 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MA16f870e2022-09-09 11:00:22 +0800896 int error = DVR_SUCCESS;
hualing chenab0d1262021-09-26 15:22:50 +0800897 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800898
Wentao MA270dc0f2022-08-23 13:17:26 +0800899 p_seg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
900 if (!p_seg) {
Wentao MA16f870e2022-09-09 11:00:22 +0800901 DVR_WRAPPER_ERROR("memory allocation failed");
902 return DVR_FAILURE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800903 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800904 p_seg->info = *seg_info;
wentao.maa22bc852022-10-13 12:18:06 +0800905 list_add(p_seg, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800906
hualing chenb9b358a2021-08-17 15:06:36 +0800907 if (ctx->record.param_open.is_timeshift ||
908 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
909
910 DVR_WrapperCtx_t *ctx_playback;
911 if (ctx->record.param_open.is_timeshift)
912 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
913 else
914 ctx_playback = ctx_getPlayback(sn);
915
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800916 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) add seg\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800917
918 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800919 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800920 if (ctx_valid(ctx_playback)) {
921 DVR_PlaybackSegmentFlag_t flags;
922
923 /*only if playback has started, the previous segments have been loaded*/
924 if (!list_empty(&ctx_playback->segments)) {
925 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800926 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800927 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
wentao.maa210e5e2022-10-12 16:10:03 +0800928 error = wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
929 if (error == DVR_FAILURE) {
930 DVR_WRAPPER_WARN("adding playback segment fails");
931 }
hualing chen451c8f72022-03-09 13:05:52 +0800932 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800933 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) list empty\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800934 }
hualing chenb9b358a2021-08-17 15:06:36 +0800935 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800936 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800937 }
Gong Kefdb31922022-06-17 17:11:16 +0800938 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800939 }
hualing chen451c8f72022-03-09 13:05:52 +0800940 else
941 {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800942 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid 2\n", ctx->sn, sn);
943 }
944
945 /*if it is not a timeshift recording, but a playing recording,
946 do not forget to obey the recording rule: link the segment!*/
947 if (!ctx->record.param_open.is_timeshift) {
948 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
949 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
hualing chen451c8f72022-03-09 13:05:52 +0800950 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800951 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800952 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
Wentao MAe8ba5172022-08-09 11:18:17 +0800953 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800954 }
955
956 return error;
957}
958
959static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
960{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800961 int error = -1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800962 DVR_WrapperPlaybackSegmentInfo_t *p_seg = NULL, *p_seg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800963 uint32_t off_set = 0;
Wentao MA96f68962022-06-15 19:45:35 +0800964 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800965
Wentao MA270dc0f2022-08-23 13:17:26 +0800966 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
967 if (p_seg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800968
969 if (ctx->current_segment_id == seg_info->id) {
970 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
971
972 /*drive the player out of this will-be-deleted segment*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800973 next_seg = list_prev_entry(p_seg, head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800974
975 if (ctx->playback.speed != 100.0f) {
976 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800977 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800978 }
hualing chenb9a1a2c2021-12-31 11:27:59 +0800979 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
980 off_set = 10 * 1000;
981 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800982 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 +0800983
984 if (ctx->playback.speed == 0.0f) {
985 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +0800986 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 +0800987 } else if (ctx->playback.speed != 100.0f) {
988 DVR_PlaybackSpeed_t dvr_speed = {
989 .speed = { ctx->playback.speed },
990 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
991 };
992 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
Wentao MA96f68962022-06-15 19:45:35 +0800993 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 +0800994 }
995 }
996
997 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
998 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800999 /*remove playback segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +08001000 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 +08001001 }
1002
Wentao MA270dc0f2022-08-23 13:17:26 +08001003 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001004
1005 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001006 ctx->playback.obsolete.time += p_seg->seg_info.duration;
1007 ctx->playback.obsolete.size += p_seg->seg_info.size;
1008 ctx->playback.obsolete.pkts += p_seg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +08001009 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 +08001010 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Wentao MA270dc0f2022-08-23 13:17:26 +08001011 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001012 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001013 }
1014 }
1015
Wentao MA96f68962022-06-15 19:45:35 +08001016 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 +08001017
1018 return error;
1019}
1020
1021static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
1022{
1023 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08001024 DVR_WrapperRecordSegmentInfo_t *p_seg, *p_seg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001025
Wentao MAf4072032022-06-30 13:50:45 +08001026 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1027 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001028
1029 /*if timeshifting, notify the playback first, then deal with record*/
1030 if (ctx->record.param_open.is_timeshift) {
1031 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1032
1033 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001034 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001035 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1036 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001037 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1038 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1039 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001040 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001041 return DVR_SUCCESS;
1042 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001043 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1044 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1045 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001046 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001047 if (ctx_valid(ctx_playback)
1048 && ctx_playback->sn == sn_timeshift_playback
1049 && !list_empty(&ctx_playback->segments)) {
1050 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
Wentao MA07d3d742022-09-06 09:58:05 +08001051 if (error != DVR_SUCCESS) {
1052 DVR_WRAPPER_ERROR("wrapper_removePlaybackSegment failed with return value %d",error);
1053 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001054 }
hualing chen56c0a162022-01-27 17:01:50 +08001055 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001056 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001057 }
1058 }
1059
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001060 uint64_t id = seg_info->info.id;
1061
Wentao MA270dc0f2022-08-23 13:17:26 +08001062 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
1063 if (p_seg->info.id == id) {
1064 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001065
1066 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001067 ctx->record.obsolete.time += p_seg->info.duration;
1068 ctx->record.obsolete.size += p_seg->info.size;
1069 ctx->record.obsolete.pkts += p_seg->info.nb_packets;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001070
Wentao MA270dc0f2022-08-23 13:17:26 +08001071 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001072 break;
1073 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001074 }
1075
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001076 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001077
Wentao MAf4072032022-06-30 13:50:45 +08001078 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1079 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001080
1081 return error;
1082}
1083
1084int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1085{
1086 int error;
1087 DVR_WrapperCtx_t *ctx;
1088 DVR_RecordOpenParams_t open_param;
1089
1090 DVR_RETURN_IF_FALSE(rec);
1091 DVR_RETURN_IF_FALSE(params);
1092
1093 /*get a free ctx*/
1094 ctx = ctx_getRecord(0);
1095 DVR_RETURN_IF_FALSE(ctx);
1096
Gong Kefdb31922022-06-17 17:11:16 +08001097 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001098
Wentao MA9a164002022-08-29 11:20:24 +08001099 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 +08001100 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001101
1102 ctx_reset(ctx);
1103
1104 ctx->record.param_open = *params;
1105 ctx->record.event_fn = params->event_fn;
1106 ctx->record.event_userdata = params->event_userdata;
1107 ctx->record.next_segment_id = 0;
1108 ctx->current_segment_id = 0;
1109 INIT_LIST_HEAD(&ctx->segments);
1110 ctx->sn = get_sn();
1111
1112 wrapper_requestThreadFor(ctx);
1113
hualing chen266b9502020-04-04 17:39:39 +08001114 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001115 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001116 open_param.dmx_dev_id = params->dmx_dev_id;
1117 open_param.data_from_memory = 0;
1118 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001119 if (params->flush_size) {
1120 open_param.notification_size = params->flush_size;
1121 } else {
1122 open_param.notification_size = 64*1024;
1123 }
hualing chen002e5b92022-02-23 17:51:21 +08001124 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001125 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001126 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001127 open_param.event_fn = wrapper_record_event_handler;
1128 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001129 if (params->keylen) {
1130 open_param.clearkey = params->clearkey;
1131 open_param.cleariv = params->cleariv;
1132 open_param.keylen = params->keylen;
1133 }
wentao.ma35a69d42022-03-10 18:08:40 +08001134 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001135 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001136
1137 error = dvr_record_open(&ctx->record.recorder, &open_param);
1138 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001139 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001140 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001141 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001142 wrapper_releaseThreadForType(ctx->type);
1143 return DVR_FAILURE;
1144 }
1145 if (params->is_timeshift)
1146 sn_timeshift_record = ctx->sn;
1147
Wentao MA96f68962022-06-15 19:45:35 +08001148 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001149
Yahui Han1fbf3292021-11-08 18:17:19 +08001150 if (params->crypto_fn) {
1151 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1152 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001153 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 +08001154 }
hualing chen266b9502020-04-04 17:39:39 +08001155 }
1156
Gong Kefdb31922022-06-17 17:11:16 +08001157 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001158
1159 *rec = (DVR_WrapperRecord_t)ctx->sn;
1160 return DVR_SUCCESS;
1161}
1162
1163int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1164{
1165 DVR_WrapperCtx_t *ctx;
1166 DVR_RecordSegmentInfo_t seg_info;
1167 int error;
1168
1169 DVR_RETURN_IF_FALSE(rec);
1170
1171 ctx = ctx_getRecord((unsigned long)rec);
1172 DVR_RETURN_IF_FALSE(ctx);
1173
Gong Kefdb31922022-06-17 17:11:16 +08001174 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001175 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001176 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001177
1178 memset(&seg_info, 0, sizeof(seg_info));
wentao.maa210e5e2022-10-12 16:10:03 +08001179 dvr_record_stop_segment(ctx->record.recorder, &seg_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001180
1181 error = dvr_record_close(ctx->record.recorder);
1182
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001183 if (ctx->record.param_open.is_timeshift)
1184 sn_timeshift_record = 0;
1185
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001186 ctx_freeSegments(ctx);
1187
Wentao MA96f68962022-06-15 19:45:35 +08001188 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001189 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001190 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001191
1192 wrapper_releaseThreadForType(ctx->type);
1193
1194 return error;
1195}
1196
1197int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1198{
1199 DVR_WrapperCtx_t *ctx;
1200 DVR_RecordStartParams_t *start_param;
1201 int i;
1202 int error;
1203
1204 DVR_RETURN_IF_FALSE(rec);
1205 DVR_RETURN_IF_FALSE(params);
1206
1207 ctx = ctx_getRecord((unsigned long)rec);
1208 DVR_RETURN_IF_FALSE(ctx);
1209
Gong Kefdb31922022-06-17 17:11:16 +08001210 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001211 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 +08001212 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001213
1214 start_param = &ctx->record.param_start;
1215 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001216 const int len = strlen(ctx->record.param_open.location);
1217 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1218 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1219 pthread_mutex_unlock(&ctx->wrapper_lock);
1220 return DVR_FAILURE;
1221 }
1222 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001223 start_param->segment.segment_id = ctx->record.next_segment_id++;
1224 start_param->segment.nb_pids = params->pids_info.nb_pids;
1225 for (i = 0; i < params->pids_info.nb_pids; i++) {
1226 start_param->segment.pids[i] = params->pids_info.pids[i];
1227 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1228 }
hualing chena5f03222021-12-02 11:22:35 +08001229 if (params->save_rec_file == 0)//default is not save
1230 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001231 {
1232 /*sync to update for further use*/
1233 DVR_RecordStartParams_t *update_param;
1234 update_param = &ctx->record.param_update;
1235 memcpy(update_param, start_param, sizeof(*update_param));
1236 for (i = 0; i < update_param->segment.nb_pids; i++)
1237 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1238 }
1239
1240 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1241 {
1242 DVR_RecordSegmentInfo_t new_seg_info =
1243 { .id = start_param->segment.segment_id, };
1244 wrapper_addRecordSegment(ctx, &new_seg_info);
1245 }
1246
Wentao MA96f68962022-06-15 19:45:35 +08001247 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001248
Gong Kefdb31922022-06-17 17:11:16 +08001249 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001250
1251 return error;
1252}
1253
1254int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1255{
1256 DVR_WrapperCtx_t *ctx;
1257 DVR_RecordSegmentInfo_t seg_info;
1258 int error;
1259
1260 DVR_RETURN_IF_FALSE(rec);
1261
1262 ctx = ctx_getRecord((unsigned long)rec);
1263 DVR_RETURN_IF_FALSE(ctx);
1264
Gong Kefdb31922022-06-17 17:11:16 +08001265 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001266 DVR_WRAPPER_INFO("stop record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001267 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001268
1269 memset(&seg_info, 0, sizeof(seg_info));
1270 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1271 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1272
Wentao MA96f68962022-06-15 19:45:35 +08001273 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001274 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001275
1276 return error;
1277}
1278
hualing chen03fd4942021-07-15 15:56:41 +08001279int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1280{
1281 DVR_WrapperCtx_t *ctx;
1282 int error;
1283
1284 DVR_RETURN_IF_FALSE(rec);
1285
1286 ctx = ctx_getRecord((unsigned long)rec);
1287 DVR_RETURN_IF_FALSE(ctx);
1288
Gong Kefdb31922022-06-17 17:11:16 +08001289 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001290 DVR_WRAPPER_INFO("pause record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001291 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001292
1293 error = dvr_record_pause(ctx->record.recorder);
1294
Wentao MA96f68962022-06-15 19:45:35 +08001295 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001296 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001297
1298 return error;
1299}
1300
1301int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1302{
1303 DVR_WrapperCtx_t *ctx;
1304 int error;
1305
1306 DVR_RETURN_IF_FALSE(rec);
1307
1308 ctx = ctx_getRecord((unsigned long)rec);
1309 DVR_RETURN_IF_FALSE(ctx);
1310
Gong Kefdb31922022-06-17 17:11:16 +08001311 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001312 DVR_WRAPPER_INFO("resume record(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001313 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001314
1315 error = dvr_record_resume(ctx->record.recorder);
1316
Wentao MA96f68962022-06-15 19:45:35 +08001317 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001318 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001319
1320 return error;
1321}
1322
Wentao MAcdea4762022-04-26 13:28:56 +08001323/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001324static DVR_Bool_t pids_test_include(
1325 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1326 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001327{
Wentao MAcdea4762022-04-26 13:28:56 +08001328 DVR_Bool_t ret = DVR_TRUE;
1329 for (int i=0;i<size2;i++)
1330 { // iterate all elements in arr2 to check if they exist in arr1
1331 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001332
1333 if (act2[i] == DVR_RECORD_PID_CLOSE)
1334 continue;
1335
Wentao MAcdea4762022-04-26 13:28:56 +08001336 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001337 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001338 if (act1[j] != DVR_RECORD_PID_CLOSE
1339 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001340 {
1341 found=DVR_TRUE;
1342 break;
1343 }
1344 }
1345 if (found == DVR_FALSE)
1346 {
Wentao MAcdea4762022-04-26 13:28:56 +08001347 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001348 break;
1349 }
1350 }
Wentao MAcdea4762022-04-26 13:28:56 +08001351 return ret;
1352}
1353
1354static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1355 const DVR_WrapperUpdatePidsParams_t* p2)
1356{
1357 int i=0;
1358 char buf[128]={0};
1359 int cnt=0;
1360 int chars=0;
1361
1362 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1363 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1364
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001365 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1366 p2->pids,p2->pid_action,p2->nb_pids);
1367 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1368 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001369 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001370 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001371
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001372 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001373 for (i=0;i<p1->nb_pids;i++)
1374 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001375 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1376 removed++;
1377 continue;
1378 }
Wentao MAcdea4762022-04-26 13:28:56 +08001379 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1380 if (chars<0)
1381 {
1382 break;
1383 }
1384 cnt += chars;
1385 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001386 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001387 memset(buf,0,sizeof(buf));
1388
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001389 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001390 for (i=0,cnt=0;i<p2->nb_pids;i++)
1391 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001392 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1393 removed++;
1394 continue;
1395 }
Wentao MAcdea4762022-04-26 13:28:56 +08001396 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1397 if (chars<0)
1398 {
1399 break;
1400 }
1401 cnt += chars;
1402 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001403 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001404 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001405 return is_equal;
1406}
1407
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001408int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1409{
1410 DVR_WrapperCtx_t *ctx;
1411 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001412 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001413 int i;
1414 int error;
1415
1416 DVR_RETURN_IF_FALSE(rec);
1417 DVR_RETURN_IF_FALSE(params);
1418
1419 ctx = ctx_getRecord((unsigned long)rec);
1420 DVR_RETURN_IF_FALSE(ctx);
1421
Gong Kefdb31922022-06-17 17:11:16 +08001422 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001423 DVR_WRAPPER_INFO("update record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001424 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001425
1426 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001427 if (pids_equal(&(start_param->segment),params))
1428 {
Gong Kefdb31922022-06-17 17:11:16 +08001429 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001430 return DVR_TRUE;
1431 }
1432
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001433 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001434 const int len = strlen(ctx->record.param_open.location);
1435 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1436 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1437 pthread_mutex_unlock(&ctx->wrapper_lock);
1438 return DVR_FAILURE;
1439 }
1440 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001441 start_param->segment.segment_id = ctx->record.next_segment_id++;
1442 start_param->segment.nb_pids = params->nb_pids;
1443 for (i = 0; i < params->nb_pids; i++) {
1444 start_param->segment.pids[i] = params->pids[i];
1445 start_param->segment.pid_action[i] = params->pid_action[i];
1446 }
1447 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1448 {
1449 DVR_RecordSegmentInfo_t new_seg_info =
1450 { .id = start_param->segment.segment_id, };
1451 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1452 wrapper_addRecordSegment(ctx, &new_seg_info);
1453 }
1454
Wentao MA96f68962022-06-15 19:45:35 +08001455 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001456 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001457
1458 return error;
1459}
1460
1461int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1462{
1463 DVR_WrapperCtx_t *ctx;
1464 DVR_WrapperRecordStatus_t s;
1465 int error;
1466
1467 DVR_RETURN_IF_FALSE(rec);
1468 DVR_RETURN_IF_FALSE(status);
1469
1470 ctx = ctx_getRecord((unsigned long)rec);
1471 DVR_RETURN_IF_FALSE(ctx);
1472
Gong Kefdb31922022-06-17 17:11:16 +08001473 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001474
Wentao MA96f68962022-06-15 19:45:35 +08001475 DVR_WRAPPER_INFO("get record(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001476 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001477
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001478 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001479
Wentao MA96f68962022-06-15 19:45:35 +08001480 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001481 ctx->sn,
1482 s.state,
1483 s.info.time,
1484 s.info.size,
1485 s.info.pkts,
1486 error);
1487
1488 *status = s;
1489
Gong Kefdb31922022-06-17 17:11:16 +08001490 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001491
1492 return error;
1493}
1494
hualing chen4fe3bee2020-10-23 13:58:52 +08001495int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1496{
1497 DVR_WrapperCtx_t *ctx;
1498 int error;
1499
1500 DVR_RETURN_IF_FALSE(rec);
1501
1502 ctx = ctx_getRecord((unsigned long)rec);
1503 DVR_RETURN_IF_FALSE(ctx);
1504
Gong Kefdb31922022-06-17 17:11:16 +08001505 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001506 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001507 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001508 return error;
1509}
1510
hualing chen266b9502020-04-04 17:39:39 +08001511int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1512{
1513 DVR_WrapperCtx_t *ctx;
1514 int error;
1515
1516 DVR_RETURN_IF_FALSE(rec);
1517 DVR_RETURN_IF_FALSE(p_secure_buf);
1518
1519 ctx = ctx_getRecord((unsigned long)rec);
1520 DVR_RETURN_IF_FALSE(ctx);
1521
Gong Kefdb31922022-06-17 17:11:16 +08001522 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001523 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001524 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001525 return error;
1526}
1527
1528int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1529{
1530 DVR_WrapperCtx_t *ctx;
1531 int error;
1532
1533 DVR_RETURN_IF_FALSE(rec);
1534 DVR_RETURN_IF_FALSE(func);
1535
1536 ctx = ctx_getRecord((unsigned long)rec);
1537 DVR_RETURN_IF_FALSE(ctx);
1538
Gong Kefdb31922022-06-17 17:11:16 +08001539 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001540 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001541 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001542 return error;
1543}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001544
1545
1546int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1547{
1548 DVR_WrapperCtx_t *ctx;
1549 DVR_PlaybackOpenParams_t open_param;
1550 int error;
1551
1552 DVR_RETURN_IF_FALSE(playback);
1553 DVR_RETURN_IF_FALSE(params);
1554 DVR_RETURN_IF_FALSE(params->playback_handle);
1555
1556 /*get a free ctx*/
1557 ctx = ctx_getPlayback(0);
1558 DVR_RETURN_IF_FALSE(ctx);
1559
Gong Kefdb31922022-06-17 17:11:16 +08001560 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001561
Wentao MA96f68962022-06-15 19:45:35 +08001562 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 +08001563
1564 ctx_reset(ctx);
1565
1566 ctx->playback.param_open = *params;
1567 ctx->playback.event_fn = params->event_fn;
1568 ctx->playback.event_userdata = params->event_userdata;
1569 ctx->current_segment_id = 0;
1570 INIT_LIST_HEAD(&ctx->segments);
1571 ctx->sn = get_sn();
1572
1573 wrapper_requestThreadFor(ctx);
1574
hualing chen266b9502020-04-04 17:39:39 +08001575 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001576 open_param.dmx_dev_id = params->dmx_dev_id;
1577 open_param.block_size = params->block_size;
1578 open_param.is_timeshift = params->is_timeshift;
1579 //open_param.notification_size = 10*1024; //not supported
1580 open_param.event_fn = wrapper_playback_event_handler;
1581 open_param.event_userdata = (void*)ctx->sn;
1582 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001583 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001584 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001585 open_param.vendor = params->vendor;
1586
Yahui Han1fbf3292021-11-08 18:17:19 +08001587 if (params->keylen) {
1588 open_param.clearkey = params->clearkey;
1589 open_param.cleariv = params->cleariv;
1590 open_param.keylen = params->keylen;
1591 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001592
1593 error = dvr_playback_open(&ctx->playback.player, &open_param);
1594 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001595 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001596 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001597 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001598 wrapper_releaseThreadForType(ctx->type);
1599 return DVR_FAILURE;
1600 }
1601 if (params->is_timeshift)
1602 sn_timeshift_playback = ctx->sn;
1603
Wentao MA270dc0f2022-08-23 13:17:26 +08001604 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001605 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1606 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001607 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001608 }
Gong Kefdb31922022-06-17 17:11:16 +08001609 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001610
1611 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1612 return DVR_SUCCESS;
1613}
1614
1615int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1616{
1617 DVR_WrapperCtx_t *ctx;
1618 int error;
1619
1620 DVR_RETURN_IF_FALSE(playback);
1621
1622 ctx = ctx_getPlayback((unsigned long)playback);
1623 DVR_RETURN_IF_FALSE(ctx);
1624
Gong Kefdb31922022-06-17 17:11:16 +08001625 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001626 DVR_WRAPPER_INFO("close playback(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001627 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001628
1629 if (ctx->playback.param_open.is_timeshift)
1630 sn_timeshift_playback = 0;
1631
1632 /*try stop first*/
wentao.maa210e5e2022-10-12 16:10:03 +08001633 dvr_playback_stop(ctx->playback.player, DVR_TRUE);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001634
1635 {
1636 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001637 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001638
wentao.mafd5283f2022-10-14 09:51:13 +08001639 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001640 // prefetch() here incurring self_assign is used to avoid some compiling
1641 // warnings.
1642 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08001643 list_for_each_entry(p_seg, &ctx->segments, head) {
1644 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001645 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001646 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001647 }
1648 ctx_freeSegments(ctx);
1649 }
1650
1651 error = dvr_playback_close(ctx->playback.player);
1652
Wentao MA96f68962022-06-15 19:45:35 +08001653 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001654 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001655 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001656
1657 wrapper_releaseThreadForType(ctx->type);
1658
1659 return error;
1660}
1661
1662int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1663{
1664 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001665 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001666 uint64_t *p_segment_ids;
1667 uint32_t segment_nb;
1668 uint32_t i;
1669 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001670 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001671 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001672 DVR_Bool_t is_timeshift = DVR_FALSE;
Wentao MAcefc13c2022-10-26 15:47:24 +08001673 DVR_PlaybackSegmentFlag_t seg_flags = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001674
1675 DVR_RETURN_IF_FALSE(playback);
1676 DVR_RETURN_IF_FALSE(p_pids);
1677
hualing chenc110f952021-01-18 11:25:37 +08001678 ctx_record = NULL;
1679
1680 /*lock the recorder to avoid changing the recording segments*/
1681 ctx_record = ctx_getRecord(sn_timeshift_record);
1682
1683 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001684 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001685 if (!ctx_valid(ctx_record)
1686 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001687 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001688 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001689 is_timeshift = DVR_FALSE;
1690 } else {
1691 is_timeshift = DVR_TRUE;
1692 }
1693 }
1694
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001695 ctx = ctx_getPlayback((unsigned long)playback);
1696 DVR_RETURN_IF_FALSE(ctx);
1697
Gong Kefdb31922022-06-17 17:11:16 +08001698 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001699
Wentao MA96f68962022-06-15 19:45:35 +08001700 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 +08001701 ctx->sn,
1702 ctx->playback.param_open.location,
1703 flags,
1704 p_pids->video.pid, p_pids->video.format,
1705 p_pids->audio.pid, p_pids->audio.format,
1706 p_pids->ad.pid, p_pids->ad.format,
1707 p_pids->subtitle.pid, p_pids->subtitle.format,
1708 p_pids->pcr.pid);
1709
Gong Kefdb31922022-06-17 17:11:16 +08001710 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001711
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001712 if (ctx->playback.param_open.is_timeshift) {
1713 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001714 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001715 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001716 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001717 return DVR_FAILURE;
1718 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001719 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001720 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001721 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001722 }
1723
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001724 /*obtain all segments in a list*/
1725 segment_nb = 0;
1726 p_segment_ids = NULL;
1727 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001728 if (!error) {
1729 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001730 struct list_head info_list; /**< segment list head*/
1731 INIT_LIST_HEAD(&info_list);
1732
Wentao MA96f68962022-06-15 19:45:35 +08001733 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001734 //we need free info list buf when we used end.
1735 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001736 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001737 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001738 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1739 ctx->playback.param_open.location, error);
wentao.maf57dd232022-10-08 16:07:29 +08001740 // Tainted data issue originating from fgets seem false positive, so we
1741 // just suppress it here.
1742 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001743 for (i = 0; i < segment_nb; i++) {
1744 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001745 memset((void*)&seg_info,0,sizeof(seg_info));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001746
hualing chenb9a02922021-12-14 11:29:47 +08001747 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1748 if (error) {
1749 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001750 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001751 ctx->playback.param_open.location, p_segment_ids[i], error);
1752 break;
1753 }
1754 //add check if has audio or video pid. if not exist. not add segment to playback
1755 int ii = 0;
1756 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001757 // Tainted data issue originating from fgets seem false positive, so we
1758 // just suppress it here.
1759 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001760 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1761 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1762 if (type == DVR_STREAM_TYPE_VIDEO ||
1763 type == DVR_STREAM_TYPE_AUDIO ||
1764 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001765 DVR_WRAPPER_INFO("success to get seg av info \n");
1766 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 +08001767 DVR_STREAM_TYPE_VIDEO,
1768 DVR_STREAM_TYPE_AUDIO,
1769 DVR_STREAM_TYPE_AD);
1770 has_av = 1;
1771 //break;
1772 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001773 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 +08001774 DVR_STREAM_TYPE_VIDEO,
1775 DVR_STREAM_TYPE_AUDIO,
1776 DVR_STREAM_TYPE_AD);
1777 }
1778 }
1779 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001780 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001781 continue;
1782 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001783 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001784 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001785 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1786 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001787 if (error == DVR_FAILURE) {
1788 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001789 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001790 }
hualing chenb9a02922021-12-14 11:29:47 +08001791 /*copy the 1st segment*/
1792 if (got_1st_seg == 0) {
1793 seg_info_1st = seg_info;
1794 got_1st_seg = 1;
1795 }
1796 }
1797 } else {
wentao.maf57dd232022-10-08 16:07:29 +08001798 // Tainted data issue originating from fgets seem false positive, so we
1799 // just suppress it here.
1800 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001801 for (i = 0; i < segment_nb; i++) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001802 DVR_RecordSegmentInfo_t *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001803 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08001804 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001805 // prefetch() here incurring self_assign is used to avoid some compiling
1806 // warnings.
1807 // coverity[self_assign]
Wentao MA4d85ff32022-09-23 11:36:18 +08001808 list_for_each_entry(p_seg_info, &info_list, head)
hualing chenb9a02922021-12-14 11:29:47 +08001809 {
Wentao MA4d85ff32022-09-23 11:36:18 +08001810 if (p_seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001811 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001812 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001813 break;
1814 }
1815 }
1816 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001817 //last info is not found if when recording occured power off.
1818 if (p_segment_ids[i] == segment_nb - 1) {
1819 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001820 memset((void*)&seg_info,0,sizeof(seg_info));
hualing chen8aed9582021-12-24 17:59:56 +08001821
1822 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1823 if (error) {
1824 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001825 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001826 ctx->playback.param_open.location, p_segment_ids[i], error);
1827 break;
1828 }
1829 //
1830 //add check if has audio or video pid. if not exist. not add segment to playback
1831 int ii = 0;
1832 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001833 // Tainted data issue originating from fgets seem false positive, so we
1834 // just suppress it here.
1835 // coverity[tainted_data]
hualing chen8aed9582021-12-24 17:59:56 +08001836 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1837 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1838 if (type == DVR_STREAM_TYPE_VIDEO ||
1839 type == DVR_STREAM_TYPE_AUDIO ||
1840 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001841 DVR_WRAPPER_INFO("success to get seg av info \n");
1842 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 +08001843 DVR_STREAM_TYPE_VIDEO,
1844 DVR_STREAM_TYPE_AUDIO,
1845 DVR_STREAM_TYPE_AD);
1846 has_av = 1;
1847 //break;
1848 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001849 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 +08001850 DVR_STREAM_TYPE_VIDEO,
1851 DVR_STREAM_TYPE_AUDIO,
1852 DVR_STREAM_TYPE_AD);
1853 }
1854 }
1855 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001856 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001857 continue;
1858 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001859 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001860 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001861 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1862 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001863 if (error == DVR_FAILURE) {
1864 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chen8aed9582021-12-24 17:59:56 +08001865 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001866 }
hualing chen8aed9582021-12-24 17:59:56 +08001867 }
hualing chenb9a02922021-12-14 11:29:47 +08001868 continue;
1869 }
1870
1871 //add check if has audio or video pid. if not exist. not add segment to playback
1872 int ii = 0;
1873 int has_av = 0;
Wentao MA4d85ff32022-09-23 11:36:18 +08001874 for (ii = 0; ii < p_seg_info->nb_pids; ii++) {
1875 int type = (p_seg_info->pids[ii].type >> 24) & 0x0f;
hualing chenb9a02922021-12-14 11:29:47 +08001876 if (type == DVR_STREAM_TYPE_VIDEO ||
1877 type == DVR_STREAM_TYPE_AUDIO ||
1878 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001879 DVR_WRAPPER_INFO("success to get seg av info \n");
Wentao MA4d85ff32022-09-23 11:36:18 +08001880 DVR_WRAPPER_INFO("success to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(p_seg_info->pids[ii].type >> 24)&0x0f,p_seg_info->pids[ii].pid,
hualing chenb9a02922021-12-14 11:29:47 +08001881 DVR_STREAM_TYPE_VIDEO,
1882 DVR_STREAM_TYPE_AUDIO,
1883 DVR_STREAM_TYPE_AD);
1884 has_av = 1;
1885 //break;
1886 } else {
Wentao MA4d85ff32022-09-23 11:36:18 +08001887 DVR_WRAPPER_INFO("error to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(p_seg_info->pids[ii].type >> 24)&0x0f,p_seg_info->pids[ii].pid,
hualing chenb9a02922021-12-14 11:29:47 +08001888 DVR_STREAM_TYPE_VIDEO,
1889 DVR_STREAM_TYPE_AUDIO,
1890 DVR_STREAM_TYPE_AD);
1891 }
1892 }
1893 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001894 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001895 continue;
1896 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001897 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001898 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001899 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1900 error = wrapper_addPlaybackSegment(ctx, p_seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001901 if (error == DVR_FAILURE) {
1902 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001903 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001904 }
hualing chenb9a02922021-12-14 11:29:47 +08001905
1906 /*copy the 1st segment*/
1907 if (got_1st_seg == 0) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001908 seg_info_1st = *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001909 got_1st_seg = 1;
1910 }
hualing chen92f3a142020-07-08 20:59:33 +08001911 }
hualing chenb9a02922021-12-14 11:29:47 +08001912 //free list
1913 DVR_RecordSegmentInfo_t *segment = NULL;
1914 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1915 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1916 {
1917 if (segment) {
1918 list_del(&segment->head);
1919 free(segment);
1920 }
1921 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001922 }
hualing chenb9a02922021-12-14 11:29:47 +08001923
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001924 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001925
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001926 /* return if no segment or fail to add */
1927 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001928
Wentao MA270dc0f2022-08-23 13:17:26 +08001929 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001930 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1931 ctx->playback.obsolete = ctx_record->record.obsolete;
1932 }
1933
Wentao MA96f68962022-06-15 19:45:35 +08001934 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001935
1936 ctx->playback.reach_end = DVR_FALSE;
1937 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1938 ctx->playback.speed = 0.0f;
1939 else
1940 ctx->playback.speed = 100.0f;
1941
1942 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001943 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001944 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001945 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001946 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001947 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001948 error = dvr_playback_start(ctx->playback.player, flags);
1949 } else {
wentao.maa210e5e2022-10-12 16:10:03 +08001950 dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
hualing chen03fd4942021-07-15 15:56:41 +08001951 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001952 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001953 ctx->sn, seg_info_1st.id, error);
1954 }
Wentao MA96f68962022-06-15 19:45:35 +08001955 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001956 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001957 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 +08001958 }
1959 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001960
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001961 if (ctx->playback.param_open.is_timeshift) {
1962 /*unlock the recorder locked above*/
1963 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001964 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001965 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001966 ctx->sn, ctx_record->sn);
1967 }
1968 }
Gong Kefdb31922022-06-17 17:11:16 +08001969 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001970
1971 return error;
1972}
hualing chen002e5b92022-02-23 17:51:21 +08001973//stop record and playback
1974int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1975{
1976 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1977 int error;
Wentao MA96f68962022-06-15 19:45:35 +08001978 DVR_WRAPPER_INFO("stop timeshift record\n");
hualing chen002e5b92022-02-23 17:51:21 +08001979
1980 //stop timeshift record
1981 ctx_record = ctx_getRecord(sn_timeshift_record);
wentao.maa210e5e2022-10-12 16:10:03 +08001982 dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
hualing chen002e5b92022-02-23 17:51:21 +08001983
Wentao MA96f68962022-06-15 19:45:35 +08001984 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08001985 //stop play
1986 error = dvr_wrapper_stop_playback(playback);
1987 //del timeshift file
1988 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08001989 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08001990 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
1991 }
1992 return error;
1993}
1994//start record and start playback
1995int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1996{
1997 DVR_WrapperCtx_t *ctx;
1998 DVR_RecordStartParams_t *start_param;
1999 int error;
2000
2001 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
2002 DVR_RETURN_IF_FALSE(ctx);
2003
Gong Kefdb31922022-06-17 17:11:16 +08002004 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002005 DVR_WRAPPER_INFO("restart record(sn:%ld, location:%s)...\n", ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08002006 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002007
hualing chen451c8f72022-03-09 13:05:52 +08002008 {
2009 //clear old record status
2010 // struct {
2011 // DVR_WrapperRecordOpenParams_t param_open;
2012 // DVR_RecordStartParams_t param_start;
2013 // DVR_RecordStartParams_t param_update;
2014 // DVR_RecordHandle_t recorder;
2015 // DVR_RecordEventFunction_t event_fn;
2016 // void *event_userdata;
2017
2018 // /*total status = seg_status + status + obsolete*/
2019 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
2020 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
2021 // uint64_t next_segment_id;
2022
2023 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
2024 // } record;
2025 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
2026 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
2027 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2028 }
2029
hualing chen002e5b92022-02-23 17:51:21 +08002030 start_param = &ctx->record.param_start;
2031
2032 error = dvr_record_start_segment(ctx->record.recorder, start_param);
2033 {
2034 DVR_RecordSegmentInfo_t new_seg_info =
2035 { .id = start_param->segment.segment_id, };
2036 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08002037 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 +08002038 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
2039 DVR_RecordStartParams_t *update_param;
2040 update_param = &ctx->record.param_update;
2041 memcpy(update_param, start_param, sizeof(*update_param));
2042 int i = 0;
2043 for (i = 0; i < update_param->segment.nb_pids; i++)
2044 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08002045 }
2046
Wentao MA96f68962022-06-15 19:45:35 +08002047 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08002048
Gong Kefdb31922022-06-17 17:11:16 +08002049 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002050
2051 //start play
Wentao MA96f68962022-06-15 19:45:35 +08002052 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08002053 //clear play statue
2054 ctx = ctx_getPlayback((unsigned long)playback);
2055 if (ctx) {
Wentao MA4d85ff32022-09-23 11:36:18 +08002056 //reset old playback status
hualing chen451c8f72022-03-09 13:05:52 +08002057 // struct {
2058 // DVR_WrapperPlaybackOpenParams_t param_open;
2059 // DVR_PlaybackHandle_t player;
2060 // DVR_PlaybackEventFunction_t event_fn;
2061 // void *event_userdata;
2062
2063 // /*total status = seg_status + status*/
2064 // DVR_PlaybackStatus_t seg_status;
2065 // DVR_WrapperPlaybackStatus_t status;
2066 // DVR_PlaybackPids_t pids_req;
2067 // DVR_PlaybackEvent_t last_event;
2068 // float speed;
2069 // DVR_Bool_t reach_end;
2070
2071 // DVR_WrapperInfo_t obsolete;
2072 // DVR_Bool_t tf_full;
2073 // } playback;
Wentao MA4d85ff32022-09-23 11:36:18 +08002074 ctx->playback.tf_full = DVR_FALSE;
2075 ctx->playback.reach_end = DVR_FALSE;
hualing chen451c8f72022-03-09 13:05:52 +08002076 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2077 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2078 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2079 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2080 }
hualing chen002e5b92022-02-23 17:51:21 +08002081 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2082 return error;
2083}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002084
2085int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2086{
2087 DVR_WrapperCtx_t *ctx;
2088 int error;
2089
2090 DVR_RETURN_IF_FALSE(playback);
2091
2092 ctx = ctx_getPlayback((unsigned long)playback);
2093 DVR_RETURN_IF_FALSE(ctx);
2094
Gong Kefdb31922022-06-17 17:11:16 +08002095 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002096 DVR_WRAPPER_INFO("stop playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002097 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002098
2099 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2100
2101 {
2102 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002103 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002104
wentao.mafd5283f2022-10-14 09:51:13 +08002105 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002106 // prefetch() here incurring self_assign is used to avoid some compiling
2107 // warnings.
2108 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002109 list_for_each_entry(p_seg, &ctx->segments, head) {
2110 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002111 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002112 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002113 }
2114 ctx_freeSegments(ctx);
2115 }
2116
Wentao MA96f68962022-06-15 19:45:35 +08002117 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002118 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002119
2120 return error;
2121}
2122
2123int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2124{
2125 DVR_WrapperCtx_t *ctx;
2126 int error;
2127
2128 DVR_RETURN_IF_FALSE(playback);
2129
2130 ctx = ctx_getPlayback((unsigned long)playback);
2131 DVR_RETURN_IF_FALSE(ctx);
2132
Gong Kefdb31922022-06-17 17:11:16 +08002133 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002134 DVR_WRAPPER_INFO("pause playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002135 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002136 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002137 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002138 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002139
2140 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2141
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002142 ctx->playback.speed = 0.0f;
2143
Wentao MA96f68962022-06-15 19:45:35 +08002144 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002145 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002146
2147 return error;
2148}
2149
2150int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2151{
2152 DVR_WrapperCtx_t *ctx;
2153 int error;
2154
2155 DVR_RETURN_IF_FALSE(playback);
2156
2157 ctx = ctx_getPlayback((unsigned long)playback);
2158 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002159 //if set limit.we need check if seek to valid data when resume
2160 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2161 if (dvr_playback_check_limit(ctx->playback.player)) {
2162 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2163 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002164 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002165 ctx->sn, time_offset, expired);
2166 time_offset = expired;
2167 dvr_wrapper_seek_playback(playback, time_offset);
2168 }
2169 }
Gong Kefdb31922022-06-17 17:11:16 +08002170 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002171 DVR_WRAPPER_INFO("resume playback(sn:%ld) ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002172 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002173
2174 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002175 ctx->playback.speed = 100.0f;
2176
Wentao MA96f68962022-06-15 19:45:35 +08002177 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002178 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002179
2180 return error;
2181}
2182
2183int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2184{
2185 DVR_WrapperCtx_t *ctx;
2186 int error;
2187 DVR_PlaybackSpeed_t dvr_speed = {
2188 .speed = { speed },
2189 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2190 };
2191
2192 DVR_RETURN_IF_FALSE(playback);
2193
2194 ctx = ctx_getPlayback((unsigned long)playback);
2195 DVR_RETURN_IF_FALSE(ctx);
2196
Gong Kefdb31922022-06-17 17:11:16 +08002197 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08002198 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 +08002199 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002200
2201 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2202
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002203 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2204 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2205 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002206 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002207 error = dvr_playback_resume(ctx->playback.player);
2208 } else if (ctx->playback.speed == 0.0f
2209 && speed != 0.0f
2210 && speed != 100.0f) {
2211 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002212 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002213 error = dvr_playback_resume(ctx->playback.player);
2214 }
2215
2216 ctx->playback.speed = speed;
2217
Wentao MA96f68962022-06-15 19:45:35 +08002218 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002219 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002220 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002221
2222 return error;
2223}
2224
hualing chen03fd4942021-07-15 15:56:41 +08002225int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2226{
2227 DVR_WrapperCtx_t *ctx;
2228 int error;
2229
2230 DVR_RETURN_IF_FALSE(playback);
2231
2232 ctx = ctx_getPlayback((unsigned long)playback);
2233 DVR_RETURN_IF_FALSE(ctx);
2234
Gong Kefdb31922022-06-17 17:11:16 +08002235 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002236
Wentao MAe8ba5172022-08-09 11:18:17 +08002237 DVR_WRAPPER_INFO("set_limit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002238 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002239
2240 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002241 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002242
Gong Kefdb31922022-06-17 17:11:16 +08002243 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002244
2245 return error;
2246}
2247
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002248int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2249{
2250 DVR_WrapperCtx_t *ctx;
2251 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002252 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002253 uint64_t segment_id;
2254 uint32_t off;
2255 uint64_t last_segment_id;
2256 uint32_t pre_off;
2257
2258 DVR_RETURN_IF_FALSE(playback);
2259
2260 ctx = ctx_getPlayback((unsigned long)playback);
2261 DVR_RETURN_IF_FALSE(ctx);
2262
Gong Kefdb31922022-06-17 17:11:16 +08002263 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002264
Wentao MA96f68962022-06-15 19:45:35 +08002265 DVR_WRAPPER_INFO("seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002266 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002267
2268 off = 0;
2269 segment_id = 0;
2270 pre_off = 0;
2271 last_segment_id = 0;
2272
hualing chen03fd4942021-07-15 15:56:41 +08002273 //if set limit info we need check ts data is
2274 //expired when seek
2275 if (dvr_playback_check_limit(ctx->playback.player)) {
2276 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2277 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002278 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002279 ctx->sn, time_offset, expired);
2280 time_offset = expired;
2281 }
2282 }
2283
wentao.mafd5283f2022-10-14 09:51:13 +08002284 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002285 // prefetch() here incurring self_assign is used to avoid some compiling
2286 // warnings.
2287 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002288 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2289 segment_id = p_seg->seg_info.id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002290
Wentao MA270dc0f2022-08-23 13:17:26 +08002291 if ((ctx->playback.obsolete.time + pre_off + p_seg->seg_info.duration) > time_offset)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002292 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002293
Wentao MA270dc0f2022-08-23 13:17:26 +08002294 last_segment_id = p_seg->seg_info.id;
2295 pre_off += p_seg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002296 }
2297
2298 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002299 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2300 off = time_offset;
2301 } else if (ctx->playback.obsolete.time >= time_offset) {
2302 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002303 } else {
hualing chenda76fc52020-05-28 14:56:42 +08002304 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002305 }
2306
Wentao MA96f68962022-06-15 19:45:35 +08002307 DVR_WRAPPER_INFO("seek playback(sn:%ld) (seg:%lld, off:%d)\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002308 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002309 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
Wentao MA96f68962022-06-15 19:45:35 +08002310 DVR_WRAPPER_INFO("playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002311
Gong Kefdb31922022-06-17 17:11:16 +08002312 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002313
2314 return error;
2315}
2316
2317int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2318{
2319 DVR_WrapperCtx_t *ctx;
2320 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002321 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002322
2323 DVR_RETURN_IF_FALSE(playback);
2324 DVR_RETURN_IF_FALSE(p_pids);
2325
2326 ctx = ctx_getPlayback((unsigned long)playback);
2327 DVR_RETURN_IF_FALSE(ctx);
2328
Gong Kefdb31922022-06-17 17:11:16 +08002329 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002330
Wentao MA96f68962022-06-15 19:45:35 +08002331 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002332 ctx->sn,
2333 p_pids->video.pid, p_pids->video.format,
2334 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002335 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002336
2337 ctx->playback.pids_req = *p_pids;
2338
2339 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002340 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002341 // prefetch() here incurring self_assign is used to avoid some compiling
2342 // warnings.
2343 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002344 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002345 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002346 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2347 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2348 /*check update for pids*/
2349 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2350 p_seg->playback_info.pids = *p_pids;
2351 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002352 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002353 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002354 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002355 /*do not break, let list updated*/
2356 }
2357 }
2358 }
2359 /*break;*/
2360 }
2361 }
2362
Wentao MA96f68962022-06-15 19:45:35 +08002363 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002364 ctx->sn,
2365 p_pids->video.pid, p_pids->video.format,
2366 p_pids->audio.pid, p_pids->audio.format,
2367 error);
2368
Gong Kefdb31922022-06-17 17:11:16 +08002369 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002370
2371 return error;
2372}
2373
hualing chena5f03222021-12-02 11:22:35 +08002374int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2375{
2376 DVR_WrapperCtx_t *ctx;
2377 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002378 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002379
2380 DVR_RETURN_IF_FALSE(playback);
2381 DVR_RETURN_IF_FALSE(p_pids);
2382
2383 ctx = ctx_getPlayback((unsigned long)playback);
2384 DVR_RETURN_IF_FALSE(ctx);
2385
Gong Kefdb31922022-06-17 17:11:16 +08002386 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002387
Wentao MA96f68962022-06-15 19:45:35 +08002388 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
hualing chena5f03222021-12-02 11:22:35 +08002389 ctx->sn,
2390 p_pids->video.pid, p_pids->video.format,
2391 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002392 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002393
2394 ctx->playback.pids_req = *p_pids;
2395
2396 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002397 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002398 // prefetch() here incurring self_assign is used to avoid some compiling
2399 // warnings.
2400 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002401 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002402 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002403 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2404 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2405 /*check update for pids*/
2406 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2407 p_seg->playback_info.pids = *p_pids;
2408 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002409 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002410 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002411 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002412 /*do not break, let list updated*/
2413 }
2414 }
2415 }
2416 /*break;*/
2417 }
2418 }
2419
Wentao MA96f68962022-06-15 19:45:35 +08002420 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002421 ctx->sn,
2422 p_pids->video.pid, p_pids->video.format,
2423 p_pids->audio.pid, p_pids->audio.format,
2424 error);
2425
Gong Kefdb31922022-06-17 17:11:16 +08002426 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002427
2428 return error;
2429}
2430
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002431int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2432{
2433 DVR_WrapperCtx_t *ctx;
2434 DVR_WrapperPlaybackStatus_t s;
2435 DVR_PlaybackStatus_t play_status;
2436 int error;
2437
2438 DVR_RETURN_IF_FALSE(playback);
2439 DVR_RETURN_IF_FALSE(status);
2440
2441 ctx = ctx_getPlayback((unsigned long)playback);
2442 DVR_RETURN_IF_FALSE(ctx);
2443
Gong Kefdb31922022-06-17 17:11:16 +08002444 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002445
Wentao MA96f68962022-06-15 19:45:35 +08002446 DVR_WRAPPER_INFO("get playback(sn:%ld) status ...\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002447 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002448
2449 error = dvr_playback_get_status(ctx->playback.player, &play_status);
Wentao MA96f68962022-06-15 19:45:35 +08002450 DVR_WRAPPER_INFO("playback(sn:%ld) get status (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002451
2452 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002453 error = process_generatePlaybackStatus(ctx, &s);
2454
hualing chenb5cd42e2020-04-15 17:03:34 +08002455 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2456 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002457 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002458 s.info_cur.time = s.info_full.time;
2459 }
Wentao MA270dc0f2022-08-23 13:17:26 +08002460 DVR_WRAPPER_INFO("playback(sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002461 ctx->sn,
2462 s.state,
2463 s.info_cur.time,
2464 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002465 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002466 error);
2467
2468 *status = s;
2469
Gong Kefdb31922022-06-17 17:11:16 +08002470 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002471
2472 return error;
2473}
2474
hualing chen266b9502020-04-04 17:39:39 +08002475int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2476{
2477 DVR_WrapperCtx_t *ctx;
2478 int error;
2479
2480 DVR_RETURN_IF_FALSE(playback);
2481 DVR_RETURN_IF_FALSE(p_secure_buf);
2482
2483 ctx = ctx_getPlayback((unsigned long)playback);
2484 DVR_RETURN_IF_FALSE(ctx);
2485
Gong Kefdb31922022-06-17 17:11:16 +08002486 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002487 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002488 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002489 return error;
2490}
2491
2492int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2493{
2494 DVR_WrapperCtx_t *ctx;
2495 int error;
2496
2497 DVR_RETURN_IF_FALSE(playback);
2498 DVR_RETURN_IF_FALSE(func);
2499
2500 ctx = ctx_getPlayback((unsigned long)playback);
2501 DVR_RETURN_IF_FALSE(ctx);
2502
Gong Kefdb31922022-06-17 17:11:16 +08002503 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002504 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002505 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002506 return error;
2507}
2508
Zhiqiang Han620b9252021-11-09 14:23:20 +08002509int dvr_wrapper_segment_del_by_location (const char *location)
2510{
2511 char fpath[DVR_MAX_LOCATION_SIZE];
2512
2513 DVR_RETURN_IF_FALSE(location);
2514
2515 /*del the stats file*/
2516 sprintf(fpath, "%s.stats", location);
2517 unlink(fpath);
2518
2519 return dvr_segment_del_by_location(location);
2520}
2521
2522int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2523{
2524 FILE *fp;
2525 char fpath[DVR_MAX_LOCATION_SIZE];
2526
2527 DVR_RETURN_IF_FALSE(location);
2528 DVR_RETURN_IF_FALSE(p_info);
2529
2530 if (p_info)
2531 memset(p_info, 0, sizeof(p_info[0]));
2532
2533 memset(fpath, 0, sizeof(fpath));
2534 sprintf(fpath, "%s.stats", location);
2535
2536 /*stats file exists*/
2537 if ((fp = fopen(fpath, "r"))) {
2538 char buf[256];
2539
2540 if (fgets(buf, sizeof(buf), fp) != NULL
2541 && (sscanf(buf, ":%llu:%lu:%u",
2542 &p_info->size,
2543 &p_info->time,
2544 &p_info->pkts) == 3)) {
2545 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002546 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 +08002547 return DVR_SUCCESS;
2548 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002549 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002550 }
2551
2552 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002553 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002554
2555 int error;
2556 uint32_t n_ids;
2557 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002558
hualing chen8aed9582021-12-24 17:59:56 +08002559 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002560
Zhiqiang Han620b9252021-11-09 14:23:20 +08002561 if (!error) {
2562 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002563 struct list_head info_list; /**< segment list head*/
2564 INIT_LIST_HEAD(&info_list);
2565
2566 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002567 error = dvr_segment_get_allInfo(location, &info_list);
2568 if (error == DVR_FAILURE) {
wentao.maa210e5e2022-10-12 16:10:03 +08002569 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2570 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
hualing chenb9a02922021-12-14 11:29:47 +08002571
2572 memset(&info, 0, sizeof(info));
2573 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002574 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002575 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002576
wentao.maf57dd232022-10-08 16:07:29 +08002577 // Tainted data issue originating from fgets seem false positive, so we
2578 // just suppress it here.
2579 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002580 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002581 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002582 if (!error) {
2583 p_info->size += info.size;
2584 p_info->time += info.duration;
2585 p_info->pkts += info.nb_packets;
2586 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002587 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002588 break;
2589 }
2590 }
2591 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002592 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
wentao.maf57dd232022-10-08 16:07:29 +08002593 // Tainted data issue originating from fgets seem false positive, so we
2594 // just suppress it here.
2595 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002596 for (i = 0; i < n_ids; i++) {
2597
2598 DVR_RecordSegmentInfo_t *seg_info;
2599 DVR_PlaybackSegmentFlag_t flags;
2600 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002601 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002602 // prefetch() here incurring self_assign is used to avoid some compiling
2603 // warnings.
2604 // coverity[self_assign]
hualing chenb9a02922021-12-14 11:29:47 +08002605 list_for_each_entry(seg_info, &info_list, head)
2606 {
hualing chen8aed9582021-12-24 17:59:56 +08002607 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002608 found = 1;
2609 break;
2610 }
2611 }
2612 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002613 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 +08002614 if (p_ids[i] == n_ids - 1) {
wentao.maa210e5e2022-10-12 16:10:03 +08002615 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2616 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
Wentao MA96f68962022-06-15 19:45:35 +08002617 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 +08002618 error = dvr_segment_get_info(location, p_ids[i], &info);
2619 if (!error) {
2620 p_info->size += info.size;
2621 p_info->time += info.duration;
2622 p_info->pkts += info.nb_packets;
2623 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002624 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002625 break;
2626 }
2627 }
hualing chenb9a02922021-12-14 11:29:47 +08002628 continue;
2629 }
2630
2631 if (!error) {
2632 p_info->size += seg_info->size;
2633 p_info->time += seg_info->duration;
2634 p_info->pkts += seg_info->nb_packets;
2635 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002636 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002637 break;
2638 }
2639 }
2640 //free list
2641 DVR_RecordSegmentInfo_t *segment = NULL;
2642 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2643 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2644 {
2645 if (segment) {
2646 list_del(&segment->head);
2647 free(segment);
2648 }
2649 }
2650 }
2651 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002652 } else {
2653 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002654 }
Wentao MA96f68962022-06-15 19:45:35 +08002655 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 +08002656
2657 return (error)? DVR_FAILURE : DVR_SUCCESS;
2658}
2659
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002660static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2661{
wentao.maa210e5e2022-10-12 16:10:03 +08002662 DVR_WrapperEventCtx_t evt = {
2663 .sn = (unsigned long)userdata,
2664 .type = W_REC,
2665 .record.event = event,
2666 .record.status = *(DVR_RecordStatus_t *)params
2667 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002668
2669 DVR_RETURN_IF_FALSE(userdata);
2670
Wentao MA96f68962022-06-15 19:45:35 +08002671 DVR_WRAPPER_INFO("evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002672 return ctx_addRecordEvent(&evt);
2673}
2674
2675static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2676{
wentao.maa210e5e2022-10-12 16:10:03 +08002677 DVR_WrapperEventCtx_t evt = {
2678 .sn = (unsigned long)userdata,
2679 .type = W_PLAYBACK,
2680 .playback.event = event,
2681 .playback.status = *(DVR_Play_Notify_t *)params
2682 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002683
2684 DVR_RETURN_IF_FALSE(userdata);
2685
2686 evt.sn = (unsigned long)userdata;
2687 evt.type = W_PLAYBACK;
2688 evt.playback.event = event;
2689 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA9a164002022-08-29 11:20:24 +08002690 DVR_WRAPPER_INFO("evt[sn:%ld, playback, evt:0x%x]\n", evt.sn, evt.playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002691 return ctx_addPlaybackEvent(&evt);
2692}
2693
2694static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2695{
Wentao MA270dc0f2022-08-23 13:17:26 +08002696 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 +08002697 ctx->sn,
2698 evt,
2699 status->info.time,
2700 status->info.size,
2701 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002702 status->info_obsolete.time,
2703 status->info_obsolete.size,
2704 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002705
2706 if (ctx->record.event_fn)
2707 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2708 return 0;
2709}
2710
Zhiqiang Han620b9252021-11-09 14:23:20 +08002711static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2712{
2713 FILE *fp;
2714 char fpath[DVR_MAX_LOCATION_SIZE];
2715
2716 DVR_RETURN_IF_FALSE(location);
2717 DVR_RETURN_IF_FALSE(p_status);
2718
2719 sprintf(fpath, "%s.stats", location);
2720
2721 /*stats file*/
2722 if ((fp = fopen(fpath, "w"))) {
2723 char buf[256];
2724 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2725 p_status->info.size - p_status->info_obsolete.size,
2726 p_status->info.time - p_status->info_obsolete.time,
2727 p_status->info.pkts - p_status->info_obsolete.pkts);
2728 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002729 fflush(fp);
2730 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002731 fclose(fp);
2732 return DVR_SUCCESS;
2733 }
2734
2735 return DVR_FAILURE;
2736}
2737
2738
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002739static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2740{
2741 DVR_RecordStartParams_t param;
2742 DVR_RecordSegmentInfo_t seg_info;
2743 int i;
2744 int error;
2745
2746 memcpy(&param, &ctx->record.param_update, sizeof(param));
2747 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2748 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2749 for (i = 0; i < param.segment.nb_pids; i++) {
2750 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2751 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2752 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2753 ctx->record.param_update.segment.nb_pids++;
2754 }
2755 }
2756 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2757 {
2758 DVR_RecordSegmentInfo_t new_seg_info =
2759 { .id = ctx->record.param_update.segment.segment_id, };
2760 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2761 wrapper_addRecordSegment(ctx, &new_seg_info);
2762 }
2763
Wentao MA96f68962022-06-15 19:45:35 +08002764 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002765 return error;
2766}
2767
Wentao MA270dc0f2022-08-23 13:17:26 +08002768static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002769{
Wentao MA270dc0f2022-08-23 13:17:26 +08002770 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002771}
2772
2773/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002774static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002775{
2776 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002777 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002778
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002779 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002780 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2781
2782 ctx->record.status.state = ctx->record.seg_status.state;
2783 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2784 memcpy(ctx->record.status.pids.pids,
2785 ctx->record.seg_status.info.pids,
2786 sizeof(ctx->record.status.pids.pids));
2787 ctx->current_segment_id = ctx->record.seg_status.info.id;
2788
wentao.mafd5283f2022-10-14 09:51:13 +08002789 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002790 // prefetch() here incurring self_assign is used to avoid some compiling
2791 // warnings.
2792 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002793 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2794 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2795 ctx->record.status.info.time += p_seg->info.duration;
2796 ctx->record.status.info.size += p_seg->info.size;
2797 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002798 }
2799 }
2800
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002801 ctx->record.status.info_obsolete = ctx->record.obsolete;
2802
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002803 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002804
2805 if (status) {
2806 *status = ctx->record.status;
2807 status->info.time += ctx->record.seg_status.info.duration;
2808 status->info.size += ctx->record.seg_status.info.size;
2809 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2810 }
2811
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002812 return DVR_SUCCESS;
2813}
2814
2815
2816static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2817{
2818 DVR_WrapperRecordStatus_t status;
2819
2820 memset(&status, 0, sizeof(status));
2821
Wentao MA96f68962022-06-15 19:45:35 +08002822 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002823 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002824 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002825 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002826 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002827 return 0;
2828 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002829 switch (evt->record.event)
2830 {
2831 case DVR_RECORD_EVENT_STATUS:
2832 {
2833 switch (evt->record.status.state)
2834 {
2835 case DVR_RECORD_STATE_OPENED:
2836 case DVR_RECORD_STATE_CLOSED:
2837 {
2838 ctx->record.seg_status = evt->record.status;
2839
2840 status.state = evt->record.status.state;
2841 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002842 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002843 } break;
2844 case DVR_RECORD_STATE_STARTED:
2845 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002846 ctx->record.seg_status = evt->record.status;
2847
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002848 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002849 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002850 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002851
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002852 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002853 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002854 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002855 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 +08002856 ctx->sn,
2857 evt->record.status.info.size,
2858 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002859 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2860 /*should notify the recording's stop*/
2861 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002862 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002863 ctx->sn, error);
2864 status.state = DVR_RECORD_STATE_CLOSED;
2865 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002866 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002867 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002868 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002869
2870 if (ctx->record.param_open.is_timeshift
2871 && ctx->record.param_open.max_time
2872 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002873 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002874
2875 /*as the player do not support null playlist,
2876 there must be one segment existed at any time,
2877 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002878 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2879 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002880 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002881 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 +08002882 status.info.size,
2883 ctx->record.param_open.max_time,
2884 ctx->record.param_open.segment_size);
2885 } else {
2886 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002887 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002888
2889 process_generateRecordStatus(ctx, &status);
2890 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002891 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002892 }
2893 }
2894
Wentao MAf4072032022-06-30 13:50:45 +08002895 const loff_t actual_size = status.info.size;
2896 const loff_t max_size = ctx->record.param_open.max_size;
2897 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002898
Wentao MAf4072032022-06-30 13:50:45 +08002899 if (ctx->record.param_open.is_timeshift && max_size) {
2900 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002901 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002902 /*as the player do not support null playlist,
2903 there must be one segment existed at any time,
2904 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002905 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2906 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002907 /*only one segment, waiting for more*/
2908 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2909 actual_size, segment_size);
2910 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002911 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002912
2913 process_generateRecordStatus(ctx, &status);
2914 process_notifyRecord(ctx, evt->record.event, &status);
2915 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2916 }
2917 if (actual_size >= max_size + segment_size/2) {
2918 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2919 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002920 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002921 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002922 }
2923 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002924 } break;
2925 case DVR_RECORD_STATE_STOPPED:
2926 {
2927 ctx->record.seg_status = evt->record.status;
2928
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002929 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002930 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002931 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002932 } break;
2933 default:
2934 break;
2935 }
2936 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002937 case DVR_RECORD_EVENT_WRITE_ERROR: {
2938 ctx->record.seg_status = evt->record.status;
2939 status.state = evt->record.status.state;
2940 process_notifyRecord(ctx, evt->record.event, &status);
2941 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002942 default:
2943 break;
2944 }
2945 return DVR_SUCCESS;
2946}
2947
2948static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2949{
Wentao MA80179512022-11-03 12:20:03 +08002950 DVR_RETURN_IF_FALSE(ctx->playback.event_fn != NULL);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002951
Wentao MA80179512022-11-03 12:20:03 +08002952 const time_t cur_time = status->info_cur.time;
2953 const time_t full_time = status->info_full.time;
2954 const time_t obsolete_time = status->info_obsolete.time;
2955 const time_t origin_offset = cur_time + obsolete_time;
2956 DVR_WRAPPER_INFO("playback progress notify(sn:%ld) evt(0x%x)"
2957 " actual_slider_pos: %02d:%02d:%02d.%03d/%02d:%02d:%02d.%03d (%7ld ms/%7ld ms),"
2958 " offset_from_origin: %02d:%02d:%02d.%03d (%7ld ms),"
2959 " dump status:state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2960 ctx->sn,evt,cur_time/1000/3600,cur_time/1000%3600/60,cur_time/1000%60,cur_time%1000,
2961 full_time/1000/3600,full_time/1000%3600/60,full_time/1000%60,full_time%1000,
2962 cur_time,full_time,
2963 origin_offset/1000/3600,origin_offset/1000%3600/60,origin_offset/1000%60,origin_offset%1000,
2964 origin_offset,status->state,cur_time,full_time,obsolete_time);
2965
2966 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002967}
2968
2969/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002970static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002971{
2972 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002973 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002974
2975 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2976 ctx->playback.status.pids = ctx->playback.pids_req;
2977
2978 ctx->playback.status.state = ctx->playback.seg_status.state;
2979 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2980 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2981 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2982
wentao.mafd5283f2022-10-14 09:51:13 +08002983 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002984 // prefetch() here incurring self_assign is used to avoid some compiling
2985 // warnings.
2986 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002987 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2988 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
2989 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 +08002990 break;
hualing chen451c8f72022-03-09 13:05:52 +08002991 }
2992
Wentao MA270dc0f2022-08-23 13:17:26 +08002993 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
2994 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
2995 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002996 }
wentao.mafd5283f2022-10-14 09:51:13 +08002997 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002998 // prefetch() here incurring self_assign is used to avoid some compiling
2999 // warnings.
3000 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003001 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3002 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
3003 ctx->playback.status.info_full.size += p_seg->seg_info.size;
3004 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
3005 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 +08003006 }
3007
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003008 if (status) {
3009 *status = ctx->playback.status;
3010 /*deal with current, lack size and pkts with the current*/
3011 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08003012 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08003013 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08003014
Wentao MA270dc0f2022-08-23 13:17:26 +08003015 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
3016 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08003017 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
3018 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08003019 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 +08003020 }
3021 else
3022 {
Wentao MA270dc0f2022-08-23 13:17:26 +08003023 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 +08003024 status->info_obsolete.time = ctx->playback.obsolete.time;
3025 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
3026 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003027 }
3028
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003029 return DVR_SUCCESS;
3030}
3031
3032static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3033{
Wentao MA96f68962022-06-15 19:45:35 +08003034 DVR_WRAPPER_INFO("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003035 evt->sn, evt->playback.event,
3036 evt->playback.status.play_status.state,
3037 evt->playback.status.play_status.segment_id,
3038 evt->playback.status.play_status.time_cur,
3039 evt->playback.status.play_status.time_end);
3040
3041 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08003042 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
3043 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
3044 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
3045 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003046 ctx->playback.last_event = evt->playback.event;
3047
3048 switch (evt->playback.event)
3049 {
3050 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
3051 case DVR_PLAYBACK_EVENT_REACHED_END:
3052 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
3053 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08003054 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08003055 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08003056 case DVR_PLAYBACK_EVENT_NODATA:
3057 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003058 {
3059 DVR_WrapperPlaybackStatus_t status;
3060
3061 /*copy status of segment*/
3062 ctx->playback.seg_status = evt->playback.status.play_status;
3063
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003064 /*generate status of the whole playback*/
3065 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003066
3067 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08003068 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08003069 if (ctx->playback.param_open.is_timeshift
3070 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003071 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08003072 }
3073 /*trust the low level, make NO check.
3074 As this evt is changed to only once due to some operations(paused) in low level.
3075 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003076 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08003077 }
3078 */
3079 else {
3080 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08003081 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003082 }
hualing chenf291cf32020-06-18 10:50:30 +08003083 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003084 process_notifyPlayback(ctx, evt->playback.event, &status);
3085 }
3086 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003087 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
3088 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
3089 case DVR_PLAYBACK_EVENT_NO_KEY:
3090 {
Wentao MA96f68962022-06-15 19:45:35 +08003091 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003092 } break;
3093 default:
3094 {
Wentao MA96f68962022-06-15 19:45:35 +08003095 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003096 } break;
3097 }
3098 return 0;
3099}
3100
3101static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3102{
3103 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
3104}
3105
Wentao MA96f68962022-06-15 19:45:35 +08003106int dvr_wrapper_set_log_level (int level)
3107{
3108 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3109 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3110 return DVR_FAILURE;
3111 }
3112 g_dvr_log_level = level;
3113 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3114 return DVR_SUCCESS;
3115}
3116
Wentao MA5629ad82022-08-24 10:03:02 +08003117int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3118{
3119 DVR_WrapperCtx_t *ctx;
3120 int error;
3121
3122 DVR_RETURN_IF_FALSE(playback==NULL);
3123
3124 ctx = ctx_getPlayback((unsigned long)playback);
3125 DVR_RETURN_IF_FALSE(ctx);
3126
3127 wrapper_mutex_lock(&ctx->wrapper_lock);
3128
3129 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3130
3131 DVR_WRAPPER_INFO("set ac4 preselection id: %d", presel_id);
3132 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3133
3134 wrapper_mutex_unlock(&ctx->wrapper_lock);
3135
3136 return error;
3137}
3138
wentao ma7d642782022-10-23 18:26:16 -07003139int dvr_wrapper_property_set(const char* prop_name, const char* prop_value)
3140{
3141 return dvr_prop_write(prop_name,prop_value);
3142}
3143
3144int dvr_wrapper_property_get(const char* prop_name, char* prop_value, int length)
3145{
3146 return dvr_prop_read(prop_name,prop_value,length);
3147}
3148