blob: 5bc679b1e40709ff77639a704ea82b8393fbada7 [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;
hualing chene3797f02021-01-13 14:53:28 +0800471
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800472 ret = wrapper_threadWait(tctx);
473 }
474
475 while ((evt = ((tctx->type == W_REC)? ctx_getRecordEvent() : ctx_getPlaybackEvent()))) {
476 DVR_WrapperCtx_t *ctx = (evt->type == W_REC)?
477 ctx_getRecord(evt->sn) : ctx_getPlayback(evt->sn);
hualing chene3797f02021-01-13 14:53:28 +0800478 DVR_WRAPPER_DEBUG(1, "start name(%s) sn(%d) runing(%d) type(%d)event end...\n", tctx->name, ctx->sn, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800479 if (tctx->running) {
480 /*
481 continue not break,
482 make all events consumed, or mem leak
483 */
484 if (!wrapper_mutex_lock_if(&ctx->lock, &tctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800485 goto processed;
486
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800487 if (ctx_valid(ctx)) {
488 /*double check after lock*/
489 if (evt->sn == ctx->sn)
490 process_handleEvents(evt, ctx);
491 }
492 pthread_mutex_unlock(&ctx->lock);
493 }
494
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800495processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800496 ctx_freeEvent(evt);
497 }
hualing chene3797f02021-01-13 14:53:28 +0800498 DVR_WRAPPER_DEBUG(1, "start name(%d) runing(%d) type(%d)event con...\n", tctx->name, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800499 }
500
501 pthread_mutex_unlock(&tctx->lock);
hualing chene3797f02021-01-13 14:53:28 +0800502 DVR_WRAPPER_DEBUG(1, "end name(%s) runing(%d) type(%d)event end...\n", tctx->name, tctx->running, tctx->type);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800503 return NULL;
504}
505
506static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
507{
508 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
509 wrapper_threadSignalForType(evt->type);
510 return 0;
511}
512
513static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
514{
515 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
516 wrapper_threadSignalForType(evt->type);
517 return 0;
518}
519
520static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
521{
522 DVR_WrapperPlaybackSegmentInfo_t *pseg, *pseg_tmp;
523 list_for_each_entry_safe(pseg, pseg_tmp, &ctx->segments, head) {
524 list_del(&pseg->head);
525 free(pseg);
526 }
527}
528
529static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *pseg,
530 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
531{
532 (void)ctx;
533 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
534 pseg->seg_info = *seg_info;
535 else if (update_flags & U_PIDS) {
536 pseg->seg_info.nb_pids = seg_info->nb_pids;
537 memcpy(pseg->seg_info.pids, seg_info->pids, sizeof(pseg->seg_info.pids));
538 } else if (update_flags & U_STAT) {
539 pseg->seg_info.duration = seg_info->duration;
540 pseg->seg_info.size = seg_info->size;
541 pseg->seg_info.nb_packets = seg_info->nb_packets;
542 }
543
544 /*no changes
545 DVR_PlaybackSegmentFlag_t flags;
546 pseg->playback_info.segment_id = pseg->seg_info.id;
547 strncpy(pseg->playback_info.location,
548 ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
549 pseg->playback_info.pids = ctx->playback.pids_req;
550 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
551 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
552 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
553 pseg->playback_info.flags = flags;
554 */
555}
556
557static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
558{
559 DVR_WrapperPlaybackSegmentInfo_t *pseg;
560
561 DVR_WRAPPER_DEBUG(1, "timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
562 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
563
564 if (list_empty(&ctx->segments)) {
565 DVR_WRAPPER_DEBUG(1, "timeshift, update while no segment exists, ignore\n");
566 return DVR_SUCCESS;
567 }
568
569 /*normally, the last segment added will be updated*/
570 pseg =
571 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
572 if (pseg->seg_info.id == seg_info->id) {
573 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
574 } else {
575 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
576 if (pseg->seg_info.id == seg_info->id) {
577 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
578 break;
579 }
580 }
581 }
582
583 /*need to notify the dvr_playback*/
584 if (ctx->playback.param_open.is_timeshift/*should must be timeshift*/
585 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
586 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
587 if (
588 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
589 (ctx->playback.seg_status.segment_id == seg_info->id
590 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
591 ||
592 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
593 (ctx->playback.seg_status.segment_id != seg_info->id
594 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
595 )
596 {
597 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800598 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800599 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800600 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800601
602 error = dvr_playback_resume(ctx->playback.player);
603 DVR_WRAPPER_DEBUG(1, "timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n",
604 ctx->sn, error,
605 seg_info->id, seg_info->duration,
606 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
607 }
608 }
609
610 return DVR_SUCCESS;
611}
612
613static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *pseg,
614 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
615{
616 (void)ctx;
617 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
618 pseg->info = *seg_info;
619 else if (update_flags & U_PIDS) {
620 pseg->info.nb_pids = seg_info->nb_pids;
621 memcpy(pseg->info.pids, seg_info->pids, sizeof(pseg->info.pids));
622 } else if (update_flags & U_STAT) {
623 pseg->info.duration = seg_info->duration;
624 pseg->info.size = seg_info->size;
625 pseg->info.nb_packets = seg_info->nb_packets;
626 }
627}
628
629static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
630{
hualing chen266b9502020-04-04 17:39:39 +0800631 DVR_WrapperRecordSegmentInfo_t *pseg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800632
633 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800634 if (!list_empty(&ctx->segments)) {
635 pseg =
636 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
637 if (pseg->info.id == seg_info->id) {
638 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
639 } else {
640 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
641 if (pseg->info.id == seg_info->id) {
642 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
643 break;
644 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800645 }
646 }
647 }
648
649 /*timeshift, update the segment for playback*/
650 /*
651 the playback should grab the segment info other than the id,
652 and the id will be updated by each segment-add during the recording
653 */
654 /*
655 the playback paused if no data been checked from recording,
656 should resume the player later when there's more data
657 */
658
659 if (ctx->record.param_open.is_timeshift) {
660 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
661
662 if (ctx_playback) {
663 pthread_mutex_lock(&ctx_playback->lock);
664 if (ctx_valid(ctx_playback)
665 && ctx_playback->sn == sn_timeshift_playback) {
666 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
667 }
668 pthread_mutex_unlock(&ctx_playback->lock);
669 }
670 }
671
672 return DVR_SUCCESS;
673}
674
675static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
676 DVR_RecordSegmentInfo_t *seg_info,
677 DVR_PlaybackPids_t *p_pids,
678 DVR_PlaybackSegmentFlag_t flags)
679{
680 DVR_WrapperPlaybackSegmentInfo_t *pseg;
681 int error;
682
683 error = 0;
684 pseg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
685 if (!pseg) {
686 error = DVR_FAILURE;
687 DVR_WRAPPER_DEBUG(1, "memory fail\n");
688 return error;
689 }
690
691 /*copy the orignal segment info*/
692 pseg->seg_info = *seg_info;
693 /*generate the segment info used in playback*/
694 pseg->playback_info.segment_id = pseg->seg_info.id;
695 strncpy(pseg->playback_info.location, ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
696 pseg->playback_info.pids = *p_pids;
697 pseg->playback_info.flags = flags;
698 list_add(&pseg->head, &ctx->segments);
699
700 error = dvr_playback_add_segment(ctx->playback.player, &pseg->playback_info);
701 if (error) {
702 DVR_WRAPPER_DEBUG(1, "fail to add segment %lld (%d)\n", pseg->playback_info.segment_id, error);
703 } else {
704 ctx->playback.status.info_full.time += pseg->seg_info.duration;
705 ctx->playback.status.info_full.size += pseg->seg_info.size;
706 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
707 }
708
709 return error;
710}
711
712static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
713{
714 DVR_WrapperRecordSegmentInfo_t *pseg;
715 int error;
716
717 error = 0;
718 pseg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
719 if (!pseg) {
720 error = DVR_FAILURE;
721 DVR_WRAPPER_DEBUG(1, "memory fail\n");
722 }
723 pseg->info = *seg_info;
724 list_add(&pseg->head, &ctx->segments);
725
726 if (ctx->record.param_open.is_timeshift) {
727 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
728
729 if (ctx_playback) {
730 pthread_mutex_lock(&ctx_playback->lock);
731 if (ctx_valid(ctx_playback)) {
732 DVR_PlaybackSegmentFlag_t flags;
733
734 /*only if playback has started, the previous segments have been loaded*/
735 if (!list_empty(&ctx_playback->segments)) {
736 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
737 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
738 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
739 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
740 }
741 }
742 pthread_mutex_unlock(&ctx_playback->lock);
743 }
744 }
745
746 return error;
747}
748
749static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
750{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800751 int error = -1;
752 DVR_WrapperPlaybackSegmentInfo_t *pseg = NULL, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800753
754 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
755
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800756 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800757 if (pseg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800758
759 if (ctx->current_segment_id == seg_info->id) {
760 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
761
762 /*drive the player out of this will-be-deleted segment*/
763 next_seg = list_prev_entry(pseg, head);
764
765 if (ctx->playback.speed != 100.0f) {
766 error = dvr_playback_resume(ctx->playback.player);
767 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
768 }
769
770 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, 0);
771 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);
772
773 if (ctx->playback.speed == 0.0f) {
774 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
775 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last paused from new start (%d)\n", ctx->sn, error);
776 } else if (ctx->playback.speed != 100.0f) {
777 DVR_PlaybackSpeed_t dvr_speed = {
778 .speed = { ctx->playback.speed },
779 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
780 };
781 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
782 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last speed(x%f) from new start (%d)\n", ctx->sn,ctx->playback.speed, error);
783 }
784 }
785
786 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
787 if (error) {
788 /*remove playack segment fail*/
789 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), failed to remove segment(%llu) (%d)\n", ctx->sn, seg_info->id, error);
790 }
791
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800792 list_del(&pseg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800793
794 /*record the obsolete*/
795 ctx->playback.obsolete.time += pseg->seg_info.duration;
796 ctx->playback.obsolete.size += pseg->seg_info.size;
797 ctx->playback.obsolete.pkts += pseg->seg_info.nb_packets;
798
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800799 free(pseg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800800 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800801 }
802 }
803
804 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->id, error);
805
806 return error;
807}
808
809static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
810{
811 int error;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800812 DVR_WrapperRecordSegmentInfo_t *pseg, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800813
814 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->info.id);
815
816 /*if timeshifting, notify the playback first, then deal with record*/
817 if (ctx->record.param_open.is_timeshift) {
818 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
819
820 if (ctx_playback) {
821 pthread_mutex_lock(&ctx_playback->lock);
822 if (ctx_valid(ctx_playback)
823 && ctx_playback->sn == sn_timeshift_playback
824 && !list_empty(&ctx_playback->segments)) {
825 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
826 }
827 pthread_mutex_unlock(&ctx_playback->lock);
828 }
829 }
830
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800831 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
832 if (pseg->info.id == seg_info->info.id) {
833 list_del(&pseg->head);
834
835 /*record the obsolete*/
836 ctx->record.obsolete.time += pseg->info.duration;
837 ctx->record.obsolete.size += pseg->info.size;
838 ctx->record.obsolete.pkts += pseg->info.nb_packets;
839
840 free(pseg);
841 break;
842 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800843 }
844
845 error = dvr_segment_delete(ctx->record.param_open.location, seg_info->info.id);
846
847 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->info.id, error);
848
849 return error;
850}
851
852int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
853{
854 int error;
855 DVR_WrapperCtx_t *ctx;
856 DVR_RecordOpenParams_t open_param;
857
858 DVR_RETURN_IF_FALSE(rec);
859 DVR_RETURN_IF_FALSE(params);
860
861 /*get a free ctx*/
862 ctx = ctx_getRecord(0);
863 DVR_RETURN_IF_FALSE(ctx);
864
865 pthread_mutex_lock(&ctx->lock);
866
hualing chen51652f02020-12-29 16:59:31 +0800867 DVR_WRAPPER_DEBUG(1, "open record(dmx:%d) .istf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
868 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800869
870 ctx_reset(ctx);
871
872 ctx->record.param_open = *params;
873 ctx->record.event_fn = params->event_fn;
874 ctx->record.event_userdata = params->event_userdata;
875 ctx->record.next_segment_id = 0;
876 ctx->current_segment_id = 0;
877 INIT_LIST_HEAD(&ctx->segments);
878 ctx->sn = get_sn();
879
880 wrapper_requestThreadFor(ctx);
881
hualing chen266b9502020-04-04 17:39:39 +0800882 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +0800883 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800884 open_param.dmx_dev_id = params->dmx_dev_id;
885 open_param.data_from_memory = 0;
886 open_param.flags = params->flags;
Zhiqiang Han104aed72020-04-03 19:37:43 +0800887 open_param.notification_size = 500*1024;
Zhiqiang Han31505452020-05-06 15:08:10 +0800888 open_param.flush_size = params->flush_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800889 open_param.event_fn = wrapper_record_event_handler;
890 open_param.event_userdata = (void*)ctx->sn;
891
892 error = dvr_record_open(&ctx->record.recorder, &open_param);
893 if (error) {
894 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
895 ctx_reset(ctx);
896 pthread_mutex_unlock(&ctx->lock);
897 wrapper_releaseThreadForType(ctx->type);
898 return DVR_FAILURE;
899 }
900 if (params->is_timeshift)
901 sn_timeshift_record = ctx->sn;
902
903 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
904
hualing chen266b9502020-04-04 17:39:39 +0800905 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
906 if (error) {
907 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) set encrypt callback fail(error:%d).\n", params->dmx_dev_id, error);
908 }
909
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800910 pthread_mutex_unlock(&ctx->lock);
911
912 *rec = (DVR_WrapperRecord_t)ctx->sn;
913 return DVR_SUCCESS;
914}
915
916int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
917{
918 DVR_WrapperCtx_t *ctx;
919 DVR_RecordSegmentInfo_t seg_info;
920 int error;
921
922 DVR_RETURN_IF_FALSE(rec);
923
924 ctx = ctx_getRecord((unsigned long)rec);
925 DVR_RETURN_IF_FALSE(ctx);
926
927 pthread_mutex_lock(&ctx->lock);
928 DVR_WRAPPER_DEBUG(1, "close record(sn:%ld)\n", ctx->sn);
929 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
930
931 memset(&seg_info, 0, sizeof(seg_info));
932 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
933
934 error = dvr_record_close(ctx->record.recorder);
935
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800936 if (ctx->record.param_open.is_timeshift)
937 sn_timeshift_record = 0;
938
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800939 ctx_freeSegments(ctx);
940
941 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) closed = (%d).\n", ctx->sn, error);
942 ctx_reset(ctx);
943 pthread_mutex_unlock(&ctx->lock);
944
945 wrapper_releaseThreadForType(ctx->type);
946
947 return error;
948}
949
950int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
951{
952 DVR_WrapperCtx_t *ctx;
953 DVR_RecordStartParams_t *start_param;
954 int i;
955 int error;
956
957 DVR_RETURN_IF_FALSE(rec);
958 DVR_RETURN_IF_FALSE(params);
959
960 ctx = ctx_getRecord((unsigned long)rec);
961 DVR_RETURN_IF_FALSE(ctx);
962
963 pthread_mutex_lock(&ctx->lock);
964 DVR_WRAPPER_DEBUG(1, "start record(sn:%ld, location:%s) ...\n", ctx->sn, ctx->record.param_open.location);
965 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
966
967 start_param = &ctx->record.param_start;
968 memset(start_param, 0, sizeof(*start_param));
969 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
970 start_param->segment.segment_id = ctx->record.next_segment_id++;
971 start_param->segment.nb_pids = params->pids_info.nb_pids;
972 for (i = 0; i < params->pids_info.nb_pids; i++) {
973 start_param->segment.pids[i] = params->pids_info.pids[i];
974 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
975 }
hualing chen7a56cba2020-04-14 14:09:27 +0800976 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800977 {
978 /*sync to update for further use*/
979 DVR_RecordStartParams_t *update_param;
980 update_param = &ctx->record.param_update;
981 memcpy(update_param, start_param, sizeof(*update_param));
982 for (i = 0; i < update_param->segment.nb_pids; i++)
983 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
984 }
985
986 error = dvr_record_start_segment(ctx->record.recorder, start_param);
987 {
988 DVR_RecordSegmentInfo_t new_seg_info =
989 { .id = start_param->segment.segment_id, };
990 wrapper_addRecordSegment(ctx, &new_seg_info);
991 }
992
993 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) started = (%d)\n", ctx->sn, error);
994
995 pthread_mutex_unlock(&ctx->lock);
996
997 return error;
998}
999
1000int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1001{
1002 DVR_WrapperCtx_t *ctx;
1003 DVR_RecordSegmentInfo_t seg_info;
1004 int error;
1005
1006 DVR_RETURN_IF_FALSE(rec);
1007
1008 ctx = ctx_getRecord((unsigned long)rec);
1009 DVR_RETURN_IF_FALSE(ctx);
1010
1011 pthread_mutex_lock(&ctx->lock);
1012 DVR_WRAPPER_DEBUG(1, "stop record(sn:%ld) ...\n", ctx->sn);
1013 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1014
1015 memset(&seg_info, 0, sizeof(seg_info));
1016 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1017 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1018
1019 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
1020 pthread_mutex_unlock(&ctx->lock);
1021
1022 return error;
1023}
1024
1025int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1026{
1027 DVR_WrapperCtx_t *ctx;
1028 DVR_RecordStartParams_t *start_param;
1029 DVR_RecordSegmentInfo_t seg_info;;
1030 int i;
1031 int error;
1032
1033 DVR_RETURN_IF_FALSE(rec);
1034 DVR_RETURN_IF_FALSE(params);
1035
1036 ctx = ctx_getRecord((unsigned long)rec);
1037 DVR_RETURN_IF_FALSE(ctx);
1038
1039 pthread_mutex_lock(&ctx->lock);
1040 DVR_WRAPPER_DEBUG(1, "update record(sn:%ld)\n", ctx->sn);
1041 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1042
1043 start_param = &ctx->record.param_update;
1044 memset(start_param, 0, sizeof(*start_param));
1045 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1046 start_param->segment.segment_id = ctx->record.next_segment_id++;
1047 start_param->segment.nb_pids = params->nb_pids;
1048 for (i = 0; i < params->nb_pids; i++) {
1049 start_param->segment.pids[i] = params->pids[i];
1050 start_param->segment.pid_action[i] = params->pid_action[i];
1051 }
1052 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1053 {
1054 DVR_RecordSegmentInfo_t new_seg_info =
1055 { .id = start_param->segment.segment_id, };
1056 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1057 wrapper_addRecordSegment(ctx, &new_seg_info);
1058 }
1059
1060 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) updated = (%d)\n", ctx->sn, error);
1061 pthread_mutex_unlock(&ctx->lock);
1062
1063 return error;
1064}
1065
1066int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1067{
1068 DVR_WrapperCtx_t *ctx;
1069 DVR_WrapperRecordStatus_t s;
1070 int error;
1071
1072 DVR_RETURN_IF_FALSE(rec);
1073 DVR_RETURN_IF_FALSE(status);
1074
1075 ctx = ctx_getRecord((unsigned long)rec);
1076 DVR_RETURN_IF_FALSE(ctx);
1077
1078 pthread_mutex_lock(&ctx->lock);
1079
1080 DVR_WRAPPER_DEBUG(1, "get record(sn:%ld) status ...\n", ctx->sn);
1081 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1082
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001083 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001084
1085 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
1086 ctx->sn,
1087 s.state,
1088 s.info.time,
1089 s.info.size,
1090 s.info.pkts,
1091 error);
1092
1093 *status = s;
1094
1095 pthread_mutex_unlock(&ctx->lock);
1096
1097 return error;
1098}
1099
hualing chen4fe3bee2020-10-23 13:58:52 +08001100int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1101{
1102 DVR_WrapperCtx_t *ctx;
1103 int error;
1104
1105 DVR_RETURN_IF_FALSE(rec);
1106
1107 ctx = ctx_getRecord((unsigned long)rec);
1108 DVR_RETURN_IF_FALSE(ctx);
1109
1110 pthread_mutex_lock(&ctx->lock);
1111 error = dvr_record_is_secure_mode(ctx->record.recorder);
1112 pthread_mutex_unlock(&ctx->lock);
1113 return error;
1114}
1115
hualing chen266b9502020-04-04 17:39:39 +08001116int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1117{
1118 DVR_WrapperCtx_t *ctx;
1119 int error;
1120
1121 DVR_RETURN_IF_FALSE(rec);
1122 DVR_RETURN_IF_FALSE(p_secure_buf);
1123
1124 ctx = ctx_getRecord((unsigned long)rec);
1125 DVR_RETURN_IF_FALSE(ctx);
1126
1127 pthread_mutex_lock(&ctx->lock);
1128 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
1129 pthread_mutex_unlock(&ctx->lock);
1130 return error;
1131}
1132
1133int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1134{
1135 DVR_WrapperCtx_t *ctx;
1136 int error;
1137
1138 DVR_RETURN_IF_FALSE(rec);
1139 DVR_RETURN_IF_FALSE(func);
1140
1141 ctx = ctx_getRecord((unsigned long)rec);
1142 DVR_RETURN_IF_FALSE(ctx);
1143
1144 pthread_mutex_lock(&ctx->lock);
1145 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
1146 pthread_mutex_unlock(&ctx->lock);
1147 return error;
1148}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001149
1150
1151int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1152{
1153 DVR_WrapperCtx_t *ctx;
1154 DVR_PlaybackOpenParams_t open_param;
1155 int error;
1156
1157 DVR_RETURN_IF_FALSE(playback);
1158 DVR_RETURN_IF_FALSE(params);
1159 DVR_RETURN_IF_FALSE(params->playback_handle);
1160
1161 /*get a free ctx*/
1162 ctx = ctx_getPlayback(0);
1163 DVR_RETURN_IF_FALSE(ctx);
1164
1165 pthread_mutex_lock(&ctx->lock);
1166
1167 DVR_WRAPPER_DEBUG(1, "open playback(dmx:%d) ...\n", params->dmx_dev_id);
1168
1169 ctx_reset(ctx);
1170
1171 ctx->playback.param_open = *params;
1172 ctx->playback.event_fn = params->event_fn;
1173 ctx->playback.event_userdata = params->event_userdata;
1174 ctx->current_segment_id = 0;
1175 INIT_LIST_HEAD(&ctx->segments);
1176 ctx->sn = get_sn();
1177
1178 wrapper_requestThreadFor(ctx);
1179
hualing chen266b9502020-04-04 17:39:39 +08001180 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001181 open_param.dmx_dev_id = params->dmx_dev_id;
1182 open_param.block_size = params->block_size;
1183 open_param.is_timeshift = params->is_timeshift;
1184 //open_param.notification_size = 10*1024; //not supported
1185 open_param.event_fn = wrapper_playback_event_handler;
1186 open_param.event_userdata = (void*)ctx->sn;
1187 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001188 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001189 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chenfbf8e022020-06-15 13:43:11 +08001190 open_param.vendor = DVR_PLAYBACK_VENDOR_AML;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001191
1192 error = dvr_playback_open(&ctx->playback.player, &open_param);
1193 if (error) {
1194 DVR_WRAPPER_DEBUG(1, "playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
1195 ctx_reset(ctx);
1196 pthread_mutex_unlock(&ctx->lock);
1197 wrapper_releaseThreadForType(ctx->type);
1198 return DVR_FAILURE;
1199 }
1200 if (params->is_timeshift)
1201 sn_timeshift_playback = ctx->sn;
1202
hualing chen266b9502020-04-04 17:39:39 +08001203 DVR_WRAPPER_DEBUG(1, "hanyh: playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
1204 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1205 if (error) {
1206 DVR_WRAPPER_DEBUG(1, "playback set deccrypt callback fail(error:%d).\n", error);
1207 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001208 pthread_mutex_unlock(&ctx->lock);
1209
1210 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1211 return DVR_SUCCESS;
1212}
1213
1214int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1215{
1216 DVR_WrapperCtx_t *ctx;
1217 int error;
1218
1219 DVR_RETURN_IF_FALSE(playback);
1220
1221 ctx = ctx_getPlayback((unsigned long)playback);
1222 DVR_RETURN_IF_FALSE(ctx);
1223
1224 pthread_mutex_lock(&ctx->lock);
1225 DVR_WRAPPER_DEBUG(1, "close playback(sn:%ld)\n", ctx->sn);
1226 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1227
1228 if (ctx->playback.param_open.is_timeshift)
1229 sn_timeshift_playback = 0;
1230
1231 /*try stop first*/
1232 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1233
1234 {
1235 /*remove all segments*/
1236 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1237
1238 list_for_each_entry(pseg, &ctx->segments, head) {
1239 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1240 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1241 ctx->sn, pseg->playback_info.segment_id, error);
1242 }
1243 ctx_freeSegments(ctx);
1244 }
1245
1246 error = dvr_playback_close(ctx->playback.player);
1247
1248 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) closed.\n", ctx->sn);
1249 ctx_reset(ctx);
1250 pthread_mutex_unlock(&ctx->lock);
1251
1252 wrapper_releaseThreadForType(ctx->type);
1253
1254 return error;
1255}
1256
1257int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1258{
1259 DVR_WrapperCtx_t *ctx;
1260 int error;
1261 uint64_t *p_segment_ids;
1262 uint32_t segment_nb;
1263 uint32_t i;
1264 DVR_RecordSegmentInfo_t seg_info_1st;
1265 int got_1st_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001266 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
1267
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001268
1269 DVR_RETURN_IF_FALSE(playback);
1270 DVR_RETURN_IF_FALSE(p_pids);
1271
1272 ctx = ctx_getPlayback((unsigned long)playback);
1273 DVR_RETURN_IF_FALSE(ctx);
1274
1275 pthread_mutex_lock(&ctx->lock);
1276
1277 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",
1278 ctx->sn,
1279 ctx->playback.param_open.location,
1280 flags,
1281 p_pids->video.pid, p_pids->video.format,
1282 p_pids->audio.pid, p_pids->audio.format,
1283 p_pids->ad.pid, p_pids->ad.format,
1284 p_pids->subtitle.pid, p_pids->subtitle.format,
1285 p_pids->pcr.pid);
1286
1287 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1288
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001289 ctx_record = NULL;
1290 if (ctx->playback.param_open.is_timeshift) {
1291 /*lock the recorder to avoid changing the recording segments*/
1292 ctx_record = ctx_getRecord(sn_timeshift_record);
1293
1294 if (ctx_record) {
1295 pthread_mutex_lock(&ctx_record->lock);
1296 if (!ctx_valid(ctx_record)
1297 || ctx_record->sn != sn_timeshift_record) {
1298 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error\n");
1299 pthread_mutex_unlock(&ctx_record->lock);
1300 } else {
1301 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
1302 ctx->sn, ctx_record->sn);
1303 }
1304 }
1305 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_record, &ctx->lock);
1306 }
1307
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001308 /*obtain all segments in a list*/
1309 segment_nb = 0;
1310 p_segment_ids = NULL;
1311 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001312 if (!error) {
1313 got_1st_seg = 0;
1314 for (i = 0; i < segment_nb; i++) {
1315 DVR_RecordSegmentInfo_t seg_info;
1316 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001317
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001318 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1319 if (error) {
1320 error = DVR_FAILURE;
1321 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1322 ctx->playback.param_open.location, p_segment_ids[i], error);
1323 break;
1324 }
hualing chen92f3a142020-07-08 20:59:33 +08001325 //add check if has audio or video pid. if not exist. not add segment to playback
1326 int ii = 0;
1327 int has_av = 0;
1328 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1329 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1330 if (type == DVR_STREAM_TYPE_VIDEO ||
1331 type == DVR_STREAM_TYPE_AUDIO ||
1332 type == DVR_STREAM_TYPE_AD) {
1333 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1334 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,
1335 DVR_STREAM_TYPE_VIDEO,
1336 DVR_STREAM_TYPE_AUDIO,
1337 DVR_STREAM_TYPE_AD);
1338 has_av = 1;
1339 //break;
1340 } else {
1341 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,
1342 DVR_STREAM_TYPE_VIDEO,
1343 DVR_STREAM_TYPE_AUDIO,
1344 DVR_STREAM_TYPE_AD);
1345 }
1346 }
1347 if (has_av == 0) {
1348 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1349 continue;
1350 } else {
1351 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1352 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001353 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1354 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1355 if (error)
1356 break;
1357
1358 /*copy the 1st segment*/
1359 if (got_1st_seg == 0) {
1360 seg_info_1st = seg_info;
1361 got_1st_seg = 1;
1362 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001363 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001364 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001365
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001366 /* return if no segment or fail to add */
1367 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001368
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001369 /*copy the obsolete infomation, must for timeshifting*/
1370 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1371 ctx->playback.obsolete = ctx_record->record.obsolete;
1372 }
1373
1374 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
1375
1376 ctx->playback.reach_end = DVR_FALSE;
1377 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1378 ctx->playback.speed = 0.0f;
1379 else
1380 ctx->playback.speed = 100.0f;
1381
1382 ctx->playback.pids_req = *p_pids;
1383
1384 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1385 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
1386 ctx->sn, seg_info_1st.id, error);
1387
1388 error = dvr_playback_start(ctx->playback.player, flags);
1389
1390 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001391 }
1392 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001393
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001394 if (ctx->playback.param_open.is_timeshift) {
1395 /*unlock the recorder locked above*/
1396 if (ctx_record && ctx_valid(ctx_record)) {
1397 pthread_mutex_unlock(&ctx_record->lock);
1398 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
1399 ctx->sn, ctx_record->sn);
1400 }
1401 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001402
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001403
1404 pthread_mutex_unlock(&ctx->lock);
1405
1406 return error;
1407}
1408
1409int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
1410{
1411 DVR_WrapperCtx_t *ctx;
1412 int error;
1413
1414 DVR_RETURN_IF_FALSE(playback);
1415
1416 ctx = ctx_getPlayback((unsigned long)playback);
1417 DVR_RETURN_IF_FALSE(ctx);
1418
1419 pthread_mutex_lock(&ctx->lock);
1420 DVR_WRAPPER_DEBUG(1, "stop playback(sn:%ld) ...\n", ctx->sn);
1421 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1422
1423 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1424
1425 {
1426 /*remove all segments*/
1427 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1428
1429 list_for_each_entry(pseg, &ctx->segments, head) {
1430 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1431 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1432 ctx->sn, pseg->playback_info.segment_id, error);
1433 }
1434 ctx_freeSegments(ctx);
1435 }
1436
1437 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
1438 pthread_mutex_unlock(&ctx->lock);
1439
1440 return error;
1441}
1442
1443int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
1444{
1445 DVR_WrapperCtx_t *ctx;
1446 int error;
1447
1448 DVR_RETURN_IF_FALSE(playback);
1449
1450 ctx = ctx_getPlayback((unsigned long)playback);
1451 DVR_RETURN_IF_FALSE(ctx);
1452
1453 pthread_mutex_lock(&ctx->lock);
1454 DVR_WRAPPER_DEBUG(1, "pause playback(sn:%ld) ...\n", ctx->sn);
1455 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08001456 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08001457 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08001458 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001459
1460 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
1461
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001462 ctx->playback.speed = 0.0f;
1463
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001464 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) paused (%d)\n", ctx->sn, error);
1465 pthread_mutex_unlock(&ctx->lock);
1466
1467 return error;
1468}
1469
1470int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
1471{
1472 DVR_WrapperCtx_t *ctx;
1473 int error;
1474
1475 DVR_RETURN_IF_FALSE(playback);
1476
1477 ctx = ctx_getPlayback((unsigned long)playback);
1478 DVR_RETURN_IF_FALSE(ctx);
1479
1480 pthread_mutex_lock(&ctx->lock);
1481 DVR_WRAPPER_DEBUG(1, "resume playback(sn:%ld) ...\n", ctx->sn);
1482 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1483
1484 error = dvr_playback_resume(ctx->playback.player);
1485
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001486 ctx->playback.speed = 100.0f;
1487
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001488 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
1489 pthread_mutex_unlock(&ctx->lock);
1490
1491 return error;
1492}
1493
1494int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
1495{
1496 DVR_WrapperCtx_t *ctx;
1497 int error;
1498 DVR_PlaybackSpeed_t dvr_speed = {
1499 .speed = { speed },
1500 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
1501 };
1502
1503 DVR_RETURN_IF_FALSE(playback);
1504
1505 ctx = ctx_getPlayback((unsigned long)playback);
1506 DVR_RETURN_IF_FALSE(ctx);
1507
1508 pthread_mutex_lock(&ctx->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001509 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 +08001510 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1511
1512 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
1513
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001514 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
1515 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
1516 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
1517 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
1518 error = dvr_playback_resume(ctx->playback.player);
1519 } else if (ctx->playback.speed == 0.0f
1520 && speed != 0.0f
1521 && speed != 100.0f) {
1522 /*libdvr do not support pause with speed=0, will not be here*/
1523 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
1524 error = dvr_playback_resume(ctx->playback.player);
1525 }
1526
1527 ctx->playback.speed = speed;
1528
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001529 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) speeded(x%f) (%d)\n",
1530 ctx->sn, speed, error);
1531 pthread_mutex_unlock(&ctx->lock);
1532
1533 return error;
1534}
1535
1536int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
1537{
1538 DVR_WrapperCtx_t *ctx;
1539 int error;
1540 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1541 uint64_t segment_id;
1542 uint32_t off;
1543 uint64_t last_segment_id;
1544 uint32_t pre_off;
1545
1546 DVR_RETURN_IF_FALSE(playback);
1547
1548 ctx = ctx_getPlayback((unsigned long)playback);
1549 DVR_RETURN_IF_FALSE(ctx);
1550
1551 pthread_mutex_lock(&ctx->lock);
1552
1553 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
1554 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1555
1556 off = 0;
1557 segment_id = 0;
1558 pre_off = 0;
1559 last_segment_id = 0;
1560
1561 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1562 segment_id = pseg->seg_info.id;
1563
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001564 if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset)
1565 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001566
1567 last_segment_id = pseg->seg_info.id;
1568 pre_off += pseg->seg_info.duration;
1569 }
1570
1571 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001572 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
1573 off = time_offset;
1574 } else if (ctx->playback.obsolete.time >= time_offset) {
1575 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001576 } else {
hualing chenda76fc52020-05-28 14:56:42 +08001577 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001578 }
1579
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001580 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (seg:%lld, off:%d)\n",
1581 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001582 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
1583 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
1584
1585 pthread_mutex_unlock(&ctx->lock);
1586
1587 return error;
1588}
1589
1590int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
1591{
1592 DVR_WrapperCtx_t *ctx;
1593 int error;
1594 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1595
1596 DVR_RETURN_IF_FALSE(playback);
1597 DVR_RETURN_IF_FALSE(p_pids);
1598
1599 ctx = ctx_getPlayback((unsigned long)playback);
1600 DVR_RETURN_IF_FALSE(ctx);
1601
1602 pthread_mutex_lock(&ctx->lock);
1603
1604 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
1605 ctx->sn,
1606 p_pids->video.pid, p_pids->video.format,
1607 p_pids->audio.pid, p_pids->audio.format);
1608 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1609
1610 ctx->playback.pids_req = *p_pids;
1611
1612 error = 0;
1613 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1614 /*should update the whole list of segments*/
1615 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
1616 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
1617 /*check udpate for pids*/
1618 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
1619 pseg->playback_info.pids = *p_pids;
1620 error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
1621 if (error) {
1622 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
1623 ctx->sn, pseg->seg_info.id, error);
1624 /*do not break, let list updated*/
1625 }
1626 }
1627 }
1628 /*break;*/
1629 }
1630 }
1631
1632 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
1633 ctx->sn,
1634 p_pids->video.pid, p_pids->video.format,
1635 p_pids->audio.pid, p_pids->audio.format,
1636 error);
1637
1638 pthread_mutex_unlock(&ctx->lock);
1639
1640 return error;
1641}
1642
1643int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
1644{
1645 DVR_WrapperCtx_t *ctx;
1646 DVR_WrapperPlaybackStatus_t s;
1647 DVR_PlaybackStatus_t play_status;
1648 int error;
1649
1650 DVR_RETURN_IF_FALSE(playback);
1651 DVR_RETURN_IF_FALSE(status);
1652
1653 ctx = ctx_getPlayback((unsigned long)playback);
1654 DVR_RETURN_IF_FALSE(ctx);
1655
1656 pthread_mutex_lock(&ctx->lock);
1657
1658 DVR_WRAPPER_DEBUG(1, "get playback(sn:%ld) status ...\n", ctx->sn);
1659 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1660
1661 error = dvr_playback_get_status(ctx->playback.player, &play_status);
1662 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) get status (%d)\n", ctx->sn, error);
1663
1664 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001665 error = process_generatePlaybackStatus(ctx, &s);
1666
hualing chenb5cd42e2020-04-15 17:03:34 +08001667 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
1668 //reach end need set full time to cur.so app can exist playback.
1669 DVR_WRAPPER_DEBUG(1, "set cur time to full time, reach end occur");
1670 s.info_cur.time = s.info_full.time;
1671 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001672 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001673 ctx->sn,
1674 s.state,
1675 s.info_cur.time,
1676 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001677 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001678 error);
1679
1680 *status = s;
1681
1682 pthread_mutex_unlock(&ctx->lock);
1683
1684 return error;
1685}
1686
hualing chen266b9502020-04-04 17:39:39 +08001687int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
1688{
1689 DVR_WrapperCtx_t *ctx;
1690 int error;
1691
1692 DVR_RETURN_IF_FALSE(playback);
1693 DVR_RETURN_IF_FALSE(p_secure_buf);
1694
1695 ctx = ctx_getPlayback((unsigned long)playback);
1696 DVR_RETURN_IF_FALSE(ctx);
1697
1698 pthread_mutex_lock(&ctx->lock);
1699 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
1700 pthread_mutex_unlock(&ctx->lock);
1701 return error;
1702}
1703
1704int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
1705{
1706 DVR_WrapperCtx_t *ctx;
1707 int error;
1708
1709 DVR_RETURN_IF_FALSE(playback);
1710 DVR_RETURN_IF_FALSE(func);
1711
1712 ctx = ctx_getPlayback((unsigned long)playback);
1713 DVR_RETURN_IF_FALSE(ctx);
1714
1715 pthread_mutex_lock(&ctx->lock);
1716 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
1717 pthread_mutex_unlock(&ctx->lock);
1718 return error;
1719}
1720
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001721static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
1722{
1723 DVR_WrapperEventCtx_t evt;
1724
1725 DVR_RETURN_IF_FALSE(userdata);
1726
1727 evt.sn = (unsigned long)userdata;
1728 evt.type = W_REC;
1729 evt.record.event = event;
1730 evt.record.status = *(DVR_RecordStatus_t *)params;
1731 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
1732 return ctx_addRecordEvent(&evt);
1733}
1734
1735static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
1736{
1737 DVR_WrapperEventCtx_t evt;
1738
1739 DVR_RETURN_IF_FALSE(userdata);
1740
1741 evt.sn = (unsigned long)userdata;
1742 evt.type = W_PLAYBACK;
1743 evt.playback.event = event;
1744 evt.playback.status = *(DVR_Play_Notify_t *)params;
1745 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event);
1746 return ctx_addPlaybackEvent(&evt);
1747}
1748
1749static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
1750{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001751 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 +08001752 ctx->sn,
1753 evt,
1754 status->info.time,
1755 status->info.size,
1756 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001757 status->info_obsolete.time,
1758 status->info_obsolete.size,
1759 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001760
1761 if (ctx->record.event_fn)
1762 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
1763 return 0;
1764}
1765
1766static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
1767{
1768 DVR_RecordStartParams_t param;
1769 DVR_RecordSegmentInfo_t seg_info;
1770 int i;
1771 int error;
1772
1773 memcpy(&param, &ctx->record.param_update, sizeof(param));
1774 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
1775 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
1776 for (i = 0; i < param.segment.nb_pids; i++) {
1777 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
1778 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
1779 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
1780 ctx->record.param_update.segment.nb_pids++;
1781 }
1782 }
1783 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
1784 {
1785 DVR_RecordSegmentInfo_t new_seg_info =
1786 { .id = ctx->record.param_update.segment.segment_id, };
1787 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1788 wrapper_addRecordSegment(ctx, &new_seg_info);
1789 }
1790
Zhiqiang Hand977e972020-05-11 11:30:47 +08001791 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 +08001792 return error;
1793}
1794
1795static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg)
1796{
1797 return wrapper_removeRecordSegment(ctx, pseg);
1798}
1799
1800/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001801static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001802{
1803 /*the current seg is not covered in the statistics*/
1804 DVR_WrapperRecordSegmentInfo_t *pseg;
1805
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001806 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001807 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
1808
1809 ctx->record.status.state = ctx->record.seg_status.state;
1810 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
1811 memcpy(ctx->record.status.pids.pids,
1812 ctx->record.seg_status.info.pids,
1813 sizeof(ctx->record.status.pids.pids));
1814 ctx->current_segment_id = ctx->record.seg_status.info.id;
1815
1816 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1817 if (pseg->info.id != ctx->record.seg_status.info.id) {
1818 ctx->record.status.info.time += pseg->info.duration;
1819 ctx->record.status.info.size += pseg->info.size;
1820 ctx->record.status.info.pkts += pseg->info.nb_packets;
1821 }
1822 }
1823
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001824 ctx->record.status.info_obsolete = ctx->record.obsolete;
1825
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001826 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001827
1828 if (status) {
1829 *status = ctx->record.status;
1830 status->info.time += ctx->record.seg_status.info.duration;
1831 status->info.size += ctx->record.seg_status.info.size;
1832 status->info.pkts += ctx->record.seg_status.info.nb_packets;
1833 }
1834
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001835 return DVR_SUCCESS;
1836}
1837
1838
1839static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
1840{
1841 DVR_WrapperRecordStatus_t status;
1842
1843 memset(&status, 0, sizeof(status));
1844
1845 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d)\n",
1846 evt->sn, evt->record.event, evt->record.status.state);
1847
1848 switch (evt->record.event)
1849 {
1850 case DVR_RECORD_EVENT_STATUS:
1851 {
1852 switch (evt->record.status.state)
1853 {
1854 case DVR_RECORD_STATE_OPENED:
1855 case DVR_RECORD_STATE_CLOSED:
1856 {
1857 ctx->record.seg_status = evt->record.status;
1858
1859 status.state = evt->record.status.state;
1860 process_notifyRecord(ctx, evt->record.event, &status);
1861 } break;
1862 case DVR_RECORD_STATE_STARTED:
1863 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001864 ctx->record.seg_status = evt->record.status;
1865
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001866 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001867 process_notifyRecord(ctx, evt->record.event, &status);
1868
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001869 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001870 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001871 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
1872 DVR_WRAPPER_DEBUG(1, "start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n",
1873 ctx->sn,
1874 evt->record.status.info.size,
1875 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08001876 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
1877 /*should notify the recording's stop*/
1878 int error = dvr_record_close(ctx->record.recorder);
1879 DVR_WRAPPER_DEBUG(1, "stop record(%lu)=%d, failed to start new segment for recording.",
1880 ctx->sn, error);
1881 status.state = DVR_RECORD_STATE_CLOSED;
1882 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
1883 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001884 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001885
1886 if (ctx->record.param_open.is_timeshift
1887 && ctx->record.param_open.max_time
1888 && status.info.time >= ctx->record.param_open.max_time) {
1889 DVR_WrapperRecordSegmentInfo_t *pseg;
1890
1891 /*as the player do not support null playlist,
1892 there must be one segment existed at any time,
1893 we have to keep two segments before remove one*/
1894 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1895 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1896 /*only one segment, waiting for more*/
1897 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n",
1898 status.info.size,
1899 ctx->record.param_open.max_time,
1900 ctx->record.param_open.segment_size);
1901 } else {
1902 /*timeshifting, remove the 1st segment and notify the player*/
1903 record_removeSegment(ctx, pseg);
1904
1905 process_generateRecordStatus(ctx, &status);
1906 process_notifyRecord(ctx, evt->record.event, &status);
1907 }
1908 }
1909
1910 if (ctx->record.param_open.is_timeshift
1911 && ctx->record.param_open.max_size
1912 && status.info.size >= ctx->record.param_open.max_size) {
1913 DVR_WrapperRecordSegmentInfo_t *pseg;
1914
1915 /*as the player do not support null playlist,
1916 there must be one segment existed at any time,
1917 we have to keep two segments before remove one*/
1918 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1919 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1920 /*only one segment, waiting for more*/
1921 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of record < max size of segment(%lld)\n",
1922 status.info.size,
1923 ctx->record.param_open.segment_size);
1924 } else {
1925 record_removeSegment(ctx, pseg);
1926
1927 process_generateRecordStatus(ctx, &status);
1928 process_notifyRecord(ctx, evt->record.event, &status);
1929 }
1930 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001931 } break;
1932 case DVR_RECORD_STATE_STOPPED:
1933 {
1934 ctx->record.seg_status = evt->record.status;
1935
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001936 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001937 process_notifyRecord(ctx, evt->record.event, &status);
1938 } break;
1939 default:
1940 break;
1941 }
1942 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08001943 case DVR_RECORD_EVENT_WRITE_ERROR: {
1944 ctx->record.seg_status = evt->record.status;
1945 status.state = evt->record.status.state;
1946 process_notifyRecord(ctx, evt->record.event, &status);
1947 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001948 default:
1949 break;
1950 }
1951 return DVR_SUCCESS;
1952}
1953
1954static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
1955{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001956 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 +08001957 ctx->sn,
1958 evt,
1959 status->state,
1960 status->info_cur.time,
1961 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001962 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001963
1964 if (ctx->playback.event_fn)
1965 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
1966 return 0;
1967}
1968
1969/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001970static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001971{
1972 /*the current seg is not covered in the statistics*/
1973 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1974
1975 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
1976 ctx->playback.status.pids = ctx->playback.pids_req;
1977
1978 ctx->playback.status.state = ctx->playback.seg_status.state;
1979 ctx->playback.status.speed = ctx->playback.seg_status.speed;
1980 ctx->playback.status.flags = ctx->playback.seg_status.flags;
1981 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
1982
1983 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1984 if (pseg->seg_info.id == ctx->playback.seg_status.segment_id)
1985 break;
1986 ctx->playback.status.info_cur.time += pseg->seg_info.duration;
1987 ctx->playback.status.info_cur.size += pseg->seg_info.size;
1988 ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets;
1989 }
1990 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1991 ctx->playback.status.info_full.time += pseg->seg_info.duration;
1992 ctx->playback.status.info_full.size += pseg->seg_info.size;
1993 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
1994 }
1995
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001996 if (status) {
1997 *status = ctx->playback.status;
1998 /*deal with current, lack size and pkts with the current*/
1999 status->info_cur.time += ctx->playback.seg_status.time_cur;
2000 status->info_obsolete.time = ctx->playback.obsolete.time;
2001 }
2002
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002003 return DVR_SUCCESS;
2004}
2005
2006static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2007{
2008 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
2009 evt->sn, evt->playback.event,
2010 evt->playback.status.play_status.state,
2011 evt->playback.status.play_status.segment_id,
2012 evt->playback.status.play_status.time_cur,
2013 evt->playback.status.play_status.time_end);
2014
2015 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002016 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2017 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2018 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2019 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002020 ctx->playback.last_event = evt->playback.event;
2021
2022 switch (evt->playback.event)
2023 {
2024 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2025 case DVR_PLAYBACK_EVENT_REACHED_END:
2026 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2027 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002028 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002029 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002030 case DVR_PLAYBACK_EVENT_NODATA:
2031 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002032 {
2033 DVR_WrapperPlaybackStatus_t status;
2034
2035 /*copy status of segment*/
2036 ctx->playback.seg_status = evt->playback.status.play_status;
2037
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002038 /*generate status of the whole playback*/
2039 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002040
2041 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
2042 if (ctx->playback.param_open.is_timeshift) {
2043 /*wait for more data in recording*/
2044 } else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
2045 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002046 } else {
2047 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002048 }
hualing chenf291cf32020-06-18 10:50:30 +08002049 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002050 process_notifyPlayback(ctx, evt->playback.event, &status);
2051 }
2052 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002053 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2054 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2055 case DVR_PLAYBACK_EVENT_NO_KEY:
2056 {
2057 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
2058 } break;
2059 default:
2060 {
2061 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
2062 } break;
2063 }
2064 return 0;
2065}
2066
2067static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2068{
2069 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2070}
2071