blob: 5df7e46e85011ea04f63df27493effae35fb3273 [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;
hualing chen56c0a162022-01-27 17:01:50 +080088 DVR_Bool_t tf_full;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080089 } playback;
90 };
91} DVR_WrapperCtx_t;
92
93typedef struct {
94 struct list_head head;
95 unsigned long sn;
96
97 /* rec or playback */
98 int type;
99
100 union {
101 struct {
102 DVR_RecordEvent_t event;
103 DVR_RecordStatus_t status;
104 } record;
105 struct {
106 DVR_PlaybackEvent_t event;
107 DVR_Play_Notify_t status;
108 } playback;
109 };
110} DVR_WrapperEventCtx_t;
111
112typedef struct {
113 pthread_mutex_t lock;
114 char *name;
115 int running;
116 pthread_cond_t cond;
117 pthread_t thread;
118 int type;
119} DVR_WrapperThreadCtx_t;
120
121typedef struct {
122 struct list_head head;
123
124 DVR_RecordSegmentInfo_t seg_info;
125 DVR_PlaybackSegmentInfo_t playback_info;
126} DVR_WrapperPlaybackSegmentInfo_t;
127
128typedef struct {
129 struct list_head head;
130
131 DVR_RecordSegmentInfo_t info;
132} DVR_WrapperRecordSegmentInfo_t;
133
134/* serial num generater */
135static unsigned long sn = 1;
136static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER;
137
138static inline unsigned long get_sn()
139{
hualing chenab0d1262021-09-26 15:22:50 +0800140 unsigned long no = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800141
142 pthread_mutex_lock(&sn_lock);
143 no = sn++;
144 if (!no)
145 no = sn++;
146 pthread_mutex_unlock(&sn_lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800147 return no;
148}
149
150/* entity ctx */
151#define DVR_WRAPPER_MAX 10
152
153static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] =
154{
155 [0 ... (DVR_WRAPPER_MAX - 1)] =
156 {
157 .lock = PTHREAD_MUTEX_INITIALIZER,
158 .type = W_REC,
159 }
160};
161
162static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] =
163{
164 [0 ... (DVR_WRAPPER_MAX - 1)] =
165 {
166 .lock = PTHREAD_MUTEX_INITIALIZER,
167 .type = W_PLAYBACK,
168 }
169};
170
171/* events lists */
172static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list);
173static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list);
174
175static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
176static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
177
178static DVR_WrapperThreadCtx_t wrapper_thread[2] =
179{
180 [0] =
181 {
182 .lock = PTHREAD_MUTEX_INITIALIZER,
183 .running = 0,
184 .name = "record",
185 .type = W_REC,
186 },
187 [1] =
188 {
189 .lock = PTHREAD_MUTEX_INITIALIZER,
190 .running = 0,
191 .name = "playback",
192 .type = W_PLAYBACK,
193 },
194};
195
196/*now only support one timeshift now*/
197static unsigned long sn_timeshift_record;
198static unsigned long sn_timeshift_playback;
199
200static void *wrapper_task(void *arg);
201static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx);
202
203static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata);
204static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata);
205
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800206static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status);
207static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800208
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800209static int get_timespec_timeout(int timeout, struct timespec *ts)
210{
211 struct timespec ots;
212 int left, diff;
213
214 clock_gettime(CLOCK_MONOTONIC, &ots);
215
216 ts->tv_sec = ots.tv_sec + timeout / 1000;
217 ts->tv_nsec = ots.tv_nsec;
218
219 left = timeout % 1000;
220 left *= 1000000;
221 diff = 1000000000 - ots.tv_nsec;
222
223 if (diff <= left) {
224 ts->tv_sec++;
225 ts->tv_nsec = left-diff;
226 } else {
227 ts->tv_nsec += left;
228 }
229
230 return 0;
231}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800232
233static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock)
234{
235 DVR_WrapperEventCtx_t *pevt;
236
237 pthread_mutex_lock(list_lock);
238 if (list_empty(list))
239 pevt = NULL;
240 else {
241 pevt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
242 list_del(&pevt->head);
243 }
244 pthread_mutex_unlock(list_lock);
245
246 return pevt;
247}
248
249static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent()
250{
251 return ctx_getEvent(&record_evt_list, &record_evt_list_lock);
252}
253
254static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent()
255{
256 return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock);
257}
258
259static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt)
260{
261 DVR_WrapperEventCtx_t *padd;
262 padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t));
263 DVR_RETURN_IF_FALSE(padd);
264
265 *padd = *evt;
266 pthread_mutex_lock(lock);
267 list_add_tail(&padd->head, list);
268 pthread_mutex_unlock(lock);
269 return DVR_SUCCESS;
270}
271
272static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt)
273{
274 free(evt);
275}
276
277/*useless*/
278static void ctx_cleanOutdatedEvents(struct list_head *evt_list,
279 pthread_mutex_t *evt_list_lock,
280 DVR_WrapperCtx_t *list)
281{
282 DVR_WrapperEventCtx_t *pevt, *pevt_tmp;
283 unsigned long sns[DVR_WRAPPER_MAX];
284 int cnt = 0;
285 int i;
286 int found = 0;
287
288 /*copy all valid sns*/
289 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
290 sns[cnt] = list[i].sn;
291 if (!sns[cnt])
292 cnt++;
293 }
294
295 /*free evts that not belong to any valid sns*/
296 pthread_mutex_lock(evt_list_lock);
297 list_for_each_entry_safe(pevt, pevt_tmp, evt_list, head) {
298 for (i = 0; i < cnt; i++) {
299 if (pevt->sn == sns[i]) {
300 found = 1;
301 break;
302 }
303 }
304 if (!found) {
305 list_del(&pevt->head);
306 ctx_freeEvent(pevt);
307 }
308 }
309 pthread_mutex_unlock(evt_list_lock);
310}
311
312static inline void ctx_cleanOutdatedRecordEvents()
313{
314 ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list);
315}
316
317static inline void ctx_cleanOutdatedPlaybackEvents()
318{
319 ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list);
320}
321
hualing chenb9b358a2021-08-17 15:06:36 +0800322//check this play is recording file
323//return 0 if not the recording
324//else return record id
325static inline int ctx_isPlay_recording(char *play_location)
326{
327 int i;
328 DVR_WrapperCtx_t *cnt;
329
330 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
331 cnt = &record_list[i];
332 //DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]R:[%s]P:[%s] ...\n", i, cnt->sn, cnt->record.param_open.location, play_location);
333 if (!strcmp(cnt->record.param_open.location, play_location)) {
334 DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]R:[%s]P:[%s] .found..\n", i, cnt->sn, cnt->record.param_open.location, play_location);
335 return cnt->sn;
336 }
337 }
338 DVR_WRAPPER_DEBUG(1, " not found play is recing [%d]", DVR_WRAPPER_MAX);
339 return 0;
340}
341//check this record is playing file
342//return 0 if not the playing
343//else return playback id
344static inline int ctx_isRecord_playing(char *rec_location)
345{
346 int i;
347 DVR_WrapperCtx_t *cnt;
348 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
349 cnt = &playback_list[i];
350 //DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]P[%s]R[%s] ...\n", i, cnt->sn, cnt->playback.param_open.location, rec_location);
351 if (!strcmp(cnt->playback.param_open.location, rec_location)) {
352 DVR_WRAPPER_DEBUG(1, "[%d]sn[%d]P[%s]R[%s] ..found.\n",i, cnt->sn, cnt->playback.param_open.location, rec_location);
353 return cnt->sn;
354 }
355 }
356 DVR_WRAPPER_DEBUG(1, " not found rec is playing [%d]", DVR_WRAPPER_MAX);
357 return 0;
358}
359
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800360static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list)
361{
362 int i;
363 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
364 if (list[i].sn == sn)
365 return &list[i];
366 }
367 return NULL;
368}
369
370static inline void ctx_reset(DVR_WrapperCtx_t *ctx)
371{
372 memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn),
373 0,
374 sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn));
375}
376
377static inline int ctx_valid(DVR_WrapperCtx_t *ctx)
378{
379 return (ctx->sn != 0);
380}
381
382static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn)
383{
384 return ctx_get(sn, record_list);
385}
386
387static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn)
388{
389 return ctx_get(sn, playback_list);
390}
391
392static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *))
393{
394 pthread_mutex_lock(&ctx->lock);
395 if (ctx->running == 0) {
396 pthread_condattr_t attr;
397 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
398 pthread_cond_init(&ctx->cond, &attr);
399 pthread_condattr_destroy(&attr);
400 DVR_WRAPPER_DEBUG(1, "start wrapper thread(%s) ...\n", ctx->name);
401 pthread_create(&ctx->thread, NULL, thread_fn, ctx);
402 DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) started\n", ctx->name);
403 }
404 ctx->running++;
405 pthread_mutex_unlock(&ctx->lock);
406 return 0;
407}
408
409static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx)
410{
411 pthread_mutex_lock(&ctx->lock);
412 ctx->running--;
413 if (!ctx->running) {
414 pthread_cond_broadcast(&ctx->cond);
415 pthread_mutex_unlock(&ctx->lock);
416
417 DVR_WRAPPER_DEBUG(1, "stop wrapper thread(%s) ...\n", ctx->name);
418 pthread_join(ctx->thread, NULL);
419 DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) stopped\n", ctx->name);
420
421 pthread_mutex_lock(&ctx->lock);
422 if (!ctx->running) /*protect*/
423 pthread_cond_destroy(&ctx->cond);
424 }
425 pthread_mutex_unlock(&ctx->lock);
426 return 0;
427}
428
429#define WRAPPER_THREAD_RECORD (&wrapper_thread[0])
430#define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1])
431
432static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx)
433{
434 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
435 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
436 return wrapper_requestThread(thread_ctx, wrapper_task);
437}
438
439static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx)
440{
441 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
442 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
443 return wrapper_releaseThread(thread_ctx);
444}
445
446static inline int wrapper_releaseThreadForType(int type)
447{
448 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)?
449 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
450 return wrapper_releaseThread(thread_ctx);
451}
452
453static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx)
454{
455 pthread_cond_signal(&thread_ctx->cond);
456}
457
458static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx)
459{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800460 struct timespec rt;
461 get_timespec_timeout(200, &rt);
462 pthread_cond_timedwait(&thread_ctx->cond, &thread_ctx->lock, &rt);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800463 return 0;
464}
465
466static inline void wrapper_threadSignalForType(int type)
467{
468 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ?
469 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
470 wrapper_threadSignal(thread_ctx);
471}
472
473static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx)
474{
475 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
476 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
477 wrapper_threadSignal(thread_ctx);
478}
479
480static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx)
481{
482 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
483 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
484 wrapper_threadWait(thread_ctx);
485 return 0;
486}
487
488static void get_timeout_real(int timeout, struct timespec *ts)
489{
490 struct timespec ots;
491 int left, diff;
492
493 clock_gettime(CLOCK_REALTIME, &ots);
494
495 ts->tv_sec = ots.tv_sec + timeout/1000;
496 ts->tv_nsec = ots.tv_nsec;
497
498 left = timeout % 1000;
499 left *= 1000000;
500 diff = 1000000000-ots.tv_nsec;
501
502 if (diff <= left) {
503 ts->tv_sec++;
504 ts->tv_nsec = left-diff;
505 } else {
506 ts->tv_nsec += left;
507 }
508}
509
510/*return condition, locked if condition == true*/
511static int wrapper_mutex_lock_if(pthread_mutex_t *lock, int *condition)
512{
513 int r2;
514 do {
515 struct timespec rt2;
516 /*android use real time for mutex timedlock*/
517 get_timeout_real(10, &rt2);
518 r2 = pthread_mutex_timedlock(lock, &rt2);
519 } while (*condition && (r2 == ETIMEDOUT));
520
521 if (!(*condition) && (r2 == 0))
522 pthread_mutex_unlock(lock);
523
524 return *condition;
525}
526
527static void *wrapper_task(void *arg)
528{
529 DVR_WrapperThreadCtx_t *tctx = (DVR_WrapperThreadCtx_t *)arg;
530 DVR_WrapperEventCtx_t *evt;
531
532 pthread_mutex_lock(&tctx->lock);
533
534 while (tctx->running) {
535 {
536 int ret;
hualing chene3797f02021-01-13 14:53:28 +0800537
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800538 evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
539 if (!evt)
540 ret = wrapper_threadWait(tctx);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800541 }
542
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800543 while (evt) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800544 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
545 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chenbc0aec92021-03-18 14:52:40 +0800546 if (ctx == NULL) {
547 DVR_WRAPPER_DEBUG(1, "warp not get ctx.free event..\n");
548 goto processed;
549 }
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800550 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 +0800551 if (tctx->running) {
552 /*
553 continue not break,
554 make all events consumed, or mem leak
555 */
556 if (!wrapper_mutex_lock_if(&ctx->lock, &tctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800557 goto processed;
558
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800559 if (ctx_valid(ctx)) {
560 /*double check after lock*/
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800561 if (evt->sn == ctx->sn) {
562 pthread_mutex_unlock(&tctx->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800563 process_handleEvents(evt, ctx);
Zhiqiang Han3b9c9082021-11-10 10:41:09 +0800564 pthread_mutex_lock(&tctx->lock);
565 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800566 }
567 pthread_mutex_unlock(&ctx->lock);
568 }
569
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800570processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800571 ctx_freeEvent(evt);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800572
573 evt = (tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent();
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800574 }
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800575 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 +0800576 }
577
578 pthread_mutex_unlock(&tctx->lock);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800579 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 +0800580 return NULL;
581}
582
583static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
584{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800585 pthread_mutex_lock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800586 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
587 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800588 pthread_mutex_unlock(&WRAPPER_THREAD_RECORD->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800589 return 0;
590}
591
592static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
593{
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800594 pthread_mutex_lock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800595 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
596 wrapper_threadSignalForType(evt->type);
Zhiqiang Han18f42c82021-08-11 17:13:28 +0800597 pthread_mutex_unlock(&WRAPPER_THREAD_PLAYBACK->lock);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800598 return 0;
599}
600
601static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
602{
603 DVR_WrapperPlaybackSegmentInfo_t *pseg, *pseg_tmp;
604 list_for_each_entry_safe(pseg, pseg_tmp, &ctx->segments, head) {
605 list_del(&pseg->head);
606 free(pseg);
607 }
608}
609
610static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *pseg,
611 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
612{
613 (void)ctx;
614 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
615 pseg->seg_info = *seg_info;
616 else if (update_flags & U_PIDS) {
617 pseg->seg_info.nb_pids = seg_info->nb_pids;
618 memcpy(pseg->seg_info.pids, seg_info->pids, sizeof(pseg->seg_info.pids));
619 } else if (update_flags & U_STAT) {
620 pseg->seg_info.duration = seg_info->duration;
621 pseg->seg_info.size = seg_info->size;
622 pseg->seg_info.nb_packets = seg_info->nb_packets;
623 }
hualing chen03fd4942021-07-15 15:56:41 +0800624 //update current segment duration on timeshift mode
hualing chenb9b358a2021-08-17 15:06:36 +0800625 if (ctx->playback.param_open.is_timeshift
626 || ctx_isPlay_recording(ctx->playback.param_open.location))
hualing chen03fd4942021-07-15 15:56:41 +0800627 dvr_playback_update_duration(ctx->playback.player,pseg->seg_info.id,pseg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800628 /*no changes
629 DVR_PlaybackSegmentFlag_t flags;
630 pseg->playback_info.segment_id = pseg->seg_info.id;
631 strncpy(pseg->playback_info.location,
632 ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
633 pseg->playback_info.pids = ctx->playback.pids_req;
634 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
635 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
636 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
637 pseg->playback_info.flags = flags;
638 */
639}
640
641static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
642{
643 DVR_WrapperPlaybackSegmentInfo_t *pseg;
644
645 DVR_WRAPPER_DEBUG(1, "timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
646 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
647
648 if (list_empty(&ctx->segments)) {
649 DVR_WRAPPER_DEBUG(1, "timeshift, update while no segment exists, ignore\n");
650 return DVR_SUCCESS;
651 }
652
653 /*normally, the last segment added will be updated*/
654 pseg =
655 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
656 if (pseg->seg_info.id == seg_info->id) {
657 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
658 } else {
659 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
660 if (pseg->seg_info.id == seg_info->id) {
661 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
662 break;
663 }
664 }
665 }
666
667 /*need to notify the dvr_playback*/
hualing chenb9b358a2021-08-17 15:06:36 +0800668 if ((ctx->playback.param_open.is_timeshift/*should must be timeshift*/
669 || ctx_isPlay_recording(ctx->playback.param_open.location))
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800670 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
671 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
672 if (
673 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
674 (ctx->playback.seg_status.segment_id == seg_info->id
675 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
676 ||
677 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
678 (ctx->playback.seg_status.segment_id != seg_info->id
679 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
680 )
681 {
682 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800683 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800684 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800685 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800686
687 error = dvr_playback_resume(ctx->playback.player);
688 DVR_WRAPPER_DEBUG(1, "timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n",
689 ctx->sn, error,
690 seg_info->id, seg_info->duration,
691 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
692 }
693 }
694
695 return DVR_SUCCESS;
696}
697
698static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *pseg,
699 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
700{
701 (void)ctx;
702 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
703 pseg->info = *seg_info;
704 else if (update_flags & U_PIDS) {
705 pseg->info.nb_pids = seg_info->nb_pids;
706 memcpy(pseg->info.pids, seg_info->pids, sizeof(pseg->info.pids));
707 } else if (update_flags & U_STAT) {
708 pseg->info.duration = seg_info->duration;
709 pseg->info.size = seg_info->size;
710 pseg->info.nb_packets = seg_info->nb_packets;
711 }
712}
713
714static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
715{
hualing chen266b9502020-04-04 17:39:39 +0800716 DVR_WrapperRecordSegmentInfo_t *pseg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800717
718 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800719 if (!list_empty(&ctx->segments)) {
720 pseg =
721 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
722 if (pseg->info.id == seg_info->id) {
723 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
724 } else {
725 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
726 if (pseg->info.id == seg_info->id) {
727 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
728 break;
729 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800730 }
731 }
732 }
733
734 /*timeshift, update the segment for playback*/
735 /*
736 the playback should grab the segment info other than the id,
737 and the id will be updated by each segment-add during the recording
738 */
739 /*
740 the playback paused if no data been checked from recording,
741 should resume the player later when there's more data
742 */
hualing chenb9b358a2021-08-17 15:06:36 +0800743 int sn = 0;
744 if (ctx->record.param_open.is_timeshift ||
745 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
746 DVR_WrapperCtx_t *ctx_playback;
747 if (ctx->record.param_open.is_timeshift)
748 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
749 else
750 ctx_playback = ctx_getPlayback(sn);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800751
752 if (ctx_playback) {
753 pthread_mutex_lock(&ctx_playback->lock);
754 if (ctx_valid(ctx_playback)
hualing chenb9b358a2021-08-17 15:06:36 +0800755 && (ctx_playback->sn == sn_timeshift_playback ||
756 ctx_playback->sn == sn)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800757 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
758 }
759 pthread_mutex_unlock(&ctx_playback->lock);
760 }
761 }
762
763 return DVR_SUCCESS;
764}
765
766static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
767 DVR_RecordSegmentInfo_t *seg_info,
768 DVR_PlaybackPids_t *p_pids,
769 DVR_PlaybackSegmentFlag_t flags)
770{
771 DVR_WrapperPlaybackSegmentInfo_t *pseg;
772 int error;
773
774 error = 0;
775 pseg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
776 if (!pseg) {
777 error = DVR_FAILURE;
778 DVR_WRAPPER_DEBUG(1, "memory fail\n");
779 return error;
780 }
781
782 /*copy the orignal segment info*/
783 pseg->seg_info = *seg_info;
784 /*generate the segment info used in playback*/
785 pseg->playback_info.segment_id = pseg->seg_info.id;
786 strncpy(pseg->playback_info.location, ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
787 pseg->playback_info.pids = *p_pids;
788 pseg->playback_info.flags = flags;
789 list_add(&pseg->head, &ctx->segments);
hualing chen451c8f72022-03-09 13:05:52 +0800790 DVR_WRAPPER_DEBUG(1, "start to add segment %lld\n", pseg->playback_info.segment_id);
hualing chen03fd4942021-07-15 15:56:41 +0800791 pseg->playback_info.duration = pseg->seg_info.duration;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800792
793 error = dvr_playback_add_segment(ctx->playback.player, &pseg->playback_info);
794 if (error) {
795 DVR_WRAPPER_DEBUG(1, "fail to add segment %lld (%d)\n", pseg->playback_info.segment_id, error);
796 } else {
797 ctx->playback.status.info_full.time += pseg->seg_info.duration;
798 ctx->playback.status.info_full.size += pseg->seg_info.size;
799 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
800 }
801
802 return error;
803}
804
805static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
806{
807 DVR_WrapperRecordSegmentInfo_t *pseg;
808 int error;
hualing chenab0d1262021-09-26 15:22:50 +0800809 int sn = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800810
811 error = 0;
812 pseg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
813 if (!pseg) {
814 error = DVR_FAILURE;
815 DVR_WRAPPER_DEBUG(1, "memory fail\n");
816 }
817 pseg->info = *seg_info;
818 list_add(&pseg->head, &ctx->segments);
hualing chenab0d1262021-09-26 15:22:50 +0800819
hualing chenb9b358a2021-08-17 15:06:36 +0800820 if (ctx->record.param_open.is_timeshift ||
821 (sn = ctx_isRecord_playing(ctx->record.param_open.location))) {
822
823 DVR_WrapperCtx_t *ctx_playback;
824 if (ctx->record.param_open.is_timeshift)
825 ctx_playback = ctx_getPlayback(sn_timeshift_playback);
826 else
827 ctx_playback = ctx_getPlayback(sn);
828
829 DVR_WRAPPER_DEBUG(1, "ctx_playback ---- add segment\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800830
831 if (ctx_playback) {
832 pthread_mutex_lock(&ctx_playback->lock);
833 if (ctx_valid(ctx_playback)) {
834 DVR_PlaybackSegmentFlag_t flags;
835
836 /*only if playback has started, the previous segments have been loaded*/
837 if (!list_empty(&ctx_playback->segments)) {
838 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
Gong Ke2a0ebbe2021-05-25 15:22:50 +0800839 if (ctx->record.param_open.flags & DVR_RECORD_FLAG_SCRAMBLED)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800840 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
841 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
hualing chen451c8f72022-03-09 13:05:52 +0800842 } else {
843 DVR_WRAPPER_DEBUG(1, "ctx_playback list_empty(&ctx_playback->segments) true\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800844 }
hualing chenb9b358a2021-08-17 15:06:36 +0800845 } else {
846 DVR_WRAPPER_DEBUG(1, "ctx_playback ---- not valid\n");
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800847 }
848 pthread_mutex_unlock(&ctx_playback->lock);
849 }
hualing chen451c8f72022-03-09 13:05:52 +0800850 else
851 {
852 DVR_WRAPPER_DEBUG(1, "ctx_playback ---- not valid 1\n");
853 }
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800854 } else {
hualing chenb9b358a2021-08-17 15:06:36 +0800855 DVR_WRAPPER_DEBUG(1, "ctx_playback -sn[%d]-\n", sn);
Zhiqiang Hane0a1c382021-06-08 11:28:05 +0800856 dvr_segment_link_op(ctx->record.param_open.location, 1, &seg_info->id, LSEG_OP_ADD);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800857 }
858
859 return error;
860}
861
862static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
863{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800864 int error = -1;
865 DVR_WrapperPlaybackSegmentInfo_t *pseg = NULL, *pseg_tmp;
hualing chenb9a1a2c2021-12-31 11:27:59 +0800866 uint32_t off_set = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800867 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
868
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800869 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800870 if (pseg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800871
872 if (ctx->current_segment_id == seg_info->id) {
873 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
874
875 /*drive the player out of this will-be-deleted segment*/
876 next_seg = list_prev_entry(pseg, head);
877
878 if (ctx->playback.speed != 100.0f) {
879 error = dvr_playback_resume(ctx->playback.player);
880 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
881 }
hualing chenb9a1a2c2021-12-31 11:27:59 +0800882 if (ctx->playback.param_open.vendor == DVR_PLAYBACK_VENDOR_AMAZON)
883 off_set = 10 * 1000;
884 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, off_set);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800885 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);
886
887 if (ctx->playback.speed == 0.0f) {
888 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
889 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last paused from new start (%d)\n", ctx->sn, error);
890 } else if (ctx->playback.speed != 100.0f) {
891 DVR_PlaybackSpeed_t dvr_speed = {
892 .speed = { ctx->playback.speed },
893 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
894 };
895 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
896 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last speed(x%f) from new start (%d)\n", ctx->sn,ctx->playback.speed, error);
897 }
898 }
899
900 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
901 if (error) {
902 /*remove playack segment fail*/
903 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), failed to remove segment(%llu) (%d)\n", ctx->sn, seg_info->id, error);
904 }
905
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800906 list_del(&pseg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800907
908 /*record the obsolete*/
909 ctx->playback.obsolete.time += pseg->seg_info.duration;
910 ctx->playback.obsolete.size += pseg->seg_info.size;
911 ctx->playback.obsolete.pkts += pseg->seg_info.nb_packets;
hualing chen56c0a162022-01-27 17:01:50 +0800912 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) ..obs(%d).\n", ctx->sn, seg_info->id, ctx->playback.obsolete.time);
hualing chen03fd4942021-07-15 15:56:41 +0800913 dvr_playback_set_obsolete(ctx->playback.player, ctx->playback.obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800914 free(pseg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800915 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800916 }
917 }
918
919 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->id, error);
920
921 return error;
922}
923
924static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
925{
926 int error;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800927 DVR_WrapperRecordSegmentInfo_t *pseg, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800928
hualing chen56c0a162022-01-27 17:01:50 +0800929 DVR_WRAPPER_DEBUG(1, "---timeshift, remove record(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->info.id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800930
931 /*if timeshifting, notify the playback first, then deal with record*/
932 if (ctx->record.param_open.is_timeshift) {
933 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
934
935 if (ctx_playback) {
936 pthread_mutex_lock(&ctx_playback->lock);
hualing chen56c0a162022-01-27 17:01:50 +0800937 if (ctx_playback->current_segment_id == seg_info->info.id && ctx_playback->playback.speed == 100.0f) {
938 ctx_playback->playback.tf_full = DVR_TRUE;
939 DVR_WRAPPER_DEBUG(1, "timeshift, not remove record(sn:%ld) segment(%lld) .(%d) (%f).isplaying.\n", ctx->sn, seg_info->info.id, DVR_TRUE, ctx_playback->playback.speed);
940 pthread_mutex_unlock(&ctx_playback->lock);
941 return DVR_SUCCESS;
942 } else {
943 DVR_WRAPPER_DEBUG(1, "timeshift, start remove record(sn:%ld) segment(%lld) (%lld).(%f)..\n", ctx->sn, seg_info->info.id, ctx_playback->current_segment_id,ctx_playback->playback.speed);
944 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800945 if (ctx_valid(ctx_playback)
946 && ctx_playback->sn == sn_timeshift_playback
947 && !list_empty(&ctx_playback->segments)) {
948 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
949 }
hualing chen56c0a162022-01-27 17:01:50 +0800950 ctx_playback->playback.tf_full = DVR_FALSE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800951 pthread_mutex_unlock(&ctx_playback->lock);
952 }
953 }
954
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +0800955 uint64_t id = seg_info->info.id;
956
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800957 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +0800958 if (pseg->info.id == id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800959 list_del(&pseg->head);
960
961 /*record the obsolete*/
962 ctx->record.obsolete.time += pseg->info.duration;
963 ctx->record.obsolete.size += pseg->info.size;
964 ctx->record.obsolete.pkts += pseg->info.nb_packets;
965
966 free(pseg);
967 break;
968 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800969 }
970
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +0800971 error = dvr_segment_delete(ctx->record.param_open.location, id);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800972
Zhiqiang Hanbc3019b2022-03-21 11:31:21 +0800973 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, id, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800974
975 return error;
976}
977
978int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
979{
980 int error;
981 DVR_WrapperCtx_t *ctx;
982 DVR_RecordOpenParams_t open_param;
983
984 DVR_RETURN_IF_FALSE(rec);
985 DVR_RETURN_IF_FALSE(params);
986
987 /*get a free ctx*/
988 ctx = ctx_getRecord(0);
989 DVR_RETURN_IF_FALSE(ctx);
990
991 pthread_mutex_lock(&ctx->lock);
992
hualing chen51652f02020-12-29 16:59:31 +0800993 DVR_WRAPPER_DEBUG(1, "open record(dmx:%d) .istf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
994 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800995
996 ctx_reset(ctx);
997
998 ctx->record.param_open = *params;
999 ctx->record.event_fn = params->event_fn;
1000 ctx->record.event_userdata = params->event_userdata;
1001 ctx->record.next_segment_id = 0;
1002 ctx->current_segment_id = 0;
1003 INIT_LIST_HEAD(&ctx->segments);
1004 ctx->sn = get_sn();
1005
1006 wrapper_requestThreadFor(ctx);
1007
hualing chen266b9502020-04-04 17:39:39 +08001008 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +08001009 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001010 open_param.dmx_dev_id = params->dmx_dev_id;
1011 open_param.data_from_memory = 0;
1012 open_param.flags = params->flags;
Yahui Han15a00f12021-11-15 19:44:39 +08001013 if (params->flush_size) {
1014 open_param.notification_size = params->flush_size;
1015 } else {
1016 open_param.notification_size = 64*1024;
1017 }
hualing chen002e5b92022-02-23 17:51:21 +08001018 open_param.notification_time = 400;//ms
Zhiqiang Han31505452020-05-06 15:08:10 +08001019 open_param.flush_size = params->flush_size;
hualing chen03fd4942021-07-15 15:56:41 +08001020 open_param.ringbuf_size = params->ringbuf_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001021 open_param.event_fn = wrapper_record_event_handler;
1022 open_param.event_userdata = (void*)ctx->sn;
Yahui Han1fbf3292021-11-08 18:17:19 +08001023 if (params->keylen) {
1024 open_param.clearkey = params->clearkey;
1025 open_param.cleariv = params->cleariv;
1026 open_param.keylen = params->keylen;
1027 }
wentao.ma35a69d42022-03-10 18:08:40 +08001028 open_param.force_sysclock = params->force_sysclock;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001029
1030 error = dvr_record_open(&ctx->record.recorder, &open_param);
1031 if (error) {
1032 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
1033 ctx_reset(ctx);
1034 pthread_mutex_unlock(&ctx->lock);
1035 wrapper_releaseThreadForType(ctx->type);
1036 return DVR_FAILURE;
1037 }
1038 if (params->is_timeshift)
1039 sn_timeshift_record = ctx->sn;
1040
1041 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
1042
Yahui Han1fbf3292021-11-08 18:17:19 +08001043 if (params->crypto_fn) {
1044 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
1045 if (error) {
1046 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) set encrypt callback fail(error:%d).\n", params->dmx_dev_id, error);
1047 }
hualing chen266b9502020-04-04 17:39:39 +08001048 }
1049
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001050 pthread_mutex_unlock(&ctx->lock);
1051
1052 *rec = (DVR_WrapperRecord_t)ctx->sn;
1053 return DVR_SUCCESS;
1054}
1055
1056int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
1057{
1058 DVR_WrapperCtx_t *ctx;
1059 DVR_RecordSegmentInfo_t seg_info;
1060 int error;
1061
1062 DVR_RETURN_IF_FALSE(rec);
1063
1064 ctx = ctx_getRecord((unsigned long)rec);
1065 DVR_RETURN_IF_FALSE(ctx);
1066
1067 pthread_mutex_lock(&ctx->lock);
1068 DVR_WRAPPER_DEBUG(1, "close record(sn:%ld)\n", ctx->sn);
1069 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1070
1071 memset(&seg_info, 0, sizeof(seg_info));
1072 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1073
1074 error = dvr_record_close(ctx->record.recorder);
1075
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001076 if (ctx->record.param_open.is_timeshift)
1077 sn_timeshift_record = 0;
1078
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001079 ctx_freeSegments(ctx);
1080
1081 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) closed = (%d).\n", ctx->sn, error);
1082 ctx_reset(ctx);
1083 pthread_mutex_unlock(&ctx->lock);
1084
1085 wrapper_releaseThreadForType(ctx->type);
1086
1087 return error;
1088}
1089
1090int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
1091{
1092 DVR_WrapperCtx_t *ctx;
1093 DVR_RecordStartParams_t *start_param;
1094 int i;
1095 int error;
1096
1097 DVR_RETURN_IF_FALSE(rec);
1098 DVR_RETURN_IF_FALSE(params);
1099
1100 ctx = ctx_getRecord((unsigned long)rec);
1101 DVR_RETURN_IF_FALSE(ctx);
1102
1103 pthread_mutex_lock(&ctx->lock);
hualing chena5f03222021-12-02 11:22:35 +08001104 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 +08001105 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1106
1107 start_param = &ctx->record.param_start;
1108 memset(start_param, 0, sizeof(*start_param));
1109 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1110 start_param->segment.segment_id = ctx->record.next_segment_id++;
1111 start_param->segment.nb_pids = params->pids_info.nb_pids;
1112 for (i = 0; i < params->pids_info.nb_pids; i++) {
1113 start_param->segment.pids[i] = params->pids_info.pids[i];
1114 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
1115 }
hualing chena5f03222021-12-02 11:22:35 +08001116 if (params->save_rec_file == 0)//default is not save
1117 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001118 {
1119 /*sync to update for further use*/
1120 DVR_RecordStartParams_t *update_param;
1121 update_param = &ctx->record.param_update;
1122 memcpy(update_param, start_param, sizeof(*update_param));
1123 for (i = 0; i < update_param->segment.nb_pids; i++)
1124 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
1125 }
1126
1127 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1128 {
1129 DVR_RecordSegmentInfo_t new_seg_info =
1130 { .id = start_param->segment.segment_id, };
1131 wrapper_addRecordSegment(ctx, &new_seg_info);
1132 }
1133
1134 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) started = (%d)\n", ctx->sn, error);
1135
1136 pthread_mutex_unlock(&ctx->lock);
1137
1138 return error;
1139}
1140
1141int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1142{
1143 DVR_WrapperCtx_t *ctx;
1144 DVR_RecordSegmentInfo_t seg_info;
1145 int error;
1146
1147 DVR_RETURN_IF_FALSE(rec);
1148
1149 ctx = ctx_getRecord((unsigned long)rec);
1150 DVR_RETURN_IF_FALSE(ctx);
1151
1152 pthread_mutex_lock(&ctx->lock);
1153 DVR_WRAPPER_DEBUG(1, "stop record(sn:%ld) ...\n", ctx->sn);
1154 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1155
1156 memset(&seg_info, 0, sizeof(seg_info));
1157 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1158 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1159
1160 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
1161 pthread_mutex_unlock(&ctx->lock);
1162
1163 return error;
1164}
1165
hualing chen03fd4942021-07-15 15:56:41 +08001166int dvr_wrapper_pause_record (DVR_WrapperRecord_t rec)
1167{
1168 DVR_WrapperCtx_t *ctx;
1169 int error;
1170
1171 DVR_RETURN_IF_FALSE(rec);
1172
1173 ctx = ctx_getRecord((unsigned long)rec);
1174 DVR_RETURN_IF_FALSE(ctx);
1175
1176 pthread_mutex_lock(&ctx->lock);
1177 DVR_WRAPPER_DEBUG(1, "pause record(sn:%ld) ...\n", ctx->sn);
1178 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1179
1180 error = dvr_record_pause(ctx->record.recorder);
1181
1182 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) pauseed = (%d)\n", ctx->sn, error);
1183 pthread_mutex_unlock(&ctx->lock);
1184
1185 return error;
1186}
1187
1188int dvr_wrapper_resume_record (DVR_WrapperRecord_t rec)
1189{
1190 DVR_WrapperCtx_t *ctx;
1191 int error;
1192
1193 DVR_RETURN_IF_FALSE(rec);
1194
1195 ctx = ctx_getRecord((unsigned long)rec);
1196 DVR_RETURN_IF_FALSE(ctx);
1197
1198 pthread_mutex_lock(&ctx->lock);
1199 DVR_WRAPPER_DEBUG(1, "resume record(sn:%ld) ...\n", ctx->sn);
1200 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1201
1202 error = dvr_record_resume(ctx->record.recorder);
1203
1204 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) resumed = (%d)\n", ctx->sn, error);
1205 pthread_mutex_unlock(&ctx->lock);
1206
1207 return error;
1208}
1209
wentao.maa69578c2022-04-07 09:27:39 +08001210static DVR_Bool_t pids_equal(const DVR_RecordSegmentStartParams_t* p1,
1211 const DVR_WrapperUpdatePidsParams_t* p2)
1212{
1213 int i=0;
1214 int j=0;
1215 DVR_Bool_t found=DVR_FALSE;
1216 DVR_Bool_t is_equal=DVR_TRUE;
1217 char buf[128]={0};
1218 int cnt=0;
1219
1220 DVR_RETURN_IF_FALSE(p1 != NULL && p2 != NULL);
1221 DVR_RETURN_IF_FALSE(p1->nb_pids>0 && p2->nb_pids>0);
1222
1223 for (i=0;i<p1->nb_pids;i++)
1224 {
1225 cnt+=snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p1->pids[i].pid);
1226 }
1227 dvr_log_print("%s nb_pids1:%d, pids1: %s",__func__,p1->nb_pids,buf);
1228 memset(buf,0,sizeof(buf));
1229
1230 for (i=0,cnt=0;i<p2->nb_pids;i++)
1231 {
1232 cnt+=snprintf(buf+cnt,sizeof(buf)-cnt,"0x%hx,",p2->pids[i].pid);
1233 }
1234 dvr_log_print("%s nb_pids2:%d, pids2: %s",__func__,p2->nb_pids,buf);
1235
1236 for (i=0,found=DVR_FALSE;i<p1->nb_pids;i++,found=DVR_FALSE)
1237 {
1238 for (j=0;j<p2->nb_pids;j++)
1239 {
1240 if (p1->pids[i].pid == p2->pids[j].pid)
1241 {
1242 found=DVR_TRUE;
1243 break;
1244 }
1245 }
1246 if (found == DVR_FALSE)
1247 {
1248 is_equal=DVR_FALSE;
1249 break;
1250 }
1251 }
1252 dvr_log_print("%s is_equal:%d",__func__,is_equal);
1253 return is_equal;
1254}
1255
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001256int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1257{
1258 DVR_WrapperCtx_t *ctx;
1259 DVR_RecordStartParams_t *start_param;
wentao.maa69578c2022-04-07 09:27:39 +08001260 DVR_RecordSegmentInfo_t seg_info;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001261 int i;
1262 int error;
1263
1264 DVR_RETURN_IF_FALSE(rec);
1265 DVR_RETURN_IF_FALSE(params);
1266
1267 ctx = ctx_getRecord((unsigned long)rec);
1268 DVR_RETURN_IF_FALSE(ctx);
1269
1270 pthread_mutex_lock(&ctx->lock);
1271 DVR_WRAPPER_DEBUG(1, "update record(sn:%ld)\n", ctx->sn);
1272 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1273
1274 start_param = &ctx->record.param_update;
wentao.maa69578c2022-04-07 09:27:39 +08001275 if (pids_equal(&(start_param->segment),params))
1276 {
1277 pthread_mutex_unlock(&ctx->lock);
1278 return DVR_TRUE;
1279 }
1280
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001281 memset(start_param, 0, sizeof(*start_param));
1282 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1283 start_param->segment.segment_id = ctx->record.next_segment_id++;
1284 start_param->segment.nb_pids = params->nb_pids;
1285 for (i = 0; i < params->nb_pids; i++) {
1286 start_param->segment.pids[i] = params->pids[i];
1287 start_param->segment.pid_action[i] = params->pid_action[i];
1288 }
1289 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1290 {
1291 DVR_RecordSegmentInfo_t new_seg_info =
1292 { .id = start_param->segment.segment_id, };
1293 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1294 wrapper_addRecordSegment(ctx, &new_seg_info);
1295 }
1296
1297 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) updated = (%d)\n", ctx->sn, error);
1298 pthread_mutex_unlock(&ctx->lock);
1299
1300 return error;
1301}
1302
1303int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1304{
1305 DVR_WrapperCtx_t *ctx;
1306 DVR_WrapperRecordStatus_t s;
1307 int error;
1308
1309 DVR_RETURN_IF_FALSE(rec);
1310 DVR_RETURN_IF_FALSE(status);
1311
1312 ctx = ctx_getRecord((unsigned long)rec);
1313 DVR_RETURN_IF_FALSE(ctx);
1314
1315 pthread_mutex_lock(&ctx->lock);
1316
1317 DVR_WRAPPER_DEBUG(1, "get record(sn:%ld) status ...\n", ctx->sn);
1318 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1319
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001320 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001321
1322 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
1323 ctx->sn,
1324 s.state,
1325 s.info.time,
1326 s.info.size,
1327 s.info.pkts,
1328 error);
1329
1330 *status = s;
1331
1332 pthread_mutex_unlock(&ctx->lock);
1333
1334 return error;
1335}
1336
hualing chen4fe3bee2020-10-23 13:58:52 +08001337int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1338{
1339 DVR_WrapperCtx_t *ctx;
1340 int error;
1341
1342 DVR_RETURN_IF_FALSE(rec);
1343
1344 ctx = ctx_getRecord((unsigned long)rec);
1345 DVR_RETURN_IF_FALSE(ctx);
1346
1347 pthread_mutex_lock(&ctx->lock);
1348 error = dvr_record_is_secure_mode(ctx->record.recorder);
1349 pthread_mutex_unlock(&ctx->lock);
1350 return error;
1351}
1352
hualing chen266b9502020-04-04 17:39:39 +08001353int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1354{
1355 DVR_WrapperCtx_t *ctx;
1356 int error;
1357
1358 DVR_RETURN_IF_FALSE(rec);
1359 DVR_RETURN_IF_FALSE(p_secure_buf);
1360
1361 ctx = ctx_getRecord((unsigned long)rec);
1362 DVR_RETURN_IF_FALSE(ctx);
1363
1364 pthread_mutex_lock(&ctx->lock);
1365 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
1366 pthread_mutex_unlock(&ctx->lock);
1367 return error;
1368}
1369
1370int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1371{
1372 DVR_WrapperCtx_t *ctx;
1373 int error;
1374
1375 DVR_RETURN_IF_FALSE(rec);
1376 DVR_RETURN_IF_FALSE(func);
1377
1378 ctx = ctx_getRecord((unsigned long)rec);
1379 DVR_RETURN_IF_FALSE(ctx);
1380
1381 pthread_mutex_lock(&ctx->lock);
1382 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
1383 pthread_mutex_unlock(&ctx->lock);
1384 return error;
1385}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001386
1387
1388int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1389{
1390 DVR_WrapperCtx_t *ctx;
1391 DVR_PlaybackOpenParams_t open_param;
1392 int error;
1393
1394 DVR_RETURN_IF_FALSE(playback);
1395 DVR_RETURN_IF_FALSE(params);
1396 DVR_RETURN_IF_FALSE(params->playback_handle);
1397
1398 /*get a free ctx*/
1399 ctx = ctx_getPlayback(0);
1400 DVR_RETURN_IF_FALSE(ctx);
1401
1402 pthread_mutex_lock(&ctx->lock);
1403
hualing chena5f03222021-12-02 11:22:35 +08001404 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 +08001405
1406 ctx_reset(ctx);
1407
1408 ctx->playback.param_open = *params;
1409 ctx->playback.event_fn = params->event_fn;
1410 ctx->playback.event_userdata = params->event_userdata;
1411 ctx->current_segment_id = 0;
1412 INIT_LIST_HEAD(&ctx->segments);
1413 ctx->sn = get_sn();
1414
1415 wrapper_requestThreadFor(ctx);
1416
hualing chen266b9502020-04-04 17:39:39 +08001417 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001418 open_param.dmx_dev_id = params->dmx_dev_id;
1419 open_param.block_size = params->block_size;
1420 open_param.is_timeshift = params->is_timeshift;
1421 //open_param.notification_size = 10*1024; //not supported
1422 open_param.event_fn = wrapper_playback_event_handler;
1423 open_param.event_userdata = (void*)ctx->sn;
1424 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001425 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001426 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chen90b3ae62021-03-30 10:49:28 +08001427 open_param.vendor = params->vendor;
1428
Yahui Han1fbf3292021-11-08 18:17:19 +08001429 if (params->keylen) {
1430 open_param.clearkey = params->clearkey;
1431 open_param.cleariv = params->cleariv;
1432 open_param.keylen = params->keylen;
1433 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001434
1435 error = dvr_playback_open(&ctx->playback.player, &open_param);
1436 if (error) {
1437 DVR_WRAPPER_DEBUG(1, "playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
1438 ctx_reset(ctx);
1439 pthread_mutex_unlock(&ctx->lock);
1440 wrapper_releaseThreadForType(ctx->type);
1441 return DVR_FAILURE;
1442 }
1443 if (params->is_timeshift)
1444 sn_timeshift_playback = ctx->sn;
1445
hualing chen266b9502020-04-04 17:39:39 +08001446 DVR_WRAPPER_DEBUG(1, "hanyh: playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
1447 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1448 if (error) {
1449 DVR_WRAPPER_DEBUG(1, "playback set deccrypt callback fail(error:%d).\n", error);
1450 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001451 pthread_mutex_unlock(&ctx->lock);
1452
1453 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1454 return DVR_SUCCESS;
1455}
1456
1457int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1458{
1459 DVR_WrapperCtx_t *ctx;
1460 int error;
1461
1462 DVR_RETURN_IF_FALSE(playback);
1463
1464 ctx = ctx_getPlayback((unsigned long)playback);
1465 DVR_RETURN_IF_FALSE(ctx);
1466
1467 pthread_mutex_lock(&ctx->lock);
1468 DVR_WRAPPER_DEBUG(1, "close playback(sn:%ld)\n", ctx->sn);
1469 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1470
1471 if (ctx->playback.param_open.is_timeshift)
1472 sn_timeshift_playback = 0;
1473
1474 /*try stop first*/
1475 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1476
1477 {
1478 /*remove all segments*/
1479 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1480
1481 list_for_each_entry(pseg, &ctx->segments, head) {
1482 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1483 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1484 ctx->sn, pseg->playback_info.segment_id, error);
1485 }
1486 ctx_freeSegments(ctx);
1487 }
1488
1489 error = dvr_playback_close(ctx->playback.player);
1490
1491 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) closed.\n", ctx->sn);
1492 ctx_reset(ctx);
1493 pthread_mutex_unlock(&ctx->lock);
1494
1495 wrapper_releaseThreadForType(ctx->type);
1496
1497 return error;
1498}
1499
1500int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1501{
1502 DVR_WrapperCtx_t *ctx;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001503 int error=0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001504 uint64_t *p_segment_ids;
1505 uint32_t segment_nb;
1506 uint32_t i;
1507 DVR_RecordSegmentInfo_t seg_info_1st;
Wentao MA9aa0aa02021-12-23 18:30:17 +08001508 int got_1st_seg=0;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001509 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001510 DVR_Bool_t is_timeshift = DVR_FALSE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001511
1512 DVR_RETURN_IF_FALSE(playback);
1513 DVR_RETURN_IF_FALSE(p_pids);
1514
hualing chenc110f952021-01-18 11:25:37 +08001515 ctx_record = NULL;
1516
1517 /*lock the recorder to avoid changing the recording segments*/
1518 ctx_record = ctx_getRecord(sn_timeshift_record);
1519
1520 if (ctx_record) {
1521 pthread_mutex_lock(&ctx_record->lock);
1522 if (!ctx_valid(ctx_record)
1523 || ctx_record->sn != sn_timeshift_record) {
1524 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error found\n");
1525 pthread_mutex_unlock(&ctx_record->lock);
1526 is_timeshift = DVR_FALSE;
1527 } else {
1528 is_timeshift = DVR_TRUE;
1529 }
1530 }
1531
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001532 ctx = ctx_getPlayback((unsigned long)playback);
1533 DVR_RETURN_IF_FALSE(ctx);
1534
1535 pthread_mutex_lock(&ctx->lock);
1536
1537 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",
1538 ctx->sn,
1539 ctx->playback.param_open.location,
1540 flags,
1541 p_pids->video.pid, p_pids->video.format,
1542 p_pids->audio.pid, p_pids->audio.format,
1543 p_pids->ad.pid, p_pids->ad.format,
1544 p_pids->subtitle.pid, p_pids->subtitle.format,
1545 p_pids->pcr.pid);
1546
1547 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1548
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001549 if (ctx->playback.param_open.is_timeshift) {
1550 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001551 if (is_timeshift == DVR_FALSE) {
1552 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error return\n");
1553 pthread_mutex_unlock(&ctx->lock);
1554 return DVR_FAILURE;
1555 } else {
1556 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
1557 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001558 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001559 }
1560
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001561 /*obtain all segments in a list*/
1562 segment_nb = 0;
1563 p_segment_ids = NULL;
1564 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001565 if (!error) {
1566 got_1st_seg = 0;
hualing chenb9a02922021-12-14 11:29:47 +08001567 struct list_head info_list; /**< segment list head*/
1568 INIT_LIST_HEAD(&info_list);
1569
hualing chenb9b358a2021-08-17 15:06:36 +08001570 DVR_WRAPPER_DEBUG(1, "get list segment_nb::%d",segment_nb);
hualing chenb9a02922021-12-14 11:29:47 +08001571 //we need free info list buf when we used end.
1572 error = dvr_segment_get_allInfo(ctx->playback.param_open.location, &info_list);
hualing chen926a8ec2021-12-20 20:38:24 +08001573 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08001574 error = DVR_FAILURE;
1575 DVR_WRAPPER_DEBUG(1, "fail to get all seg info (location:%s, seg:%llu), (error:%d)\n",
1576 ctx->playback.param_open.location, p_segment_ids[i], error);
1577 for (i = 0; i < segment_nb; i++) {
1578 DVR_RecordSegmentInfo_t seg_info;
1579 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001580
hualing chenb9a02922021-12-14 11:29:47 +08001581 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1582 if (error) {
1583 error = DVR_FAILURE;
1584 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1585 ctx->playback.param_open.location, p_segment_ids[i], error);
1586 break;
1587 }
1588 //add check if has audio or video pid. if not exist. not add segment to playback
1589 int ii = 0;
1590 int has_av = 0;
1591 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1592 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1593 if (type == DVR_STREAM_TYPE_VIDEO ||
1594 type == DVR_STREAM_TYPE_AUDIO ||
1595 type == DVR_STREAM_TYPE_AD) {
1596 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1597 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,
1598 DVR_STREAM_TYPE_VIDEO,
1599 DVR_STREAM_TYPE_AUDIO,
1600 DVR_STREAM_TYPE_AD);
1601 has_av = 1;
1602 //break;
1603 } else {
1604 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,
1605 DVR_STREAM_TYPE_VIDEO,
1606 DVR_STREAM_TYPE_AUDIO,
1607 DVR_STREAM_TYPE_AD);
1608 }
1609 }
1610 if (has_av == 0) {
1611 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1612 continue;
1613 } else {
1614 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1615 }
1616 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1617 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1618 if (error)
1619 break;
1620 /*copy the 1st segment*/
1621 if (got_1st_seg == 0) {
1622 seg_info_1st = seg_info;
1623 got_1st_seg = 1;
1624 }
1625 }
1626 } else {
1627 for (i = 0; i < segment_nb; i++) {
1628 DVR_RecordSegmentInfo_t *seg_info;
1629 DVR_PlaybackSegmentFlag_t flags;
1630 int found = 0;
1631 list_for_each_entry(seg_info, &info_list, head)
1632 {
hualing chen8aed9582021-12-24 17:59:56 +08001633 if (seg_info->id == p_segment_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08001634 found = 1;
1635 DVR_WRAPPER_DEBUG(1, "get segment info::%d", i);
1636 break;
1637 }
1638 }
1639 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08001640 //last info is not found if when recording occured power off.
1641 if (p_segment_ids[i] == segment_nb - 1) {
1642 DVR_RecordSegmentInfo_t seg_info;
1643 DVR_PlaybackSegmentFlag_t flags;
1644
1645 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1646 if (error) {
1647 error = DVR_FAILURE;
1648 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1649 ctx->playback.param_open.location, p_segment_ids[i], error);
1650 break;
1651 }
1652 //
1653 //add check if has audio or video pid. if not exist. not add segment to playback
1654 int ii = 0;
1655 int has_av = 0;
1656 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1657 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1658 if (type == DVR_STREAM_TYPE_VIDEO ||
1659 type == DVR_STREAM_TYPE_AUDIO ||
1660 type == DVR_STREAM_TYPE_AD) {
1661 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1662 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,
1663 DVR_STREAM_TYPE_VIDEO,
1664 DVR_STREAM_TYPE_AUDIO,
1665 DVR_STREAM_TYPE_AD);
1666 has_av = 1;
1667 //break;
1668 } else {
1669 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,
1670 DVR_STREAM_TYPE_VIDEO,
1671 DVR_STREAM_TYPE_AUDIO,
1672 DVR_STREAM_TYPE_AD);
1673 }
1674 }
1675 if (has_av == 0) {
1676 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1677 continue;
1678 } else {
1679 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1680 }
1681 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1682 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1683 if (error)
1684 break;
1685 }
hualing chenb9a02922021-12-14 11:29:47 +08001686 continue;
1687 }
1688
1689 //add check if has audio or video pid. if not exist. not add segment to playback
1690 int ii = 0;
1691 int has_av = 0;
1692 for (ii = 0; ii < seg_info->nb_pids; ii++) {
1693 int type = (seg_info->pids[ii].type >> 24) & 0x0f;
1694 if (type == DVR_STREAM_TYPE_VIDEO ||
1695 type == DVR_STREAM_TYPE_AUDIO ||
1696 type == DVR_STREAM_TYPE_AD) {
1697 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1698 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,
1699 DVR_STREAM_TYPE_VIDEO,
1700 DVR_STREAM_TYPE_AUDIO,
1701 DVR_STREAM_TYPE_AD);
1702 has_av = 1;
1703 //break;
1704 } else {
1705 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,
1706 DVR_STREAM_TYPE_VIDEO,
1707 DVR_STREAM_TYPE_AUDIO,
1708 DVR_STREAM_TYPE_AD);
1709 }
1710 }
1711 if (has_av == 0) {
1712 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1713 continue;
1714 } else {
1715 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1716 }
1717 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1718 error = wrapper_addPlaybackSegment(ctx, seg_info, p_pids, flags);
1719 if (error)
1720 break;
1721
1722 /*copy the 1st segment*/
1723 if (got_1st_seg == 0) {
1724 seg_info_1st = *seg_info;
1725 got_1st_seg = 1;
1726 }
hualing chen92f3a142020-07-08 20:59:33 +08001727 }
hualing chenb9a02922021-12-14 11:29:47 +08001728 //free list
1729 DVR_RecordSegmentInfo_t *segment = NULL;
1730 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
1731 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
1732 {
1733 if (segment) {
1734 list_del(&segment->head);
1735 free(segment);
1736 }
1737 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001738 }
hualing chenb9a02922021-12-14 11:29:47 +08001739
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001740 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001741
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001742 /* return if no segment or fail to add */
1743 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001744
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001745 /*copy the obsolete infomation, must for timeshifting*/
1746 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1747 ctx->playback.obsolete = ctx_record->record.obsolete;
1748 }
1749
1750 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
1751
1752 ctx->playback.reach_end = DVR_FALSE;
1753 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1754 ctx->playback.speed = 0.0f;
1755 else
1756 ctx->playback.speed = 100.0f;
1757
1758 ctx->playback.pids_req = *p_pids;
hualing chen03fd4942021-07-15 15:56:41 +08001759 //calualte segment id and pos
1760 if (dvr_playback_check_limit(ctx->playback.player)) {
1761 pthread_mutex_unlock(&ctx->lock);
1762 dvr_wrapper_seek_playback(playback, 0);
1763 pthread_mutex_lock(&ctx->lock);
1764 error = dvr_playback_start(ctx->playback.player, flags);
1765 } else {
1766 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1767 error = dvr_playback_start(ctx->playback.player, flags);
1768 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
1769 ctx->sn, seg_info_1st.id, error);
1770 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001771 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)\n", ctx->sn, error);
hualing chen451c8f72022-03-09 13:05:52 +08001772 } else {
1773 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)got_1st_seg:%d\n", ctx->sn, error, got_1st_seg);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001774 }
1775 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001776
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001777 if (ctx->playback.param_open.is_timeshift) {
1778 /*unlock the recorder locked above*/
1779 if (ctx_record && ctx_valid(ctx_record)) {
1780 pthread_mutex_unlock(&ctx_record->lock);
1781 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
1782 ctx->sn, ctx_record->sn);
1783 }
1784 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001785 pthread_mutex_unlock(&ctx->lock);
1786
1787 return error;
1788}
hualing chen002e5b92022-02-23 17:51:21 +08001789//stop record and playback
1790int dvr_wrapper_stop_timeshift (DVR_WrapperPlayback_t playback)
1791{
1792 DVR_WrapperCtx_t *ctx_record = NULL;/*for timeshift*/
1793 int error;
1794 DVR_WRAPPER_DEBUG(1, "stop timeshift record\n");
1795
1796 //stop timeshift record
1797 ctx_record = ctx_getRecord(sn_timeshift_record);
1798 error = dvr_wrapper_stop_record((DVR_WrapperRecord_t)sn_timeshift_record);
1799
1800 DVR_WRAPPER_DEBUG(1, "stop timeshift ...stop play\n");
1801 //stop play
1802 error = dvr_wrapper_stop_playback(playback);
1803 //del timeshift file
1804 if (ctx_record != NULL) {
1805 DVR_WRAPPER_DEBUG(1, "del timeshift(sn:%ld) ...3\n", ctx_record->sn);
1806 error = dvr_segment_del_by_location(ctx_record->record.param_open.location);
1807 }
1808 return error;
1809}
1810//start record and start playback
1811int dvr_wrapper_restart_timeshift(DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1812{
1813 DVR_WrapperCtx_t *ctx;
1814 DVR_RecordStartParams_t *start_param;
1815 int error;
1816
1817 ctx = ctx_getRecord((unsigned long)sn_timeshift_record);
1818 DVR_RETURN_IF_FALSE(ctx);
1819
1820 pthread_mutex_lock(&ctx->lock);
1821 DVR_WRAPPER_DEBUG(1, "restart record(sn:%ld, location:%s)...\n", ctx->sn, ctx->record.param_open.location);
1822 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1823
hualing chen451c8f72022-03-09 13:05:52 +08001824 {
1825 //clear old record status
1826 // struct {
1827 // DVR_WrapperRecordOpenParams_t param_open;
1828 // DVR_RecordStartParams_t param_start;
1829 // DVR_RecordStartParams_t param_update;
1830 // DVR_RecordHandle_t recorder;
1831 // DVR_RecordEventFunction_t event_fn;
1832 // void *event_userdata;
1833
1834 // /*total status = seg_status + status + obsolete*/
1835 // DVR_RecordStatus_t seg_status; /**<status of current segment*/
1836 // DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
1837 // uint64_t next_segment_id;
1838
1839 // DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
1840 // } record;
1841 memset(&(ctx->record.seg_status), 0, sizeof(DVR_RecordStatus_t));
1842 memset(&(ctx->record.status), 0, sizeof(DVR_WrapperRecordStatus_t));
1843 memset(&(ctx->record.obsolete), 0, sizeof(DVR_WrapperInfo_t));
1844 }
1845
hualing chen002e5b92022-02-23 17:51:21 +08001846 start_param = &ctx->record.param_start;
1847
1848 error = dvr_record_start_segment(ctx->record.recorder, start_param);
1849 {
1850 DVR_RecordSegmentInfo_t new_seg_info =
1851 { .id = start_param->segment.segment_id, };
1852 wrapper_addRecordSegment(ctx, &new_seg_info);
hualing chen451c8f72022-03-09 13:05:52 +08001853 DVR_WRAPPER_DEBUG(1, "re record(sn:%ld) started = (%d)start id[%lld]id+[%lld]update id[%lld]\n", ctx->sn, error, start_param->segment.segment_id, ctx->record.next_segment_id, ctx->record.param_update.segment.segment_id);
1854 ctx->record.next_segment_id = start_param->segment.segment_id + 1;
1855 DVR_RecordStartParams_t *update_param;
1856 update_param = &ctx->record.param_update;
1857 memcpy(update_param, start_param, sizeof(*update_param));
1858 int i = 0;
1859 for (i = 0; i < update_param->segment.nb_pids; i++)
1860 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
hualing chen002e5b92022-02-23 17:51:21 +08001861 }
1862
1863 DVR_WRAPPER_DEBUG(1, "re record(sn:%ld) started = (%d)\n", ctx->sn, error);
1864
1865 pthread_mutex_unlock(&ctx->lock);
1866
1867 //start play
hualing chen451c8f72022-03-09 13:05:52 +08001868 DVR_WRAPPER_DEBUG(1, "re start play and clear old status\n");
1869 //clear play statue
1870 ctx = ctx_getPlayback((unsigned long)playback);
1871 if (ctx) {
1872 //clear old playback status
1873 // struct {
1874 // DVR_WrapperPlaybackOpenParams_t param_open;
1875 // DVR_PlaybackHandle_t player;
1876 // DVR_PlaybackEventFunction_t event_fn;
1877 // void *event_userdata;
1878
1879 // /*total status = seg_status + status*/
1880 // DVR_PlaybackStatus_t seg_status;
1881 // DVR_WrapperPlaybackStatus_t status;
1882 // DVR_PlaybackPids_t pids_req;
1883 // DVR_PlaybackEvent_t last_event;
1884 // float speed;
1885 // DVR_Bool_t reach_end;
1886
1887 // DVR_WrapperInfo_t obsolete;
1888 // DVR_Bool_t tf_full;
1889 // } playback;
1890 ctx->playback.tf_full == DVR_FALSE;
1891 ctx->playback.reach_end == DVR_FALSE;
1892 memset(&(ctx->playback.last_event), 0, sizeof(DVR_PlaybackEvent_t));
1893 memset(&(ctx->playback.seg_status), 0, sizeof(DVR_PlaybackStatus_t));
1894 memset(&(ctx->playback.status), 0, sizeof(DVR_WrapperPlaybackStatus_t));
1895 memset(&(ctx->playback.obsolete), 0, sizeof(DVR_WrapperInfo_t));
1896 }
hualing chen002e5b92022-02-23 17:51:21 +08001897 error = dvr_wrapper_start_playback(playback, flags, p_pids);
1898 return error;
1899}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001900
1901int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
1902{
1903 DVR_WrapperCtx_t *ctx;
1904 int error;
1905
1906 DVR_RETURN_IF_FALSE(playback);
1907
1908 ctx = ctx_getPlayback((unsigned long)playback);
1909 DVR_RETURN_IF_FALSE(ctx);
1910
1911 pthread_mutex_lock(&ctx->lock);
1912 DVR_WRAPPER_DEBUG(1, "stop playback(sn:%ld) ...\n", ctx->sn);
1913 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1914
1915 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1916
1917 {
1918 /*remove all segments*/
1919 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1920
1921 list_for_each_entry(pseg, &ctx->segments, head) {
1922 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1923 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1924 ctx->sn, pseg->playback_info.segment_id, error);
1925 }
1926 ctx_freeSegments(ctx);
1927 }
1928
1929 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
1930 pthread_mutex_unlock(&ctx->lock);
1931
1932 return error;
1933}
1934
1935int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
1936{
1937 DVR_WrapperCtx_t *ctx;
1938 int error;
1939
1940 DVR_RETURN_IF_FALSE(playback);
1941
1942 ctx = ctx_getPlayback((unsigned long)playback);
1943 DVR_RETURN_IF_FALSE(ctx);
1944
1945 pthread_mutex_lock(&ctx->lock);
1946 DVR_WRAPPER_DEBUG(1, "pause playback(sn:%ld) ...\n", ctx->sn);
1947 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08001948 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08001949 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08001950 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001951
1952 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
1953
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001954 ctx->playback.speed = 0.0f;
1955
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001956 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) paused (%d)\n", ctx->sn, error);
1957 pthread_mutex_unlock(&ctx->lock);
1958
1959 return error;
1960}
1961
1962int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
1963{
1964 DVR_WrapperCtx_t *ctx;
1965 int error;
1966
1967 DVR_RETURN_IF_FALSE(playback);
1968
1969 ctx = ctx_getPlayback((unsigned long)playback);
1970 DVR_RETURN_IF_FALSE(ctx);
hualing chen03fd4942021-07-15 15:56:41 +08001971 //if set limit.we need check if seek to valid data when resume
1972 uint32_t time_offset = ctx->playback.status.info_cur.time + ctx->playback.status.info_obsolete.time;
1973 if (dvr_playback_check_limit(ctx->playback.player)) {
1974 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
1975 if (expired > time_offset) {
1976 DVR_WRAPPER_DEBUG(1, "seek before resume reset offset playback(sn:%ld) (off:%d expired:%d)\n",
1977 ctx->sn, time_offset, expired);
1978 time_offset = expired;
1979 dvr_wrapper_seek_playback(playback, time_offset);
1980 }
1981 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001982 pthread_mutex_lock(&ctx->lock);
1983 DVR_WRAPPER_DEBUG(1, "resume playback(sn:%ld) ...\n", ctx->sn);
1984 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1985
1986 error = dvr_playback_resume(ctx->playback.player);
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001987 ctx->playback.speed = 100.0f;
1988
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001989 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
1990 pthread_mutex_unlock(&ctx->lock);
1991
1992 return error;
1993}
1994
1995int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
1996{
1997 DVR_WrapperCtx_t *ctx;
1998 int error;
1999 DVR_PlaybackSpeed_t dvr_speed = {
2000 .speed = { speed },
2001 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
2002 };
2003
2004 DVR_RETURN_IF_FALSE(playback);
2005
2006 ctx = ctx_getPlayback((unsigned long)playback);
2007 DVR_RETURN_IF_FALSE(ctx);
2008
2009 pthread_mutex_lock(&ctx->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08002010 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 +08002011 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2012
2013 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
2014
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08002015 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
2016 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
2017 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
2018 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
2019 error = dvr_playback_resume(ctx->playback.player);
2020 } else if (ctx->playback.speed == 0.0f
2021 && speed != 0.0f
2022 && speed != 100.0f) {
2023 /*libdvr do not support pause with speed=0, will not be here*/
2024 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
2025 error = dvr_playback_resume(ctx->playback.player);
2026 }
2027
2028 ctx->playback.speed = speed;
2029
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002030 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) speeded(x%f) (%d)\n",
2031 ctx->sn, speed, error);
2032 pthread_mutex_unlock(&ctx->lock);
2033
2034 return error;
2035}
2036
hualing chen03fd4942021-07-15 15:56:41 +08002037int dvr_wrapper_setlimit_playback (DVR_WrapperPlayback_t playback, uint64_t time, int32_t limit)
2038{
2039 DVR_WrapperCtx_t *ctx;
2040 int error;
2041
2042 DVR_RETURN_IF_FALSE(playback);
2043
2044 ctx = ctx_getPlayback((unsigned long)playback);
2045 DVR_RETURN_IF_FALSE(ctx);
2046
2047 pthread_mutex_lock(&ctx->lock);
2048
2049 DVR_WRAPPER_DEBUG(1, "setlimit playback(sn:%ld) (time:%lld limit:%d) ...\n", ctx->sn, time, limit);
2050 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2051
2052 error = dvr_playback_setlimit(ctx->playback.player, time, limit);
2053 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) setlimit(time:%lld limit:%d) ...\n", ctx->sn, time, limit);
2054
2055 pthread_mutex_unlock(&ctx->lock);
2056
2057 return error;
2058}
2059
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002060int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
2061{
2062 DVR_WrapperCtx_t *ctx;
2063 int error;
2064 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2065 uint64_t segment_id;
2066 uint32_t off;
2067 uint64_t last_segment_id;
2068 uint32_t pre_off;
2069
2070 DVR_RETURN_IF_FALSE(playback);
2071
2072 ctx = ctx_getPlayback((unsigned long)playback);
2073 DVR_RETURN_IF_FALSE(ctx);
2074
2075 pthread_mutex_lock(&ctx->lock);
2076
2077 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
2078 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2079
2080 off = 0;
2081 segment_id = 0;
2082 pre_off = 0;
2083 last_segment_id = 0;
2084
hualing chen03fd4942021-07-15 15:56:41 +08002085 //if set limit info we need check ts data is
2086 //expired when seek
2087 if (dvr_playback_check_limit(ctx->playback.player)) {
2088 int expired = dvr_playback_calculate_expiredlen(ctx->playback.player);
2089 if (expired > time_offset) {
2090 DVR_WRAPPER_DEBUG(1, "seek reset offset playback(sn:%ld) (off:%d expired:%d)\n",
2091 ctx->sn, time_offset, expired);
2092 time_offset = expired;
2093 }
2094 }
2095
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002096 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2097 segment_id = pseg->seg_info.id;
2098
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002099 if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset)
2100 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002101
2102 last_segment_id = pseg->seg_info.id;
2103 pre_off += pseg->seg_info.duration;
2104 }
2105
2106 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002107 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
2108 off = time_offset;
2109 } else if (ctx->playback.obsolete.time >= time_offset) {
2110 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002111 } else {
hualing chenda76fc52020-05-28 14:56:42 +08002112 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002113 }
2114
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002115 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (seg:%lld, off:%d)\n",
2116 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002117 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
2118 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
2119
2120 pthread_mutex_unlock(&ctx->lock);
2121
2122 return error;
2123}
2124
2125int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2126{
2127 DVR_WrapperCtx_t *ctx;
2128 int error;
2129 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2130
2131 DVR_RETURN_IF_FALSE(playback);
2132 DVR_RETURN_IF_FALSE(p_pids);
2133
2134 ctx = ctx_getPlayback((unsigned long)playback);
2135 DVR_RETURN_IF_FALSE(ctx);
2136
2137 pthread_mutex_lock(&ctx->lock);
2138
2139 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
2140 ctx->sn,
2141 p_pids->video.pid, p_pids->video.format,
2142 p_pids->audio.pid, p_pids->audio.format);
2143 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2144
2145 ctx->playback.pids_req = *p_pids;
2146
2147 error = 0;
2148 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2149 /*should update the whole list of segments*/
2150 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
2151 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
2152 /*check udpate for pids*/
2153 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2154 pseg->playback_info.pids = *p_pids;
2155 error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
2156 if (error) {
2157 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
2158 ctx->sn, pseg->seg_info.id, error);
2159 /*do not break, let list updated*/
2160 }
2161 }
2162 }
2163 /*break;*/
2164 }
2165 }
2166
2167 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
2168 ctx->sn,
2169 p_pids->video.pid, p_pids->video.format,
2170 p_pids->audio.pid, p_pids->audio.format,
2171 error);
2172
2173 pthread_mutex_unlock(&ctx->lock);
2174
2175 return error;
2176}
2177
hualing chena5f03222021-12-02 11:22:35 +08002178int dvr_wrapper_only_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
2179{
2180 DVR_WrapperCtx_t *ctx;
2181 int error;
2182 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2183
2184 DVR_RETURN_IF_FALSE(playback);
2185 DVR_RETURN_IF_FALSE(p_pids);
2186
2187 ctx = ctx_getPlayback((unsigned long)playback);
2188 DVR_RETURN_IF_FALSE(ctx);
2189
2190 pthread_mutex_lock(&ctx->lock);
2191
2192 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
2193 ctx->sn,
2194 p_pids->video.pid, p_pids->video.format,
2195 p_pids->audio.pid, p_pids->audio.format);
2196 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2197
2198 ctx->playback.pids_req = *p_pids;
2199
2200 error = 0;
2201 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2202 /*should update the whole list of segments*/
2203 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
2204 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
2205 /*check udpate for pids*/
2206 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
2207 pseg->playback_info.pids = *p_pids;
2208 error = dvr_playback_only_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
2209 if (error) {
2210 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
2211 ctx->sn, pseg->seg_info.id, error);
2212 /*do not break, let list updated*/
2213 }
2214 }
2215 }
2216 /*break;*/
2217 }
2218 }
2219
2220 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
2221 ctx->sn,
2222 p_pids->video.pid, p_pids->video.format,
2223 p_pids->audio.pid, p_pids->audio.format,
2224 error);
2225
2226 pthread_mutex_unlock(&ctx->lock);
2227
2228 return error;
2229}
2230
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002231int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
2232{
2233 DVR_WrapperCtx_t *ctx;
2234 DVR_WrapperPlaybackStatus_t s;
2235 DVR_PlaybackStatus_t play_status;
2236 int error;
2237
2238 DVR_RETURN_IF_FALSE(playback);
2239 DVR_RETURN_IF_FALSE(status);
2240
2241 ctx = ctx_getPlayback((unsigned long)playback);
2242 DVR_RETURN_IF_FALSE(ctx);
2243
2244 pthread_mutex_lock(&ctx->lock);
2245
2246 DVR_WRAPPER_DEBUG(1, "get playback(sn:%ld) status ...\n", ctx->sn);
2247 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
2248
2249 error = dvr_playback_get_status(ctx->playback.player, &play_status);
2250 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) get status (%d)\n", ctx->sn, error);
2251
2252 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002253 error = process_generatePlaybackStatus(ctx, &s);
2254
hualing chenb5cd42e2020-04-15 17:03:34 +08002255 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
2256 //reach end need set full time to cur.so app can exist playback.
2257 DVR_WRAPPER_DEBUG(1, "set cur time to full time, reach end occur");
2258 s.info_cur.time = s.info_full.time;
2259 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002260 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002261 ctx->sn,
2262 s.state,
2263 s.info_cur.time,
2264 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002265 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002266 error);
2267
2268 *status = s;
2269
2270 pthread_mutex_unlock(&ctx->lock);
2271
2272 return error;
2273}
2274
hualing chen266b9502020-04-04 17:39:39 +08002275int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
2276{
2277 DVR_WrapperCtx_t *ctx;
2278 int error;
2279
2280 DVR_RETURN_IF_FALSE(playback);
2281 DVR_RETURN_IF_FALSE(p_secure_buf);
2282
2283 ctx = ctx_getPlayback((unsigned long)playback);
2284 DVR_RETURN_IF_FALSE(ctx);
2285
2286 pthread_mutex_lock(&ctx->lock);
2287 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
2288 pthread_mutex_unlock(&ctx->lock);
2289 return error;
2290}
2291
2292int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
2293{
2294 DVR_WrapperCtx_t *ctx;
2295 int error;
2296
2297 DVR_RETURN_IF_FALSE(playback);
2298 DVR_RETURN_IF_FALSE(func);
2299
2300 ctx = ctx_getPlayback((unsigned long)playback);
2301 DVR_RETURN_IF_FALSE(ctx);
2302
2303 pthread_mutex_lock(&ctx->lock);
2304 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
2305 pthread_mutex_unlock(&ctx->lock);
2306 return error;
2307}
2308
Zhiqiang Han620b9252021-11-09 14:23:20 +08002309int dvr_wrapper_segment_del_by_location (const char *location)
2310{
2311 char fpath[DVR_MAX_LOCATION_SIZE];
2312
2313 DVR_RETURN_IF_FALSE(location);
2314
2315 /*del the stats file*/
2316 sprintf(fpath, "%s.stats", location);
2317 unlink(fpath);
2318
2319 return dvr_segment_del_by_location(location);
2320}
2321
2322int dvr_wrapper_segment_get_info_by_location (const char *location, DVR_WrapperInfo_t *p_info)
2323{
2324 FILE *fp;
2325 char fpath[DVR_MAX_LOCATION_SIZE];
2326
2327 DVR_RETURN_IF_FALSE(location);
2328 DVR_RETURN_IF_FALSE(p_info);
2329
2330 if (p_info)
2331 memset(p_info, 0, sizeof(p_info[0]));
2332
2333 memset(fpath, 0, sizeof(fpath));
2334 sprintf(fpath, "%s.stats", location);
2335
2336 /*stats file exists*/
2337 if ((fp = fopen(fpath, "r"))) {
2338 char buf[256];
2339
2340 if (fgets(buf, sizeof(buf), fp) != NULL
2341 && (sscanf(buf, ":%llu:%lu:%u",
2342 &p_info->size,
2343 &p_info->time,
2344 &p_info->pkts) == 3)) {
2345 fclose(fp);
2346 DVR_WRAPPER_DEBUG(1, "rec(%s) t/s/p:(%lu/%llu/%u)\n", location, p_info->time, p_info->size, p_info->pkts);
2347 return DVR_SUCCESS;
2348 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002349 fclose(fp);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002350 }
2351
2352 /*fallback, slow on mass files*/
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002353 DVR_WRAPPER_DEBUG(1, "rec '%s.stats' invalid.\n", location);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002354
2355 int error;
2356 uint32_t n_ids;
2357 uint64_t *p_ids;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002358
hualing chen8aed9582021-12-24 17:59:56 +08002359 error = dvr_segment_get_list(location, &n_ids, &p_ids);
hualing chenb9a02922021-12-14 11:29:47 +08002360
Zhiqiang Han620b9252021-11-09 14:23:20 +08002361 if (!error) {
2362 int i;
hualing chenb9a02922021-12-14 11:29:47 +08002363 struct list_head info_list; /**< segment list head*/
2364 INIT_LIST_HEAD(&info_list);
2365
2366 //we need free info list buf when we used end.
hualing chen8aed9582021-12-24 17:59:56 +08002367 error = dvr_segment_get_allInfo(location, &info_list);
2368 if (error == DVR_FAILURE) {
hualing chenb9a02922021-12-14 11:29:47 +08002369 DVR_RecordSegmentInfo_t info;
2370
2371 memset(&info, 0, sizeof(info));
2372 error = DVR_FAILURE;
2373 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
hualing chen8aed9582021-12-24 17:59:56 +08002374 location, 0, error);
hualing chenb9a02922021-12-14 11:29:47 +08002375
2376 for (i = 0; i < n_ids; i++) {
hualing chen8aed9582021-12-24 17:59:56 +08002377 error = dvr_segment_get_info(location, p_ids[i], &info);
hualing chenb9a02922021-12-14 11:29:47 +08002378 if (!error) {
2379 p_info->size += info.size;
2380 p_info->time += info.duration;
2381 p_info->pkts += info.nb_packets;
2382 } else {
hualing chen8aed9582021-12-24 17:59:56 +08002383 DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002384 break;
2385 }
2386 }
2387 } else {
2388 DVR_WRAPPER_DEBUG(1, "get list segment_nb::%d",n_ids);
2389 for (i = 0; i < n_ids; i++) {
2390
2391 DVR_RecordSegmentInfo_t *seg_info;
2392 DVR_PlaybackSegmentFlag_t flags;
2393 int found = 0;
2394 list_for_each_entry(seg_info, &info_list, head)
2395 {
hualing chen8aed9582021-12-24 17:59:56 +08002396 if (seg_info->id == p_ids[i]) {
hualing chenb9a02922021-12-14 11:29:47 +08002397 found = 1;
2398 break;
2399 }
2400 }
2401 if (!found) {
hualing chen8aed9582021-12-24 17:59:56 +08002402 DVR_WRAPPER_DEBUG(1, "get segment info::%d [%d]n_ids[%d]error", i, p_ids[i], n_ids);
2403 if (p_ids[i] == n_ids - 1) {
2404 DVR_RecordSegmentInfo_t info;
2405 DVR_WRAPPER_DEBUG(1, "get last segment info::%d [%d]n_ids[%d] from subfile", i, p_ids[i], n_ids);
2406 error = dvr_segment_get_info(location, p_ids[i], &info);
2407 if (!error) {
2408 p_info->size += info.size;
2409 p_info->time += info.duration;
2410 p_info->pkts += info.nb_packets;
2411 } else {
2412 DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
2413 break;
2414 }
2415 }
hualing chenb9a02922021-12-14 11:29:47 +08002416 continue;
2417 }
2418
2419 if (!error) {
2420 p_info->size += seg_info->size;
2421 p_info->time += seg_info->duration;
2422 p_info->pkts += seg_info->nb_packets;
2423 } else {
hualing chen8aed9582021-12-24 17:59:56 +08002424 DVR_WRAPPER_DEBUG(1, "%s:%lld get seg info fail.\n", location, p_ids[i]);
hualing chenb9a02922021-12-14 11:29:47 +08002425 break;
2426 }
2427 }
2428 //free list
2429 DVR_RecordSegmentInfo_t *segment = NULL;
2430 DVR_RecordSegmentInfo_t *segment_tmp = NULL;
2431 list_for_each_entry_safe(segment, segment_tmp, &info_list, head)
2432 {
2433 if (segment) {
2434 list_del(&segment->head);
2435 free(segment);
2436 }
2437 }
2438 }
2439 free(p_ids);
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002440 } else {
2441 n_ids = 0;
Zhiqiang Han620b9252021-11-09 14:23:20 +08002442 }
Zhiqiang Hanb9785922021-11-26 18:47:39 +08002443 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 +08002444
2445 return (error)? DVR_FAILURE : DVR_SUCCESS;
2446}
2447
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002448static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
2449{
2450 DVR_WrapperEventCtx_t evt;
2451
2452 DVR_RETURN_IF_FALSE(userdata);
2453
2454 evt.sn = (unsigned long)userdata;
2455 evt.type = W_REC;
2456 evt.record.event = event;
2457 evt.record.status = *(DVR_RecordStatus_t *)params;
2458 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
2459 return ctx_addRecordEvent(&evt);
2460}
2461
2462static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
2463{
2464 DVR_WrapperEventCtx_t evt;
2465
2466 DVR_RETURN_IF_FALSE(userdata);
2467
2468 evt.sn = (unsigned long)userdata;
2469 evt.type = W_PLAYBACK;
2470 evt.playback.event = event;
2471 evt.playback.status = *(DVR_Play_Notify_t *)params;
2472 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event);
2473 return ctx_addPlaybackEvent(&evt);
2474}
2475
2476static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
2477{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002478 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 +08002479 ctx->sn,
2480 evt,
2481 status->info.time,
2482 status->info.size,
2483 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002484 status->info_obsolete.time,
2485 status->info_obsolete.size,
2486 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002487
2488 if (ctx->record.event_fn)
2489 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
2490 return 0;
2491}
2492
Zhiqiang Han620b9252021-11-09 14:23:20 +08002493static int wrapper_saveRecordStatistics(const char *location, DVR_WrapperRecordStatus_t *p_status)
2494{
2495 FILE *fp;
2496 char fpath[DVR_MAX_LOCATION_SIZE];
2497
2498 DVR_RETURN_IF_FALSE(location);
2499 DVR_RETURN_IF_FALSE(p_status);
2500
2501 sprintf(fpath, "%s.stats", location);
2502
2503 /*stats file*/
2504 if ((fp = fopen(fpath, "w"))) {
2505 char buf[256];
2506 snprintf(buf, sizeof(buf), ":%llu:%lu:%u\n",
2507 p_status->info.size - p_status->info_obsolete.size,
2508 p_status->info.time - p_status->info_obsolete.time,
2509 p_status->info.pkts - p_status->info_obsolete.pkts);
2510 fputs(buf, fp);
2511 fclose(fp);
2512 return DVR_SUCCESS;
2513 }
2514
2515 return DVR_FAILURE;
2516}
2517
2518
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002519static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
2520{
2521 DVR_RecordStartParams_t param;
2522 DVR_RecordSegmentInfo_t seg_info;
2523 int i;
2524 int error;
2525
2526 memcpy(&param, &ctx->record.param_update, sizeof(param));
2527 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
2528 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
2529 for (i = 0; i < param.segment.nb_pids; i++) {
2530 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
2531 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
2532 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
2533 ctx->record.param_update.segment.nb_pids++;
2534 }
2535 }
2536 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
2537 {
2538 DVR_RecordSegmentInfo_t new_seg_info =
2539 { .id = ctx->record.param_update.segment.segment_id, };
2540 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
2541 wrapper_addRecordSegment(ctx, &new_seg_info);
2542 }
2543
Zhiqiang Hand977e972020-05-11 11:30:47 +08002544 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 +08002545 return error;
2546}
2547
2548static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg)
2549{
2550 return wrapper_removeRecordSegment(ctx, pseg);
2551}
2552
2553/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002554static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002555{
2556 /*the current seg is not covered in the statistics*/
2557 DVR_WrapperRecordSegmentInfo_t *pseg;
2558
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002559 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002560 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
2561
2562 ctx->record.status.state = ctx->record.seg_status.state;
2563 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
2564 memcpy(ctx->record.status.pids.pids,
2565 ctx->record.seg_status.info.pids,
2566 sizeof(ctx->record.status.pids.pids));
2567 ctx->current_segment_id = ctx->record.seg_status.info.id;
2568
2569 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2570 if (pseg->info.id != ctx->record.seg_status.info.id) {
2571 ctx->record.status.info.time += pseg->info.duration;
2572 ctx->record.status.info.size += pseg->info.size;
2573 ctx->record.status.info.pkts += pseg->info.nb_packets;
2574 }
2575 }
2576
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002577 ctx->record.status.info_obsolete = ctx->record.obsolete;
2578
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002579 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002580
2581 if (status) {
2582 *status = ctx->record.status;
2583 status->info.time += ctx->record.seg_status.info.duration;
2584 status->info.size += ctx->record.seg_status.info.size;
2585 status->info.pkts += ctx->record.seg_status.info.nb_packets;
2586 }
2587
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002588 return DVR_SUCCESS;
2589}
2590
2591
2592static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2593{
2594 DVR_WrapperRecordStatus_t status;
2595
2596 memset(&status, 0, sizeof(status));
2597
2598 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d)\n",
2599 evt->sn, evt->record.event, evt->record.status.state);
hualing chend3b55ab2021-05-06 09:56:27 +08002600 if (ctx->record.param_update.segment.segment_id != evt->record.status.info.id) {
2601 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) cur id:0x%x (event id:%d)\n",
Gong Ke2a0ebbe2021-05-25 15:22:50 +08002602 evt->sn, (int)ctx->record.param_update.segment.segment_id, (int)evt->record.status.info.id);
hualing chend3b55ab2021-05-06 09:56:27 +08002603 return 0;
2604 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002605 switch (evt->record.event)
2606 {
2607 case DVR_RECORD_EVENT_STATUS:
2608 {
2609 switch (evt->record.status.state)
2610 {
2611 case DVR_RECORD_STATE_OPENED:
2612 case DVR_RECORD_STATE_CLOSED:
2613 {
2614 ctx->record.seg_status = evt->record.status;
2615
2616 status.state = evt->record.status.state;
2617 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002618 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002619 } break;
2620 case DVR_RECORD_STATE_STARTED:
2621 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002622 ctx->record.seg_status = evt->record.status;
2623
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002624 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002625 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002626 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002627
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002628 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002629 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002630 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
2631 DVR_WRAPPER_DEBUG(1, "start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n",
2632 ctx->sn,
2633 evt->record.status.info.size,
2634 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002635 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
2636 /*should notify the recording's stop*/
2637 int error = dvr_record_close(ctx->record.recorder);
2638 DVR_WRAPPER_DEBUG(1, "stop record(%lu)=%d, failed to start new segment for recording.",
2639 ctx->sn, error);
2640 status.state = DVR_RECORD_STATE_CLOSED;
2641 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002642 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hand977e972020-05-11 11:30:47 +08002643 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002644 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002645
2646 if (ctx->record.param_open.is_timeshift
2647 && ctx->record.param_open.max_time
2648 && status.info.time >= ctx->record.param_open.max_time) {
2649 DVR_WrapperRecordSegmentInfo_t *pseg;
2650
2651 /*as the player do not support null playlist,
2652 there must be one segment existed at any time,
2653 we have to keep two segments before remove one*/
2654 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2655 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
2656 /*only one segment, waiting for more*/
2657 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n",
2658 status.info.size,
2659 ctx->record.param_open.max_time,
2660 ctx->record.param_open.segment_size);
2661 } else {
2662 /*timeshifting, remove the 1st segment and notify the player*/
2663 record_removeSegment(ctx, pseg);
2664
2665 process_generateRecordStatus(ctx, &status);
2666 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002667 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002668 }
2669 }
2670
2671 if (ctx->record.param_open.is_timeshift
2672 && ctx->record.param_open.max_size
2673 && status.info.size >= ctx->record.param_open.max_size) {
2674 DVR_WrapperRecordSegmentInfo_t *pseg;
2675
2676 /*as the player do not support null playlist,
2677 there must be one segment existed at any time,
2678 we have to keep two segments before remove one*/
2679 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2680 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
2681 /*only one segment, waiting for more*/
2682 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of record < max size of segment(%lld)\n",
2683 status.info.size,
2684 ctx->record.param_open.segment_size);
2685 } else {
2686 record_removeSegment(ctx, pseg);
2687
2688 process_generateRecordStatus(ctx, &status);
2689 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002690 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002691 }
2692 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002693 } break;
2694 case DVR_RECORD_STATE_STOPPED:
2695 {
2696 ctx->record.seg_status = evt->record.status;
2697
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002698 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002699 process_notifyRecord(ctx, evt->record.event, &status);
Zhiqiang Han620b9252021-11-09 14:23:20 +08002700 wrapper_saveRecordStatistics(ctx->record.param_open.location, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002701 } break;
2702 default:
2703 break;
2704 }
2705 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08002706 case DVR_RECORD_EVENT_WRITE_ERROR: {
2707 ctx->record.seg_status = evt->record.status;
2708 status.state = evt->record.status.state;
2709 process_notifyRecord(ctx, evt->record.event, &status);
2710 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002711 default:
2712 break;
2713 }
2714 return DVR_SUCCESS;
2715}
2716
2717static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
2718{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002719 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 +08002720 ctx->sn,
2721 evt,
2722 status->state,
2723 status->info_cur.time,
2724 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002725 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002726
2727 if (ctx->playback.event_fn)
2728 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
2729 return 0;
2730}
2731
2732/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002733static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002734{
2735 /*the current seg is not covered in the statistics*/
2736 DVR_WrapperPlaybackSegmentInfo_t *pseg;
2737
2738 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
2739 ctx->playback.status.pids = ctx->playback.pids_req;
2740
2741 ctx->playback.status.state = ctx->playback.seg_status.state;
2742 ctx->playback.status.speed = ctx->playback.seg_status.speed;
2743 ctx->playback.status.flags = ctx->playback.seg_status.flags;
2744 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
2745
2746 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
hualing chen451c8f72022-03-09 13:05:52 +08002747 if (pseg->seg_info.id == ctx->playback.seg_status.segment_id) {
2748 DVR_WRAPPER_DEBUG(1, "caulate cur time :[%lld]time[%d]\n", pseg->seg_info.id, pseg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002749 break;
hualing chen451c8f72022-03-09 13:05:52 +08002750 }
2751
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002752 ctx->playback.status.info_cur.time += pseg->seg_info.duration;
2753 ctx->playback.status.info_cur.size += pseg->seg_info.size;
2754 ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets;
2755 }
2756 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2757 ctx->playback.status.info_full.time += pseg->seg_info.duration;
2758 ctx->playback.status.info_full.size += pseg->seg_info.size;
2759 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
hualing chen451c8f72022-03-09 13:05:52 +08002760 DVR_WRAPPER_DEBUG(1, "caulate full time :[%lld]time[%d]\n", pseg->seg_info.id, pseg->seg_info.duration);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002761 }
2762
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002763 if (status) {
2764 *status = ctx->playback.status;
2765 /*deal with current, lack size and pkts with the current*/
2766 status->info_cur.time += ctx->playback.seg_status.time_cur;
hualing chen56c0a162022-01-27 17:01:50 +08002767 //get last segment id
2768 DVR_WrapperRecordSegmentInfo_t *pseg;
2769
2770 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
2771 if (ctx->playback.tf_full == DVR_TRUE && pseg->info.id == ctx->current_segment_id) {
2772 status->disguised_info_obsolete.time = ctx->playback.obsolete.time + ctx->playback.seg_status.time_cur;
2773 status->info_obsolete.time = ctx->playback.obsolete.time;
2774 DVR_WRAPPER_DEBUG(1, "warning change start time :id[%lld] [%d]cur[%d]\n", pseg->info.id, status->info_obsolete.time, status->info_cur.time);
2775 }
2776 else
2777 {
2778 DVR_WRAPPER_DEBUG(1, "warning not change start time :ctx->playback.tf_full[%d]id[%lld] [%lld] cur[%d]\n",ctx->playback.tf_full , pseg->info.id, ctx->current_segment_id, status->info_cur.time);
2779 status->info_obsolete.time = ctx->playback.obsolete.time;
2780 status->disguised_info_obsolete.time = ctx->playback.obsolete.time;
2781 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002782 }
2783
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002784 return DVR_SUCCESS;
2785}
2786
2787static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2788{
2789 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
2790 evt->sn, evt->playback.event,
2791 evt->playback.status.play_status.state,
2792 evt->playback.status.play_status.segment_id,
2793 evt->playback.status.play_status.time_cur,
2794 evt->playback.status.play_status.time_end);
2795
2796 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002797 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2798 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2799 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2800 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002801 ctx->playback.last_event = evt->playback.event;
2802
2803 switch (evt->playback.event)
2804 {
2805 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2806 case DVR_PLAYBACK_EVENT_REACHED_END:
2807 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2808 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002809 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002810 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002811 case DVR_PLAYBACK_EVENT_NODATA:
2812 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002813 {
2814 DVR_WrapperPlaybackStatus_t status;
2815
2816 /*copy status of segment*/
2817 ctx->playback.seg_status = evt->playback.status.play_status;
2818
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002819 /*generate status of the whole playback*/
2820 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002821
2822 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
Zhiqiang Han31846002021-11-04 10:49:06 +08002823 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) event:0x%x\n", evt->sn, evt->playback.event);
hualing chenb9b358a2021-08-17 15:06:36 +08002824 if (ctx->playback.param_open.is_timeshift
2825 || ctx_isPlay_recording(ctx->playback.param_open.location)) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002826 /*wait for more data in recording*/
Zhiqiang Han31846002021-11-04 10:49:06 +08002827 }
2828 /*trust the low level, make NO check.
2829 As this evt is changed to only once due to some operations(paused) in low level.
2830 else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002831 process_notifyPlayback(ctx, evt->playback.event, &status);
Zhiqiang Han31846002021-11-04 10:49:06 +08002832 }
2833 */
2834 else {
2835 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002836 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002837 }
hualing chenf291cf32020-06-18 10:50:30 +08002838 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002839 process_notifyPlayback(ctx, evt->playback.event, &status);
2840 }
2841 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002842 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2843 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2844 case DVR_PLAYBACK_EVENT_NO_KEY:
2845 {
2846 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
2847 } break;
2848 default:
2849 {
2850 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
2851 } break;
2852 }
2853 return 0;
2854}
2855
2856static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2857{
2858 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2859}
2860