blob: 8d49b38d01cc918489c8dd2b75848eb15004c68d [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
hualing chenb9a1a2c2021-12-31 11:27:59 +0800979 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
980 off_set = 10 * 1000;
981 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800982 DVR_WRAPPER_INFO("timeshift, playback(sn:%ld), seek(seg:%llu 0) from new start (%d)\n", ctx->sn, next_seg->seg_info.id, error);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800983 }
984
985 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
986 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800987 /*remove playback segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +0800988 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 +0800989 }
990
Wentao MA270dc0f2022-08-23 13:17:26 +0800991 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800992
993 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800994 ctx->playback.obsolete.time += p_seg->seg_info.duration;
995 ctx->playback.obsolete.size += p_seg->seg_info.size;
996 ctx->playback.obsolete.pkts += p_seg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +0800997 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 +0800998 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Wentao MA270dc0f2022-08-23 13:17:26 +0800999 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001000 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001001 }
1002 }
1003
Wentao MA96f68962022-06-15 19:45:35 +08001004 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 +08001005
1006 return error;
1007}
1008
1009static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
1010{
1011 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08001012 DVR_WrapperRecordSegmentInfo_t *p_seg, *p_seg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001013
Wentao MAf4072032022-06-30 13:50:45 +08001014 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1015 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001016
1017 /*if timeshifting, notify the playback first, then deal with record*/
1018 if (ctx->record.param_open.is_timeshift) {
1019 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1020
1021 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001022 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001023 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1024 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001025 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1026 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1027 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001028 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001029 return DVR_SUCCESS;
1030 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001031 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1032 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1033 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001034 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001035 if (ctx_valid(ctx_playback)
1036 && ctx_playback->sn == sn_timeshift_playback
1037 && !list_empty(&ctx_playback->segments)) {
1038 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
Wentao MA07d3d742022-09-06 09:58:05 +08001039 if (error != DVR_SUCCESS) {
1040 DVR_WRAPPER_ERROR("wrapper_removePlaybackSegment failed with return value %d",error);
1041 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001042 }
hualing chen56c0a162022-01-27 17:01:50 +08001043 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001044 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001045 }
1046 }
1047
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001048 uint64_t id = seg_info->info.id;
1049
Wentao MA270dc0f2022-08-23 13:17:26 +08001050 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
1051 if (p_seg->info.id == id) {
1052 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001053
1054 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001055 ctx->record.obsolete.time += p_seg->info.duration;
1056 ctx->record.obsolete.size += p_seg->info.size;
1057 ctx->record.obsolete.pkts += p_seg->info.nb_packets;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001058
Wentao MA270dc0f2022-08-23 13:17:26 +08001059 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001060 break;
1061 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001062 }
1063
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001064 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001065
Wentao MAf4072032022-06-30 13:50:45 +08001066 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1067 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001068
1069 return error;
1070}
1071
1072int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1073{
1074 int error;
1075 DVR_WrapperCtx_t *ctx;
1076 DVR_RecordOpenParams_t open_param;
1077
1078 DVR_RETURN_IF_FALSE(rec);
1079 DVR_RETURN_IF_FALSE(params);
1080
1081 /*get a free ctx*/
1082 ctx = ctx_getRecord(0);
1083 DVR_RETURN_IF_FALSE(ctx);
1084
Gong Kefdb31922022-06-17 17:11:16 +08001085 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001086
Wentao MA9a164002022-08-29 11:20:24 +08001087 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 +08001088 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001089
1090 ctx_reset(ctx);
1091
1092 ctx->record.param_open = *params;
1093 ctx->record.event_fn = params->event_fn;
1094 ctx->record.event_userdata = params->event_userdata;
Wentao MA2394fa82022-06-10 14:46:47 +08001095
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001096 INIT_LIST_HEAD(&ctx->segments);
1097 ctx->sn = get_sn();
1098
1099 wrapper_requestThreadFor(ctx);
1100
hualing chen266b9502020-04-04 17:39:39 +08001101 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001102 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001103 open_param.dmx_dev_id = params->dmx_dev_id;
1104 open_param.data_from_memory = 0;
1105 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001106 if (params->flush_size) {
1107 open_param.notification_size = params->flush_size;
1108 } else {
1109 open_param.notification_size = 64*1024;
1110 }
hualing chen002e5b92022-02-23 17:51:21 +08001111 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001112 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001113 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001114 open_param.event_fn = wrapper_record_event_handler;
1115 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001116 if (params->keylen) {
1117 open_param.clearkey = params->clearkey;
1118 open_param.cleariv = params->cleariv;
1119 open_param.keylen = params->keylen;
1120 }
wentao.ma35a69d42022-03-10 18:08:40 +08001121 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001122 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001123
1124 error = dvr_record_open(&ctx->record.recorder, &open_param);
1125 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001126 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001127 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001128 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001129 wrapper_releaseThreadForType(ctx->type);
1130 return DVR_FAILURE;
1131 }
1132 if (params->is_timeshift)
1133 sn_timeshift_record = ctx->sn;
1134
Wentao MA96f68962022-06-15 19:45:35 +08001135 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001136
Yahui Han1fbf3292021-11-08 18:17:19 +08001137 if (params->crypto_fn) {
1138 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1139 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001140 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 +08001141 }
hualing chen266b9502020-04-04 17:39:39 +08001142 }
1143
Gong Kefdb31922022-06-17 17:11:16 +08001144 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001145
1146 *rec = (DVR_WrapperRecord_t)ctx->sn;
1147 return DVR_SUCCESS;
1148}
1149
1150int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1151{
1152 DVR_WrapperCtx_t *ctx;
1153 DVR_RecordSegmentInfo_t seg_info;
1154 int error;
1155
1156 DVR_RETURN_IF_FALSE(rec);
1157
1158 ctx = ctx_getRecord((unsigned long)rec);
1159 DVR_RETURN_IF_FALSE(ctx);
1160
Gong Kefdb31922022-06-17 17:11:16 +08001161 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001162 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001163 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001164
1165 memset(&seg_info, 0, sizeof(seg_info));
wentao.maa210e5e2022-10-12 16:10:03 +08001166 dvr_record_stop_segment(ctx->record.recorder, &seg_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001167
1168 error = dvr_record_close(ctx->record.recorder);
1169
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001170 if (ctx->record.param_open.is_timeshift)
1171 sn_timeshift_record = 0;
1172
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001173 ctx_freeSegments(ctx);
1174
Wentao MA96f68962022-06-15 19:45:35 +08001175 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001176 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001177 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001178
1179 wrapper_releaseThreadForType(ctx->type);
1180
1181 return error;
1182}
1183
1184int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1185{
1186 DVR_WrapperCtx_t *ctx;
1187 DVR_RecordStartParams_t *start_param;
1188 int i;
1189 int error;
1190
1191 DVR_RETURN_IF_FALSE(rec);
1192 DVR_RETURN_IF_FALSE(params);
1193
1194 ctx = ctx_getRecord((unsigned long)rec);
1195 DVR_RETURN_IF_FALSE(ctx);
1196
Gong Kefdb31922022-06-17 17:11:16 +08001197 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001198 DVR_WRAPPER_INFO("libdvr_api, start_record (sn:%ld) location:%s, save:%d",
1199 ctx->sn, ctx->record.param_open.location, params->save_rec_file);
Gong Kefdb31922022-06-17 17:11:16 +08001200 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001201
1202 start_param = &ctx->record.param_start;
1203 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001204 const int len = strlen(ctx->record.param_open.location);
1205 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1206 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1207 pthread_mutex_unlock(&ctx->wrapper_lock);
1208 return DVR_FAILURE;
1209 }
1210 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001211 start_param->segment.nb_pids = params->pids_info.nb_pids;
1212 for (i = 0; i < params->pids_info.nb_pids; i++) {
1213 start_param->segment.pids[i] = params->pids_info.pids[i];
1214 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1215 }
Zhiqiang Han89c77972022-12-08 12:16:39 +08001216
hualing chena5f03222021-12-02 11:22:35 +08001217 if (params->save_rec_file == 0)//default is not save
1218 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han89c77972022-12-08 12:16:39 +08001219
1220 //wait for the file status to stabilize before set the new segment id
1221 uint64_t new_segment_id = 0;
Zhiqiang Han02e890c2023-04-25 14:55:02 +08001222
1223 if (params->save_rec_file != 0) {
Zhiqiang Han89c77972022-12-08 12:16:39 +08001224 uint32_t segment_nb = 0;
1225 uint64_t *p_segment_ids = NULL;
1226 error = dvr_segment_get_list(ctx->record.param_open.location, &segment_nb, &p_segment_ids);
1227 if (error == DVR_SUCCESS && segment_nb>0) {
1228 // Tainted data issue originating from fgets seem false positive, so we
1229 // just suppress it here.
1230 // coverity[tainted_data]
1231 new_segment_id = p_segment_ids[segment_nb-1]+1;
1232 }
1233 if (p_segment_ids != NULL) {
1234 free(p_segment_ids);
1235 }
Zhiqiang Han89c77972022-12-08 12:16:39 +08001236 }
Zhiqiang Han02e890c2023-04-25 14:55:02 +08001237 DVR_WRAPPER_DEBUG("new_segment_id:%lld\n", new_segment_id);
1238
Zhiqiang Han89c77972022-12-08 12:16:39 +08001239 ctx->record.next_segment_id = new_segment_id;
1240 ctx->current_segment_id = new_segment_id;
1241
1242 start_param->segment.segment_id = ctx->record.next_segment_id++;
1243
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001244 {
1245 /*sync to update for further use*/
1246 DVR_RecordStartParams_t *update_param;
1247 update_param = &ctx->record.param_update;
1248 memcpy(update_param, start_param, sizeof(*update_param));
1249 for (i = 0; i < update_param->segment.nb_pids; i++)
1250 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1251 }
1252
1253 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1254 {
1255 DVR_RecordSegmentInfo_t new_seg_info =
1256 { .id = start_param->segment.segment_id, };
1257 wrapper_addRecordSegment(ctx, &new_seg_info);
1258 }
1259
Wentao MA96f68962022-06-15 19:45:35 +08001260 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001261
Gong Kefdb31922022-06-17 17:11:16 +08001262 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001263
1264 return error;
1265}
1266
1267int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1268{
1269 DVR_WrapperCtx_t *ctx;
1270 DVR_RecordSegmentInfo_t seg_info;
1271 int error;
1272
1273 DVR_RETURN_IF_FALSE(rec);
1274
1275 ctx = ctx_getRecord((unsigned long)rec);
1276 DVR_RETURN_IF_FALSE(ctx);
1277
Gong Kefdb31922022-06-17 17:11:16 +08001278 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001279 DVR_WRAPPER_INFO("libdvr_api, stop_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001280 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001281
1282 memset(&seg_info, 0, sizeof(seg_info));
1283 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1284 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1285
Wentao MAd71e2062023-02-15 10:10:49 +08001286 ctx_freeSegments(ctx);
1287
Wentao MA96f68962022-06-15 19:45:35 +08001288 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001289 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001290
1291 return error;
1292}
1293
hualing chen03fd4942021-07-15 15:56:41 +08001294int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1295{
1296 DVR_WrapperCtx_t *ctx;
1297 int error;
1298
1299 DVR_RETURN_IF_FALSE(rec);
1300
1301 ctx = ctx_getRecord((unsigned long)rec);
1302 DVR_RETURN_IF_FALSE(ctx);
1303
Gong Kefdb31922022-06-17 17:11:16 +08001304 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001305 DVR_WRAPPER_INFO("libdvr_api, pause_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001306 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001307
1308 error = dvr_record_pause(ctx->record.recorder);
1309
Wentao MA96f68962022-06-15 19:45:35 +08001310 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001311 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001312
1313 return error;
1314}
1315
1316int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1317{
1318 DVR_WrapperCtx_t *ctx;
1319 int error;
1320
1321 DVR_RETURN_IF_FALSE(rec);
1322
1323 ctx = ctx_getRecord((unsigned long)rec);
1324 DVR_RETURN_IF_FALSE(ctx);
1325
Gong Kefdb31922022-06-17 17:11:16 +08001326 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001327 DVR_WRAPPER_INFO("libdvr_api, resume_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001328 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001329
1330 error = dvr_record_resume(ctx->record.recorder);
1331
Wentao MA96f68962022-06-15 19:45:35 +08001332 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001333 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001334
1335 return error;
1336}
1337
Wentao MAcdea4762022-04-26 13:28:56 +08001338/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001339static DVR_Bool_t pids_test_include(
1340 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1341 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001342{
Wentao MAcdea4762022-04-26 13:28:56 +08001343 DVR_Bool_t ret = DVR_TRUE;
1344 for (int i=0;i<size2;i++)
1345 { // iterate all elements in arr2 to check if they exist in arr1
1346 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001347
1348 if (act2[i] == DVR_RECORD_PID_CLOSE)
1349 continue;
1350
Wentao MAcdea4762022-04-26 13:28:56 +08001351 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001352 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001353 if (act1[j] != DVR_RECORD_PID_CLOSE
1354 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001355 {
1356 found=DVR_TRUE;
1357 break;
1358 }
1359 }
1360 if (found == DVR_FALSE)
1361 {
Wentao MAcdea4762022-04-26 13:28:56 +08001362 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001363 break;
1364 }
1365 }
Wentao MAcdea4762022-04-26 13:28:56 +08001366 return ret;
1367}
1368
1369static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1370 const DVR_WrapperUpdatePidsParams_t* p2)
1371{
1372 int i=0;
1373 char buf[128]={0};
1374 int cnt=0;
1375 int chars=0;
1376
1377 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1378 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1379
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001380 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1381 p2->pids,p2->pid_action,p2->nb_pids);
1382 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1383 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001384 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001385 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001386
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001387 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001388 for (i=0;i<p1->nb_pids;i++)
1389 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001390 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1391 removed++;
1392 continue;
1393 }
Wentao MAcdea4762022-04-26 13:28:56 +08001394 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1395 if (chars<0)
1396 {
1397 break;
1398 }
1399 cnt += chars;
1400 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001401 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001402 memset(buf,0,sizeof(buf));
1403
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001404 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001405 for (i=0,cnt=0;i<p2->nb_pids;i++)
1406 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001407 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1408 removed++;
1409 continue;
1410 }
Wentao MAcdea4762022-04-26 13:28:56 +08001411 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1412 if (chars<0)
1413 {
1414 break;
1415 }
1416 cnt += chars;
1417 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001418 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001419 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001420 return is_equal;
1421}
1422
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001423int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1424{
1425 DVR_WrapperCtx_t *ctx;
1426 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001427 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001428 int i;
1429 int error;
1430
1431 DVR_RETURN_IF_FALSE(rec);
1432 DVR_RETURN_IF_FALSE(params);
1433
1434 ctx = ctx_getRecord((unsigned long)rec);
1435 DVR_RETURN_IF_FALSE(ctx);
1436
Gong Kefdb31922022-06-17 17:11:16 +08001437 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001438 DVR_WRAPPER_INFO("libdvr_api, update_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001439 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001440
1441 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001442 if (pids_equal(&(start_param->segment),params))
1443 {
Gong Kefdb31922022-06-17 17:11:16 +08001444 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001445 return DVR_TRUE;
1446 }
1447
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001448 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001449 const int len = strlen(ctx->record.param_open.location);
1450 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1451 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1452 pthread_mutex_unlock(&ctx->wrapper_lock);
1453 return DVR_FAILURE;
1454 }
1455 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001456 start_param->segment.segment_id = ctx->record.next_segment_id++;
1457 start_param->segment.nb_pids = params->nb_pids;
1458 for (i = 0; i < params->nb_pids; i++) {
1459 start_param->segment.pids[i] = params->pids[i];
1460 start_param->segment.pid_action[i] = params->pid_action[i];
1461 }
1462 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1463 {
1464 DVR_RecordSegmentInfo_t new_seg_info =
1465 { .id = start_param->segment.segment_id, };
1466 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1467 wrapper_addRecordSegment(ctx, &new_seg_info);
1468 }
1469
Wentao MA96f68962022-06-15 19:45:35 +08001470 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001471 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001472
1473 return error;
1474}
1475
1476int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1477{
1478 DVR_WrapperCtx_t *ctx;
1479 DVR_WrapperRecordStatus_t s;
1480 int error;
1481
1482 DVR_RETURN_IF_FALSE(rec);
1483 DVR_RETURN_IF_FALSE(status);
1484
1485 ctx = ctx_getRecord((unsigned long)rec);
1486 DVR_RETURN_IF_FALSE(ctx);
1487
Gong Kefdb31922022-06-17 17:11:16 +08001488 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001489
Wentao MA804bab12022-11-29 10:01:26 +08001490 DVR_WRAPPER_INFO("libdvr_api, get_record_status (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001491 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001492
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001493 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001494
Wentao MA96f68962022-06-15 19:45:35 +08001495 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001496 ctx->sn,
1497 s.state,
1498 s.info.time,
1499 s.info.size,
1500 s.info.pkts,
1501 error);
1502
1503 *status = s;
1504
Gong Kefdb31922022-06-17 17:11:16 +08001505 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001506
1507 return error;
1508}
1509
hualing chen4fe3bee2020-10-23 13:58:52 +08001510int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1511{
1512 DVR_WrapperCtx_t *ctx;
1513 int error;
1514
1515 DVR_RETURN_IF_FALSE(rec);
1516
1517 ctx = ctx_getRecord((unsigned long)rec);
1518 DVR_RETURN_IF_FALSE(ctx);
1519
Gong Kefdb31922022-06-17 17:11:16 +08001520 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001521 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001522 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001523 return error;
1524}
1525
hualing chen266b9502020-04-04 17:39:39 +08001526int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1527{
1528 DVR_WrapperCtx_t *ctx;
1529 int error;
1530
1531 DVR_RETURN_IF_FALSE(rec);
1532 DVR_RETURN_IF_FALSE(p_secure_buf);
1533
1534 ctx = ctx_getRecord((unsigned long)rec);
1535 DVR_RETURN_IF_FALSE(ctx);
1536
Gong Kefdb31922022-06-17 17:11:16 +08001537 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001538 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001539 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001540 return error;
1541}
1542
1543int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1544{
1545 DVR_WrapperCtx_t *ctx;
1546 int error;
1547
1548 DVR_RETURN_IF_FALSE(rec);
1549 DVR_RETURN_IF_FALSE(func);
1550
1551 ctx = ctx_getRecord((unsigned long)rec);
1552 DVR_RETURN_IF_FALSE(ctx);
1553
Gong Kefdb31922022-06-17 17:11:16 +08001554 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001555 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001556 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001557 return error;
1558}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001559
1560
1561int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1562{
1563 DVR_WrapperCtx_t *ctx;
1564 DVR_PlaybackOpenParams_t open_param;
1565 int error;
1566
1567 DVR_RETURN_IF_FALSE(playback);
1568 DVR_RETURN_IF_FALSE(params);
1569 DVR_RETURN_IF_FALSE(params->playback_handle);
1570
1571 /*get a free ctx*/
1572 ctx = ctx_getPlayback(0);
1573 DVR_RETURN_IF_FALSE(ctx);
1574
Gong Kefdb31922022-06-17 17:11:16 +08001575 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001576
Wentao MA804bab12022-11-29 10:01:26 +08001577 DVR_WRAPPER_INFO("libdvr_api, open_playback (dmx:%d) ..vendor[%d]params->block_size[%d].",
1578 params->dmx_dev_id, params->vendor, params->block_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001579
1580 ctx_reset(ctx);
1581
1582 ctx->playback.param_open = *params;
1583 ctx->playback.event_fn = params->event_fn;
1584 ctx->playback.event_userdata = params->event_userdata;
1585 ctx->current_segment_id = 0;
1586 INIT_LIST_HEAD(&ctx->segments);
1587 ctx->sn = get_sn();
1588
1589 wrapper_requestThreadFor(ctx);
1590
hualing chen266b9502020-04-04 17:39:39 +08001591 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001592 open_param.dmx_dev_id = params->dmx_dev_id;
1593 open_param.block_size = params->block_size;
1594 open_param.is_timeshift = params->is_timeshift;
1595 //open_param.notification_size = 10*1024; //not supported
1596 open_param.event_fn = wrapper_playback_event_handler;
1597 open_param.event_userdata = (void*)ctx->sn;
1598 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001599 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001600 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001601 open_param.vendor = params->vendor;
1602
Yahui Han1fbf3292021-11-08 18:17:19 +08001603 if (params->keylen) {
1604 open_param.clearkey = params->clearkey;
1605 open_param.cleariv = params->cleariv;
1606 open_param.keylen = params->keylen;
1607 }
shenghui.gengbec6a462023-01-12 15:21:02 +08001608 open_param.control_speed_enable = params->control_speed_enable;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001609
1610 error = dvr_playback_open(&ctx->playback.player, &open_param);
1611 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001612 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001613 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001614 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001615 wrapper_releaseThreadForType(ctx->type);
1616 return DVR_FAILURE;
1617 }
1618 if (params->is_timeshift)
1619 sn_timeshift_playback = ctx->sn;
1620
Wentao MA270dc0f2022-08-23 13:17:26 +08001621 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001622 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1623 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001624 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001625 }
Gong Kefdb31922022-06-17 17:11:16 +08001626 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001627
1628 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1629 return DVR_SUCCESS;
1630}
1631
1632int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1633{
1634 DVR_WrapperCtx_t *ctx;
1635 int error;
1636
1637 DVR_RETURN_IF_FALSE(playback);
1638
1639 ctx = ctx_getPlayback((unsigned long)playback);
1640 DVR_RETURN_IF_FALSE(ctx);
1641
Gong Kefdb31922022-06-17 17:11:16 +08001642 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001643 DVR_WRAPPER_INFO("libdvr_api, close_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001644 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001645
1646 if (ctx->playback.param_open.is_timeshift)
1647 sn_timeshift_playback = 0;
1648
1649 /*try stop first*/
wentao.maa210e5e2022-10-12 16:10:03 +08001650 dvr_playback_stop(ctx->playback.player, DVR_TRUE);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001651
1652 {
1653 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001654 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001655
wentao.mafd5283f2022-10-14 09:51:13 +08001656 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001657 // prefetch() here incurring self_assign is used to avoid some compiling
1658 // warnings.
1659 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08001660 list_for_each_entry(p_seg, &ctx->segments, head) {
1661 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001662 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001663 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001664 }
1665 ctx_freeSegments(ctx);
1666 }
1667
1668 error = dvr_playback_close(ctx->playback.player);
1669
Wentao MA96f68962022-06-15 19:45:35 +08001670 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001671 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001672 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001673
1674 wrapper_releaseThreadForType(ctx->type);
1675
1676 return error;
1677}
1678
1679int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1680{
1681 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001682 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001683 uint64_t *p_segment_ids;
1684 uint32_t segment_nb;
1685 uint32_t i;
1686 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001687 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001688 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001689 DVR_Bool_t is_timeshift = DVR_FALSE;
Wentao MAcefc13c2022-10-26 15:47:24 +08001690 DVR_PlaybackSegmentFlag_t seg_flags = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001691
1692 DVR_RETURN_IF_FALSE(playback);
1693 DVR_RETURN_IF_FALSE(p_pids);
1694
hualing chenc110f952021-01-18 11:25:37 +08001695 ctx_record = NULL;
1696
1697 /*lock the recorder to avoid changing the recording segments*/
1698 ctx_record = ctx_getRecord(sn_timeshift_record);
1699
1700 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001701 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001702 if (!ctx_valid(ctx_record)
1703 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001704 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001705 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001706 is_timeshift = DVR_FALSE;
1707 } else {
1708 is_timeshift = DVR_TRUE;
1709 }
1710 }
1711
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001712 ctx = ctx_getPlayback((unsigned long)playback);
1713 DVR_RETURN_IF_FALSE(ctx);
1714
Gong Kefdb31922022-06-17 17:11:16 +08001715 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001716
Wentao MA804bab12022-11-29 10:01:26 +08001717 DVR_WRAPPER_INFO("libdvr_api, start_playback (sn:%ld) location:%s"
1718 " flags:0x%x v/a/ad/sub/pcr(%d:%d %d:%d %d:%d %d:%d %d)",
1719 ctx->sn,
1720 ctx->playback.param_open.location,
1721 flags,
1722 p_pids->video.pid, p_pids->video.format,
1723 p_pids->audio.pid, p_pids->audio.format,
1724 p_pids->ad.pid, p_pids->ad.format,
1725 p_pids->subtitle.pid, p_pids->subtitle.format,
1726 p_pids->pcr.pid);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001727
Gong Kefdb31922022-06-17 17:11:16 +08001728 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001729
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001730 if (ctx->playback.param_open.is_timeshift) {
1731 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001732 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001733 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001734 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001735 return DVR_FAILURE;
1736 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001737 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001738 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001739 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001740 }
1741
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001742 /*obtain all segments in a list*/
1743 segment_nb = 0;
1744 p_segment_ids = NULL;
1745 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001746 if (!error) {
1747 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001748 struct list_head info_list; /**< segment list head*/
1749 INIT_LIST_HEAD(&info_list);
1750
Wentao MA96f68962022-06-15 19:45:35 +08001751 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001752 //we need free info list buf when we used end.
1753 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001754 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001755 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001756 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1757 ctx->playback.param_open.location, error);
wentao.maf57dd232022-10-08 16:07:29 +08001758 // Tainted data issue originating from fgets seem false positive, so we
1759 // just suppress it here.
1760 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001761 for (i = 0; i < segment_nb; i++) {
1762 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001763 memset((void*)&seg_info,0,sizeof(seg_info));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001764
hualing chenb9a02922021-12-14 11:29:47 +08001765 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1766 if (error) {
1767 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001768 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001769 ctx->playback.param_open.location, p_segment_ids[i], error);
1770 break;
1771 }
1772 //add check if has audio or video pid. if not exist. not add segment to playback
1773 int ii = 0;
1774 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001775 // Tainted data issue originating from fgets seem false positive, so we
1776 // just suppress it here.
1777 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001778 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1779 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1780 if (type == DVR_STREAM_TYPE_VIDEO ||
1781 type == DVR_STREAM_TYPE_AUDIO ||
1782 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001783 DVR_WRAPPER_INFO("success to get seg av info \n");
1784 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 +08001785 DVR_STREAM_TYPE_VIDEO,
1786 DVR_STREAM_TYPE_AUDIO,
1787 DVR_STREAM_TYPE_AD);
1788 has_av = 1;
1789 //break;
1790 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001791 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 +08001792 DVR_STREAM_TYPE_VIDEO,
1793 DVR_STREAM_TYPE_AUDIO,
1794 DVR_STREAM_TYPE_AD);
1795 }
1796 }
1797 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001798 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001799 continue;
1800 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001801 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001802 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001803 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1804 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001805 if (error == DVR_FAILURE) {
1806 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001807 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001808 }
hualing chenb9a02922021-12-14 11:29:47 +08001809 /*copy the 1st segment*/
1810 if (got_1st_seg == 0) {
1811 seg_info_1st = seg_info;
1812 got_1st_seg = 1;
1813 }
1814 }
1815 } else {
wentao.maf57dd232022-10-08 16:07:29 +08001816 // Tainted data issue originating from fgets seem false positive, so we
1817 // just suppress it here.
1818 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001819 for (i = 0; i < segment_nb; i++) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001820 DVR_RecordSegmentInfo_t *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001821 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08001822 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001823 // prefetch() here incurring self_assign is used to avoid some compiling
1824 // warnings.
1825 // coverity[self_assign]
Wentao MA4d85ff32022-09-23 11:36:18 +08001826 list_for_each_entry(p_seg_info, &info_list, head)
hualing chenb9a02922021-12-14 11:29:47 +08001827 {
Wentao MA4d85ff32022-09-23 11:36:18 +08001828 if (p_seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001829 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001830 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001831 break;
1832 }
1833 }
1834 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001835 //last info is not found if when recording occured power off.
1836 if (p_segment_ids[i] == segment_nb - 1) {
1837 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001838 memset((void*)&seg_info,0,sizeof(seg_info));
hualing chen8aed9582021-12-24 17:59:56 +08001839
1840 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1841 if (error) {
1842 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001843 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001844 ctx->playback.param_open.location, p_segment_ids[i], error);
1845 break;
1846 }
1847 //
1848 //add check if has audio or video pid. if not exist. not add segment to playback
1849 int ii = 0;
1850 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001851 // Tainted data issue originating from fgets seem false positive, so we
1852 // just suppress it here.
1853 // coverity[tainted_data]
hualing chen8aed9582021-12-24 17:59:56 +08001854 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1855 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1856 if (type == DVR_STREAM_TYPE_VIDEO ||
1857 type == DVR_STREAM_TYPE_AUDIO ||
1858 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001859 DVR_WRAPPER_INFO("success to get seg av info \n");
1860 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 +08001861 DVR_STREAM_TYPE_VIDEO,
1862 DVR_STREAM_TYPE_AUDIO,
1863 DVR_STREAM_TYPE_AD);
1864 has_av = 1;
1865 //break;
1866 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001867 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 +08001868 DVR_STREAM_TYPE_VIDEO,
1869 DVR_STREAM_TYPE_AUDIO,
1870 DVR_STREAM_TYPE_AD);
1871 }
1872 }
1873 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001874 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001875 continue;
1876 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001877 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001878 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001879 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1880 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001881 if (error == DVR_FAILURE) {
1882 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chen8aed9582021-12-24 17:59:56 +08001883 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001884 }
hualing chen8aed9582021-12-24 17:59:56 +08001885 }
hualing chenb9a02922021-12-14 11:29:47 +08001886 continue;
1887 }
1888
1889 //add check if has audio or video pid. if not exist. not add segment to playback
1890 int ii = 0;
1891 int has_av = 0;
Wentao MA4d85ff32022-09-23 11:36:18 +08001892 for (ii = 0; ii < p_seg_info->nb_pids; ii++) {
1893 int type = (p_seg_info->pids[ii].type >> 24) & 0x0f;
hualing chenb9a02922021-12-14 11:29:47 +08001894 if (type == DVR_STREAM_TYPE_VIDEO ||
1895 type == DVR_STREAM_TYPE_AUDIO ||
1896 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001897 DVR_WRAPPER_INFO("success to get seg av info \n");
Wentao MA4d85ff32022-09-23 11:36:18 +08001898 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 +08001899 DVR_STREAM_TYPE_VIDEO,
1900 DVR_STREAM_TYPE_AUDIO,
1901 DVR_STREAM_TYPE_AD);
1902 has_av = 1;
1903 //break;
1904 } else {
Wentao MA4d85ff32022-09-23 11:36:18 +08001905 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 +08001906 DVR_STREAM_TYPE_VIDEO,
1907 DVR_STREAM_TYPE_AUDIO,
1908 DVR_STREAM_TYPE_AD);
1909 }
1910 }
1911 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001912 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001913 continue;
1914 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001915 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001916 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001917 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1918 error = wrapper_addPlaybackSegment(ctx, p_seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001919 if (error == DVR_FAILURE) {
1920 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001921 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001922 }
hualing chenb9a02922021-12-14 11:29:47 +08001923
1924 /*copy the 1st segment*/
1925 if (got_1st_seg == 0) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001926 seg_info_1st = *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001927 got_1st_seg = 1;
1928 }
hualing chen92f3a142020-07-08 20:59:33 +08001929 }
hualing chenb9a02922021-12-14 11:29:47 +08001930 //free list
1931 DVR_RecordSegmentInfo_t *segment = NULL;
1932 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1933 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1934 {
1935 if (segment) {
1936 list_del(&segment->head);
1937 free(segment);
1938 }
1939 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001940 }
hualing chenb9a02922021-12-14 11:29:47 +08001941
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001942 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001943
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001944 /* return if no segment or fail to add */
1945 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001946
Wentao MA270dc0f2022-08-23 13:17:26 +08001947 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001948 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1949 ctx->playback.obsolete = ctx_record->record.obsolete;
1950 }
1951
Wentao MA96f68962022-06-15 19:45:35 +08001952 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001953
1954 ctx->playback.reach_end = DVR_FALSE;
1955 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1956 ctx->playback.speed = 0.0f;
1957 else
1958 ctx->playback.speed = 100.0f;
1959
1960 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001961 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001962 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001963 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001964 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001965 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001966 error = dvr_playback_start(ctx->playback.player, flags);
1967 } else {
wentao.maa210e5e2022-10-12 16:10:03 +08001968 dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
hualing chen03fd4942021-07-15 15:56:41 +08001969 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001970 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001971 ctx->sn, seg_info_1st.id, error);
1972 }
Wentao MA96f68962022-06-15 19:45:35 +08001973 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001974 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001975 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 +08001976 }
1977 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001978
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001979 if (ctx->playback.param_open.is_timeshift) {
1980 /*unlock the recorder locked above*/
1981 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001982 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001983 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001984 ctx->sn, ctx_record->sn);
1985 }
1986 }
Gong Kefdb31922022-06-17 17:11:16 +08001987 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001988
1989 return error;
1990}
hualing chen002e5b92022-02-23 17:51:21 +08001991//stop record and playback
1992int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1993{
1994 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1995 int error;
Wentao MA804bab12022-11-29 10:01:26 +08001996 DVR_WRAPPER_INFO("libdvr_api, stop_timeshift");
hualing chen002e5b92022-02-23 17:51:21 +08001997
1998 //stop timeshift record
1999 ctx_record = ctx_getRecord(sn_timeshift_record);
wentao.maa210e5e2022-10-12 16:10:03 +08002000 dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
hualing chen002e5b92022-02-23 17:51:21 +08002001
Wentao MA96f68962022-06-15 19:45:35 +08002002 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08002003 //stop play
2004 error = dvr_wrapper_stop_playback(playback);
2005 //del timeshift file
2006 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002007 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08002008 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
2009 }
2010 return error;
2011}
2012//start record and start playback
2013int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
2014{
2015 DVR_WrapperCtx_t *ctx;
2016 DVR_RecordStartParams_t *start_param;
2017 int error;
2018
2019 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
2020 DVR_RETURN_IF_FALSE(ctx);
2021
Gong Kefdb31922022-06-17 17:11:16 +08002022 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002023 DVR_WRAPPER_INFO("libdvr_api, restart_timeshift (sn:%ld) location:%s",
2024 ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08002025 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002026
hualing chen451c8f72022-03-09 13:05:52 +08002027 {
2028 //clear old record status
2029 // struct {
2030 // DVR_WrapperRecordOpenParams_t param_open;
2031 // DVR_RecordStartParams_t param_start;
2032 // DVR_RecordStartParams_t param_update;
2033 // DVR_RecordHandle_t recorder;
2034 // DVR_RecordEventFunction_t event_fn;
2035 // void *event_userdata;
2036
2037 // /*total status = seg_status + status + obsolete*/
2038 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
2039 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
2040 // uint64_t next_segment_id;
2041
2042 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
2043 // } record;
2044 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
2045 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
2046 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2047 }
2048
hualing chen002e5b92022-02-23 17:51:21 +08002049 start_param = &ctx->record.param_start;
2050
2051 error = dvr_record_start_segment(ctx->record.recorder, start_param);
2052 {
2053 DVR_RecordSegmentInfo_t new_seg_info =
2054 { .id = start_param->segment.segment_id, };
2055 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08002056 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 +08002057 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
2058 DVR_RecordStartParams_t *update_param;
2059 update_param = &ctx->record.param_update;
2060 memcpy(update_param, start_param, sizeof(*update_param));
2061 int i = 0;
2062 for (i = 0; i < update_param->segment.nb_pids; i++)
2063 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08002064 }
2065
Wentao MA96f68962022-06-15 19:45:35 +08002066 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08002067
Gong Kefdb31922022-06-17 17:11:16 +08002068 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002069
2070 //start play
Wentao MA96f68962022-06-15 19:45:35 +08002071 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08002072 //clear play statue
2073 ctx = ctx_getPlayback((unsigned long)playback);
2074 if (ctx) {
Wentao MA4d85ff32022-09-23 11:36:18 +08002075 //reset old playback status
hualing chen451c8f72022-03-09 13:05:52 +08002076 // struct {
2077 // DVR_WrapperPlaybackOpenParams_t param_open;
2078 // DVR_PlaybackHandle_t player;
2079 // DVR_PlaybackEventFunction_t event_fn;
2080 // void *event_userdata;
2081
2082 // /*total status = seg_status + status*/
2083 // DVR_PlaybackStatus_t seg_status;
2084 // DVR_WrapperPlaybackStatus_t status;
2085 // DVR_PlaybackPids_t pids_req;
2086 // DVR_PlaybackEvent_t last_event;
2087 // float speed;
2088 // DVR_Bool_t reach_end;
2089
2090 // DVR_WrapperInfo_t obsolete;
2091 // DVR_Bool_t tf_full;
2092 // } playback;
Wentao MA4d85ff32022-09-23 11:36:18 +08002093 ctx->playback.tf_full = DVR_FALSE;
2094 ctx->playback.reach_end = DVR_FALSE;
hualing chen451c8f72022-03-09 13:05:52 +08002095 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2096 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2097 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2098 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2099 }
hualing chen002e5b92022-02-23 17:51:21 +08002100 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2101 return error;
2102}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002103
2104int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2105{
2106 DVR_WrapperCtx_t *ctx;
2107 int error;
2108
2109 DVR_RETURN_IF_FALSE(playback);
2110
2111 ctx = ctx_getPlayback((unsigned long)playback);
2112 DVR_RETURN_IF_FALSE(ctx);
2113
Gong Kefdb31922022-06-17 17:11:16 +08002114 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002115 DVR_WRAPPER_INFO("libdvr_api, stop_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002116 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002117
2118 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2119
2120 {
2121 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002122 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002123
wentao.mafd5283f2022-10-14 09:51:13 +08002124 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002125 // prefetch() here incurring self_assign is used to avoid some compiling
2126 // warnings.
2127 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002128 list_for_each_entry(p_seg, &ctx->segments, head) {
2129 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002130 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002131 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002132 }
2133 ctx_freeSegments(ctx);
2134 }
2135
Wentao MA96f68962022-06-15 19:45:35 +08002136 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002137 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002138
2139 return error;
2140}
2141
2142int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2143{
2144 DVR_WrapperCtx_t *ctx;
2145 int error;
2146
2147 DVR_RETURN_IF_FALSE(playback);
2148
2149 ctx = ctx_getPlayback((unsigned long)playback);
2150 DVR_RETURN_IF_FALSE(ctx);
2151
Gong Kefdb31922022-06-17 17:11:16 +08002152 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002153 DVR_WRAPPER_INFO("libdvr_api, pause_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002154 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002155 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002156 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002157 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002158
2159 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2160
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002161 ctx->playback.speed = 0.0f;
2162
Wentao MA96f68962022-06-15 19:45:35 +08002163 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002164 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002165
2166 return error;
2167}
2168
2169int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2170{
2171 DVR_WrapperCtx_t *ctx;
2172 int error;
2173
2174 DVR_RETURN_IF_FALSE(playback);
2175
2176 ctx = ctx_getPlayback((unsigned long)playback);
2177 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002178 //if set limit.we need check if seek to valid data when resume
2179 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2180 if (dvr_playback_check_limit(ctx->playback.player)) {
2181 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2182 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002183 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002184 ctx->sn, time_offset, expired);
2185 time_offset = expired;
2186 dvr_wrapper_seek_playback(playback, time_offset);
2187 }
2188 }
Gong Kefdb31922022-06-17 17:11:16 +08002189 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002190 DVR_WRAPPER_INFO("libdvr_api, resume_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002191 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002192
2193 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002194 ctx->playback.speed = 100.0f;
2195
Wentao MA96f68962022-06-15 19:45:35 +08002196 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002197 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002198
2199 return error;
2200}
2201
2202int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2203{
2204 DVR_WrapperCtx_t *ctx;
2205 int error;
2206 DVR_PlaybackSpeed_t dvr_speed = {
2207 .speed = { speed },
2208 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2209 };
2210
2211 DVR_RETURN_IF_FALSE(playback);
2212
2213 ctx = ctx_getPlayback((unsigned long)playback);
2214 DVR_RETURN_IF_FALSE(ctx);
2215
Gong Kefdb31922022-06-17 17:11:16 +08002216 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002217 DVR_WRAPPER_INFO("libdvr_api, set_playback_speed (sn:%ld) speed:%d", ctx->sn, (int)speed);
Gong Kefdb31922022-06-17 17:11:16 +08002218 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002219
2220 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2221
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002222 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2223 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2224 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002225 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002226 error = dvr_playback_resume(ctx->playback.player);
2227 } else if (ctx->playback.speed == 0.0f
2228 && speed != 0.0f
2229 && speed != 100.0f) {
2230 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002231 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002232 error = dvr_playback_resume(ctx->playback.player);
2233 }
2234
2235 ctx->playback.speed = speed;
2236
Wentao MA96f68962022-06-15 19:45:35 +08002237 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002238 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002239 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002240
2241 return error;
2242}
2243
hualing chen03fd4942021-07-15 15:56:41 +08002244int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2245{
2246 DVR_WrapperCtx_t *ctx;
2247 int error;
2248
2249 DVR_RETURN_IF_FALSE(playback);
2250
2251 ctx = ctx_getPlayback((unsigned long)playback);
2252 DVR_RETURN_IF_FALSE(ctx);
2253
Gong Kefdb31922022-06-17 17:11:16 +08002254 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002255
Wentao MA804bab12022-11-29 10:01:26 +08002256 DVR_WRAPPER_INFO("libdvr_api, setlimit_playback (sn:%ld) time:%lld, limit:%d",
2257 ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002258 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002259
2260 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002261 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002262
Gong Kefdb31922022-06-17 17:11:16 +08002263 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002264
2265 return error;
2266}
2267
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002268int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2269{
2270 DVR_WrapperCtx_t *ctx;
2271 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002272 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Wentao MA804bab12022-11-29 10:01:26 +08002273 uint64_t segment_id = ULLONG_MAX;
2274 uint32_t segment_offset = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002275
2276 DVR_RETURN_IF_FALSE(playback);
2277
2278 ctx = ctx_getPlayback((unsigned long)playback);
2279 DVR_RETURN_IF_FALSE(ctx);
2280
Gong Kefdb31922022-06-17 17:11:16 +08002281 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002282
Wentao MA804bab12022-11-29 10:01:26 +08002283 DVR_WRAPPER_INFO("libdvr_api, seek_playback (sn:%ld) offset:%dms", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002284 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002285
hualing chen03fd4942021-07-15 15:56:41 +08002286 //if set limit info we need check ts data is
2287 //expired when seek
2288 if (dvr_playback_check_limit(ctx->playback.player)) {
2289 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2290 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002291 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002292 ctx->sn, time_offset, expired);
2293 time_offset = expired;
2294 }
2295 }
2296
Wentao MA804bab12022-11-29 10:01:26 +08002297 const uint32_t obsolete_time = (uint32_t)ctx->playback.obsolete.time;
2298 DVR_WrapperPlaybackSegmentInfo_t *p_seg_first = ctx->segments.c_prev;
2299 DVR_WrapperPlaybackSegmentInfo_t *p_seg_last = ctx->segments.c_next;
2300 const uint64_t first_id = p_seg_first->seg_info.id;
2301 const uint64_t last_id = p_seg_last->seg_info.id;
2302 const uint32_t last_duration = p_seg_last->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002303
Wentao MA804bab12022-11-29 10:01:26 +08002304 if (time_offset <= obsolete_time) {
2305 segment_id = first_id;
2306 segment_offset = 0;
2307 DVR_WRAPPER_WARN("time_offset %u isn't greater than obsolete time %u, "
2308 "so seek to beginning position of segment %llu",
2309 time_offset,obsolete_time,segment_id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002310 } else {
Wentao MA804bab12022-11-29 10:01:26 +08002311 uint32_t total_duration = 0;
2312 // This error is suppressed as the macro code is picked from kernel.
2313 // prefetch() here incurring self_assign is used to avoid some compiling
2314 // warnings.
2315 // coverity[self_assign]
2316 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2317 const uint32_t segment_begin = obsolete_time + total_duration;
2318 const uint32_t segment_end = segment_begin + p_seg->seg_info.duration;
2319 if (time_offset >= segment_begin && time_offset <= segment_end) {
2320 segment_id = p_seg->seg_info.id;
2321 segment_offset = time_offset - segment_begin;
2322 break;
2323 }
2324 total_duration += p_seg->seg_info.duration;
2325 }
2326 if (segment_id == ULLONG_MAX) {
2327 segment_id = last_id;
2328 segment_offset = last_duration;
2329 DVR_WRAPPER_WARN("time_offset %u is out of range, so seek to"
2330 " the end position of segment %llu",time_offset,segment_id);
2331 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002332 }
2333
Wentao MA804bab12022-11-29 10:01:26 +08002334 DVR_WRAPPER_INFO("seek playback(sn:%ld) (segment_id:%llu, segment_offset:%u)\n",
2335 ctx->sn, segment_id, segment_offset);
2336 error = dvr_playback_seek(ctx->playback.player, segment_id, segment_offset);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002337
Gong Kefdb31922022-06-17 17:11:16 +08002338 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002339
2340 return error;
2341}
2342
2343int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2344{
2345 DVR_WrapperCtx_t *ctx;
2346 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002347 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002348
2349 DVR_RETURN_IF_FALSE(playback);
2350 DVR_RETURN_IF_FALSE(p_pids);
2351
2352 ctx = ctx_getPlayback((unsigned long)playback);
2353 DVR_RETURN_IF_FALSE(ctx);
2354
Gong Kefdb31922022-06-17 17:11:16 +08002355 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002356
Wentao MA804bab12022-11-29 10:01:26 +08002357 DVR_WRAPPER_INFO("libdvr_api, update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002358 ctx->sn,
2359 p_pids->video.pid, p_pids->video.format,
2360 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002361 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002362
2363 ctx->playback.pids_req = *p_pids;
2364
2365 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002366 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002367 // prefetch() here incurring self_assign is used to avoid some compiling
2368 // warnings.
2369 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002370 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002371 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002372 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2373 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2374 /*check update for pids*/
2375 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2376 p_seg->playback_info.pids = *p_pids;
2377 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002378 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002379 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002380 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002381 /*do not break, let list updated*/
2382 }
2383 }
2384 }
2385 /*break;*/
2386 }
2387 }
2388
Wentao MA96f68962022-06-15 19:45:35 +08002389 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002390 ctx->sn,
2391 p_pids->video.pid, p_pids->video.format,
2392 p_pids->audio.pid, p_pids->audio.format,
2393 error);
2394
Gong Kefdb31922022-06-17 17:11:16 +08002395 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002396
2397 return error;
2398}
2399
hualing chena5f03222021-12-02 11:22:35 +08002400int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2401{
2402 DVR_WrapperCtx_t *ctx;
2403 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002404 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002405
2406 DVR_RETURN_IF_FALSE(playback);
2407 DVR_RETURN_IF_FALSE(p_pids);
2408
2409 ctx = ctx_getPlayback((unsigned long)playback);
2410 DVR_RETURN_IF_FALSE(ctx);
2411
Gong Kefdb31922022-06-17 17:11:16 +08002412 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002413
Wentao MA804bab12022-11-29 10:01:26 +08002414 DVR_WRAPPER_INFO("libdvr_api, only_update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
hualing chena5f03222021-12-02 11:22:35 +08002415 ctx->sn,
2416 p_pids->video.pid, p_pids->video.format,
2417 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002418 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002419
2420 ctx->playback.pids_req = *p_pids;
2421
2422 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002423 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002424 // prefetch() here incurring self_assign is used to avoid some compiling
2425 // warnings.
2426 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002427 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002428 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002429 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2430 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2431 /*check update for pids*/
2432 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2433 p_seg->playback_info.pids = *p_pids;
2434 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002435 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002436 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002437 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002438 /*do not break, let list updated*/
2439 }
2440 }
2441 }
2442 /*break;*/
2443 }
2444 }
2445
Wentao MA96f68962022-06-15 19:45:35 +08002446 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002447 ctx->sn,
2448 p_pids->video.pid, p_pids->video.format,
2449 p_pids->audio.pid, p_pids->audio.format,
2450 error);
2451
Gong Kefdb31922022-06-17 17:11:16 +08002452 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002453
2454 return error;
2455}
2456
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002457int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2458{
2459 DVR_WrapperCtx_t *ctx;
2460 DVR_WrapperPlaybackStatus_t s;
2461 DVR_PlaybackStatus_t play_status;
2462 int error;
2463
2464 DVR_RETURN_IF_FALSE(playback);
2465 DVR_RETURN_IF_FALSE(status);
2466
2467 ctx = ctx_getPlayback((unsigned long)playback);
2468 DVR_RETURN_IF_FALSE(ctx);
2469
Gong Kefdb31922022-06-17 17:11:16 +08002470 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002471
Gong Kefdb31922022-06-17 17:11:16 +08002472 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002473
Wentao MA804bab12022-11-29 10:01:26 +08002474 dvr_playback_get_status(ctx->playback.player, &play_status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002475
2476 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002477 error = process_generatePlaybackStatus(ctx, &s);
2478
hualing chenb5cd42e2020-04-15 17:03:34 +08002479 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2480 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002481 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002482 s.info_cur.time = s.info_full.time;
2483 }
Wentao MA804bab12022-11-29 10:01:26 +08002484 DVR_WRAPPER_INFO("get_playback_status (sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2485 ctx->sn, s.state, s.info_cur.time, s.info_full.time, s.info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002486
2487 *status = s;
2488
Gong Kefdb31922022-06-17 17:11:16 +08002489 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002490
2491 return error;
2492}
2493
hualing chen266b9502020-04-04 17:39:39 +08002494int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2495{
2496 DVR_WrapperCtx_t *ctx;
2497 int error;
2498
2499 DVR_RETURN_IF_FALSE(playback);
2500 DVR_RETURN_IF_FALSE(p_secure_buf);
2501
2502 ctx = ctx_getPlayback((unsigned long)playback);
2503 DVR_RETURN_IF_FALSE(ctx);
2504
Gong Kefdb31922022-06-17 17:11:16 +08002505 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002506 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002507 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002508 return error;
2509}
2510
2511int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2512{
2513 DVR_WrapperCtx_t *ctx;
2514 int error;
2515
2516 DVR_RETURN_IF_FALSE(playback);
2517 DVR_RETURN_IF_FALSE(func);
2518
2519 ctx = ctx_getPlayback((unsigned long)playback);
2520 DVR_RETURN_IF_FALSE(ctx);
2521
Gong Kefdb31922022-06-17 17:11:16 +08002522 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002523 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002524 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002525 return error;
2526}
2527
Zhiqiang Han620b9252021-11-09 14:23:20 +08002528int dvr_wrapper_segment_del_by_location (const char *location)
2529{
2530 char fpath[DVR_MAX_LOCATION_SIZE];
2531
2532 DVR_RETURN_IF_FALSE(location);
2533
2534 /*del the stats file*/
2535 sprintf(fpath, "%s.stats", location);
2536 unlink(fpath);
2537
2538 return dvr_segment_del_by_location(location);
2539}
2540
2541int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2542{
2543 FILE *fp;
2544 char fpath[DVR_MAX_LOCATION_SIZE];
2545
2546 DVR_RETURN_IF_FALSE(location);
2547 DVR_RETURN_IF_FALSE(p_info);
2548
2549 if (p_info)
2550 memset(p_info, 0, sizeof(p_info[0]));
2551
2552 memset(fpath, 0, sizeof(fpath));
2553 sprintf(fpath, "%s.stats", location);
2554
2555 /*stats file exists*/
2556 if ((fp = fopen(fpath, "r"))) {
2557 char buf[256];
2558
2559 if (fgets(buf, sizeof(buf), fp) != NULL
2560 && (sscanf(buf, ":%llu:%lu:%u",
2561 &p_info->size,
2562 &p_info->time,
2563 &p_info->pkts) == 3)) {
2564 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002565 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 +08002566 return DVR_SUCCESS;
2567 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002568 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002569 }
2570
2571 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002572 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002573
2574 int error;
2575 uint32_t n_ids;
2576 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002577
hualing chen8aed9582021-12-24 17:59:56 +08002578 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002579
Zhiqiang Han620b9252021-11-09 14:23:20 +08002580 if (!error) {
2581 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002582 struct list_head info_list; /**< segment list head*/
2583 INIT_LIST_HEAD(&info_list);
2584
2585 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002586 error = dvr_segment_get_allInfo(location, &info_list);
2587 if (error == DVR_FAILURE) {
wentao.maa210e5e2022-10-12 16:10:03 +08002588 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2589 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
hualing chenb9a02922021-12-14 11:29:47 +08002590
2591 memset(&info, 0, sizeof(info));
2592 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002593 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002594 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002595
wentao.maf57dd232022-10-08 16:07:29 +08002596 // Tainted data issue originating from fgets seem false positive, so we
2597 // just suppress it here.
2598 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002599 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002600 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002601 if (!error) {
2602 p_info->size += info.size;
2603 p_info->time += info.duration;
2604 p_info->pkts += info.nb_packets;
2605 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002606 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002607 break;
2608 }
2609 }
2610 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002611 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
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++) {
2616
2617 DVR_RecordSegmentInfo_t *seg_info;
2618 DVR_PlaybackSegmentFlag_t flags;
2619 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002620 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002621 // prefetch() here incurring self_assign is used to avoid some compiling
2622 // warnings.
2623 // coverity[self_assign]
hualing chenb9a02922021-12-14 11:29:47 +08002624 list_for_each_entry(seg_info, &info_list, head)
2625 {
hualing chen8aed9582021-12-24 17:59:56 +08002626 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002627 found = 1;
2628 break;
2629 }
2630 }
2631 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002632 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 +08002633 if (p_ids[i] == n_ids - 1) {
wentao.maa210e5e2022-10-12 16:10:03 +08002634 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2635 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
Wentao MA96f68962022-06-15 19:45:35 +08002636 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 +08002637 error = dvr_segment_get_info(location, p_ids[i], &info);
2638 if (!error) {
2639 p_info->size += info.size;
2640 p_info->time += info.duration;
2641 p_info->pkts += info.nb_packets;
2642 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002643 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002644 break;
2645 }
2646 }
hualing chenb9a02922021-12-14 11:29:47 +08002647 continue;
2648 }
2649
2650 if (!error) {
2651 p_info->size += seg_info->size;
2652 p_info->time += seg_info->duration;
2653 p_info->pkts += seg_info->nb_packets;
2654 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002655 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002656 break;
2657 }
2658 }
2659 //free list
2660 DVR_RecordSegmentInfo_t *segment = NULL;
2661 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2662 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2663 {
2664 if (segment) {
2665 list_del(&segment->head);
2666 free(segment);
2667 }
2668 }
2669 }
2670 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002671 } else {
2672 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002673 }
Wentao MA96f68962022-06-15 19:45:35 +08002674 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 +08002675
2676 return (error)? DVR_FAILURE : DVR_SUCCESS;
2677}
2678
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002679static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2680{
wentao.maa210e5e2022-10-12 16:10:03 +08002681 DVR_WrapperEventCtx_t evt = {
2682 .sn = (unsigned long)userdata,
2683 .type = W_REC,
2684 .record.event = event,
2685 .record.status = *(DVR_RecordStatus_t *)params
2686 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002687
2688 DVR_RETURN_IF_FALSE(userdata);
2689
Wentao MA804bab12022-11-29 10:01:26 +08002690 DVR_WRAPPER_DEBUG("record event 0x%x (sn:%ld)", evt.record.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002691 return ctx_addRecordEvent(&evt);
2692}
2693
2694static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2695{
wentao.maa210e5e2022-10-12 16:10:03 +08002696 DVR_WrapperEventCtx_t evt = {
2697 .sn = (unsigned long)userdata,
2698 .type = W_PLAYBACK,
2699 .playback.event = event,
2700 .playback.status = *(DVR_Play_Notify_t *)params
2701 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002702
2703 DVR_RETURN_IF_FALSE(userdata);
2704
2705 evt.sn = (unsigned long)userdata;
2706 evt.type = W_PLAYBACK;
2707 evt.playback.event = event;
2708 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA804bab12022-11-29 10:01:26 +08002709 DVR_WRAPPER_DEBUG("playback event 0x%x (sn:%ld)", evt.playback.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002710 return ctx_addPlaybackEvent(&evt);
2711}
2712
2713static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2714{
Wentao MA270dc0f2022-08-23 13:17:26 +08002715 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 +08002716 ctx->sn,
2717 evt,
2718 status->info.time,
2719 status->info.size,
2720 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002721 status->info_obsolete.time,
2722 status->info_obsolete.size,
2723 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002724
2725 if (ctx->record.event_fn)
2726 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2727 return 0;
2728}
2729
Zhiqiang Han620b9252021-11-09 14:23:20 +08002730static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2731{
2732 FILE *fp;
2733 char fpath[DVR_MAX_LOCATION_SIZE];
2734
2735 DVR_RETURN_IF_FALSE(location);
2736 DVR_RETURN_IF_FALSE(p_status);
2737
2738 sprintf(fpath, "%s.stats", location);
2739
2740 /*stats file*/
2741 if ((fp = fopen(fpath, "w"))) {
2742 char buf[256];
2743 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2744 p_status->info.size - p_status->info_obsolete.size,
2745 p_status->info.time - p_status->info_obsolete.time,
2746 p_status->info.pkts - p_status->info_obsolete.pkts);
2747 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002748 fflush(fp);
2749 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002750 fclose(fp);
2751 return DVR_SUCCESS;
2752 }
2753
2754 return DVR_FAILURE;
2755}
2756
2757
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002758static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2759{
2760 DVR_RecordStartParams_t param;
2761 DVR_RecordSegmentInfo_t seg_info;
2762 int i;
2763 int error;
2764
2765 memcpy(&param, &ctx->record.param_update, sizeof(param));
2766 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2767 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2768 for (i = 0; i < param.segment.nb_pids; i++) {
2769 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2770 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2771 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2772 ctx->record.param_update.segment.nb_pids++;
2773 }
2774 }
2775 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2776 {
2777 DVR_RecordSegmentInfo_t new_seg_info =
2778 { .id = ctx->record.param_update.segment.segment_id, };
2779 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2780 wrapper_addRecordSegment(ctx, &new_seg_info);
2781 }
2782
Wentao MA96f68962022-06-15 19:45:35 +08002783 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002784 return error;
2785}
2786
Wentao MA270dc0f2022-08-23 13:17:26 +08002787static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002788{
Wentao MA270dc0f2022-08-23 13:17:26 +08002789 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002790}
2791
2792/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002793static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002794{
2795 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002796 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002797
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002798 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002799 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2800
2801 ctx->record.status.state = ctx->record.seg_status.state;
2802 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2803 memcpy(ctx->record.status.pids.pids,
2804 ctx->record.seg_status.info.pids,
2805 sizeof(ctx->record.status.pids.pids));
2806 ctx->current_segment_id = ctx->record.seg_status.info.id;
2807
wentao.mafd5283f2022-10-14 09:51:13 +08002808 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002809 // prefetch() here incurring self_assign is used to avoid some compiling
2810 // warnings.
2811 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002812 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2813 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2814 ctx->record.status.info.time += p_seg->info.duration;
2815 ctx->record.status.info.size += p_seg->info.size;
2816 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002817 }
2818 }
2819
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002820 ctx->record.status.info_obsolete = ctx->record.obsolete;
2821
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002822 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002823
2824 if (status) {
2825 *status = ctx->record.status;
2826 status->info.time += ctx->record.seg_status.info.duration;
2827 status->info.size += ctx->record.seg_status.info.size;
2828 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2829 }
2830
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002831 return DVR_SUCCESS;
2832}
2833
2834
2835static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2836{
2837 DVR_WrapperRecordStatus_t status;
2838
2839 memset(&status, 0, sizeof(status));
2840
Wentao MA804bab12022-11-29 10:01:26 +08002841 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d)\n",
2842 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002843 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002844 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002845 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002846 return 0;
2847 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002848 switch (evt->record.event)
2849 {
2850 case DVR_RECORD_EVENT_STATUS:
2851 {
2852 switch (evt->record.status.state)
2853 {
2854 case DVR_RECORD_STATE_OPENED:
2855 case DVR_RECORD_STATE_CLOSED:
2856 {
2857 ctx->record.seg_status = evt->record.status;
2858
2859 status.state = evt->record.status.state;
2860 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002861 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002862 } break;
2863 case DVR_RECORD_STATE_STARTED:
2864 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002865 ctx->record.seg_status = evt->record.status;
2866
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002867 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002868 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002869 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002870
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002871 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002872 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002873 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002874 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 +08002875 ctx->sn,
2876 evt->record.status.info.size,
2877 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002878 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2879 /*should notify the recording's stop*/
2880 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002881 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002882 ctx->sn, error);
2883 status.state = DVR_RECORD_STATE_CLOSED;
2884 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002885 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002886 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002887 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002888
2889 if (ctx->record.param_open.is_timeshift
2890 && ctx->record.param_open.max_time
2891 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002892 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002893
2894 /*as the player do not support null playlist,
2895 there must be one segment existed at any time,
2896 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002897 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2898 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002899 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002900 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 +08002901 status.info.size,
2902 ctx->record.param_open.max_time,
2903 ctx->record.param_open.segment_size);
2904 } else {
2905 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002906 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002907
2908 process_generateRecordStatus(ctx, &status);
2909 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002910 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002911 }
2912 }
2913
Wentao MAf4072032022-06-30 13:50:45 +08002914 const loff_t actual_size = status.info.size;
2915 const loff_t max_size = ctx->record.param_open.max_size;
2916 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002917
Wentao MAf4072032022-06-30 13:50:45 +08002918 if (ctx->record.param_open.is_timeshift && max_size) {
2919 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002920 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002921 /*as the player do not support null playlist,
2922 there must be one segment existed at any time,
2923 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002924 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2925 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002926 /*only one segment, waiting for more*/
2927 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2928 actual_size, segment_size);
2929 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002930 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002931
2932 process_generateRecordStatus(ctx, &status);
2933 process_notifyRecord(ctx, evt->record.event, &status);
2934 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2935 }
2936 if (actual_size >= max_size + segment_size/2) {
2937 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2938 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002939 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002940 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002941 }
2942 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002943 } break;
2944 case DVR_RECORD_STATE_STOPPED:
2945 {
2946 ctx->record.seg_status = evt->record.status;
2947
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002948 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002949 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002950 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002951 } break;
2952 default:
2953 break;
2954 }
2955 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002956 case DVR_RECORD_EVENT_WRITE_ERROR: {
2957 ctx->record.seg_status = evt->record.status;
2958 status.state = evt->record.status.state;
2959 process_notifyRecord(ctx, evt->record.event, &status);
2960 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002961 default:
2962 break;
2963 }
2964 return DVR_SUCCESS;
2965}
2966
2967static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2968{
Wentao MA80179512022-11-03 12:20:03 +08002969 DVR_RETURN_IF_FALSE(ctx->playback.event_fn != NULL);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002970
Wentao MA80179512022-11-03 12:20:03 +08002971 const time_t cur_time = status->info_cur.time;
2972 const time_t full_time = status->info_full.time;
2973 const time_t obsolete_time = status->info_obsolete.time;
2974 const time_t origin_offset = cur_time + obsolete_time;
2975 DVR_WRAPPER_INFO("playback progress notify(sn:%ld) evt(0x%x)"
2976 " actual_slider_pos: %02d:%02d:%02d.%03d/%02d:%02d:%02d.%03d (%7ld ms/%7ld ms),"
2977 " offset_from_origin: %02d:%02d:%02d.%03d (%7ld ms),"
2978 " dump status:state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2979 ctx->sn,evt,cur_time/1000/3600,cur_time/1000%3600/60,cur_time/1000%60,cur_time%1000,
2980 full_time/1000/3600,full_time/1000%3600/60,full_time/1000%60,full_time%1000,
2981 cur_time,full_time,
2982 origin_offset/1000/3600,origin_offset/1000%3600/60,origin_offset/1000%60,origin_offset%1000,
2983 origin_offset,status->state,cur_time,full_time,obsolete_time);
2984
2985 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002986}
2987
2988/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002989static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002990{
2991 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002992 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002993
2994 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2995 ctx->playback.status.pids = ctx->playback.pids_req;
2996
2997 ctx->playback.status.state = ctx->playback.seg_status.state;
2998 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2999 ctx->playback.status.flags = ctx->playback.seg_status.flags;
3000 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
3001
wentao.mafd5283f2022-10-14 09:51:13 +08003002 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003003 // prefetch() here incurring self_assign is used to avoid some compiling
3004 // warnings.
3005 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003006 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3007 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003008 break;
hualing chen451c8f72022-03-09 13:05:52 +08003009 }
3010
Wentao MA270dc0f2022-08-23 13:17:26 +08003011 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
3012 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
3013 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003014 }
wentao.mafd5283f2022-10-14 09:51:13 +08003015 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003016 // prefetch() here incurring self_assign is used to avoid some compiling
3017 // warnings.
3018 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003019 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3020 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
3021 ctx->playback.status.info_full.size += p_seg->seg_info.size;
3022 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003023 }
3024
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003025 if (status) {
3026 *status = ctx->playback.status;
3027 /*deal with current, lack size and pkts with the current*/
3028 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08003029 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08003030 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08003031
Wentao MA270dc0f2022-08-23 13:17:26 +08003032 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
3033 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08003034 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
3035 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08003036 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 +08003037 }
3038 else
3039 {
Wentao MA804bab12022-11-29 10:01:26 +08003040 status->info_obsolete.time = ctx->playback.obsolete.time;
3041 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
hualing chen56c0a162022-01-27 17:01:50 +08003042 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003043 }
3044
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003045 return DVR_SUCCESS;
3046}
3047
3048static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3049{
Wentao MA804bab12022-11-29 10:01:26 +08003050 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003051 evt->sn, evt->playback.event,
3052 evt->playback.status.play_status.state,
3053 evt->playback.status.play_status.segment_id,
3054 evt->playback.status.play_status.time_cur,
3055 evt->playback.status.play_status.time_end);
3056
3057 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08003058 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
3059 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
3060 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
3061 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003062 ctx->playback.last_event = evt->playback.event;
3063
3064 switch (evt->playback.event)
3065 {
3066 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
3067 case DVR_PLAYBACK_EVENT_REACHED_END:
3068 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
3069 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08003070 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08003071 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08003072 case DVR_PLAYBACK_EVENT_NODATA:
3073 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003074 {
3075 DVR_WrapperPlaybackStatus_t status;
3076
3077 /*copy status of segment*/
3078 ctx->playback.seg_status = evt->playback.status.play_status;
3079
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003080 /*generate status of the whole playback*/
3081 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003082
3083 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08003084 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08003085 if (ctx->playback.param_open.is_timeshift
3086 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003087 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08003088 }
3089 /*trust the low level, make NO check.
3090 As this evt is changed to only once due to some operations(paused) in low level.
3091 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003092 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08003093 }
3094 */
3095 else {
3096 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08003097 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003098 }
wentao.mab9fe0ff2023-07-05 09:55:41 +08003099 } else {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003100 process_notifyPlayback(ctx, evt->playback.event, &status);
3101 }
3102 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003103 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
3104 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
3105 case DVR_PLAYBACK_EVENT_NO_KEY:
3106 {
Wentao MA96f68962022-06-15 19:45:35 +08003107 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003108 } break;
3109 default:
3110 {
Wentao MA96f68962022-06-15 19:45:35 +08003111 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003112 } break;
3113 }
3114 return 0;
3115}
3116
3117static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3118{
3119 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
3120}
3121
Wentao MA96f68962022-06-15 19:45:35 +08003122int dvr_wrapper_set_log_level (int level)
3123{
Wentao MA804bab12022-11-29 10:01:26 +08003124 DVR_WRAPPER_INFO("libdvr_api, set_log_level %d", level);
Wentao MA96f68962022-06-15 19:45:35 +08003125 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3126 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3127 return DVR_FAILURE;
3128 }
3129 g_dvr_log_level = level;
3130 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3131 return DVR_SUCCESS;
3132}
3133
Wentao MA5629ad82022-08-24 10:03:02 +08003134int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3135{
3136 DVR_WrapperCtx_t *ctx;
3137 int error;
3138
Wentao MA3e2dc452022-12-20 11:17:16 +08003139 DVR_RETURN_IF_FALSE(playback!=NULL);
Wentao MA5629ad82022-08-24 10:03:02 +08003140
3141 ctx = ctx_getPlayback((unsigned long)playback);
3142 DVR_RETURN_IF_FALSE(ctx);
3143
3144 wrapper_mutex_lock(&ctx->wrapper_lock);
3145
3146 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3147
Wentao MA804bab12022-11-29 10:01:26 +08003148 DVR_WRAPPER_INFO("libdvr_api, set_ac4_preselection_id %d", presel_id);
Wentao MA5629ad82022-08-24 10:03:02 +08003149 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3150
3151 wrapper_mutex_unlock(&ctx->wrapper_lock);
3152
3153 return error;
3154}
3155
wentao ma7d642782022-10-23 18:26:16 -07003156int dvr_wrapper_property_set(const char* prop_name, const char* prop_value)
3157{
3158 return dvr_prop_write(prop_name,prop_value);
3159}
3160
3161int dvr_wrapper_property_get(const char* prop_name, char* prop_value, int length)
3162{
3163 return dvr_prop_read(prop_name,prop_value,length);
3164}
3165