blob: ce9b19084ccc6bc56eb4881da3ab900079535bc9 [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 while (thread_ctx->running) {
Zhiqiang Han055aac72023-09-04 16:52:22 +0800610 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800611
Zhiqiang Han055aac72023-09-04 16:52:22 +0800612 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
613 if (!evt) {
614 pthread_mutex_lock(&thread_ctx->lock);
615 ret = wrapper_threadWait(thread_ctx);
616 pthread_mutex_unlock(&thread_ctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800617 }
618
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800619 while (evt) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800620 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
621 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chenbc0aec92021-03-18 14:52:40 +0800622 if (ctx == NULL) {
Wentao MA804bab12022-11-29 10:01:26 +0800623 DVR_WRAPPER_ERROR("Wrapper context is NULL");
hualing chenbc0aec92021-03-18 14:52:40 +0800624 goto processed;
625 }
Wentao MA804bab12022-11-29 10:01:26 +0800626 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 +0800627 if (thread_ctx->running) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800628 /*
629 continue not break,
630 make all events consumed, or mem leak
631 */
Wentao MA270dc0f2022-08-23 13:17:26 +0800632 if (!wrapper_mutex_lock_if(&ctx->wrapper_lock, &thread_ctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800633 goto processed;
634
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800635 if (ctx_valid(ctx)) {
636 /*double check after lock*/
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800637 if (evt->sn == ctx->sn) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800638 process_handleEvents(evt, ctx);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800639 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800640 }
Zhiqiang Han055aac72023-09-04 16:52:22 +0800641
Gong Kefdb31922022-06-17 17:11:16 +0800642 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800643 }
644
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800645processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800646 ctx_freeEvent(evt);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800647
Wentao MA270dc0f2022-08-23 13:17:26 +0800648 evt = (thread_ctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800649 }
650 }
651
Wentao MA804bab12022-11-29 10:01:26 +0800652 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 +0800653 return NULL;
654}
655
656static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
657{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800658 pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800659 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
660 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800661 pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800662 return 0;
663}
664
665static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
666{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800667 pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800668 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
Zhiqiang Han055aac72023-09-04 16:52:22 +0800669 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800670 pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800671 return 0;
672}
673
674static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
675{
Wentao MA270dc0f2022-08-23 13:17:26 +0800676 DVR_WrapperPlaybackSegmentInfo_t *p_seg, *p_seg_tmp;
677 list_for_each_entry_safe(p_seg, p_seg_tmp, &ctx->segments, head) {
678 list_del(&p_seg->head);
679 free(p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800680 }
681}
682
Wentao MA270dc0f2022-08-23 13:17:26 +0800683static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800684 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
685{
686 (void)ctx;
687 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800688 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800689 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800690 p_seg->seg_info.nb_pids = seg_info->nb_pids;
691 memcpy(p_seg->seg_info.pids, seg_info->pids, sizeof(p_seg->seg_info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800692 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800693 p_seg->seg_info.duration = seg_info->duration;
694 p_seg->seg_info.size = seg_info->size;
695 p_seg->seg_info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800696 }
hualing chen03fd4942021-07-15 15:56:41 +0800697 //update current segment duration on timeshift mode
hualing chenb9b358a2021-08-17 15:06:36 +0800698 if (ctx->playback.param_open.is_timeshift
699 || ctx_isPlay_recording(ctx->playback.param_open.location))
Wentao MA270dc0f2022-08-23 13:17:26 +0800700 dvr_playback_update_duration(ctx->playback.player,p_seg->seg_info.id,p_seg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800701 /*no changes
702 DVR_PlaybackSegmentFlag_t flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800703 p_seg->playback_info.segment_id = p_seg->seg_info.id;
704 strncpy(p_seg->playback_info.location,
705 ctx->playback.param_open.location, sizeof(p_seg->playback_info.location));
706 p_seg->playback_info.pids = ctx->playback.pids_req;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800707 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
708 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
709 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
Wentao MA270dc0f2022-08-23 13:17:26 +0800710 p_seg->playback_info.flags = flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800711 */
712}
713
714static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
715{
Wentao MA270dc0f2022-08-23 13:17:26 +0800716 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800717
Wentao MA96f68962022-06-15 19:45:35 +0800718 DVR_WRAPPER_INFO("timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800719 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
720
721 if (list_empty(&ctx->segments)) {
Wentao MA96f68962022-06-15 19:45:35 +0800722 DVR_WRAPPER_INFO("timeshift, update while no segment exists, ignore\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800723 return DVR_SUCCESS;
724 }
725
726 /*normally, the last segment added will be updated*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800727 p_seg =
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800728 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800729 if (p_seg->seg_info.id == seg_info->id) {
730 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800731 } else {
wentao.mafd5283f2022-10-14 09:51:13 +0800732 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +0800733 // prefetch() here incurring self_assign is used to avoid some compiling
734 // warnings.
735 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +0800736 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
737 if (p_seg->seg_info.id == seg_info->id) {
738 _updatePlaybackSegment(p_seg, seg_info, update_flags, ctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800739 break;
740 }
741 }
742 }
743
744 /*need to notify the dvr_playback*/
hualing chenb9b358a2021-08-17 15:06:36 +0800745 if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/
746 || ctx_isPlay_recording(ctx->playback.param_open.location))
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800747 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
748 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
749 if (
750 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
751 (ctx->playback.seg_status.segment_id == seg_info->id
Zhiqiang Han9b0b7292023-02-21 17:15:32 +0800752 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME))
753 && ctx->playback.speed != 0.0f)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800754 ||
755 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
756 (ctx->playback.seg_status.segment_id != seg_info->id
Zhiqiang Han9b0b7292023-02-21 17:15:32 +0800757 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME)
758 && ctx->playback.speed != 0.0f)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800759 )
760 {
761 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800762 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800763 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800764 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800765
766 error = dvr_playback_resume(ctx->playback.player);
Wentao MA96f68962022-06-15 19:45:35 +0800767 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 +0800768 ctx->sn, error,
769 seg_info->id, seg_info->duration,
770 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
771 }
772 }
773
774 return DVR_SUCCESS;
775}
776
Wentao MA270dc0f2022-08-23 13:17:26 +0800777static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *p_seg,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800778 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
779{
780 (void)ctx;
781 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
Wentao MA270dc0f2022-08-23 13:17:26 +0800782 p_seg->info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800783 else if (update_flags & U_PIDS) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800784 p_seg->info.nb_pids = seg_info->nb_pids;
785 memcpy(p_seg->info.pids, seg_info->pids, sizeof(p_seg->info.pids));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800786 } else if (update_flags & U_STAT) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800787 p_seg->info.duration = seg_info->duration;
788 p_seg->info.size = seg_info->size;
789 p_seg->info.nb_packets = seg_info->nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800790 }
791}
792
793static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
794{
Wentao MA270dc0f2022-08-23 13:17:26 +0800795 DVR_WrapperRecordSegmentInfo_t *p_seg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800796
797 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800798 if (!list_empty(&ctx->segments)) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800799 p_seg =
hualing chen266b9502020-04-04 17:39:39 +0800800 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
Wentao MA270dc0f2022-08-23 13:17:26 +0800801 if (p_seg->info.id == seg_info->id) {
802 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800803 } else {
wentao.mafd5283f2022-10-14 09:51:13 +0800804 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +0800805 // prefetch() here incurring self_assign is used to avoid some compiling
806 // warnings.
807 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +0800808 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
809 if (p_seg->info.id == seg_info->id) {
810 _updateRecordSegment(p_seg, seg_info, update_flags, ctx);
hualing chen266b9502020-04-04 17:39:39 +0800811 break;
812 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800813 }
814 }
815 }
816
817 /*timeshift, update the segment for playback*/
818 /*
819 the playback should grab the segment info other than the id,
820 and the id will be updated by each segment-add during the recording
821 */
822 /*
823 the playback paused if no data been checked from recording,
824 should resume the player later when there's more data
825 */
hualing chenb9b358a2021-08-17 15:06:36 +0800826 int sn = 0;
827 if (ctx->record.param_open.is_timeshift ||
828 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
829 DVR_WrapperCtx_t *ctx_playback;
830 if (ctx->record.param_open.is_timeshift)
831 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
832 else
833 ctx_playback = ctx_getPlayback(sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800834
835 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800836 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800837 if (ctx_valid(ctx_playback)
hualing chenb9b358a2021-08-17 15:06:36 +0800838 && (ctx_playback->sn == sn_timeshift_playback ||
839 ctx_playback->sn == sn)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800840 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
841 }
Gong Kefdb31922022-06-17 17:11:16 +0800842 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800843 }
844 }
845
846 return DVR_SUCCESS;
847}
848
849static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
850 DVR_RecordSegmentInfo_t *seg_info,
851 DVR_PlaybackPids_t *p_pids,
852 DVR_PlaybackSegmentFlag_t flags)
853{
Wentao MA270dc0f2022-08-23 13:17:26 +0800854 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800855 int error;
856
857 error = 0;
Wentao MA270dc0f2022-08-23 13:17:26 +0800858 p_seg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
859 if (!p_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800860 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +0800861 DVR_WRAPPER_INFO("memory fail\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800862 return error;
863 }
864
Wentao MA270dc0f2022-08-23 13:17:26 +0800865 /*copy the original segment info*/
866 p_seg->seg_info = *seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800867 /*generate the segment info used in playback*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800868 p_seg->playback_info.segment_id = p_seg->seg_info.id;
Wentao MAe88ad702022-09-02 10:35:00 +0800869 const int len = strlen(ctx->playback.param_open.location);
870 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
871 DVR_WRAPPER_ERROR("Invalid playback.param_open.location length %d", len);
Wentao MA4d85ff32022-09-23 11:36:18 +0800872 free(p_seg);
Wentao MAe88ad702022-09-02 10:35:00 +0800873 return DVR_FAILURE;
874 }
875 strncpy(p_seg->playback_info.location, ctx->playback.param_open.location, len+1);
Wentao MA270dc0f2022-08-23 13:17:26 +0800876 p_seg->playback_info.pids = *p_pids;
877 p_seg->playback_info.flags = flags;
Wentao MA270dc0f2022-08-23 13:17:26 +0800878 p_seg->playback_info.duration = p_seg->seg_info.duration;
wentao.maa22bc852022-10-13 12:18:06 +0800879 list_add(p_seg, &ctx->segments);
880 DVR_WRAPPER_INFO("start to add segment %lld\n", p_seg->playback_info.segment_id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800881
Wentao MA270dc0f2022-08-23 13:17:26 +0800882 error = dvr_playback_add_segment(ctx->playback.player, &p_seg->playback_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800883 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800884 DVR_WRAPPER_INFO("fail to add segment %lld (%d)\n", p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800885 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +0800886 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
887 ctx->playback.status.info_full.size += p_seg->seg_info.size;
888 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800889 }
890
891 return error;
892}
893
894static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
895{
Wentao MA270dc0f2022-08-23 13:17:26 +0800896 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MA16f870e2022-09-09 11:00:22 +0800897 int error = DVR_SUCCESS;
hualing chenab0d1262021-09-26 15:22:50 +0800898 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800899
Wentao MA270dc0f2022-08-23 13:17:26 +0800900 p_seg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
901 if (!p_seg) {
Wentao MA16f870e2022-09-09 11:00:22 +0800902 DVR_WRAPPER_ERROR("memory allocation failed");
903 return DVR_FAILURE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800904 }
Wentao MA270dc0f2022-08-23 13:17:26 +0800905 p_seg->info = *seg_info;
wentao.maa22bc852022-10-13 12:18:06 +0800906 list_add(p_seg, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800907
hualing chenb9b358a2021-08-17 15:06:36 +0800908 if (ctx->record.param_open.is_timeshift ||
909 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
910
911 DVR_WrapperCtx_t *ctx_playback;
912 if (ctx->record.param_open.is_timeshift)
913 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
914 else
915 ctx_playback = ctx_getPlayback(sn);
916
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800917 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) add seg\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800918
919 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +0800920 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800921 if (ctx_valid(ctx_playback)) {
922 DVR_PlaybackSegmentFlag_t flags;
923
924 /*only if playback has started, the previous segments have been loaded*/
925 if (!list_empty(&ctx_playback->segments)) {
926 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800927 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800928 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
wentao.maa210e5e2022-10-12 16:10:03 +0800929 error = wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
930 if (error == DVR_FAILURE) {
931 DVR_WRAPPER_WARN("adding playback segment fails");
932 }
hualing chen451c8f72022-03-09 13:05:52 +0800933 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800934 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) list empty\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800935 }
hualing chenb9b358a2021-08-17 15:06:36 +0800936 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800937 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid\n", ctx->sn, sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800938 }
Gong Kefdb31922022-06-17 17:11:16 +0800939 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800940 }
hualing chen451c8f72022-03-09 13:05:52 +0800941 else
942 {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800943 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: playback(sn:%ld) not valid 2\n", ctx->sn, sn);
944 }
945
946 /*if it is not a timeshift recording, but a playing recording,
947 do not forget to obey the recording rule: link the segment!*/
948 if (!ctx->record.param_open.is_timeshift) {
949 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
950 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
hualing chen451c8f72022-03-09 13:05:52 +0800951 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800952 } else {
Zhiqiang Hanfd72b592022-07-04 18:36:52 +0800953 DVR_WRAPPER_INFO("rec(sn:%ld) add_seg: update link\n", ctx->sn);
Wentao MAe8ba5172022-08-09 11:18:17 +0800954 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, SEGMENT_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800955 }
956
957 return error;
958}
959
960static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
961{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800962 int error = -1;
Wentao MA270dc0f2022-08-23 13:17:26 +0800963 DVR_WrapperPlaybackSegmentInfo_t *p_seg = NULL, *p_seg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800964 uint32_t off_set = 0;
Wentao MA96f68962022-06-15 19:45:35 +0800965 DVR_WRAPPER_INFO("timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800966
Wentao MA270dc0f2022-08-23 13:17:26 +0800967 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
968 if (p_seg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800969
970 if (ctx->current_segment_id == seg_info->id) {
971 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
972
973 /*drive the player out of this will-be-deleted segment*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800974 next_seg = list_prev_entry(p_seg, head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800975
hualing chenb9a1a2c2021-12-31 11:27:59 +0800976 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
977 off_set = 10 * 1000;
978 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Wentao MA96f68962022-06-15 19:45:35 +0800979 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 +0800980 }
981
982 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
983 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +0800984 /*remove playback segment fail*/
Wentao MA96f68962022-06-15 19:45:35 +0800985 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 +0800986 }
987
Wentao MA270dc0f2022-08-23 13:17:26 +0800988 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800989
990 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +0800991 ctx->playback.obsolete.time += p_seg->seg_info.duration;
992 ctx->playback.obsolete.size += p_seg->seg_info.size;
993 ctx->playback.obsolete.pkts += p_seg->seg_info.nb_packets;
Wentao MA96f68962022-06-15 19:45:35 +0800994 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 +0800995 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Wentao MA270dc0f2022-08-23 13:17:26 +0800996 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800997 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800998 }
999 }
1000
Wentao MA96f68962022-06-15 19:45:35 +08001001 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 +08001002
1003 return error;
1004}
1005
1006static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
1007{
1008 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08001009 DVR_WrapperRecordSegmentInfo_t *p_seg, *p_seg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001010
Wentao MAf4072032022-06-30 13:50:45 +08001011 DVR_WRAPPER_INFO("calling %s on record(sn:%ld) segment(%lld) ...",
1012 __func__, ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001013
1014 /*if timeshifting, notify the playback first, then deal with record*/
1015 if (ctx->record.param_open.is_timeshift) {
1016 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
1017
1018 if (ctx_playback) {
Gong Kefdb31922022-06-17 17:11:16 +08001019 wrapper_mutex_lock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001020 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
1021 ctx_playback->playback.tf_full = DVR_TRUE;
Wentao MAf4072032022-06-30 13:50:45 +08001022 DVR_WRAPPER_INFO("%s, cannot remove record(sn:%ld) segment(%lld) for it is being"
1023 " played on segment(%lld) at speed %f.", __func__, ctx->sn, seg_info->info.id,
1024 ctx_playback->current_segment_id, ctx_playback->playback.speed);
Gong Kefdb31922022-06-17 17:11:16 +08001025 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
hualing chen56c0a162022-01-27 17:01:50 +08001026 return DVR_SUCCESS;
1027 } else {
Wentao MAf4072032022-06-30 13:50:45 +08001028 DVR_WRAPPER_INFO("%s, removing record(sn:%ld) segment(%lld) which is being played "
1029 "on segment (%lld) at speed (%f).", __func__, ctx->sn, seg_info->info.id,
1030 ctx_playback->current_segment_id,ctx_playback->playback.speed);
hualing chen56c0a162022-01-27 17:01:50 +08001031 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001032 if (ctx_valid(ctx_playback)
1033 && ctx_playback->sn == sn_timeshift_playback
1034 && !list_empty(&ctx_playback->segments)) {
1035 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
Wentao MA07d3d742022-09-06 09:58:05 +08001036 if (error != DVR_SUCCESS) {
1037 DVR_WRAPPER_ERROR("wrapper_removePlaybackSegment failed with return value %d",error);
1038 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001039 }
hualing chen56c0a162022-01-27 17:01:50 +08001040 ctx_playback->playback.tf_full = DVR_FALSE;
Gong Kefdb31922022-06-17 17:11:16 +08001041 wrapper_mutex_unlock(&ctx_playback->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001042 }
1043 }
1044
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001045 uint64_t id = seg_info->info.id;
1046
Wentao MA270dc0f2022-08-23 13:17:26 +08001047 list_for_each_entry_safe_reverse(p_seg, p_seg_tmp, &ctx->segments, head) {
1048 if (p_seg->info.id == id) {
1049 list_del(&p_seg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001050
1051 /*record the obsolete*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001052 ctx->record.obsolete.time += p_seg->info.duration;
1053 ctx->record.obsolete.size += p_seg->info.size;
1054 ctx->record.obsolete.pkts += p_seg->info.nb_packets;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001055
Wentao MA270dc0f2022-08-23 13:17:26 +08001056 free(p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001057 break;
1058 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001059 }
1060
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +08001061 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001062
Wentao MAf4072032022-06-30 13:50:45 +08001063 DVR_WRAPPER_INFO("%s, removed record(sn:%ld) segment(%lld), ret=(%d)\n",
1064 __func__, ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001065
1066 return error;
1067}
1068
1069int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
1070{
1071 int error;
1072 DVR_WrapperCtx_t *ctx;
1073 DVR_RecordOpenParams_t open_param;
1074
1075 DVR_RETURN_IF_FALSE(rec);
1076 DVR_RETURN_IF_FALSE(params);
1077
1078 /*get a free ctx*/
1079 ctx = ctx_getRecord(0);
1080 DVR_RETURN_IF_FALSE(ctx);
1081
Gong Kefdb31922022-06-17 17:11:16 +08001082 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001083
Wentao MA9a164002022-08-29 11:20:24 +08001084 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 +08001085 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001086
1087 ctx_reset(ctx);
1088
1089 ctx->record.param_open = *params;
1090 ctx->record.event_fn = params->event_fn;
1091 ctx->record.event_userdata = params->event_userdata;
Wentao MA2394fa82022-06-10 14:46:47 +08001092
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001093 INIT_LIST_HEAD(&ctx->segments);
1094 ctx->sn = get_sn();
1095
1096 wrapper_requestThreadFor(ctx);
1097
hualing chen266b9502020-04-04 17:39:39 +08001098 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001099 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001100 open_param.dmx_dev_id = params->dmx_dev_id;
1101 open_param.data_from_memory = 0;
1102 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001103 if (params->flush_size) {
1104 open_param.notification_size = params->flush_size;
1105 } else {
1106 open_param.notification_size = 64*1024;
1107 }
hualing chen002e5b92022-02-23 17:51:21 +08001108 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001109 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001110 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001111 open_param.event_fn = wrapper_record_event_handler;
1112 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001113 if (params->keylen) {
1114 open_param.clearkey = params->clearkey;
1115 open_param.cleariv = params->cleariv;
1116 open_param.keylen = params->keylen;
1117 }
wentao.ma35a69d42022-03-10 18:08:40 +08001118 open_param.force_sysclock = params->force_sysclock;
Wentao MAeeffdb02022-06-27 16:34:35 +08001119 open_param.guarded_segment_size = params->segment_size/2*3;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001120
1121 error = dvr_record_open(&ctx->record.recorder, &open_param);
1122 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001123 DVR_WRAPPER_INFO("record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001124 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001125 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001126 wrapper_releaseThreadForType(ctx->type);
1127 return DVR_FAILURE;
1128 }
1129 if (params->is_timeshift)
1130 sn_timeshift_record = ctx->sn;
1131
Wentao MA96f68962022-06-15 19:45:35 +08001132 DVR_WRAPPER_INFO("record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001133
Yahui Han1fbf3292021-11-08 18:17:19 +08001134 if (params->crypto_fn) {
1135 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1136 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001137 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 +08001138 }
hualing chen266b9502020-04-04 17:39:39 +08001139 }
1140
Gong Kefdb31922022-06-17 17:11:16 +08001141 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001142
1143 *rec = (DVR_WrapperRecord_t)ctx->sn;
1144 return DVR_SUCCESS;
1145}
1146
1147int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1148{
1149 DVR_WrapperCtx_t *ctx;
1150 DVR_RecordSegmentInfo_t seg_info;
1151 int error;
1152
1153 DVR_RETURN_IF_FALSE(rec);
1154
1155 ctx = ctx_getRecord((unsigned long)rec);
1156 DVR_RETURN_IF_FALSE(ctx);
1157
Gong Kefdb31922022-06-17 17:11:16 +08001158 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001159 DVR_WRAPPER_INFO("close record(sn:%ld)\n", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001160 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001161
1162 memset(&seg_info, 0, sizeof(seg_info));
wentao.maa210e5e2022-10-12 16:10:03 +08001163 dvr_record_stop_segment(ctx->record.recorder, &seg_info);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001164
1165 error = dvr_record_close(ctx->record.recorder);
1166
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001167 if (ctx->record.param_open.is_timeshift)
1168 sn_timeshift_record = 0;
1169
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001170 ctx_freeSegments(ctx);
1171
Wentao MA96f68962022-06-15 19:45:35 +08001172 DVR_WRAPPER_INFO("record(sn:%ld) closed = (%d).\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001173 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001174 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001175
1176 wrapper_releaseThreadForType(ctx->type);
1177
1178 return error;
1179}
1180
1181int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1182{
1183 DVR_WrapperCtx_t *ctx;
1184 DVR_RecordStartParams_t *start_param;
1185 int i;
1186 int error;
1187
1188 DVR_RETURN_IF_FALSE(rec);
1189 DVR_RETURN_IF_FALSE(params);
1190
1191 ctx = ctx_getRecord((unsigned long)rec);
1192 DVR_RETURN_IF_FALSE(ctx);
1193
Gong Kefdb31922022-06-17 17:11:16 +08001194 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001195 DVR_WRAPPER_INFO("libdvr_api, start_record (sn:%ld) location:%s, save:%d",
1196 ctx->sn, ctx->record.param_open.location, params->save_rec_file);
Gong Kefdb31922022-06-17 17:11:16 +08001197 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001198
1199 start_param = &ctx->record.param_start;
1200 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001201 const int len = strlen(ctx->record.param_open.location);
1202 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1203 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1204 pthread_mutex_unlock(&ctx->wrapper_lock);
1205 return DVR_FAILURE;
1206 }
1207 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001208 start_param->segment.nb_pids = params->pids_info.nb_pids;
1209 for (i = 0; i < params->pids_info.nb_pids; i++) {
1210 start_param->segment.pids[i] = params->pids_info.pids[i];
1211 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1212 }
Zhiqiang Han89c77972022-12-08 12:16:39 +08001213
hualing chena5f03222021-12-02 11:22:35 +08001214 if (params->save_rec_file == 0)//default is not save
1215 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han89c77972022-12-08 12:16:39 +08001216
1217 //wait for the file status to stabilize before set the new segment id
1218 uint64_t new_segment_id = 0;
Zhiqiang Han02e890c2023-04-25 14:55:02 +08001219
1220 if (params->save_rec_file != 0) {
Zhiqiang Han89c77972022-12-08 12:16:39 +08001221 uint32_t segment_nb = 0;
1222 uint64_t *p_segment_ids = NULL;
1223 error = dvr_segment_get_list(ctx->record.param_open.location, &segment_nb, &p_segment_ids);
1224 if (error == DVR_SUCCESS && segment_nb>0) {
1225 // Tainted data issue originating from fgets seem false positive, so we
1226 // just suppress it here.
1227 // coverity[tainted_data]
1228 new_segment_id = p_segment_ids[segment_nb-1]+1;
1229 }
1230 if (p_segment_ids != NULL) {
1231 free(p_segment_ids);
1232 }
Zhiqiang Han89c77972022-12-08 12:16:39 +08001233 }
Zhiqiang Han02e890c2023-04-25 14:55:02 +08001234 DVR_WRAPPER_DEBUG("new_segment_id:%lld\n", new_segment_id);
1235
Zhiqiang Han89c77972022-12-08 12:16:39 +08001236 ctx->record.next_segment_id = new_segment_id;
1237 ctx->current_segment_id = new_segment_id;
1238
1239 start_param->segment.segment_id = ctx->record.next_segment_id++;
1240
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001241 {
1242 /*sync to update for further use*/
1243 DVR_RecordStartParams_t *update_param;
1244 update_param = &ctx->record.param_update;
1245 memcpy(update_param, start_param, sizeof(*update_param));
1246 for (i = 0; i < update_param->segment.nb_pids; i++)
1247 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1248 }
1249
1250 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1251 {
1252 DVR_RecordSegmentInfo_t new_seg_info =
1253 { .id = start_param->segment.segment_id, };
1254 wrapper_addRecordSegment(ctx, &new_seg_info);
1255 }
1256
Wentao MA96f68962022-06-15 19:45:35 +08001257 DVR_WRAPPER_INFO("record(sn:%ld) started = (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001258
Gong Kefdb31922022-06-17 17:11:16 +08001259 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001260
1261 return error;
1262}
1263
1264int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1265{
1266 DVR_WrapperCtx_t *ctx;
1267 DVR_RecordSegmentInfo_t seg_info;
1268 int error;
1269
1270 DVR_RETURN_IF_FALSE(rec);
1271
1272 ctx = ctx_getRecord((unsigned long)rec);
1273 DVR_RETURN_IF_FALSE(ctx);
1274
Gong Kefdb31922022-06-17 17:11:16 +08001275 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001276 DVR_WRAPPER_INFO("libdvr_api, stop_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001277 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001278
1279 memset(&seg_info, 0, sizeof(seg_info));
1280 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1281 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1282
Wentao MAd71e2062023-02-15 10:10:49 +08001283 ctx_freeSegments(ctx);
1284
Wentao MA96f68962022-06-15 19:45:35 +08001285 DVR_WRAPPER_INFO("record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001286 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001287
1288 return error;
1289}
1290
hualing chen03fd4942021-07-15 15:56:41 +08001291int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1292{
1293 DVR_WrapperCtx_t *ctx;
1294 int error;
1295
1296 DVR_RETURN_IF_FALSE(rec);
1297
1298 ctx = ctx_getRecord((unsigned long)rec);
1299 DVR_RETURN_IF_FALSE(ctx);
1300
Gong Kefdb31922022-06-17 17:11:16 +08001301 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001302 DVR_WRAPPER_INFO("libdvr_api, pause_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001303 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001304
1305 error = dvr_record_pause(ctx->record.recorder);
1306
Wentao MA96f68962022-06-15 19:45:35 +08001307 DVR_WRAPPER_INFO("record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001308 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001309
1310 return error;
1311}
1312
1313int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1314{
1315 DVR_WrapperCtx_t *ctx;
1316 int error;
1317
1318 DVR_RETURN_IF_FALSE(rec);
1319
1320 ctx = ctx_getRecord((unsigned long)rec);
1321 DVR_RETURN_IF_FALSE(ctx);
1322
Gong Kefdb31922022-06-17 17:11:16 +08001323 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001324 DVR_WRAPPER_INFO("libdvr_api, resume_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001325 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001326
1327 error = dvr_record_resume(ctx->record.recorder);
1328
Wentao MA96f68962022-06-15 19:45:35 +08001329 DVR_WRAPPER_INFO("record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001330 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001331
1332 return error;
1333}
1334
Wentao MAcdea4762022-04-26 13:28:56 +08001335/* Return true if arr1 contains all elements in arr2 */
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001336static DVR_Bool_t pids_test_include(
1337 DVR_StreamPid_t* arr1, DVR_RecordPidAction_t *act1, int size1,
1338 DVR_StreamPid_t* arr2, DVR_RecordPidAction_t *act2, int size2)
wentao.maa69578c2022-04-07 09:27:39 +08001339{
Wentao MAcdea4762022-04-26 13:28:56 +08001340 DVR_Bool_t ret = DVR_TRUE;
1341 for (int i=0;i<size2;i++)
1342 { // iterate all elements in arr2 to check if they exist in arr1
1343 DVR_Bool_t found=DVR_FALSE;
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001344
1345 if (act2[i] == DVR_RECORD_PID_CLOSE)
1346 continue;
1347
Wentao MAcdea4762022-04-26 13:28:56 +08001348 for (int j=0;j<size1;j++)
wentao.maa69578c2022-04-07 09:27:39 +08001349 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001350 if (act1[j] != DVR_RECORD_PID_CLOSE
1351 && arr2[i].pid == arr1[j].pid)
wentao.maa69578c2022-04-07 09:27:39 +08001352 {
1353 found=DVR_TRUE;
1354 break;
1355 }
1356 }
1357 if (found == DVR_FALSE)
1358 {
Wentao MAcdea4762022-04-26 13:28:56 +08001359 ret=DVR_FALSE;
wentao.maa69578c2022-04-07 09:27:39 +08001360 break;
1361 }
1362 }
Wentao MAcdea4762022-04-26 13:28:56 +08001363 return ret;
1364}
1365
1366static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1367 const DVR_WrapperUpdatePidsParams_t* p2)
1368{
1369 int i=0;
1370 char buf[128]={0};
1371 int cnt=0;
1372 int chars=0;
1373
1374 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1375 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1376
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001377 DVR_Bool_t cond1 = pids_test_include(p1->pids,p1->pid_action,p1->nb_pids,
1378 p2->pids,p2->pid_action,p2->nb_pids);
1379 DVR_Bool_t cond2 = pids_test_include(p2->pids,p2->pid_action,p2->nb_pids,
1380 p1->pids,p1->pid_action,p1->nb_pids);
Wentao MAcdea4762022-04-26 13:28:56 +08001381 DVR_Bool_t is_equal = (cond1 && cond2);
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001382 int removed;
Wentao MAcdea4762022-04-26 13:28:56 +08001383
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001384 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001385 for (i=0;i<p1->nb_pids;i++)
1386 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001387 if (p1->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1388 removed++;
1389 continue;
1390 }
Wentao MAcdea4762022-04-26 13:28:56 +08001391 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1392 if (chars<0)
1393 {
1394 break;
1395 }
1396 cnt += chars;
1397 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001398 DVR_INFO("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids-removed,buf);
Wentao MAcdea4762022-04-26 13:28:56 +08001399 memset(buf,0,sizeof(buf));
1400
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001401 removed = 0;
Wentao MAcdea4762022-04-26 13:28:56 +08001402 for (i=0,cnt=0;i<p2->nb_pids;i++)
1403 {
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001404 if (p2->pid_action[i] == DVR_RECORD_PID_CLOSE) {
1405 removed++;
1406 continue;
1407 }
Wentao MAcdea4762022-04-26 13:28:56 +08001408 chars = snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1409 if (chars<0)
1410 {
1411 break;
1412 }
1413 cnt += chars;
1414 }
Zhiqiang Han42d91aa2022-08-10 16:38:12 +08001415 DVR_INFO("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids-removed,buf);
Wentao MA96f68962022-06-15 19:45:35 +08001416 DVR_INFO("%s is_equal:%d",__func__,is_equal);
wentao.maa69578c2022-04-07 09:27:39 +08001417 return is_equal;
1418}
1419
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001420int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1421{
1422 DVR_WrapperCtx_t *ctx;
1423 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001424 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001425 int i;
1426 int error;
1427
1428 DVR_RETURN_IF_FALSE(rec);
1429 DVR_RETURN_IF_FALSE(params);
1430
1431 ctx = ctx_getRecord((unsigned long)rec);
1432 DVR_RETURN_IF_FALSE(ctx);
1433
Gong Kefdb31922022-06-17 17:11:16 +08001434 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001435 DVR_WRAPPER_INFO("libdvr_api, update_record (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001436 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001437
1438 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001439 if (pids_equal(&(start_param->segment),params))
1440 {
Gong Kefdb31922022-06-17 17:11:16 +08001441 wrapper_mutex_unlock(&ctx->wrapper_lock);
wentao.maa69578c2022-04-07 09:27:39 +08001442 return DVR_TRUE;
1443 }
1444
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001445 memset(start_param, 0, sizeof(*start_param));
Wentao MAe88ad702022-09-02 10:35:00 +08001446 const int len = strlen(ctx->record.param_open.location);
1447 if (len >= DVR_MAX_LOCATION_SIZE || len <= 0) {
1448 DVR_WRAPPER_ERROR("Invalid record.param_open.location length %d",len);
1449 pthread_mutex_unlock(&ctx->wrapper_lock);
1450 return DVR_FAILURE;
1451 }
1452 strncpy(start_param->location, ctx->record.param_open.location, len+1);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001453 start_param->segment.segment_id = ctx->record.next_segment_id++;
1454 start_param->segment.nb_pids = params->nb_pids;
1455 for (i = 0; i < params->nb_pids; i++) {
1456 start_param->segment.pids[i] = params->pids[i];
1457 start_param->segment.pid_action[i] = params->pid_action[i];
1458 }
1459 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1460 {
1461 DVR_RecordSegmentInfo_t new_seg_info =
1462 { .id = start_param->segment.segment_id, };
1463 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1464 wrapper_addRecordSegment(ctx, &new_seg_info);
1465 }
1466
Wentao MA96f68962022-06-15 19:45:35 +08001467 DVR_WRAPPER_INFO("record(sn:%ld) updated = (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08001468 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001469
1470 return error;
1471}
1472
1473int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1474{
1475 DVR_WrapperCtx_t *ctx;
1476 DVR_WrapperRecordStatus_t s;
1477 int error;
1478
1479 DVR_RETURN_IF_FALSE(rec);
1480 DVR_RETURN_IF_FALSE(status);
1481
1482 ctx = ctx_getRecord((unsigned long)rec);
1483 DVR_RETURN_IF_FALSE(ctx);
1484
Gong Kefdb31922022-06-17 17:11:16 +08001485 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001486
Wentao MA804bab12022-11-29 10:01:26 +08001487 DVR_WRAPPER_INFO("libdvr_api, get_record_status (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001488 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001489
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001490 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001491
Wentao MA96f68962022-06-15 19:45:35 +08001492 DVR_WRAPPER_INFO("record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001493 ctx->sn,
1494 s.state,
1495 s.info.time,
1496 s.info.size,
1497 s.info.pkts,
1498 error);
1499
1500 *status = s;
1501
Gong Kefdb31922022-06-17 17:11:16 +08001502 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001503
1504 return error;
1505}
1506
hualing chen4fe3bee2020-10-23 13:58:52 +08001507int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1508{
1509 DVR_WrapperCtx_t *ctx;
1510 int error;
1511
1512 DVR_RETURN_IF_FALSE(rec);
1513
1514 ctx = ctx_getRecord((unsigned long)rec);
1515 DVR_RETURN_IF_FALSE(ctx);
1516
Gong Kefdb31922022-06-17 17:11:16 +08001517 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001518 error = dvr_record_is_secure_mode(ctx->record.recorder);
Gong Kefdb31922022-06-17 17:11:16 +08001519 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen4fe3bee2020-10-23 13:58:52 +08001520 return error;
1521}
1522
hualing chen266b9502020-04-04 17:39:39 +08001523int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1524{
1525 DVR_WrapperCtx_t *ctx;
1526 int error;
1527
1528 DVR_RETURN_IF_FALSE(rec);
1529 DVR_RETURN_IF_FALSE(p_secure_buf);
1530
1531 ctx = ctx_getRecord((unsigned long)rec);
1532 DVR_RETURN_IF_FALSE(ctx);
1533
Gong Kefdb31922022-06-17 17:11:16 +08001534 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001535 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08001536 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001537 return error;
1538}
1539
1540int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1541{
1542 DVR_WrapperCtx_t *ctx;
1543 int error;
1544
1545 DVR_RETURN_IF_FALSE(rec);
1546 DVR_RETURN_IF_FALSE(func);
1547
1548 ctx = ctx_getRecord((unsigned long)rec);
1549 DVR_RETURN_IF_FALSE(ctx);
1550
Gong Kefdb31922022-06-17 17:11:16 +08001551 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001552 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08001553 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08001554 return error;
1555}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001556
1557
1558int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1559{
1560 DVR_WrapperCtx_t *ctx;
1561 DVR_PlaybackOpenParams_t open_param;
1562 int error;
1563
1564 DVR_RETURN_IF_FALSE(playback);
1565 DVR_RETURN_IF_FALSE(params);
1566 DVR_RETURN_IF_FALSE(params->playback_handle);
1567
1568 /*get a free ctx*/
1569 ctx = ctx_getPlayback(0);
1570 DVR_RETURN_IF_FALSE(ctx);
1571
Gong Kefdb31922022-06-17 17:11:16 +08001572 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001573
Wentao MA804bab12022-11-29 10:01:26 +08001574 DVR_WRAPPER_INFO("libdvr_api, open_playback (dmx:%d) ..vendor[%d]params->block_size[%d].",
1575 params->dmx_dev_id, params->vendor, params->block_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001576
1577 ctx_reset(ctx);
1578
1579 ctx->playback.param_open = *params;
1580 ctx->playback.event_fn = params->event_fn;
1581 ctx->playback.event_userdata = params->event_userdata;
1582 ctx->current_segment_id = 0;
1583 INIT_LIST_HEAD(&ctx->segments);
1584 ctx->sn = get_sn();
1585
1586 wrapper_requestThreadFor(ctx);
1587
hualing chen266b9502020-04-04 17:39:39 +08001588 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001589 open_param.dmx_dev_id = params->dmx_dev_id;
1590 open_param.block_size = params->block_size;
1591 open_param.is_timeshift = params->is_timeshift;
1592 //open_param.notification_size = 10*1024; //not supported
1593 open_param.event_fn = wrapper_playback_event_handler;
1594 open_param.event_userdata = (void*)ctx->sn;
1595 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001596 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001597 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001598 open_param.vendor = params->vendor;
1599
Yahui Han1fbf3292021-11-08 18:17:19 +08001600 if (params->keylen) {
1601 open_param.clearkey = params->clearkey;
1602 open_param.cleariv = params->cleariv;
1603 open_param.keylen = params->keylen;
1604 }
shenghui.gengbec6a462023-01-12 15:21:02 +08001605 open_param.control_speed_enable = params->control_speed_enable;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001606
1607 error = dvr_playback_open(&ctx->playback.player, &open_param);
1608 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08001609 DVR_WRAPPER_INFO("playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001610 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001611 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001612 wrapper_releaseThreadForType(ctx->type);
1613 return DVR_FAILURE;
1614 }
1615 if (params->is_timeshift)
1616 sn_timeshift_playback = ctx->sn;
1617
Wentao MA270dc0f2022-08-23 13:17:26 +08001618 DVR_WRAPPER_INFO("playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
hualing chen266b9502020-04-04 17:39:39 +08001619 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1620 if (error) {
Wentao MA270dc0f2022-08-23 13:17:26 +08001621 DVR_WRAPPER_INFO("playback set decrypt callback fail(error:%d).\n", error);
hualing chen266b9502020-04-04 17:39:39 +08001622 }
Gong Kefdb31922022-06-17 17:11:16 +08001623 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001624
1625 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1626 return DVR_SUCCESS;
1627}
1628
1629int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1630{
1631 DVR_WrapperCtx_t *ctx;
1632 int error;
1633
1634 DVR_RETURN_IF_FALSE(playback);
1635
1636 ctx = ctx_getPlayback((unsigned long)playback);
1637 DVR_RETURN_IF_FALSE(ctx);
1638
Gong Kefdb31922022-06-17 17:11:16 +08001639 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08001640 DVR_WRAPPER_INFO("libdvr_api, close_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08001641 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001642
1643 if (ctx->playback.param_open.is_timeshift)
1644 sn_timeshift_playback = 0;
1645
1646 /*try stop first*/
wentao.maa210e5e2022-10-12 16:10:03 +08001647 dvr_playback_stop(ctx->playback.player, DVR_TRUE);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001648
1649 {
1650 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08001651 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001652
wentao.mafd5283f2022-10-14 09:51:13 +08001653 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001654 // prefetch() here incurring self_assign is used to avoid some compiling
1655 // warnings.
1656 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08001657 list_for_each_entry(p_seg, &ctx->segments, head) {
1658 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08001659 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08001660 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001661 }
1662 ctx_freeSegments(ctx);
1663 }
1664
1665 error = dvr_playback_close(ctx->playback.player);
1666
Wentao MA96f68962022-06-15 19:45:35 +08001667 DVR_WRAPPER_INFO("playback(sn:%ld) closed.\n", ctx->sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001668 ctx_reset(ctx);
Gong Kefdb31922022-06-17 17:11:16 +08001669 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001670
1671 wrapper_releaseThreadForType(ctx->type);
1672
1673 return error;
1674}
1675
1676int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1677{
1678 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001679 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001680 uint64_t *p_segment_ids;
1681 uint32_t segment_nb;
1682 uint32_t i;
1683 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001684 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001685 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001686 DVR_Bool_t is_timeshift = DVR_FALSE;
Wentao MAcefc13c2022-10-26 15:47:24 +08001687 DVR_PlaybackSegmentFlag_t seg_flags = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001688
1689 DVR_RETURN_IF_FALSE(playback);
1690 DVR_RETURN_IF_FALSE(p_pids);
1691
hualing chenc110f952021-01-18 11:25:37 +08001692 ctx_record = NULL;
1693
1694 /*lock the recorder to avoid changing the recording segments*/
1695 ctx_record = ctx_getRecord(sn_timeshift_record);
1696
1697 if (ctx_record) {
Gong Kefdb31922022-06-17 17:11:16 +08001698 wrapper_mutex_lock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001699 if (!ctx_valid(ctx_record)
1700 || ctx_record->sn != sn_timeshift_record) {
Wentao MA96f68962022-06-15 19:45:35 +08001701 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error found\n");
Gong Kefdb31922022-06-17 17:11:16 +08001702 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001703 is_timeshift = DVR_FALSE;
1704 } else {
1705 is_timeshift = DVR_TRUE;
1706 }
1707 }
1708
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001709 ctx = ctx_getPlayback((unsigned long)playback);
1710 DVR_RETURN_IF_FALSE(ctx);
1711
Gong Kefdb31922022-06-17 17:11:16 +08001712 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001713
Wentao MA804bab12022-11-29 10:01:26 +08001714 DVR_WRAPPER_INFO("libdvr_api, start_playback (sn:%ld) location:%s"
1715 " flags:0x%x v/a/ad/sub/pcr(%d:%d %d:%d %d:%d %d:%d %d)",
1716 ctx->sn,
1717 ctx->playback.param_open.location,
1718 flags,
1719 p_pids->video.pid, p_pids->video.format,
1720 p_pids->audio.pid, p_pids->audio.format,
1721 p_pids->ad.pid, p_pids->ad.format,
1722 p_pids->subtitle.pid, p_pids->subtitle.format,
1723 p_pids->pcr.pid);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001724
Gong Kefdb31922022-06-17 17:11:16 +08001725 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001726
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001727 if (ctx->playback.param_open.is_timeshift) {
1728 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001729 if (is_timeshift == DVR_FALSE) {
Wentao MA96f68962022-06-15 19:45:35 +08001730 DVR_WRAPPER_INFO("timeshift, record is not for timeshifting, FATAL error return\n");
Gong Kefdb31922022-06-17 17:11:16 +08001731 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chenc110f952021-01-18 11:25:37 +08001732 return DVR_FAILURE;
1733 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001734 DVR_WRAPPER_INFO("playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
hualing chenc110f952021-01-18 11:25:37 +08001735 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001736 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001737 }
1738
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001739 /*obtain all segments in a list*/
1740 segment_nb = 0;
1741 p_segment_ids = NULL;
1742 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001743 if (!error) {
1744 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001745 struct list_head info_list; /**< segment list head*/
1746 INIT_LIST_HEAD(&info_list);
1747
Wentao MA96f68962022-06-15 19:45:35 +08001748 DVR_WRAPPER_INFO("get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001749 //we need free info list buf when we used end.
1750 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001751 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001752 error = DVR_FAILURE;
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08001753 DVR_WRAPPER_INFO("fail to get all seg info (location:%s), (error:%d)\n",
1754 ctx->playback.param_open.location, error);
wentao.maf57dd232022-10-08 16:07:29 +08001755 // Tainted data issue originating from fgets seem false positive, so we
1756 // just suppress it here.
1757 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001758 for (i = 0; i < segment_nb; i++) {
1759 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001760 memset((void*)&seg_info,0,sizeof(seg_info));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001761
hualing chenb9a02922021-12-14 11:29:47 +08001762 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1763 if (error) {
1764 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001765 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chenb9a02922021-12-14 11:29:47 +08001766 ctx->playback.param_open.location, p_segment_ids[i], error);
1767 break;
1768 }
1769 //add check if has audio or video pid. if not exist. not add segment to playback
1770 int ii = 0;
1771 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001772 // Tainted data issue originating from fgets seem false positive, so we
1773 // just suppress it here.
1774 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001775 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1776 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1777 if (type == DVR_STREAM_TYPE_VIDEO ||
1778 type == DVR_STREAM_TYPE_AUDIO ||
1779 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001780 DVR_WRAPPER_INFO("success to get seg av info \n");
1781 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 +08001782 DVR_STREAM_TYPE_VIDEO,
1783 DVR_STREAM_TYPE_AUDIO,
1784 DVR_STREAM_TYPE_AD);
1785 has_av = 1;
1786 //break;
1787 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001788 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 +08001789 DVR_STREAM_TYPE_VIDEO,
1790 DVR_STREAM_TYPE_AUDIO,
1791 DVR_STREAM_TYPE_AD);
1792 }
1793 }
1794 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001795 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001796 continue;
1797 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001798 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001799 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001800 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1801 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001802 if (error == DVR_FAILURE) {
1803 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001804 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001805 }
hualing chenb9a02922021-12-14 11:29:47 +08001806 /*copy the 1st segment*/
1807 if (got_1st_seg == 0) {
1808 seg_info_1st = seg_info;
1809 got_1st_seg = 1;
1810 }
1811 }
1812 } else {
wentao.maf57dd232022-10-08 16:07:29 +08001813 // Tainted data issue originating from fgets seem false positive, so we
1814 // just suppress it here.
1815 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08001816 for (i = 0; i < segment_nb; i++) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001817 DVR_RecordSegmentInfo_t *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001818 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08001819 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08001820 // prefetch() here incurring self_assign is used to avoid some compiling
1821 // warnings.
1822 // coverity[self_assign]
Wentao MA4d85ff32022-09-23 11:36:18 +08001823 list_for_each_entry(p_seg_info, &info_list, head)
hualing chenb9a02922021-12-14 11:29:47 +08001824 {
Wentao MA4d85ff32022-09-23 11:36:18 +08001825 if (p_seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001826 found = 1;
Wentao MA96f68962022-06-15 19:45:35 +08001827 DVR_WRAPPER_INFO("get segment info::%d", i);
hualing chenb9a02922021-12-14 11:29:47 +08001828 break;
1829 }
1830 }
1831 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001832 //last info is not found if when recording occured power off.
1833 if (p_segment_ids[i] == segment_nb - 1) {
1834 DVR_RecordSegmentInfo_t seg_info;
Wentao MA4d85ff32022-09-23 11:36:18 +08001835 memset((void*)&seg_info,0,sizeof(seg_info));
hualing chen8aed9582021-12-24 17:59:56 +08001836
1837 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1838 if (error) {
1839 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08001840 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08001841 ctx->playback.param_open.location, p_segment_ids[i], error);
1842 break;
1843 }
1844 //
1845 //add check if has audio or video pid. if not exist. not add segment to playback
1846 int ii = 0;
1847 int has_av = 0;
wentao.maf57dd232022-10-08 16:07:29 +08001848 // Tainted data issue originating from fgets seem false positive, so we
1849 // just suppress it here.
1850 // coverity[tainted_data]
hualing chen8aed9582021-12-24 17:59:56 +08001851 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1852 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1853 if (type == DVR_STREAM_TYPE_VIDEO ||
1854 type == DVR_STREAM_TYPE_AUDIO ||
1855 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001856 DVR_WRAPPER_INFO("success to get seg av info \n");
1857 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 +08001858 DVR_STREAM_TYPE_VIDEO,
1859 DVR_STREAM_TYPE_AUDIO,
1860 DVR_STREAM_TYPE_AD);
1861 has_av = 1;
1862 //break;
1863 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001864 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 +08001865 DVR_STREAM_TYPE_VIDEO,
1866 DVR_STREAM_TYPE_AUDIO,
1867 DVR_STREAM_TYPE_AD);
1868 }
1869 }
1870 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001871 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001872 continue;
1873 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001874 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chen8aed9582021-12-24 17:59:56 +08001875 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001876 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1877 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001878 if (error == DVR_FAILURE) {
1879 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chen8aed9582021-12-24 17:59:56 +08001880 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001881 }
hualing chen8aed9582021-12-24 17:59:56 +08001882 }
hualing chenb9a02922021-12-14 11:29:47 +08001883 continue;
1884 }
1885
1886 //add check if has audio or video pid. if not exist. not add segment to playback
1887 int ii = 0;
1888 int has_av = 0;
Wentao MA4d85ff32022-09-23 11:36:18 +08001889 for (ii = 0; ii < p_seg_info->nb_pids; ii++) {
1890 int type = (p_seg_info->pids[ii].type >> 24) & 0x0f;
hualing chenb9a02922021-12-14 11:29:47 +08001891 if (type == DVR_STREAM_TYPE_VIDEO ||
1892 type == DVR_STREAM_TYPE_AUDIO ||
1893 type == DVR_STREAM_TYPE_AD) {
Wentao MA96f68962022-06-15 19:45:35 +08001894 DVR_WRAPPER_INFO("success to get seg av info \n");
Wentao MA4d85ff32022-09-23 11:36:18 +08001895 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 +08001896 DVR_STREAM_TYPE_VIDEO,
1897 DVR_STREAM_TYPE_AUDIO,
1898 DVR_STREAM_TYPE_AD);
1899 has_av = 1;
1900 //break;
1901 } else {
Wentao MA4d85ff32022-09-23 11:36:18 +08001902 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 +08001903 DVR_STREAM_TYPE_VIDEO,
1904 DVR_STREAM_TYPE_AUDIO,
1905 DVR_STREAM_TYPE_AD);
1906 }
1907 }
1908 if (has_av == 0) {
Wentao MA96f68962022-06-15 19:45:35 +08001909 DVR_WRAPPER_INFO("fail to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001910 continue;
1911 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001912 DVR_WRAPPER_INFO("success to get seg av info \n");
hualing chenb9a02922021-12-14 11:29:47 +08001913 }
Wentao MAcefc13c2022-10-26 15:47:24 +08001914 seg_flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1915 error = wrapper_addPlaybackSegment(ctx, p_seg_info, p_pids, seg_flags);
wentao.maa210e5e2022-10-12 16:10:03 +08001916 if (error == DVR_FAILURE) {
1917 DVR_WRAPPER_WARN("adding playback segment fails");
hualing chenb9a02922021-12-14 11:29:47 +08001918 break;
wentao.maa210e5e2022-10-12 16:10:03 +08001919 }
hualing chenb9a02922021-12-14 11:29:47 +08001920
1921 /*copy the 1st segment*/
1922 if (got_1st_seg == 0) {
Wentao MA4d85ff32022-09-23 11:36:18 +08001923 seg_info_1st = *p_seg_info;
hualing chenb9a02922021-12-14 11:29:47 +08001924 got_1st_seg = 1;
1925 }
hualing chen92f3a142020-07-08 20:59:33 +08001926 }
hualing chenb9a02922021-12-14 11:29:47 +08001927 //free list
1928 DVR_RecordSegmentInfo_t *segment = NULL;
1929 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1930 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1931 {
1932 if (segment) {
1933 list_del(&segment->head);
1934 free(segment);
1935 }
1936 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001937 }
hualing chenb9a02922021-12-14 11:29:47 +08001938
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001939 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001940
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001941 /* return if no segment or fail to add */
1942 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001943
Wentao MA270dc0f2022-08-23 13:17:26 +08001944 /*copy the obsolete information, must for timeshifting*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001945 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1946 ctx->playback.obsolete = ctx_record->record.obsolete;
1947 }
1948
Wentao MA96f68962022-06-15 19:45:35 +08001949 DVR_WRAPPER_INFO("playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001950
1951 ctx->playback.reach_end = DVR_FALSE;
1952 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1953 ctx->playback.speed = 0.0f;
1954 else
1955 ctx->playback.speed = 100.0f;
1956
1957 ctx->playback.pids_req = *p_pids;
Wentao MA270dc0f2022-08-23 13:17:26 +08001958 //calculate segment id and pos
hualing chen03fd4942021-07-15 15:56:41 +08001959 if (dvr_playback_check_limit(ctx->playback.player)) {
Gong Kefdb31922022-06-17 17:11:16 +08001960 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001961 dvr_wrapper_seek_playback(playback, 0);
Gong Kefdb31922022-06-17 17:11:16 +08001962 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08001963 error = dvr_playback_start(ctx->playback.player, flags);
1964 } else {
wentao.maa210e5e2022-10-12 16:10:03 +08001965 dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
hualing chen03fd4942021-07-15 15:56:41 +08001966 error = dvr_playback_start(ctx->playback.player, flags);
Wentao MA96f68962022-06-15 19:45:35 +08001967 DVR_WRAPPER_INFO("playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08001968 ctx->sn, seg_info_1st.id, error);
1969 }
Wentao MA96f68962022-06-15 19:45:35 +08001970 DVR_WRAPPER_INFO("playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001971 } else {
Wentao MA96f68962022-06-15 19:45:35 +08001972 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 +08001973 }
1974 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001975
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001976 if (ctx->playback.param_open.is_timeshift) {
1977 /*unlock the recorder locked above*/
1978 if (ctx_record && ctx_valid(ctx_record)) {
Gong Kefdb31922022-06-17 17:11:16 +08001979 wrapper_mutex_unlock(&ctx_record->wrapper_lock);
Wentao MA96f68962022-06-15 19:45:35 +08001980 DVR_WRAPPER_INFO("playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001981 ctx->sn, ctx_record->sn);
1982 }
1983 }
Gong Kefdb31922022-06-17 17:11:16 +08001984 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001985
1986 return error;
1987}
hualing chen002e5b92022-02-23 17:51:21 +08001988//stop record and playback
1989int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1990{
1991 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1992 int error;
Wentao MA804bab12022-11-29 10:01:26 +08001993 DVR_WRAPPER_INFO("libdvr_api, stop_timeshift");
hualing chen002e5b92022-02-23 17:51:21 +08001994
1995 //stop timeshift record
1996 ctx_record = ctx_getRecord(sn_timeshift_record);
wentao.maa210e5e2022-10-12 16:10:03 +08001997 dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
hualing chen002e5b92022-02-23 17:51:21 +08001998
Wentao MA96f68962022-06-15 19:45:35 +08001999 DVR_WRAPPER_INFO("stop timeshift ...stop play\n");
hualing chen002e5b92022-02-23 17:51:21 +08002000 //stop play
2001 error = dvr_wrapper_stop_playback(playback);
2002 //del timeshift file
2003 if (ctx_record != NULL) {
Wentao MA96f68962022-06-15 19:45:35 +08002004 DVR_WRAPPER_INFO("del timeshift(sn:%ld) ...3\n", ctx_record->sn);
hualing chen002e5b92022-02-23 17:51:21 +08002005 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
2006 }
2007 return error;
2008}
2009//start record and start playback
2010int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
2011{
2012 DVR_WrapperCtx_t *ctx;
2013 DVR_RecordStartParams_t *start_param;
2014 int error;
2015
2016 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
2017 DVR_RETURN_IF_FALSE(ctx);
2018
Gong Kefdb31922022-06-17 17:11:16 +08002019 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002020 DVR_WRAPPER_INFO("libdvr_api, restart_timeshift (sn:%ld) location:%s",
2021 ctx->sn, ctx->record.param_open.location);
Gong Kefdb31922022-06-17 17:11:16 +08002022 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002023
hualing chen451c8f72022-03-09 13:05:52 +08002024 {
2025 //clear old record status
2026 // struct {
2027 // DVR_WrapperRecordOpenParams_t param_open;
2028 // DVR_RecordStartParams_t param_start;
2029 // DVR_RecordStartParams_t param_update;
2030 // DVR_RecordHandle_t recorder;
2031 // DVR_RecordEventFunction_t event_fn;
2032 // void *event_userdata;
2033
2034 // /*total status = seg_status + status + obsolete*/
2035 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
2036 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
2037 // uint64_t next_segment_id;
2038
2039 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
2040 // } record;
2041 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
2042 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
2043 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2044 }
2045
hualing chen002e5b92022-02-23 17:51:21 +08002046 start_param = &ctx->record.param_start;
2047
2048 error = dvr_record_start_segment(ctx->record.recorder, start_param);
2049 {
2050 DVR_RecordSegmentInfo_t new_seg_info =
2051 { .id = start_param->segment.segment_id, };
2052 wrapper_addRecordSegment(ctx, &new_seg_info);
Wentao MA96f68962022-06-15 19:45:35 +08002053 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 +08002054 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
2055 DVR_RecordStartParams_t *update_param;
2056 update_param = &ctx->record.param_update;
2057 memcpy(update_param, start_param, sizeof(*update_param));
2058 int i = 0;
2059 for (i = 0; i < update_param->segment.nb_pids; i++)
2060 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08002061 }
2062
Wentao MA96f68962022-06-15 19:45:35 +08002063 DVR_WRAPPER_INFO("re record(sn:%ld) started = (%d)\n", ctx->sn, error);
hualing chen002e5b92022-02-23 17:51:21 +08002064
Gong Kefdb31922022-06-17 17:11:16 +08002065 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen002e5b92022-02-23 17:51:21 +08002066
2067 //start play
Wentao MA96f68962022-06-15 19:45:35 +08002068 DVR_WRAPPER_INFO("re start play and clear old status\n");
hualing chen451c8f72022-03-09 13:05:52 +08002069 //clear play statue
2070 ctx = ctx_getPlayback((unsigned long)playback);
2071 if (ctx) {
Wentao MA4d85ff32022-09-23 11:36:18 +08002072 //reset old playback status
hualing chen451c8f72022-03-09 13:05:52 +08002073 // struct {
2074 // DVR_WrapperPlaybackOpenParams_t param_open;
2075 // DVR_PlaybackHandle_t player;
2076 // DVR_PlaybackEventFunction_t event_fn;
2077 // void *event_userdata;
2078
2079 // /*total status = seg_status + status*/
2080 // DVR_PlaybackStatus_t seg_status;
2081 // DVR_WrapperPlaybackStatus_t status;
2082 // DVR_PlaybackPids_t pids_req;
2083 // DVR_PlaybackEvent_t last_event;
2084 // float speed;
2085 // DVR_Bool_t reach_end;
2086
2087 // DVR_WrapperInfo_t obsolete;
2088 // DVR_Bool_t tf_full;
2089 // } playback;
Wentao MA4d85ff32022-09-23 11:36:18 +08002090 ctx->playback.tf_full = DVR_FALSE;
2091 ctx->playback.reach_end = DVR_FALSE;
hualing chen451c8f72022-03-09 13:05:52 +08002092 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
2093 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
2094 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
2095 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
2096 }
hualing chen002e5b92022-02-23 17:51:21 +08002097 error = dvr_wrapper_start_playback(playback, flags, p_pids);
2098 return error;
2099}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002100
2101int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
2102{
2103 DVR_WrapperCtx_t *ctx;
2104 int error;
2105
2106 DVR_RETURN_IF_FALSE(playback);
2107
2108 ctx = ctx_getPlayback((unsigned long)playback);
2109 DVR_RETURN_IF_FALSE(ctx);
2110
Gong Kefdb31922022-06-17 17:11:16 +08002111 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002112 DVR_WRAPPER_INFO("libdvr_api, stop_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002113 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002114
2115 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
2116
2117 {
2118 /*remove all segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002119 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002120
wentao.mafd5283f2022-10-14 09:51:13 +08002121 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002122 // prefetch() here incurring self_assign is used to avoid some compiling
2123 // warnings.
2124 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002125 list_for_each_entry(p_seg, &ctx->segments, head) {
2126 error = dvr_playback_remove_segment(ctx->playback.player, p_seg->playback_info.segment_id);
Wentao MA96f68962022-06-15 19:45:35 +08002127 DVR_WRAPPER_INFO("playback(sn:%ld) remove seg(%lld) (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002128 ctx->sn, p_seg->playback_info.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002129 }
2130 ctx_freeSegments(ctx);
2131 }
2132
Wentao MA96f68962022-06-15 19:45:35 +08002133 DVR_WRAPPER_INFO("playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002134 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002135
2136 return error;
2137}
2138
2139int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
2140{
2141 DVR_WrapperCtx_t *ctx;
2142 int error;
2143
2144 DVR_RETURN_IF_FALSE(playback);
2145
2146 ctx = ctx_getPlayback((unsigned long)playback);
2147 DVR_RETURN_IF_FALSE(ctx);
2148
Gong Kefdb31922022-06-17 17:11:16 +08002149 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002150 DVR_WRAPPER_INFO("libdvr_api, pause_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002151 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08002152 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08002153 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08002154 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002155
2156 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
2157
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002158 ctx->playback.speed = 0.0f;
2159
Wentao MA96f68962022-06-15 19:45:35 +08002160 DVR_WRAPPER_INFO("playback(sn:%ld) paused (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002161 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002162
2163 return error;
2164}
2165
2166int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
2167{
2168 DVR_WrapperCtx_t *ctx;
2169 int error;
2170
2171 DVR_RETURN_IF_FALSE(playback);
2172
2173 ctx = ctx_getPlayback((unsigned long)playback);
2174 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08002175 //if set limit.we need check if seek to valid data when resume
2176 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
2177 if (dvr_playback_check_limit(ctx->playback.player)) {
2178 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2179 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002180 DVR_WRAPPER_INFO("seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002181 ctx->sn, time_offset, expired);
2182 time_offset = expired;
2183 dvr_wrapper_seek_playback(playback, time_offset);
2184 }
2185 }
Gong Kefdb31922022-06-17 17:11:16 +08002186 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002187 DVR_WRAPPER_INFO("libdvr_api, resume_playback (sn:%ld)", ctx->sn);
Gong Kefdb31922022-06-17 17:11:16 +08002188 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002189
2190 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002191 ctx->playback.speed = 100.0f;
2192
Wentao MA96f68962022-06-15 19:45:35 +08002193 DVR_WRAPPER_INFO("playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
Gong Kefdb31922022-06-17 17:11:16 +08002194 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002195
2196 return error;
2197}
2198
2199int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
2200{
2201 DVR_WrapperCtx_t *ctx;
2202 int error;
2203 DVR_PlaybackSpeed_t dvr_speed = {
2204 .speed = { speed },
2205 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2206 };
2207
2208 DVR_RETURN_IF_FALSE(playback);
2209
2210 ctx = ctx_getPlayback((unsigned long)playback);
2211 DVR_RETURN_IF_FALSE(ctx);
2212
Gong Kefdb31922022-06-17 17:11:16 +08002213 wrapper_mutex_lock(&ctx->wrapper_lock);
Wentao MA804bab12022-11-29 10:01:26 +08002214 DVR_WRAPPER_INFO("libdvr_api, set_playback_speed (sn:%ld) speed:%d", ctx->sn, (int)speed);
Gong Kefdb31922022-06-17 17:11:16 +08002215 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002216
2217 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2218
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002219 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2220 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2221 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
Wentao MA96f68962022-06-15 19:45:35 +08002222 DVR_WRAPPER_INFO("x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002223 error = dvr_playback_resume(ctx->playback.player);
2224 } else if (ctx->playback.speed == 0.0f
2225 && speed != 0.0f
2226 && speed != 100.0f) {
2227 /*libdvr do not support pause with speed=0, will not be here*/
Wentao MA96f68962022-06-15 19:45:35 +08002228 DVR_WRAPPER_INFO("x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002229 error = dvr_playback_resume(ctx->playback.player);
2230 }
2231
2232 ctx->playback.speed = speed;
2233
Wentao MA96f68962022-06-15 19:45:35 +08002234 DVR_WRAPPER_INFO("playback(sn:%ld) speeded(x%f) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002235 ctx->sn, speed, error);
Gong Kefdb31922022-06-17 17:11:16 +08002236 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002237
2238 return error;
2239}
2240
hualing chen03fd4942021-07-15 15:56:41 +08002241int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2242{
2243 DVR_WrapperCtx_t *ctx;
2244 int error;
2245
2246 DVR_RETURN_IF_FALSE(playback);
2247
2248 ctx = ctx_getPlayback((unsigned long)playback);
2249 DVR_RETURN_IF_FALSE(ctx);
2250
Gong Kefdb31922022-06-17 17:11:16 +08002251 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002252
Wentao MA804bab12022-11-29 10:01:26 +08002253 DVR_WRAPPER_INFO("libdvr_api, setlimit_playback (sn:%ld) time:%lld, limit:%d",
2254 ctx->sn, time, limit);
Gong Kefdb31922022-06-17 17:11:16 +08002255 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002256
2257 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
Wentao MAe8ba5172022-08-09 11:18:17 +08002258 DVR_WRAPPER_INFO("playback(sn:%ld) set_limit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
hualing chen03fd4942021-07-15 15:56:41 +08002259
Gong Kefdb31922022-06-17 17:11:16 +08002260 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen03fd4942021-07-15 15:56:41 +08002261
2262 return error;
2263}
2264
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002265int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2266{
2267 DVR_WrapperCtx_t *ctx;
2268 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002269 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Wentao MA804bab12022-11-29 10:01:26 +08002270 uint64_t segment_id = ULLONG_MAX;
2271 uint32_t segment_offset = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002272
2273 DVR_RETURN_IF_FALSE(playback);
2274
2275 ctx = ctx_getPlayback((unsigned long)playback);
2276 DVR_RETURN_IF_FALSE(ctx);
2277
Gong Kefdb31922022-06-17 17:11:16 +08002278 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002279
Wentao MA804bab12022-11-29 10:01:26 +08002280 DVR_WRAPPER_INFO("libdvr_api, seek_playback (sn:%ld) offset:%dms", ctx->sn, time_offset);
Gong Kefdb31922022-06-17 17:11:16 +08002281 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002282
hualing chen03fd4942021-07-15 15:56:41 +08002283 //if set limit info we need check ts data is
2284 //expired when seek
2285 if (dvr_playback_check_limit(ctx->playback.player)) {
2286 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2287 if (expired > time_offset) {
Wentao MA96f68962022-06-15 19:45:35 +08002288 DVR_WRAPPER_INFO("seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
hualing chen03fd4942021-07-15 15:56:41 +08002289 ctx->sn, time_offset, expired);
2290 time_offset = expired;
2291 }
2292 }
Wentao MAf51f0142023-09-27 13:59:48 +08002293 ctx->playback.reach_end = DVR_FALSE;
hualing chen03fd4942021-07-15 15:56:41 +08002294
Wentao MA804bab12022-11-29 10:01:26 +08002295 const uint32_t obsolete_time = (uint32_t)ctx->playback.obsolete.time;
2296 DVR_WrapperPlaybackSegmentInfo_t *p_seg_first = ctx->segments.c_prev;
2297 DVR_WrapperPlaybackSegmentInfo_t *p_seg_last = ctx->segments.c_next;
2298 const uint64_t first_id = p_seg_first->seg_info.id;
2299 const uint64_t last_id = p_seg_last->seg_info.id;
2300 const uint32_t last_duration = p_seg_last->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002301
Wentao MA804bab12022-11-29 10:01:26 +08002302 if (time_offset <= obsolete_time) {
2303 segment_id = first_id;
2304 segment_offset = 0;
2305 DVR_WRAPPER_WARN("time_offset %u isn't greater than obsolete time %u, "
2306 "so seek to beginning position of segment %llu",
2307 time_offset,obsolete_time,segment_id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002308 } else {
Wentao MA804bab12022-11-29 10:01:26 +08002309 uint32_t total_duration = 0;
2310 // This error is suppressed as the macro code is picked from kernel.
2311 // prefetch() here incurring self_assign is used to avoid some compiling
2312 // warnings.
2313 // coverity[self_assign]
2314 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2315 const uint32_t segment_begin = obsolete_time + total_duration;
2316 const uint32_t segment_end = segment_begin + p_seg->seg_info.duration;
2317 if (time_offset >= segment_begin && time_offset <= segment_end) {
2318 segment_id = p_seg->seg_info.id;
2319 segment_offset = time_offset - segment_begin;
2320 break;
2321 }
2322 total_duration += p_seg->seg_info.duration;
2323 }
2324 if (segment_id == ULLONG_MAX) {
2325 segment_id = last_id;
2326 segment_offset = last_duration;
2327 DVR_WRAPPER_WARN("time_offset %u is out of range, so seek to"
2328 " the end position of segment %llu",time_offset,segment_id);
2329 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002330 }
2331
Wentao MA804bab12022-11-29 10:01:26 +08002332 DVR_WRAPPER_INFO("seek playback(sn:%ld) (segment_id:%llu, segment_offset:%u)\n",
2333 ctx->sn, segment_id, segment_offset);
2334 error = dvr_playback_seek(ctx->playback.player, segment_id, segment_offset);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002335
Gong Kefdb31922022-06-17 17:11:16 +08002336 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002337
2338 return error;
2339}
2340
2341int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2342{
2343 DVR_WrapperCtx_t *ctx;
2344 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002345 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002346
2347 DVR_RETURN_IF_FALSE(playback);
2348 DVR_RETURN_IF_FALSE(p_pids);
2349
2350 ctx = ctx_getPlayback((unsigned long)playback);
2351 DVR_RETURN_IF_FALSE(ctx);
2352
Gong Kefdb31922022-06-17 17:11:16 +08002353 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002354
Wentao MA804bab12022-11-29 10:01:26 +08002355 DVR_WRAPPER_INFO("libdvr_api, update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002356 ctx->sn,
2357 p_pids->video.pid, p_pids->video.format,
2358 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002359 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002360
2361 ctx->playback.pids_req = *p_pids;
2362
2363 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002364 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002365 // prefetch() here incurring self_assign is used to avoid some compiling
2366 // warnings.
2367 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002368 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002369 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002370 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2371 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2372 /*check update for pids*/
2373 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2374 p_seg->playback_info.pids = *p_pids;
2375 error = dvr_playback_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002376 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002377 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002378 ctx->sn, p_seg->seg_info.id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002379 /*do not break, let list updated*/
2380 }
2381 }
2382 }
2383 /*break;*/
2384 }
2385 }
2386
Wentao MA96f68962022-06-15 19:45:35 +08002387 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002388 ctx->sn,
2389 p_pids->video.pid, p_pids->video.format,
2390 p_pids->audio.pid, p_pids->audio.format,
2391 error);
2392
Gong Kefdb31922022-06-17 17:11:16 +08002393 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002394
2395 return error;
2396}
2397
hualing chena5f03222021-12-02 11:22:35 +08002398int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2399{
2400 DVR_WrapperCtx_t *ctx;
2401 int error;
Wentao MA270dc0f2022-08-23 13:17:26 +08002402 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
hualing chena5f03222021-12-02 11:22:35 +08002403
2404 DVR_RETURN_IF_FALSE(playback);
2405 DVR_RETURN_IF_FALSE(p_pids);
2406
2407 ctx = ctx_getPlayback((unsigned long)playback);
2408 DVR_RETURN_IF_FALSE(ctx);
2409
Gong Kefdb31922022-06-17 17:11:16 +08002410 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002411
Wentao MA804bab12022-11-29 10:01:26 +08002412 DVR_WRAPPER_INFO("libdvr_api, only_update_playback (sn:%ld) v/a(%d:%d/%d:%d)",
hualing chena5f03222021-12-02 11:22:35 +08002413 ctx->sn,
2414 p_pids->video.pid, p_pids->video.format,
2415 p_pids->audio.pid, p_pids->audio.format);
Gong Kefdb31922022-06-17 17:11:16 +08002416 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002417
2418 ctx->playback.pids_req = *p_pids;
2419
2420 error = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002421 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002422 // prefetch() here incurring self_assign is used to avoid some compiling
2423 // warnings.
2424 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002425 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
hualing chena5f03222021-12-02 11:22:35 +08002426 /*should update the whole list of segments*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002427 /*if (p_seg->seg_info.id == ctx->current_segment_id)*/ {
2428 /*list_for_each_entry_from(p_seg, &ctx->segments, head)*/ {
2429 /*check update for pids*/
2430 if (memcmp(&p_seg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2431 p_seg->playback_info.pids = *p_pids;
2432 error = dvr_playback_only_update_segment_pids(ctx->playback.player, p_seg->seg_info.id, p_pids);
hualing chena5f03222021-12-02 11:22:35 +08002433 if (error) {
Wentao MA96f68962022-06-15 19:45:35 +08002434 DVR_WRAPPER_INFO("failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
Wentao MA270dc0f2022-08-23 13:17:26 +08002435 ctx->sn, p_seg->seg_info.id, error);
hualing chena5f03222021-12-02 11:22:35 +08002436 /*do not break, let list updated*/
2437 }
2438 }
2439 }
2440 /*break;*/
2441 }
2442 }
2443
Wentao MA96f68962022-06-15 19:45:35 +08002444 DVR_WRAPPER_INFO("update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
hualing chena5f03222021-12-02 11:22:35 +08002445 ctx->sn,
2446 p_pids->video.pid, p_pids->video.format,
2447 p_pids->audio.pid, p_pids->audio.format,
2448 error);
2449
Gong Kefdb31922022-06-17 17:11:16 +08002450 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chena5f03222021-12-02 11:22:35 +08002451
2452 return error;
2453}
2454
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002455int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2456{
2457 DVR_WrapperCtx_t *ctx;
2458 DVR_WrapperPlaybackStatus_t s;
2459 DVR_PlaybackStatus_t play_status;
2460 int error;
2461
2462 DVR_RETURN_IF_FALSE(playback);
2463 DVR_RETURN_IF_FALSE(status);
2464
2465 ctx = ctx_getPlayback((unsigned long)playback);
2466 DVR_RETURN_IF_FALSE(ctx);
2467
Gong Kefdb31922022-06-17 17:11:16 +08002468 wrapper_mutex_lock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002469
Gong Kefdb31922022-06-17 17:11:16 +08002470 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002471
Wentao MA804bab12022-11-29 10:01:26 +08002472 dvr_playback_get_status(ctx->playback.player, &play_status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002473
2474 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002475 error = process_generatePlaybackStatus(ctx, &s);
2476
hualing chenb5cd42e2020-04-15 17:03:34 +08002477 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2478 //reach end need set full time to cur.so app can exist playback.
Wentao MA96f68962022-06-15 19:45:35 +08002479 DVR_WRAPPER_INFO("set cur time to full time, reach end occur");
hualing chenb5cd42e2020-04-15 17:03:34 +08002480 s.info_cur.time = s.info_full.time;
2481 }
Wentao MA804bab12022-11-29 10:01:26 +08002482 DVR_WRAPPER_INFO("get_playback_status (sn:%ld) state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2483 ctx->sn, s.state, s.info_cur.time, s.info_full.time, s.info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002484
2485 *status = s;
2486
Gong Kefdb31922022-06-17 17:11:16 +08002487 wrapper_mutex_unlock(&ctx->wrapper_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002488
2489 return error;
2490}
2491
hualing chen266b9502020-04-04 17:39:39 +08002492int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2493{
2494 DVR_WrapperCtx_t *ctx;
2495 int error;
2496
2497 DVR_RETURN_IF_FALSE(playback);
2498 DVR_RETURN_IF_FALSE(p_secure_buf);
2499
2500 ctx = ctx_getPlayback((unsigned long)playback);
2501 DVR_RETURN_IF_FALSE(ctx);
2502
Gong Kefdb31922022-06-17 17:11:16 +08002503 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002504 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
Gong Kefdb31922022-06-17 17:11:16 +08002505 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002506 return error;
2507}
2508
2509int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2510{
2511 DVR_WrapperCtx_t *ctx;
2512 int error;
2513
2514 DVR_RETURN_IF_FALSE(playback);
2515 DVR_RETURN_IF_FALSE(func);
2516
2517 ctx = ctx_getPlayback((unsigned long)playback);
2518 DVR_RETURN_IF_FALSE(ctx);
2519
Gong Kefdb31922022-06-17 17:11:16 +08002520 wrapper_mutex_lock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002521 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
Gong Kefdb31922022-06-17 17:11:16 +08002522 wrapper_mutex_unlock(&ctx->wrapper_lock);
hualing chen266b9502020-04-04 17:39:39 +08002523 return error;
2524}
2525
Zhiqiang Han620b9252021-11-09 14:23:20 +08002526int dvr_wrapper_segment_del_by_location (const char *location)
2527{
2528 char fpath[DVR_MAX_LOCATION_SIZE];
2529
2530 DVR_RETURN_IF_FALSE(location);
2531
2532 /*del the stats file*/
2533 sprintf(fpath, "%s.stats", location);
2534 unlink(fpath);
2535
2536 return dvr_segment_del_by_location(location);
2537}
2538
2539int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2540{
2541 FILE *fp;
2542 char fpath[DVR_MAX_LOCATION_SIZE];
2543
2544 DVR_RETURN_IF_FALSE(location);
2545 DVR_RETURN_IF_FALSE(p_info);
2546
2547 if (p_info)
2548 memset(p_info, 0, sizeof(p_info[0]));
2549
2550 memset(fpath, 0, sizeof(fpath));
2551 sprintf(fpath, "%s.stats", location);
2552
2553 /*stats file exists*/
2554 if ((fp = fopen(fpath, "r"))) {
2555 char buf[256];
2556
2557 if (fgets(buf, sizeof(buf), fp) != NULL
2558 && (sscanf(buf, ":%llu:%lu:%u",
2559 &p_info->size,
2560 &p_info->time,
2561 &p_info->pkts) == 3)) {
2562 fclose(fp);
Wentao MA96f68962022-06-15 19:45:35 +08002563 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 +08002564 return DVR_SUCCESS;
2565 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002566 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002567 }
2568
2569 /*fallback, slow on mass files*/
Wentao MA96f68962022-06-15 19:45:35 +08002570 DVR_WRAPPER_INFO("rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002571
2572 int error;
2573 uint32_t n_ids;
2574 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002575
hualing chen8aed9582021-12-24 17:59:56 +08002576 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002577
Zhiqiang Han620b9252021-11-09 14:23:20 +08002578 if (!error) {
2579 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002580 struct list_head info_list; /**< segment list head*/
2581 INIT_LIST_HEAD(&info_list);
2582
2583 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002584 error = dvr_segment_get_allInfo(location, &info_list);
2585 if (error == DVR_FAILURE) {
wentao.maa210e5e2022-10-12 16:10:03 +08002586 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2587 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
hualing chenb9a02922021-12-14 11:29:47 +08002588
2589 memset(&info, 0, sizeof(info));
2590 error = DVR_FAILURE;
Wentao MA96f68962022-06-15 19:45:35 +08002591 DVR_WRAPPER_INFO("fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002592 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002593
wentao.maf57dd232022-10-08 16:07:29 +08002594 // Tainted data issue originating from fgets seem false positive, so we
2595 // just suppress it here.
2596 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002597 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002598 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002599 if (!error) {
2600 p_info->size += info.size;
2601 p_info->time += info.duration;
2602 p_info->pkts += info.nb_packets;
2603 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002604 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002605 break;
2606 }
2607 }
2608 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002609 DVR_WRAPPER_INFO("get list segment_nb::%d",n_ids);
wentao.maf57dd232022-10-08 16:07:29 +08002610 // Tainted data issue originating from fgets seem false positive, so we
2611 // just suppress it here.
2612 // coverity[tainted_data]
hualing chenb9a02922021-12-14 11:29:47 +08002613 for (i = 0; i < n_ids; i++) {
2614
2615 DVR_RecordSegmentInfo_t *seg_info;
2616 DVR_PlaybackSegmentFlag_t flags;
2617 int found = 0;
wentao.mafd5283f2022-10-14 09:51:13 +08002618 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002619 // prefetch() here incurring self_assign is used to avoid some compiling
2620 // warnings.
2621 // coverity[self_assign]
hualing chenb9a02922021-12-14 11:29:47 +08002622 list_for_each_entry(seg_info, &info_list, head)
2623 {
hualing chen8aed9582021-12-24 17:59:56 +08002624 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002625 found = 1;
2626 break;
2627 }
2628 }
2629 if (!found) {
Wentao MA96f68962022-06-15 19:45:35 +08002630 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 +08002631 if (p_ids[i] == n_ids - 1) {
wentao.maa210e5e2022-10-12 16:10:03 +08002632 DVR_RecordSegmentInfo_t info = { .id = 0, .nb_pids = 0,
2633 .pids = {0}, .duration = 0, .size = 0, .nb_packets = 0 };
Wentao MA96f68962022-06-15 19:45:35 +08002634 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 +08002635 error = dvr_segment_get_info(location, p_ids[i], &info);
2636 if (!error) {
2637 p_info->size += info.size;
2638 p_info->time += info.duration;
2639 p_info->pkts += info.nb_packets;
2640 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002641 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chen8aed9582021-12-24 17:59:56 +08002642 break;
2643 }
2644 }
hualing chenb9a02922021-12-14 11:29:47 +08002645 continue;
2646 }
2647
2648 if (!error) {
2649 p_info->size += seg_info->size;
2650 p_info->time += seg_info->duration;
2651 p_info->pkts += seg_info->nb_packets;
2652 } else {
Wentao MA96f68962022-06-15 19:45:35 +08002653 DVR_WRAPPER_INFO("%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002654 break;
2655 }
2656 }
2657 //free list
2658 DVR_RecordSegmentInfo_t *segment = NULL;
2659 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2660 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2661 {
2662 if (segment) {
2663 list_del(&segment->head);
2664 free(segment);
2665 }
2666 }
2667 }
2668 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002669 } else {
2670 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002671 }
Wentao MA96f68962022-06-15 19:45:35 +08002672 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 +08002673
2674 return (error)? DVR_FAILURE : DVR_SUCCESS;
2675}
2676
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002677static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2678{
wentao.maa210e5e2022-10-12 16:10:03 +08002679 DVR_WrapperEventCtx_t evt = {
2680 .sn = (unsigned long)userdata,
2681 .type = W_REC,
2682 .record.event = event,
2683 .record.status = *(DVR_RecordStatus_t *)params
2684 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002685
2686 DVR_RETURN_IF_FALSE(userdata);
2687
Wentao MA804bab12022-11-29 10:01:26 +08002688 DVR_WRAPPER_DEBUG("record event 0x%x (sn:%ld)", evt.record.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002689 return ctx_addRecordEvent(&evt);
2690}
2691
2692static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2693{
wentao.maa210e5e2022-10-12 16:10:03 +08002694 DVR_WrapperEventCtx_t evt = {
2695 .sn = (unsigned long)userdata,
2696 .type = W_PLAYBACK,
2697 .playback.event = event,
2698 .playback.status = *(DVR_Play_Notify_t *)params
2699 };
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002700
2701 DVR_RETURN_IF_FALSE(userdata);
2702
2703 evt.sn = (unsigned long)userdata;
2704 evt.type = W_PLAYBACK;
2705 evt.playback.event = event;
2706 evt.playback.status = *(DVR_Play_Notify_t *)params;
Wentao MA804bab12022-11-29 10:01:26 +08002707 DVR_WRAPPER_DEBUG("playback event 0x%x (sn:%ld)", evt.playback.event, evt.sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002708 return ctx_addPlaybackEvent(&evt);
2709}
2710
2711static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2712{
Wentao MA270dc0f2022-08-23 13:17:26 +08002713 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 +08002714 ctx->sn,
2715 evt,
2716 status->info.time,
2717 status->info.size,
2718 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002719 status->info_obsolete.time,
2720 status->info_obsolete.size,
2721 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002722
2723 if (ctx->record.event_fn)
2724 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2725 return 0;
2726}
2727
Zhiqiang Han620b9252021-11-09 14:23:20 +08002728static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2729{
2730 FILE *fp;
2731 char fpath[DVR_MAX_LOCATION_SIZE];
2732
2733 DVR_RETURN_IF_FALSE(location);
2734 DVR_RETURN_IF_FALSE(p_status);
2735
2736 sprintf(fpath, "%s.stats", location);
2737
2738 /*stats file*/
2739 if ((fp = fopen(fpath, "w"))) {
2740 char buf[256];
2741 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2742 p_status->info.size - p_status->info_obsolete.size,
2743 p_status->info.time - p_status->info_obsolete.time,
2744 p_status->info.pkts - p_status->info_obsolete.pkts);
2745 fputs(buf, fp);
Zhiqiang Handc3bfe52022-07-07 10:48:39 +08002746 fflush(fp);
2747 fsync(fileno(fp));
Zhiqiang Han620b9252021-11-09 14:23:20 +08002748 fclose(fp);
2749 return DVR_SUCCESS;
2750 }
2751
2752 return DVR_FAILURE;
2753}
2754
2755
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002756static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2757{
2758 DVR_RecordStartParams_t param;
2759 DVR_RecordSegmentInfo_t seg_info;
2760 int i;
2761 int error;
2762
2763 memcpy(&param, &ctx->record.param_update, sizeof(param));
2764 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2765 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2766 for (i = 0; i < param.segment.nb_pids; i++) {
2767 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2768 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2769 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2770 ctx->record.param_update.segment.nb_pids++;
2771 }
2772 }
2773 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2774 {
2775 DVR_RecordSegmentInfo_t new_seg_info =
2776 { .id = ctx->record.param_update.segment.segment_id, };
2777 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2778 wrapper_addRecordSegment(ctx, &new_seg_info);
2779 }
2780
Wentao MA96f68962022-06-15 19:45:35 +08002781 DVR_WRAPPER_INFO("record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002782 return error;
2783}
2784
Wentao MA270dc0f2022-08-23 13:17:26 +08002785static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *p_seg)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002786{
Wentao MA270dc0f2022-08-23 13:17:26 +08002787 return wrapper_removeRecordSegment(ctx, p_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002788}
2789
2790/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002791static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002792{
2793 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002794 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002795
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002796 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002797 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2798
2799 ctx->record.status.state = ctx->record.seg_status.state;
2800 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2801 memcpy(ctx->record.status.pids.pids,
2802 ctx->record.seg_status.info.pids,
2803 sizeof(ctx->record.status.pids.pids));
2804 ctx->current_segment_id = ctx->record.seg_status.info.id;
2805
wentao.mafd5283f2022-10-14 09:51:13 +08002806 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08002807 // prefetch() here incurring self_assign is used to avoid some compiling
2808 // warnings.
2809 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08002810 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
2811 if (p_seg->info.id != ctx->record.seg_status.info.id) {
2812 ctx->record.status.info.time += p_seg->info.duration;
2813 ctx->record.status.info.size += p_seg->info.size;
2814 ctx->record.status.info.pkts += p_seg->info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002815 }
2816 }
2817
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002818 ctx->record.status.info_obsolete = ctx->record.obsolete;
2819
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002820 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002821
2822 if (status) {
2823 *status = ctx->record.status;
2824 status->info.time += ctx->record.seg_status.info.duration;
2825 status->info.size += ctx->record.seg_status.info.size;
2826 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2827 }
2828
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002829 return DVR_SUCCESS;
2830}
2831
2832
2833static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2834{
2835 DVR_WrapperRecordStatus_t status;
2836
2837 memset(&status, 0, sizeof(status));
2838
Wentao MA804bab12022-11-29 10:01:26 +08002839 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d)\n",
2840 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002841 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
Wentao MA96f68962022-06-15 19:45:35 +08002842 DVR_WRAPPER_INFO("evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002843 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002844 return 0;
2845 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002846 switch (evt->record.event)
2847 {
2848 case DVR_RECORD_EVENT_STATUS:
2849 {
2850 switch (evt->record.status.state)
2851 {
2852 case DVR_RECORD_STATE_OPENED:
2853 case DVR_RECORD_STATE_CLOSED:
2854 {
2855 ctx->record.seg_status = evt->record.status;
2856
2857 status.state = evt->record.status.state;
2858 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002859 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002860 } break;
2861 case DVR_RECORD_STATE_STARTED:
2862 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002863 ctx->record.seg_status = evt->record.status;
2864
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002865 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002866 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002867 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002868
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002869 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002870 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002871 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
Wentao MA96f68962022-06-15 19:45:35 +08002872 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 +08002873 ctx->sn,
2874 evt->record.status.info.size,
2875 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002876 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2877 /*should notify the recording's stop*/
2878 int error = dvr_record_close(ctx->record.recorder);
Wentao MA96f68962022-06-15 19:45:35 +08002879 DVR_WRAPPER_INFO("stop record(%lu)=%d, failed to start new segment for recording.",
Zhiqiang Hand977e972020-05-11 11:30:47 +08002880 ctx->sn, error);
2881 status.state = DVR_RECORD_STATE_CLOSED;
2882 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002883 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002884 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002885 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002886
2887 if (ctx->record.param_open.is_timeshift
2888 && ctx->record.param_open.max_time
2889 && status.info.time >= ctx->record.param_open.max_time) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002890 DVR_WrapperRecordSegmentInfo_t *p_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002891
2892 /*as the player do not support null playlist,
2893 there must be one segment existed at any time,
2894 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002895 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2896 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002897 /*only one segment, waiting for more*/
Wentao MA96f68962022-06-15 19:45:35 +08002898 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 +08002899 status.info.size,
2900 ctx->record.param_open.max_time,
2901 ctx->record.param_open.segment_size);
2902 } else {
2903 /*timeshifting, remove the 1st segment and notify the player*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002904 record_removeSegment(ctx, p_seg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002905
2906 process_generateRecordStatus(ctx, &status);
2907 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002908 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002909 }
2910 }
2911
Wentao MAf4072032022-06-30 13:50:45 +08002912 const loff_t actual_size = status.info.size;
2913 const loff_t max_size = ctx->record.param_open.max_size;
2914 const loff_t segment_size = ctx->record.param_open.segment_size;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002915
Wentao MAf4072032022-06-30 13:50:45 +08002916 if (ctx->record.param_open.is_timeshift && max_size) {
2917 if (actual_size >= max_size) {
Wentao MA270dc0f2022-08-23 13:17:26 +08002918 DVR_WrapperRecordSegmentInfo_t *p_seg;
Wentao MAf4072032022-06-30 13:50:45 +08002919 /*as the player do not support null playlist,
2920 there must be one segment existed at any time,
2921 we have to keep two segments before remove one*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002922 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2923 if (p_seg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
Wentao MAf4072032022-06-30 13:50:45 +08002924 /*only one segment, waiting for more*/
2925 DVR_WRAPPER_INFO("warning: the size(%lld) of record < max size of segment(%lld)\n",
2926 actual_size, segment_size);
2927 } else {
Wentao MA270dc0f2022-08-23 13:17:26 +08002928 record_removeSegment(ctx, p_seg);
Wentao MAf4072032022-06-30 13:50:45 +08002929
2930 process_generateRecordStatus(ctx, &status);
2931 process_notifyRecord(ctx, evt->record.event, &status);
2932 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
2933 }
2934 if (actual_size >= max_size + segment_size/2) {
2935 dvr_record_discard_coming_data(ctx->record.recorder,DVR_TRUE);
2936 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002937 } else {
Wentao MAf4072032022-06-30 13:50:45 +08002938 dvr_record_discard_coming_data(ctx->record.recorder,DVR_FALSE);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002939 }
2940 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002941 } break;
2942 case DVR_RECORD_STATE_STOPPED:
2943 {
2944 ctx->record.seg_status = evt->record.status;
2945
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002946 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002947 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002948 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002949 } break;
2950 default:
2951 break;
2952 }
2953 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002954 case DVR_RECORD_EVENT_WRITE_ERROR: {
2955 ctx->record.seg_status = evt->record.status;
2956 status.state = evt->record.status.state;
2957 process_notifyRecord(ctx, evt->record.event, &status);
2958 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002959 default:
2960 break;
2961 }
2962 return DVR_SUCCESS;
2963}
2964
2965static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2966{
Wentao MA80179512022-11-03 12:20:03 +08002967 DVR_RETURN_IF_FALSE(ctx->playback.event_fn != NULL);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002968
Wentao MA80179512022-11-03 12:20:03 +08002969 const time_t cur_time = status->info_cur.time;
2970 const time_t full_time = status->info_full.time;
2971 const time_t obsolete_time = status->info_obsolete.time;
2972 const time_t origin_offset = cur_time + obsolete_time;
2973 DVR_WRAPPER_INFO("playback progress notify(sn:%ld) evt(0x%x)"
2974 " actual_slider_pos: %02d:%02d:%02d.%03d/%02d:%02d:%02d.%03d (%7ld ms/%7ld ms),"
2975 " offset_from_origin: %02d:%02d:%02d.%03d (%7ld ms),"
2976 " dump status:state/cur/full/obsolete(%d/%ld/%ld/%ld)",
2977 ctx->sn,evt,cur_time/1000/3600,cur_time/1000%3600/60,cur_time/1000%60,cur_time%1000,
2978 full_time/1000/3600,full_time/1000%3600/60,full_time/1000%60,full_time%1000,
2979 cur_time,full_time,
2980 origin_offset/1000/3600,origin_offset/1000%3600/60,origin_offset/1000%60,origin_offset%1000,
2981 origin_offset,status->state,cur_time,full_time,obsolete_time);
2982
2983 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002984}
2985
2986/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002987static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002988{
2989 /*the current seg is not covered in the statistics*/
Wentao MA270dc0f2022-08-23 13:17:26 +08002990 DVR_WrapperPlaybackSegmentInfo_t *p_seg;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002991
2992 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2993 ctx->playback.status.pids = ctx->playback.pids_req;
2994
2995 ctx->playback.status.state = ctx->playback.seg_status.state;
2996 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2997 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2998 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2999
wentao.mafd5283f2022-10-14 09:51:13 +08003000 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003001 // prefetch() here incurring self_assign is used to avoid some compiling
3002 // warnings.
3003 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003004 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3005 if (p_seg->seg_info.id == ctx->playback.seg_status.segment_id) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003006 break;
hualing chen451c8f72022-03-09 13:05:52 +08003007 }
3008
Wentao MA270dc0f2022-08-23 13:17:26 +08003009 ctx->playback.status.info_cur.time += p_seg->seg_info.duration;
3010 ctx->playback.status.info_cur.size += p_seg->seg_info.size;
3011 ctx->playback.status.info_cur.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003012 }
wentao.mafd5283f2022-10-14 09:51:13 +08003013 // This error is suppressed as the macro code is picked from kernel.
wentao.maa22bc852022-10-13 12:18:06 +08003014 // prefetch() here incurring self_assign is used to avoid some compiling
3015 // warnings.
3016 // coverity[self_assign]
Wentao MA270dc0f2022-08-23 13:17:26 +08003017 list_for_each_entry_reverse(p_seg, &ctx->segments, head) {
3018 ctx->playback.status.info_full.time += p_seg->seg_info.duration;
3019 ctx->playback.status.info_full.size += p_seg->seg_info.size;
3020 ctx->playback.status.info_full.pkts += p_seg->seg_info.nb_packets;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003021 }
3022
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003023 if (status) {
3024 *status = ctx->playback.status;
3025 /*deal with current, lack size and pkts with the current*/
3026 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08003027 //get last segment id
Wentao MA270dc0f2022-08-23 13:17:26 +08003028 DVR_WrapperRecordSegmentInfo_t *p_seg;
hualing chen56c0a162022-01-27 17:01:50 +08003029
Wentao MA270dc0f2022-08-23 13:17:26 +08003030 p_seg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
3031 if (ctx->playback.tf_full == DVR_TRUE && p_seg->info.id == ctx->current_segment_id) {
hualing chen56c0a162022-01-27 17:01:50 +08003032 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
3033 status->info_obsolete.time = ctx->playback.obsolete.time;
Wentao MA270dc0f2022-08-23 13:17:26 +08003034 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 +08003035 }
3036 else
3037 {
Wentao MA804bab12022-11-29 10:01:26 +08003038 status->info_obsolete.time = ctx->playback.obsolete.time;
3039 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
hualing chen56c0a162022-01-27 17:01:50 +08003040 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003041 }
3042
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003043 return DVR_SUCCESS;
3044}
3045
3046static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3047{
Wentao MA804bab12022-11-29 10:01:26 +08003048 DVR_WRAPPER_DEBUG("evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003049 evt->sn, evt->playback.event,
3050 evt->playback.status.play_status.state,
3051 evt->playback.status.play_status.segment_id,
3052 evt->playback.status.play_status.time_cur,
3053 evt->playback.status.play_status.time_end);
3054
3055 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08003056 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
3057 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
3058 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
3059 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003060 ctx->playback.last_event = evt->playback.event;
3061
3062 switch (evt->playback.event)
3063 {
3064 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
3065 case DVR_PLAYBACK_EVENT_REACHED_END:
3066 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
3067 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08003068 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08003069 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08003070 case DVR_PLAYBACK_EVENT_NODATA:
3071 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003072 {
3073 DVR_WrapperPlaybackStatus_t status;
3074
3075 /*copy status of segment*/
3076 ctx->playback.seg_status = evt->playback.status.play_status;
3077
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08003078 /*generate status of the whole playback*/
3079 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003080
3081 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Wentao MA96f68962022-06-15 19:45:35 +08003082 DVR_WRAPPER_INFO("playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08003083 if (ctx->playback.param_open.is_timeshift
3084 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003085 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08003086 }
3087 /*trust the low level, make NO check.
3088 As this evt is changed to only once due to some operations(paused) in low level.
3089 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003090 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08003091 }
3092 */
3093 else {
3094 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08003095 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003096 }
wentao.mab9fe0ff2023-07-05 09:55:41 +08003097 } else {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003098 process_notifyPlayback(ctx, evt->playback.event, &status);
3099 }
3100 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003101 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
3102 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
3103 case DVR_PLAYBACK_EVENT_NO_KEY:
3104 {
Wentao MA96f68962022-06-15 19:45:35 +08003105 DVR_WRAPPER_INFO("playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003106 } break;
3107 default:
3108 {
Wentao MA96f68962022-06-15 19:45:35 +08003109 DVR_WRAPPER_INFO("playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003110 } break;
3111 }
3112 return 0;
3113}
3114
3115static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
3116{
3117 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
3118}
3119
Wentao MA96f68962022-06-15 19:45:35 +08003120int dvr_wrapper_set_log_level (int level)
3121{
Wentao MA804bab12022-11-29 10:01:26 +08003122 DVR_WRAPPER_INFO("libdvr_api, set_log_level %d", level);
Wentao MA96f68962022-06-15 19:45:35 +08003123 if (level<LOG_LV_DEFAULT || level>LOG_LV_FATAL) {
3124 DVR_WRAPPER_ERROR("Invalid dvr log level:%d", g_dvr_log_level);
3125 return DVR_FAILURE;
3126 }
3127 g_dvr_log_level = level;
3128 DVR_WRAPPER_INFO("New dvr log level:%d", g_dvr_log_level);
3129 return DVR_SUCCESS;
3130}
3131
Wentao MA5629ad82022-08-24 10:03:02 +08003132int dvr_wrapper_set_ac4_preselection_id(DVR_WrapperPlayback_t playback, int presel_id)
3133{
3134 DVR_WrapperCtx_t *ctx;
3135 int error;
3136
Wentao MA3e2dc452022-12-20 11:17:16 +08003137 DVR_RETURN_IF_FALSE(playback!=NULL);
Wentao MA5629ad82022-08-24 10:03:02 +08003138
3139 ctx = ctx_getPlayback((unsigned long)playback);
3140 DVR_RETURN_IF_FALSE(ctx);
3141
3142 wrapper_mutex_lock(&ctx->wrapper_lock);
3143
3144 WRAPPER_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->wrapper_lock);
3145
Wentao MA804bab12022-11-29 10:01:26 +08003146 DVR_WRAPPER_INFO("libdvr_api, set_ac4_preselection_id %d", presel_id);
Wentao MA5629ad82022-08-24 10:03:02 +08003147 error = dvr_playback_set_ac4_preselection_id(ctx->playback.player, presel_id);
3148
3149 wrapper_mutex_unlock(&ctx->wrapper_lock);
3150
3151 return error;
3152}
3153
wentao ma7d642782022-10-23 18:26:16 -07003154int dvr_wrapper_property_set(const char* prop_name, const char* prop_value)
3155{
3156 return dvr_prop_write(prop_name,prop_value);
3157}
3158
3159int dvr_wrapper_property_get(const char* prop_name, char* prop_value, int length)
3160{
3161 return dvr_prop_read(prop_name,prop_value,length);
3162}
3163