blob: 0bb6bc34b458eea54e6dd98f081d89c665c20b3f [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>
Wentao MA361eaac2023-03-21 13:12:28 +08009#include <sys/prctl.h>
Zhiqiang Han18f42c82021-08-11 17:13:28 +080010#include <time.h>
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080011
12#include "dvr_types.h"
13#include "dvr_record.h"
14#include "dvr_crypto.h"
15#include "dvr_playback.h"
16#include "dvr_segment.h"
wentao.ma9009aaa2022-10-25 14:26:33 +080017#include "dvr_utils.h"
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080018
19#include "AmTsPlayer.h"
20
21#include "list.h"
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080022
23#include "dvr_wrapper.h"
24
Wentao MA96f68962022-06-15 19:45:35 +080025#define WRAPPER_LOG_TAG "libdvr-wrapper"
26#define DVR_WRAPPER_DEBUG(...) DVR_LOG_PRINT(LOG_LV_DEBUG, WRAPPER_LOG_TAG, __VA_ARGS__)
27#define DVR_WRAPPER_INFO(...) DVR_LOG_PRINT(LOG_LV_INFO, WRAPPER_LOG_TAG, __VA_ARGS__)
28#define DVR_WRAPPER_WARN(...) DVR_LOG_PRINT(LOG_LV_WARN, WRAPPER_LOG_TAG, __VA_ARGS__)
29#define DVR_WRAPPER_ERROR(...) DVR_LOG_PRINT(LOG_LV_ERROR, WRAPPER_LOG_TAG, __VA_ARGS__)
30#define DVR_WRAPPER_FATAL(...) DVR_LOG_PRINT(LOG_LV_FATAL, WRAPPER_LOG_TAG, __VA_ARGS__)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080031
32/*duration of data to resume if paused with EVT_REACHED_END in timeshifting*/
hualing chen2932d372020-04-29 13:44:00 +080033#define TIMESHIFT_DATA_DURATION_TO_RESUME (600)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080034/*a tolerant gap*/
35#define DVR_PLAYBACK_END_GAP (1000)
36
Wentao MA96f68962022-06-15 19:45:35 +080037int g_dvr_log_level = LOG_LV_DEFAULT;
38
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080039enum {
40 W_REC = 1,
41 W_PLAYBACK = 2,
42};
43
44enum {
45 U_PIDS = 0x01,
46 U_STAT = 0x02,
47 U_ALL = U_PIDS | U_STAT,
48};
49
50typedef struct {
Gong Kefdb31922022-06-17 17:11:16 +080051 pthread_mutex_t lock;
52 pthread_cond_t cond;
53 int inited;
54 int locked;
55} DVR_WrapperMutex_t;
56
57static void
58wrapper_mutex_init (DVR_WrapperMutex_t *lock)
59{
60 pthread_condattr_t cattr;
61
Zhiqiang Han2259da32022-07-07 15:52:58 +080062 if (lock->inited)
Gong Kefdb31922022-06-17 17:11:16 +080063 return;
64
65 pthread_mutex_init(&lock->lock, NULL);
66
67 pthread_condattr_init(&cattr);
68 pthread_condattr_setclock(&cattr, CLOCK_MONOTONIC);
69 pthread_cond_init(&lock->cond, &cattr);
70 pthread_condattr_destroy(&cattr);
71
wentao.maa22bc852022-10-13 12:18:06 +080072 // It is not necessary to protect code block below with
73 // DVR_WrapperMutex_t.lock, so the following annotation
wentao.mafd5283f2022-10-14 09:51:13 +080074 // is given to suppress related Coverity complaint.
wentao.maa22bc852022-10-13 12:18:06 +080075 // coverity[missing_lock]
Gong Kefdb31922022-06-17 17:11:16 +080076 lock->locked = 0;
77 lock->inited = 1;
78}
79
80static int
81wrapper_mutex_lock (DVR_WrapperMutex_t *lock)
82{
83 pthread_mutex_lock(&lock->lock);
wentao.maa22bc852022-10-13 12:18:06 +080084 // This couldn't be a infinite loop as Coverity reported.
85 // Loop can finish when another thread calls wrapper_mutex_unlock.
86 // coverity[loop_condition]
Gong Kefdb31922022-06-17 17:11:16 +080087 while (lock->locked) {
88 pthread_cond_wait(&lock->cond, &lock->lock);
89 }
90 lock->locked = 1;
91 pthread_mutex_unlock(&lock->lock);
92
93 return 0;
94}
95
96static int
97wrapper_mutex_unlock (DVR_WrapperMutex_t *lock)
98{
99 pthread_mutex_lock(&lock->lock);
100 lock->locked = 0;
101 pthread_mutex_unlock(&lock->lock);
102 pthread_cond_signal(&lock->cond);
103
104 return 0;
105}
106
107static int
108wrapper_mutex_timedlock (DVR_WrapperMutex_t *lock, struct timespec *tv)
109{
110 int r = 0;
111
112 pthread_mutex_lock(&lock->lock);
113 if (lock->locked) {
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800114 //DVR_WRAPPER_DEBUG("Enter cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800115 r = pthread_cond_timedwait(&lock->cond, &lock->lock, tv);
Zhiqiang Han61ceb3a2022-07-04 17:03:52 +0800116 //DVR_WRAPPER_DEBUG("Leave cond_timedwait");
Gong Kefdb31922022-06-17 17:11:16 +0800117 }
118 if (r == 0) {
119 if (!lock->locked) {
120 lock->locked = 1;
121 } else {
122 r = ETIMEDOUT;
123 }
124 }
125 pthread_mutex_unlock(&lock->lock);
126
127 return r;
128}
129
130#define WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(expr, lock)\
131 do {\
132 if (!(expr)) {\
133 DVR_INFO("%s-%d failed", __func__, __LINE__);\
134 wrapper_mutex_unlock(lock);\
135 return DVR_FAILURE;\
136 }\
137 } while (0);
138
139
140typedef struct {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800141 /*make lock the 1st item in the structure*/
Gong Kefdb31922022-06-17 17:11:16 +0800142 DVR_WrapperMutex_t wrapper_lock;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800143
144 /*rec or play*/
145 int type;
146
147 /*valid if (sn != 0)*/
148 unsigned long sn;
149 unsigned long sn_linked;
150
151 struct list_head segments; /**<head-add list*/
152 uint64_t current_segment_id; /**<id of the current segment*/
153
154 union {
155 struct {
156 DVR_WrapperRecordOpenParams_t param_open;
157 DVR_RecordStartParams_t param_start;
158 DVR_RecordStartParams_t param_update;
159 DVR_RecordHandle_t recorder;
160 DVR_RecordEventFunction_t event_fn;
161 void *event_userdata;
162
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800163 /*total status = seg_status + status + obsolete*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800164 DVR_RecordStatus_t seg_status; /**<status of current segment*/
165 DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
166 uint64_t next_segment_id;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800167
168 DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800169 } record;
170
171 struct {
172 DVR_WrapperPlaybackOpenParams_t param_open;
173 DVR_PlaybackHandle_t player;
174 DVR_PlaybackEventFunction_t event_fn;
175 void *event_userdata;
176
177 /*total status = seg_status + status*/
178 DVR_PlaybackStatus_t seg_status;
179 DVR_WrapperPlaybackStatus_t status;
180 DVR_PlaybackPids_t pids_req;
181 DVR_PlaybackEvent_t last_event;
Zhiqiang Han3eb75f92020-04-08 10:07:55 +0800182 float speed;
hualing chenb5cd42e2020-04-15 17:03:34 +0800183 DVR_Bool_t reach_end;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800184
185 DVR_WrapperInfo_t obsolete;
hualing chen56c0a162022-01-27 17:01:50 +0800186 DVR_Bool_t tf_full;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800187 } playback;
188 };
189} DVR_WrapperCtx_t;
190
191typedef struct {
192 struct list_head head;
193 unsigned long sn;
194
195 /* rec or playback */
196 int type;
197
198 union {
199 struct {
200 DVR_RecordEvent_t event;
201 DVR_RecordStatus_t status;
202 } record;
203 struct {
204 DVR_PlaybackEvent_t event;
205 DVR_Play_Notify_t status;
206 } playback;
207 };
208} DVR_WrapperEventCtx_t;
209
210typedef struct {
211 pthread_mutex_t lock;
212 char *name;
213 int running;
214 pthread_cond_t cond;
215 pthread_t thread;
216 int type;
217} DVR_WrapperThreadCtx_t;
218
219typedef struct {
220 struct list_head head;
221
222 DVR_RecordSegmentInfo_t seg_info;
223 DVR_PlaybackSegmentInfo_t playback_info;
224} DVR_WrapperPlaybackSegmentInfo_t;
225
226typedef struct {
227 struct list_head head;
228
229 DVR_RecordSegmentInfo_t info;
230} DVR_WrapperRecordSegmentInfo_t;
231
232/* serial num generater */
233static unsigned long sn = 1;
234static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER;
235
236static inline unsigned long get_sn()
237{
hualing chenab0d1262021-09-26 15:22:50 +0800238 unsigned long no = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800239
240 pthread_mutex_lock(&sn_lock);
241 no = sn++;
242 if (!no)
243 no = sn++;
244 pthread_mutex_unlock(&sn_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800245 return no;
246}
247
248/* entity ctx */
249#define DVR_WRAPPER_MAX 10
250
251static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] =
252{
253 [0 ... (DVR_WRAPPER_MAX - 1)] =
254 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800255 .type = W_REC,
256 }
257};
258
259static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] =
260{
261 [0 ... (DVR_WRAPPER_MAX - 1)] =
262 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800263 .type = W_PLAYBACK,
264 }
265};
266
267/* events lists */
268static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list);
269static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list);
270
271static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
272static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
273
274static DVR_WrapperThreadCtx_t wrapper_thread[2] =
275{
276 [0] =
277 {
278 .lock = PTHREAD_MUTEX_INITIALIZER,
279 .running = 0,
280 .name = "record",
281 .type = W_REC,
282 },
283 [1] =
284 {
285 .lock = PTHREAD_MUTEX_INITIALIZER,
286 .running = 0,
287 .name = "playback",
288 .type = W_PLAYBACK,
289 },
290};
291
292/*now only support one timeshift now*/
293static unsigned long sn_timeshift_record;
294static unsigned long sn_timeshift_playback;
295
296static void *wrapper_task(void *arg);
297static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx);
298
299static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata);
300static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata);
301
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800302static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status);
303static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800304
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800305static int get_timespec_timeout(int timeout, struct timespec *ts)
306{
307 struct timespec ots;
308 int left, diff;
309
310 clock_gettime(CLOCK_MONOTONIC, &ots);
311
312 ts->tv_sec = ots.tv_sec + timeout / 1000;
313 ts->tv_nsec = ots.tv_nsec;
314
315 left = timeout % 1000;
316 left *= 1000000;
317 diff = 1000000000 - ots.tv_nsec;
318
319 if (diff <= left) {
320 ts->tv_sec++;
321 ts->tv_nsec = left-diff;
322 } else {
323 ts->tv_nsec += left;
324 }
325
326 return 0;
327}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800328
329static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock)
330{
Wentao MA270dc0f2022-08-23 13:17:26 +0800331 DVR_WrapperEventCtx_t *p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800332
333 pthread_mutex_lock(list_lock);
334 if (list_empty(list))
Wentao MA270dc0f2022-08-23 13:17:26 +0800335 p_evt = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800336 else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800337 p_evt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
338 list_del(&p_evt->head);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800339 }
340 pthread_mutex_unlock(list_lock);
341
Wentao MA270dc0f2022-08-23 13:17:26 +0800342 return p_evt;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800343}
344
345static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent()
346{
347 return ctx_getEvent(&record_evt_list, &record_evt_list_lock);
348}
349
350static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent()
351{
352 return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock);
353}
354
355static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt)
356{
357 DVR_WrapperEventCtx_t *padd;
358 padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t));
359 DVR_RETURN_IF_FALSE(padd);
360
361 *padd = *evt;
362 pthread_mutex_lock(lock);
wentao.maa22bc852022-10-13 12:18:06 +0800363 list_add_tail(padd, list);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800364 pthread_mutex_unlock(lock);
365 return DVR_SUCCESS;
366}
367
368static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt)
369{
370 free(evt);
371}
372
373/*useless*/
374static void ctx_cleanOutdatedEvents(struct list_head *evt_list,
375 pthread_mutex_t *evt_list_lock,
376 DVR_WrapperCtx_t *list)
377{
Wentao MA270dc0f2022-08-23 13:17:26 +0800378 DVR_WrapperEventCtx_t *p_evt, *p_evt_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800379 unsigned long sns[DVR_WRAPPER_MAX];
380 int cnt = 0;
381 int i;
382 int found = 0;
383
384 /*copy all valid sns*/
385 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
386 sns[cnt] = list[i].sn;
387 if (!sns[cnt])
388 cnt++;
389 }
390
391 /*free evts that not belong to any valid sns*/
392 pthread_mutex_lock(evt_list_lock);
Wentao MA270dc0f2022-08-23 13:17:26 +0800393 list_for_each_entry_safe(p_evt, p_evt_tmp, evt_list, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800394 for (i = 0; i < cnt; i++) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800395 if (p_evt->sn == sns[i]) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800396 found = 1;
397 break;
398 }
399 }
400 if (!found) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800401 list_del(&p_evt->head);
402 ctx_freeEvent(p_evt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800403 }
404 }
405 pthread_mutex_unlock(evt_list_lock);
406}
407
408static inline void ctx_cleanOutdatedRecordEvents()
409{
410 ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list);
411}
412
413static inline void ctx_cleanOutdatedPlaybackEvents()
414{
415 ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list);
416}
417
hualing chenb9b358a2021-08-17 15:06:36 +0800418//check this play is recording file
419//return 0 if not the recording
420//else return record id
421static inline int ctx_isPlay_recording(char *play_location)
422{
423 int i;
424 DVR_WrapperCtx_t *cnt;
425
426 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
427 cnt = &record_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800428 //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 +0800429 if (!strcmp(cnt->record.param_open.location, play_location)) {
Wentao MA96f68962022-06-15 19:45:35 +0800430 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 +0800431 return cnt->sn;
432 }
433 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800434 DVR_WRAPPER_INFO(" not found any play is in recording [%d]", DVR_WRAPPER_MAX);
hualing chenb9b358a2021-08-17 15:06:36 +0800435 return 0;
436}
Wentao MA804bab12022-11-29 10:01:26 +0800437
438// Check if the given record is being played.
439// Return 0 if it is not being played, otherwise return its playback id.
hualing chenb9b358a2021-08-17 15:06:36 +0800440static inline int ctx_isRecord_playing(char *rec_location)
441{
442 int i;
443 DVR_WrapperCtx_t *cnt;
444 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
445 cnt = &playback_list[i];
Wentao MA96f68962022-06-15 19:45:35 +0800446 //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 +0800447 if (!strcmp(cnt->playback.param_open.location, rec_location)) {
Wentao MA804bab12022-11-29 10:01:26 +0800448 DVR_WRAPPER_DEBUG("[%d]sn[%d]P[%s]R[%s] ..found.",
449 i, cnt->sn, cnt->playback.param_open.location, rec_location);
hualing chenb9b358a2021-08-17 15:06:36 +0800450 return cnt->sn;
451 }
452 }
hualing chenb9b358a2021-08-17 15:06:36 +0800453 return 0;
454}
455
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800456static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list)
457{
458 int i;
459 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
Gong Kefdb31922022-06-17 17:11:16 +0800460 if (list[i].sn == sn) {
461 wrapper_mutex_init(&list[i].wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800462 return &list[i];
Gong Kefdb31922022-06-17 17:11:16 +0800463 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800464 }
465 return NULL;
466}
467
468static inline void ctx_reset(DVR_WrapperCtx_t *ctx)
469{
470 memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn),
471 0,
472 sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn));
473}
474
475static inline int ctx_valid(DVR_WrapperCtx_t *ctx)
476{
477 return (ctx->sn != 0);
478}
479
480static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn)
481{
482 return ctx_get(sn, record_list);
483}
484
485static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn)
486{
487 return ctx_get(sn, playback_list);
488}
489
490static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *))
491{
492 pthread_mutex_lock(&ctx->lock);
493 if (ctx->running == 0) {
wentao.maa210e5e2022-10-12 16:10:03 +0800494 pthread_condattr_t attr = {0};
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800495 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
496 pthread_cond_init(&ctx->cond, &attr);
497 pthread_condattr_destroy(&attr);
Wentao MA96f68962022-06-15 19:45:35 +0800498 DVR_WRAPPER_INFO("start wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800499 pthread_create(&ctx->thread, NULL, thread_fn, ctx);
Wentao MA96f68962022-06-15 19:45:35 +0800500 DVR_WRAPPER_INFO("wrapper thread(%s) started\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800501 }
502 ctx->running++;
503 pthread_mutex_unlock(&ctx->lock);
504 return 0;
505}
506
507static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx)
508{
509 pthread_mutex_lock(&ctx->lock);
510 ctx->running--;
511 if (!ctx->running) {
512 pthread_cond_broadcast(&ctx->cond);
513 pthread_mutex_unlock(&ctx->lock);
514
Wentao MA96f68962022-06-15 19:45:35 +0800515 DVR_WRAPPER_INFO("stop wrapper thread(%s) ...\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800516 pthread_join(ctx->thread, NULL);
Wentao MA96f68962022-06-15 19:45:35 +0800517 DVR_WRAPPER_INFO("wrapper thread(%s) stopped\n", ctx->name);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800518
519 pthread_mutex_lock(&ctx->lock);
520 if (!ctx->running) /*protect*/
521 pthread_cond_destroy(&ctx->cond);
522 }
523 pthread_mutex_unlock(&ctx->lock);
524 return 0;
525}
526
527#define WRAPPER_THREAD_RECORD (&wrapper_thread[0])
528#define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1])
529
530static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx)
531{
532 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
533 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
534 return wrapper_requestThread(thread_ctx, wrapper_task);
535}
536
537static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx)
538{
539 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
540 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
541 return wrapper_releaseThread(thread_ctx);
542}
543
544static inline int wrapper_releaseThreadForType(int type)
545{
546 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)?
547 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
548 return wrapper_releaseThread(thread_ctx);
549}
550
551static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx)
552{
553 pthread_cond_signal(&thread_ctx->cond);
554}
555
556static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx)
557{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800558 struct timespec rt;
559 get_timespec_timeout(200, &rt);
560 pthread_cond_timedwait(&thread_ctx->cond, &thread_ctx->lock, &rt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800561 return 0;
562}
563
564static inline void wrapper_threadSignalForType(int type)
565{
566 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ?
567 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
568 wrapper_threadSignal(thread_ctx);
569}
570
571static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx)
572{
573 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
574 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
575 wrapper_threadSignal(thread_ctx);
576}
577
578static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx)
579{
580 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
581 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
582 wrapper_threadWait(thread_ctx);
583 return 0;
584}
585
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800586/*return condition, locked if condition == true*/
Gong Kefdb31922022-06-17 17:11:16 +0800587static int wrapper_mutex_lock_if(DVR_WrapperMutex_t *lock, int *condition)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800588{
589 int r2;
590 do {
591 struct timespec rt2;
Gong Kefdb31922022-06-17 17:11:16 +0800592 get_timespec_timeout(10, &rt2);
593 r2 = wrapper_mutex_timedlock(lock, &rt2);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800594 } while (*condition && (r2 == ETIMEDOUT));
595
596 if (!(*condition) && (r2 == 0))
Gong Kefdb31922022-06-17 17:11:16 +0800597 wrapper_mutex_unlock(lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800598
599 return *condition;
600}
601
602static void *wrapper_task(void *arg)
603{
Wentao MA270dc0f2022-08-23 13:17:26 +0800604 DVR_WrapperThreadCtx_t *thread_ctx = (DVR_WrapperThreadCtx_t *)arg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800605 DVR_WrapperEventCtx_t *evt;
606
Wentao MA361eaac2023-03-21 13:12:28 +0800607 prctl(PR_SET_NAME,"DvrWrapper");
608
Wentao MA270dc0f2022-08-23 13:17:26 +0800609 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800610
Wentao MA270dc0f2022-08-23 13:17:26 +0800611 while (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800612 {
613 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800614
Wentao MA270dc0f2022-08-23 13:17:26 +0800615 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800616 if (!evt)
Wentao MA270dc0f2022-08-23 13:17:26 +0800617 ret = wrapper_threadWait(thread_ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800618 }
619
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800620 while (evt) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800621 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
622 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chenbc0aec92021-03-18 14:52:40 +0800623 if (ctx == NULL) {
Wentao MA804bab12022-11-29 10:01:26 +0800624 DVR_WRAPPER_ERROR("Wrapper context is NULL");
hualing chenbc0aec92021-03-18 14:52:40 +0800625 goto processed;
626 }
Wentao MA804bab12022-11-29 10:01:26 +0800627 DVR_WRAPPER_DEBUG("start name(%s) sn(%d) running(%d) type(%d)\n", thread_ctx->name, (int)ctx->sn, thread_ctx->running, thread_ctx->type);
Wentao MA270dc0f2022-08-23 13:17:26 +0800628 if (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800629 /*
630 continue not break,
631 make all events consumed, or mem leak
632 */
Wentao MA270dc0f2022-08-23 13:17:26 +0800633 if (!wrapper_mutex_lock_if(&ctx->wrapper_lock, &thread_ctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800634 goto processed;
635
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800636 if (ctx_valid(ctx)) {
637 /*double check after lock*/
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800638 if (evt->sn == ctx->sn) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800639 pthread_mutex_unlock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800640 process_handleEvents(evt, ctx);
Wentao MA270dc0f2022-08-23 13:17:26 +0800641 pthread_mutex_lock(&thread_ctx->lock);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800642 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800643 }
Gong Kefdb31922022-06-17 17:11:16 +0800644 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800645 }
646
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800647processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800648 ctx_freeEvent(evt);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800649
Wentao MA270dc0f2022-08-23 13:17:26 +0800650 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800651 }
652 }
653
Wentao MA270dc0f2022-08-23 13:17:26 +0800654 pthread_mutex_unlock(&thread_ctx->lock);
Wentao MA804bab12022-11-29 10:01:26 +0800655 DVR_WRAPPER_DEBUG("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 +0800656 return NULL;
657}
658
659static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
660{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800661 pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800662 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
663 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800664 pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800665 return 0;
666}
667
668static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
669{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800670 pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800671 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
672 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800673 pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800674 return 0;
675}
676
677static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
678{
Wentao MA270dc0f2022-08-23 13:17:26 +0800679 DVR_WrapperPlaybackSegmentInfo_t *p_seg, *p_seg_tmp;
680 list_for_each_entry_safe(p_seg, p_seg_tmp, &ctx->segments, head) {
681 list_del(&p_seg->head);
682 free(p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800683 }
684}
685
Wentao MA270dc0f2022-08-23 13:17:26 +0800686static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800687 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
688{
689 (void)ctx;
690 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800691 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800692 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800693 p_seg->seg_info.nb_pids = seg_info->nb_pids;
694 memcpy(p_seg->seg_info.pids, seg_info->pids, sizeof(p_seg->seg_info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800695 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800696 p_seg->seg_info.duration = seg_info->duration;
697 p_seg->seg_info.size = seg_info->size;
698 p_seg->seg_info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800699 }
hualing chen03fd4942021-07-15 15:56:41 +0800700 //update current segment duration on timeshift mode
hualing chenb9b358a2021-08-17 15:06:36 +0800701 if (ctx->playback.param_open.is_timeshift
702 || ctx_isPlay_recording(ctx->playback.param_open.location))
Wentao MA270dc0f2022-08-23 13:17:26 +0800703 dvr_playback_update_duration(ctx->playback.player,p_seg->seg_info.id,p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800704 /*no changes
705 DVR_PlaybackSegmentFlag_t flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800706 p_seg->playback_info.segment_id = p_seg->seg_info.id;
707 strncpy(p_seg->playback_info.location,
708 ctx->playback.param_open.location, sizeof(p_seg->playback_info.location));
709 p_seg->playback_info.pids = ctx->playback.pids_req;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800710 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
711 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
712 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
Wentao MA270dc0f2022-08-23 13:17:26 +0800713 p_seg->playback_info.flags = flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800714 */
715}
716
717static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
718{
Wentao MA270dc0f2022-08-23 13:17:26 +0800719 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800720
Wentao MA96f68962022-06-15 19:45:35 +0800721 DVR_WRAPPER_INFO("timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800722 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
723
724 if (list_empty(&ctx->segments)) {
Wentao MA96f68962022-06-15 19:45:35 +0800725 DVR_WRAPPER_INFO("timeshift, update while no segment exists, ignore\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800726 return DVR_SUCCESS;
727 }
728
729 /*normally, the last segment added will be updated*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800730 p_seg =
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800731 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800732 if (p_seg->seg_info.id == seg_info->id) {
733 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800734 } else {
wentao.mafd5283f2022-10-14 09:51:13 +0800735 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +0800736 // prefetch() here incurring self_assign is used to avoid some compiling
737 // warnings.
738 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +0800739 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
740 if (p_seg->seg_info.id == seg_info->id) {
741 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800742 break;
743 }
744 }
745 }
746
747 /*need to notify the dvr_playback*/
hualing chenb9b358a2021-08-17 15:06:36 +0800748 if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/
749 || ctx_isPlay_recording(ctx->playback.param_open.location))
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800750 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
751 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
752 if (
753 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
754 (ctx->playback.seg_status.segment_id == seg_info->id
Zhiqiang Han9b0b7292023-02-21 17:15:32 +0800755 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME))
756 && ctx->playback.speed != 0.0f)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800757 ||
758 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
759 (ctx->playback.seg_status.segment_id != seg_info->id
Zhiqiang Han9b0b7292023-02-21 17:15:32 +0800760 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME)
761 && ctx->playback.speed != 0.0f)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800762 )
763 {
764 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800765 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800766 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800767 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800768
769 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800770 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 +0800771 ctx->sn, error,
772 seg_info->id, seg_info->duration,
773 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
774 }
775 }
776
777 return DVR_SUCCESS;
778}
779
Wentao MA270dc0f2022-08-23 13:17:26 +0800780static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800781 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
782{
783 (void)ctx;
784 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800785 p_seg->info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800786 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800787 p_seg->info.nb_pids = seg_info->nb_pids;
788 memcpy(p_seg->info.pids, seg_info->pids, sizeof(p_seg->info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800789 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800790 p_seg->info.duration = seg_info->duration;
791 p_seg->info.size = seg_info->size;
792 p_seg->info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800793 }
794}
795
796static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
797{
Wentao MA270dc0f2022-08-23 13:17:26 +0800798 DVR_WrapperRecordSegmentInfo_t *p_seg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800799
800 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800801 if (!list_empty(&ctx->segments)) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800802 p_seg =
hualing chen266b9502020-04-04 17:39:39 +0800803 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800804 if (p_seg->info.id == seg_info->id) {
805 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800806 } else {
wentao.mafd5283f2022-10-14 09:51:13 +0800807 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +0800808 // prefetch() here incurring self_assign is used to avoid some compiling
809 // warnings.
810 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +0800811 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
812 if (p_seg->info.id == seg_info->id) {
813 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800814 break;
815 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800816 }
817 }
818 }
819
820 /*timeshift, update the segment for playback*/
821 /*
822 the playback should grab the segment info other than the id,
823 and the id will be updated by each segment-add during the recording
824 */
825 /*
826 the playback paused if no data been checked from recording,
827 should resume the player later when there's more data
828 */
hualing chenb9b358a2021-08-17 15:06:36 +0800829 int sn = 0;
830 if (ctx->record.param_open.is_timeshift ||
831 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
832 DVR_WrapperCtx_t *ctx_playback;
833 if (ctx->record.param_open.is_timeshift)
834 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
835 else
836 ctx_playback = ctx_getPlayback(sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800837
838 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800839 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800840 if (ctx_valid(ctx_playback)
hualing chenb9b358a2021-08-17 15:06:36 +0800841 && (ctx_playback->sn == sn_timeshift_playback ||
842 ctx_playback->sn == sn)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800843 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
844 }
Gong Kefdb31922022-06-17 17:11:16 +0800845 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800846 }
847 }
848
849 return DVR_SUCCESS;
850}
851
852static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
853 DVR_RecordSegmentInfo_t *seg_info,
854 DVR_PlaybackPids_t *p_pids,
855 DVR_PlaybackSegmentFlag_t flags)
856{
Wentao MA270dc0f2022-08-23 13:17:26 +0800857 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800858 int error;
859
860 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +0800861 p_seg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
862 if (!p_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800863 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +0800864 DVR_WRAPPER_INFO("memory fail\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800865 return error;
866 }
867
Wentao MA270dc0f2022-08-23 13:17:26 +0800868 /*copy the original segment info*/
869 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800870 /*generate the segment info used in playback*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800871 p_seg->playback_info.segment_id = p_seg->seg_info.id;
Wentao MAe88ad702022-09-02 10:35:00 +0800872 const int len = strlen(ctx->playback.param_open.location);
873 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
874 DVR_WRAPPER_ERROR("Invalid playback.param_open.location length %d", len);
Wentao MA4d85ff32022-09-23 11:36:18 +0800875 free(p_seg);
Wentao MAe88ad702022-09-02 10:35:00 +0800876 return DVR_FAILURE;
877 }
878 strncpy(p_seg->playback_info.location, ctx->playback.param_open.location, len+1);
Wentao MA270dc0f2022-08-23 13:17:26 +0800879 p_seg->playback_info.pids = *p_pids;
880 p_seg->playback_info.flags = flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800881 p_seg->playback_info.duration = p_seg->seg_info.duration;
wentao.maa22bc852022-10-13 12:18:06 +0800882 list_add(p_seg, &ctx->segments);
883 DVR_WRAPPER_INFO("start to add segment %lld\n", p_seg->playback_info.segment_id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800884
Wentao MA270dc0f2022-08-23 13:17:26 +0800885 error = dvr_playback_add_segment(ctx->playback.player, &p_seg->playback_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800886 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800887 DVR_WRAPPER_INFO("fail to add segment %lld (%d)\n", p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800888 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800889 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
890 ctx->playback.status.info_full.size += p_seg->seg_info.size;
891 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800892 }
893
894 return error;
895}
896
897static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
898{
Wentao MA270dc0f2022-08-23 13:17:26 +0800899 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MA16f870e2022-09-09 11:00:22 +0800900 int error = DVR_SUCCESS;
hualing chenab0d1262021-09-26 15:22:50 +0800901 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800902
Wentao MA270dc0f2022-08-23 13:17:26 +0800903 p_seg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
904 if (!p_seg) {
Wentao MA16f870e2022-09-09 11:00:22 +0800905 DVR_WRAPPER_ERROR("memory allocation failed");
906 return DVR_FAILURE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800907 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800908 p_seg->info = *seg_info;
wentao.maa22bc852022-10-13 12:18:06 +0800909 list_add(p_seg, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800910
hualing chenb9b358a2021-08-17 15:06:36 +0800911 if (ctx->record.param_open.is_timeshift ||
912 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
913
914 DVR_WrapperCtx_t *ctx_playback;
915 if (ctx->record.param_open.is_timeshift)
916 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
917 else
918 ctx_playback = ctx_getPlayback(sn);
919
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800920 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) add seg\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800921
922 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800923 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800924 if (ctx_valid(ctx_playback)) {
925 DVR_PlaybackSegmentFlag_t flags;
926
927 /*only if playback has started, the previous segments have been loaded*/
928 if (!list_empty(&ctx_playback->segments)) {
929 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800930 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800931 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
wentao.maa210e5e2022-10-12 16:10:03 +0800932 error = wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
933 if (error == DVR_FAILURE) {
934 DVR_WRAPPER_WARN("adding playback segment fails");
935 }
hualing chen451c8f72022-03-09 13:05:52 +0800936 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800937 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) list empty\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800938 }
hualing chenb9b358a2021-08-17 15:06:36 +0800939 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800940 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800941 }
Gong Kefdb31922022-06-17 17:11:16 +0800942 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800943 }
hualing chen451c8f72022-03-09 13:05:52 +0800944 else
945 {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800946 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid 2\n", ctx->sn, sn);
947 }
948
949 /*if it is not a timeshift recording, but a playing recording,
950 do not forget to obey the recording rule: link the segment!*/
951 if (!ctx->record.param_open.is_timeshift) {
952 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
953 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
hualing chen451c8f72022-03-09 13:05:52 +0800954 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800955 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800956 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
Wentao MAe8ba5172022-08-09 11:18:17 +0800957 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800958 }
959
960 return error;
961}
962
963static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
964{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800965 int error = -1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800966 DVR_WrapperPlaybackSegmentInfo_t *p_seg = NULL, *p_seg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800967 uint32_t off_set = 0;
Wentao MA96f68962022-06-15 19:45:35 +0800968 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800969
Wentao MA270dc0f2022-08-23 13:17:26 +0800970 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
971 if (p_seg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800972
973 if (ctx->current_segment_id == seg_info->id) {
974 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
975
976 /*drive the player out of this will-be-deleted segment*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800977 next_seg = list_prev_entry(p_seg, head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800978
979 if (ctx->playback.speed != 100.0f) {
980 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800981 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800982 }
hualing chenb9a1a2c2021-12-31 11:27:59 +0800983 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
984 off_set = 10 * 1000;
985 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800986 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 +0800987
988 if (ctx->playback.speed == 0.0f) {
989 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
Wentao MA96f68962022-06-15 19:45:35 +0800990 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 +0800991 } else if (ctx->playback.speed != 100.0f) {
992 DVR_PlaybackSpeed_t dvr_speed = {
993 .speed = { ctx->playback.speed },
994 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
995 };
996 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
Wentao MA96f68962022-06-15 19:45:35 +0800997 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 +0800998 }
999 }
1000
1001 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
1002 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001003 /*remove playback segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +08001004 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 +08001005 }
1006
Wentao MA270dc0f2022-08-23 13:17:26 +08001007 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001008
1009 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001010 ctx->playback.obsolete.time += p_seg->seg_info.duration;
1011 ctx->playback.obsolete.size += p_seg->seg_info.size;
1012 ctx->playback.obsolete.pkts += p_seg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +08001013 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 +08001014 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Wentao MA270dc0f2022-08-23 13:17:26 +08001015 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001016 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001017 }
1018 }
1019
Wentao MA96f68962022-06-15 19:45:35 +08001020 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 +08001021
1022 return error;
1023}
1024
1025static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
1026{
1027 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08001028 DVR_WrapperRecordSegmentInfo_t *p_seg, *p_seg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001029
Wentao MAf4072032022-06-30 13:50:45 +08001030 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1031 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001032
1033 /*if timeshifting, notify the playback first, then deal with record*/
1034 if (ctx->record.param_open.is_timeshift) {
1035 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1036
1037 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001038 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001039 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1040 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001041 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1042 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1043 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001044 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001045 return DVR_SUCCESS;
1046 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001047 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1048 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1049 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001050 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001051 if (ctx_valid(ctx_playback)
1052 && ctx_playback->sn == sn_timeshift_playback
1053 && !list_empty(&ctx_playback->segments)) {
1054 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
Wentao MA07d3d742022-09-06 09:58:05 +08001055 if (error != DVR_SUCCESS) {
1056 DVR_WRAPPER_ERROR("wrapper_removePlaybackSegment failed with return value %d",error);
1057 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001058 }
hualing chen56c0a162022-01-27 17:01:50 +08001059 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001060 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001061 }
1062 }
1063
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001064 uint64_t id = seg_info->info.id;
1065
Wentao MA270dc0f2022-08-23 13:17:26 +08001066 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
1067 if (p_seg->info.id == id) {
1068 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001069
1070 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001071 ctx->record.obsolete.time += p_seg->info.duration;
1072 ctx->record.obsolete.size += p_seg->info.size;
1073 ctx->record.obsolete.pkts += p_seg->info.nb_packets;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001074
Wentao MA270dc0f2022-08-23 13:17:26 +08001075 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001076 break;
1077 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001078 }
1079
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001080 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001081
Wentao MAf4072032022-06-30 13:50:45 +08001082 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1083 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001084
1085 return error;
1086}
1087
1088int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1089{
1090 int error;
1091 DVR_WrapperCtx_t *ctx;
1092 DVR_RecordOpenParams_t open_param;
1093
1094 DVR_RETURN_IF_FALSE(rec);
1095 DVR_RETURN_IF_FALSE(params);
1096
1097 /*get a free ctx*/
1098 ctx = ctx_getRecord(0);
1099 DVR_RETURN_IF_FALSE(ctx);
1100
Gong Kefdb31922022-06-17 17:11:16 +08001101 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001102
Wentao MA9a164002022-08-29 11:20:24 +08001103 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 +08001104 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001105
1106 ctx_reset(ctx);
1107
1108 ctx->record.param_open = *params;
1109 ctx->record.event_fn = params->event_fn;
1110 ctx->record.event_userdata = params->event_userdata;
Wentao MA2394fa82022-06-10 14:46:47 +08001111
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001112 INIT_LIST_HEAD(&ctx->segments);
1113 ctx->sn = get_sn();
1114
1115 wrapper_requestThreadFor(ctx);
1116
hualing chen266b9502020-04-04 17:39:39 +08001117 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001118 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001119 open_param.dmx_dev_id = params->dmx_dev_id;
1120 open_param.data_from_memory = 0;
1121 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001122 if (params->flush_size) {
1123 open_param.notification_size = params->flush_size;
1124 } else {
1125 open_param.notification_size = 64*1024;
1126 }
hualing chen002e5b92022-02-23 17:51:21 +08001127 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001128 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001129 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001130 open_param.event_fn = wrapper_record_event_handler;
1131 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001132 if (params->keylen) {
1133 open_param.clearkey = params->clearkey;
1134 open_param.cleariv = params->cleariv;
1135 open_param.keylen = params->keylen;
1136 }
wentao.ma35a69d42022-03-10 18:08:40 +08001137 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001138 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001139
1140 error = dvr_record_open(&ctx->record.recorder, &open_param);
1141 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001142 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001143 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001144 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001145 wrapper_releaseThreadForType(ctx->type);
1146 return DVR_FAILURE;
1147 }
1148 if (params->is_timeshift)
1149 sn_timeshift_record = ctx->sn;
1150
Wentao MA96f68962022-06-15 19:45:35 +08001151 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001152
Yahui Han1fbf3292021-11-08 18:17:19 +08001153 if (params->crypto_fn) {
1154 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1155 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001156 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 +08001157 }
hualing chen266b9502020-04-04 17:39:39 +08001158 }
1159
Gong Kefdb31922022-06-17 17:11:16 +08001160 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001161
1162 *rec = (DVR_WrapperRecord_t)ctx->sn;
1163 return DVR_SUCCESS;
1164}
1165
1166int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1167{
1168 DVR_WrapperCtx_t *ctx;
1169 DVR_RecordSegmentInfo_t seg_info;
1170 int error;
1171
1172 DVR_RETURN_IF_FALSE(rec);
1173
1174 ctx = ctx_getRecord((unsigned long)rec);
1175 DVR_RETURN_IF_FALSE(ctx);
1176
Gong Kefdb31922022-06-17 17:11:16 +08001177 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001178 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001179 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001180
1181 memset(&seg_info, 0, sizeof(seg_info));
wentao.maa210e5e2022-10-12 16:10:03 +08001182 dvr_record_stop_segment(ctx->record.recorder, &seg_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001183
1184 error = dvr_record_close(ctx->record.recorder);
1185
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001186 if (ctx->record.param_open.is_timeshift)
1187 sn_timeshift_record = 0;
1188
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001189 ctx_freeSegments(ctx);
1190
Wentao MA96f68962022-06-15 19:45:35 +08001191 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001192 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001193 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001194
1195 wrapper_releaseThreadForType(ctx->type);
1196
1197 return error;
1198}
1199
1200int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1201{
1202 DVR_WrapperCtx_t *ctx;
1203 DVR_RecordStartParams_t *start_param;
1204 int i;
1205 int error;
1206
1207 DVR_RETURN_IF_FALSE(rec);
1208 DVR_RETURN_IF_FALSE(params);
1209
1210 ctx = ctx_getRecord((unsigned long)rec);
1211 DVR_RETURN_IF_FALSE(ctx);
1212
Gong Kefdb31922022-06-17 17:11:16 +08001213 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001214 DVR_WRAPPER_INFO("libdvr_api, start_record (sn:%ld) location:%s, save:%d",
1215 ctx->sn, ctx->record.param_open.location, params->save_rec_file);
Gong Kefdb31922022-06-17 17:11:16 +08001216 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001217
1218 start_param = &ctx->record.param_start;
1219 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001220 const int len = strlen(ctx->record.param_open.location);
1221 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1222 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1223 pthread_mutex_unlock(&ctx->wrapper_lock);
1224 return DVR_FAILURE;
1225 }
1226 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001227 start_param->segment.nb_pids = params->pids_info.nb_pids;
1228 for (i = 0; i < params->pids_info.nb_pids; i++) {
1229 start_param->segment.pids[i] = params->pids_info.pids[i];
1230 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1231 }
Zhiqiang Han89c77972022-12-08 12:16:39 +08001232
hualing chena5f03222021-12-02 11:22:35 +08001233 if (params->save_rec_file == 0)//default is not save
1234 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han89c77972022-12-08 12:16:39 +08001235
1236 //wait for the file status to stabilize before set the new segment id
1237 uint64_t new_segment_id = 0;
1238 {
1239 uint32_t segment_nb = 0;
1240 uint64_t *p_segment_ids = NULL;
1241 error = dvr_segment_get_list(ctx->record.param_open.location, &segment_nb, &p_segment_ids);
1242 if (error == DVR_SUCCESS && segment_nb>0) {
1243 // Tainted data issue originating from fgets seem false positive, so we
1244 // just suppress it here.
1245 // coverity[tainted_data]
1246 new_segment_id = p_segment_ids[segment_nb-1]+1;
1247 }
1248 if (p_segment_ids != NULL) {
1249 free(p_segment_ids);
1250 }
1251 DVR_WRAPPER_DEBUG("new_segment_id:%lld\n", new_segment_id);
1252 }
1253 ctx->record.next_segment_id = new_segment_id;
1254 ctx->current_segment_id = new_segment_id;
1255
1256 start_param->segment.segment_id = ctx->record.next_segment_id++;
1257
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001258 {
1259 /*sync to update for further use*/
1260 DVR_RecordStartParams_t *update_param;
1261 update_param = &ctx->record.param_update;
1262 memcpy(update_param, start_param, sizeof(*update_param));
1263 for (i = 0; i < update_param->segment.nb_pids; i++)
1264 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1265 }
1266
1267 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1268 {
1269 DVR_RecordSegmentInfo_t new_seg_info =
1270 { .id = start_param->segment.segment_id, };
1271 wrapper_addRecordSegment(ctx, &new_seg_info);
1272 }
1273
Wentao MA96f68962022-06-15 19:45:35 +08001274 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001275
Gong Kefdb31922022-06-17 17:11:16 +08001276 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001277
1278 return error;
1279}
1280
1281int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1282{
1283 DVR_WrapperCtx_t *ctx;
1284 DVR_RecordSegmentInfo_t seg_info;
1285 int error;
1286
1287 DVR_RETURN_IF_FALSE(rec);
1288
1289 ctx = ctx_getRecord((unsigned long)rec);
1290 DVR_RETURN_IF_FALSE(ctx);
1291
Gong Kefdb31922022-06-17 17:11:16 +08001292 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001293 DVR_WRAPPER_INFO("libdvr_api, stop_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001294 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001295
1296 memset(&seg_info, 0, sizeof(seg_info));
1297 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1298 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1299
Wentao MAd71e2062023-02-15 10:10:49 +08001300 ctx_freeSegments(ctx);
1301
Wentao MA96f68962022-06-15 19:45:35 +08001302 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001303 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001304
1305 return error;
1306}
1307
hualing chen03fd4942021-07-15 15:56:41 +08001308int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1309{
1310 DVR_WrapperCtx_t *ctx;
1311 int error;
1312
1313 DVR_RETURN_IF_FALSE(rec);
1314
1315 ctx = ctx_getRecord((unsigned long)rec);
1316 DVR_RETURN_IF_FALSE(ctx);
1317
Gong Kefdb31922022-06-17 17:11:16 +08001318 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001319 DVR_WRAPPER_INFO("libdvr_api, pause_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001320 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001321
1322 error = dvr_record_pause(ctx->record.recorder);
1323
Wentao MA96f68962022-06-15 19:45:35 +08001324 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001325 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001326
1327 return error;
1328}
1329
1330int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1331{
1332 DVR_WrapperCtx_t *ctx;
1333 int error;
1334
1335 DVR_RETURN_IF_FALSE(rec);
1336
1337 ctx = ctx_getRecord((unsigned long)rec);
1338 DVR_RETURN_IF_FALSE(ctx);
1339
Gong Kefdb31922022-06-17 17:11:16 +08001340 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001341 DVR_WRAPPER_INFO("libdvr_api, resume_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001342 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001343
1344 error = dvr_record_resume(ctx->record.recorder);
1345
Wentao MA96f68962022-06-15 19:45:35 +08001346 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001347 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001348
1349 return error;
1350}
1351
Wentao MAcdea4762022-04-26 13:28:56 +08001352/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001353static DVR_Bool_t pids_test_include(
1354 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1355 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001356{
Wentao MAcdea4762022-04-26 13:28:56 +08001357 DVR_Bool_t ret = DVR_TRUE;
1358 for (int i=0;i<size2;i++)
1359 { // iterate all elements in arr2 to check if they exist in arr1
1360 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001361
1362 if (act2[i] == DVR_RECORD_PID_CLOSE)
1363 continue;
1364
Wentao MAcdea4762022-04-26 13:28:56 +08001365 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001366 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001367 if (act1[j] != DVR_RECORD_PID_CLOSE
1368 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001369 {
1370 found=DVR_TRUE;
1371 break;
1372 }
1373 }
1374 if (found == DVR_FALSE)
1375 {
Wentao MAcdea4762022-04-26 13:28:56 +08001376 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001377 break;
1378 }
1379 }
Wentao MAcdea4762022-04-26 13:28:56 +08001380 return ret;
1381}
1382
1383static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1384 const DVR_WrapperUpdatePidsParams_t* p2)
1385{
1386 int i=0;
1387 char buf[128]={0};
1388 int cnt=0;
1389 int chars=0;
1390
1391 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1392 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1393
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001394 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1395 p2->pids,p2->pid_action,p2->nb_pids);
1396 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1397 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001398 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001399 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001400
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001401 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001402 for (i=0;i<p1->nb_pids;i++)
1403 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001404 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1405 removed++;
1406 continue;
1407 }
Wentao MAcdea4762022-04-26 13:28:56 +08001408 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1409 if (chars<0)
1410 {
1411 break;
1412 }
1413 cnt += chars;
1414 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001415 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001416 memset(buf,0,sizeof(buf));
1417
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001418 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001419 for (i=0,cnt=0;i<p2->nb_pids;i++)
1420 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001421 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1422 removed++;
1423 continue;
1424 }
Wentao MAcdea4762022-04-26 13:28:56 +08001425 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1426 if (chars<0)
1427 {
1428 break;
1429 }
1430 cnt += chars;
1431 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001432 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001433 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001434 return is_equal;
1435}
1436
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001437int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1438{
1439 DVR_WrapperCtx_t *ctx;
1440 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001441 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001442 int i;
1443 int error;
1444
1445 DVR_RETURN_IF_FALSE(rec);
1446 DVR_RETURN_IF_FALSE(params);
1447
1448 ctx = ctx_getRecord((unsigned long)rec);
1449 DVR_RETURN_IF_FALSE(ctx);
1450
Gong Kefdb31922022-06-17 17:11:16 +08001451 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001452 DVR_WRAPPER_INFO("libdvr_api, update_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001453 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001454
1455 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001456 if (pids_equal(&(start_param->segment),params))
1457 {
Gong Kefdb31922022-06-17 17:11:16 +08001458 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001459 return DVR_TRUE;
1460 }
1461
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001462 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001463 const int len = strlen(ctx->record.param_open.location);
1464 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1465 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1466 pthread_mutex_unlock(&ctx->wrapper_lock);
1467 return DVR_FAILURE;
1468 }
1469 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001470 start_param->segment.segment_id = ctx->record.next_segment_id++;
1471 start_param->segment.nb_pids = params->nb_pids;
1472 for (i = 0; i < params->nb_pids; i++) {
1473 start_param->segment.pids[i] = params->pids[i];
1474 start_param->segment.pid_action[i] = params->pid_action[i];
1475 }
1476 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1477 {
1478 DVR_RecordSegmentInfo_t new_seg_info =
1479 { .id = start_param->segment.segment_id, };
1480 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1481 wrapper_addRecordSegment(ctx, &new_seg_info);
1482 }
1483
Wentao MA96f68962022-06-15 19:45:35 +08001484 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001485 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001486
1487 return error;
1488}
1489
1490int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1491{
1492 DVR_WrapperCtx_t *ctx;
1493 DVR_WrapperRecordStatus_t s;
1494 int error;
1495
1496 DVR_RETURN_IF_FALSE(rec);
1497 DVR_RETURN_IF_FALSE(status);
1498
1499 ctx = ctx_getRecord((unsigned long)rec);
1500 DVR_RETURN_IF_FALSE(ctx);
1501
Gong Kefdb31922022-06-17 17:11:16 +08001502 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001503
Wentao MA804bab12022-11-29 10:01:26 +08001504 DVR_WRAPPER_INFO("libdvr_api, get_record_status (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001505 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001506
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001507 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001508
Wentao MA96f68962022-06-15 19:45:35 +08001509 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001510 ctx->sn,
1511 s.state,
1512 s.info.time,
1513 s.info.size,
1514 s.info.pkts,
1515 error);
1516
1517 *status = s;
1518
Gong Kefdb31922022-06-17 17:11:16 +08001519 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001520
1521 return error;
1522}
1523
hualing chen4fe3bee2020-10-23 13:58:52 +08001524int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1525{
1526 DVR_WrapperCtx_t *ctx;
1527 int error;
1528
1529 DVR_RETURN_IF_FALSE(rec);
1530
1531 ctx = ctx_getRecord((unsigned long)rec);
1532 DVR_RETURN_IF_FALSE(ctx);
1533
Gong Kefdb31922022-06-17 17:11:16 +08001534 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001535 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001536 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001537 return error;
1538}
1539
hualing chen266b9502020-04-04 17:39:39 +08001540int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1541{
1542 DVR_WrapperCtx_t *ctx;
1543 int error;
1544
1545 DVR_RETURN_IF_FALSE(rec);
1546 DVR_RETURN_IF_FALSE(p_secure_buf);
1547
1548 ctx = ctx_getRecord((unsigned long)rec);
1549 DVR_RETURN_IF_FALSE(ctx);
1550
Gong Kefdb31922022-06-17 17:11:16 +08001551 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001552 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001553 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001554 return error;
1555}
1556
1557int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1558{
1559 DVR_WrapperCtx_t *ctx;
1560 int error;
1561
1562 DVR_RETURN_IF_FALSE(rec);
1563 DVR_RETURN_IF_FALSE(func);
1564
1565 ctx = ctx_getRecord((unsigned long)rec);
1566 DVR_RETURN_IF_FALSE(ctx);
1567
Gong Kefdb31922022-06-17 17:11:16 +08001568 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001569 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001570 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001571 return error;
1572}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001573
1574
1575int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1576{
1577 DVR_WrapperCtx_t *ctx;
1578 DVR_PlaybackOpenParams_t open_param;
1579 int error;
1580
1581 DVR_RETURN_IF_FALSE(playback);
1582 DVR_RETURN_IF_FALSE(params);
1583 DVR_RETURN_IF_FALSE(params->playback_handle);
1584
1585 /*get a free ctx*/
1586 ctx = ctx_getPlayback(0);
1587 DVR_RETURN_IF_FALSE(ctx);
1588
Gong Kefdb31922022-06-17 17:11:16 +08001589 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001590
Wentao MA804bab12022-11-29 10:01:26 +08001591 DVR_WRAPPER_INFO("libdvr_api, open_playback (dmx:%d) ..vendor[%d]params->block_size[%d].",
1592 params->dmx_dev_id, params->vendor, params->block_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001593
1594 ctx_reset(ctx);
1595
1596 ctx->playback.param_open = *params;
1597 ctx->playback.event_fn = params->event_fn;
1598 ctx->playback.event_userdata = params->event_userdata;
1599 ctx->current_segment_id = 0;
1600 INIT_LIST_HEAD(&ctx->segments);
1601 ctx->sn = get_sn();
1602
1603 wrapper_requestThreadFor(ctx);
1604
hualing chen266b9502020-04-04 17:39:39 +08001605 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001606 open_param.dmx_dev_id = params->dmx_dev_id;
1607 open_param.block_size = params->block_size;
1608 open_param.is_timeshift = params->is_timeshift;
1609 //open_param.notification_size = 10*1024; //not supported
1610 open_param.event_fn = wrapper_playback_event_handler;
1611 open_param.event_userdata = (void*)ctx->sn;
1612 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001613 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001614 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001615 open_param.vendor = params->vendor;
1616
Yahui Han1fbf3292021-11-08 18:17:19 +08001617 if (params->keylen) {
1618 open_param.clearkey = params->clearkey;
1619 open_param.cleariv = params->cleariv;
1620 open_param.keylen = params->keylen;
1621 }
shenghui.gengbec6a462023-01-12 15:21:02 +08001622 open_param.control_speed_enable = params->control_speed_enable;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001623
1624 error = dvr_playback_open(&ctx->playback.player, &open_param);
1625 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001626 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001627 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001628 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001629 wrapper_releaseThreadForType(ctx->type);
1630 return DVR_FAILURE;
1631 }
1632 if (params->is_timeshift)
1633 sn_timeshift_playback = ctx->sn;
1634
Wentao MA270dc0f2022-08-23 13:17:26 +08001635 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001636 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1637 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001638 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001639 }
Gong Kefdb31922022-06-17 17:11:16 +08001640 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001641
1642 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1643 return DVR_SUCCESS;
1644}
1645
1646int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1647{
1648 DVR_WrapperCtx_t *ctx;
1649 int error;
1650
1651 DVR_RETURN_IF_FALSE(playback);
1652
1653 ctx = ctx_getPlayback((unsigned long)playback);
1654 DVR_RETURN_IF_FALSE(ctx);
1655
Gong Kefdb31922022-06-17 17:11:16 +08001656 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001657 DVR_WRAPPER_INFO("libdvr_api, close_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001658 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001659
1660 if (ctx->playback.param_open.is_timeshift)
1661 sn_timeshift_playback = 0;
1662
1663 /*try stop first*/
wentao.maa210e5e2022-10-12 16:10:03 +08001664 dvr_playback_stop(ctx->playback.player, DVR_TRUE);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001665
1666 {
1667 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001668 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001669
wentao.mafd5283f2022-10-14 09:51:13 +08001670 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001671 // prefetch() here incurring self_assign is used to avoid some compiling
1672 // warnings.
1673 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08001674 list_for_each_entry(p_seg, &ctx->segments, head) {
1675 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001676 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001677 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001678 }
1679 ctx_freeSegments(ctx);
1680 }
1681
1682 error = dvr_playback_close(ctx->playback.player);
1683
Wentao MA96f68962022-06-15 19:45:35 +08001684 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001685 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001686 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001687
1688 wrapper_releaseThreadForType(ctx->type);
1689
1690 return error;
1691}
1692
1693int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1694{
1695 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001696 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001697 uint64_t *p_segment_ids;
1698 uint32_t segment_nb;
1699 uint32_t i;
1700 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001701 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001702 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001703 DVR_Bool_t is_timeshift = DVR_FALSE;
Wentao MAcefc13c2022-10-26 15:47:24 +08001704 DVR_PlaybackSegmentFlag_t seg_flags = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001705
1706 DVR_RETURN_IF_FALSE(playback);
1707 DVR_RETURN_IF_FALSE(p_pids);
1708
hualing chenc110f952021-01-18 11:25:37 +08001709 ctx_record = NULL;
1710
1711 /*lock the recorder to avoid changing the recording segments*/
1712 ctx_record = ctx_getRecord(sn_timeshift_record);
1713
1714 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001715 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001716 if (!ctx_valid(ctx_record)
1717 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001718 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001719 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001720 is_timeshift = DVR_FALSE;
1721 } else {
1722 is_timeshift = DVR_TRUE;
1723 }
1724 }
1725
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001726 ctx = ctx_getPlayback((unsigned long)playback);
1727 DVR_RETURN_IF_FALSE(ctx);
1728
Gong Kefdb31922022-06-17 17:11:16 +08001729 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001730
Wentao MA804bab12022-11-29 10:01:26 +08001731 DVR_WRAPPER_INFO("libdvr_api, start_playback (sn:%ld) location:%s"
1732 " flags:0x%x v/a/ad/sub/pcr(%d:%d %d:%d %d:%d %d:%d %d)",
1733 ctx->sn,
1734 ctx->playback.param_open.location,
1735 flags,
1736 p_pids->video.pid, p_pids->video.format,
1737 p_pids->audio.pid, p_pids->audio.format,
1738 p_pids->ad.pid, p_pids->ad.format,
1739 p_pids->subtitle.pid, p_pids->subtitle.format,
1740 p_pids->pcr.pid);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001741
Gong Kefdb31922022-06-17 17:11:16 +08001742 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001743
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001744 if (ctx->playback.param_open.is_timeshift) {
1745 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001746 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001747 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001748 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001749 return DVR_FAILURE;
1750 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001751 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001752 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001753 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001754 }
1755
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001756 /*obtain all segments in a list*/
1757 segment_nb = 0;
1758 p_segment_ids = NULL;
1759 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001760 if (!error) {
1761 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001762 struct list_head info_list; /**< segment list head*/
1763 INIT_LIST_HEAD(&info_list);
1764
Wentao MA96f68962022-06-15 19:45:35 +08001765 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001766 //we need free info list buf when we used end.
1767 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001768 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001769 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001770 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1771 ctx->playback.param_open.location, error);
wentao.maf57dd232022-10-08 16:07:29 +08001772 // Tainted data issue originating from fgets seem false positive, so we
1773 // just suppress it here.
1774 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001775 for (i = 0; i < segment_nb; i++) {
1776 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001777 memset((void*)&seg_info,0,sizeof(seg_info));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001778
hualing chenb9a02922021-12-14 11:29:47 +08001779 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1780 if (error) {
1781 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001782 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001783 ctx->playback.param_open.location, p_segment_ids[i], error);
1784 break;
1785 }
1786 //add check if has audio or video pid. if not exist. not add segment to playback
1787 int ii = 0;
1788 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001789 // Tainted data issue originating from fgets seem false positive, so we
1790 // just suppress it here.
1791 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001792 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1793 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1794 if (type == DVR_STREAM_TYPE_VIDEO ||
1795 type == DVR_STREAM_TYPE_AUDIO ||
1796 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001797 DVR_WRAPPER_INFO("success to get seg av info \n");
1798 DVR_WRAPPER_INFO("success to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid,
hualing chenb9a02922021-12-14 11:29:47 +08001799 DVR_STREAM_TYPE_VIDEO,
1800 DVR_STREAM_TYPE_AUDIO,
1801 DVR_STREAM_TYPE_AD);
1802 has_av = 1;
1803 //break;
1804 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001805 DVR_WRAPPER_INFO("error to get seg av info type[0x%x][%d] [%d][%d][%d]\n",(seg_info.pids[ii].type >> 24)&0x0f,seg_info.pids[ii].pid,
hualing chenb9a02922021-12-14 11:29:47 +08001806 DVR_STREAM_TYPE_VIDEO,
1807 DVR_STREAM_TYPE_AUDIO,
1808 DVR_STREAM_TYPE_AD);
1809 }
1810 }
1811 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001812 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001813 continue;
1814 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001815 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001816 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001817 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1818 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001819 if (error == DVR_FAILURE) {
1820 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001821 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001822 }
hualing chenb9a02922021-12-14 11:29:47 +08001823 /*copy the 1st segment*/
1824 if (got_1st_seg == 0) {
1825 seg_info_1st = seg_info;
1826 got_1st_seg = 1;
1827 }
1828 }
1829 } else {
wentao.maf57dd232022-10-08 16:07:29 +08001830 // Tainted data issue originating from fgets seem false positive, so we
1831 // just suppress it here.
1832 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001833 for (i = 0; i < segment_nb; i++) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001834 DVR_RecordSegmentInfo_t *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001835 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08001836 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001837 // prefetch() here incurring self_assign is used to avoid some compiling
1838 // warnings.
1839 // coverity[self_assign]
Wentao MA4d85ff32022-09-23 11:36:18 +08001840 list_for_each_entry(p_seg_info, &info_list, head)
hualing chenb9a02922021-12-14 11:29:47 +08001841 {
Wentao MA4d85ff32022-09-23 11:36:18 +08001842 if (p_seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001843 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001844 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001845 break;
1846 }
1847 }
1848 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001849 //last info is not found if when recording occured power off.
1850 if (p_segment_ids[i] == segment_nb - 1) {
1851 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001852 memset((void*)&seg_info,0,sizeof(seg_info));
hualing chen8aed9582021-12-24 17:59:56 +08001853
1854 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1855 if (error) {
1856 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001857 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001858 ctx->playback.param_open.location, p_segment_ids[i], error);
1859 break;
1860 }
1861 //
1862 //add check if has audio or video pid. if not exist. not add segment to playback
1863 int ii = 0;
1864 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001865 // Tainted data issue originating from fgets seem false positive, so we
1866 // just suppress it here.
1867 // coverity[tainted_data]
hualing chen8aed9582021-12-24 17:59:56 +08001868 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1869 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1870 if (type == DVR_STREAM_TYPE_VIDEO ||
1871 type == DVR_STREAM_TYPE_AUDIO ||
1872 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001873 DVR_WRAPPER_INFO("success to get seg av info \n");
1874 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 +08001875 DVR_STREAM_TYPE_VIDEO,
1876 DVR_STREAM_TYPE_AUDIO,
1877 DVR_STREAM_TYPE_AD);
1878 has_av = 1;
1879 //break;
1880 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001881 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 +08001882 DVR_STREAM_TYPE_VIDEO,
1883 DVR_STREAM_TYPE_AUDIO,
1884 DVR_STREAM_TYPE_AD);
1885 }
1886 }
1887 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001888 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001889 continue;
1890 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001891 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001892 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001893 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1894 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001895 if (error == DVR_FAILURE) {
1896 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chen8aed9582021-12-24 17:59:56 +08001897 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001898 }
hualing chen8aed9582021-12-24 17:59:56 +08001899 }
hualing chenb9a02922021-12-14 11:29:47 +08001900 continue;
1901 }
1902
1903 //add check if has audio or video pid. if not exist. not add segment to playback
1904 int ii = 0;
1905 int has_av = 0;
Wentao MA4d85ff32022-09-23 11:36:18 +08001906 for (ii = 0; ii < p_seg_info->nb_pids; ii++) {
1907 int type = (p_seg_info->pids[ii].type >> 24) & 0x0f;
hualing chenb9a02922021-12-14 11:29:47 +08001908 if (type == DVR_STREAM_TYPE_VIDEO ||
1909 type == DVR_STREAM_TYPE_AUDIO ||
1910 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001911 DVR_WRAPPER_INFO("success to get seg av info \n");
Wentao MA4d85ff32022-09-23 11:36:18 +08001912 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 +08001913 DVR_STREAM_TYPE_VIDEO,
1914 DVR_STREAM_TYPE_AUDIO,
1915 DVR_STREAM_TYPE_AD);
1916 has_av = 1;
1917 //break;
1918 } else {
Wentao MA4d85ff32022-09-23 11:36:18 +08001919 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 +08001920 DVR_STREAM_TYPE_VIDEO,
1921 DVR_STREAM_TYPE_AUDIO,
1922 DVR_STREAM_TYPE_AD);
1923 }
1924 }
1925 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001926 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001927 continue;
1928 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001929 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001930 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001931 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1932 error = wrapper_addPlaybackSegment(ctx, p_seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001933 if (error == DVR_FAILURE) {
1934 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001935 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001936 }
hualing chenb9a02922021-12-14 11:29:47 +08001937
1938 /*copy the 1st segment*/
1939 if (got_1st_seg == 0) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001940 seg_info_1st = *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001941 got_1st_seg = 1;
1942 }
hualing chen92f3a142020-07-08 20:59:33 +08001943 }
hualing chenb9a02922021-12-14 11:29:47 +08001944 //free list
1945 DVR_RecordSegmentInfo_t *segment = NULL;
1946 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1947 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1948 {
1949 if (segment) {
1950 list_del(&segment->head);
1951 free(segment);
1952 }
1953 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001954 }
hualing chenb9a02922021-12-14 11:29:47 +08001955
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001956 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001957
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001958 /* return if no segment or fail to add */
1959 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001960
Wentao MA270dc0f2022-08-23 13:17:26 +08001961 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001962 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1963 ctx->playback.obsolete = ctx_record->record.obsolete;
1964 }
1965
Wentao MA96f68962022-06-15 19:45:35 +08001966 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001967
1968 ctx->playback.reach_end = DVR_FALSE;
1969 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1970 ctx->playback.speed = 0.0f;
1971 else
1972 ctx->playback.speed = 100.0f;
1973
1974 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001975 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001976 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001977 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001978 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001979 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001980 error = dvr_playback_start(ctx->playback.player, flags);
1981 } else {
wentao.maa210e5e2022-10-12 16:10:03 +08001982 dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
hualing chen03fd4942021-07-15 15:56:41 +08001983 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001984 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001985 ctx->sn, seg_info_1st.id, error);
1986 }
Wentao MA96f68962022-06-15 19:45:35 +08001987 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001988 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001989 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 +08001990 }
1991 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001992
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001993 if (ctx->playback.param_open.is_timeshift) {
1994 /*unlock the recorder locked above*/
1995 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001996 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001997 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001998 ctx->sn, ctx_record->sn);
1999 }
2000 }
Gong Kefdb31922022-06-17 17:11:16 +08002001 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002002
2003 return error;
2004}
hualing chen002e5b92022-02-23 17:51:21 +08002005//stop record and playback
2006int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
2007{
2008 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
2009 int error;
Wentao MA804bab12022-11-29 10:01:26 +08002010 DVR_WRAPPER_INFO("libdvr_api, stop_timeshift");
hualing chen002e5b92022-02-23 17:51:21 +08002011
2012 //stop timeshift record
2013 ctx_record = ctx_getRecord(sn_timeshift_record);
wentao.maa210e5e2022-10-12 16:10:03 +08002014 dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
hualing chen002e5b92022-02-23 17:51:21 +08002015
Wentao MA96f68962022-06-15 19:45:35 +08002016 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08002017 //stop play
2018 error = dvr_wrapper_stop_playback(playback);
2019 //del timeshift file
2020 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002021 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08002022 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
2023 }
2024 return error;
2025}
2026//start record and start playback
2027int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
2028{
2029 DVR_WrapperCtx_t *ctx;
2030 DVR_RecordStartParams_t *start_param;
2031 int error;
2032
2033 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
2034 DVR_RETURN_IF_FALSE(ctx);
2035
Gong Kefdb31922022-06-17 17:11:16 +08002036 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002037 DVR_WRAPPER_INFO("libdvr_api, restart_timeshift (sn:%ld) location:%s",
2038 ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08002039 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002040
hualing chen451c8f72022-03-09 13:05:52 +08002041 {
2042 //clear old record status
2043 // struct {
2044 // DVR_WrapperRecordOpenParams_t param_open;
2045 // DVR_RecordStartParams_t param_start;
2046 // DVR_RecordStartParams_t param_update;
2047 // DVR_RecordHandle_t recorder;
2048 // DVR_RecordEventFunction_t event_fn;
2049 // void *event_userdata;
2050
2051 // /*total status = seg_status + status + obsolete*/
2052 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
2053 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
2054 // uint64_t next_segment_id;
2055
2056 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
2057 // } record;
2058 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
2059 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
2060 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2061 }
2062
hualing chen002e5b92022-02-23 17:51:21 +08002063 start_param = &ctx->record.param_start;
2064
2065 error = dvr_record_start_segment(ctx->record.recorder, start_param);
2066 {
2067 DVR_RecordSegmentInfo_t new_seg_info =
2068 { .id = start_param->segment.segment_id, };
2069 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08002070 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 +08002071 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
2072 DVR_RecordStartParams_t *update_param;
2073 update_param = &ctx->record.param_update;
2074 memcpy(update_param, start_param, sizeof(*update_param));
2075 int i = 0;
2076 for (i = 0; i < update_param->segment.nb_pids; i++)
2077 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08002078 }
2079
Wentao MA96f68962022-06-15 19:45:35 +08002080 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08002081
Gong Kefdb31922022-06-17 17:11:16 +08002082 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002083
2084 //start play
Wentao MA96f68962022-06-15 19:45:35 +08002085 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08002086 //clear play statue
2087 ctx = ctx_getPlayback((unsigned long)playback);
2088 if (ctx) {
Wentao MA4d85ff32022-09-23 11:36:18 +08002089 //reset old playback status
hualing chen451c8f72022-03-09 13:05:52 +08002090 // struct {
2091 // DVR_WrapperPlaybackOpenParams_t param_open;
2092 // DVR_PlaybackHandle_t player;
2093 // DVR_PlaybackEventFunction_t event_fn;
2094 // void *event_userdata;
2095
2096 // /*total status = seg_status + status*/
2097 // DVR_PlaybackStatus_t seg_status;
2098 // DVR_WrapperPlaybackStatus_t status;
2099 // DVR_PlaybackPids_t pids_req;
2100 // DVR_PlaybackEvent_t last_event;
2101 // float speed;
2102 // DVR_Bool_t reach_end;
2103
2104 // DVR_WrapperInfo_t obsolete;
2105 // DVR_Bool_t tf_full;
2106 // } playback;
Wentao MA4d85ff32022-09-23 11:36:18 +08002107 ctx->playback.tf_full = DVR_FALSE;
2108 ctx->playback.reach_end = DVR_FALSE;
hualing chen451c8f72022-03-09 13:05:52 +08002109 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2110 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2111 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2112 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2113 }
hualing chen002e5b92022-02-23 17:51:21 +08002114 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2115 return error;
2116}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002117
2118int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2119{
2120 DVR_WrapperCtx_t *ctx;
2121 int error;
2122
2123 DVR_RETURN_IF_FALSE(playback);
2124
2125 ctx = ctx_getPlayback((unsigned long)playback);
2126 DVR_RETURN_IF_FALSE(ctx);
2127
Gong Kefdb31922022-06-17 17:11:16 +08002128 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002129 DVR_WRAPPER_INFO("libdvr_api, stop_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002130 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002131
2132 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2133
2134 {
2135 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002136 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002137
wentao.mafd5283f2022-10-14 09:51:13 +08002138 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002139 // prefetch() here incurring self_assign is used to avoid some compiling
2140 // warnings.
2141 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002142 list_for_each_entry(p_seg, &ctx->segments, head) {
2143 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002144 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002145 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002146 }
2147 ctx_freeSegments(ctx);
2148 }
2149
Wentao MA96f68962022-06-15 19:45:35 +08002150 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002151 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002152
2153 return error;
2154}
2155
2156int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2157{
2158 DVR_WrapperCtx_t *ctx;
2159 int error;
2160
2161 DVR_RETURN_IF_FALSE(playback);
2162
2163 ctx = ctx_getPlayback((unsigned long)playback);
2164 DVR_RETURN_IF_FALSE(ctx);
2165
Gong Kefdb31922022-06-17 17:11:16 +08002166 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002167 DVR_WRAPPER_INFO("libdvr_api, pause_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002168 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002169 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002170 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002171 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002172
2173 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2174
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002175 ctx->playback.speed = 0.0f;
2176
Wentao MA96f68962022-06-15 19:45:35 +08002177 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%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_resume_playback (DVR_WrapperPlayback_t playback)
2184{
2185 DVR_WrapperCtx_t *ctx;
2186 int error;
2187
2188 DVR_RETURN_IF_FALSE(playback);
2189
2190 ctx = ctx_getPlayback((unsigned long)playback);
2191 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002192 //if set limit.we need check if seek to valid data when resume
2193 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2194 if (dvr_playback_check_limit(ctx->playback.player)) {
2195 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2196 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002197 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002198 ctx->sn, time_offset, expired);
2199 time_offset = expired;
2200 dvr_wrapper_seek_playback(playback, time_offset);
2201 }
2202 }
Gong Kefdb31922022-06-17 17:11:16 +08002203 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002204 DVR_WRAPPER_INFO("libdvr_api, resume_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002205 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002206
2207 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002208 ctx->playback.speed = 100.0f;
2209
Wentao MA96f68962022-06-15 19:45:35 +08002210 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002211 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002212
2213 return error;
2214}
2215
2216int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2217{
2218 DVR_WrapperCtx_t *ctx;
2219 int error;
2220 DVR_PlaybackSpeed_t dvr_speed = {
2221 .speed = { speed },
2222 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2223 };
2224
2225 DVR_RETURN_IF_FALSE(playback);
2226
2227 ctx = ctx_getPlayback((unsigned long)playback);
2228 DVR_RETURN_IF_FALSE(ctx);
2229
Gong Kefdb31922022-06-17 17:11:16 +08002230 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002231 DVR_WRAPPER_INFO("libdvr_api, set_playback_speed (sn:%ld) speed:%d", ctx->sn, (int)speed);
Gong Kefdb31922022-06-17 17:11:16 +08002232 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002233
2234 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2235
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002236 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2237 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2238 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002239 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002240 error = dvr_playback_resume(ctx->playback.player);
2241 } else if (ctx->playback.speed == 0.0f
2242 && speed != 0.0f
2243 && speed != 100.0f) {
2244 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002245 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002246 error = dvr_playback_resume(ctx->playback.player);
2247 }
2248
2249 ctx->playback.speed = speed;
2250
Wentao MA96f68962022-06-15 19:45:35 +08002251 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002252 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002253 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002254
2255 return error;
2256}
2257
hualing chen03fd4942021-07-15 15:56:41 +08002258int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2259{
2260 DVR_WrapperCtx_t *ctx;
2261 int error;
2262
2263 DVR_RETURN_IF_FALSE(playback);
2264
2265 ctx = ctx_getPlayback((unsigned long)playback);
2266 DVR_RETURN_IF_FALSE(ctx);
2267
Gong Kefdb31922022-06-17 17:11:16 +08002268 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002269
Wentao MA804bab12022-11-29 10:01:26 +08002270 DVR_WRAPPER_INFO("libdvr_api, setlimit_playback (sn:%ld) time:%lld, limit:%d",
2271 ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002272 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002273
2274 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002275 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002276
Gong Kefdb31922022-06-17 17:11:16 +08002277 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002278
2279 return error;
2280}
2281
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002282int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2283{
2284 DVR_WrapperCtx_t *ctx;
2285 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002286 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Wentao MA804bab12022-11-29 10:01:26 +08002287 uint64_t segment_id = ULLONG_MAX;
2288 uint32_t segment_offset = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002289
2290 DVR_RETURN_IF_FALSE(playback);
2291
2292 ctx = ctx_getPlayback((unsigned long)playback);
2293 DVR_RETURN_IF_FALSE(ctx);
2294
Gong Kefdb31922022-06-17 17:11:16 +08002295 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002296
Wentao MA804bab12022-11-29 10:01:26 +08002297 DVR_WRAPPER_INFO("libdvr_api, seek_playback (sn:%ld) offset:%dms", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002298 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002299
hualing chen03fd4942021-07-15 15:56:41 +08002300 //if set limit info we need check ts data is
2301 //expired when seek
2302 if (dvr_playback_check_limit(ctx->playback.player)) {
2303 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2304 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002305 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002306 ctx->sn, time_offset, expired);
2307 time_offset = expired;
2308 }
2309 }
2310
Wentao MA804bab12022-11-29 10:01:26 +08002311 const uint32_t obsolete_time = (uint32_t)ctx->playback.obsolete.time;
2312 DVR_WrapperPlaybackSegmentInfo_t *p_seg_first = ctx->segments.c_prev;
2313 DVR_WrapperPlaybackSegmentInfo_t *p_seg_last = ctx->segments.c_next;
2314 const uint64_t first_id = p_seg_first->seg_info.id;
2315 const uint64_t last_id = p_seg_last->seg_info.id;
2316 const uint32_t last_duration = p_seg_last->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002317
Wentao MA804bab12022-11-29 10:01:26 +08002318 if (time_offset <= obsolete_time) {
2319 segment_id = first_id;
2320 segment_offset = 0;
2321 DVR_WRAPPER_WARN("time_offset %u isn't greater than obsolete time %u, "
2322 "so seek to beginning position of segment %llu",
2323 time_offset,obsolete_time,segment_id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002324 } else {
Wentao MA804bab12022-11-29 10:01:26 +08002325 uint32_t total_duration = 0;
2326 // This error is suppressed as the macro code is picked from kernel.
2327 // prefetch() here incurring self_assign is used to avoid some compiling
2328 // warnings.
2329 // coverity[self_assign]
2330 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2331 const uint32_t segment_begin = obsolete_time + total_duration;
2332 const uint32_t segment_end = segment_begin + p_seg->seg_info.duration;
2333 if (time_offset >= segment_begin && time_offset <= segment_end) {
2334 segment_id = p_seg->seg_info.id;
2335 segment_offset = time_offset - segment_begin;
2336 break;
2337 }
2338 total_duration += p_seg->seg_info.duration;
2339 }
2340 if (segment_id == ULLONG_MAX) {
2341 segment_id = last_id;
2342 segment_offset = last_duration;
2343 DVR_WRAPPER_WARN("time_offset %u is out of range, so seek to"
2344 " the end position of segment %llu",time_offset,segment_id);
2345 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002346 }
2347
Wentao MA804bab12022-11-29 10:01:26 +08002348 DVR_WRAPPER_INFO("seek playback(sn:%ld) (segment_id:%llu, segment_offset:%u)\n",
2349 ctx->sn, segment_id, segment_offset);
2350 error = dvr_playback_seek(ctx->playback.player, segment_id, segment_offset);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002351
Gong Kefdb31922022-06-17 17:11:16 +08002352 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002353
2354 return error;
2355}
2356
2357int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2358{
2359 DVR_WrapperCtx_t *ctx;
2360 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002361 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002362
2363 DVR_RETURN_IF_FALSE(playback);
2364 DVR_RETURN_IF_FALSE(p_pids);
2365
2366 ctx = ctx_getPlayback((unsigned long)playback);
2367 DVR_RETURN_IF_FALSE(ctx);
2368
Gong Kefdb31922022-06-17 17:11:16 +08002369 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002370
Wentao MA804bab12022-11-29 10:01:26 +08002371 DVR_WRAPPER_INFO("libdvr_api, update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002372 ctx->sn,
2373 p_pids->video.pid, p_pids->video.format,
2374 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002375 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002376
2377 ctx->playback.pids_req = *p_pids;
2378
2379 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002380 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002381 // prefetch() here incurring self_assign is used to avoid some compiling
2382 // warnings.
2383 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002384 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002385 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002386 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2387 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2388 /*check update for pids*/
2389 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2390 p_seg->playback_info.pids = *p_pids;
2391 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002392 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002393 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002394 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002395 /*do not break, let list updated*/
2396 }
2397 }
2398 }
2399 /*break;*/
2400 }
2401 }
2402
Wentao MA96f68962022-06-15 19:45:35 +08002403 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002404 ctx->sn,
2405 p_pids->video.pid, p_pids->video.format,
2406 p_pids->audio.pid, p_pids->audio.format,
2407 error);
2408
Gong Kefdb31922022-06-17 17:11:16 +08002409 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002410
2411 return error;
2412}
2413
hualing chena5f03222021-12-02 11:22:35 +08002414int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2415{
2416 DVR_WrapperCtx_t *ctx;
2417 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002418 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002419
2420 DVR_RETURN_IF_FALSE(playback);
2421 DVR_RETURN_IF_FALSE(p_pids);
2422
2423 ctx = ctx_getPlayback((unsigned long)playback);
2424 DVR_RETURN_IF_FALSE(ctx);
2425
Gong Kefdb31922022-06-17 17:11:16 +08002426 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002427
Wentao MA804bab12022-11-29 10:01:26 +08002428 DVR_WRAPPER_INFO("libdvr_api, only_update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
hualing chena5f03222021-12-02 11:22:35 +08002429 ctx->sn,
2430 p_pids->video.pid, p_pids->video.format,
2431 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002432 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002433
2434 ctx->playback.pids_req = *p_pids;
2435
2436 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002437 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002438 // prefetch() here incurring self_assign is used to avoid some compiling
2439 // warnings.
2440 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002441 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002442 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002443 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2444 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2445 /*check update for pids*/
2446 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2447 p_seg->playback_info.pids = *p_pids;
2448 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002449 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002450 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002451 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002452 /*do not break, let list updated*/
2453 }
2454 }
2455 }
2456 /*break;*/
2457 }
2458 }
2459
Wentao MA96f68962022-06-15 19:45:35 +08002460 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002461 ctx->sn,
2462 p_pids->video.pid, p_pids->video.format,
2463 p_pids->audio.pid, p_pids->audio.format,
2464 error);
2465
Gong Kefdb31922022-06-17 17:11:16 +08002466 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002467
2468 return error;
2469}
2470
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002471int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2472{
2473 DVR_WrapperCtx_t *ctx;
2474 DVR_WrapperPlaybackStatus_t s;
2475 DVR_PlaybackStatus_t play_status;
2476 int error;
2477
2478 DVR_RETURN_IF_FALSE(playback);
2479 DVR_RETURN_IF_FALSE(status);
2480
2481 ctx = ctx_getPlayback((unsigned long)playback);
2482 DVR_RETURN_IF_FALSE(ctx);
2483
Gong Kefdb31922022-06-17 17:11:16 +08002484 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002485
Gong Kefdb31922022-06-17 17:11:16 +08002486 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002487
Wentao MA804bab12022-11-29 10:01:26 +08002488 dvr_playback_get_status(ctx->playback.player, &play_status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002489
2490 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002491 error = process_generatePlaybackStatus(ctx, &s);
2492
hualing chenb5cd42e2020-04-15 17:03:34 +08002493 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2494 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002495 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002496 s.info_cur.time = s.info_full.time;
2497 }
Wentao MA804bab12022-11-29 10:01:26 +08002498 DVR_WRAPPER_INFO("get_playback_status (sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2499 ctx->sn, s.state, s.info_cur.time, s.info_full.time, s.info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002500
2501 *status = s;
2502
Gong Kefdb31922022-06-17 17:11:16 +08002503 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002504
2505 return error;
2506}
2507
hualing chen266b9502020-04-04 17:39:39 +08002508int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2509{
2510 DVR_WrapperCtx_t *ctx;
2511 int error;
2512
2513 DVR_RETURN_IF_FALSE(playback);
2514 DVR_RETURN_IF_FALSE(p_secure_buf);
2515
2516 ctx = ctx_getPlayback((unsigned long)playback);
2517 DVR_RETURN_IF_FALSE(ctx);
2518
Gong Kefdb31922022-06-17 17:11:16 +08002519 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002520 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002521 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002522 return error;
2523}
2524
2525int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2526{
2527 DVR_WrapperCtx_t *ctx;
2528 int error;
2529
2530 DVR_RETURN_IF_FALSE(playback);
2531 DVR_RETURN_IF_FALSE(func);
2532
2533 ctx = ctx_getPlayback((unsigned long)playback);
2534 DVR_RETURN_IF_FALSE(ctx);
2535
Gong Kefdb31922022-06-17 17:11:16 +08002536 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002537 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002538 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002539 return error;
2540}
2541
Zhiqiang Han620b9252021-11-09 14:23:20 +08002542int dvr_wrapper_segment_del_by_location (const char *location)
2543{
2544 char fpath[DVR_MAX_LOCATION_SIZE];
2545
2546 DVR_RETURN_IF_FALSE(location);
2547
2548 /*del the stats file*/
2549 sprintf(fpath, "%s.stats", location);
2550 unlink(fpath);
2551
2552 return dvr_segment_del_by_location(location);
2553}
2554
2555int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2556{
2557 FILE *fp;
2558 char fpath[DVR_MAX_LOCATION_SIZE];
2559
2560 DVR_RETURN_IF_FALSE(location);
2561 DVR_RETURN_IF_FALSE(p_info);
2562
2563 if (p_info)
2564 memset(p_info, 0, sizeof(p_info[0]));
2565
2566 memset(fpath, 0, sizeof(fpath));
2567 sprintf(fpath, "%s.stats", location);
2568
2569 /*stats file exists*/
2570 if ((fp = fopen(fpath, "r"))) {
2571 char buf[256];
2572
2573 if (fgets(buf, sizeof(buf), fp) != NULL
2574 && (sscanf(buf, ":%llu:%lu:%u",
2575 &p_info->size,
2576 &p_info->time,
2577 &p_info->pkts) == 3)) {
2578 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002579 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 +08002580 return DVR_SUCCESS;
2581 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002582 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002583 }
2584
2585 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002586 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002587
2588 int error;
2589 uint32_t n_ids;
2590 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002591
hualing chen8aed9582021-12-24 17:59:56 +08002592 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002593
Zhiqiang Han620b9252021-11-09 14:23:20 +08002594 if (!error) {
2595 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002596 struct list_head info_list; /**< segment list head*/
2597 INIT_LIST_HEAD(&info_list);
2598
2599 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002600 error = dvr_segment_get_allInfo(location, &info_list);
2601 if (error == DVR_FAILURE) {
wentao.maa210e5e2022-10-12 16:10:03 +08002602 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2603 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
hualing chenb9a02922021-12-14 11:29:47 +08002604
2605 memset(&info, 0, sizeof(info));
2606 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002607 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002608 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002609
wentao.maf57dd232022-10-08 16:07:29 +08002610 // Tainted data issue originating from fgets seem false positive, so we
2611 // just suppress it here.
2612 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002613 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002614 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002615 if (!error) {
2616 p_info->size += info.size;
2617 p_info->time += info.duration;
2618 p_info->pkts += info.nb_packets;
2619 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002620 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002621 break;
2622 }
2623 }
2624 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002625 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
wentao.maf57dd232022-10-08 16:07:29 +08002626 // Tainted data issue originating from fgets seem false positive, so we
2627 // just suppress it here.
2628 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002629 for (i = 0; i < n_ids; i++) {
2630
2631 DVR_RecordSegmentInfo_t *seg_info;
2632 DVR_PlaybackSegmentFlag_t flags;
2633 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002634 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002635 // prefetch() here incurring self_assign is used to avoid some compiling
2636 // warnings.
2637 // coverity[self_assign]
hualing chenb9a02922021-12-14 11:29:47 +08002638 list_for_each_entry(seg_info, &info_list, head)
2639 {
hualing chen8aed9582021-12-24 17:59:56 +08002640 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002641 found = 1;
2642 break;
2643 }
2644 }
2645 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002646 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 +08002647 if (p_ids[i] == n_ids - 1) {
wentao.maa210e5e2022-10-12 16:10:03 +08002648 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2649 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
Wentao MA96f68962022-06-15 19:45:35 +08002650 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 +08002651 error = dvr_segment_get_info(location, p_ids[i], &info);
2652 if (!error) {
2653 p_info->size += info.size;
2654 p_info->time += info.duration;
2655 p_info->pkts += info.nb_packets;
2656 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002657 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002658 break;
2659 }
2660 }
hualing chenb9a02922021-12-14 11:29:47 +08002661 continue;
2662 }
2663
2664 if (!error) {
2665 p_info->size += seg_info->size;
2666 p_info->time += seg_info->duration;
2667 p_info->pkts += seg_info->nb_packets;
2668 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002669 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002670 break;
2671 }
2672 }
2673 //free list
2674 DVR_RecordSegmentInfo_t *segment = NULL;
2675 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2676 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2677 {
2678 if (segment) {
2679 list_del(&segment->head);
2680 free(segment);
2681 }
2682 }
2683 }
2684 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002685 } else {
2686 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002687 }
Wentao MA96f68962022-06-15 19:45:35 +08002688 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 +08002689
2690 return (error)? DVR_FAILURE : DVR_SUCCESS;
2691}
2692
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002693static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2694{
wentao.maa210e5e2022-10-12 16:10:03 +08002695 DVR_WrapperEventCtx_t evt = {
2696 .sn = (unsigned long)userdata,
2697 .type = W_REC,
2698 .record.event = event,
2699 .record.status = *(DVR_RecordStatus_t *)params
2700 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002701
2702 DVR_RETURN_IF_FALSE(userdata);
2703
Wentao MA804bab12022-11-29 10:01:26 +08002704 DVR_WRAPPER_DEBUG("record event 0x%x (sn:%ld)", evt.record.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002705 return ctx_addRecordEvent(&evt);
2706}
2707
2708static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2709{
wentao.maa210e5e2022-10-12 16:10:03 +08002710 DVR_WrapperEventCtx_t evt = {
2711 .sn = (unsigned long)userdata,
2712 .type = W_PLAYBACK,
2713 .playback.event = event,
2714 .playback.status = *(DVR_Play_Notify_t *)params
2715 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002716
2717 DVR_RETURN_IF_FALSE(userdata);
2718
2719 evt.sn = (unsigned long)userdata;
2720 evt.type = W_PLAYBACK;
2721 evt.playback.event = event;
2722 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA804bab12022-11-29 10:01:26 +08002723 DVR_WRAPPER_DEBUG("playback event 0x%x (sn:%ld)", evt.playback.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002724 return ctx_addPlaybackEvent(&evt);
2725}
2726
2727static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2728{
Wentao MA270dc0f2022-08-23 13:17:26 +08002729 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 +08002730 ctx->sn,
2731 evt,
2732 status->info.time,
2733 status->info.size,
2734 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002735 status->info_obsolete.time,
2736 status->info_obsolete.size,
2737 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002738
2739 if (ctx->record.event_fn)
2740 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2741 return 0;
2742}
2743
Zhiqiang Han620b9252021-11-09 14:23:20 +08002744static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2745{
2746 FILE *fp;
2747 char fpath[DVR_MAX_LOCATION_SIZE];
2748
2749 DVR_RETURN_IF_FALSE(location);
2750 DVR_RETURN_IF_FALSE(p_status);
2751
2752 sprintf(fpath, "%s.stats", location);
2753
2754 /*stats file*/
2755 if ((fp = fopen(fpath, "w"))) {
2756 char buf[256];
2757 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2758 p_status->info.size - p_status->info_obsolete.size,
2759 p_status->info.time - p_status->info_obsolete.time,
2760 p_status->info.pkts - p_status->info_obsolete.pkts);
2761 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002762 fflush(fp);
2763 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002764 fclose(fp);
2765 return DVR_SUCCESS;
2766 }
2767
2768 return DVR_FAILURE;
2769}
2770
2771
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002772static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2773{
2774 DVR_RecordStartParams_t param;
2775 DVR_RecordSegmentInfo_t seg_info;
2776 int i;
2777 int error;
2778
2779 memcpy(&param, &ctx->record.param_update, sizeof(param));
2780 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2781 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2782 for (i = 0; i < param.segment.nb_pids; i++) {
2783 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2784 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2785 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2786 ctx->record.param_update.segment.nb_pids++;
2787 }
2788 }
2789 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2790 {
2791 DVR_RecordSegmentInfo_t new_seg_info =
2792 { .id = ctx->record.param_update.segment.segment_id, };
2793 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2794 wrapper_addRecordSegment(ctx, &new_seg_info);
2795 }
2796
Wentao MA96f68962022-06-15 19:45:35 +08002797 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002798 return error;
2799}
2800
Wentao MA270dc0f2022-08-23 13:17:26 +08002801static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002802{
Wentao MA270dc0f2022-08-23 13:17:26 +08002803 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002804}
2805
2806/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002807static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002808{
2809 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002810 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002811
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002812 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002813 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2814
2815 ctx->record.status.state = ctx->record.seg_status.state;
2816 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2817 memcpy(ctx->record.status.pids.pids,
2818 ctx->record.seg_status.info.pids,
2819 sizeof(ctx->record.status.pids.pids));
2820 ctx->current_segment_id = ctx->record.seg_status.info.id;
2821
wentao.mafd5283f2022-10-14 09:51:13 +08002822 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002823 // prefetch() here incurring self_assign is used to avoid some compiling
2824 // warnings.
2825 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002826 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2827 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2828 ctx->record.status.info.time += p_seg->info.duration;
2829 ctx->record.status.info.size += p_seg->info.size;
2830 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002831 }
2832 }
2833
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002834 ctx->record.status.info_obsolete = ctx->record.obsolete;
2835
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002836 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002837
2838 if (status) {
2839 *status = ctx->record.status;
2840 status->info.time += ctx->record.seg_status.info.duration;
2841 status->info.size += ctx->record.seg_status.info.size;
2842 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2843 }
2844
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002845 return DVR_SUCCESS;
2846}
2847
2848
2849static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2850{
2851 DVR_WrapperRecordStatus_t status;
2852
2853 memset(&status, 0, sizeof(status));
2854
Wentao MA804bab12022-11-29 10:01:26 +08002855 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d)\n",
2856 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002857 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002858 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002859 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002860 return 0;
2861 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002862 switch (evt->record.event)
2863 {
2864 case DVR_RECORD_EVENT_STATUS:
2865 {
2866 switch (evt->record.status.state)
2867 {
2868 case DVR_RECORD_STATE_OPENED:
2869 case DVR_RECORD_STATE_CLOSED:
2870 {
2871 ctx->record.seg_status = evt->record.status;
2872
2873 status.state = evt->record.status.state;
2874 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002875 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002876 } break;
2877 case DVR_RECORD_STATE_STARTED:
2878 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002879 ctx->record.seg_status = evt->record.status;
2880
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002881 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002882 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002883 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002884
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002885 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002886 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002887 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002888 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 +08002889 ctx->sn,
2890 evt->record.status.info.size,
2891 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002892 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2893 /*should notify the recording's stop*/
2894 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002895 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002896 ctx->sn, error);
2897 status.state = DVR_RECORD_STATE_CLOSED;
2898 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002899 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002900 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002901 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002902
2903 if (ctx->record.param_open.is_timeshift
2904 && ctx->record.param_open.max_time
2905 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002906 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002907
2908 /*as the player do not support null playlist,
2909 there must be one segment existed at any time,
2910 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002911 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2912 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002913 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002914 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 +08002915 status.info.size,
2916 ctx->record.param_open.max_time,
2917 ctx->record.param_open.segment_size);
2918 } else {
2919 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002920 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002921
2922 process_generateRecordStatus(ctx, &status);
2923 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002924 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002925 }
2926 }
2927
Wentao MAf4072032022-06-30 13:50:45 +08002928 const loff_t actual_size = status.info.size;
2929 const loff_t max_size = ctx->record.param_open.max_size;
2930 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002931
Wentao MAf4072032022-06-30 13:50:45 +08002932 if (ctx->record.param_open.is_timeshift && max_size) {
2933 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002934 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002935 /*as the player do not support null playlist,
2936 there must be one segment existed at any time,
2937 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002938 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2939 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002940 /*only one segment, waiting for more*/
2941 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2942 actual_size, segment_size);
2943 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002944 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002945
2946 process_generateRecordStatus(ctx, &status);
2947 process_notifyRecord(ctx, evt->record.event, &status);
2948 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2949 }
2950 if (actual_size >= max_size + segment_size/2) {
2951 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2952 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002953 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002954 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002955 }
2956 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002957 } break;
2958 case DVR_RECORD_STATE_STOPPED:
2959 {
2960 ctx->record.seg_status = evt->record.status;
2961
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002962 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002963 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002964 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002965 } break;
2966 default:
2967 break;
2968 }
2969 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002970 case DVR_RECORD_EVENT_WRITE_ERROR: {
2971 ctx->record.seg_status = evt->record.status;
2972 status.state = evt->record.status.state;
2973 process_notifyRecord(ctx, evt->record.event, &status);
2974 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002975 default:
2976 break;
2977 }
2978 return DVR_SUCCESS;
2979}
2980
2981static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2982{
Wentao MA80179512022-11-03 12:20:03 +08002983 DVR_RETURN_IF_FALSE(ctx->playback.event_fn != NULL);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002984
Wentao MA80179512022-11-03 12:20:03 +08002985 const time_t cur_time = status->info_cur.time;
2986 const time_t full_time = status->info_full.time;
2987 const time_t obsolete_time = status->info_obsolete.time;
2988 const time_t origin_offset = cur_time + obsolete_time;
2989 DVR_WRAPPER_INFO("playback progress notify(sn:%ld) evt(0x%x)"
2990 " actual_slider_pos: %02d:%02d:%02d.%03d/%02d:%02d:%02d.%03d (%7ld ms/%7ld ms),"
2991 " offset_from_origin: %02d:%02d:%02d.%03d (%7ld ms),"
2992 " dump status:state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2993 ctx->sn,evt,cur_time/1000/3600,cur_time/1000%3600/60,cur_time/1000%60,cur_time%1000,
2994 full_time/1000/3600,full_time/1000%3600/60,full_time/1000%60,full_time%1000,
2995 cur_time,full_time,
2996 origin_offset/1000/3600,origin_offset/1000%3600/60,origin_offset/1000%60,origin_offset%1000,
2997 origin_offset,status->state,cur_time,full_time,obsolete_time);
2998
2999 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003000}
3001
3002/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003003static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003004{
3005 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08003006 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003007
3008 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
3009 ctx->playback.status.pids = ctx->playback.pids_req;
3010
3011 ctx->playback.status.state = ctx->playback.seg_status.state;
3012 ctx->playback.status.speed = ctx->playback.seg_status.speed;
3013 ctx->playback.status.flags = ctx->playback.seg_status.flags;
3014 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
3015
wentao.mafd5283f2022-10-14 09:51:13 +08003016 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003017 // prefetch() here incurring self_assign is used to avoid some compiling
3018 // warnings.
3019 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003020 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3021 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003022 break;
hualing chen451c8f72022-03-09 13:05:52 +08003023 }
3024
Wentao MA270dc0f2022-08-23 13:17:26 +08003025 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
3026 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
3027 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003028 }
wentao.mafd5283f2022-10-14 09:51:13 +08003029 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003030 // prefetch() here incurring self_assign is used to avoid some compiling
3031 // warnings.
3032 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003033 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3034 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
3035 ctx->playback.status.info_full.size += p_seg->seg_info.size;
3036 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003037 }
3038
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003039 if (status) {
3040 *status = ctx->playback.status;
3041 /*deal with current, lack size and pkts with the current*/
3042 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08003043 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08003044 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08003045
Wentao MA270dc0f2022-08-23 13:17:26 +08003046 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
3047 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08003048 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
3049 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08003050 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 +08003051 }
3052 else
3053 {
Wentao MA804bab12022-11-29 10:01:26 +08003054 status->info_obsolete.time = ctx->playback.obsolete.time;
3055 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
hualing chen56c0a162022-01-27 17:01:50 +08003056 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003057 }
3058
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003059 return DVR_SUCCESS;
3060}
3061
3062static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3063{
Wentao MA804bab12022-11-29 10:01:26 +08003064 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003065 evt->sn, evt->playback.event,
3066 evt->playback.status.play_status.state,
3067 evt->playback.status.play_status.segment_id,
3068 evt->playback.status.play_status.time_cur,
3069 evt->playback.status.play_status.time_end);
3070
3071 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08003072 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
3073 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
3074 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
3075 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003076 ctx->playback.last_event = evt->playback.event;
3077
3078 switch (evt->playback.event)
3079 {
3080 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
3081 case DVR_PLAYBACK_EVENT_REACHED_END:
3082 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
3083 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08003084 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08003085 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08003086 case DVR_PLAYBACK_EVENT_NODATA:
3087 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003088 {
3089 DVR_WrapperPlaybackStatus_t status;
3090
3091 /*copy status of segment*/
3092 ctx->playback.seg_status = evt->playback.status.play_status;
3093
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003094 /*generate status of the whole playback*/
3095 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003096
3097 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08003098 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08003099 if (ctx->playback.param_open.is_timeshift
3100 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003101 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08003102 }
3103 /*trust the low level, make NO check.
3104 As this evt is changed to only once due to some operations(paused) in low level.
3105 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003106 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08003107 }
3108 */
3109 else {
3110 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08003111 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003112 }
hualing chenf291cf32020-06-18 10:50:30 +08003113 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003114 process_notifyPlayback(ctx, evt->playback.event, &status);
3115 }
3116 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003117 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
3118 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
3119 case DVR_PLAYBACK_EVENT_NO_KEY:
3120 {
Wentao MA96f68962022-06-15 19:45:35 +08003121 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003122 } break;
3123 default:
3124 {
Wentao MA96f68962022-06-15 19:45:35 +08003125 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003126 } break;
3127 }
3128 return 0;
3129}
3130
3131static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3132{
3133 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
3134}
3135
Wentao MA96f68962022-06-15 19:45:35 +08003136int dvr_wrapper_set_log_level (int level)
3137{
Wentao MA804bab12022-11-29 10:01:26 +08003138 DVR_WRAPPER_INFO("libdvr_api, set_log_level %d", level);
Wentao MA96f68962022-06-15 19:45:35 +08003139 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3140 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3141 return DVR_FAILURE;
3142 }
3143 g_dvr_log_level = level;
3144 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3145 return DVR_SUCCESS;
3146}
3147
Wentao MA5629ad82022-08-24 10:03:02 +08003148int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3149{
3150 DVR_WrapperCtx_t *ctx;
3151 int error;
3152
Wentao MA3e2dc452022-12-20 11:17:16 +08003153 DVR_RETURN_IF_FALSE(playback!=NULL);
Wentao MA5629ad82022-08-24 10:03:02 +08003154
3155 ctx = ctx_getPlayback((unsigned long)playback);
3156 DVR_RETURN_IF_FALSE(ctx);
3157
3158 wrapper_mutex_lock(&ctx->wrapper_lock);
3159
3160 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3161
Wentao MA804bab12022-11-29 10:01:26 +08003162 DVR_WRAPPER_INFO("libdvr_api, set_ac4_preselection_id %d", presel_id);
Wentao MA5629ad82022-08-24 10:03:02 +08003163 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3164
3165 wrapper_mutex_unlock(&ctx->wrapper_lock);
3166
3167 return error;
3168}
3169
wentao ma7d642782022-10-23 18:26:16 -07003170int dvr_wrapper_property_set(const char* prop_name, const char* prop_value)
3171{
3172 return dvr_prop_write(prop_name,prop_value);
3173}
3174
3175int dvr_wrapper_property_get(const char* prop_name, char* prop_value, int length)
3176{
3177 return dvr_prop_read(prop_name,prop_value,length);
3178}
3179