blob: 197bbb11273218e3997c2ce0c86255d888d5bb77 [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;
Zhiqiang Han02e890c2023-04-25 14:55:02 +08001238
1239 if (params->save_rec_file != 0) {
Zhiqiang Han89c77972022-12-08 12:16:39 +08001240 uint32_t segment_nb = 0;
1241 uint64_t *p_segment_ids = NULL;
1242 error = dvr_segment_get_list(ctx->record.param_open.location, &segment_nb, &p_segment_ids);
1243 if (error == DVR_SUCCESS && segment_nb>0) {
1244 // Tainted data issue originating from fgets seem false positive, so we
1245 // just suppress it here.
1246 // coverity[tainted_data]
1247 new_segment_id = p_segment_ids[segment_nb-1]+1;
1248 }
1249 if (p_segment_ids != NULL) {
1250 free(p_segment_ids);
1251 }
Zhiqiang Han89c77972022-12-08 12:16:39 +08001252 }
Zhiqiang Han02e890c2023-04-25 14:55:02 +08001253 DVR_WRAPPER_DEBUG("new_segment_id:%lld\n", new_segment_id);
1254
Zhiqiang Han89c77972022-12-08 12:16:39 +08001255 ctx->record.next_segment_id = new_segment_id;
1256 ctx->current_segment_id = new_segment_id;
1257
1258 start_param->segment.segment_id = ctx->record.next_segment_id++;
1259
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001260 {
1261 /*sync to update for further use*/
1262 DVR_RecordStartParams_t *update_param;
1263 update_param = &ctx->record.param_update;
1264 memcpy(update_param, start_param, sizeof(*update_param));
1265 for (i = 0; i < update_param->segment.nb_pids; i++)
1266 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1267 }
1268
1269 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1270 {
1271 DVR_RecordSegmentInfo_t new_seg_info =
1272 { .id = start_param->segment.segment_id, };
1273 wrapper_addRecordSegment(ctx, &new_seg_info);
1274 }
1275
Wentao MA96f68962022-06-15 19:45:35 +08001276 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001277
Gong Kefdb31922022-06-17 17:11:16 +08001278 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001279
1280 return error;
1281}
1282
1283int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1284{
1285 DVR_WrapperCtx_t *ctx;
1286 DVR_RecordSegmentInfo_t seg_info;
1287 int error;
1288
1289 DVR_RETURN_IF_FALSE(rec);
1290
1291 ctx = ctx_getRecord((unsigned long)rec);
1292 DVR_RETURN_IF_FALSE(ctx);
1293
Gong Kefdb31922022-06-17 17:11:16 +08001294 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001295 DVR_WRAPPER_INFO("libdvr_api, stop_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001296 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001297
1298 memset(&seg_info, 0, sizeof(seg_info));
1299 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1300 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1301
Wentao MAd71e2062023-02-15 10:10:49 +08001302 ctx_freeSegments(ctx);
1303
Wentao MA96f68962022-06-15 19:45:35 +08001304 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001305 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001306
1307 return error;
1308}
1309
hualing chen03fd4942021-07-15 15:56:41 +08001310int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1311{
1312 DVR_WrapperCtx_t *ctx;
1313 int error;
1314
1315 DVR_RETURN_IF_FALSE(rec);
1316
1317 ctx = ctx_getRecord((unsigned long)rec);
1318 DVR_RETURN_IF_FALSE(ctx);
1319
Gong Kefdb31922022-06-17 17:11:16 +08001320 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001321 DVR_WRAPPER_INFO("libdvr_api, pause_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001322 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001323
1324 error = dvr_record_pause(ctx->record.recorder);
1325
Wentao MA96f68962022-06-15 19:45:35 +08001326 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001327 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001328
1329 return error;
1330}
1331
1332int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1333{
1334 DVR_WrapperCtx_t *ctx;
1335 int error;
1336
1337 DVR_RETURN_IF_FALSE(rec);
1338
1339 ctx = ctx_getRecord((unsigned long)rec);
1340 DVR_RETURN_IF_FALSE(ctx);
1341
Gong Kefdb31922022-06-17 17:11:16 +08001342 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001343 DVR_WRAPPER_INFO("libdvr_api, resume_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001344 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001345
1346 error = dvr_record_resume(ctx->record.recorder);
1347
Wentao MA96f68962022-06-15 19:45:35 +08001348 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001349 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001350
1351 return error;
1352}
1353
Wentao MAcdea4762022-04-26 13:28:56 +08001354/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001355static DVR_Bool_t pids_test_include(
1356 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1357 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001358{
Wentao MAcdea4762022-04-26 13:28:56 +08001359 DVR_Bool_t ret = DVR_TRUE;
1360 for (int i=0;i<size2;i++)
1361 { // iterate all elements in arr2 to check if they exist in arr1
1362 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001363
1364 if (act2[i] == DVR_RECORD_PID_CLOSE)
1365 continue;
1366
Wentao MAcdea4762022-04-26 13:28:56 +08001367 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001368 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001369 if (act1[j] != DVR_RECORD_PID_CLOSE
1370 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001371 {
1372 found=DVR_TRUE;
1373 break;
1374 }
1375 }
1376 if (found == DVR_FALSE)
1377 {
Wentao MAcdea4762022-04-26 13:28:56 +08001378 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001379 break;
1380 }
1381 }
Wentao MAcdea4762022-04-26 13:28:56 +08001382 return ret;
1383}
1384
1385static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1386 const DVR_WrapperUpdatePidsParams_t* p2)
1387{
1388 int i=0;
1389 char buf[128]={0};
1390 int cnt=0;
1391 int chars=0;
1392
1393 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1394 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1395
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001396 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1397 p2->pids,p2->pid_action,p2->nb_pids);
1398 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1399 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001400 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001401 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001402
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001403 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001404 for (i=0;i<p1->nb_pids;i++)
1405 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001406 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1407 removed++;
1408 continue;
1409 }
Wentao MAcdea4762022-04-26 13:28:56 +08001410 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1411 if (chars<0)
1412 {
1413 break;
1414 }
1415 cnt += chars;
1416 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001417 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001418 memset(buf,0,sizeof(buf));
1419
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001420 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001421 for (i=0,cnt=0;i<p2->nb_pids;i++)
1422 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001423 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1424 removed++;
1425 continue;
1426 }
Wentao MAcdea4762022-04-26 13:28:56 +08001427 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1428 if (chars<0)
1429 {
1430 break;
1431 }
1432 cnt += chars;
1433 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001434 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001435 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001436 return is_equal;
1437}
1438
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001439int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1440{
1441 DVR_WrapperCtx_t *ctx;
1442 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001443 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001444 int i;
1445 int error;
1446
1447 DVR_RETURN_IF_FALSE(rec);
1448 DVR_RETURN_IF_FALSE(params);
1449
1450 ctx = ctx_getRecord((unsigned long)rec);
1451 DVR_RETURN_IF_FALSE(ctx);
1452
Gong Kefdb31922022-06-17 17:11:16 +08001453 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001454 DVR_WRAPPER_INFO("libdvr_api, update_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001455 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001456
1457 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001458 if (pids_equal(&(start_param->segment),params))
1459 {
Gong Kefdb31922022-06-17 17:11:16 +08001460 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001461 return DVR_TRUE;
1462 }
1463
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001464 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001465 const int len = strlen(ctx->record.param_open.location);
1466 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1467 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1468 pthread_mutex_unlock(&ctx->wrapper_lock);
1469 return DVR_FAILURE;
1470 }
1471 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001472 start_param->segment.segment_id = ctx->record.next_segment_id++;
1473 start_param->segment.nb_pids = params->nb_pids;
1474 for (i = 0; i < params->nb_pids; i++) {
1475 start_param->segment.pids[i] = params->pids[i];
1476 start_param->segment.pid_action[i] = params->pid_action[i];
1477 }
1478 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1479 {
1480 DVR_RecordSegmentInfo_t new_seg_info =
1481 { .id = start_param->segment.segment_id, };
1482 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1483 wrapper_addRecordSegment(ctx, &new_seg_info);
1484 }
1485
Wentao MA96f68962022-06-15 19:45:35 +08001486 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001487 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001488
1489 return error;
1490}
1491
1492int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1493{
1494 DVR_WrapperCtx_t *ctx;
1495 DVR_WrapperRecordStatus_t s;
1496 int error;
1497
1498 DVR_RETURN_IF_FALSE(rec);
1499 DVR_RETURN_IF_FALSE(status);
1500
1501 ctx = ctx_getRecord((unsigned long)rec);
1502 DVR_RETURN_IF_FALSE(ctx);
1503
Gong Kefdb31922022-06-17 17:11:16 +08001504 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001505
Wentao MA804bab12022-11-29 10:01:26 +08001506 DVR_WRAPPER_INFO("libdvr_api, get_record_status (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001507 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001508
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001509 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001510
Wentao MA96f68962022-06-15 19:45:35 +08001511 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001512 ctx->sn,
1513 s.state,
1514 s.info.time,
1515 s.info.size,
1516 s.info.pkts,
1517 error);
1518
1519 *status = s;
1520
Gong Kefdb31922022-06-17 17:11:16 +08001521 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001522
1523 return error;
1524}
1525
hualing chen4fe3bee2020-10-23 13:58:52 +08001526int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1527{
1528 DVR_WrapperCtx_t *ctx;
1529 int error;
1530
1531 DVR_RETURN_IF_FALSE(rec);
1532
1533 ctx = ctx_getRecord((unsigned long)rec);
1534 DVR_RETURN_IF_FALSE(ctx);
1535
Gong Kefdb31922022-06-17 17:11:16 +08001536 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001537 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001538 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001539 return error;
1540}
1541
hualing chen266b9502020-04-04 17:39:39 +08001542int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1543{
1544 DVR_WrapperCtx_t *ctx;
1545 int error;
1546
1547 DVR_RETURN_IF_FALSE(rec);
1548 DVR_RETURN_IF_FALSE(p_secure_buf);
1549
1550 ctx = ctx_getRecord((unsigned long)rec);
1551 DVR_RETURN_IF_FALSE(ctx);
1552
Gong Kefdb31922022-06-17 17:11:16 +08001553 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001554 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001555 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001556 return error;
1557}
1558
1559int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1560{
1561 DVR_WrapperCtx_t *ctx;
1562 int error;
1563
1564 DVR_RETURN_IF_FALSE(rec);
1565 DVR_RETURN_IF_FALSE(func);
1566
1567 ctx = ctx_getRecord((unsigned long)rec);
1568 DVR_RETURN_IF_FALSE(ctx);
1569
Gong Kefdb31922022-06-17 17:11:16 +08001570 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001571 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001572 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001573 return error;
1574}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001575
1576
1577int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1578{
1579 DVR_WrapperCtx_t *ctx;
1580 DVR_PlaybackOpenParams_t open_param;
1581 int error;
1582
1583 DVR_RETURN_IF_FALSE(playback);
1584 DVR_RETURN_IF_FALSE(params);
1585 DVR_RETURN_IF_FALSE(params->playback_handle);
1586
1587 /*get a free ctx*/
1588 ctx = ctx_getPlayback(0);
1589 DVR_RETURN_IF_FALSE(ctx);
1590
Gong Kefdb31922022-06-17 17:11:16 +08001591 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001592
Wentao MA804bab12022-11-29 10:01:26 +08001593 DVR_WRAPPER_INFO("libdvr_api, open_playback (dmx:%d) ..vendor[%d]params->block_size[%d].",
1594 params->dmx_dev_id, params->vendor, params->block_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001595
1596 ctx_reset(ctx);
1597
1598 ctx->playback.param_open = *params;
1599 ctx->playback.event_fn = params->event_fn;
1600 ctx->playback.event_userdata = params->event_userdata;
1601 ctx->current_segment_id = 0;
1602 INIT_LIST_HEAD(&ctx->segments);
1603 ctx->sn = get_sn();
1604
1605 wrapper_requestThreadFor(ctx);
1606
hualing chen266b9502020-04-04 17:39:39 +08001607 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001608 open_param.dmx_dev_id = params->dmx_dev_id;
1609 open_param.block_size = params->block_size;
1610 open_param.is_timeshift = params->is_timeshift;
1611 //open_param.notification_size = 10*1024; //not supported
1612 open_param.event_fn = wrapper_playback_event_handler;
1613 open_param.event_userdata = (void*)ctx->sn;
1614 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001615 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001616 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001617 open_param.vendor = params->vendor;
1618
Yahui Han1fbf3292021-11-08 18:17:19 +08001619 if (params->keylen) {
1620 open_param.clearkey = params->clearkey;
1621 open_param.cleariv = params->cleariv;
1622 open_param.keylen = params->keylen;
1623 }
shenghui.gengbec6a462023-01-12 15:21:02 +08001624 open_param.control_speed_enable = params->control_speed_enable;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001625
1626 error = dvr_playback_open(&ctx->playback.player, &open_param);
1627 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001628 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001629 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001630 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001631 wrapper_releaseThreadForType(ctx->type);
1632 return DVR_FAILURE;
1633 }
1634 if (params->is_timeshift)
1635 sn_timeshift_playback = ctx->sn;
1636
Wentao MA270dc0f2022-08-23 13:17:26 +08001637 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001638 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1639 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001640 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001641 }
Gong Kefdb31922022-06-17 17:11:16 +08001642 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001643
1644 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1645 return DVR_SUCCESS;
1646}
1647
1648int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1649{
1650 DVR_WrapperCtx_t *ctx;
1651 int error;
1652
1653 DVR_RETURN_IF_FALSE(playback);
1654
1655 ctx = ctx_getPlayback((unsigned long)playback);
1656 DVR_RETURN_IF_FALSE(ctx);
1657
Gong Kefdb31922022-06-17 17:11:16 +08001658 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001659 DVR_WRAPPER_INFO("libdvr_api, close_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001660 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001661
1662 if (ctx->playback.param_open.is_timeshift)
1663 sn_timeshift_playback = 0;
1664
1665 /*try stop first*/
wentao.maa210e5e2022-10-12 16:10:03 +08001666 dvr_playback_stop(ctx->playback.player, DVR_TRUE);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001667
1668 {
1669 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001670 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001671
wentao.mafd5283f2022-10-14 09:51:13 +08001672 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001673 // prefetch() here incurring self_assign is used to avoid some compiling
1674 // warnings.
1675 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08001676 list_for_each_entry(p_seg, &ctx->segments, head) {
1677 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001678 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001679 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001680 }
1681 ctx_freeSegments(ctx);
1682 }
1683
1684 error = dvr_playback_close(ctx->playback.player);
1685
Wentao MA96f68962022-06-15 19:45:35 +08001686 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001687 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001688 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001689
1690 wrapper_releaseThreadForType(ctx->type);
1691
1692 return error;
1693}
1694
1695int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1696{
1697 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001698 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001699 uint64_t *p_segment_ids;
1700 uint32_t segment_nb;
1701 uint32_t i;
1702 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001703 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001704 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001705 DVR_Bool_t is_timeshift = DVR_FALSE;
Wentao MAcefc13c2022-10-26 15:47:24 +08001706 DVR_PlaybackSegmentFlag_t seg_flags = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001707
1708 DVR_RETURN_IF_FALSE(playback);
1709 DVR_RETURN_IF_FALSE(p_pids);
1710
hualing chenc110f952021-01-18 11:25:37 +08001711 ctx_record = NULL;
1712
1713 /*lock the recorder to avoid changing the recording segments*/
1714 ctx_record = ctx_getRecord(sn_timeshift_record);
1715
1716 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001717 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001718 if (!ctx_valid(ctx_record)
1719 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001720 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001721 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001722 is_timeshift = DVR_FALSE;
1723 } else {
1724 is_timeshift = DVR_TRUE;
1725 }
1726 }
1727
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001728 ctx = ctx_getPlayback((unsigned long)playback);
1729 DVR_RETURN_IF_FALSE(ctx);
1730
Gong Kefdb31922022-06-17 17:11:16 +08001731 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001732
Wentao MA804bab12022-11-29 10:01:26 +08001733 DVR_WRAPPER_INFO("libdvr_api, start_playback (sn:%ld) location:%s"
1734 " flags:0x%x v/a/ad/sub/pcr(%d:%d %d:%d %d:%d %d:%d %d)",
1735 ctx->sn,
1736 ctx->playback.param_open.location,
1737 flags,
1738 p_pids->video.pid, p_pids->video.format,
1739 p_pids->audio.pid, p_pids->audio.format,
1740 p_pids->ad.pid, p_pids->ad.format,
1741 p_pids->subtitle.pid, p_pids->subtitle.format,
1742 p_pids->pcr.pid);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001743
Gong Kefdb31922022-06-17 17:11:16 +08001744 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001745
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001746 if (ctx->playback.param_open.is_timeshift) {
1747 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001748 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001749 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001750 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001751 return DVR_FAILURE;
1752 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001753 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001754 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001755 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001756 }
1757
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001758 /*obtain all segments in a list*/
1759 segment_nb = 0;
1760 p_segment_ids = NULL;
1761 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001762 if (!error) {
1763 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001764 struct list_head info_list; /**< segment list head*/
1765 INIT_LIST_HEAD(&info_list);
1766
Wentao MA96f68962022-06-15 19:45:35 +08001767 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001768 //we need free info list buf when we used end.
1769 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001770 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001771 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001772 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1773 ctx->playback.param_open.location, error);
wentao.maf57dd232022-10-08 16:07:29 +08001774 // Tainted data issue originating from fgets seem false positive, so we
1775 // just suppress it here.
1776 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001777 for (i = 0; i < segment_nb; i++) {
1778 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001779 memset((void*)&seg_info,0,sizeof(seg_info));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001780
hualing chenb9a02922021-12-14 11:29:47 +08001781 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1782 if (error) {
1783 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001784 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001785 ctx->playback.param_open.location, p_segment_ids[i], error);
1786 break;
1787 }
1788 //add check if has audio or video pid. if not exist. not add segment to playback
1789 int ii = 0;
1790 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001791 // Tainted data issue originating from fgets seem false positive, so we
1792 // just suppress it here.
1793 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001794 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1795 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1796 if (type == DVR_STREAM_TYPE_VIDEO ||
1797 type == DVR_STREAM_TYPE_AUDIO ||
1798 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001799 DVR_WRAPPER_INFO("success to get seg av info \n");
1800 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 +08001801 DVR_STREAM_TYPE_VIDEO,
1802 DVR_STREAM_TYPE_AUDIO,
1803 DVR_STREAM_TYPE_AD);
1804 has_av = 1;
1805 //break;
1806 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001807 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 +08001808 DVR_STREAM_TYPE_VIDEO,
1809 DVR_STREAM_TYPE_AUDIO,
1810 DVR_STREAM_TYPE_AD);
1811 }
1812 }
1813 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001814 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001815 continue;
1816 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001817 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001818 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001819 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1820 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001821 if (error == DVR_FAILURE) {
1822 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001823 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001824 }
hualing chenb9a02922021-12-14 11:29:47 +08001825 /*copy the 1st segment*/
1826 if (got_1st_seg == 0) {
1827 seg_info_1st = seg_info;
1828 got_1st_seg = 1;
1829 }
1830 }
1831 } else {
wentao.maf57dd232022-10-08 16:07:29 +08001832 // Tainted data issue originating from fgets seem false positive, so we
1833 // just suppress it here.
1834 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001835 for (i = 0; i < segment_nb; i++) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001836 DVR_RecordSegmentInfo_t *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001837 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08001838 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001839 // prefetch() here incurring self_assign is used to avoid some compiling
1840 // warnings.
1841 // coverity[self_assign]
Wentao MA4d85ff32022-09-23 11:36:18 +08001842 list_for_each_entry(p_seg_info, &info_list, head)
hualing chenb9a02922021-12-14 11:29:47 +08001843 {
Wentao MA4d85ff32022-09-23 11:36:18 +08001844 if (p_seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001845 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001846 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001847 break;
1848 }
1849 }
1850 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001851 //last info is not found if when recording occured power off.
1852 if (p_segment_ids[i] == segment_nb - 1) {
1853 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001854 memset((void*)&seg_info,0,sizeof(seg_info));
hualing chen8aed9582021-12-24 17:59:56 +08001855
1856 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1857 if (error) {
1858 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001859 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001860 ctx->playback.param_open.location, p_segment_ids[i], error);
1861 break;
1862 }
1863 //
1864 //add check if has audio or video pid. if not exist. not add segment to playback
1865 int ii = 0;
1866 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001867 // Tainted data issue originating from fgets seem false positive, so we
1868 // just suppress it here.
1869 // coverity[tainted_data]
hualing chen8aed9582021-12-24 17:59:56 +08001870 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1871 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1872 if (type == DVR_STREAM_TYPE_VIDEO ||
1873 type == DVR_STREAM_TYPE_AUDIO ||
1874 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001875 DVR_WRAPPER_INFO("success to get seg av info \n");
1876 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 +08001877 DVR_STREAM_TYPE_VIDEO,
1878 DVR_STREAM_TYPE_AUDIO,
1879 DVR_STREAM_TYPE_AD);
1880 has_av = 1;
1881 //break;
1882 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001883 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 +08001884 DVR_STREAM_TYPE_VIDEO,
1885 DVR_STREAM_TYPE_AUDIO,
1886 DVR_STREAM_TYPE_AD);
1887 }
1888 }
1889 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001890 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001891 continue;
1892 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001893 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001894 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001895 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1896 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001897 if (error == DVR_FAILURE) {
1898 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chen8aed9582021-12-24 17:59:56 +08001899 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001900 }
hualing chen8aed9582021-12-24 17:59:56 +08001901 }
hualing chenb9a02922021-12-14 11:29:47 +08001902 continue;
1903 }
1904
1905 //add check if has audio or video pid. if not exist. not add segment to playback
1906 int ii = 0;
1907 int has_av = 0;
Wentao MA4d85ff32022-09-23 11:36:18 +08001908 for (ii = 0; ii < p_seg_info->nb_pids; ii++) {
1909 int type = (p_seg_info->pids[ii].type >> 24) & 0x0f;
hualing chenb9a02922021-12-14 11:29:47 +08001910 if (type == DVR_STREAM_TYPE_VIDEO ||
1911 type == DVR_STREAM_TYPE_AUDIO ||
1912 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001913 DVR_WRAPPER_INFO("success to get seg av info \n");
Wentao MA4d85ff32022-09-23 11:36:18 +08001914 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 +08001915 DVR_STREAM_TYPE_VIDEO,
1916 DVR_STREAM_TYPE_AUDIO,
1917 DVR_STREAM_TYPE_AD);
1918 has_av = 1;
1919 //break;
1920 } else {
Wentao MA4d85ff32022-09-23 11:36:18 +08001921 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 +08001922 DVR_STREAM_TYPE_VIDEO,
1923 DVR_STREAM_TYPE_AUDIO,
1924 DVR_STREAM_TYPE_AD);
1925 }
1926 }
1927 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001928 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001929 continue;
1930 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001931 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001932 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001933 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1934 error = wrapper_addPlaybackSegment(ctx, p_seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001935 if (error == DVR_FAILURE) {
1936 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001937 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001938 }
hualing chenb9a02922021-12-14 11:29:47 +08001939
1940 /*copy the 1st segment*/
1941 if (got_1st_seg == 0) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001942 seg_info_1st = *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001943 got_1st_seg = 1;
1944 }
hualing chen92f3a142020-07-08 20:59:33 +08001945 }
hualing chenb9a02922021-12-14 11:29:47 +08001946 //free list
1947 DVR_RecordSegmentInfo_t *segment = NULL;
1948 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1949 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1950 {
1951 if (segment) {
1952 list_del(&segment->head);
1953 free(segment);
1954 }
1955 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001956 }
hualing chenb9a02922021-12-14 11:29:47 +08001957
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001958 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001959
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001960 /* return if no segment or fail to add */
1961 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001962
Wentao MA270dc0f2022-08-23 13:17:26 +08001963 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001964 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1965 ctx->playback.obsolete = ctx_record->record.obsolete;
1966 }
1967
Wentao MA96f68962022-06-15 19:45:35 +08001968 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001969
1970 ctx->playback.reach_end = DVR_FALSE;
1971 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1972 ctx->playback.speed = 0.0f;
1973 else
1974 ctx->playback.speed = 100.0f;
1975
1976 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001977 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001978 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001979 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001980 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001981 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001982 error = dvr_playback_start(ctx->playback.player, flags);
1983 } else {
wentao.maa210e5e2022-10-12 16:10:03 +08001984 dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
hualing chen03fd4942021-07-15 15:56:41 +08001985 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001986 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001987 ctx->sn, seg_info_1st.id, error);
1988 }
Wentao MA96f68962022-06-15 19:45:35 +08001989 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001990 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001991 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 +08001992 }
1993 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001994
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001995 if (ctx->playback.param_open.is_timeshift) {
1996 /*unlock the recorder locked above*/
1997 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001998 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001999 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002000 ctx->sn, ctx_record->sn);
2001 }
2002 }
Gong Kefdb31922022-06-17 17:11:16 +08002003 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002004
2005 return error;
2006}
hualing chen002e5b92022-02-23 17:51:21 +08002007//stop record and playback
2008int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
2009{
2010 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
2011 int error;
Wentao MA804bab12022-11-29 10:01:26 +08002012 DVR_WRAPPER_INFO("libdvr_api, stop_timeshift");
hualing chen002e5b92022-02-23 17:51:21 +08002013
2014 //stop timeshift record
2015 ctx_record = ctx_getRecord(sn_timeshift_record);
wentao.maa210e5e2022-10-12 16:10:03 +08002016 dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
hualing chen002e5b92022-02-23 17:51:21 +08002017
Wentao MA96f68962022-06-15 19:45:35 +08002018 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08002019 //stop play
2020 error = dvr_wrapper_stop_playback(playback);
2021 //del timeshift file
2022 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002023 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08002024 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
2025 }
2026 return error;
2027}
2028//start record and start playback
2029int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
2030{
2031 DVR_WrapperCtx_t *ctx;
2032 DVR_RecordStartParams_t *start_param;
2033 int error;
2034
2035 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
2036 DVR_RETURN_IF_FALSE(ctx);
2037
Gong Kefdb31922022-06-17 17:11:16 +08002038 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002039 DVR_WRAPPER_INFO("libdvr_api, restart_timeshift (sn:%ld) location:%s",
2040 ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08002041 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002042
hualing chen451c8f72022-03-09 13:05:52 +08002043 {
2044 //clear old record status
2045 // struct {
2046 // DVR_WrapperRecordOpenParams_t param_open;
2047 // DVR_RecordStartParams_t param_start;
2048 // DVR_RecordStartParams_t param_update;
2049 // DVR_RecordHandle_t recorder;
2050 // DVR_RecordEventFunction_t event_fn;
2051 // void *event_userdata;
2052
2053 // /*total status = seg_status + status + obsolete*/
2054 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
2055 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
2056 // uint64_t next_segment_id;
2057
2058 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
2059 // } record;
2060 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
2061 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
2062 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2063 }
2064
hualing chen002e5b92022-02-23 17:51:21 +08002065 start_param = &ctx->record.param_start;
2066
2067 error = dvr_record_start_segment(ctx->record.recorder, start_param);
2068 {
2069 DVR_RecordSegmentInfo_t new_seg_info =
2070 { .id = start_param->segment.segment_id, };
2071 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08002072 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 +08002073 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
2074 DVR_RecordStartParams_t *update_param;
2075 update_param = &ctx->record.param_update;
2076 memcpy(update_param, start_param, sizeof(*update_param));
2077 int i = 0;
2078 for (i = 0; i < update_param->segment.nb_pids; i++)
2079 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08002080 }
2081
Wentao MA96f68962022-06-15 19:45:35 +08002082 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08002083
Gong Kefdb31922022-06-17 17:11:16 +08002084 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002085
2086 //start play
Wentao MA96f68962022-06-15 19:45:35 +08002087 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08002088 //clear play statue
2089 ctx = ctx_getPlayback((unsigned long)playback);
2090 if (ctx) {
Wentao MA4d85ff32022-09-23 11:36:18 +08002091 //reset old playback status
hualing chen451c8f72022-03-09 13:05:52 +08002092 // struct {
2093 // DVR_WrapperPlaybackOpenParams_t param_open;
2094 // DVR_PlaybackHandle_t player;
2095 // DVR_PlaybackEventFunction_t event_fn;
2096 // void *event_userdata;
2097
2098 // /*total status = seg_status + status*/
2099 // DVR_PlaybackStatus_t seg_status;
2100 // DVR_WrapperPlaybackStatus_t status;
2101 // DVR_PlaybackPids_t pids_req;
2102 // DVR_PlaybackEvent_t last_event;
2103 // float speed;
2104 // DVR_Bool_t reach_end;
2105
2106 // DVR_WrapperInfo_t obsolete;
2107 // DVR_Bool_t tf_full;
2108 // } playback;
Wentao MA4d85ff32022-09-23 11:36:18 +08002109 ctx->playback.tf_full = DVR_FALSE;
2110 ctx->playback.reach_end = DVR_FALSE;
hualing chen451c8f72022-03-09 13:05:52 +08002111 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2112 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2113 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2114 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2115 }
hualing chen002e5b92022-02-23 17:51:21 +08002116 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2117 return error;
2118}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002119
2120int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2121{
2122 DVR_WrapperCtx_t *ctx;
2123 int error;
2124
2125 DVR_RETURN_IF_FALSE(playback);
2126
2127 ctx = ctx_getPlayback((unsigned long)playback);
2128 DVR_RETURN_IF_FALSE(ctx);
2129
Gong Kefdb31922022-06-17 17:11:16 +08002130 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002131 DVR_WRAPPER_INFO("libdvr_api, stop_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002132 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002133
2134 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2135
2136 {
2137 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002138 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002139
wentao.mafd5283f2022-10-14 09:51:13 +08002140 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002141 // prefetch() here incurring self_assign is used to avoid some compiling
2142 // warnings.
2143 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002144 list_for_each_entry(p_seg, &ctx->segments, head) {
2145 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002146 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002147 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002148 }
2149 ctx_freeSegments(ctx);
2150 }
2151
Wentao MA96f68962022-06-15 19:45:35 +08002152 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002153 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002154
2155 return error;
2156}
2157
2158int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2159{
2160 DVR_WrapperCtx_t *ctx;
2161 int error;
2162
2163 DVR_RETURN_IF_FALSE(playback);
2164
2165 ctx = ctx_getPlayback((unsigned long)playback);
2166 DVR_RETURN_IF_FALSE(ctx);
2167
Gong Kefdb31922022-06-17 17:11:16 +08002168 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002169 DVR_WRAPPER_INFO("libdvr_api, pause_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002170 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002171 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002172 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002173 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002174
2175 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2176
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002177 ctx->playback.speed = 0.0f;
2178
Wentao MA96f68962022-06-15 19:45:35 +08002179 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002180 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002181
2182 return error;
2183}
2184
2185int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2186{
2187 DVR_WrapperCtx_t *ctx;
2188 int error;
2189
2190 DVR_RETURN_IF_FALSE(playback);
2191
2192 ctx = ctx_getPlayback((unsigned long)playback);
2193 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002194 //if set limit.we need check if seek to valid data when resume
2195 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2196 if (dvr_playback_check_limit(ctx->playback.player)) {
2197 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2198 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002199 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002200 ctx->sn, time_offset, expired);
2201 time_offset = expired;
2202 dvr_wrapper_seek_playback(playback, time_offset);
2203 }
2204 }
Gong Kefdb31922022-06-17 17:11:16 +08002205 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002206 DVR_WRAPPER_INFO("libdvr_api, resume_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002207 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002208
2209 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002210 ctx->playback.speed = 100.0f;
2211
Wentao MA96f68962022-06-15 19:45:35 +08002212 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002213 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002214
2215 return error;
2216}
2217
2218int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2219{
2220 DVR_WrapperCtx_t *ctx;
2221 int error;
2222 DVR_PlaybackSpeed_t dvr_speed = {
2223 .speed = { speed },
2224 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2225 };
2226
2227 DVR_RETURN_IF_FALSE(playback);
2228
2229 ctx = ctx_getPlayback((unsigned long)playback);
2230 DVR_RETURN_IF_FALSE(ctx);
2231
Gong Kefdb31922022-06-17 17:11:16 +08002232 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002233 DVR_WRAPPER_INFO("libdvr_api, set_playback_speed (sn:%ld) speed:%d", ctx->sn, (int)speed);
Gong Kefdb31922022-06-17 17:11:16 +08002234 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002235
2236 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2237
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002238 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2239 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2240 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002241 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002242 error = dvr_playback_resume(ctx->playback.player);
2243 } else if (ctx->playback.speed == 0.0f
2244 && speed != 0.0f
2245 && speed != 100.0f) {
2246 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002247 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002248 error = dvr_playback_resume(ctx->playback.player);
2249 }
2250
2251 ctx->playback.speed = speed;
2252
Wentao MA96f68962022-06-15 19:45:35 +08002253 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002254 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002255 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002256
2257 return error;
2258}
2259
hualing chen03fd4942021-07-15 15:56:41 +08002260int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2261{
2262 DVR_WrapperCtx_t *ctx;
2263 int error;
2264
2265 DVR_RETURN_IF_FALSE(playback);
2266
2267 ctx = ctx_getPlayback((unsigned long)playback);
2268 DVR_RETURN_IF_FALSE(ctx);
2269
Gong Kefdb31922022-06-17 17:11:16 +08002270 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002271
Wentao MA804bab12022-11-29 10:01:26 +08002272 DVR_WRAPPER_INFO("libdvr_api, setlimit_playback (sn:%ld) time:%lld, limit:%d",
2273 ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002274 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002275
2276 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002277 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002278
Gong Kefdb31922022-06-17 17:11:16 +08002279 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002280
2281 return error;
2282}
2283
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002284int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2285{
2286 DVR_WrapperCtx_t *ctx;
2287 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002288 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Wentao MA804bab12022-11-29 10:01:26 +08002289 uint64_t segment_id = ULLONG_MAX;
2290 uint32_t segment_offset = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002291
2292 DVR_RETURN_IF_FALSE(playback);
2293
2294 ctx = ctx_getPlayback((unsigned long)playback);
2295 DVR_RETURN_IF_FALSE(ctx);
2296
Gong Kefdb31922022-06-17 17:11:16 +08002297 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002298
Wentao MA804bab12022-11-29 10:01:26 +08002299 DVR_WRAPPER_INFO("libdvr_api, seek_playback (sn:%ld) offset:%dms", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002300 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002301
hualing chen03fd4942021-07-15 15:56:41 +08002302 //if set limit info we need check ts data is
2303 //expired when seek
2304 if (dvr_playback_check_limit(ctx->playback.player)) {
2305 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2306 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002307 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002308 ctx->sn, time_offset, expired);
2309 time_offset = expired;
2310 }
2311 }
2312
Wentao MA804bab12022-11-29 10:01:26 +08002313 const uint32_t obsolete_time = (uint32_t)ctx->playback.obsolete.time;
2314 DVR_WrapperPlaybackSegmentInfo_t *p_seg_first = ctx->segments.c_prev;
2315 DVR_WrapperPlaybackSegmentInfo_t *p_seg_last = ctx->segments.c_next;
2316 const uint64_t first_id = p_seg_first->seg_info.id;
2317 const uint64_t last_id = p_seg_last->seg_info.id;
2318 const uint32_t last_duration = p_seg_last->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002319
Wentao MA804bab12022-11-29 10:01:26 +08002320 if (time_offset <= obsolete_time) {
2321 segment_id = first_id;
2322 segment_offset = 0;
2323 DVR_WRAPPER_WARN("time_offset %u isn't greater than obsolete time %u, "
2324 "so seek to beginning position of segment %llu",
2325 time_offset,obsolete_time,segment_id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002326 } else {
Wentao MA804bab12022-11-29 10:01:26 +08002327 uint32_t total_duration = 0;
2328 // This error is suppressed as the macro code is picked from kernel.
2329 // prefetch() here incurring self_assign is used to avoid some compiling
2330 // warnings.
2331 // coverity[self_assign]
2332 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2333 const uint32_t segment_begin = obsolete_time + total_duration;
2334 const uint32_t segment_end = segment_begin + p_seg->seg_info.duration;
2335 if (time_offset >= segment_begin && time_offset <= segment_end) {
2336 segment_id = p_seg->seg_info.id;
2337 segment_offset = time_offset - segment_begin;
2338 break;
2339 }
2340 total_duration += p_seg->seg_info.duration;
2341 }
2342 if (segment_id == ULLONG_MAX) {
2343 segment_id = last_id;
2344 segment_offset = last_duration;
2345 DVR_WRAPPER_WARN("time_offset %u is out of range, so seek to"
2346 " the end position of segment %llu",time_offset,segment_id);
2347 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002348 }
2349
Wentao MA804bab12022-11-29 10:01:26 +08002350 DVR_WRAPPER_INFO("seek playback(sn:%ld) (segment_id:%llu, segment_offset:%u)\n",
2351 ctx->sn, segment_id, segment_offset);
2352 error = dvr_playback_seek(ctx->playback.player, segment_id, segment_offset);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002353
Gong Kefdb31922022-06-17 17:11:16 +08002354 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002355
2356 return error;
2357}
2358
2359int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2360{
2361 DVR_WrapperCtx_t *ctx;
2362 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002363 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002364
2365 DVR_RETURN_IF_FALSE(playback);
2366 DVR_RETURN_IF_FALSE(p_pids);
2367
2368 ctx = ctx_getPlayback((unsigned long)playback);
2369 DVR_RETURN_IF_FALSE(ctx);
2370
Gong Kefdb31922022-06-17 17:11:16 +08002371 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002372
Wentao MA804bab12022-11-29 10:01:26 +08002373 DVR_WRAPPER_INFO("libdvr_api, update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002374 ctx->sn,
2375 p_pids->video.pid, p_pids->video.format,
2376 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002377 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002378
2379 ctx->playback.pids_req = *p_pids;
2380
2381 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002382 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002383 // prefetch() here incurring self_assign is used to avoid some compiling
2384 // warnings.
2385 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002386 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002387 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002388 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2389 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2390 /*check update for pids*/
2391 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2392 p_seg->playback_info.pids = *p_pids;
2393 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002394 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002395 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002396 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002397 /*do not break, let list updated*/
2398 }
2399 }
2400 }
2401 /*break;*/
2402 }
2403 }
2404
Wentao MA96f68962022-06-15 19:45:35 +08002405 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002406 ctx->sn,
2407 p_pids->video.pid, p_pids->video.format,
2408 p_pids->audio.pid, p_pids->audio.format,
2409 error);
2410
Gong Kefdb31922022-06-17 17:11:16 +08002411 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002412
2413 return error;
2414}
2415
hualing chena5f03222021-12-02 11:22:35 +08002416int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2417{
2418 DVR_WrapperCtx_t *ctx;
2419 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002420 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002421
2422 DVR_RETURN_IF_FALSE(playback);
2423 DVR_RETURN_IF_FALSE(p_pids);
2424
2425 ctx = ctx_getPlayback((unsigned long)playback);
2426 DVR_RETURN_IF_FALSE(ctx);
2427
Gong Kefdb31922022-06-17 17:11:16 +08002428 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002429
Wentao MA804bab12022-11-29 10:01:26 +08002430 DVR_WRAPPER_INFO("libdvr_api, only_update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
hualing chena5f03222021-12-02 11:22:35 +08002431 ctx->sn,
2432 p_pids->video.pid, p_pids->video.format,
2433 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002434 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002435
2436 ctx->playback.pids_req = *p_pids;
2437
2438 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002439 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002440 // prefetch() here incurring self_assign is used to avoid some compiling
2441 // warnings.
2442 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002443 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002444 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002445 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2446 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2447 /*check update for pids*/
2448 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2449 p_seg->playback_info.pids = *p_pids;
2450 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002451 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002452 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002453 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002454 /*do not break, let list updated*/
2455 }
2456 }
2457 }
2458 /*break;*/
2459 }
2460 }
2461
Wentao MA96f68962022-06-15 19:45:35 +08002462 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002463 ctx->sn,
2464 p_pids->video.pid, p_pids->video.format,
2465 p_pids->audio.pid, p_pids->audio.format,
2466 error);
2467
Gong Kefdb31922022-06-17 17:11:16 +08002468 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002469
2470 return error;
2471}
2472
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002473int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2474{
2475 DVR_WrapperCtx_t *ctx;
2476 DVR_WrapperPlaybackStatus_t s;
2477 DVR_PlaybackStatus_t play_status;
2478 int error;
2479
2480 DVR_RETURN_IF_FALSE(playback);
2481 DVR_RETURN_IF_FALSE(status);
2482
2483 ctx = ctx_getPlayback((unsigned long)playback);
2484 DVR_RETURN_IF_FALSE(ctx);
2485
Gong Kefdb31922022-06-17 17:11:16 +08002486 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002487
Gong Kefdb31922022-06-17 17:11:16 +08002488 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002489
Wentao MA804bab12022-11-29 10:01:26 +08002490 dvr_playback_get_status(ctx->playback.player, &play_status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002491
2492 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002493 error = process_generatePlaybackStatus(ctx, &s);
2494
hualing chenb5cd42e2020-04-15 17:03:34 +08002495 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2496 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002497 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002498 s.info_cur.time = s.info_full.time;
2499 }
Wentao MA804bab12022-11-29 10:01:26 +08002500 DVR_WRAPPER_INFO("get_playback_status (sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2501 ctx->sn, s.state, s.info_cur.time, s.info_full.time, s.info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002502
2503 *status = s;
2504
Gong Kefdb31922022-06-17 17:11:16 +08002505 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002506
2507 return error;
2508}
2509
hualing chen266b9502020-04-04 17:39:39 +08002510int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2511{
2512 DVR_WrapperCtx_t *ctx;
2513 int error;
2514
2515 DVR_RETURN_IF_FALSE(playback);
2516 DVR_RETURN_IF_FALSE(p_secure_buf);
2517
2518 ctx = ctx_getPlayback((unsigned long)playback);
2519 DVR_RETURN_IF_FALSE(ctx);
2520
Gong Kefdb31922022-06-17 17:11:16 +08002521 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002522 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002523 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002524 return error;
2525}
2526
2527int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2528{
2529 DVR_WrapperCtx_t *ctx;
2530 int error;
2531
2532 DVR_RETURN_IF_FALSE(playback);
2533 DVR_RETURN_IF_FALSE(func);
2534
2535 ctx = ctx_getPlayback((unsigned long)playback);
2536 DVR_RETURN_IF_FALSE(ctx);
2537
Gong Kefdb31922022-06-17 17:11:16 +08002538 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002539 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002540 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002541 return error;
2542}
2543
Zhiqiang Han620b9252021-11-09 14:23:20 +08002544int dvr_wrapper_segment_del_by_location (const char *location)
2545{
2546 char fpath[DVR_MAX_LOCATION_SIZE];
2547
2548 DVR_RETURN_IF_FALSE(location);
2549
2550 /*del the stats file*/
2551 sprintf(fpath, "%s.stats", location);
2552 unlink(fpath);
2553
2554 return dvr_segment_del_by_location(location);
2555}
2556
2557int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2558{
2559 FILE *fp;
2560 char fpath[DVR_MAX_LOCATION_SIZE];
2561
2562 DVR_RETURN_IF_FALSE(location);
2563 DVR_RETURN_IF_FALSE(p_info);
2564
2565 if (p_info)
2566 memset(p_info, 0, sizeof(p_info[0]));
2567
2568 memset(fpath, 0, sizeof(fpath));
2569 sprintf(fpath, "%s.stats", location);
2570
2571 /*stats file exists*/
2572 if ((fp = fopen(fpath, "r"))) {
2573 char buf[256];
2574
2575 if (fgets(buf, sizeof(buf), fp) != NULL
2576 && (sscanf(buf, ":%llu:%lu:%u",
2577 &p_info->size,
2578 &p_info->time,
2579 &p_info->pkts) == 3)) {
2580 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002581 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 +08002582 return DVR_SUCCESS;
2583 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002584 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002585 }
2586
2587 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002588 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002589
2590 int error;
2591 uint32_t n_ids;
2592 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002593
hualing chen8aed9582021-12-24 17:59:56 +08002594 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002595
Zhiqiang Han620b9252021-11-09 14:23:20 +08002596 if (!error) {
2597 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002598 struct list_head info_list; /**< segment list head*/
2599 INIT_LIST_HEAD(&info_list);
2600
2601 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002602 error = dvr_segment_get_allInfo(location, &info_list);
2603 if (error == DVR_FAILURE) {
wentao.maa210e5e2022-10-12 16:10:03 +08002604 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2605 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
hualing chenb9a02922021-12-14 11:29:47 +08002606
2607 memset(&info, 0, sizeof(info));
2608 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002609 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002610 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002611
wentao.maf57dd232022-10-08 16:07:29 +08002612 // Tainted data issue originating from fgets seem false positive, so we
2613 // just suppress it here.
2614 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002615 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002616 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002617 if (!error) {
2618 p_info->size += info.size;
2619 p_info->time += info.duration;
2620 p_info->pkts += info.nb_packets;
2621 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002622 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002623 break;
2624 }
2625 }
2626 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002627 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
wentao.maf57dd232022-10-08 16:07:29 +08002628 // Tainted data issue originating from fgets seem false positive, so we
2629 // just suppress it here.
2630 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002631 for (i = 0; i < n_ids; i++) {
2632
2633 DVR_RecordSegmentInfo_t *seg_info;
2634 DVR_PlaybackSegmentFlag_t flags;
2635 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002636 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002637 // prefetch() here incurring self_assign is used to avoid some compiling
2638 // warnings.
2639 // coverity[self_assign]
hualing chenb9a02922021-12-14 11:29:47 +08002640 list_for_each_entry(seg_info, &info_list, head)
2641 {
hualing chen8aed9582021-12-24 17:59:56 +08002642 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002643 found = 1;
2644 break;
2645 }
2646 }
2647 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002648 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 +08002649 if (p_ids[i] == n_ids - 1) {
wentao.maa210e5e2022-10-12 16:10:03 +08002650 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2651 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
Wentao MA96f68962022-06-15 19:45:35 +08002652 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 +08002653 error = dvr_segment_get_info(location, p_ids[i], &info);
2654 if (!error) {
2655 p_info->size += info.size;
2656 p_info->time += info.duration;
2657 p_info->pkts += info.nb_packets;
2658 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002659 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002660 break;
2661 }
2662 }
hualing chenb9a02922021-12-14 11:29:47 +08002663 continue;
2664 }
2665
2666 if (!error) {
2667 p_info->size += seg_info->size;
2668 p_info->time += seg_info->duration;
2669 p_info->pkts += seg_info->nb_packets;
2670 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002671 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002672 break;
2673 }
2674 }
2675 //free list
2676 DVR_RecordSegmentInfo_t *segment = NULL;
2677 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2678 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2679 {
2680 if (segment) {
2681 list_del(&segment->head);
2682 free(segment);
2683 }
2684 }
2685 }
2686 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002687 } else {
2688 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002689 }
Wentao MA96f68962022-06-15 19:45:35 +08002690 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 +08002691
2692 return (error)? DVR_FAILURE : DVR_SUCCESS;
2693}
2694
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002695static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2696{
wentao.maa210e5e2022-10-12 16:10:03 +08002697 DVR_WrapperEventCtx_t evt = {
2698 .sn = (unsigned long)userdata,
2699 .type = W_REC,
2700 .record.event = event,
2701 .record.status = *(DVR_RecordStatus_t *)params
2702 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002703
2704 DVR_RETURN_IF_FALSE(userdata);
2705
Wentao MA804bab12022-11-29 10:01:26 +08002706 DVR_WRAPPER_DEBUG("record event 0x%x (sn:%ld)", evt.record.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002707 return ctx_addRecordEvent(&evt);
2708}
2709
2710static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2711{
wentao.maa210e5e2022-10-12 16:10:03 +08002712 DVR_WrapperEventCtx_t evt = {
2713 .sn = (unsigned long)userdata,
2714 .type = W_PLAYBACK,
2715 .playback.event = event,
2716 .playback.status = *(DVR_Play_Notify_t *)params
2717 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002718
2719 DVR_RETURN_IF_FALSE(userdata);
2720
2721 evt.sn = (unsigned long)userdata;
2722 evt.type = W_PLAYBACK;
2723 evt.playback.event = event;
2724 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA804bab12022-11-29 10:01:26 +08002725 DVR_WRAPPER_DEBUG("playback event 0x%x (sn:%ld)", evt.playback.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002726 return ctx_addPlaybackEvent(&evt);
2727}
2728
2729static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2730{
Wentao MA270dc0f2022-08-23 13:17:26 +08002731 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 +08002732 ctx->sn,
2733 evt,
2734 status->info.time,
2735 status->info.size,
2736 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002737 status->info_obsolete.time,
2738 status->info_obsolete.size,
2739 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002740
2741 if (ctx->record.event_fn)
2742 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2743 return 0;
2744}
2745
Zhiqiang Han620b9252021-11-09 14:23:20 +08002746static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2747{
2748 FILE *fp;
2749 char fpath[DVR_MAX_LOCATION_SIZE];
2750
2751 DVR_RETURN_IF_FALSE(location);
2752 DVR_RETURN_IF_FALSE(p_status);
2753
2754 sprintf(fpath, "%s.stats", location);
2755
2756 /*stats file*/
2757 if ((fp = fopen(fpath, "w"))) {
2758 char buf[256];
2759 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2760 p_status->info.size - p_status->info_obsolete.size,
2761 p_status->info.time - p_status->info_obsolete.time,
2762 p_status->info.pkts - p_status->info_obsolete.pkts);
2763 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002764 fflush(fp);
2765 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002766 fclose(fp);
2767 return DVR_SUCCESS;
2768 }
2769
2770 return DVR_FAILURE;
2771}
2772
2773
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002774static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2775{
2776 DVR_RecordStartParams_t param;
2777 DVR_RecordSegmentInfo_t seg_info;
2778 int i;
2779 int error;
2780
2781 memcpy(&param, &ctx->record.param_update, sizeof(param));
2782 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2783 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2784 for (i = 0; i < param.segment.nb_pids; i++) {
2785 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2786 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2787 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2788 ctx->record.param_update.segment.nb_pids++;
2789 }
2790 }
2791 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2792 {
2793 DVR_RecordSegmentInfo_t new_seg_info =
2794 { .id = ctx->record.param_update.segment.segment_id, };
2795 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2796 wrapper_addRecordSegment(ctx, &new_seg_info);
2797 }
2798
Wentao MA96f68962022-06-15 19:45:35 +08002799 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002800 return error;
2801}
2802
Wentao MA270dc0f2022-08-23 13:17:26 +08002803static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002804{
Wentao MA270dc0f2022-08-23 13:17:26 +08002805 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002806}
2807
2808/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002809static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002810{
2811 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002812 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002813
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002814 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002815 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2816
2817 ctx->record.status.state = ctx->record.seg_status.state;
2818 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2819 memcpy(ctx->record.status.pids.pids,
2820 ctx->record.seg_status.info.pids,
2821 sizeof(ctx->record.status.pids.pids));
2822 ctx->current_segment_id = ctx->record.seg_status.info.id;
2823
wentao.mafd5283f2022-10-14 09:51:13 +08002824 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002825 // prefetch() here incurring self_assign is used to avoid some compiling
2826 // warnings.
2827 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002828 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2829 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2830 ctx->record.status.info.time += p_seg->info.duration;
2831 ctx->record.status.info.size += p_seg->info.size;
2832 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002833 }
2834 }
2835
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002836 ctx->record.status.info_obsolete = ctx->record.obsolete;
2837
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002838 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002839
2840 if (status) {
2841 *status = ctx->record.status;
2842 status->info.time += ctx->record.seg_status.info.duration;
2843 status->info.size += ctx->record.seg_status.info.size;
2844 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2845 }
2846
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002847 return DVR_SUCCESS;
2848}
2849
2850
2851static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2852{
2853 DVR_WrapperRecordStatus_t status;
2854
2855 memset(&status, 0, sizeof(status));
2856
Wentao MA804bab12022-11-29 10:01:26 +08002857 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d)\n",
2858 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002859 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002860 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002861 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002862 return 0;
2863 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002864 switch (evt->record.event)
2865 {
2866 case DVR_RECORD_EVENT_STATUS:
2867 {
2868 switch (evt->record.status.state)
2869 {
2870 case DVR_RECORD_STATE_OPENED:
2871 case DVR_RECORD_STATE_CLOSED:
2872 {
2873 ctx->record.seg_status = evt->record.status;
2874
2875 status.state = evt->record.status.state;
2876 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002877 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002878 } break;
2879 case DVR_RECORD_STATE_STARTED:
2880 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002881 ctx->record.seg_status = evt->record.status;
2882
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002883 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002884 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002885 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002886
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002887 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002888 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002889 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002890 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 +08002891 ctx->sn,
2892 evt->record.status.info.size,
2893 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002894 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2895 /*should notify the recording's stop*/
2896 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002897 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002898 ctx->sn, error);
2899 status.state = DVR_RECORD_STATE_CLOSED;
2900 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002901 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002902 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002903 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002904
2905 if (ctx->record.param_open.is_timeshift
2906 && ctx->record.param_open.max_time
2907 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002908 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002909
2910 /*as the player do not support null playlist,
2911 there must be one segment existed at any time,
2912 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002913 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2914 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002915 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002916 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 +08002917 status.info.size,
2918 ctx->record.param_open.max_time,
2919 ctx->record.param_open.segment_size);
2920 } else {
2921 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002922 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002923
2924 process_generateRecordStatus(ctx, &status);
2925 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002926 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002927 }
2928 }
2929
Wentao MAf4072032022-06-30 13:50:45 +08002930 const loff_t actual_size = status.info.size;
2931 const loff_t max_size = ctx->record.param_open.max_size;
2932 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002933
Wentao MAf4072032022-06-30 13:50:45 +08002934 if (ctx->record.param_open.is_timeshift && max_size) {
2935 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002936 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002937 /*as the player do not support null playlist,
2938 there must be one segment existed at any time,
2939 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002940 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2941 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002942 /*only one segment, waiting for more*/
2943 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2944 actual_size, segment_size);
2945 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002946 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002947
2948 process_generateRecordStatus(ctx, &status);
2949 process_notifyRecord(ctx, evt->record.event, &status);
2950 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2951 }
2952 if (actual_size >= max_size + segment_size/2) {
2953 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2954 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002955 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002956 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002957 }
2958 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002959 } break;
2960 case DVR_RECORD_STATE_STOPPED:
2961 {
2962 ctx->record.seg_status = evt->record.status;
2963
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002964 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002965 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002966 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002967 } break;
2968 default:
2969 break;
2970 }
2971 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002972 case DVR_RECORD_EVENT_WRITE_ERROR: {
2973 ctx->record.seg_status = evt->record.status;
2974 status.state = evt->record.status.state;
2975 process_notifyRecord(ctx, evt->record.event, &status);
2976 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002977 default:
2978 break;
2979 }
2980 return DVR_SUCCESS;
2981}
2982
2983static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2984{
Wentao MA80179512022-11-03 12:20:03 +08002985 DVR_RETURN_IF_FALSE(ctx->playback.event_fn != NULL);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002986
Wentao MA80179512022-11-03 12:20:03 +08002987 const time_t cur_time = status->info_cur.time;
2988 const time_t full_time = status->info_full.time;
2989 const time_t obsolete_time = status->info_obsolete.time;
2990 const time_t origin_offset = cur_time + obsolete_time;
2991 DVR_WRAPPER_INFO("playback progress notify(sn:%ld) evt(0x%x)"
2992 " actual_slider_pos: %02d:%02d:%02d.%03d/%02d:%02d:%02d.%03d (%7ld ms/%7ld ms),"
2993 " offset_from_origin: %02d:%02d:%02d.%03d (%7ld ms),"
2994 " dump status:state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2995 ctx->sn,evt,cur_time/1000/3600,cur_time/1000%3600/60,cur_time/1000%60,cur_time%1000,
2996 full_time/1000/3600,full_time/1000%3600/60,full_time/1000%60,full_time%1000,
2997 cur_time,full_time,
2998 origin_offset/1000/3600,origin_offset/1000%3600/60,origin_offset/1000%60,origin_offset%1000,
2999 origin_offset,status->state,cur_time,full_time,obsolete_time);
3000
3001 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003002}
3003
3004/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003005static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003006{
3007 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08003008 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003009
3010 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
3011 ctx->playback.status.pids = ctx->playback.pids_req;
3012
3013 ctx->playback.status.state = ctx->playback.seg_status.state;
3014 ctx->playback.status.speed = ctx->playback.seg_status.speed;
3015 ctx->playback.status.flags = ctx->playback.seg_status.flags;
3016 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
3017
wentao.mafd5283f2022-10-14 09:51:13 +08003018 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003019 // prefetch() here incurring self_assign is used to avoid some compiling
3020 // warnings.
3021 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003022 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3023 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003024 break;
hualing chen451c8f72022-03-09 13:05:52 +08003025 }
3026
Wentao MA270dc0f2022-08-23 13:17:26 +08003027 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
3028 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
3029 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003030 }
wentao.mafd5283f2022-10-14 09:51:13 +08003031 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003032 // prefetch() here incurring self_assign is used to avoid some compiling
3033 // warnings.
3034 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003035 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3036 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
3037 ctx->playback.status.info_full.size += p_seg->seg_info.size;
3038 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003039 }
3040
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003041 if (status) {
3042 *status = ctx->playback.status;
3043 /*deal with current, lack size and pkts with the current*/
3044 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08003045 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08003046 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08003047
Wentao MA270dc0f2022-08-23 13:17:26 +08003048 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
3049 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08003050 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
3051 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08003052 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 +08003053 }
3054 else
3055 {
Wentao MA804bab12022-11-29 10:01:26 +08003056 status->info_obsolete.time = ctx->playback.obsolete.time;
3057 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
hualing chen56c0a162022-01-27 17:01:50 +08003058 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003059 }
3060
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003061 return DVR_SUCCESS;
3062}
3063
3064static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3065{
Wentao MA804bab12022-11-29 10:01:26 +08003066 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003067 evt->sn, evt->playback.event,
3068 evt->playback.status.play_status.state,
3069 evt->playback.status.play_status.segment_id,
3070 evt->playback.status.play_status.time_cur,
3071 evt->playback.status.play_status.time_end);
3072
3073 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08003074 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
3075 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
3076 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
3077 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003078 ctx->playback.last_event = evt->playback.event;
3079
3080 switch (evt->playback.event)
3081 {
3082 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
3083 case DVR_PLAYBACK_EVENT_REACHED_END:
3084 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
3085 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08003086 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08003087 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08003088 case DVR_PLAYBACK_EVENT_NODATA:
3089 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003090 {
3091 DVR_WrapperPlaybackStatus_t status;
3092
3093 /*copy status of segment*/
3094 ctx->playback.seg_status = evt->playback.status.play_status;
3095
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003096 /*generate status of the whole playback*/
3097 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003098
3099 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08003100 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08003101 if (ctx->playback.param_open.is_timeshift
3102 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003103 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08003104 }
3105 /*trust the low level, make NO check.
3106 As this evt is changed to only once due to some operations(paused) in low level.
3107 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003108 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08003109 }
3110 */
3111 else {
3112 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08003113 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003114 }
hualing chenf291cf32020-06-18 10:50:30 +08003115 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003116 process_notifyPlayback(ctx, evt->playback.event, &status);
3117 }
3118 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003119 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
3120 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
3121 case DVR_PLAYBACK_EVENT_NO_KEY:
3122 {
Wentao MA96f68962022-06-15 19:45:35 +08003123 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003124 } break;
3125 default:
3126 {
Wentao MA96f68962022-06-15 19:45:35 +08003127 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003128 } break;
3129 }
3130 return 0;
3131}
3132
3133static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3134{
3135 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
3136}
3137
Wentao MA96f68962022-06-15 19:45:35 +08003138int dvr_wrapper_set_log_level (int level)
3139{
Wentao MA804bab12022-11-29 10:01:26 +08003140 DVR_WRAPPER_INFO("libdvr_api, set_log_level %d", level);
Wentao MA96f68962022-06-15 19:45:35 +08003141 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3142 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3143 return DVR_FAILURE;
3144 }
3145 g_dvr_log_level = level;
3146 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3147 return DVR_SUCCESS;
3148}
3149
Wentao MA5629ad82022-08-24 10:03:02 +08003150int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3151{
3152 DVR_WrapperCtx_t *ctx;
3153 int error;
3154
Wentao MA3e2dc452022-12-20 11:17:16 +08003155 DVR_RETURN_IF_FALSE(playback!=NULL);
Wentao MA5629ad82022-08-24 10:03:02 +08003156
3157 ctx = ctx_getPlayback((unsigned long)playback);
3158 DVR_RETURN_IF_FALSE(ctx);
3159
3160 wrapper_mutex_lock(&ctx->wrapper_lock);
3161
3162 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3163
Wentao MA804bab12022-11-29 10:01:26 +08003164 DVR_WRAPPER_INFO("libdvr_api, set_ac4_preselection_id %d", presel_id);
Wentao MA5629ad82022-08-24 10:03:02 +08003165 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3166
3167 wrapper_mutex_unlock(&ctx->wrapper_lock);
3168
3169 return error;
3170}
3171
wentao ma7d642782022-10-23 18:26:16 -07003172int dvr_wrapper_property_set(const char* prop_name, const char* prop_value)
3173{
3174 return dvr_prop_write(prop_name,prop_value);
3175}
3176
3177int dvr_wrapper_property_get(const char* prop_name, char* prop_value, int length)
3178{
3179 return dvr_prop_read(prop_name,prop_value,length);
3180}
3181