blob: 9491a041fa98b008652975eec64cdfff9a181130 [file] [log] [blame]
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001#include <stddef.h>
Zhiqiang Han620b9252021-11-09 14:23:20 +08002#include <unistd.h>
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08003#include <stdlib.h>
4#include <pthread.h>
5#include <string.h>
6#include <time.h>
7#include <errno.h>
Zhiqiang Han18f42c82021-08-11 17:13:28 +08008#include <sys/time.h>
9#include <time.h>
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080010
11#include "dvr_types.h"
12#include "dvr_record.h"
13#include "dvr_crypto.h"
14#include "dvr_playback.h"
15#include "dvr_segment.h"
16
17#include "AmTsPlayer.h"
18
19#include "list.h"
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080020
21#include "dvr_wrapper.h"
22
Gong Ke2a0ebbe2021-05-25 15:22:50 +080023#define DVR_WRAPPER_DEBUG(_level, _fmt...) \
24 DVR_DEBUG_FL(_level, "wrapper", _fmt)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080025
26/*duration of data to resume if paused with EVT_REACHED_END in timeshifting*/
hualing chen2932d372020-04-29 13:44:00 +080027#define TIMESHIFT_DATA_DURATION_TO_RESUME (600)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080028/*a tolerant gap*/
29#define DVR_PLAYBACK_END_GAP (1000)
30
31enum {
32 W_REC = 1,
33 W_PLAYBACK = 2,
34};
35
36enum {
37 U_PIDS = 0x01,
38 U_STAT = 0x02,
39 U_ALL = U_PIDS | U_STAT,
40};
41
42typedef struct {
43 /*make lock the 1st item in the structure*/
44 pthread_mutex_t lock;
45
46 /*rec or play*/
47 int type;
48
49 /*valid if (sn != 0)*/
50 unsigned long sn;
51 unsigned long sn_linked;
52
53 struct list_head segments; /**<head-add list*/
54 uint64_t current_segment_id; /**<id of the current segment*/
55
56 union {
57 struct {
58 DVR_WrapperRecordOpenParams_t param_open;
59 DVR_RecordStartParams_t param_start;
60 DVR_RecordStartParams_t param_update;
61 DVR_RecordHandle_t recorder;
62 DVR_RecordEventFunction_t event_fn;
63 void *event_userdata;
64
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +080065 /*total status = seg_status + status + obsolete*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080066 DVR_RecordStatus_t seg_status; /**<status of current segment*/
67 DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
68 uint64_t next_segment_id;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +080069
70 DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080071 } record;
72
73 struct {
74 DVR_WrapperPlaybackOpenParams_t param_open;
75 DVR_PlaybackHandle_t player;
76 DVR_PlaybackEventFunction_t event_fn;
77 void *event_userdata;
78
79 /*total status = seg_status + status*/
80 DVR_PlaybackStatus_t seg_status;
81 DVR_WrapperPlaybackStatus_t status;
82 DVR_PlaybackPids_t pids_req;
83 DVR_PlaybackEvent_t last_event;
Zhiqiang Han3eb75f92020-04-08 10:07:55 +080084 float speed;
hualing chenb5cd42e2020-04-15 17:03:34 +080085 DVR_Bool_t reach_end;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +080086
87 DVR_WrapperInfo_t obsolete;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080088 } playback;
89 };
90} DVR_WrapperCtx_t;
91
92typedef struct {
93 struct list_head head;
94 unsigned long sn;
95
96 /* rec or playback */
97 int type;
98
99 union {
100 struct {
101 DVR_RecordEvent_t event;
102 DVR_RecordStatus_t status;
103 } record;
104 struct {
105 DVR_PlaybackEvent_t event;
106 DVR_Play_Notify_t status;
107 } playback;
108 };
109} DVR_WrapperEventCtx_t;
110
111typedef struct {
112 pthread_mutex_t lock;
113 char *name;
114 int running;
115 pthread_cond_t cond;
116 pthread_t thread;
117 int type;
118} DVR_WrapperThreadCtx_t;
119
120typedef struct {
121 struct list_head head;
122
123 DVR_RecordSegmentInfo_t seg_info;
124 DVR_PlaybackSegmentInfo_t playback_info;
125} DVR_WrapperPlaybackSegmentInfo_t;
126
127typedef struct {
128 struct list_head head;
129
130 DVR_RecordSegmentInfo_t info;
131} DVR_WrapperRecordSegmentInfo_t;
132
133/* serial num generater */
134static unsigned long sn = 1;
135static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER;
136
137static inline unsigned long get_sn()
138{
hualing chenab0d1262021-09-26 15:22:50 +0800139 unsigned long no = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800140
141 pthread_mutex_lock(&sn_lock);
142 no = sn++;
143 if (!no)
144 no = sn++;
145 pthread_mutex_unlock(&sn_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800146 return no;
147}
148
149/* entity ctx */
150#define DVR_WRAPPER_MAX 10
151
152static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] =
153{
154 [0 ... (DVR_WRAPPER_MAX - 1)] =
155 {
156 .lock = PTHREAD_MUTEX_INITIALIZER,
157 .type = W_REC,
158 }
159};
160
161static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] =
162{
163 [0 ... (DVR_WRAPPER_MAX - 1)] =
164 {
165 .lock = PTHREAD_MUTEX_INITIALIZER,
166 .type = W_PLAYBACK,
167 }
168};
169
170/* events lists */
171static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list);
172static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list);
173
174static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
175static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
176
177static DVR_WrapperThreadCtx_t wrapper_thread[2] =
178{
179 [0] =
180 {
181 .lock = PTHREAD_MUTEX_INITIALIZER,
182 .running = 0,
183 .name = "record",
184 .type = W_REC,
185 },
186 [1] =
187 {
188 .lock = PTHREAD_MUTEX_INITIALIZER,
189 .running = 0,
190 .name = "playback",
191 .type = W_PLAYBACK,
192 },
193};
194
195/*now only support one timeshift now*/
196static unsigned long sn_timeshift_record;
197static unsigned long sn_timeshift_playback;
198
199static void *wrapper_task(void *arg);
200static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx);
201
202static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata);
203static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata);
204
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800205static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status);
206static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800207
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800208static int get_timespec_timeout(int timeout, struct timespec *ts)
209{
210 struct timespec ots;
211 int left, diff;
212
213 clock_gettime(CLOCK_MONOTONIC, &ots);
214
215 ts->tv_sec = ots.tv_sec + timeout / 1000;
216 ts->tv_nsec = ots.tv_nsec;
217
218 left = timeout % 1000;
219 left *= 1000000;
220 diff = 1000000000 - ots.tv_nsec;
221
222 if (diff <= left) {
223 ts->tv_sec++;
224 ts->tv_nsec = left-diff;
225 } else {
226 ts->tv_nsec += left;
227 }
228
229 return 0;
230}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800231
232static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock)
233{
234 DVR_WrapperEventCtx_t *pevt;
235
236 pthread_mutex_lock(list_lock);
237 if (list_empty(list))
238 pevt = NULL;
239 else {
240 pevt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
241 list_del(&pevt->head);
242 }
243 pthread_mutex_unlock(list_lock);
244
245 return pevt;
246}
247
248static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent()
249{
250 return ctx_getEvent(&record_evt_list, &record_evt_list_lock);
251}
252
253static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent()
254{
255 return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock);
256}
257
258static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt)
259{
260 DVR_WrapperEventCtx_t *padd;
261 padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t));
262 DVR_RETURN_IF_FALSE(padd);
263
264 *padd = *evt;
265 pthread_mutex_lock(lock);
266 list_add_tail(&padd->head, list);
267 pthread_mutex_unlock(lock);
268 return DVR_SUCCESS;
269}
270
271static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt)
272{
273 free(evt);
274}
275
276/*useless*/
277static void ctx_cleanOutdatedEvents(struct list_head *evt_list,
278 pthread_mutex_t *evt_list_lock,
279 DVR_WrapperCtx_t *list)
280{
281 DVR_WrapperEventCtx_t *pevt, *pevt_tmp;
282 unsigned long sns[DVR_WRAPPER_MAX];
283 int cnt = 0;
284 int i;
285 int found = 0;
286
287 /*copy all valid sns*/
288 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
289 sns[cnt] = list[i].sn;
290 if (!sns[cnt])
291 cnt++;
292 }
293
294 /*free evts that not belong to any valid sns*/
295 pthread_mutex_lock(evt_list_lock);
296 list_for_each_entry_safe(pevt, pevt_tmp, evt_list, head) {
297 for (i = 0; i < cnt; i++) {
298 if (pevt->sn == sns[i]) {
299 found = 1;
300 break;
301 }
302 }
303 if (!found) {
304 list_del(&pevt->head);
305 ctx_freeEvent(pevt);
306 }
307 }
308 pthread_mutex_unlock(evt_list_lock);
309}
310
311static inline void ctx_cleanOutdatedRecordEvents()
312{
313 ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list);
314}
315
316static inline void ctx_cleanOutdatedPlaybackEvents()
317{
318 ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list);
319}
320
hualing chenb9b358a2021-08-17 15:06:36 +0800321//check this play is recording file
322//return 0 if not the recording
323//else return record id
324static inline int ctx_isPlay_recording(char *play_location)
325{
326 int i;
327 DVR_WrapperCtx_t *cnt;
328
329 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
330 cnt = &record_list[i];
331 //DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]R:[%s]P:[%s] ...\n", i, cnt->sn, cnt->record.param_open.location, play_location);
332 if (!strcmp(cnt->record.param_open.location, play_location)) {
333 DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]R:[%s]P:[%s] .found..\n", i, cnt->sn, cnt->record.param_open.location, play_location);
334 return cnt->sn;
335 }
336 }
337 DVR_WRAPPER_DEBUG(1, " not found play is recing [%d]", DVR_WRAPPER_MAX);
338 return 0;
339}
340//check this record is playing file
341//return 0 if not the playing
342//else return playback id
343static inline int ctx_isRecord_playing(char *rec_location)
344{
345 int i;
346 DVR_WrapperCtx_t *cnt;
347 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
348 cnt = &playback_list[i];
349 //DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]P[%s]R[%s] ...\n", i, cnt->sn, cnt->playback.param_open.location, rec_location);
350 if (!strcmp(cnt->playback.param_open.location, rec_location)) {
351 DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]P[%s]R[%s] ..found.\n",i, cnt->sn, cnt->playback.param_open.location, rec_location);
352 return cnt->sn;
353 }
354 }
355 DVR_WRAPPER_DEBUG(1, " not found rec is playing [%d]", DVR_WRAPPER_MAX);
356 return 0;
357}
358
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800359static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list)
360{
361 int i;
362 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
363 if (list[i].sn == sn)
364 return &list[i];
365 }
366 return NULL;
367}
368
369static inline void ctx_reset(DVR_WrapperCtx_t *ctx)
370{
371 memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn),
372 0,
373 sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn));
374}
375
376static inline int ctx_valid(DVR_WrapperCtx_t *ctx)
377{
378 return (ctx->sn != 0);
379}
380
381static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn)
382{
383 return ctx_get(sn, record_list);
384}
385
386static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn)
387{
388 return ctx_get(sn, playback_list);
389}
390
391static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *))
392{
393 pthread_mutex_lock(&ctx->lock);
394 if (ctx->running == 0) {
395 pthread_condattr_t attr;
396 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
397 pthread_cond_init(&ctx->cond, &attr);
398 pthread_condattr_destroy(&attr);
399 DVR_WRAPPER_DEBUG(1, "start wrapper thread(%s) ...\n", ctx->name);
400 pthread_create(&ctx->thread, NULL, thread_fn, ctx);
401 DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) started\n", ctx->name);
402 }
403 ctx->running++;
404 pthread_mutex_unlock(&ctx->lock);
405 return 0;
406}
407
408static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx)
409{
410 pthread_mutex_lock(&ctx->lock);
411 ctx->running--;
412 if (!ctx->running) {
413 pthread_cond_broadcast(&ctx->cond);
414 pthread_mutex_unlock(&ctx->lock);
415
416 DVR_WRAPPER_DEBUG(1, "stop wrapper thread(%s) ...\n", ctx->name);
417 pthread_join(ctx->thread, NULL);
418 DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) stopped\n", ctx->name);
419
420 pthread_mutex_lock(&ctx->lock);
421 if (!ctx->running) /*protect*/
422 pthread_cond_destroy(&ctx->cond);
423 }
424 pthread_mutex_unlock(&ctx->lock);
425 return 0;
426}
427
428#define WRAPPER_THREAD_RECORD (&wrapper_thread[0])
429#define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1])
430
431static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx)
432{
433 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
434 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
435 return wrapper_requestThread(thread_ctx, wrapper_task);
436}
437
438static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx)
439{
440 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
441 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
442 return wrapper_releaseThread(thread_ctx);
443}
444
445static inline int wrapper_releaseThreadForType(int type)
446{
447 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)?
448 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
449 return wrapper_releaseThread(thread_ctx);
450}
451
452static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx)
453{
454 pthread_cond_signal(&thread_ctx->cond);
455}
456
457static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx)
458{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800459 struct timespec rt;
460 get_timespec_timeout(200, &rt);
461 pthread_cond_timedwait(&thread_ctx->cond, &thread_ctx->lock, &rt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800462 return 0;
463}
464
465static inline void wrapper_threadSignalForType(int type)
466{
467 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ?
468 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
469 wrapper_threadSignal(thread_ctx);
470}
471
472static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx)
473{
474 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
475 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
476 wrapper_threadSignal(thread_ctx);
477}
478
479static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx)
480{
481 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
482 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
483 wrapper_threadWait(thread_ctx);
484 return 0;
485}
486
487static void get_timeout_real(int timeout, struct timespec *ts)
488{
489 struct timespec ots;
490 int left, diff;
491
492 clock_gettime(CLOCK_REALTIME, &ots);
493
494 ts->tv_sec = ots.tv_sec + timeout/1000;
495 ts->tv_nsec = ots.tv_nsec;
496
497 left = timeout % 1000;
498 left *= 1000000;
499 diff = 1000000000-ots.tv_nsec;
500
501 if (diff <= left) {
502 ts->tv_sec++;
503 ts->tv_nsec = left-diff;
504 } else {
505 ts->tv_nsec += left;
506 }
507}
508
509/*return condition, locked if condition == true*/
510static int wrapper_mutex_lock_if(pthread_mutex_t *lock, int *condition)
511{
512 int r2;
513 do {
514 struct timespec rt2;
515 /*android use real time for mutex timedlock*/
516 get_timeout_real(10, &rt2);
517 r2 = pthread_mutex_timedlock(lock, &rt2);
518 } while (*condition && (r2 == ETIMEDOUT));
519
520 if (!(*condition) && (r2 == 0))
521 pthread_mutex_unlock(lock);
522
523 return *condition;
524}
525
526static void *wrapper_task(void *arg)
527{
528 DVR_WrapperThreadCtx_t *tctx = (DVR_WrapperThreadCtx_t *)arg;
529 DVR_WrapperEventCtx_t *evt;
530
531 pthread_mutex_lock(&tctx->lock);
532
533 while (tctx->running) {
534 {
535 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800536
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800537 evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
538 if (!evt)
539 ret = wrapper_threadWait(tctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800540 }
541
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800542 while (evt) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800543 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
544 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chenbc0aec92021-03-18 14:52:40 +0800545 if (ctx == NULL) {
546 DVR_WRAPPER_DEBUG(1, "warp not get ctx.free event..\n");
547 goto processed;
548 }
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800549 DVR_WRAPPER_DEBUG(1, "start name(%s) sn(%d) running(%d) type(%d)\n", tctx->name, (int)ctx->sn, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800550 if (tctx->running) {
551 /*
552 continue not break,
553 make all events consumed, or mem leak
554 */
555 if (!wrapper_mutex_lock_if(&ctx->lock, &tctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800556 goto processed;
557
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800558 if (ctx_valid(ctx)) {
559 /*double check after lock*/
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800560 if (evt->sn == ctx->sn) {
561 pthread_mutex_unlock(&tctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800562 process_handleEvents(evt, ctx);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800563 pthread_mutex_lock(&tctx->lock);
564 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800565 }
566 pthread_mutex_unlock(&ctx->lock);
567 }
568
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800569processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800570 ctx_freeEvent(evt);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800571
572 evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800573 }
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800574 DVR_WRAPPER_DEBUG(1, "start name(%s) running(%d) type(%d) con...\n", tctx->name, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800575 }
576
577 pthread_mutex_unlock(&tctx->lock);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800578 DVR_WRAPPER_DEBUG(1, "end name(%s) running(%d) type(%d) end...\n", tctx->name, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800579 return NULL;
580}
581
582static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
583{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800584 pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800585 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
586 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800587 pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800588 return 0;
589}
590
591static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
592{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800593 pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800594 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
595 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800596 pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800597 return 0;
598}
599
600static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
601{
602 DVR_WrapperPlaybackSegmentInfo_t *pseg, *pseg_tmp;
603 list_for_each_entry_safe(pseg, pseg_tmp, &ctx->segments, head) {
604 list_del(&pseg->head);
605 free(pseg);
606 }
607}
608
609static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *pseg,
610 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
611{
612 (void)ctx;
613 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
614 pseg->seg_info = *seg_info;
615 else if (update_flags & U_PIDS) {
616 pseg->seg_info.nb_pids = seg_info->nb_pids;
617 memcpy(pseg->seg_info.pids, seg_info->pids, sizeof(pseg->seg_info.pids));
618 } else if (update_flags & U_STAT) {
619 pseg->seg_info.duration = seg_info->duration;
620 pseg->seg_info.size = seg_info->size;
621 pseg->seg_info.nb_packets = seg_info->nb_packets;
622 }
hualing chen03fd4942021-07-15 15:56:41 +0800623 //update current segment duration on timeshift mode
hualing chenb9b358a2021-08-17 15:06:36 +0800624 if (ctx->playback.param_open.is_timeshift
625 || ctx_isPlay_recording(ctx->playback.param_open.location))
hualing chen03fd4942021-07-15 15:56:41 +0800626 dvr_playback_update_duration(ctx->playback.player,pseg->seg_info.id,pseg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800627 /*no changes
628 DVR_PlaybackSegmentFlag_t flags;
629 pseg->playback_info.segment_id = pseg->seg_info.id;
630 strncpy(pseg->playback_info.location,
631 ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
632 pseg->playback_info.pids = ctx->playback.pids_req;
633 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
634 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
635 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
636 pseg->playback_info.flags = flags;
637 */
638}
639
640static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
641{
642 DVR_WrapperPlaybackSegmentInfo_t *pseg;
643
644 DVR_WRAPPER_DEBUG(1, "timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
645 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
646
647 if (list_empty(&ctx->segments)) {
648 DVR_WRAPPER_DEBUG(1, "timeshift, update while no segment exists, ignore\n");
649 return DVR_SUCCESS;
650 }
651
652 /*normally, the last segment added will be updated*/
653 pseg =
654 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
655 if (pseg->seg_info.id == seg_info->id) {
656 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
657 } else {
658 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
659 if (pseg->seg_info.id == seg_info->id) {
660 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
661 break;
662 }
663 }
664 }
665
666 /*need to notify the dvr_playback*/
hualing chenb9b358a2021-08-17 15:06:36 +0800667 if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/
668 || ctx_isPlay_recording(ctx->playback.param_open.location))
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800669 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
670 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
671 if (
672 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
673 (ctx->playback.seg_status.segment_id == seg_info->id
674 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
675 ||
676 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
677 (ctx->playback.seg_status.segment_id != seg_info->id
678 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
679 )
680 {
681 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800682 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800683 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800684 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800685
686 error = dvr_playback_resume(ctx->playback.player);
687 DVR_WRAPPER_DEBUG(1, "timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n",
688 ctx->sn, error,
689 seg_info->id, seg_info->duration,
690 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
691 }
692 }
693
694 return DVR_SUCCESS;
695}
696
697static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *pseg,
698 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
699{
700 (void)ctx;
701 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
702 pseg->info = *seg_info;
703 else if (update_flags & U_PIDS) {
704 pseg->info.nb_pids = seg_info->nb_pids;
705 memcpy(pseg->info.pids, seg_info->pids, sizeof(pseg->info.pids));
706 } else if (update_flags & U_STAT) {
707 pseg->info.duration = seg_info->duration;
708 pseg->info.size = seg_info->size;
709 pseg->info.nb_packets = seg_info->nb_packets;
710 }
711}
712
713static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
714{
hualing chen266b9502020-04-04 17:39:39 +0800715 DVR_WrapperRecordSegmentInfo_t *pseg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800716
717 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800718 if (!list_empty(&ctx->segments)) {
719 pseg =
720 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
721 if (pseg->info.id == seg_info->id) {
722 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
723 } else {
724 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
725 if (pseg->info.id == seg_info->id) {
726 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
727 break;
728 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800729 }
730 }
731 }
732
733 /*timeshift, update the segment for playback*/
734 /*
735 the playback should grab the segment info other than the id,
736 and the id will be updated by each segment-add during the recording
737 */
738 /*
739 the playback paused if no data been checked from recording,
740 should resume the player later when there's more data
741 */
hualing chenb9b358a2021-08-17 15:06:36 +0800742 int sn = 0;
743 if (ctx->record.param_open.is_timeshift ||
744 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
745 DVR_WrapperCtx_t *ctx_playback;
746 if (ctx->record.param_open.is_timeshift)
747 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
748 else
749 ctx_playback = ctx_getPlayback(sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800750
751 if (ctx_playback) {
752 pthread_mutex_lock(&ctx_playback->lock);
753 if (ctx_valid(ctx_playback)
hualing chenb9b358a2021-08-17 15:06:36 +0800754 && (ctx_playback->sn == sn_timeshift_playback ||
755 ctx_playback->sn == sn)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800756 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
757 }
758 pthread_mutex_unlock(&ctx_playback->lock);
759 }
760 }
761
762 return DVR_SUCCESS;
763}
764
765static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
766 DVR_RecordSegmentInfo_t *seg_info,
767 DVR_PlaybackPids_t *p_pids,
768 DVR_PlaybackSegmentFlag_t flags)
769{
770 DVR_WrapperPlaybackSegmentInfo_t *pseg;
771 int error;
772
773 error = 0;
774 pseg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
775 if (!pseg) {
776 error = DVR_FAILURE;
777 DVR_WRAPPER_DEBUG(1, "memory fail\n");
778 return error;
779 }
780
781 /*copy the orignal segment info*/
782 pseg->seg_info = *seg_info;
783 /*generate the segment info used in playback*/
784 pseg->playback_info.segment_id = pseg->seg_info.id;
785 strncpy(pseg->playback_info.location, ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
786 pseg->playback_info.pids = *p_pids;
787 pseg->playback_info.flags = flags;
788 list_add(&pseg->head, &ctx->segments);
hualing chen03fd4942021-07-15 15:56:41 +0800789 pseg->playback_info.duration = pseg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800790
791 error = dvr_playback_add_segment(ctx->playback.player, &pseg->playback_info);
792 if (error) {
793 DVR_WRAPPER_DEBUG(1, "fail to add segment %lld (%d)\n", pseg->playback_info.segment_id, error);
794 } else {
795 ctx->playback.status.info_full.time += pseg->seg_info.duration;
796 ctx->playback.status.info_full.size += pseg->seg_info.size;
797 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
798 }
799
800 return error;
801}
802
803static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
804{
805 DVR_WrapperRecordSegmentInfo_t *pseg;
806 int error;
hualing chenab0d1262021-09-26 15:22:50 +0800807 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800808
809 error = 0;
810 pseg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
811 if (!pseg) {
812 error = DVR_FAILURE;
813 DVR_WRAPPER_DEBUG(1, "memory fail\n");
814 }
815 pseg->info = *seg_info;
816 list_add(&pseg->head, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800817
hualing chenb9b358a2021-08-17 15:06:36 +0800818 if (ctx->record.param_open.is_timeshift ||
819 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
820
821 DVR_WrapperCtx_t *ctx_playback;
822 if (ctx->record.param_open.is_timeshift)
823 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
824 else
825 ctx_playback = ctx_getPlayback(sn);
826
827 DVR_WRAPPER_DEBUG(1, "ctx_playback ---- add segment\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800828
829 if (ctx_playback) {
830 pthread_mutex_lock(&ctx_playback->lock);
831 if (ctx_valid(ctx_playback)) {
832 DVR_PlaybackSegmentFlag_t flags;
833
834 /*only if playback has started, the previous segments have been loaded*/
835 if (!list_empty(&ctx_playback->segments)) {
836 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800837 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800838 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
839 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
840 }
hualing chenb9b358a2021-08-17 15:06:36 +0800841 } else {
842 DVR_WRAPPER_DEBUG(1, "ctx_playback ---- not valid\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800843 }
844 pthread_mutex_unlock(&ctx_playback->lock);
845 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800846 } else {
hualing chenb9b358a2021-08-17 15:06:36 +0800847 DVR_WRAPPER_DEBUG(1, "ctx_playback -sn[%d]-\n", sn);
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800848 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, LSEG_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800849 }
850
851 return error;
852}
853
854static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
855{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800856 int error = -1;
857 DVR_WrapperPlaybackSegmentInfo_t *pseg = NULL, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800858
859 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
860
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800861 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800862 if (pseg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800863
864 if (ctx->current_segment_id == seg_info->id) {
865 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
866
867 /*drive the player out of this will-be-deleted segment*/
868 next_seg = list_prev_entry(pseg, head);
869
870 if (ctx->playback.speed != 100.0f) {
871 error = dvr_playback_resume(ctx->playback.player);
872 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
873 }
874
875 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, 0);
876 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), seek(seg:%llu 0) from new start (%d)\n", ctx->sn, next_seg->seg_info.id, error);
877
878 if (ctx->playback.speed == 0.0f) {
879 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
880 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last paused from new start (%d)\n", ctx->sn, error);
881 } else if (ctx->playback.speed != 100.0f) {
882 DVR_PlaybackSpeed_t dvr_speed = {
883 .speed = { ctx->playback.speed },
884 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
885 };
886 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
887 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last speed(x%f) from new start (%d)\n", ctx->sn,ctx->playback.speed, error);
888 }
889 }
890
891 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
892 if (error) {
893 /*remove playack segment fail*/
894 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), failed to remove segment(%llu) (%d)\n", ctx->sn, seg_info->id, error);
895 }
896
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800897 list_del(&pseg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800898
899 /*record the obsolete*/
900 ctx->playback.obsolete.time += pseg->seg_info.duration;
901 ctx->playback.obsolete.size += pseg->seg_info.size;
902 ctx->playback.obsolete.pkts += pseg->seg_info.nb_packets;
hualing chen03fd4942021-07-15 15:56:41 +0800903 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800904 free(pseg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800905 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800906 }
907 }
908
909 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->id, error);
910
911 return error;
912}
913
914static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
915{
916 int error;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800917 DVR_WrapperRecordSegmentInfo_t *pseg, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800918
919 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->info.id);
920
921 /*if timeshifting, notify the playback first, then deal with record*/
922 if (ctx->record.param_open.is_timeshift) {
923 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
924
925 if (ctx_playback) {
926 pthread_mutex_lock(&ctx_playback->lock);
927 if (ctx_valid(ctx_playback)
928 && ctx_playback->sn == sn_timeshift_playback
929 && !list_empty(&ctx_playback->segments)) {
930 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
931 }
932 pthread_mutex_unlock(&ctx_playback->lock);
933 }
934 }
935
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800936 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
937 if (pseg->info.id == seg_info->info.id) {
938 list_del(&pseg->head);
939
940 /*record the obsolete*/
941 ctx->record.obsolete.time += pseg->info.duration;
942 ctx->record.obsolete.size += pseg->info.size;
943 ctx->record.obsolete.pkts += pseg->info.nb_packets;
944
945 free(pseg);
946 break;
947 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800948 }
949
950 error = dvr_segment_delete(ctx->record.param_open.location, seg_info->info.id);
951
952 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->info.id, error);
953
954 return error;
955}
956
957int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
958{
959 int error;
960 DVR_WrapperCtx_t *ctx;
961 DVR_RecordOpenParams_t open_param;
962
963 DVR_RETURN_IF_FALSE(rec);
964 DVR_RETURN_IF_FALSE(params);
965
966 /*get a free ctx*/
967 ctx = ctx_getRecord(0);
968 DVR_RETURN_IF_FALSE(ctx);
969
970 pthread_mutex_lock(&ctx->lock);
971
hualing chen51652f02020-12-29 16:59:31 +0800972 DVR_WRAPPER_DEBUG(1, "open record(dmx:%d) .istf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
973 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800974
975 ctx_reset(ctx);
976
977 ctx->record.param_open = *params;
978 ctx->record.event_fn = params->event_fn;
979 ctx->record.event_userdata = params->event_userdata;
980 ctx->record.next_segment_id = 0;
981 ctx->current_segment_id = 0;
982 INIT_LIST_HEAD(&ctx->segments);
983 ctx->sn = get_sn();
984
985 wrapper_requestThreadFor(ctx);
986
hualing chen266b9502020-04-04 17:39:39 +0800987 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +0800988 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800989 open_param.dmx_dev_id = params->dmx_dev_id;
990 open_param.data_from_memory = 0;
991 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +0800992 if (params->flush_size) {
993 open_param.notification_size = params->flush_size;
994 } else {
995 open_param.notification_size = 64*1024;
996 }
Zhiqiang Han31505452020-05-06 15:08:10 +0800997 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +0800998 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800999 open_param.event_fn = wrapper_record_event_handler;
1000 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001001 if (params->keylen) {
1002 open_param.clearkey = params->clearkey;
1003 open_param.cleariv = params->cleariv;
1004 open_param.keylen = params->keylen;
1005 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001006
1007 error = dvr_record_open(&ctx->record.recorder, &open_param);
1008 if (error) {
1009 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
1010 ctx_reset(ctx);
1011 pthread_mutex_unlock(&ctx->lock);
1012 wrapper_releaseThreadForType(ctx->type);
1013 return DVR_FAILURE;
1014 }
1015 if (params->is_timeshift)
1016 sn_timeshift_record = ctx->sn;
1017
1018 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
1019
Yahui Han1fbf3292021-11-08 18:17:19 +08001020 if (params->crypto_fn) {
1021 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1022 if (error) {
1023 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) set encrypt callback fail(error:%d).\n", params->dmx_dev_id, error);
1024 }
hualing chen266b9502020-04-04 17:39:39 +08001025 }
1026
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001027 pthread_mutex_unlock(&ctx->lock);
1028
1029 *rec = (DVR_WrapperRecord_t)ctx->sn;
1030 return DVR_SUCCESS;
1031}
1032
1033int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1034{
1035 DVR_WrapperCtx_t *ctx;
1036 DVR_RecordSegmentInfo_t seg_info;
1037 int error;
1038
1039 DVR_RETURN_IF_FALSE(rec);
1040
1041 ctx = ctx_getRecord((unsigned long)rec);
1042 DVR_RETURN_IF_FALSE(ctx);
1043
1044 pthread_mutex_lock(&ctx->lock);
1045 DVR_WRAPPER_DEBUG(1, "close record(sn:%ld)\n", ctx->sn);
1046 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1047
1048 memset(&seg_info, 0, sizeof(seg_info));
1049 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1050
1051 error = dvr_record_close(ctx->record.recorder);
1052
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001053 if (ctx->record.param_open.is_timeshift)
1054 sn_timeshift_record = 0;
1055
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001056 ctx_freeSegments(ctx);
1057
1058 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) closed = (%d).\n", ctx->sn, error);
1059 ctx_reset(ctx);
1060 pthread_mutex_unlock(&ctx->lock);
1061
1062 wrapper_releaseThreadForType(ctx->type);
1063
1064 return error;
1065}
1066
1067int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1068{
1069 DVR_WrapperCtx_t *ctx;
1070 DVR_RecordStartParams_t *start_param;
1071 int i;
1072 int error;
1073
1074 DVR_RETURN_IF_FALSE(rec);
1075 DVR_RETURN_IF_FALSE(params);
1076
1077 ctx = ctx_getRecord((unsigned long)rec);
1078 DVR_RETURN_IF_FALSE(ctx);
1079
1080 pthread_mutex_lock(&ctx->lock);
hualing chena5f03222021-12-02 11:22:35 +08001081 DVR_WRAPPER_DEBUG(1, "start record(sn:%ld, location:%s) save(%d)...\n", ctx->sn, ctx->record.param_open.location, params->save_rec_file);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001082 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1083
1084 start_param = &ctx->record.param_start;
1085 memset(start_param, 0, sizeof(*start_param));
1086 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1087 start_param->segment.segment_id = ctx->record.next_segment_id++;
1088 start_param->segment.nb_pids = params->pids_info.nb_pids;
1089 for (i = 0; i < params->pids_info.nb_pids; i++) {
1090 start_param->segment.pids[i] = params->pids_info.pids[i];
1091 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1092 }
hualing chena5f03222021-12-02 11:22:35 +08001093 if (params->save_rec_file == 0)//default is not save
1094 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001095 {
1096 /*sync to update for further use*/
1097 DVR_RecordStartParams_t *update_param;
1098 update_param = &ctx->record.param_update;
1099 memcpy(update_param, start_param, sizeof(*update_param));
1100 for (i = 0; i < update_param->segment.nb_pids; i++)
1101 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1102 }
1103
1104 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1105 {
1106 DVR_RecordSegmentInfo_t new_seg_info =
1107 { .id = start_param->segment.segment_id, };
1108 wrapper_addRecordSegment(ctx, &new_seg_info);
1109 }
1110
1111 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) started = (%d)\n", ctx->sn, error);
1112
1113 pthread_mutex_unlock(&ctx->lock);
1114
1115 return error;
1116}
1117
1118int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1119{
1120 DVR_WrapperCtx_t *ctx;
1121 DVR_RecordSegmentInfo_t seg_info;
1122 int error;
1123
1124 DVR_RETURN_IF_FALSE(rec);
1125
1126 ctx = ctx_getRecord((unsigned long)rec);
1127 DVR_RETURN_IF_FALSE(ctx);
1128
1129 pthread_mutex_lock(&ctx->lock);
1130 DVR_WRAPPER_DEBUG(1, "stop record(sn:%ld) ...\n", ctx->sn);
1131 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1132
1133 memset(&seg_info, 0, sizeof(seg_info));
1134 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1135 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1136
1137 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
1138 pthread_mutex_unlock(&ctx->lock);
1139
1140 return error;
1141}
1142
hualing chen03fd4942021-07-15 15:56:41 +08001143int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1144{
1145 DVR_WrapperCtx_t *ctx;
1146 int error;
1147
1148 DVR_RETURN_IF_FALSE(rec);
1149
1150 ctx = ctx_getRecord((unsigned long)rec);
1151 DVR_RETURN_IF_FALSE(ctx);
1152
1153 pthread_mutex_lock(&ctx->lock);
1154 DVR_WRAPPER_DEBUG(1, "pause record(sn:%ld) ...\n", ctx->sn);
1155 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1156
1157 error = dvr_record_pause(ctx->record.recorder);
1158
1159 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
1160 pthread_mutex_unlock(&ctx->lock);
1161
1162 return error;
1163}
1164
1165int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1166{
1167 DVR_WrapperCtx_t *ctx;
1168 int error;
1169
1170 DVR_RETURN_IF_FALSE(rec);
1171
1172 ctx = ctx_getRecord((unsigned long)rec);
1173 DVR_RETURN_IF_FALSE(ctx);
1174
1175 pthread_mutex_lock(&ctx->lock);
1176 DVR_WRAPPER_DEBUG(1, "resume record(sn:%ld) ...\n", ctx->sn);
1177 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1178
1179 error = dvr_record_resume(ctx->record.recorder);
1180
1181 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
1182 pthread_mutex_unlock(&ctx->lock);
1183
1184 return error;
1185}
1186
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001187int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1188{
1189 DVR_WrapperCtx_t *ctx;
1190 DVR_RecordStartParams_t *start_param;
1191 DVR_RecordSegmentInfo_t seg_info;;
1192 int i;
1193 int error;
1194
1195 DVR_RETURN_IF_FALSE(rec);
1196 DVR_RETURN_IF_FALSE(params);
1197
1198 ctx = ctx_getRecord((unsigned long)rec);
1199 DVR_RETURN_IF_FALSE(ctx);
1200
1201 pthread_mutex_lock(&ctx->lock);
1202 DVR_WRAPPER_DEBUG(1, "update record(sn:%ld)\n", ctx->sn);
1203 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1204
1205 start_param = &ctx->record.param_update;
1206 memset(start_param, 0, sizeof(*start_param));
1207 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1208 start_param->segment.segment_id = ctx->record.next_segment_id++;
1209 start_param->segment.nb_pids = params->nb_pids;
1210 for (i = 0; i < params->nb_pids; i++) {
1211 start_param->segment.pids[i] = params->pids[i];
1212 start_param->segment.pid_action[i] = params->pid_action[i];
1213 }
1214 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1215 {
1216 DVR_RecordSegmentInfo_t new_seg_info =
1217 { .id = start_param->segment.segment_id, };
1218 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1219 wrapper_addRecordSegment(ctx, &new_seg_info);
1220 }
1221
1222 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) updated = (%d)\n", ctx->sn, error);
1223 pthread_mutex_unlock(&ctx->lock);
1224
1225 return error;
1226}
1227
1228int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1229{
1230 DVR_WrapperCtx_t *ctx;
1231 DVR_WrapperRecordStatus_t s;
1232 int error;
1233
1234 DVR_RETURN_IF_FALSE(rec);
1235 DVR_RETURN_IF_FALSE(status);
1236
1237 ctx = ctx_getRecord((unsigned long)rec);
1238 DVR_RETURN_IF_FALSE(ctx);
1239
1240 pthread_mutex_lock(&ctx->lock);
1241
1242 DVR_WRAPPER_DEBUG(1, "get record(sn:%ld) status ...\n", ctx->sn);
1243 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1244
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001245 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001246
1247 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
1248 ctx->sn,
1249 s.state,
1250 s.info.time,
1251 s.info.size,
1252 s.info.pkts,
1253 error);
1254
1255 *status = s;
1256
1257 pthread_mutex_unlock(&ctx->lock);
1258
1259 return error;
1260}
1261
hualing chen4fe3bee2020-10-23 13:58:52 +08001262int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1263{
1264 DVR_WrapperCtx_t *ctx;
1265 int error;
1266
1267 DVR_RETURN_IF_FALSE(rec);
1268
1269 ctx = ctx_getRecord((unsigned long)rec);
1270 DVR_RETURN_IF_FALSE(ctx);
1271
1272 pthread_mutex_lock(&ctx->lock);
1273 error = dvr_record_is_secure_mode(ctx->record.recorder);
1274 pthread_mutex_unlock(&ctx->lock);
1275 return error;
1276}
1277
hualing chen266b9502020-04-04 17:39:39 +08001278int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1279{
1280 DVR_WrapperCtx_t *ctx;
1281 int error;
1282
1283 DVR_RETURN_IF_FALSE(rec);
1284 DVR_RETURN_IF_FALSE(p_secure_buf);
1285
1286 ctx = ctx_getRecord((unsigned long)rec);
1287 DVR_RETURN_IF_FALSE(ctx);
1288
1289 pthread_mutex_lock(&ctx->lock);
1290 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
1291 pthread_mutex_unlock(&ctx->lock);
1292 return error;
1293}
1294
1295int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1296{
1297 DVR_WrapperCtx_t *ctx;
1298 int error;
1299
1300 DVR_RETURN_IF_FALSE(rec);
1301 DVR_RETURN_IF_FALSE(func);
1302
1303 ctx = ctx_getRecord((unsigned long)rec);
1304 DVR_RETURN_IF_FALSE(ctx);
1305
1306 pthread_mutex_lock(&ctx->lock);
1307 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
1308 pthread_mutex_unlock(&ctx->lock);
1309 return error;
1310}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001311
1312
1313int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1314{
1315 DVR_WrapperCtx_t *ctx;
1316 DVR_PlaybackOpenParams_t open_param;
1317 int error;
1318
1319 DVR_RETURN_IF_FALSE(playback);
1320 DVR_RETURN_IF_FALSE(params);
1321 DVR_RETURN_IF_FALSE(params->playback_handle);
1322
1323 /*get a free ctx*/
1324 ctx = ctx_getPlayback(0);
1325 DVR_RETURN_IF_FALSE(ctx);
1326
1327 pthread_mutex_lock(&ctx->lock);
1328
hualing chena5f03222021-12-02 11:22:35 +08001329 DVR_WRAPPER_DEBUG(1, "open playback(dmx:%d) ..vendor[%d]params->block_size[%d].\n", params->dmx_dev_id, params->vendor, params->block_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001330
1331 ctx_reset(ctx);
1332
1333 ctx->playback.param_open = *params;
1334 ctx->playback.event_fn = params->event_fn;
1335 ctx->playback.event_userdata = params->event_userdata;
1336 ctx->current_segment_id = 0;
1337 INIT_LIST_HEAD(&ctx->segments);
1338 ctx->sn = get_sn();
1339
1340 wrapper_requestThreadFor(ctx);
1341
hualing chen266b9502020-04-04 17:39:39 +08001342 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001343 open_param.dmx_dev_id = params->dmx_dev_id;
1344 open_param.block_size = params->block_size;
1345 open_param.is_timeshift = params->is_timeshift;
1346 //open_param.notification_size = 10*1024; //not supported
1347 open_param.event_fn = wrapper_playback_event_handler;
1348 open_param.event_userdata = (void*)ctx->sn;
1349 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001350 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001351 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001352 open_param.vendor = params->vendor;
1353
Yahui Han1fbf3292021-11-08 18:17:19 +08001354 if (params->keylen) {
1355 open_param.clearkey = params->clearkey;
1356 open_param.cleariv = params->cleariv;
1357 open_param.keylen = params->keylen;
1358 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001359
1360 error = dvr_playback_open(&ctx->playback.player, &open_param);
1361 if (error) {
1362 DVR_WRAPPER_DEBUG(1, "playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
1363 ctx_reset(ctx);
1364 pthread_mutex_unlock(&ctx->lock);
1365 wrapper_releaseThreadForType(ctx->type);
1366 return DVR_FAILURE;
1367 }
1368 if (params->is_timeshift)
1369 sn_timeshift_playback = ctx->sn;
1370
hualing chen266b9502020-04-04 17:39:39 +08001371 DVR_WRAPPER_DEBUG(1, "hanyh: playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
1372 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1373 if (error) {
1374 DVR_WRAPPER_DEBUG(1, "playback set deccrypt callback fail(error:%d).\n", error);
1375 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001376 pthread_mutex_unlock(&ctx->lock);
1377
1378 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1379 return DVR_SUCCESS;
1380}
1381
1382int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1383{
1384 DVR_WrapperCtx_t *ctx;
1385 int error;
1386
1387 DVR_RETURN_IF_FALSE(playback);
1388
1389 ctx = ctx_getPlayback((unsigned long)playback);
1390 DVR_RETURN_IF_FALSE(ctx);
1391
1392 pthread_mutex_lock(&ctx->lock);
1393 DVR_WRAPPER_DEBUG(1, "close playback(sn:%ld)\n", ctx->sn);
1394 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1395
1396 if (ctx->playback.param_open.is_timeshift)
1397 sn_timeshift_playback = 0;
1398
1399 /*try stop first*/
1400 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1401
1402 {
1403 /*remove all segments*/
1404 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1405
1406 list_for_each_entry(pseg, &ctx->segments, head) {
1407 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1408 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1409 ctx->sn, pseg->playback_info.segment_id, error);
1410 }
1411 ctx_freeSegments(ctx);
1412 }
1413
1414 error = dvr_playback_close(ctx->playback.player);
1415
1416 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) closed.\n", ctx->sn);
1417 ctx_reset(ctx);
1418 pthread_mutex_unlock(&ctx->lock);
1419
1420 wrapper_releaseThreadForType(ctx->type);
1421
1422 return error;
1423}
1424
1425int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1426{
1427 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001428 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001429 uint64_t *p_segment_ids;
1430 uint32_t segment_nb;
1431 uint32_t i;
1432 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001433 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001434 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001435 DVR_Bool_t is_timeshift = DVR_FALSE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001436
1437 DVR_RETURN_IF_FALSE(playback);
1438 DVR_RETURN_IF_FALSE(p_pids);
1439
hualing chenc110f952021-01-18 11:25:37 +08001440 ctx_record = NULL;
1441
1442 /*lock the recorder to avoid changing the recording segments*/
1443 ctx_record = ctx_getRecord(sn_timeshift_record);
1444
1445 if (ctx_record) {
1446 pthread_mutex_lock(&ctx_record->lock);
1447 if (!ctx_valid(ctx_record)
1448 || ctx_record->sn != sn_timeshift_record) {
1449 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error found\n");
1450 pthread_mutex_unlock(&ctx_record->lock);
1451 is_timeshift = DVR_FALSE;
1452 } else {
1453 is_timeshift = DVR_TRUE;
1454 }
1455 }
1456
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001457 ctx = ctx_getPlayback((unsigned long)playback);
1458 DVR_RETURN_IF_FALSE(ctx);
1459
1460 pthread_mutex_lock(&ctx->lock);
1461
1462 DVR_WRAPPER_DEBUG(1, "start playback(sn:%ld) (%s)\n\t flags(0x%x) v/a/ad/sub/pcr(%d:%d %d:%d %d:%d %d:%d %d)\n",
1463 ctx->sn,
1464 ctx->playback.param_open.location,
1465 flags,
1466 p_pids->video.pid, p_pids->video.format,
1467 p_pids->audio.pid, p_pids->audio.format,
1468 p_pids->ad.pid, p_pids->ad.format,
1469 p_pids->subtitle.pid, p_pids->subtitle.format,
1470 p_pids->pcr.pid);
1471
1472 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1473
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001474 if (ctx->playback.param_open.is_timeshift) {
1475 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001476 if (is_timeshift == DVR_FALSE) {
1477 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error return\n");
1478 pthread_mutex_unlock(&ctx->lock);
1479 return DVR_FAILURE;
1480 } else {
1481 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
1482 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001483 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001484 }
1485
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001486 /*obtain all segments in a list*/
1487 segment_nb = 0;
1488 p_segment_ids = NULL;
1489 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001490 if (!error) {
1491 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001492 struct list_head info_list; /**< segment list head*/
1493 INIT_LIST_HEAD(&info_list);
1494
hualing chenb9b358a2021-08-17 15:06:36 +08001495 DVR_WRAPPER_DEBUG(1, "get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001496 //we need free info list buf when we used end.
1497 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001498 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001499 error = DVR_FAILURE;
1500 DVR_WRAPPER_DEBUG(1, "fail to get all seg info (location:%s, seg:%llu), (error:%d)\n",
1501 ctx->playback.param_open.location, p_segment_ids[i], error);
1502 for (i = 0; i < segment_nb; i++) {
1503 DVR_RecordSegmentInfo_t seg_info;
1504 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001505
hualing chenb9a02922021-12-14 11:29:47 +08001506 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1507 if (error) {
1508 error = DVR_FAILURE;
1509 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1510 ctx->playback.param_open.location, p_segment_ids[i], error);
1511 break;
1512 }
1513 //add check if has audio or video pid. if not exist. not add segment to playback
1514 int ii = 0;
1515 int has_av = 0;
1516 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1517 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1518 if (type == DVR_STREAM_TYPE_VIDEO ||
1519 type == DVR_STREAM_TYPE_AUDIO ||
1520 type == DVR_STREAM_TYPE_AD) {
1521 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1522 DVR_WRAPPER_DEBUG(1, "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,
1523 DVR_STREAM_TYPE_VIDEO,
1524 DVR_STREAM_TYPE_AUDIO,
1525 DVR_STREAM_TYPE_AD);
1526 has_av = 1;
1527 //break;
1528 } else {
1529 DVR_WRAPPER_DEBUG(1, "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,
1530 DVR_STREAM_TYPE_VIDEO,
1531 DVR_STREAM_TYPE_AUDIO,
1532 DVR_STREAM_TYPE_AD);
1533 }
1534 }
1535 if (has_av == 0) {
1536 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1537 continue;
1538 } else {
1539 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1540 }
1541 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1542 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1543 if (error)
1544 break;
1545 /*copy the 1st segment*/
1546 if (got_1st_seg == 0) {
1547 seg_info_1st = seg_info;
1548 got_1st_seg = 1;
1549 }
1550 }
1551 } else {
1552 for (i = 0; i < segment_nb; i++) {
1553 DVR_RecordSegmentInfo_t *seg_info;
1554 DVR_PlaybackSegmentFlag_t flags;
1555 int found = 0;
1556 list_for_each_entry(seg_info, &info_list, head)
1557 {
hualing chen8aed9582021-12-24 17:59:56 +08001558 if (seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001559 found = 1;
1560 DVR_WRAPPER_DEBUG(1, "get segment info::%d", i);
1561 break;
1562 }
1563 }
1564 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001565 //last info is not found if when recording occured power off.
1566 if (p_segment_ids[i] == segment_nb - 1) {
1567 DVR_RecordSegmentInfo_t seg_info;
1568 DVR_PlaybackSegmentFlag_t flags;
1569
1570 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1571 if (error) {
1572 error = DVR_FAILURE;
1573 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1574 ctx->playback.param_open.location, p_segment_ids[i], error);
1575 break;
1576 }
1577 //
1578 //add check if has audio or video pid. if not exist. not add segment to playback
1579 int ii = 0;
1580 int has_av = 0;
1581 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1582 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1583 if (type == DVR_STREAM_TYPE_VIDEO ||
1584 type == DVR_STREAM_TYPE_AUDIO ||
1585 type == DVR_STREAM_TYPE_AD) {
1586 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1587 DVR_WRAPPER_DEBUG(1, "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,
1588 DVR_STREAM_TYPE_VIDEO,
1589 DVR_STREAM_TYPE_AUDIO,
1590 DVR_STREAM_TYPE_AD);
1591 has_av = 1;
1592 //break;
1593 } else {
1594 DVR_WRAPPER_DEBUG(1, "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,
1595 DVR_STREAM_TYPE_VIDEO,
1596 DVR_STREAM_TYPE_AUDIO,
1597 DVR_STREAM_TYPE_AD);
1598 }
1599 }
1600 if (has_av == 0) {
1601 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1602 continue;
1603 } else {
1604 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1605 }
1606 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1607 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1608 if (error)
1609 break;
1610 }
hualing chenb9a02922021-12-14 11:29:47 +08001611 continue;
1612 }
1613
1614 //add check if has audio or video pid. if not exist. not add segment to playback
1615 int ii = 0;
1616 int has_av = 0;
1617 for (ii = 0; ii < seg_info->nb_pids; ii++) {
1618 int type = (seg_info->pids[ii].type >> 24) & 0x0f;
1619 if (type == DVR_STREAM_TYPE_VIDEO ||
1620 type == DVR_STREAM_TYPE_AUDIO ||
1621 type == DVR_STREAM_TYPE_AD) {
1622 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1623 DVR_WRAPPER_DEBUG(1, "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,
1624 DVR_STREAM_TYPE_VIDEO,
1625 DVR_STREAM_TYPE_AUDIO,
1626 DVR_STREAM_TYPE_AD);
1627 has_av = 1;
1628 //break;
1629 } else {
1630 DVR_WRAPPER_DEBUG(1, "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,
1631 DVR_STREAM_TYPE_VIDEO,
1632 DVR_STREAM_TYPE_AUDIO,
1633 DVR_STREAM_TYPE_AD);
1634 }
1635 }
1636 if (has_av == 0) {
1637 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1638 continue;
1639 } else {
1640 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1641 }
1642 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1643 error = wrapper_addPlaybackSegment(ctx, seg_info, p_pids, flags);
1644 if (error)
1645 break;
1646
1647 /*copy the 1st segment*/
1648 if (got_1st_seg == 0) {
1649 seg_info_1st = *seg_info;
1650 got_1st_seg = 1;
1651 }
hualing chen92f3a142020-07-08 20:59:33 +08001652 }
hualing chenb9a02922021-12-14 11:29:47 +08001653 //free list
1654 DVR_RecordSegmentInfo_t *segment = NULL;
1655 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1656 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1657 {
1658 if (segment) {
1659 list_del(&segment->head);
1660 free(segment);
1661 }
1662 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001663 }
hualing chenb9a02922021-12-14 11:29:47 +08001664
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001665 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001666
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001667 /* return if no segment or fail to add */
1668 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001669
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001670 /*copy the obsolete infomation, must for timeshifting*/
1671 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1672 ctx->playback.obsolete = ctx_record->record.obsolete;
1673 }
1674
1675 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
1676
1677 ctx->playback.reach_end = DVR_FALSE;
1678 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1679 ctx->playback.speed = 0.0f;
1680 else
1681 ctx->playback.speed = 100.0f;
1682
1683 ctx->playback.pids_req = *p_pids;
hualing chen03fd4942021-07-15 15:56:41 +08001684 //calualte segment id and pos
1685 if (dvr_playback_check_limit(ctx->playback.player)) {
1686 pthread_mutex_unlock(&ctx->lock);
1687 dvr_wrapper_seek_playback(playback, 0);
1688 pthread_mutex_lock(&ctx->lock);
1689 error = dvr_playback_start(ctx->playback.player, flags);
1690 } else {
1691 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1692 error = dvr_playback_start(ctx->playback.player, flags);
1693 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
1694 ctx->sn, seg_info_1st.id, error);
1695 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001696 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001697 }
1698 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001699
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001700 if (ctx->playback.param_open.is_timeshift) {
1701 /*unlock the recorder locked above*/
1702 if (ctx_record && ctx_valid(ctx_record)) {
1703 pthread_mutex_unlock(&ctx_record->lock);
1704 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
1705 ctx->sn, ctx_record->sn);
1706 }
1707 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001708 pthread_mutex_unlock(&ctx->lock);
1709
1710 return error;
1711}
1712
1713int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
1714{
1715 DVR_WrapperCtx_t *ctx;
1716 int error;
1717
1718 DVR_RETURN_IF_FALSE(playback);
1719
1720 ctx = ctx_getPlayback((unsigned long)playback);
1721 DVR_RETURN_IF_FALSE(ctx);
1722
1723 pthread_mutex_lock(&ctx->lock);
1724 DVR_WRAPPER_DEBUG(1, "stop playback(sn:%ld) ...\n", ctx->sn);
1725 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1726
1727 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1728
1729 {
1730 /*remove all segments*/
1731 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1732
1733 list_for_each_entry(pseg, &ctx->segments, head) {
1734 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1735 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1736 ctx->sn, pseg->playback_info.segment_id, error);
1737 }
1738 ctx_freeSegments(ctx);
1739 }
1740
1741 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
1742 pthread_mutex_unlock(&ctx->lock);
1743
1744 return error;
1745}
1746
1747int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
1748{
1749 DVR_WrapperCtx_t *ctx;
1750 int error;
1751
1752 DVR_RETURN_IF_FALSE(playback);
1753
1754 ctx = ctx_getPlayback((unsigned long)playback);
1755 DVR_RETURN_IF_FALSE(ctx);
1756
1757 pthread_mutex_lock(&ctx->lock);
1758 DVR_WRAPPER_DEBUG(1, "pause playback(sn:%ld) ...\n", ctx->sn);
1759 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08001760 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08001761 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08001762 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001763
1764 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
1765
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001766 ctx->playback.speed = 0.0f;
1767
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001768 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) paused (%d)\n", ctx->sn, error);
1769 pthread_mutex_unlock(&ctx->lock);
1770
1771 return error;
1772}
1773
1774int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
1775{
1776 DVR_WrapperCtx_t *ctx;
1777 int error;
1778
1779 DVR_RETURN_IF_FALSE(playback);
1780
1781 ctx = ctx_getPlayback((unsigned long)playback);
1782 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08001783 //if set limit.we need check if seek to valid data when resume
1784 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
1785 if (dvr_playback_check_limit(ctx->playback.player)) {
1786 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
1787 if (expired > time_offset) {
1788 DVR_WRAPPER_DEBUG(1, "seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
1789 ctx->sn, time_offset, expired);
1790 time_offset = expired;
1791 dvr_wrapper_seek_playback(playback, time_offset);
1792 }
1793 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001794 pthread_mutex_lock(&ctx->lock);
1795 DVR_WRAPPER_DEBUG(1, "resume playback(sn:%ld) ...\n", ctx->sn);
1796 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1797
1798 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001799 ctx->playback.speed = 100.0f;
1800
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001801 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
1802 pthread_mutex_unlock(&ctx->lock);
1803
1804 return error;
1805}
1806
1807int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
1808{
1809 DVR_WrapperCtx_t *ctx;
1810 int error;
1811 DVR_PlaybackSpeed_t dvr_speed = {
1812 .speed = { speed },
1813 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
1814 };
1815
1816 DVR_RETURN_IF_FALSE(playback);
1817
1818 ctx = ctx_getPlayback((unsigned long)playback);
1819 DVR_RETURN_IF_FALSE(ctx);
1820
1821 pthread_mutex_lock(&ctx->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001822 DVR_WRAPPER_DEBUG(1, "speed playback(sn:%ld) (x%f) .(x%f)..\n", ctx->sn, speed, ctx->playback.speed);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001823 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1824
1825 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
1826
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001827 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
1828 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
1829 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
1830 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
1831 error = dvr_playback_resume(ctx->playback.player);
1832 } else if (ctx->playback.speed == 0.0f
1833 && speed != 0.0f
1834 && speed != 100.0f) {
1835 /*libdvr do not support pause with speed=0, will not be here*/
1836 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
1837 error = dvr_playback_resume(ctx->playback.player);
1838 }
1839
1840 ctx->playback.speed = speed;
1841
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001842 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) speeded(x%f) (%d)\n",
1843 ctx->sn, speed, error);
1844 pthread_mutex_unlock(&ctx->lock);
1845
1846 return error;
1847}
1848
hualing chen03fd4942021-07-15 15:56:41 +08001849int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
1850{
1851 DVR_WrapperCtx_t *ctx;
1852 int error;
1853
1854 DVR_RETURN_IF_FALSE(playback);
1855
1856 ctx = ctx_getPlayback((unsigned long)playback);
1857 DVR_RETURN_IF_FALSE(ctx);
1858
1859 pthread_mutex_lock(&ctx->lock);
1860
1861 DVR_WRAPPER_DEBUG(1, "setlimit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit);
1862 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1863
1864 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
1865 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) setlimit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
1866
1867 pthread_mutex_unlock(&ctx->lock);
1868
1869 return error;
1870}
1871
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001872int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
1873{
1874 DVR_WrapperCtx_t *ctx;
1875 int error;
1876 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1877 uint64_t segment_id;
1878 uint32_t off;
1879 uint64_t last_segment_id;
1880 uint32_t pre_off;
1881
1882 DVR_RETURN_IF_FALSE(playback);
1883
1884 ctx = ctx_getPlayback((unsigned long)playback);
1885 DVR_RETURN_IF_FALSE(ctx);
1886
1887 pthread_mutex_lock(&ctx->lock);
1888
1889 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
1890 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1891
1892 off = 0;
1893 segment_id = 0;
1894 pre_off = 0;
1895 last_segment_id = 0;
1896
hualing chen03fd4942021-07-15 15:56:41 +08001897 //if set limit info we need check ts data is
1898 //expired when seek
1899 if (dvr_playback_check_limit(ctx->playback.player)) {
1900 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
1901 if (expired > time_offset) {
1902 DVR_WRAPPER_DEBUG(1, "seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
1903 ctx->sn, time_offset, expired);
1904 time_offset = expired;
1905 }
1906 }
1907
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001908 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1909 segment_id = pseg->seg_info.id;
1910
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001911 if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset)
1912 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001913
1914 last_segment_id = pseg->seg_info.id;
1915 pre_off += pseg->seg_info.duration;
1916 }
1917
1918 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001919 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
1920 off = time_offset;
1921 } else if (ctx->playback.obsolete.time >= time_offset) {
1922 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001923 } else {
hualing chenda76fc52020-05-28 14:56:42 +08001924 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001925 }
1926
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001927 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (seg:%lld, off:%d)\n",
1928 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001929 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
1930 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
1931
1932 pthread_mutex_unlock(&ctx->lock);
1933
1934 return error;
1935}
1936
1937int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
1938{
1939 DVR_WrapperCtx_t *ctx;
1940 int error;
1941 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1942
1943 DVR_RETURN_IF_FALSE(playback);
1944 DVR_RETURN_IF_FALSE(p_pids);
1945
1946 ctx = ctx_getPlayback((unsigned long)playback);
1947 DVR_RETURN_IF_FALSE(ctx);
1948
1949 pthread_mutex_lock(&ctx->lock);
1950
1951 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
1952 ctx->sn,
1953 p_pids->video.pid, p_pids->video.format,
1954 p_pids->audio.pid, p_pids->audio.format);
1955 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1956
1957 ctx->playback.pids_req = *p_pids;
1958
1959 error = 0;
1960 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1961 /*should update the whole list of segments*/
1962 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
1963 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
1964 /*check udpate for pids*/
1965 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
1966 pseg->playback_info.pids = *p_pids;
1967 error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
1968 if (error) {
1969 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
1970 ctx->sn, pseg->seg_info.id, error);
1971 /*do not break, let list updated*/
1972 }
1973 }
1974 }
1975 /*break;*/
1976 }
1977 }
1978
1979 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
1980 ctx->sn,
1981 p_pids->video.pid, p_pids->video.format,
1982 p_pids->audio.pid, p_pids->audio.format,
1983 error);
1984
1985 pthread_mutex_unlock(&ctx->lock);
1986
1987 return error;
1988}
1989
hualing chena5f03222021-12-02 11:22:35 +08001990int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
1991{
1992 DVR_WrapperCtx_t *ctx;
1993 int error;
1994 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1995
1996 DVR_RETURN_IF_FALSE(playback);
1997 DVR_RETURN_IF_FALSE(p_pids);
1998
1999 ctx = ctx_getPlayback((unsigned long)playback);
2000 DVR_RETURN_IF_FALSE(ctx);
2001
2002 pthread_mutex_lock(&ctx->lock);
2003
2004 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
2005 ctx->sn,
2006 p_pids->video.pid, p_pids->video.format,
2007 p_pids->audio.pid, p_pids->audio.format);
2008 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2009
2010 ctx->playback.pids_req = *p_pids;
2011
2012 error = 0;
2013 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2014 /*should update the whole list of segments*/
2015 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
2016 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
2017 /*check udpate for pids*/
2018 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2019 pseg->playback_info.pids = *p_pids;
2020 error = dvr_playback_only_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
2021 if (error) {
2022 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
2023 ctx->sn, pseg->seg_info.id, error);
2024 /*do not break, let list updated*/
2025 }
2026 }
2027 }
2028 /*break;*/
2029 }
2030 }
2031
2032 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
2033 ctx->sn,
2034 p_pids->video.pid, p_pids->video.format,
2035 p_pids->audio.pid, p_pids->audio.format,
2036 error);
2037
2038 pthread_mutex_unlock(&ctx->lock);
2039
2040 return error;
2041}
2042
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002043int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2044{
2045 DVR_WrapperCtx_t *ctx;
2046 DVR_WrapperPlaybackStatus_t s;
2047 DVR_PlaybackStatus_t play_status;
2048 int error;
2049
2050 DVR_RETURN_IF_FALSE(playback);
2051 DVR_RETURN_IF_FALSE(status);
2052
2053 ctx = ctx_getPlayback((unsigned long)playback);
2054 DVR_RETURN_IF_FALSE(ctx);
2055
2056 pthread_mutex_lock(&ctx->lock);
2057
2058 DVR_WRAPPER_DEBUG(1, "get playback(sn:%ld) status ...\n", ctx->sn);
2059 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2060
2061 error = dvr_playback_get_status(ctx->playback.player, &play_status);
2062 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) get status (%d)\n", ctx->sn, error);
2063
2064 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002065 error = process_generatePlaybackStatus(ctx, &s);
2066
hualing chenb5cd42e2020-04-15 17:03:34 +08002067 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2068 //reach end need set full time to cur.so app can exist playback.
2069 DVR_WRAPPER_DEBUG(1, "set cur time to full time, reach end occur");
2070 s.info_cur.time = s.info_full.time;
2071 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002072 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002073 ctx->sn,
2074 s.state,
2075 s.info_cur.time,
2076 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002077 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002078 error);
2079
2080 *status = s;
2081
2082 pthread_mutex_unlock(&ctx->lock);
2083
2084 return error;
2085}
2086
hualing chen266b9502020-04-04 17:39:39 +08002087int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2088{
2089 DVR_WrapperCtx_t *ctx;
2090 int error;
2091
2092 DVR_RETURN_IF_FALSE(playback);
2093 DVR_RETURN_IF_FALSE(p_secure_buf);
2094
2095 ctx = ctx_getPlayback((unsigned long)playback);
2096 DVR_RETURN_IF_FALSE(ctx);
2097
2098 pthread_mutex_lock(&ctx->lock);
2099 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
2100 pthread_mutex_unlock(&ctx->lock);
2101 return error;
2102}
2103
2104int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2105{
2106 DVR_WrapperCtx_t *ctx;
2107 int error;
2108
2109 DVR_RETURN_IF_FALSE(playback);
2110 DVR_RETURN_IF_FALSE(func);
2111
2112 ctx = ctx_getPlayback((unsigned long)playback);
2113 DVR_RETURN_IF_FALSE(ctx);
2114
2115 pthread_mutex_lock(&ctx->lock);
2116 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
2117 pthread_mutex_unlock(&ctx->lock);
2118 return error;
2119}
2120
Zhiqiang Han620b9252021-11-09 14:23:20 +08002121int dvr_wrapper_segment_del_by_location (const char *location)
2122{
2123 char fpath[DVR_MAX_LOCATION_SIZE];
2124
2125 DVR_RETURN_IF_FALSE(location);
2126
2127 /*del the stats file*/
2128 sprintf(fpath, "%s.stats", location);
2129 unlink(fpath);
2130
2131 return dvr_segment_del_by_location(location);
2132}
2133
2134int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2135{
2136 FILE *fp;
2137 char fpath[DVR_MAX_LOCATION_SIZE];
2138
2139 DVR_RETURN_IF_FALSE(location);
2140 DVR_RETURN_IF_FALSE(p_info);
2141
2142 if (p_info)
2143 memset(p_info, 0, sizeof(p_info[0]));
2144
2145 memset(fpath, 0, sizeof(fpath));
2146 sprintf(fpath, "%s.stats", location);
2147
2148 /*stats file exists*/
2149 if ((fp = fopen(fpath, "r"))) {
2150 char buf[256];
2151
2152 if (fgets(buf, sizeof(buf), fp) != NULL
2153 && (sscanf(buf, ":%llu:%lu:%u",
2154 &p_info->size,
2155 &p_info->time,
2156 &p_info->pkts) == 3)) {
2157 fclose(fp);
2158 DVR_WRAPPER_DEBUG(1, "rec(%s) t/s/p:(%lu/%llu/%u)\n", location, p_info->time, p_info->size, p_info->pkts);
2159 return DVR_SUCCESS;
2160 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002161 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002162 }
2163
2164 /*fallback, slow on mass files*/
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002165 DVR_WRAPPER_DEBUG(1, "rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002166
2167 int error;
2168 uint32_t n_ids;
2169 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002170
hualing chen8aed9582021-12-24 17:59:56 +08002171 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002172
Zhiqiang Han620b9252021-11-09 14:23:20 +08002173 if (!error) {
2174 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002175 struct list_head info_list; /**< segment list head*/
2176 INIT_LIST_HEAD(&info_list);
2177
2178 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002179 error = dvr_segment_get_allInfo(location, &info_list);
2180 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08002181 DVR_RecordSegmentInfo_t info;
2182
2183 memset(&info, 0, sizeof(info));
2184 error = DVR_FAILURE;
2185 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002186 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002187
2188 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002189 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002190 if (!error) {
2191 p_info->size += info.size;
2192 p_info->time += info.duration;
2193 p_info->pkts += info.nb_packets;
2194 } else {
hualing chen8aed9582021-12-24 17:59:56 +08002195 DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002196 break;
2197 }
2198 }
2199 } else {
2200 DVR_WRAPPER_DEBUG(1, "get list segment_nb::%d",n_ids);
2201 for (i = 0; i < n_ids; i++) {
2202
2203 DVR_RecordSegmentInfo_t *seg_info;
2204 DVR_PlaybackSegmentFlag_t flags;
2205 int found = 0;
2206 list_for_each_entry(seg_info, &info_list, head)
2207 {
hualing chen8aed9582021-12-24 17:59:56 +08002208 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002209 found = 1;
2210 break;
2211 }
2212 }
2213 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08002214 DVR_WRAPPER_DEBUG(1, "get segment info::%d [%d]n_ids[%d]error", i, p_ids[i], n_ids);
2215 if (p_ids[i] == n_ids - 1) {
2216 DVR_RecordSegmentInfo_t info;
2217 DVR_WRAPPER_DEBUG(1, "get last segment info::%d [%d]n_ids[%d] from subfile", i, p_ids[i], n_ids);
2218 error = dvr_segment_get_info(location, p_ids[i], &info);
2219 if (!error) {
2220 p_info->size += info.size;
2221 p_info->time += info.duration;
2222 p_info->pkts += info.nb_packets;
2223 } else {
2224 DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
2225 break;
2226 }
2227 }
hualing chenb9a02922021-12-14 11:29:47 +08002228 continue;
2229 }
2230
2231 if (!error) {
2232 p_info->size += seg_info->size;
2233 p_info->time += seg_info->duration;
2234 p_info->pkts += seg_info->nb_packets;
2235 } else {
hualing chen8aed9582021-12-24 17:59:56 +08002236 DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002237 break;
2238 }
2239 }
2240 //free list
2241 DVR_RecordSegmentInfo_t *segment = NULL;
2242 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2243 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2244 {
2245 if (segment) {
2246 list_del(&segment->head);
2247 free(segment);
2248 }
2249 }
2250 }
2251 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002252 } else {
2253 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002254 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002255 DVR_WRAPPER_DEBUG(1, "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 +08002256
2257 return (error)? DVR_FAILURE : DVR_SUCCESS;
2258}
2259
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002260static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2261{
2262 DVR_WrapperEventCtx_t evt;
2263
2264 DVR_RETURN_IF_FALSE(userdata);
2265
2266 evt.sn = (unsigned long)userdata;
2267 evt.type = W_REC;
2268 evt.record.event = event;
2269 evt.record.status = *(DVR_RecordStatus_t *)params;
2270 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
2271 return ctx_addRecordEvent(&evt);
2272}
2273
2274static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2275{
2276 DVR_WrapperEventCtx_t evt;
2277
2278 DVR_RETURN_IF_FALSE(userdata);
2279
2280 evt.sn = (unsigned long)userdata;
2281 evt.type = W_PLAYBACK;
2282 evt.playback.event = event;
2283 evt.playback.status = *(DVR_Play_Notify_t *)params;
2284 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event);
2285 return ctx_addPlaybackEvent(&evt);
2286}
2287
2288static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2289{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002290 DVR_WRAPPER_DEBUG(1, "notify(sn:%ld) evt(0x%x) statistic:time/size/pkts(%ld/%lld/%u) obsl:(%ld/%llu/%u)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002291 ctx->sn,
2292 evt,
2293 status->info.time,
2294 status->info.size,
2295 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002296 status->info_obsolete.time,
2297 status->info_obsolete.size,
2298 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002299
2300 if (ctx->record.event_fn)
2301 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2302 return 0;
2303}
2304
Zhiqiang Han620b9252021-11-09 14:23:20 +08002305static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2306{
2307 FILE *fp;
2308 char fpath[DVR_MAX_LOCATION_SIZE];
2309
2310 DVR_RETURN_IF_FALSE(location);
2311 DVR_RETURN_IF_FALSE(p_status);
2312
2313 sprintf(fpath, "%s.stats", location);
2314
2315 /*stats file*/
2316 if ((fp = fopen(fpath, "w"))) {
2317 char buf[256];
2318 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2319 p_status->info.size - p_status->info_obsolete.size,
2320 p_status->info.time - p_status->info_obsolete.time,
2321 p_status->info.pkts - p_status->info_obsolete.pkts);
2322 fputs(buf, fp);
2323 fclose(fp);
2324 return DVR_SUCCESS;
2325 }
2326
2327 return DVR_FAILURE;
2328}
2329
2330
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002331static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2332{
2333 DVR_RecordStartParams_t param;
2334 DVR_RecordSegmentInfo_t seg_info;
2335 int i;
2336 int error;
2337
2338 memcpy(&param, &ctx->record.param_update, sizeof(param));
2339 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2340 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2341 for (i = 0; i < param.segment.nb_pids; i++) {
2342 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2343 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2344 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2345 ctx->record.param_update.segment.nb_pids++;
2346 }
2347 }
2348 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2349 {
2350 DVR_RecordSegmentInfo_t new_seg_info =
2351 { .id = ctx->record.param_update.segment.segment_id, };
2352 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2353 wrapper_addRecordSegment(ctx, &new_seg_info);
2354 }
2355
Zhiqiang Hand977e972020-05-11 11:30:47 +08002356 DVR_WRAPPER_DEBUG(1, "record next segment(%llu)=(%d)\n", ctx->record.param_update.segment.segment_id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002357 return error;
2358}
2359
2360static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg)
2361{
2362 return wrapper_removeRecordSegment(ctx, pseg);
2363}
2364
2365/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002366static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002367{
2368 /*the current seg is not covered in the statistics*/
2369 DVR_WrapperRecordSegmentInfo_t *pseg;
2370
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002371 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002372 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2373
2374 ctx->record.status.state = ctx->record.seg_status.state;
2375 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2376 memcpy(ctx->record.status.pids.pids,
2377 ctx->record.seg_status.info.pids,
2378 sizeof(ctx->record.status.pids.pids));
2379 ctx->current_segment_id = ctx->record.seg_status.info.id;
2380
2381 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2382 if (pseg->info.id != ctx->record.seg_status.info.id) {
2383 ctx->record.status.info.time += pseg->info.duration;
2384 ctx->record.status.info.size += pseg->info.size;
2385 ctx->record.status.info.pkts += pseg->info.nb_packets;
2386 }
2387 }
2388
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002389 ctx->record.status.info_obsolete = ctx->record.obsolete;
2390
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002391 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002392
2393 if (status) {
2394 *status = ctx->record.status;
2395 status->info.time += ctx->record.seg_status.info.duration;
2396 status->info.size += ctx->record.seg_status.info.size;
2397 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2398 }
2399
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002400 return DVR_SUCCESS;
2401}
2402
2403
2404static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2405{
2406 DVR_WrapperRecordStatus_t status;
2407
2408 memset(&status, 0, sizeof(status));
2409
2410 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d)\n",
2411 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002412 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
2413 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002414 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002415 return 0;
2416 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002417 switch (evt->record.event)
2418 {
2419 case DVR_RECORD_EVENT_STATUS:
2420 {
2421 switch (evt->record.status.state)
2422 {
2423 case DVR_RECORD_STATE_OPENED:
2424 case DVR_RECORD_STATE_CLOSED:
2425 {
2426 ctx->record.seg_status = evt->record.status;
2427
2428 status.state = evt->record.status.state;
2429 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002430 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002431 } break;
2432 case DVR_RECORD_STATE_STARTED:
2433 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002434 ctx->record.seg_status = evt->record.status;
2435
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002436 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002437 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002438 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002439
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002440 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002441 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002442 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
2443 DVR_WRAPPER_DEBUG(1, "start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n",
2444 ctx->sn,
2445 evt->record.status.info.size,
2446 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002447 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2448 /*should notify the recording's stop*/
2449 int error = dvr_record_close(ctx->record.recorder);
2450 DVR_WRAPPER_DEBUG(1, "stop record(%lu)=%d, failed to start new segment for recording.",
2451 ctx->sn, error);
2452 status.state = DVR_RECORD_STATE_CLOSED;
2453 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002454 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002455 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002456 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002457
2458 if (ctx->record.param_open.is_timeshift
2459 && ctx->record.param_open.max_time
2460 && status.info.time >= ctx->record.param_open.max_time) {
2461 DVR_WrapperRecordSegmentInfo_t *pseg;
2462
2463 /*as the player do not support null playlist,
2464 there must be one segment existed at any time,
2465 we have to keep two segments before remove one*/
2466 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2467 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
2468 /*only one segment, waiting for more*/
2469 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n",
2470 status.info.size,
2471 ctx->record.param_open.max_time,
2472 ctx->record.param_open.segment_size);
2473 } else {
2474 /*timeshifting, remove the 1st segment and notify the player*/
2475 record_removeSegment(ctx, pseg);
2476
2477 process_generateRecordStatus(ctx, &status);
2478 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002479 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002480 }
2481 }
2482
2483 if (ctx->record.param_open.is_timeshift
2484 && ctx->record.param_open.max_size
2485 && status.info.size >= ctx->record.param_open.max_size) {
2486 DVR_WrapperRecordSegmentInfo_t *pseg;
2487
2488 /*as the player do not support null playlist,
2489 there must be one segment existed at any time,
2490 we have to keep two segments before remove one*/
2491 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2492 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
2493 /*only one segment, waiting for more*/
2494 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of record < max size of segment(%lld)\n",
2495 status.info.size,
2496 ctx->record.param_open.segment_size);
2497 } else {
2498 record_removeSegment(ctx, pseg);
2499
2500 process_generateRecordStatus(ctx, &status);
2501 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002502 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002503 }
2504 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002505 } break;
2506 case DVR_RECORD_STATE_STOPPED:
2507 {
2508 ctx->record.seg_status = evt->record.status;
2509
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002510 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002511 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002512 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002513 } break;
2514 default:
2515 break;
2516 }
2517 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002518 case DVR_RECORD_EVENT_WRITE_ERROR: {
2519 ctx->record.seg_status = evt->record.status;
2520 status.state = evt->record.status.state;
2521 process_notifyRecord(ctx, evt->record.event, &status);
2522 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002523 default:
2524 break;
2525 }
2526 return DVR_SUCCESS;
2527}
2528
2529static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2530{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002531 DVR_WRAPPER_DEBUG(1, "notify(sn:%ld) evt(0x%x) statistics:state/cur/full/obsl(%d/%ld/%ld/%ld)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002532 ctx->sn,
2533 evt,
2534 status->state,
2535 status->info_cur.time,
2536 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002537 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002538
2539 if (ctx->playback.event_fn)
2540 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
2541 return 0;
2542}
2543
2544/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002545static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002546{
2547 /*the current seg is not covered in the statistics*/
2548 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2549
2550 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2551 ctx->playback.status.pids = ctx->playback.pids_req;
2552
2553 ctx->playback.status.state = ctx->playback.seg_status.state;
2554 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2555 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2556 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2557
2558 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2559 if (pseg->seg_info.id == ctx->playback.seg_status.segment_id)
2560 break;
2561 ctx->playback.status.info_cur.time += pseg->seg_info.duration;
2562 ctx->playback.status.info_cur.size += pseg->seg_info.size;
2563 ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets;
2564 }
2565 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2566 ctx->playback.status.info_full.time += pseg->seg_info.duration;
2567 ctx->playback.status.info_full.size += pseg->seg_info.size;
2568 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
2569 }
2570
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002571 if (status) {
2572 *status = ctx->playback.status;
2573 /*deal with current, lack size and pkts with the current*/
2574 status->info_cur.time += ctx->playback.seg_status.time_cur;
2575 status->info_obsolete.time = ctx->playback.obsolete.time;
2576 }
2577
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002578 return DVR_SUCCESS;
2579}
2580
2581static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2582{
2583 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
2584 evt->sn, evt->playback.event,
2585 evt->playback.status.play_status.state,
2586 evt->playback.status.play_status.segment_id,
2587 evt->playback.status.play_status.time_cur,
2588 evt->playback.status.play_status.time_end);
2589
2590 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002591 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2592 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2593 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2594 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002595 ctx->playback.last_event = evt->playback.event;
2596
2597 switch (evt->playback.event)
2598 {
2599 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2600 case DVR_PLAYBACK_EVENT_REACHED_END:
2601 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2602 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002603 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002604 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002605 case DVR_PLAYBACK_EVENT_NODATA:
2606 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002607 {
2608 DVR_WrapperPlaybackStatus_t status;
2609
2610 /*copy status of segment*/
2611 ctx->playback.seg_status = evt->playback.status.play_status;
2612
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002613 /*generate status of the whole playback*/
2614 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002615
2616 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Zhiqiang Han31846002021-11-04 10:49:06 +08002617 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08002618 if (ctx->playback.param_open.is_timeshift
2619 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002620 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08002621 }
2622 /*trust the low level, make NO check.
2623 As this evt is changed to only once due to some operations(paused) in low level.
2624 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002625 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08002626 }
2627 */
2628 else {
2629 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002630 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002631 }
hualing chenf291cf32020-06-18 10:50:30 +08002632 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002633 process_notifyPlayback(ctx, evt->playback.event, &status);
2634 }
2635 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002636 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2637 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2638 case DVR_PLAYBACK_EVENT_NO_KEY:
2639 {
2640 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
2641 } break;
2642 default:
2643 {
2644 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
2645 } break;
2646 }
2647 return 0;
2648}
2649
2650static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2651{
2652 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2653}
2654