blob: e0367d4cb445169f7b10c4c16d5262dbaf8f0973 [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 chenbc0aec92021-03-18 14:52:40 +0800478 if (ctx == NULL) {
479 DVR_WRAPPER_DEBUG(1, "warp not get ctx.free event..\n");
480 goto processed;
481 }
hualing chene3797f02021-01-13 14:53:28 +0800482 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 +0800483 if (tctx->running) {
484 /*
485 continue not break,
486 make all events consumed, or mem leak
487 */
488 if (!wrapper_mutex_lock_if(&ctx->lock, &tctx->running))
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800489 goto processed;
490
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800491 if (ctx_valid(ctx)) {
492 /*double check after lock*/
493 if (evt->sn == ctx->sn)
494 process_handleEvents(evt, ctx);
495 }
496 pthread_mutex_unlock(&ctx->lock);
497 }
498
Zhiqiang Hanef61c0a2020-04-13 15:49:24 +0800499processed:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800500 ctx_freeEvent(evt);
501 }
hualing chene3797f02021-01-13 14:53:28 +0800502 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 +0800503 }
504
505 pthread_mutex_unlock(&tctx->lock);
hualing chene3797f02021-01-13 14:53:28 +0800506 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 +0800507 return NULL;
508}
509
510static inline int ctx_addRecordEvent(DVR_WrapperEventCtx_t *evt)
511{
512 if (ctx_addEvent(&record_evt_list, &record_evt_list_lock, evt) == 0)
513 wrapper_threadSignalForType(evt->type);
514 return 0;
515}
516
517static inline int ctx_addPlaybackEvent(DVR_WrapperEventCtx_t *evt)
518{
519 if (ctx_addEvent(&playback_evt_list, &playback_evt_list_lock, evt) == 0)
520 wrapper_threadSignalForType(evt->type);
521 return 0;
522}
523
524static inline void ctx_freeSegments(DVR_WrapperCtx_t *ctx)
525{
526 DVR_WrapperPlaybackSegmentInfo_t *pseg, *pseg_tmp;
527 list_for_each_entry_safe(pseg, pseg_tmp, &ctx->segments, head) {
528 list_del(&pseg->head);
529 free(pseg);
530 }
531}
532
533static inline void _updatePlaybackSegment(DVR_WrapperPlaybackSegmentInfo_t *pseg,
534 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
535{
536 (void)ctx;
537 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
538 pseg->seg_info = *seg_info;
539 else if (update_flags & U_PIDS) {
540 pseg->seg_info.nb_pids = seg_info->nb_pids;
541 memcpy(pseg->seg_info.pids, seg_info->pids, sizeof(pseg->seg_info.pids));
542 } else if (update_flags & U_STAT) {
543 pseg->seg_info.duration = seg_info->duration;
544 pseg->seg_info.size = seg_info->size;
545 pseg->seg_info.nb_packets = seg_info->nb_packets;
546 }
547
548 /*no changes
549 DVR_PlaybackSegmentFlag_t flags;
550 pseg->playback_info.segment_id = pseg->seg_info.id;
551 strncpy(pseg->playback_info.location,
552 ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
553 pseg->playback_info.pids = ctx->playback.pids_req;
554 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
555 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
556 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
557 pseg->playback_info.flags = flags;
558 */
559}
560
561static int wrapper_updatePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
562{
563 DVR_WrapperPlaybackSegmentInfo_t *pseg;
564
565 DVR_WRAPPER_DEBUG(1, "timeshift, update playback segments(wrapper), seg:%lld t/s/p(%ld/%zu/%u)\n",
566 seg_info->id, seg_info->duration, seg_info->size, seg_info->nb_packets);
567
568 if (list_empty(&ctx->segments)) {
569 DVR_WRAPPER_DEBUG(1, "timeshift, update while no segment exists, ignore\n");
570 return DVR_SUCCESS;
571 }
572
573 /*normally, the last segment added will be updated*/
574 pseg =
575 list_first_entry(&ctx->segments, DVR_WrapperPlaybackSegmentInfo_t, head);
576 if (pseg->seg_info.id == seg_info->id) {
577 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
578 } else {
579 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
580 if (pseg->seg_info.id == seg_info->id) {
581 _updatePlaybackSegment(pseg, seg_info, update_flags, ctx);
582 break;
583 }
584 }
585 }
586
587 /*need to notify the dvr_playback*/
588 if (ctx->playback.param_open.is_timeshift/*should must be timeshift*/
589 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END
590 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
591 if (
592 /*there's $TIMESHIFT_DATA_DURATION_TO_RESUME more of data in the current segment playing*/
593 (ctx->playback.seg_status.segment_id == seg_info->id
594 && (seg_info->duration >= ((time_t)ctx->playback.seg_status.time_cur + TIMESHIFT_DATA_DURATION_TO_RESUME)))
595 ||
596 /*or there's a new segment and has $TIMESHIFT_DATA_DURATION_TO_RESUME of data*/
597 (ctx->playback.seg_status.segment_id != seg_info->id
598 && (seg_info->duration >= TIMESHIFT_DATA_DURATION_TO_RESUME))
599 )
600 {
601 int error;
hualing chen36e0dfd2020-05-02 16:33:06 +0800602 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +0800603 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +0800604 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800605
606 error = dvr_playback_resume(ctx->playback.player);
607 DVR_WRAPPER_DEBUG(1, "timeshift, resume playback(sn:%ld) (%d) id/dur: rec(%lld/%ld) play(%lld/%u)\n",
608 ctx->sn, error,
609 seg_info->id, seg_info->duration,
610 ctx->playback.seg_status.segment_id, ctx->playback.seg_status.time_cur);
611 }
612 }
613
614 return DVR_SUCCESS;
615}
616
617static void _updateRecordSegment(DVR_WrapperRecordSegmentInfo_t *pseg,
618 DVR_RecordSegmentInfo_t *seg_info, int update_flags, DVR_WrapperCtx_t *ctx)
619{
620 (void)ctx;
621 if ((update_flags & U_PIDS) && (update_flags & U_STAT))
622 pseg->info = *seg_info;
623 else if (update_flags & U_PIDS) {
624 pseg->info.nb_pids = seg_info->nb_pids;
625 memcpy(pseg->info.pids, seg_info->pids, sizeof(pseg->info.pids));
626 } else if (update_flags & U_STAT) {
627 pseg->info.duration = seg_info->duration;
628 pseg->info.size = seg_info->size;
629 pseg->info.nb_packets = seg_info->nb_packets;
630 }
631}
632
633static int wrapper_updateRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info, int update_flags)
634{
hualing chen266b9502020-04-04 17:39:39 +0800635 DVR_WrapperRecordSegmentInfo_t *pseg = NULL;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800636
637 /*normally, the last segment added will be updated*/
hualing chen266b9502020-04-04 17:39:39 +0800638 if (!list_empty(&ctx->segments)) {
639 pseg =
640 list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
641 if (pseg->info.id == seg_info->id) {
642 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
643 } else {
644 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
645 if (pseg->info.id == seg_info->id) {
646 _updateRecordSegment(pseg, seg_info, update_flags, ctx);
647 break;
648 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800649 }
650 }
651 }
652
653 /*timeshift, update the segment for playback*/
654 /*
655 the playback should grab the segment info other than the id,
656 and the id will be updated by each segment-add during the recording
657 */
658 /*
659 the playback paused if no data been checked from recording,
660 should resume the player later when there's more data
661 */
662
663 if (ctx->record.param_open.is_timeshift) {
664 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
665
666 if (ctx_playback) {
667 pthread_mutex_lock(&ctx_playback->lock);
668 if (ctx_valid(ctx_playback)
669 && ctx_playback->sn == sn_timeshift_playback) {
670 wrapper_updatePlaybackSegment(ctx_playback, seg_info, update_flags);
671 }
672 pthread_mutex_unlock(&ctx_playback->lock);
673 }
674 }
675
676 return DVR_SUCCESS;
677}
678
679static int wrapper_addPlaybackSegment(DVR_WrapperCtx_t *ctx,
680 DVR_RecordSegmentInfo_t *seg_info,
681 DVR_PlaybackPids_t *p_pids,
682 DVR_PlaybackSegmentFlag_t flags)
683{
684 DVR_WrapperPlaybackSegmentInfo_t *pseg;
685 int error;
686
687 error = 0;
688 pseg = (DVR_WrapperPlaybackSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperPlaybackSegmentInfo_t));
689 if (!pseg) {
690 error = DVR_FAILURE;
691 DVR_WRAPPER_DEBUG(1, "memory fail\n");
692 return error;
693 }
694
695 /*copy the orignal segment info*/
696 pseg->seg_info = *seg_info;
697 /*generate the segment info used in playback*/
698 pseg->playback_info.segment_id = pseg->seg_info.id;
699 strncpy(pseg->playback_info.location, ctx->playback.param_open.location, sizeof(pseg->playback_info.location));
700 pseg->playback_info.pids = *p_pids;
701 pseg->playback_info.flags = flags;
702 list_add(&pseg->head, &ctx->segments);
703
704 error = dvr_playback_add_segment(ctx->playback.player, &pseg->playback_info);
705 if (error) {
706 DVR_WRAPPER_DEBUG(1, "fail to add segment %lld (%d)\n", pseg->playback_info.segment_id, error);
707 } else {
708 ctx->playback.status.info_full.time += pseg->seg_info.duration;
709 ctx->playback.status.info_full.size += pseg->seg_info.size;
710 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
711 }
712
713 return error;
714}
715
716static int wrapper_addRecordSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
717{
718 DVR_WrapperRecordSegmentInfo_t *pseg;
719 int error;
720
721 error = 0;
722 pseg = (DVR_WrapperRecordSegmentInfo_t *)calloc(1, sizeof(DVR_WrapperRecordSegmentInfo_t));
723 if (!pseg) {
724 error = DVR_FAILURE;
725 DVR_WRAPPER_DEBUG(1, "memory fail\n");
726 }
727 pseg->info = *seg_info;
728 list_add(&pseg->head, &ctx->segments);
729
730 if (ctx->record.param_open.is_timeshift) {
731 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
732
733 if (ctx_playback) {
734 pthread_mutex_lock(&ctx_playback->lock);
735 if (ctx_valid(ctx_playback)) {
736 DVR_PlaybackSegmentFlag_t flags;
737
738 /*only if playback has started, the previous segments have been loaded*/
739 if (!list_empty(&ctx_playback->segments)) {
740 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
741 if (ctx->record.param_open.flags | DVR_RECORD_FLAG_SCRAMBLED)
742 flags |= DVR_PLAYBACK_SEGMENT_ENCRYPTED;
743 wrapper_addPlaybackSegment(ctx_playback, seg_info, &ctx_playback->playback.pids_req, flags);
744 }
745 }
746 pthread_mutex_unlock(&ctx_playback->lock);
747 }
748 }
749
750 return error;
751}
752
753static int wrapper_removePlaybackSegment(DVR_WrapperCtx_t *ctx, DVR_RecordSegmentInfo_t *seg_info)
754{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800755 int error = -1;
756 DVR_WrapperPlaybackSegmentInfo_t *pseg = NULL, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800757
758 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->id);
759
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800760 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800761 if (pseg->seg_info.id == seg_info->id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800762
763 if (ctx->current_segment_id == seg_info->id) {
764 DVR_WrapperPlaybackSegmentInfo_t *next_seg;
765
766 /*drive the player out of this will-be-deleted segment*/
767 next_seg = list_prev_entry(pseg, head);
768
769 if (ctx->playback.speed != 100.0f) {
770 error = dvr_playback_resume(ctx->playback.player);
771 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), resume for new start (%d)\n", ctx->sn, error);
772 }
773
774 error = dvr_playback_seek(ctx->playback.player, next_seg->seg_info.id, 0);
775 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);
776
777 if (ctx->playback.speed == 0.0f) {
778 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
779 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last paused from new start (%d)\n", ctx->sn, error);
780 } else if (ctx->playback.speed != 100.0f) {
781 DVR_PlaybackSpeed_t dvr_speed = {
782 .speed = { ctx->playback.speed },
783 .mode = ( ctx->playback.speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
784 };
785 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
786 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), keep last speed(x%f) from new start (%d)\n", ctx->sn,ctx->playback.speed, error);
787 }
788 }
789
790 error = dvr_playback_remove_segment(ctx->playback.player, seg_info->id);
791 if (error) {
792 /*remove playack segment fail*/
793 DVR_WRAPPER_DEBUG(1, "timeshift, playback(sn:%ld), failed to remove segment(%llu) (%d)\n", ctx->sn, seg_info->id, error);
794 }
795
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800796 list_del(&pseg->head);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800797
798 /*record the obsolete*/
799 ctx->playback.obsolete.time += pseg->seg_info.duration;
800 ctx->playback.obsolete.size += pseg->seg_info.size;
801 ctx->playback.obsolete.pkts += pseg->seg_info.nb_packets;
802
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800803 free(pseg);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800804 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800805 }
806 }
807
808 DVR_WRAPPER_DEBUG(1, "timeshift, remove playback(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->id, error);
809
810 return error;
811}
812
813static int wrapper_removeRecordSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *seg_info)
814{
815 int error;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800816 DVR_WrapperRecordSegmentInfo_t *pseg, *pseg_tmp;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800817
818 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) ...\n", ctx->sn, seg_info->info.id);
819
820 /*if timeshifting, notify the playback first, then deal with record*/
821 if (ctx->record.param_open.is_timeshift) {
822 DVR_WrapperCtx_t *ctx_playback = ctx_getPlayback(sn_timeshift_playback);
823
824 if (ctx_playback) {
825 pthread_mutex_lock(&ctx_playback->lock);
826 if (ctx_valid(ctx_playback)
827 && ctx_playback->sn == sn_timeshift_playback
828 && !list_empty(&ctx_playback->segments)) {
829 error = wrapper_removePlaybackSegment(ctx_playback, &seg_info->info);
830 }
831 pthread_mutex_unlock(&ctx_playback->lock);
832 }
833 }
834
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800835 list_for_each_entry_safe_reverse(pseg, pseg_tmp, &ctx->segments, head) {
836 if (pseg->info.id == seg_info->info.id) {
837 list_del(&pseg->head);
838
839 /*record the obsolete*/
840 ctx->record.obsolete.time += pseg->info.duration;
841 ctx->record.obsolete.size += pseg->info.size;
842 ctx->record.obsolete.pkts += pseg->info.nb_packets;
843
844 free(pseg);
845 break;
846 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800847 }
848
849 error = dvr_segment_delete(ctx->record.param_open.location, seg_info->info.id);
850
851 DVR_WRAPPER_DEBUG(1, "timeshift, remove record(sn:%ld) segment(%lld) =(%d)\n", ctx->sn, seg_info->info.id, error);
852
853 return error;
854}
855
856int dvr_wrapper_open_record (DVR_WrapperRecord_t *rec, DVR_WrapperRecordOpenParams_t *params)
857{
858 int error;
859 DVR_WrapperCtx_t *ctx;
860 DVR_RecordOpenParams_t open_param;
861
862 DVR_RETURN_IF_FALSE(rec);
863 DVR_RETURN_IF_FALSE(params);
864
865 /*get a free ctx*/
866 ctx = ctx_getRecord(0);
867 DVR_RETURN_IF_FALSE(ctx);
868
869 pthread_mutex_lock(&ctx->lock);
870
hualing chen51652f02020-12-29 16:59:31 +0800871 DVR_WRAPPER_DEBUG(1, "open record(dmx:%d) .istf(%d)..time (%ld)ms max size(%lld)byte seg size(%lld)byte\n",
872 params->dmx_dev_id, params->is_timeshift, params->max_time, params->max_size, params->segment_size);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800873
874 ctx_reset(ctx);
875
876 ctx->record.param_open = *params;
877 ctx->record.event_fn = params->event_fn;
878 ctx->record.event_userdata = params->event_userdata;
879 ctx->record.next_segment_id = 0;
880 ctx->current_segment_id = 0;
881 INIT_LIST_HEAD(&ctx->segments);
882 ctx->sn = get_sn();
883
884 wrapper_requestThreadFor(ctx);
885
hualing chen266b9502020-04-04 17:39:39 +0800886 memset(&open_param, 0, sizeof(DVR_RecordOpenParams_t));
Yahui Hance15e9c2020-12-08 18:08:32 +0800887 open_param.fend_dev_id = params->fend_dev_id;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800888 open_param.dmx_dev_id = params->dmx_dev_id;
889 open_param.data_from_memory = 0;
890 open_param.flags = params->flags;
Zhiqiang Han104aed72020-04-03 19:37:43 +0800891 open_param.notification_size = 500*1024;
Zhiqiang Han31505452020-05-06 15:08:10 +0800892 open_param.flush_size = params->flush_size;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800893 open_param.event_fn = wrapper_record_event_handler;
894 open_param.event_userdata = (void*)ctx->sn;
895
896 error = dvr_record_open(&ctx->record.recorder, &open_param);
897 if (error) {
898 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) open fail(error:%d).\n", params->dmx_dev_id, error);
899 ctx_reset(ctx);
900 pthread_mutex_unlock(&ctx->lock);
901 wrapper_releaseThreadForType(ctx->type);
902 return DVR_FAILURE;
903 }
904 if (params->is_timeshift)
905 sn_timeshift_record = ctx->sn;
906
907 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
908
hualing chen266b9502020-04-04 17:39:39 +0800909 error = dvr_record_set_encrypt_callback(ctx->record.recorder, params->crypto_fn, params->crypto_data);
910 if (error) {
911 DVR_WRAPPER_DEBUG(1, "record(dmx:%d) set encrypt callback fail(error:%d).\n", params->dmx_dev_id, error);
912 }
913
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800914 pthread_mutex_unlock(&ctx->lock);
915
916 *rec = (DVR_WrapperRecord_t)ctx->sn;
917 return DVR_SUCCESS;
918}
919
920int dvr_wrapper_close_record (DVR_WrapperRecord_t rec)
921{
922 DVR_WrapperCtx_t *ctx;
923 DVR_RecordSegmentInfo_t seg_info;
924 int error;
925
926 DVR_RETURN_IF_FALSE(rec);
927
928 ctx = ctx_getRecord((unsigned long)rec);
929 DVR_RETURN_IF_FALSE(ctx);
930
931 pthread_mutex_lock(&ctx->lock);
932 DVR_WRAPPER_DEBUG(1, "close record(sn:%ld)\n", ctx->sn);
933 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
934
935 memset(&seg_info, 0, sizeof(seg_info));
936 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
937
938 error = dvr_record_close(ctx->record.recorder);
939
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800940 if (ctx->record.param_open.is_timeshift)
941 sn_timeshift_record = 0;
942
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800943 ctx_freeSegments(ctx);
944
945 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) closed = (%d).\n", ctx->sn, error);
946 ctx_reset(ctx);
947 pthread_mutex_unlock(&ctx->lock);
948
949 wrapper_releaseThreadForType(ctx->type);
950
951 return error;
952}
953
954int dvr_wrapper_start_record (DVR_WrapperRecord_t rec, DVR_WrapperRecordStartParams_t *params)
955{
956 DVR_WrapperCtx_t *ctx;
957 DVR_RecordStartParams_t *start_param;
958 int i;
959 int error;
960
961 DVR_RETURN_IF_FALSE(rec);
962 DVR_RETURN_IF_FALSE(params);
963
964 ctx = ctx_getRecord((unsigned long)rec);
965 DVR_RETURN_IF_FALSE(ctx);
966
967 pthread_mutex_lock(&ctx->lock);
968 DVR_WRAPPER_DEBUG(1, "start record(sn:%ld, location:%s) ...\n", ctx->sn, ctx->record.param_open.location);
969 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
970
971 start_param = &ctx->record.param_start;
972 memset(start_param, 0, sizeof(*start_param));
973 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
974 start_param->segment.segment_id = ctx->record.next_segment_id++;
975 start_param->segment.nb_pids = params->pids_info.nb_pids;
976 for (i = 0; i < params->pids_info.nb_pids; i++) {
977 start_param->segment.pids[i] = params->pids_info.pids[i];
978 start_param->segment.pid_action[i] = DVR_RECORD_PID_CREATE;
979 }
hualing chen7a56cba2020-04-14 14:09:27 +0800980 dvr_segment_del_by_location(start_param->location);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +0800981 {
982 /*sync to update for further use*/
983 DVR_RecordStartParams_t *update_param;
984 update_param = &ctx->record.param_update;
985 memcpy(update_param, start_param, sizeof(*update_param));
986 for (i = 0; i < update_param->segment.nb_pids; i++)
987 update_param->segment.pid_action[i] = DVR_RECORD_PID_KEEP;
988 }
989
990 error = dvr_record_start_segment(ctx->record.recorder, start_param);
991 {
992 DVR_RecordSegmentInfo_t new_seg_info =
993 { .id = start_param->segment.segment_id, };
994 wrapper_addRecordSegment(ctx, &new_seg_info);
995 }
996
997 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) started = (%d)\n", ctx->sn, error);
998
999 pthread_mutex_unlock(&ctx->lock);
1000
1001 return error;
1002}
1003
1004int dvr_wrapper_stop_record (DVR_WrapperRecord_t rec)
1005{
1006 DVR_WrapperCtx_t *ctx;
1007 DVR_RecordSegmentInfo_t seg_info;
1008 int error;
1009
1010 DVR_RETURN_IF_FALSE(rec);
1011
1012 ctx = ctx_getRecord((unsigned long)rec);
1013 DVR_RETURN_IF_FALSE(ctx);
1014
1015 pthread_mutex_lock(&ctx->lock);
1016 DVR_WRAPPER_DEBUG(1, "stop record(sn:%ld) ...\n", ctx->sn);
1017 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1018
1019 memset(&seg_info, 0, sizeof(seg_info));
1020 error = dvr_record_stop_segment(ctx->record.recorder, &seg_info);
1021 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1022
1023 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) stopped = (%d)\n", ctx->sn, error);
1024 pthread_mutex_unlock(&ctx->lock);
1025
1026 return error;
1027}
1028
1029int dvr_wrapper_update_record_pids (DVR_WrapperRecord_t rec, DVR_WrapperUpdatePidsParams_t *params)
1030{
1031 DVR_WrapperCtx_t *ctx;
1032 DVR_RecordStartParams_t *start_param;
1033 DVR_RecordSegmentInfo_t seg_info;;
1034 int i;
1035 int error;
1036
1037 DVR_RETURN_IF_FALSE(rec);
1038 DVR_RETURN_IF_FALSE(params);
1039
1040 ctx = ctx_getRecord((unsigned long)rec);
1041 DVR_RETURN_IF_FALSE(ctx);
1042
1043 pthread_mutex_lock(&ctx->lock);
1044 DVR_WRAPPER_DEBUG(1, "update record(sn:%ld)\n", ctx->sn);
1045 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1046
1047 start_param = &ctx->record.param_update;
1048 memset(start_param, 0, sizeof(*start_param));
1049 strncpy(start_param->location, ctx->record.param_open.location, sizeof(start_param->location));
1050 start_param->segment.segment_id = ctx->record.next_segment_id++;
1051 start_param->segment.nb_pids = params->nb_pids;
1052 for (i = 0; i < params->nb_pids; i++) {
1053 start_param->segment.pids[i] = params->pids[i];
1054 start_param->segment.pid_action[i] = params->pid_action[i];
1055 }
1056 error = dvr_record_next_segment(ctx->record.recorder, start_param, &seg_info);
1057 {
1058 DVR_RecordSegmentInfo_t new_seg_info =
1059 { .id = start_param->segment.segment_id, };
1060 wrapper_updateRecordSegment(ctx, &seg_info, U_PIDS);
1061 wrapper_addRecordSegment(ctx, &new_seg_info);
1062 }
1063
1064 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) updated = (%d)\n", ctx->sn, error);
1065 pthread_mutex_unlock(&ctx->lock);
1066
1067 return error;
1068}
1069
1070int dvr_wrapper_get_record_status(DVR_WrapperRecord_t rec, DVR_WrapperRecordStatus_t *status)
1071{
1072 DVR_WrapperCtx_t *ctx;
1073 DVR_WrapperRecordStatus_t s;
1074 int error;
1075
1076 DVR_RETURN_IF_FALSE(rec);
1077 DVR_RETURN_IF_FALSE(status);
1078
1079 ctx = ctx_getRecord((unsigned long)rec);
1080 DVR_RETURN_IF_FALSE(ctx);
1081
1082 pthread_mutex_lock(&ctx->lock);
1083
1084 DVR_WRAPPER_DEBUG(1, "get record(sn:%ld) status ...\n", ctx->sn);
1085 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1086
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001087 error = process_generateRecordStatus(ctx, &s);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001088
1089 DVR_WRAPPER_DEBUG(1, "record(sn:%ld) state/time/size/pkts(%d/%ld/%lld/%u) (%d)\n",
1090 ctx->sn,
1091 s.state,
1092 s.info.time,
1093 s.info.size,
1094 s.info.pkts,
1095 error);
1096
1097 *status = s;
1098
1099 pthread_mutex_unlock(&ctx->lock);
1100
1101 return error;
1102}
1103
hualing chen4fe3bee2020-10-23 13:58:52 +08001104int dvr_wrapper_record_is_secure_mode(DVR_WrapperRecord_t rec)
1105{
1106 DVR_WrapperCtx_t *ctx;
1107 int error;
1108
1109 DVR_RETURN_IF_FALSE(rec);
1110
1111 ctx = ctx_getRecord((unsigned long)rec);
1112 DVR_RETURN_IF_FALSE(ctx);
1113
1114 pthread_mutex_lock(&ctx->lock);
1115 error = dvr_record_is_secure_mode(ctx->record.recorder);
1116 pthread_mutex_unlock(&ctx->lock);
1117 return error;
1118}
1119
hualing chen266b9502020-04-04 17:39:39 +08001120int dvr_wrapper_set_record_secure_buffer (DVR_WrapperRecord_t rec, uint8_t *p_secure_buf, uint32_t len)
1121{
1122 DVR_WrapperCtx_t *ctx;
1123 int error;
1124
1125 DVR_RETURN_IF_FALSE(rec);
1126 DVR_RETURN_IF_FALSE(p_secure_buf);
1127
1128 ctx = ctx_getRecord((unsigned long)rec);
1129 DVR_RETURN_IF_FALSE(ctx);
1130
1131 pthread_mutex_lock(&ctx->lock);
1132 error = dvr_record_set_secure_buffer(ctx->record.recorder, p_secure_buf, len);
1133 pthread_mutex_unlock(&ctx->lock);
1134 return error;
1135}
1136
1137int dvr_wrapper_set_record_decrypt_callback (DVR_WrapperRecord_t rec, DVR_CryptoFunction_t func, void *userdata)
1138{
1139 DVR_WrapperCtx_t *ctx;
1140 int error;
1141
1142 DVR_RETURN_IF_FALSE(rec);
1143 DVR_RETURN_IF_FALSE(func);
1144
1145 ctx = ctx_getRecord((unsigned long)rec);
1146 DVR_RETURN_IF_FALSE(ctx);
1147
1148 pthread_mutex_lock(&ctx->lock);
1149 error = dvr_record_set_encrypt_callback(ctx->record.recorder, func, userdata);
1150 pthread_mutex_unlock(&ctx->lock);
1151 return error;
1152}
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001153
1154
1155int dvr_wrapper_open_playback (DVR_WrapperPlayback_t *playback, DVR_WrapperPlaybackOpenParams_t *params)
1156{
1157 DVR_WrapperCtx_t *ctx;
1158 DVR_PlaybackOpenParams_t open_param;
1159 int error;
1160
1161 DVR_RETURN_IF_FALSE(playback);
1162 DVR_RETURN_IF_FALSE(params);
1163 DVR_RETURN_IF_FALSE(params->playback_handle);
1164
1165 /*get a free ctx*/
1166 ctx = ctx_getPlayback(0);
1167 DVR_RETURN_IF_FALSE(ctx);
1168
1169 pthread_mutex_lock(&ctx->lock);
1170
1171 DVR_WRAPPER_DEBUG(1, "open playback(dmx:%d) ...\n", params->dmx_dev_id);
1172
1173 ctx_reset(ctx);
1174
1175 ctx->playback.param_open = *params;
1176 ctx->playback.event_fn = params->event_fn;
1177 ctx->playback.event_userdata = params->event_userdata;
1178 ctx->current_segment_id = 0;
1179 INIT_LIST_HEAD(&ctx->segments);
1180 ctx->sn = get_sn();
1181
1182 wrapper_requestThreadFor(ctx);
1183
hualing chen266b9502020-04-04 17:39:39 +08001184 memset(&open_param, 0, sizeof(DVR_PlaybackOpenParams_t));
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001185 open_param.dmx_dev_id = params->dmx_dev_id;
1186 open_param.block_size = params->block_size;
1187 open_param.is_timeshift = params->is_timeshift;
1188 //open_param.notification_size = 10*1024; //not supported
1189 open_param.event_fn = wrapper_playback_event_handler;
1190 open_param.event_userdata = (void*)ctx->sn;
1191 /*open_param.has_pids = 0;*/
hualing chene3797f02021-01-13 14:53:28 +08001192 open_param.is_notify_time = params->is_notify_time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001193 open_param.player_handle = (am_tsplayer_handle)params->playback_handle;
hualing chenfbf8e022020-06-15 13:43:11 +08001194 open_param.vendor = DVR_PLAYBACK_VENDOR_AML;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001195
1196 error = dvr_playback_open(&ctx->playback.player, &open_param);
1197 if (error) {
1198 DVR_WRAPPER_DEBUG(1, "playback(dmx:%d) openned fail(error:%d).\n", params->dmx_dev_id, error);
1199 ctx_reset(ctx);
1200 pthread_mutex_unlock(&ctx->lock);
1201 wrapper_releaseThreadForType(ctx->type);
1202 return DVR_FAILURE;
1203 }
1204 if (params->is_timeshift)
1205 sn_timeshift_playback = ctx->sn;
1206
hualing chen266b9502020-04-04 17:39:39 +08001207 DVR_WRAPPER_DEBUG(1, "hanyh: playback(dmx:%d) openned ok(sn:%ld).\n", params->dmx_dev_id, ctx->sn);
1208 error = dvr_playback_set_decrypt_callback(ctx->playback.player, params->crypto_fn, params->crypto_data);
1209 if (error) {
1210 DVR_WRAPPER_DEBUG(1, "playback set deccrypt callback fail(error:%d).\n", error);
1211 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001212 pthread_mutex_unlock(&ctx->lock);
1213
1214 *playback = (DVR_WrapperPlayback_t)ctx->sn;
1215 return DVR_SUCCESS;
1216}
1217
1218int dvr_wrapper_close_playback (DVR_WrapperPlayback_t playback)
1219{
1220 DVR_WrapperCtx_t *ctx;
1221 int error;
1222
1223 DVR_RETURN_IF_FALSE(playback);
1224
1225 ctx = ctx_getPlayback((unsigned long)playback);
1226 DVR_RETURN_IF_FALSE(ctx);
1227
1228 pthread_mutex_lock(&ctx->lock);
1229 DVR_WRAPPER_DEBUG(1, "close playback(sn:%ld)\n", ctx->sn);
1230 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1231
1232 if (ctx->playback.param_open.is_timeshift)
1233 sn_timeshift_playback = 0;
1234
1235 /*try stop first*/
1236 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1237
1238 {
1239 /*remove all segments*/
1240 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1241
1242 list_for_each_entry(pseg, &ctx->segments, head) {
1243 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1244 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1245 ctx->sn, pseg->playback_info.segment_id, error);
1246 }
1247 ctx_freeSegments(ctx);
1248 }
1249
1250 error = dvr_playback_close(ctx->playback.player);
1251
1252 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) closed.\n", ctx->sn);
1253 ctx_reset(ctx);
1254 pthread_mutex_unlock(&ctx->lock);
1255
1256 wrapper_releaseThreadForType(ctx->type);
1257
1258 return error;
1259}
1260
1261int dvr_wrapper_start_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackFlag_t flags, DVR_PlaybackPids_t *p_pids)
1262{
1263 DVR_WrapperCtx_t *ctx;
1264 int error;
1265 uint64_t *p_segment_ids;
1266 uint32_t segment_nb;
1267 uint32_t i;
1268 DVR_RecordSegmentInfo_t seg_info_1st;
1269 int got_1st_seg;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001270 DVR_WrapperCtx_t *ctx_record;/*for timeshift*/
hualing chenc110f952021-01-18 11:25:37 +08001271 DVR_Bool_t is_timeshift = DVR_FALSE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001272
1273 DVR_RETURN_IF_FALSE(playback);
1274 DVR_RETURN_IF_FALSE(p_pids);
1275
hualing chenc110f952021-01-18 11:25:37 +08001276 ctx_record = NULL;
1277
1278 /*lock the recorder to avoid changing the recording segments*/
1279 ctx_record = ctx_getRecord(sn_timeshift_record);
1280
1281 if (ctx_record) {
1282 pthread_mutex_lock(&ctx_record->lock);
1283 if (!ctx_valid(ctx_record)
1284 || ctx_record->sn != sn_timeshift_record) {
1285 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error found\n");
1286 pthread_mutex_unlock(&ctx_record->lock);
1287 is_timeshift = DVR_FALSE;
1288 } else {
1289 is_timeshift = DVR_TRUE;
1290 }
1291 }
1292
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001293 ctx = ctx_getPlayback((unsigned long)playback);
1294 DVR_RETURN_IF_FALSE(ctx);
1295
1296 pthread_mutex_lock(&ctx->lock);
1297
1298 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",
1299 ctx->sn,
1300 ctx->playback.param_open.location,
1301 flags,
1302 p_pids->video.pid, p_pids->video.format,
1303 p_pids->audio.pid, p_pids->audio.format,
1304 p_pids->ad.pid, p_pids->ad.format,
1305 p_pids->subtitle.pid, p_pids->subtitle.format,
1306 p_pids->pcr.pid);
1307
1308 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1309
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001310 if (ctx->playback.param_open.is_timeshift) {
1311 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001312 if (is_timeshift == DVR_FALSE) {
1313 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error return\n");
1314 pthread_mutex_unlock(&ctx->lock);
1315 return DVR_FAILURE;
1316 } else {
1317 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
1318 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001319 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001320 }
1321
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001322 /*obtain all segments in a list*/
1323 segment_nb = 0;
1324 p_segment_ids = NULL;
1325 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001326 if (!error) {
1327 got_1st_seg = 0;
1328 for (i = 0; i < segment_nb; i++) {
1329 DVR_RecordSegmentInfo_t seg_info;
1330 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001331
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001332 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1333 if (error) {
1334 error = DVR_FAILURE;
1335 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1336 ctx->playback.param_open.location, p_segment_ids[i], error);
1337 break;
1338 }
hualing chen92f3a142020-07-08 20:59:33 +08001339 //add check if has audio or video pid. if not exist. not add segment to playback
1340 int ii = 0;
1341 int has_av = 0;
1342 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1343 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1344 if (type == DVR_STREAM_TYPE_VIDEO ||
1345 type == DVR_STREAM_TYPE_AUDIO ||
1346 type == DVR_STREAM_TYPE_AD) {
1347 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1348 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,
1349 DVR_STREAM_TYPE_VIDEO,
1350 DVR_STREAM_TYPE_AUDIO,
1351 DVR_STREAM_TYPE_AD);
1352 has_av = 1;
1353 //break;
1354 } else {
1355 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,
1356 DVR_STREAM_TYPE_VIDEO,
1357 DVR_STREAM_TYPE_AUDIO,
1358 DVR_STREAM_TYPE_AD);
1359 }
1360 }
1361 if (has_av == 0) {
1362 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1363 continue;
1364 } else {
1365 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1366 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001367 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1368 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1369 if (error)
1370 break;
1371
1372 /*copy the 1st segment*/
1373 if (got_1st_seg == 0) {
1374 seg_info_1st = seg_info;
1375 got_1st_seg = 1;
1376 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001377 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001378 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001379
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001380 /* return if no segment or fail to add */
1381 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001382
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001383 /*copy the obsolete infomation, must for timeshifting*/
1384 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1385 ctx->playback.obsolete = ctx_record->record.obsolete;
1386 }
1387
1388 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
1389
1390 ctx->playback.reach_end = DVR_FALSE;
1391 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1392 ctx->playback.speed = 0.0f;
1393 else
1394 ctx->playback.speed = 100.0f;
1395
1396 ctx->playback.pids_req = *p_pids;
1397
1398 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1399 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
1400 ctx->sn, seg_info_1st.id, error);
1401
1402 error = dvr_playback_start(ctx->playback.player, flags);
1403
1404 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001405 }
1406 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001407
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001408 if (ctx->playback.param_open.is_timeshift) {
1409 /*unlock the recorder locked above*/
1410 if (ctx_record && ctx_valid(ctx_record)) {
1411 pthread_mutex_unlock(&ctx_record->lock);
1412 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
1413 ctx->sn, ctx_record->sn);
1414 }
1415 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001416
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001417
1418 pthread_mutex_unlock(&ctx->lock);
1419
1420 return error;
1421}
1422
1423int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
1424{
1425 DVR_WrapperCtx_t *ctx;
1426 int error;
1427
1428 DVR_RETURN_IF_FALSE(playback);
1429
1430 ctx = ctx_getPlayback((unsigned long)playback);
1431 DVR_RETURN_IF_FALSE(ctx);
1432
1433 pthread_mutex_lock(&ctx->lock);
1434 DVR_WRAPPER_DEBUG(1, "stop playback(sn:%ld) ...\n", ctx->sn);
1435 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1436
1437 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1438
1439 {
1440 /*remove all segments*/
1441 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1442
1443 list_for_each_entry(pseg, &ctx->segments, head) {
1444 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1445 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1446 ctx->sn, pseg->playback_info.segment_id, error);
1447 }
1448 ctx_freeSegments(ctx);
1449 }
1450
1451 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
1452 pthread_mutex_unlock(&ctx->lock);
1453
1454 return error;
1455}
1456
1457int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
1458{
1459 DVR_WrapperCtx_t *ctx;
1460 int error;
1461
1462 DVR_RETURN_IF_FALSE(playback);
1463
1464 ctx = ctx_getPlayback((unsigned long)playback);
1465 DVR_RETURN_IF_FALSE(ctx);
1466
1467 pthread_mutex_lock(&ctx->lock);
1468 DVR_WRAPPER_DEBUG(1, "pause playback(sn:%ld) ...\n", ctx->sn);
1469 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08001470 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08001471 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08001472 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001473
1474 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
1475
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001476 ctx->playback.speed = 0.0f;
1477
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001478 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) paused (%d)\n", ctx->sn, error);
1479 pthread_mutex_unlock(&ctx->lock);
1480
1481 return error;
1482}
1483
1484int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
1485{
1486 DVR_WrapperCtx_t *ctx;
1487 int error;
1488
1489 DVR_RETURN_IF_FALSE(playback);
1490
1491 ctx = ctx_getPlayback((unsigned long)playback);
1492 DVR_RETURN_IF_FALSE(ctx);
1493
1494 pthread_mutex_lock(&ctx->lock);
1495 DVR_WRAPPER_DEBUG(1, "resume playback(sn:%ld) ...\n", ctx->sn);
1496 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1497
1498 error = dvr_playback_resume(ctx->playback.player);
1499
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001500 ctx->playback.speed = 100.0f;
1501
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001502 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
1503 pthread_mutex_unlock(&ctx->lock);
1504
1505 return error;
1506}
1507
1508int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
1509{
1510 DVR_WrapperCtx_t *ctx;
1511 int error;
1512 DVR_PlaybackSpeed_t dvr_speed = {
1513 .speed = { speed },
1514 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
1515 };
1516
1517 DVR_RETURN_IF_FALSE(playback);
1518
1519 ctx = ctx_getPlayback((unsigned long)playback);
1520 DVR_RETURN_IF_FALSE(ctx);
1521
1522 pthread_mutex_lock(&ctx->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001523 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 +08001524 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1525
1526 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
1527
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001528 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
1529 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
1530 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
1531 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
1532 error = dvr_playback_resume(ctx->playback.player);
1533 } else if (ctx->playback.speed == 0.0f
1534 && speed != 0.0f
1535 && speed != 100.0f) {
1536 /*libdvr do not support pause with speed=0, will not be here*/
1537 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
1538 error = dvr_playback_resume(ctx->playback.player);
1539 }
1540
1541 ctx->playback.speed = speed;
1542
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001543 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) speeded(x%f) (%d)\n",
1544 ctx->sn, speed, error);
1545 pthread_mutex_unlock(&ctx->lock);
1546
1547 return error;
1548}
1549
1550int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
1551{
1552 DVR_WrapperCtx_t *ctx;
1553 int error;
1554 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1555 uint64_t segment_id;
1556 uint32_t off;
1557 uint64_t last_segment_id;
1558 uint32_t pre_off;
1559
1560 DVR_RETURN_IF_FALSE(playback);
1561
1562 ctx = ctx_getPlayback((unsigned long)playback);
1563 DVR_RETURN_IF_FALSE(ctx);
1564
1565 pthread_mutex_lock(&ctx->lock);
1566
1567 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
1568 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1569
1570 off = 0;
1571 segment_id = 0;
1572 pre_off = 0;
1573 last_segment_id = 0;
1574
1575 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1576 segment_id = pseg->seg_info.id;
1577
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001578 if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset)
1579 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001580
1581 last_segment_id = pseg->seg_info.id;
1582 pre_off += pseg->seg_info.duration;
1583 }
1584
1585 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001586 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
1587 off = time_offset;
1588 } else if (ctx->playback.obsolete.time >= time_offset) {
1589 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001590 } else {
hualing chenda76fc52020-05-28 14:56:42 +08001591 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001592 }
1593
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001594 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (seg:%lld, off:%d)\n",
1595 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001596 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
1597 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
1598
1599 pthread_mutex_unlock(&ctx->lock);
1600
1601 return error;
1602}
1603
1604int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
1605{
1606 DVR_WrapperCtx_t *ctx;
1607 int error;
1608 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1609
1610 DVR_RETURN_IF_FALSE(playback);
1611 DVR_RETURN_IF_FALSE(p_pids);
1612
1613 ctx = ctx_getPlayback((unsigned long)playback);
1614 DVR_RETURN_IF_FALSE(ctx);
1615
1616 pthread_mutex_lock(&ctx->lock);
1617
1618 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
1619 ctx->sn,
1620 p_pids->video.pid, p_pids->video.format,
1621 p_pids->audio.pid, p_pids->audio.format);
1622 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1623
1624 ctx->playback.pids_req = *p_pids;
1625
1626 error = 0;
1627 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1628 /*should update the whole list of segments*/
1629 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
1630 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
1631 /*check udpate for pids*/
1632 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
1633 pseg->playback_info.pids = *p_pids;
1634 error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
1635 if (error) {
1636 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
1637 ctx->sn, pseg->seg_info.id, error);
1638 /*do not break, let list updated*/
1639 }
1640 }
1641 }
1642 /*break;*/
1643 }
1644 }
1645
1646 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
1647 ctx->sn,
1648 p_pids->video.pid, p_pids->video.format,
1649 p_pids->audio.pid, p_pids->audio.format,
1650 error);
1651
1652 pthread_mutex_unlock(&ctx->lock);
1653
1654 return error;
1655}
1656
1657int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
1658{
1659 DVR_WrapperCtx_t *ctx;
1660 DVR_WrapperPlaybackStatus_t s;
1661 DVR_PlaybackStatus_t play_status;
1662 int error;
1663
1664 DVR_RETURN_IF_FALSE(playback);
1665 DVR_RETURN_IF_FALSE(status);
1666
1667 ctx = ctx_getPlayback((unsigned long)playback);
1668 DVR_RETURN_IF_FALSE(ctx);
1669
1670 pthread_mutex_lock(&ctx->lock);
1671
1672 DVR_WRAPPER_DEBUG(1, "get playback(sn:%ld) status ...\n", ctx->sn);
1673 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1674
1675 error = dvr_playback_get_status(ctx->playback.player, &play_status);
1676 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) get status (%d)\n", ctx->sn, error);
1677
1678 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001679 error = process_generatePlaybackStatus(ctx, &s);
1680
hualing chenb5cd42e2020-04-15 17:03:34 +08001681 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
1682 //reach end need set full time to cur.so app can exist playback.
1683 DVR_WRAPPER_DEBUG(1, "set cur time to full time, reach end occur");
1684 s.info_cur.time = s.info_full.time;
1685 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001686 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001687 ctx->sn,
1688 s.state,
1689 s.info_cur.time,
1690 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001691 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001692 error);
1693
1694 *status = s;
1695
1696 pthread_mutex_unlock(&ctx->lock);
1697
1698 return error;
1699}
1700
hualing chen266b9502020-04-04 17:39:39 +08001701int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
1702{
1703 DVR_WrapperCtx_t *ctx;
1704 int error;
1705
1706 DVR_RETURN_IF_FALSE(playback);
1707 DVR_RETURN_IF_FALSE(p_secure_buf);
1708
1709 ctx = ctx_getPlayback((unsigned long)playback);
1710 DVR_RETURN_IF_FALSE(ctx);
1711
1712 pthread_mutex_lock(&ctx->lock);
1713 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
1714 pthread_mutex_unlock(&ctx->lock);
1715 return error;
1716}
1717
1718int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
1719{
1720 DVR_WrapperCtx_t *ctx;
1721 int error;
1722
1723 DVR_RETURN_IF_FALSE(playback);
1724 DVR_RETURN_IF_FALSE(func);
1725
1726 ctx = ctx_getPlayback((unsigned long)playback);
1727 DVR_RETURN_IF_FALSE(ctx);
1728
1729 pthread_mutex_lock(&ctx->lock);
1730 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
1731 pthread_mutex_unlock(&ctx->lock);
1732 return error;
1733}
1734
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001735static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_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_REC;
1743 evt.record.event = event;
1744 evt.record.status = *(DVR_RecordStatus_t *)params;
1745 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
1746 return ctx_addRecordEvent(&evt);
1747}
1748
1749static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
1750{
1751 DVR_WrapperEventCtx_t evt;
1752
1753 DVR_RETURN_IF_FALSE(userdata);
1754
1755 evt.sn = (unsigned long)userdata;
1756 evt.type = W_PLAYBACK;
1757 evt.playback.event = event;
1758 evt.playback.status = *(DVR_Play_Notify_t *)params;
1759 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event);
1760 return ctx_addPlaybackEvent(&evt);
1761}
1762
1763static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
1764{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001765 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 +08001766 ctx->sn,
1767 evt,
1768 status->info.time,
1769 status->info.size,
1770 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001771 status->info_obsolete.time,
1772 status->info_obsolete.size,
1773 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001774
1775 if (ctx->record.event_fn)
1776 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
1777 return 0;
1778}
1779
1780static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
1781{
1782 DVR_RecordStartParams_t param;
1783 DVR_RecordSegmentInfo_t seg_info;
1784 int i;
1785 int error;
1786
1787 memcpy(&param, &ctx->record.param_update, sizeof(param));
1788 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
1789 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
1790 for (i = 0; i < param.segment.nb_pids; i++) {
1791 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
1792 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
1793 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
1794 ctx->record.param_update.segment.nb_pids++;
1795 }
1796 }
1797 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
1798 {
1799 DVR_RecordSegmentInfo_t new_seg_info =
1800 { .id = ctx->record.param_update.segment.segment_id, };
1801 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1802 wrapper_addRecordSegment(ctx, &new_seg_info);
1803 }
1804
Zhiqiang Hand977e972020-05-11 11:30:47 +08001805 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 +08001806 return error;
1807}
1808
1809static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg)
1810{
1811 return wrapper_removeRecordSegment(ctx, pseg);
1812}
1813
1814/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001815static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001816{
1817 /*the current seg is not covered in the statistics*/
1818 DVR_WrapperRecordSegmentInfo_t *pseg;
1819
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001820 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001821 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
1822
1823 ctx->record.status.state = ctx->record.seg_status.state;
1824 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
1825 memcpy(ctx->record.status.pids.pids,
1826 ctx->record.seg_status.info.pids,
1827 sizeof(ctx->record.status.pids.pids));
1828 ctx->current_segment_id = ctx->record.seg_status.info.id;
1829
1830 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1831 if (pseg->info.id != ctx->record.seg_status.info.id) {
1832 ctx->record.status.info.time += pseg->info.duration;
1833 ctx->record.status.info.size += pseg->info.size;
1834 ctx->record.status.info.pkts += pseg->info.nb_packets;
1835 }
1836 }
1837
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001838 ctx->record.status.info_obsolete = ctx->record.obsolete;
1839
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001840 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001841
1842 if (status) {
1843 *status = ctx->record.status;
1844 status->info.time += ctx->record.seg_status.info.duration;
1845 status->info.size += ctx->record.seg_status.info.size;
1846 status->info.pkts += ctx->record.seg_status.info.nb_packets;
1847 }
1848
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001849 return DVR_SUCCESS;
1850}
1851
1852
1853static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
1854{
1855 DVR_WrapperRecordStatus_t status;
1856
1857 memset(&status, 0, sizeof(status));
1858
1859 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d)\n",
1860 evt->sn, evt->record.event, evt->record.status.state);
1861
1862 switch (evt->record.event)
1863 {
1864 case DVR_RECORD_EVENT_STATUS:
1865 {
1866 switch (evt->record.status.state)
1867 {
1868 case DVR_RECORD_STATE_OPENED:
1869 case DVR_RECORD_STATE_CLOSED:
1870 {
1871 ctx->record.seg_status = evt->record.status;
1872
1873 status.state = evt->record.status.state;
1874 process_notifyRecord(ctx, evt->record.event, &status);
1875 } break;
1876 case DVR_RECORD_STATE_STARTED:
1877 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001878 ctx->record.seg_status = evt->record.status;
1879
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001880 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001881 process_notifyRecord(ctx, evt->record.event, &status);
1882
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001883 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001884 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001885 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
1886 DVR_WRAPPER_DEBUG(1, "start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n",
1887 ctx->sn,
1888 evt->record.status.info.size,
1889 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08001890 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
1891 /*should notify the recording's stop*/
1892 int error = dvr_record_close(ctx->record.recorder);
1893 DVR_WRAPPER_DEBUG(1, "stop record(%lu)=%d, failed to start new segment for recording.",
1894 ctx->sn, error);
1895 status.state = DVR_RECORD_STATE_CLOSED;
1896 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
1897 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001898 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001899
1900 if (ctx->record.param_open.is_timeshift
1901 && ctx->record.param_open.max_time
1902 && status.info.time >= ctx->record.param_open.max_time) {
1903 DVR_WrapperRecordSegmentInfo_t *pseg;
1904
1905 /*as the player do not support null playlist,
1906 there must be one segment existed at any time,
1907 we have to keep two segments before remove one*/
1908 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1909 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1910 /*only one segment, waiting for more*/
1911 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n",
1912 status.info.size,
1913 ctx->record.param_open.max_time,
1914 ctx->record.param_open.segment_size);
1915 } else {
1916 /*timeshifting, remove the 1st segment and notify the player*/
1917 record_removeSegment(ctx, pseg);
1918
1919 process_generateRecordStatus(ctx, &status);
1920 process_notifyRecord(ctx, evt->record.event, &status);
1921 }
1922 }
1923
1924 if (ctx->record.param_open.is_timeshift
1925 && ctx->record.param_open.max_size
1926 && status.info.size >= ctx->record.param_open.max_size) {
1927 DVR_WrapperRecordSegmentInfo_t *pseg;
1928
1929 /*as the player do not support null playlist,
1930 there must be one segment existed at any time,
1931 we have to keep two segments before remove one*/
1932 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1933 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1934 /*only one segment, waiting for more*/
1935 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of record < max size of segment(%lld)\n",
1936 status.info.size,
1937 ctx->record.param_open.segment_size);
1938 } else {
1939 record_removeSegment(ctx, pseg);
1940
1941 process_generateRecordStatus(ctx, &status);
1942 process_notifyRecord(ctx, evt->record.event, &status);
1943 }
1944 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001945 } break;
1946 case DVR_RECORD_STATE_STOPPED:
1947 {
1948 ctx->record.seg_status = evt->record.status;
1949
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001950 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001951 process_notifyRecord(ctx, evt->record.event, &status);
1952 } break;
1953 default:
1954 break;
1955 }
1956 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08001957 case DVR_RECORD_EVENT_WRITE_ERROR: {
1958 ctx->record.seg_status = evt->record.status;
1959 status.state = evt->record.status.state;
1960 process_notifyRecord(ctx, evt->record.event, &status);
1961 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001962 default:
1963 break;
1964 }
1965 return DVR_SUCCESS;
1966}
1967
1968static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
1969{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001970 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 +08001971 ctx->sn,
1972 evt,
1973 status->state,
1974 status->info_cur.time,
1975 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001976 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001977
1978 if (ctx->playback.event_fn)
1979 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
1980 return 0;
1981}
1982
1983/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001984static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001985{
1986 /*the current seg is not covered in the statistics*/
1987 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1988
1989 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
1990 ctx->playback.status.pids = ctx->playback.pids_req;
1991
1992 ctx->playback.status.state = ctx->playback.seg_status.state;
1993 ctx->playback.status.speed = ctx->playback.seg_status.speed;
1994 ctx->playback.status.flags = ctx->playback.seg_status.flags;
1995 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
1996
1997 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1998 if (pseg->seg_info.id == ctx->playback.seg_status.segment_id)
1999 break;
2000 ctx->playback.status.info_cur.time += pseg->seg_info.duration;
2001 ctx->playback.status.info_cur.size += pseg->seg_info.size;
2002 ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets;
2003 }
2004 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2005 ctx->playback.status.info_full.time += pseg->seg_info.duration;
2006 ctx->playback.status.info_full.size += pseg->seg_info.size;
2007 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
2008 }
2009
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002010 if (status) {
2011 *status = ctx->playback.status;
2012 /*deal with current, lack size and pkts with the current*/
2013 status->info_cur.time += ctx->playback.seg_status.time_cur;
2014 status->info_obsolete.time = ctx->playback.obsolete.time;
2015 }
2016
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002017 return DVR_SUCCESS;
2018}
2019
2020static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2021{
2022 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
2023 evt->sn, evt->playback.event,
2024 evt->playback.status.play_status.state,
2025 evt->playback.status.play_status.segment_id,
2026 evt->playback.status.play_status.time_cur,
2027 evt->playback.status.play_status.time_end);
2028
2029 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002030 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2031 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2032 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2033 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002034 ctx->playback.last_event = evt->playback.event;
2035
2036 switch (evt->playback.event)
2037 {
2038 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2039 case DVR_PLAYBACK_EVENT_REACHED_END:
2040 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2041 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002042 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002043 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002044 case DVR_PLAYBACK_EVENT_NODATA:
2045 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002046 {
2047 DVR_WrapperPlaybackStatus_t status;
2048
2049 /*copy status of segment*/
2050 ctx->playback.seg_status = evt->playback.status.play_status;
2051
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002052 /*generate status of the whole playback*/
2053 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002054
2055 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
2056 if (ctx->playback.param_open.is_timeshift) {
2057 /*wait for more data in recording*/
2058 } else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
2059 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002060 } else {
2061 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002062 }
hualing chenf291cf32020-06-18 10:50:30 +08002063 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002064 process_notifyPlayback(ctx, evt->playback.event, &status);
2065 }
2066 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002067 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2068 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2069 case DVR_PLAYBACK_EVENT_NO_KEY:
2070 {
2071 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
2072 } break;
2073 default:
2074 {
2075 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
2076 } break;
2077 }
2078 return 0;
2079}
2080
2081static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2082{
2083 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2084}
2085