blob: b9340ac31e44d74159a5e9a1987512dbd900ac2f [file] [log] [blame]
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001#include <stddef.h>
2#include <stdlib.h>
3#include <pthread.h>
4#include <string.h>
5#include <time.h>
6#include <errno.h>
7
8#include "dvr_types.h"
9#include "dvr_record.h"
10#include "dvr_crypto.h"
11#include "dvr_playback.h"
12#include "dvr_segment.h"
13
14#include "AmTsPlayer.h"
15
16#include "list.h"
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080017
18#include "dvr_wrapper.h"
19
20#define DVR_WRAPPER_DEBUG(_level, _fmt, ...) \
21 DVR_DEBUG(_level, "wrapper %-30.30s:%d " _fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
22
23/*duration of data to resume if paused with EVT_REACHED_END in timeshifting*/
hualing chen2932d372020-04-29 13:44:00 +080024#define TIMESHIFT_DATA_DURATION_TO_RESUME (600)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080025/*a tolerant gap*/
26#define DVR_PLAYBACK_END_GAP (1000)
27
28enum {
29 W_REC = 1,
30 W_PLAYBACK = 2,
31};
32
33enum {
34 U_PIDS = 0x01,
35 U_STAT = 0x02,
36 U_ALL = U_PIDS | U_STAT,
37};
38
39typedef struct {
40 /*make lock the 1st item in the structure*/
41 pthread_mutex_t lock;
42
43 /*rec or play*/
44 int type;
45
46 /*valid if (sn != 0)*/
47 unsigned long sn;
48 unsigned long sn_linked;
49
50 struct list_head segments; /**<head-add list*/
51 uint64_t current_segment_id; /**<id of the current segment*/
52
53 union {
54 struct {
55 DVR_WrapperRecordOpenParams_t param_open;
56 DVR_RecordStartParams_t param_start;
57 DVR_RecordStartParams_t param_update;
58 DVR_RecordHandle_t recorder;
59 DVR_RecordEventFunction_t event_fn;
60 void *event_userdata;
61
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +080062 /*total status = seg_status + status + obsolete*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080063 DVR_RecordStatus_t seg_status; /**<status of current segment*/
64 DVR_WrapperRecordStatus_t status; /**<status of remaining segments*/
65 uint64_t next_segment_id;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +080066
67 DVR_WrapperInfo_t obsolete; /**<data obsolete due to the max limit*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080068 } record;
69
70 struct {
71 DVR_WrapperPlaybackOpenParams_t param_open;
72 DVR_PlaybackHandle_t player;
73 DVR_PlaybackEventFunction_t event_fn;
74 void *event_userdata;
75
76 /*total status = seg_status + status*/
77 DVR_PlaybackStatus_t seg_status;
78 DVR_WrapperPlaybackStatus_t status;
79 DVR_PlaybackPids_t pids_req;
80 DVR_PlaybackEvent_t last_event;
Zhiqiang Han3eb75f92020-04-08 10:07:55 +080081 float speed;
hualing chenb5cd42e2020-04-15 17:03:34 +080082 DVR_Bool_t reach_end;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +080083
84 DVR_WrapperInfo_t obsolete;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +080085 } playback;
86 };
87} DVR_WrapperCtx_t;
88
89typedef struct {
90 struct list_head head;
91 unsigned long sn;
92
93 /* rec or playback */
94 int type;
95
96 union {
97 struct {
98 DVR_RecordEvent_t event;
99 DVR_RecordStatus_t status;
100 } record;
101 struct {
102 DVR_PlaybackEvent_t event;
103 DVR_Play_Notify_t status;
104 } playback;
105 };
106} DVR_WrapperEventCtx_t;
107
108typedef struct {
109 pthread_mutex_t lock;
110 char *name;
111 int running;
112 pthread_cond_t cond;
113 pthread_t thread;
114 int type;
115} DVR_WrapperThreadCtx_t;
116
117typedef struct {
118 struct list_head head;
119
120 DVR_RecordSegmentInfo_t seg_info;
121 DVR_PlaybackSegmentInfo_t playback_info;
122} DVR_WrapperPlaybackSegmentInfo_t;
123
124typedef struct {
125 struct list_head head;
126
127 DVR_RecordSegmentInfo_t info;
128} DVR_WrapperRecordSegmentInfo_t;
129
130/* serial num generater */
131static unsigned long sn = 1;
132static pthread_mutex_t sn_lock = PTHREAD_MUTEX_INITIALIZER;
133
134static inline unsigned long get_sn()
135{
136 unsigned long no;
137
138 pthread_mutex_lock(&sn_lock);
139 no = sn++;
140 if (!no)
141 no = sn++;
142 pthread_mutex_unlock(&sn_lock);
143
144 return no;
145}
146
147/* entity ctx */
148#define DVR_WRAPPER_MAX 10
149
150static DVR_WrapperCtx_t record_list[DVR_WRAPPER_MAX] =
151{
152 [0 ... (DVR_WRAPPER_MAX - 1)] =
153 {
154 .lock = PTHREAD_MUTEX_INITIALIZER,
155 .type = W_REC,
156 }
157};
158
159static DVR_WrapperCtx_t playback_list[DVR_WRAPPER_MAX] =
160{
161 [0 ... (DVR_WRAPPER_MAX - 1)] =
162 {
163 .lock = PTHREAD_MUTEX_INITIALIZER,
164 .type = W_PLAYBACK,
165 }
166};
167
168/* events lists */
169static struct list_head record_evt_list = LIST_HEAD_INIT(record_evt_list);
170static struct list_head playback_evt_list = LIST_HEAD_INIT(playback_evt_list);
171
172static pthread_mutex_t record_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
173static pthread_mutex_t playback_evt_list_lock = PTHREAD_MUTEX_INITIALIZER;
174
175static DVR_WrapperThreadCtx_t wrapper_thread[2] =
176{
177 [0] =
178 {
179 .lock = PTHREAD_MUTEX_INITIALIZER,
180 .running = 0,
181 .name = "record",
182 .type = W_REC,
183 },
184 [1] =
185 {
186 .lock = PTHREAD_MUTEX_INITIALIZER,
187 .running = 0,
188 .name = "playback",
189 .type = W_PLAYBACK,
190 },
191};
192
193/*now only support one timeshift now*/
194static unsigned long sn_timeshift_record;
195static unsigned long sn_timeshift_playback;
196
197static void *wrapper_task(void *arg);
198static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx);
199
200static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata);
201static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata);
202
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800203static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status);
204static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800205
206
207static DVR_WrapperEventCtx_t *ctx_getEvent(struct list_head *list, pthread_mutex_t *list_lock)
208{
209 DVR_WrapperEventCtx_t *pevt;
210
211 pthread_mutex_lock(list_lock);
212 if (list_empty(list))
213 pevt = NULL;
214 else {
215 pevt = list_first_entry(list, DVR_WrapperEventCtx_t, head);
216 list_del(&pevt->head);
217 }
218 pthread_mutex_unlock(list_lock);
219
220 return pevt;
221}
222
223static inline DVR_WrapperEventCtx_t *ctx_getRecordEvent()
224{
225 return ctx_getEvent(&record_evt_list, &record_evt_list_lock);
226}
227
228static inline DVR_WrapperEventCtx_t *ctx_getPlaybackEvent()
229{
230 return ctx_getEvent(&playback_evt_list, &playback_evt_list_lock);
231}
232
233static int ctx_addEvent(struct list_head *list, pthread_mutex_t *lock, DVR_WrapperEventCtx_t *evt)
234{
235 DVR_WrapperEventCtx_t *padd;
236 padd = (DVR_WrapperEventCtx_t *)calloc(1, sizeof(DVR_WrapperEventCtx_t));
237 DVR_RETURN_IF_FALSE(padd);
238
239 *padd = *evt;
240 pthread_mutex_lock(lock);
241 list_add_tail(&padd->head, list);
242 pthread_mutex_unlock(lock);
243 return DVR_SUCCESS;
244}
245
246static inline void ctx_freeEvent(DVR_WrapperEventCtx_t *evt)
247{
248 free(evt);
249}
250
251/*useless*/
252static void ctx_cleanOutdatedEvents(struct list_head *evt_list,
253 pthread_mutex_t *evt_list_lock,
254 DVR_WrapperCtx_t *list)
255{
256 DVR_WrapperEventCtx_t *pevt, *pevt_tmp;
257 unsigned long sns[DVR_WRAPPER_MAX];
258 int cnt = 0;
259 int i;
260 int found = 0;
261
262 /*copy all valid sns*/
263 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
264 sns[cnt] = list[i].sn;
265 if (!sns[cnt])
266 cnt++;
267 }
268
269 /*free evts that not belong to any valid sns*/
270 pthread_mutex_lock(evt_list_lock);
271 list_for_each_entry_safe(pevt, pevt_tmp, evt_list, head) {
272 for (i = 0; i < cnt; i++) {
273 if (pevt->sn == sns[i]) {
274 found = 1;
275 break;
276 }
277 }
278 if (!found) {
279 list_del(&pevt->head);
280 ctx_freeEvent(pevt);
281 }
282 }
283 pthread_mutex_unlock(evt_list_lock);
284}
285
286static inline void ctx_cleanOutdatedRecordEvents()
287{
288 ctx_cleanOutdatedEvents(&record_evt_list, &record_evt_list_lock, record_list);
289}
290
291static inline void ctx_cleanOutdatedPlaybackEvents()
292{
293 ctx_cleanOutdatedEvents(&playback_evt_list, &playback_evt_list_lock, playback_list);
294}
295
296static inline DVR_WrapperCtx_t *ctx_get(unsigned long sn, DVR_WrapperCtx_t *list)
297{
298 int i;
299 for (i = 0; i < DVR_WRAPPER_MAX; i++) {
300 if (list[i].sn == sn)
301 return &list[i];
302 }
303 return NULL;
304}
305
306static inline void ctx_reset(DVR_WrapperCtx_t *ctx)
307{
308 memset((char *)ctx + offsetof(DVR_WrapperCtx_t, sn),
309 0,
310 sizeof(DVR_WrapperCtx_t) - offsetof(DVR_WrapperCtx_t, sn));
311}
312
313static inline int ctx_valid(DVR_WrapperCtx_t *ctx)
314{
315 return (ctx->sn != 0);
316}
317
318static inline DVR_WrapperCtx_t *ctx_getRecord(unsigned long sn)
319{
320 return ctx_get(sn, record_list);
321}
322
323static inline DVR_WrapperCtx_t *ctx_getPlayback(unsigned long sn)
324{
325 return ctx_get(sn, playback_list);
326}
327
328static int wrapper_requestThread(DVR_WrapperThreadCtx_t *ctx, void *(thread_fn)(void *))
329{
330 pthread_mutex_lock(&ctx->lock);
331 if (ctx->running == 0) {
332 pthread_condattr_t attr;
333 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
334 pthread_cond_init(&ctx->cond, &attr);
335 pthread_condattr_destroy(&attr);
336 DVR_WRAPPER_DEBUG(1, "start wrapper thread(%s) ...\n", ctx->name);
337 pthread_create(&ctx->thread, NULL, thread_fn, ctx);
338 DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) started\n", ctx->name);
339 }
340 ctx->running++;
341 pthread_mutex_unlock(&ctx->lock);
342 return 0;
343}
344
345static int wrapper_releaseThread(DVR_WrapperThreadCtx_t *ctx)
346{
347 pthread_mutex_lock(&ctx->lock);
348 ctx->running--;
349 if (!ctx->running) {
350 pthread_cond_broadcast(&ctx->cond);
351 pthread_mutex_unlock(&ctx->lock);
352
353 DVR_WRAPPER_DEBUG(1, "stop wrapper thread(%s) ...\n", ctx->name);
354 pthread_join(ctx->thread, NULL);
355 DVR_WRAPPER_DEBUG(1, "wrapper thread(%s) stopped\n", ctx->name);
356
357 pthread_mutex_lock(&ctx->lock);
358 if (!ctx->running) /*protect*/
359 pthread_cond_destroy(&ctx->cond);
360 }
361 pthread_mutex_unlock(&ctx->lock);
362 return 0;
363}
364
365#define WRAPPER_THREAD_RECORD (&wrapper_thread[0])
366#define WRAPPER_THREAD_PLAYBACK (&wrapper_thread[1])
367
368static inline int wrapper_requestThreadFor(DVR_WrapperCtx_t *ctx)
369{
370 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
371 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
372 return wrapper_requestThread(thread_ctx, wrapper_task);
373}
374
375static inline int wrapper_releaseThreadFor(DVR_WrapperCtx_t *ctx)
376{
377 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC)?
378 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
379 return wrapper_releaseThread(thread_ctx);
380}
381
382static inline int wrapper_releaseThreadForType(int type)
383{
384 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC)?
385 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
386 return wrapper_releaseThread(thread_ctx);
387}
388
389static inline void wrapper_threadSignal(DVR_WrapperThreadCtx_t *thread_ctx)
390{
391 pthread_cond_signal(&thread_ctx->cond);
392}
393
394static inline int wrapper_threadWait(DVR_WrapperThreadCtx_t *thread_ctx)
395{
396 pthread_cond_wait(&thread_ctx->cond, &thread_ctx->lock);
397 return 0;
398}
399
400static inline void wrapper_threadSignalForType(int type)
401{
402 DVR_WrapperThreadCtx_t *thread_ctx = (type == W_REC) ?
403 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
404 wrapper_threadSignal(thread_ctx);
405}
406
407static inline void wrapper_threadSignalFor(DVR_WrapperCtx_t *ctx)
408{
409 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
410 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
411 wrapper_threadSignal(thread_ctx);
412}
413
414static inline int wrapper_threadWaitFor(DVR_WrapperCtx_t *ctx)
415{
416 DVR_WrapperThreadCtx_t *thread_ctx = (ctx->type == W_REC) ?
417 WRAPPER_THREAD_RECORD : WRAPPER_THREAD_PLAYBACK;
418 wrapper_threadWait(thread_ctx);
419 return 0;
420}
421
422static void get_timeout_real(int timeout, struct timespec *ts)
423{
424 struct timespec ots;
425 int left, diff;
426
427 clock_gettime(CLOCK_REALTIME, &ots);
428
429 ts->tv_sec = ots.tv_sec + timeout/1000;
430 ts->tv_nsec = ots.tv_nsec;
431
432 left = timeout % 1000;
433 left *= 1000000;
434 diff = 1000000000-ots.tv_nsec;
435
436 if (diff <= left) {
437 ts->tv_sec++;
438 ts->tv_nsec = left-diff;
439 } else {
440 ts->tv_nsec += left;
441 }
442}
443
444/*return condition, locked if condition == true*/
445static int wrapper_mutex_lock_if(pthread_mutex_t *lock, int *condition)
446{
447 int r2;
448 do {
449 struct timespec rt2;
450 /*android use real time for mutex timedlock*/
451 get_timeout_real(10, &rt2);
452 r2 = pthread_mutex_timedlock(lock, &rt2);
453 } while (*condition && (r2 == ETIMEDOUT));
454
455 if (!(*condition) && (r2 == 0))
456 pthread_mutex_unlock(lock);
457
458 return *condition;
459}
460
461static void *wrapper_task(void *arg)
462{
463 DVR_WrapperThreadCtx_t *tctx = (DVR_WrapperThreadCtx_t *)arg;
464 DVR_WrapperEventCtx_t *evt;
465
466 pthread_mutex_lock(&tctx->lock);
467
468 while (tctx->running) {
469 {
470 int ret;
471 ret = wrapper_threadWait(tctx);
472 }
473
474 while ((evt = ((tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent()))) {
475 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
476 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
477
478 if (tctx->running) {
479 /*
480 continue not break,
481 make all events consumed, or mem leak
482 */
483 if (!wrapper_mutex_lock_if(&ctx->lock, &tctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800484 goto processed;
485
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800486 if (ctx_valid(ctx)) {
487 /*double check after lock*/
488 if (evt->sn == ctx->sn)
489 process_handleEvents(evt, ctx);
490 }
491 pthread_mutex_unlock(&ctx->lock);
492 }
493
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800494processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800495 ctx_freeEvent(evt);
496 }
497 }
498
499 pthread_mutex_unlock(&tctx->lock);
500 return NULL;
501}
502
503static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
504{
505 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
506 wrapper_threadSignalForType(evt->type);
507 return 0;
508}
509
510static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
511{
512 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
513 wrapper_threadSignalForType(evt->type);
514 return 0;
515}
516
517static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
518{
519 DVR_WrapperPlaybackSegmentInfo_t *pseg, *pseg_tmp;
520 list_for_each_entry_safe(pseg, pseg_tmp, &ctx->segments, head) {
521 list_del(&pseg->head);
522 free(pseg);
523 }
524}
525
526static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *pseg,
527 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
528{
529 (void)ctx;
530 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
531 pseg->seg_info = *seg_info;
532 else if (update_flags & U_PIDS) {
533 pseg->seg_info.nb_pids = seg_info->nb_pids;
534 memcpy(pseg->seg_info.pids, seg_info->pids, sizeof(pseg->seg_info.pids));
535 } else if (update_flags & U_STAT) {
536 pseg->seg_info.duration = seg_info->duration;
537 pseg->seg_info.size = seg_info->size;
538 pseg->seg_info.nb_packets = seg_info->nb_packets;
539 }
540
541 /*no changes
542 DVR_PlaybackSegmentFlag_t flags;
543 pseg->playback_info.segment_id = pseg->seg_info.id;
544 strncpy(pseg->playback_info.location,
545 ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
546 pseg->playback_info.pids = ctx->playback.pids_req;
547 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
548 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
549 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
550 pseg->playback_info.flags = flags;
551 */
552}
553
554static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
555{
556 DVR_WrapperPlaybackSegmentInfo_t *pseg;
557
558 DVR_WRAPPER_DEBUG(1, "timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
559 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
560
561 if (list_empty(&ctx->segments)) {
562 DVR_WRAPPER_DEBUG(1, "timeshift, update while no segment exists, ignore\n");
563 return DVR_SUCCESS;
564 }
565
566 /*normally, the last segment added will be updated*/
567 pseg =
568 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
569 if (pseg->seg_info.id == seg_info->id) {
570 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
571 } else {
572 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
573 if (pseg->seg_info.id == seg_info->id) {
574 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
575 break;
576 }
577 }
578 }
579
580 /*need to notify the dvr_playback*/
581 if (ctx->playback.param_open.is_timeshift/*should must be timeshift*/
582 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
583 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
584 if (
585 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
586 (ctx->playback.seg_status.segment_id == seg_info->id
587 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
588 ||
589 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
590 (ctx->playback.seg_status.segment_id != seg_info->id
591 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
592 )
593 {
594 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800595 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800596 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800597 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800598
599 error = dvr_playback_resume(ctx->playback.player);
600 DVR_WRAPPER_DEBUG(1, "timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n",
601 ctx->sn, error,
602 seg_info->id, seg_info->duration,
603 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
604 }
605 }
606
607 return DVR_SUCCESS;
608}
609
610static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_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->info = *seg_info;
616 else if (update_flags & U_PIDS) {
617 pseg->info.nb_pids = seg_info->nb_pids;
618 memcpy(pseg->info.pids, seg_info->pids, sizeof(pseg->info.pids));
619 } else if (update_flags & U_STAT) {
620 pseg->info.duration = seg_info->duration;
621 pseg->info.size = seg_info->size;
622 pseg->info.nb_packets = seg_info->nb_packets;
623 }
624}
625
626static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
627{
hualing chen266b9502020-04-04 17:39:39 +0800628 DVR_WrapperRecordSegmentInfo_t *pseg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800629
630 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800631 if (!list_empty(&ctx->segments)) {
632 pseg =
633 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
634 if (pseg->info.id == seg_info->id) {
635 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
636 } else {
637 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
638 if (pseg->info.id == seg_info->id) {
639 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
640 break;
641 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800642 }
643 }
644 }
645
646 /*timeshift, update the segment for playback*/
647 /*
648 the playback should grab the segment info other than the id,
649 and the id will be updated by each segment-add during the recording
650 */
651 /*
652 the playback paused if no data been checked from recording,
653 should resume the player later when there's more data
654 */
655
656 if (ctx->record.param_open.is_timeshift) {
657 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
658
659 if (ctx_playback) {
660 pthread_mutex_lock(&ctx_playback->lock);
661 if (ctx_valid(ctx_playback)
662 && ctx_playback->sn == sn_timeshift_playback) {
663 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
664 }
665 pthread_mutex_unlock(&ctx_playback->lock);
666 }
667 }
668
669 return DVR_SUCCESS;
670}
671
672static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
673 DVR_RecordSegmentInfo_t *seg_info,
674 DVR_PlaybackPids_t *p_pids,
675 DVR_PlaybackSegmentFlag_t flags)
676{
677 DVR_WrapperPlaybackSegmentInfo_t *pseg;
678 int error;
679
680 error = 0;
681 pseg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
682 if (!pseg) {
683 error = DVR_FAILURE;
684 DVR_WRAPPER_DEBUG(1, "memory fail\n");
685 return error;
686 }
687
688 /*copy the orignal segment info*/
689 pseg->seg_info = *seg_info;
690 /*generate the segment info used in playback*/
691 pseg->playback_info.segment_id = pseg->seg_info.id;
692 strncpy(pseg->playback_info.location, ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
693 pseg->playback_info.pids = *p_pids;
694 pseg->playback_info.flags = flags;
695 list_add(&pseg->head, &ctx->segments);
696
697 error = dvr_playback_add_segment(ctx->playback.player, &pseg->playback_info);
698 if (error) {
699 DVR_WRAPPER_DEBUG(1, "fail to add segment %lld (%d)\n", pseg->playback_info.segment_id, error);
700 } else {
701 ctx->playback.status.info_full.time += pseg->seg_info.duration;
702 ctx->playback.status.info_full.size += pseg->seg_info.size;
703 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
704 }
705
706 return error;
707}
708
709static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
710{
711 DVR_WrapperRecordSegmentInfo_t *pseg;
712 int error;
713
714 error = 0;
715 pseg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
716 if (!pseg) {
717 error = DVR_FAILURE;
718 DVR_WRAPPER_DEBUG(1, "memory fail\n");
719 }
720 pseg->info = *seg_info;
721 list_add(&pseg->head, &ctx->segments);
722
723 if (ctx->record.param_open.is_timeshift) {
724 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
725
726 if (ctx_playback) {
727 pthread_mutex_lock(&ctx_playback->lock);
728 if (ctx_valid(ctx_playback)) {
729 DVR_PlaybackSegmentFlag_t flags;
730
731 /*only if playback has started, the previous segments have been loaded*/
732 if (!list_empty(&ctx_playback->segments)) {
733 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
734 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
735 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
736 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
737 }
738 }
739 pthread_mutex_unlock(&ctx_playback->lock);
740 }
741 }
742
743 return error;
744}
745
746static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
747{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800748 int error = -1;
749 DVR_WrapperPlaybackSegmentInfo_t *pseg = NULL, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800750
751 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
752
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800753 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800754 if (pseg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800755
756 if (ctx->current_segment_id == seg_info->id) {
757 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
758
759 /*drive the player out of this will-be-deleted segment*/
760 next_seg = list_prev_entry(pseg, head);
761
762 if (ctx->playback.speed != 100.0f) {
763 error = dvr_playback_resume(ctx->playback.player);
764 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
765 }
766
767 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, 0);
768 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);
769
770 if (ctx->playback.speed == 0.0f) {
771 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
772 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last paused from new start (%d)\n", ctx->sn, error);
773 } else if (ctx->playback.speed != 100.0f) {
774 DVR_PlaybackSpeed_t dvr_speed = {
775 .speed = { ctx->playback.speed },
776 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
777 };
778 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
779 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last speed(x%f) from new start (%d)\n", ctx->sn,ctx->playback.speed, error);
780 }
781 }
782
783 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
784 if (error) {
785 /*remove playack segment fail*/
786 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), failed to remove segment(%llu) (%d)\n", ctx->sn, seg_info->id, error);
787 }
788
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800789 list_del(&pseg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800790
791 /*record the obsolete*/
792 ctx->playback.obsolete.time += pseg->seg_info.duration;
793 ctx->playback.obsolete.size += pseg->seg_info.size;
794 ctx->playback.obsolete.pkts += pseg->seg_info.nb_packets;
795
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800796 free(pseg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800797 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800798 }
799 }
800
801 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->id, error);
802
803 return error;
804}
805
806static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
807{
808 int error;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800809 DVR_WrapperRecordSegmentInfo_t *pseg, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800810
811 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->info.id);
812
813 /*if timeshifting, notify the playback first, then deal with record*/
814 if (ctx->record.param_open.is_timeshift) {
815 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
816
817 if (ctx_playback) {
818 pthread_mutex_lock(&ctx_playback->lock);
819 if (ctx_valid(ctx_playback)
820 && ctx_playback->sn == sn_timeshift_playback
821 && !list_empty(&ctx_playback->segments)) {
822 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
823 }
824 pthread_mutex_unlock(&ctx_playback->lock);
825 }
826 }
827
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800828 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
829 if (pseg->info.id == seg_info->info.id) {
830 list_del(&pseg->head);
831
832 /*record the obsolete*/
833 ctx->record.obsolete.time += pseg->info.duration;
834 ctx->record.obsolete.size += pseg->info.size;
835 ctx->record.obsolete.pkts += pseg->info.nb_packets;
836
837 free(pseg);
838 break;
839 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800840 }
841
842 error = dvr_segment_delete(ctx->record.param_open.location, seg_info->info.id);
843
844 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->info.id, error);
845
846 return error;
847}
848
849int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
850{
851 int error;
852 DVR_WrapperCtx_t *ctx;
853 DVR_RecordOpenParams_t open_param;
854
855 DVR_RETURN_IF_FALSE(rec);
856 DVR_RETURN_IF_FALSE(params);
857
858 /*get a free ctx*/
859 ctx = ctx_getRecord(0);
860 DVR_RETURN_IF_FALSE(ctx);
861
862 pthread_mutex_lock(&ctx->lock);
863
hualing chen51652f02020-12-29 16:59:31 +0800864 DVR_WRAPPER_DEBUG(1, "open record(dmx:%d) .istf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
865 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800866
867 ctx_reset(ctx);
868
869 ctx->record.param_open = *params;
870 ctx->record.event_fn = params->event_fn;
871 ctx->record.event_userdata = params->event_userdata;
872 ctx->record.next_segment_id = 0;
873 ctx->current_segment_id = 0;
874 INIT_LIST_HEAD(&ctx->segments);
875 ctx->sn = get_sn();
876
877 wrapper_requestThreadFor(ctx);
878
hualing chen266b9502020-04-04 17:39:39 +0800879 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800880 open_param.dmx_dev_id = params->dmx_dev_id;
881 open_param.data_from_memory = 0;
882 open_param.flags = params->flags;
Zhiqiang Han104aed72020-04-03 19:37:43 +0800883 open_param.notification_size = 500*1024;
Zhiqiang Han31505452020-05-06 15:08:10 +0800884 open_param.flush_size = params->flush_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800885 open_param.event_fn = wrapper_record_event_handler;
886 open_param.event_userdata = (void*)ctx->sn;
887
888 error = dvr_record_open(&ctx->record.recorder, &open_param);
889 if (error) {
890 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
891 ctx_reset(ctx);
892 pthread_mutex_unlock(&ctx->lock);
893 wrapper_releaseThreadForType(ctx->type);
894 return DVR_FAILURE;
895 }
896 if (params->is_timeshift)
897 sn_timeshift_record = ctx->sn;
898
899 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
900
hualing chen266b9502020-04-04 17:39:39 +0800901 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
902 if (error) {
903 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) set encrypt callback fail(error:%d).\n", params->dmx_dev_id, error);
904 }
905
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800906 pthread_mutex_unlock(&ctx->lock);
907
908 *rec = (DVR_WrapperRecord_t)ctx->sn;
909 return DVR_SUCCESS;
910}
911
912int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
913{
914 DVR_WrapperCtx_t *ctx;
915 DVR_RecordSegmentInfo_t seg_info;
916 int error;
917
918 DVR_RETURN_IF_FALSE(rec);
919
920 ctx = ctx_getRecord((unsigned long)rec);
921 DVR_RETURN_IF_FALSE(ctx);
922
923 pthread_mutex_lock(&ctx->lock);
924 DVR_WRAPPER_DEBUG(1, "close record(sn:%ld)\n", ctx->sn);
925 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
926
927 memset(&seg_info, 0, sizeof(seg_info));
928 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
929
930 error = dvr_record_close(ctx->record.recorder);
931
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800932 if (ctx->record.param_open.is_timeshift)
933 sn_timeshift_record = 0;
934
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800935 ctx_freeSegments(ctx);
936
937 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) closed = (%d).\n", ctx->sn, error);
938 ctx_reset(ctx);
939 pthread_mutex_unlock(&ctx->lock);
940
941 wrapper_releaseThreadForType(ctx->type);
942
943 return error;
944}
945
946int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
947{
948 DVR_WrapperCtx_t *ctx;
949 DVR_RecordStartParams_t *start_param;
950 int i;
951 int error;
952
953 DVR_RETURN_IF_FALSE(rec);
954 DVR_RETURN_IF_FALSE(params);
955
956 ctx = ctx_getRecord((unsigned long)rec);
957 DVR_RETURN_IF_FALSE(ctx);
958
959 pthread_mutex_lock(&ctx->lock);
960 DVR_WRAPPER_DEBUG(1, "start record(sn:%ld, location:%s) ...\n", ctx->sn, ctx->record.param_open.location);
961 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
962
963 start_param = &ctx->record.param_start;
964 memset(start_param, 0, sizeof(*start_param));
965 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
966 start_param->segment.segment_id = ctx->record.next_segment_id++;
967 start_param->segment.nb_pids = params->pids_info.nb_pids;
968 for (i = 0; i < params->pids_info.nb_pids; i++) {
969 start_param->segment.pids[i] = params->pids_info.pids[i];
970 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
971 }
hualing chen7a56cba2020-04-14 14:09:27 +0800972 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800973 {
974 /*sync to update for further use*/
975 DVR_RecordStartParams_t *update_param;
976 update_param = &ctx->record.param_update;
977 memcpy(update_param, start_param, sizeof(*update_param));
978 for (i = 0; i < update_param->segment.nb_pids; i++)
979 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
980 }
981
982 error = dvr_record_start_segment(ctx->record.recorder, start_param);
983 {
984 DVR_RecordSegmentInfo_t new_seg_info =
985 { .id = start_param->segment.segment_id, };
986 wrapper_addRecordSegment(ctx, &new_seg_info);
987 }
988
989 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) started = (%d)\n", ctx->sn, error);
990
991 pthread_mutex_unlock(&ctx->lock);
992
993 return error;
994}
995
996int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
997{
998 DVR_WrapperCtx_t *ctx;
999 DVR_RecordSegmentInfo_t seg_info;
1000 int error;
1001
1002 DVR_RETURN_IF_FALSE(rec);
1003
1004 ctx = ctx_getRecord((unsigned long)rec);
1005 DVR_RETURN_IF_FALSE(ctx);
1006
1007 pthread_mutex_lock(&ctx->lock);
1008 DVR_WRAPPER_DEBUG(1, "stop record(sn:%ld) ...\n", ctx->sn);
1009 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1010
1011 memset(&seg_info, 0, sizeof(seg_info));
1012 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1013 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1014
1015 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
1016 pthread_mutex_unlock(&ctx->lock);
1017
1018 return error;
1019}
1020
1021int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1022{
1023 DVR_WrapperCtx_t *ctx;
1024 DVR_RecordStartParams_t *start_param;
1025 DVR_RecordSegmentInfo_t seg_info;;
1026 int i;
1027 int error;
1028
1029 DVR_RETURN_IF_FALSE(rec);
1030 DVR_RETURN_IF_FALSE(params);
1031
1032 ctx = ctx_getRecord((unsigned long)rec);
1033 DVR_RETURN_IF_FALSE(ctx);
1034
1035 pthread_mutex_lock(&ctx->lock);
1036 DVR_WRAPPER_DEBUG(1, "update record(sn:%ld)\n", ctx->sn);
1037 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1038
1039 start_param = &ctx->record.param_update;
1040 memset(start_param, 0, sizeof(*start_param));
1041 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1042 start_param->segment.segment_id = ctx->record.next_segment_id++;
1043 start_param->segment.nb_pids = params->nb_pids;
1044 for (i = 0; i < params->nb_pids; i++) {
1045 start_param->segment.pids[i] = params->pids[i];
1046 start_param->segment.pid_action[i] = params->pid_action[i];
1047 }
1048 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1049 {
1050 DVR_RecordSegmentInfo_t new_seg_info =
1051 { .id = start_param->segment.segment_id, };
1052 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1053 wrapper_addRecordSegment(ctx, &new_seg_info);
1054 }
1055
1056 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) updated = (%d)\n", ctx->sn, error);
1057 pthread_mutex_unlock(&ctx->lock);
1058
1059 return error;
1060}
1061
1062int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1063{
1064 DVR_WrapperCtx_t *ctx;
1065 DVR_WrapperRecordStatus_t s;
1066 int error;
1067
1068 DVR_RETURN_IF_FALSE(rec);
1069 DVR_RETURN_IF_FALSE(status);
1070
1071 ctx = ctx_getRecord((unsigned long)rec);
1072 DVR_RETURN_IF_FALSE(ctx);
1073
1074 pthread_mutex_lock(&ctx->lock);
1075
1076 DVR_WRAPPER_DEBUG(1, "get record(sn:%ld) status ...\n", ctx->sn);
1077 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1078
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001079 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001080
1081 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
1082 ctx->sn,
1083 s.state,
1084 s.info.time,
1085 s.info.size,
1086 s.info.pkts,
1087 error);
1088
1089 *status = s;
1090
1091 pthread_mutex_unlock(&ctx->lock);
1092
1093 return error;
1094}
1095
hualing chen4fe3bee2020-10-23 13:58:52 +08001096int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1097{
1098 DVR_WrapperCtx_t *ctx;
1099 int error;
1100
1101 DVR_RETURN_IF_FALSE(rec);
1102
1103 ctx = ctx_getRecord((unsigned long)rec);
1104 DVR_RETURN_IF_FALSE(ctx);
1105
1106 pthread_mutex_lock(&ctx->lock);
1107 error = dvr_record_is_secure_mode(ctx->record.recorder);
1108 pthread_mutex_unlock(&ctx->lock);
1109 return error;
1110}
1111
hualing chen266b9502020-04-04 17:39:39 +08001112int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1113{
1114 DVR_WrapperCtx_t *ctx;
1115 int error;
1116
1117 DVR_RETURN_IF_FALSE(rec);
1118 DVR_RETURN_IF_FALSE(p_secure_buf);
1119
1120 ctx = ctx_getRecord((unsigned long)rec);
1121 DVR_RETURN_IF_FALSE(ctx);
1122
1123 pthread_mutex_lock(&ctx->lock);
1124 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
1125 pthread_mutex_unlock(&ctx->lock);
1126 return error;
1127}
1128
1129int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1130{
1131 DVR_WrapperCtx_t *ctx;
1132 int error;
1133
1134 DVR_RETURN_IF_FALSE(rec);
1135 DVR_RETURN_IF_FALSE(func);
1136
1137 ctx = ctx_getRecord((unsigned long)rec);
1138 DVR_RETURN_IF_FALSE(ctx);
1139
1140 pthread_mutex_lock(&ctx->lock);
1141 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
1142 pthread_mutex_unlock(&ctx->lock);
1143 return error;
1144}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001145
1146
1147int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1148{
1149 DVR_WrapperCtx_t *ctx;
1150 DVR_PlaybackOpenParams_t open_param;
1151 int error;
1152
1153 DVR_RETURN_IF_FALSE(playback);
1154 DVR_RETURN_IF_FALSE(params);
1155 DVR_RETURN_IF_FALSE(params->playback_handle);
1156
1157 /*get a free ctx*/
1158 ctx = ctx_getPlayback(0);
1159 DVR_RETURN_IF_FALSE(ctx);
1160
1161 pthread_mutex_lock(&ctx->lock);
1162
1163 DVR_WRAPPER_DEBUG(1, "open playback(dmx:%d) ...\n", params->dmx_dev_id);
1164
1165 ctx_reset(ctx);
1166
1167 ctx->playback.param_open = *params;
1168 ctx->playback.event_fn = params->event_fn;
1169 ctx->playback.event_userdata = params->event_userdata;
1170 ctx->current_segment_id = 0;
1171 INIT_LIST_HEAD(&ctx->segments);
1172 ctx->sn = get_sn();
1173
1174 wrapper_requestThreadFor(ctx);
1175
hualing chen266b9502020-04-04 17:39:39 +08001176 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001177 open_param.dmx_dev_id = params->dmx_dev_id;
1178 open_param.block_size = params->block_size;
1179 open_param.is_timeshift = params->is_timeshift;
1180 //open_param.notification_size = 10*1024; //not supported
1181 open_param.event_fn = wrapper_playback_event_handler;
1182 open_param.event_userdata = (void*)ctx->sn;
1183 /*open_param.has_pids = 0;*/
1184
1185 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chenfbf8e022020-06-15 13:43:11 +08001186 open_param.vendor = DVR_PLAYBACK_VENDOR_AML;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001187
1188 error = dvr_playback_open(&ctx->playback.player, &open_param);
1189 if (error) {
1190 DVR_WRAPPER_DEBUG(1, "playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
1191 ctx_reset(ctx);
1192 pthread_mutex_unlock(&ctx->lock);
1193 wrapper_releaseThreadForType(ctx->type);
1194 return DVR_FAILURE;
1195 }
1196 if (params->is_timeshift)
1197 sn_timeshift_playback = ctx->sn;
1198
hualing chen266b9502020-04-04 17:39:39 +08001199 DVR_WRAPPER_DEBUG(1, "hanyh: playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
1200 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1201 if (error) {
1202 DVR_WRAPPER_DEBUG(1, "playback set deccrypt callback fail(error:%d).\n", error);
1203 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001204 pthread_mutex_unlock(&ctx->lock);
1205
1206 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1207 return DVR_SUCCESS;
1208}
1209
1210int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1211{
1212 DVR_WrapperCtx_t *ctx;
1213 int error;
1214
1215 DVR_RETURN_IF_FALSE(playback);
1216
1217 ctx = ctx_getPlayback((unsigned long)playback);
1218 DVR_RETURN_IF_FALSE(ctx);
1219
1220 pthread_mutex_lock(&ctx->lock);
1221 DVR_WRAPPER_DEBUG(1, "close playback(sn:%ld)\n", ctx->sn);
1222 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1223
1224 if (ctx->playback.param_open.is_timeshift)
1225 sn_timeshift_playback = 0;
1226
1227 /*try stop first*/
1228 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1229
1230 {
1231 /*remove all segments*/
1232 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1233
1234 list_for_each_entry(pseg, &ctx->segments, head) {
1235 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1236 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1237 ctx->sn, pseg->playback_info.segment_id, error);
1238 }
1239 ctx_freeSegments(ctx);
1240 }
1241
1242 error = dvr_playback_close(ctx->playback.player);
1243
1244 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) closed.\n", ctx->sn);
1245 ctx_reset(ctx);
1246 pthread_mutex_unlock(&ctx->lock);
1247
1248 wrapper_releaseThreadForType(ctx->type);
1249
1250 return error;
1251}
1252
1253int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1254{
1255 DVR_WrapperCtx_t *ctx;
1256 int error;
1257 uint64_t *p_segment_ids;
1258 uint32_t segment_nb;
1259 uint32_t i;
1260 DVR_RecordSegmentInfo_t seg_info_1st;
1261 int got_1st_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001262 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
1263
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001264
1265 DVR_RETURN_IF_FALSE(playback);
1266 DVR_RETURN_IF_FALSE(p_pids);
1267
1268 ctx = ctx_getPlayback((unsigned long)playback);
1269 DVR_RETURN_IF_FALSE(ctx);
1270
1271 pthread_mutex_lock(&ctx->lock);
1272
1273 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",
1274 ctx->sn,
1275 ctx->playback.param_open.location,
1276 flags,
1277 p_pids->video.pid, p_pids->video.format,
1278 p_pids->audio.pid, p_pids->audio.format,
1279 p_pids->ad.pid, p_pids->ad.format,
1280 p_pids->subtitle.pid, p_pids->subtitle.format,
1281 p_pids->pcr.pid);
1282
1283 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1284
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001285 ctx_record = NULL;
1286 if (ctx->playback.param_open.is_timeshift) {
1287 /*lock the recorder to avoid changing the recording segments*/
1288 ctx_record = ctx_getRecord(sn_timeshift_record);
1289
1290 if (ctx_record) {
1291 pthread_mutex_lock(&ctx_record->lock);
1292 if (!ctx_valid(ctx_record)
1293 || ctx_record->sn != sn_timeshift_record) {
1294 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error\n");
1295 pthread_mutex_unlock(&ctx_record->lock);
1296 } else {
1297 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
1298 ctx->sn, ctx_record->sn);
1299 }
1300 }
1301 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_record, &ctx->lock);
1302 }
1303
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001304 /*obtain all segments in a list*/
1305 segment_nb = 0;
1306 p_segment_ids = NULL;
1307 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001308 if (!error) {
1309 got_1st_seg = 0;
1310 for (i = 0; i < segment_nb; i++) {
1311 DVR_RecordSegmentInfo_t seg_info;
1312 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001313
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001314 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1315 if (error) {
1316 error = DVR_FAILURE;
1317 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1318 ctx->playback.param_open.location, p_segment_ids[i], error);
1319 break;
1320 }
hualing chen92f3a142020-07-08 20:59:33 +08001321 //add check if has audio or video pid. if not exist. not add segment to playback
1322 int ii = 0;
1323 int has_av = 0;
1324 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1325 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1326 if (type == DVR_STREAM_TYPE_VIDEO ||
1327 type == DVR_STREAM_TYPE_AUDIO ||
1328 type == DVR_STREAM_TYPE_AD) {
1329 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1330 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,
1331 DVR_STREAM_TYPE_VIDEO,
1332 DVR_STREAM_TYPE_AUDIO,
1333 DVR_STREAM_TYPE_AD);
1334 has_av = 1;
1335 //break;
1336 } else {
1337 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,
1338 DVR_STREAM_TYPE_VIDEO,
1339 DVR_STREAM_TYPE_AUDIO,
1340 DVR_STREAM_TYPE_AD);
1341 }
1342 }
1343 if (has_av == 0) {
1344 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1345 continue;
1346 } else {
1347 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1348 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001349 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1350 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1351 if (error)
1352 break;
1353
1354 /*copy the 1st segment*/
1355 if (got_1st_seg == 0) {
1356 seg_info_1st = seg_info;
1357 got_1st_seg = 1;
1358 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001359 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001360 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001361
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001362 /* return if no segment or fail to add */
1363 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001364
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001365 /*copy the obsolete infomation, must for timeshifting*/
1366 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1367 ctx->playback.obsolete = ctx_record->record.obsolete;
1368 }
1369
1370 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
1371
1372 ctx->playback.reach_end = DVR_FALSE;
1373 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1374 ctx->playback.speed = 0.0f;
1375 else
1376 ctx->playback.speed = 100.0f;
1377
1378 ctx->playback.pids_req = *p_pids;
1379
1380 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1381 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
1382 ctx->sn, seg_info_1st.id, error);
1383
1384 error = dvr_playback_start(ctx->playback.player, flags);
1385
1386 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001387 }
1388 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001389
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001390 if (ctx->playback.param_open.is_timeshift) {
1391 /*unlock the recorder locked above*/
1392 if (ctx_record && ctx_valid(ctx_record)) {
1393 pthread_mutex_unlock(&ctx_record->lock);
1394 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
1395 ctx->sn, ctx_record->sn);
1396 }
1397 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001398
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001399
1400 pthread_mutex_unlock(&ctx->lock);
1401
1402 return error;
1403}
1404
1405int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
1406{
1407 DVR_WrapperCtx_t *ctx;
1408 int error;
1409
1410 DVR_RETURN_IF_FALSE(playback);
1411
1412 ctx = ctx_getPlayback((unsigned long)playback);
1413 DVR_RETURN_IF_FALSE(ctx);
1414
1415 pthread_mutex_lock(&ctx->lock);
1416 DVR_WRAPPER_DEBUG(1, "stop playback(sn:%ld) ...\n", ctx->sn);
1417 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1418
1419 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1420
1421 {
1422 /*remove all segments*/
1423 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1424
1425 list_for_each_entry(pseg, &ctx->segments, head) {
1426 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1427 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1428 ctx->sn, pseg->playback_info.segment_id, error);
1429 }
1430 ctx_freeSegments(ctx);
1431 }
1432
1433 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
1434 pthread_mutex_unlock(&ctx->lock);
1435
1436 return error;
1437}
1438
1439int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
1440{
1441 DVR_WrapperCtx_t *ctx;
1442 int error;
1443
1444 DVR_RETURN_IF_FALSE(playback);
1445
1446 ctx = ctx_getPlayback((unsigned long)playback);
1447 DVR_RETURN_IF_FALSE(ctx);
1448
1449 pthread_mutex_lock(&ctx->lock);
1450 DVR_WRAPPER_DEBUG(1, "pause playback(sn:%ld) ...\n", ctx->sn);
1451 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08001452 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08001453 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08001454 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001455
1456 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
1457
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001458 ctx->playback.speed = 0.0f;
1459
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001460 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) paused (%d)\n", ctx->sn, error);
1461 pthread_mutex_unlock(&ctx->lock);
1462
1463 return error;
1464}
1465
1466int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
1467{
1468 DVR_WrapperCtx_t *ctx;
1469 int error;
1470
1471 DVR_RETURN_IF_FALSE(playback);
1472
1473 ctx = ctx_getPlayback((unsigned long)playback);
1474 DVR_RETURN_IF_FALSE(ctx);
1475
1476 pthread_mutex_lock(&ctx->lock);
1477 DVR_WRAPPER_DEBUG(1, "resume playback(sn:%ld) ...\n", ctx->sn);
1478 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1479
1480 error = dvr_playback_resume(ctx->playback.player);
1481
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001482 ctx->playback.speed = 100.0f;
1483
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001484 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
1485 pthread_mutex_unlock(&ctx->lock);
1486
1487 return error;
1488}
1489
1490int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
1491{
1492 DVR_WrapperCtx_t *ctx;
1493 int error;
1494 DVR_PlaybackSpeed_t dvr_speed = {
1495 .speed = { speed },
1496 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
1497 };
1498
1499 DVR_RETURN_IF_FALSE(playback);
1500
1501 ctx = ctx_getPlayback((unsigned long)playback);
1502 DVR_RETURN_IF_FALSE(ctx);
1503
1504 pthread_mutex_lock(&ctx->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001505 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 +08001506 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1507
1508 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
1509
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001510 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
1511 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
1512 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
1513 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
1514 error = dvr_playback_resume(ctx->playback.player);
1515 } else if (ctx->playback.speed == 0.0f
1516 && speed != 0.0f
1517 && speed != 100.0f) {
1518 /*libdvr do not support pause with speed=0, will not be here*/
1519 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
1520 error = dvr_playback_resume(ctx->playback.player);
1521 }
1522
1523 ctx->playback.speed = speed;
1524
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001525 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) speeded(x%f) (%d)\n",
1526 ctx->sn, speed, error);
1527 pthread_mutex_unlock(&ctx->lock);
1528
1529 return error;
1530}
1531
1532int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
1533{
1534 DVR_WrapperCtx_t *ctx;
1535 int error;
1536 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1537 uint64_t segment_id;
1538 uint32_t off;
1539 uint64_t last_segment_id;
1540 uint32_t pre_off;
1541
1542 DVR_RETURN_IF_FALSE(playback);
1543
1544 ctx = ctx_getPlayback((unsigned long)playback);
1545 DVR_RETURN_IF_FALSE(ctx);
1546
1547 pthread_mutex_lock(&ctx->lock);
1548
1549 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
1550 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1551
1552 off = 0;
1553 segment_id = 0;
1554 pre_off = 0;
1555 last_segment_id = 0;
1556
1557 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1558 segment_id = pseg->seg_info.id;
1559
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001560 if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset)
1561 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001562
1563 last_segment_id = pseg->seg_info.id;
1564 pre_off += pseg->seg_info.duration;
1565 }
1566
1567 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001568 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
1569 off = time_offset;
1570 } else if (ctx->playback.obsolete.time >= time_offset) {
1571 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001572 } else {
hualing chenda76fc52020-05-28 14:56:42 +08001573 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001574 }
1575
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001576 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (seg:%lld, off:%d)\n",
1577 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001578 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
1579 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
1580
1581 pthread_mutex_unlock(&ctx->lock);
1582
1583 return error;
1584}
1585
1586int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
1587{
1588 DVR_WrapperCtx_t *ctx;
1589 int error;
1590 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1591
1592 DVR_RETURN_IF_FALSE(playback);
1593 DVR_RETURN_IF_FALSE(p_pids);
1594
1595 ctx = ctx_getPlayback((unsigned long)playback);
1596 DVR_RETURN_IF_FALSE(ctx);
1597
1598 pthread_mutex_lock(&ctx->lock);
1599
1600 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
1601 ctx->sn,
1602 p_pids->video.pid, p_pids->video.format,
1603 p_pids->audio.pid, p_pids->audio.format);
1604 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1605
1606 ctx->playback.pids_req = *p_pids;
1607
1608 error = 0;
1609 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1610 /*should update the whole list of segments*/
1611 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
1612 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
1613 /*check udpate for pids*/
1614 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
1615 pseg->playback_info.pids = *p_pids;
1616 error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
1617 if (error) {
1618 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
1619 ctx->sn, pseg->seg_info.id, error);
1620 /*do not break, let list updated*/
1621 }
1622 }
1623 }
1624 /*break;*/
1625 }
1626 }
1627
1628 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
1629 ctx->sn,
1630 p_pids->video.pid, p_pids->video.format,
1631 p_pids->audio.pid, p_pids->audio.format,
1632 error);
1633
1634 pthread_mutex_unlock(&ctx->lock);
1635
1636 return error;
1637}
1638
1639int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
1640{
1641 DVR_WrapperCtx_t *ctx;
1642 DVR_WrapperPlaybackStatus_t s;
1643 DVR_PlaybackStatus_t play_status;
1644 int error;
1645
1646 DVR_RETURN_IF_FALSE(playback);
1647 DVR_RETURN_IF_FALSE(status);
1648
1649 ctx = ctx_getPlayback((unsigned long)playback);
1650 DVR_RETURN_IF_FALSE(ctx);
1651
1652 pthread_mutex_lock(&ctx->lock);
1653
1654 DVR_WRAPPER_DEBUG(1, "get playback(sn:%ld) status ...\n", ctx->sn);
1655 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1656
1657 error = dvr_playback_get_status(ctx->playback.player, &play_status);
1658 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) get status (%d)\n", ctx->sn, error);
1659
1660 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001661 error = process_generatePlaybackStatus(ctx, &s);
1662
hualing chenb5cd42e2020-04-15 17:03:34 +08001663 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
1664 //reach end need set full time to cur.so app can exist playback.
1665 DVR_WRAPPER_DEBUG(1, "set cur time to full time, reach end occur");
1666 s.info_cur.time = s.info_full.time;
1667 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001668 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001669 ctx->sn,
1670 s.state,
1671 s.info_cur.time,
1672 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001673 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001674 error);
1675
1676 *status = s;
1677
1678 pthread_mutex_unlock(&ctx->lock);
1679
1680 return error;
1681}
1682
hualing chen266b9502020-04-04 17:39:39 +08001683int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
1684{
1685 DVR_WrapperCtx_t *ctx;
1686 int error;
1687
1688 DVR_RETURN_IF_FALSE(playback);
1689 DVR_RETURN_IF_FALSE(p_secure_buf);
1690
1691 ctx = ctx_getPlayback((unsigned long)playback);
1692 DVR_RETURN_IF_FALSE(ctx);
1693
1694 pthread_mutex_lock(&ctx->lock);
1695 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
1696 pthread_mutex_unlock(&ctx->lock);
1697 return error;
1698}
1699
1700int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
1701{
1702 DVR_WrapperCtx_t *ctx;
1703 int error;
1704
1705 DVR_RETURN_IF_FALSE(playback);
1706 DVR_RETURN_IF_FALSE(func);
1707
1708 ctx = ctx_getPlayback((unsigned long)playback);
1709 DVR_RETURN_IF_FALSE(ctx);
1710
1711 pthread_mutex_lock(&ctx->lock);
1712 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
1713 pthread_mutex_unlock(&ctx->lock);
1714 return error;
1715}
1716
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001717static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
1718{
1719 DVR_WrapperEventCtx_t evt;
1720
1721 DVR_RETURN_IF_FALSE(userdata);
1722
1723 evt.sn = (unsigned long)userdata;
1724 evt.type = W_REC;
1725 evt.record.event = event;
1726 evt.record.status = *(DVR_RecordStatus_t *)params;
1727 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
1728 return ctx_addRecordEvent(&evt);
1729}
1730
1731static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
1732{
1733 DVR_WrapperEventCtx_t evt;
1734
1735 DVR_RETURN_IF_FALSE(userdata);
1736
1737 evt.sn = (unsigned long)userdata;
1738 evt.type = W_PLAYBACK;
1739 evt.playback.event = event;
1740 evt.playback.status = *(DVR_Play_Notify_t *)params;
1741 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event);
1742 return ctx_addPlaybackEvent(&evt);
1743}
1744
1745static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
1746{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001747 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 +08001748 ctx->sn,
1749 evt,
1750 status->info.time,
1751 status->info.size,
1752 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001753 status->info_obsolete.time,
1754 status->info_obsolete.size,
1755 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001756
1757 if (ctx->record.event_fn)
1758 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
1759 return 0;
1760}
1761
1762static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
1763{
1764 DVR_RecordStartParams_t param;
1765 DVR_RecordSegmentInfo_t seg_info;
1766 int i;
1767 int error;
1768
1769 memcpy(&param, &ctx->record.param_update, sizeof(param));
1770 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
1771 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
1772 for (i = 0; i < param.segment.nb_pids; i++) {
1773 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
1774 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
1775 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
1776 ctx->record.param_update.segment.nb_pids++;
1777 }
1778 }
1779 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
1780 {
1781 DVR_RecordSegmentInfo_t new_seg_info =
1782 { .id = ctx->record.param_update.segment.segment_id, };
1783 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1784 wrapper_addRecordSegment(ctx, &new_seg_info);
1785 }
1786
Zhiqiang Hand977e972020-05-11 11:30:47 +08001787 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 +08001788 return error;
1789}
1790
1791static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg)
1792{
1793 return wrapper_removeRecordSegment(ctx, pseg);
1794}
1795
1796/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001797static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001798{
1799 /*the current seg is not covered in the statistics*/
1800 DVR_WrapperRecordSegmentInfo_t *pseg;
1801
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001802 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001803 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
1804
1805 ctx->record.status.state = ctx->record.seg_status.state;
1806 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
1807 memcpy(ctx->record.status.pids.pids,
1808 ctx->record.seg_status.info.pids,
1809 sizeof(ctx->record.status.pids.pids));
1810 ctx->current_segment_id = ctx->record.seg_status.info.id;
1811
1812 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1813 if (pseg->info.id != ctx->record.seg_status.info.id) {
1814 ctx->record.status.info.time += pseg->info.duration;
1815 ctx->record.status.info.size += pseg->info.size;
1816 ctx->record.status.info.pkts += pseg->info.nb_packets;
1817 }
1818 }
1819
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001820 ctx->record.status.info_obsolete = ctx->record.obsolete;
1821
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001822 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001823
1824 if (status) {
1825 *status = ctx->record.status;
1826 status->info.time += ctx->record.seg_status.info.duration;
1827 status->info.size += ctx->record.seg_status.info.size;
1828 status->info.pkts += ctx->record.seg_status.info.nb_packets;
1829 }
1830
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001831 return DVR_SUCCESS;
1832}
1833
1834
1835static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
1836{
1837 DVR_WrapperRecordStatus_t status;
1838
1839 memset(&status, 0, sizeof(status));
1840
1841 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d)\n",
1842 evt->sn, evt->record.event, evt->record.status.state);
1843
1844 switch (evt->record.event)
1845 {
1846 case DVR_RECORD_EVENT_STATUS:
1847 {
1848 switch (evt->record.status.state)
1849 {
1850 case DVR_RECORD_STATE_OPENED:
1851 case DVR_RECORD_STATE_CLOSED:
1852 {
1853 ctx->record.seg_status = evt->record.status;
1854
1855 status.state = evt->record.status.state;
1856 process_notifyRecord(ctx, evt->record.event, &status);
1857 } break;
1858 case DVR_RECORD_STATE_STARTED:
1859 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001860 ctx->record.seg_status = evt->record.status;
1861
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001862 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001863 process_notifyRecord(ctx, evt->record.event, &status);
1864
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001865 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001866 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001867 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
1868 DVR_WRAPPER_DEBUG(1, "start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n",
1869 ctx->sn,
1870 evt->record.status.info.size,
1871 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08001872 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
1873 /*should notify the recording's stop*/
1874 int error = dvr_record_close(ctx->record.recorder);
1875 DVR_WRAPPER_DEBUG(1, "stop record(%lu)=%d, failed to start new segment for recording.",
1876 ctx->sn, error);
1877 status.state = DVR_RECORD_STATE_CLOSED;
1878 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
1879 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001880 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001881
1882 if (ctx->record.param_open.is_timeshift
1883 && ctx->record.param_open.max_time
1884 && status.info.time >= ctx->record.param_open.max_time) {
1885 DVR_WrapperRecordSegmentInfo_t *pseg;
1886
1887 /*as the player do not support null playlist,
1888 there must be one segment existed at any time,
1889 we have to keep two segments before remove one*/
1890 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1891 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1892 /*only one segment, waiting for more*/
1893 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n",
1894 status.info.size,
1895 ctx->record.param_open.max_time,
1896 ctx->record.param_open.segment_size);
1897 } else {
1898 /*timeshifting, remove the 1st segment and notify the player*/
1899 record_removeSegment(ctx, pseg);
1900
1901 process_generateRecordStatus(ctx, &status);
1902 process_notifyRecord(ctx, evt->record.event, &status);
1903 }
1904 }
1905
1906 if (ctx->record.param_open.is_timeshift
1907 && ctx->record.param_open.max_size
1908 && status.info.size >= ctx->record.param_open.max_size) {
1909 DVR_WrapperRecordSegmentInfo_t *pseg;
1910
1911 /*as the player do not support null playlist,
1912 there must be one segment existed at any time,
1913 we have to keep two segments before remove one*/
1914 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1915 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1916 /*only one segment, waiting for more*/
1917 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of record < max size of segment(%lld)\n",
1918 status.info.size,
1919 ctx->record.param_open.segment_size);
1920 } else {
1921 record_removeSegment(ctx, pseg);
1922
1923 process_generateRecordStatus(ctx, &status);
1924 process_notifyRecord(ctx, evt->record.event, &status);
1925 }
1926 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001927 } break;
1928 case DVR_RECORD_STATE_STOPPED:
1929 {
1930 ctx->record.seg_status = evt->record.status;
1931
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001932 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001933 process_notifyRecord(ctx, evt->record.event, &status);
1934 } break;
1935 default:
1936 break;
1937 }
1938 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08001939 case DVR_RECORD_EVENT_WRITE_ERROR: {
1940 ctx->record.seg_status = evt->record.status;
1941 status.state = evt->record.status.state;
1942 process_notifyRecord(ctx, evt->record.event, &status);
1943 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001944 default:
1945 break;
1946 }
1947 return DVR_SUCCESS;
1948}
1949
1950static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
1951{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001952 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 +08001953 ctx->sn,
1954 evt,
1955 status->state,
1956 status->info_cur.time,
1957 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001958 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001959
1960 if (ctx->playback.event_fn)
1961 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
1962 return 0;
1963}
1964
1965/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001966static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001967{
1968 /*the current seg is not covered in the statistics*/
1969 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1970
1971 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
1972 ctx->playback.status.pids = ctx->playback.pids_req;
1973
1974 ctx->playback.status.state = ctx->playback.seg_status.state;
1975 ctx->playback.status.speed = ctx->playback.seg_status.speed;
1976 ctx->playback.status.flags = ctx->playback.seg_status.flags;
1977 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
1978
1979 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1980 if (pseg->seg_info.id == ctx->playback.seg_status.segment_id)
1981 break;
1982 ctx->playback.status.info_cur.time += pseg->seg_info.duration;
1983 ctx->playback.status.info_cur.size += pseg->seg_info.size;
1984 ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets;
1985 }
1986 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1987 ctx->playback.status.info_full.time += pseg->seg_info.duration;
1988 ctx->playback.status.info_full.size += pseg->seg_info.size;
1989 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
1990 }
1991
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001992 if (status) {
1993 *status = ctx->playback.status;
1994 /*deal with current, lack size and pkts with the current*/
1995 status->info_cur.time += ctx->playback.seg_status.time_cur;
1996 status->info_obsolete.time = ctx->playback.obsolete.time;
1997 }
1998
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001999 return DVR_SUCCESS;
2000}
2001
2002static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2003{
2004 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
2005 evt->sn, evt->playback.event,
2006 evt->playback.status.play_status.state,
2007 evt->playback.status.play_status.segment_id,
2008 evt->playback.status.play_status.time_cur,
2009 evt->playback.status.play_status.time_end);
2010
2011 /*evt PLAYTIME will break the last logic, do not save*/
2012 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME)
2013 ctx->playback.last_event = evt->playback.event;
2014
2015 switch (evt->playback.event)
2016 {
2017 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2018 case DVR_PLAYBACK_EVENT_REACHED_END:
2019 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2020 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002021 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002022 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002023 {
2024 DVR_WrapperPlaybackStatus_t status;
2025
2026 /*copy status of segment*/
2027 ctx->playback.seg_status = evt->playback.status.play_status;
2028
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002029 /*generate status of the whole playback*/
2030 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002031
2032 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
2033 if (ctx->playback.param_open.is_timeshift) {
2034 /*wait for more data in recording*/
2035 } else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
2036 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002037 } else {
2038 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002039 }
hualing chenf291cf32020-06-18 10:50:30 +08002040 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002041 process_notifyPlayback(ctx, evt->playback.event, &status);
2042 }
2043 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002044 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2045 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2046 case DVR_PLAYBACK_EVENT_NO_KEY:
2047 {
2048 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
2049 } break;
2050 default:
2051 {
2052 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
2053 } break;
2054 }
2055 return 0;
2056}
2057
2058static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2059{
2060 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2061}
2062