blob: 7db2a831a87718f34e91033f71132a13037a2bf9 [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*/
hualing chenc110f952021-01-18 11:25:37 +08001267 DVR_Bool_t is_timeshift = DVR_FALSE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001268
1269 DVR_RETURN_IF_FALSE(playback);
1270 DVR_RETURN_IF_FALSE(p_pids);
1271
hualing chenc110f952021-01-18 11:25:37 +08001272 ctx_record = NULL;
1273
1274 /*lock the recorder to avoid changing the recording segments*/
1275 ctx_record = ctx_getRecord(sn_timeshift_record);
1276
1277 if (ctx_record) {
1278 pthread_mutex_lock(&ctx_record->lock);
1279 if (!ctx_valid(ctx_record)
1280 || ctx_record->sn != sn_timeshift_record) {
1281 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error found\n");
1282 pthread_mutex_unlock(&ctx_record->lock);
1283 is_timeshift = DVR_FALSE;
1284 } else {
1285 is_timeshift = DVR_TRUE;
1286 }
1287 }
1288
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001289 ctx = ctx_getPlayback((unsigned long)playback);
1290 DVR_RETURN_IF_FALSE(ctx);
1291
1292 pthread_mutex_lock(&ctx->lock);
1293
1294 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",
1295 ctx->sn,
1296 ctx->playback.param_open.location,
1297 flags,
1298 p_pids->video.pid, p_pids->video.format,
1299 p_pids->audio.pid, p_pids->audio.format,
1300 p_pids->ad.pid, p_pids->ad.format,
1301 p_pids->subtitle.pid, p_pids->subtitle.format,
1302 p_pids->pcr.pid);
1303
1304 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1305
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001306 if (ctx->playback.param_open.is_timeshift) {
1307 /*lock the recorder to avoid changing the recording segments*/
hualing chenc110f952021-01-18 11:25:37 +08001308 if (is_timeshift == DVR_FALSE) {
1309 DVR_WRAPPER_DEBUG(1, "timeshift, record is not for timeshifting, FATAL error return\n");
1310 pthread_mutex_unlock(&ctx->lock);
1311 return DVR_FAILURE;
1312 } else {
1313 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) record(sn:%ld) locked ok due to timeshift\n",
1314 ctx->sn, ctx_record->sn);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001315 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001316 }
1317
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001318 /*obtain all segments in a list*/
1319 segment_nb = 0;
1320 p_segment_ids = NULL;
1321 error = dvr_segment_get_list(ctx->playback.param_open.location, &segment_nb, &p_segment_ids);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001322 if (!error) {
1323 got_1st_seg = 0;
1324 for (i = 0; i < segment_nb; i++) {
1325 DVR_RecordSegmentInfo_t seg_info;
1326 DVR_PlaybackSegmentFlag_t flags;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001327
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001328 error = dvr_segment_get_info(ctx->playback.param_open.location, p_segment_ids[i], &seg_info);
1329 if (error) {
1330 error = DVR_FAILURE;
1331 DVR_WRAPPER_DEBUG(1, "fail to get seg info (location:%s, seg:%llu), (error:%d)\n",
1332 ctx->playback.param_open.location, p_segment_ids[i], error);
1333 break;
1334 }
hualing chen92f3a142020-07-08 20:59:33 +08001335 //add check if has audio or video pid. if not exist. not add segment to playback
1336 int ii = 0;
1337 int has_av = 0;
1338 for (ii = 0; ii < seg_info.nb_pids; ii++) {
1339 int type = (seg_info.pids[ii].type >> 24) & 0x0f;
1340 if (type == DVR_STREAM_TYPE_VIDEO ||
1341 type == DVR_STREAM_TYPE_AUDIO ||
1342 type == DVR_STREAM_TYPE_AD) {
1343 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1344 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,
1345 DVR_STREAM_TYPE_VIDEO,
1346 DVR_STREAM_TYPE_AUDIO,
1347 DVR_STREAM_TYPE_AD);
1348 has_av = 1;
1349 //break;
1350 } else {
1351 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,
1352 DVR_STREAM_TYPE_VIDEO,
1353 DVR_STREAM_TYPE_AUDIO,
1354 DVR_STREAM_TYPE_AD);
1355 }
1356 }
1357 if (has_av == 0) {
1358 DVR_WRAPPER_DEBUG(1, "fail to get seg av info \n");
1359 continue;
1360 } else {
1361 DVR_WRAPPER_DEBUG(1, "success to get seg av info \n");
1362 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001363 flags = DVR_PLAYBACK_SEGMENT_DISPLAYABLE | DVR_PLAYBACK_SEGMENT_CONTINUOUS;
1364 error = wrapper_addPlaybackSegment(ctx, &seg_info, p_pids, flags);
1365 if (error)
1366 break;
1367
1368 /*copy the 1st segment*/
1369 if (got_1st_seg == 0) {
1370 seg_info_1st = seg_info;
1371 got_1st_seg = 1;
1372 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001373 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001374 free(p_segment_ids);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001375
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001376 /* return if no segment or fail to add */
1377 if (!error && got_1st_seg) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001378
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001379 /*copy the obsolete infomation, must for timeshifting*/
1380 if (ctx->playback.param_open.is_timeshift && ctx_record) {
1381 ctx->playback.obsolete = ctx_record->record.obsolete;
1382 }
1383
1384 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) (%d) segments added\n", ctx->sn, i);
1385
1386 ctx->playback.reach_end = DVR_FALSE;
1387 if ((flags&DVR_PLAYBACK_STARTED_PAUSEDLIVE) == DVR_PLAYBACK_STARTED_PAUSEDLIVE)
1388 ctx->playback.speed = 0.0f;
1389 else
1390 ctx->playback.speed = 100.0f;
1391
1392 ctx->playback.pids_req = *p_pids;
1393
1394 error = dvr_playback_seek(ctx->playback.player, seg_info_1st.id, 0);
1395 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seek(seg:%llu 0) for start (%d)\n",
1396 ctx->sn, seg_info_1st.id, error);
1397
1398 error = dvr_playback_start(ctx->playback.player, flags);
1399
1400 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) started (%d)\n", ctx->sn, error);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001401 }
1402 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001403
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001404 if (ctx->playback.param_open.is_timeshift) {
1405 /*unlock the recorder locked above*/
1406 if (ctx_record && ctx_valid(ctx_record)) {
1407 pthread_mutex_unlock(&ctx_record->lock);
1408 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld), record(sn:%ld) unlocked ok due to timeshift\n",
1409 ctx->sn, ctx_record->sn);
1410 }
1411 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001412
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001413
1414 pthread_mutex_unlock(&ctx->lock);
1415
1416 return error;
1417}
1418
1419int dvr_wrapper_stop_playback (DVR_WrapperPlayback_t playback)
1420{
1421 DVR_WrapperCtx_t *ctx;
1422 int error;
1423
1424 DVR_RETURN_IF_FALSE(playback);
1425
1426 ctx = ctx_getPlayback((unsigned long)playback);
1427 DVR_RETURN_IF_FALSE(ctx);
1428
1429 pthread_mutex_lock(&ctx->lock);
1430 DVR_WRAPPER_DEBUG(1, "stop playback(sn:%ld) ...\n", ctx->sn);
1431 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1432
1433 error = dvr_playback_stop(ctx->playback.player, DVR_TRUE);
1434
1435 {
1436 /*remove all segments*/
1437 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1438
1439 list_for_each_entry(pseg, &ctx->segments, head) {
1440 error = dvr_playback_remove_segment(ctx->playback.player, pseg->playback_info.segment_id);
1441 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) remove seg(%lld) (%d)\n",
1442 ctx->sn, pseg->playback_info.segment_id, error);
1443 }
1444 ctx_freeSegments(ctx);
1445 }
1446
1447 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) stopped (%d)\n", ctx->sn, error);
1448 pthread_mutex_unlock(&ctx->lock);
1449
1450 return error;
1451}
1452
1453int dvr_wrapper_pause_playback (DVR_WrapperPlayback_t playback)
1454{
1455 DVR_WrapperCtx_t *ctx;
1456 int error;
1457
1458 DVR_RETURN_IF_FALSE(playback);
1459
1460 ctx = ctx_getPlayback((unsigned long)playback);
1461 DVR_RETURN_IF_FALSE(ctx);
1462
1463 pthread_mutex_lock(&ctx->lock);
1464 DVR_WRAPPER_DEBUG(1, "pause playback(sn:%ld) ...\n", ctx->sn);
1465 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
hualing chen36e0dfd2020-05-02 16:33:06 +08001466 //clear end event
Zhiqiang Hanb723cdb2020-05-09 11:10:29 +08001467 if (ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_END)
hualing chen36e0dfd2020-05-02 16:33:06 +08001468 ctx->playback.last_event = DVR_PLAYBACK_EVENT_TRANSITION_OK;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001469
1470 error = dvr_playback_pause(ctx->playback.player, DVR_FALSE);
1471
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001472 ctx->playback.speed = 0.0f;
1473
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001474 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) paused (%d)\n", ctx->sn, error);
1475 pthread_mutex_unlock(&ctx->lock);
1476
1477 return error;
1478}
1479
1480int dvr_wrapper_resume_playback (DVR_WrapperPlayback_t playback)
1481{
1482 DVR_WrapperCtx_t *ctx;
1483 int error;
1484
1485 DVR_RETURN_IF_FALSE(playback);
1486
1487 ctx = ctx_getPlayback((unsigned long)playback);
1488 DVR_RETURN_IF_FALSE(ctx);
1489
1490 pthread_mutex_lock(&ctx->lock);
1491 DVR_WRAPPER_DEBUG(1, "resume playback(sn:%ld) ...\n", ctx->sn);
1492 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1493
1494 error = dvr_playback_resume(ctx->playback.player);
1495
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001496 ctx->playback.speed = 100.0f;
1497
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001498 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) resumed (%d)\n", ctx->sn, error);
1499 pthread_mutex_unlock(&ctx->lock);
1500
1501 return error;
1502}
1503
1504int dvr_wrapper_set_playback_speed (DVR_WrapperPlayback_t playback, float speed)
1505{
1506 DVR_WrapperCtx_t *ctx;
1507 int error;
1508 DVR_PlaybackSpeed_t dvr_speed = {
1509 .speed = { speed },
1510 .mode = (speed > 0) ? DVR_PLAYBACK_FAST_FORWARD : DVR_PLAYBACK_FAST_BACKWARD
1511 };
1512
1513 DVR_RETURN_IF_FALSE(playback);
1514
1515 ctx = ctx_getPlayback((unsigned long)playback);
1516 DVR_RETURN_IF_FALSE(ctx);
1517
1518 pthread_mutex_lock(&ctx->lock);
hualing chenc70a8df2020-05-12 19:23:11 +08001519 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 +08001520 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1521
1522 error = dvr_playback_set_speed(ctx->playback.player, dvr_speed);
1523
Zhiqiang Han3eb75f92020-04-08 10:07:55 +08001524 if (ctx->playback.speed != 0.0f && ctx->playback.speed != 100.0f
1525 && ctx->playback.last_event == DVR_PLAYBACK_EVENT_REACHED_BEGIN
1526 && ctx->playback.seg_status.state == DVR_PLAYBACK_STATE_PAUSE) {
1527 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, paused, do resume first\n", ctx->playback.speed, speed);
1528 error = dvr_playback_resume(ctx->playback.player);
1529 } else if (ctx->playback.speed == 0.0f
1530 && speed != 0.0f
1531 && speed != 100.0f) {
1532 /*libdvr do not support pause with speed=0, will not be here*/
1533 DVR_WRAPPER_DEBUG(1, "x%f -> x%f, do resume first\n", ctx->playback.speed, speed);
1534 error = dvr_playback_resume(ctx->playback.player);
1535 }
1536
1537 ctx->playback.speed = speed;
1538
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001539 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) speeded(x%f) (%d)\n",
1540 ctx->sn, speed, error);
1541 pthread_mutex_unlock(&ctx->lock);
1542
1543 return error;
1544}
1545
1546int dvr_wrapper_seek_playback (DVR_WrapperPlayback_t playback, uint32_t time_offset)
1547{
1548 DVR_WrapperCtx_t *ctx;
1549 int error;
1550 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1551 uint64_t segment_id;
1552 uint32_t off;
1553 uint64_t last_segment_id;
1554 uint32_t pre_off;
1555
1556 DVR_RETURN_IF_FALSE(playback);
1557
1558 ctx = ctx_getPlayback((unsigned long)playback);
1559 DVR_RETURN_IF_FALSE(ctx);
1560
1561 pthread_mutex_lock(&ctx->lock);
1562
1563 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (off:%d) ...\n", ctx->sn, time_offset);
1564 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1565
1566 off = 0;
1567 segment_id = 0;
1568 pre_off = 0;
1569 last_segment_id = 0;
1570
1571 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1572 segment_id = pseg->seg_info.id;
1573
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001574 if ((ctx->playback.obsolete.time + pre_off + pseg->seg_info.duration) > time_offset)
1575 break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001576
1577 last_segment_id = pseg->seg_info.id;
1578 pre_off += pseg->seg_info.duration;
1579 }
1580
1581 if (last_segment_id == segment_id) {
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001582 /*1.only one seg with id:0, 2.offset exceeds the total duration*/
1583 off = time_offset;
1584 } else if (ctx->playback.obsolete.time >= time_offset) {
1585 off = 0;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001586 } else {
hualing chenda76fc52020-05-28 14:56:42 +08001587 off = time_offset - pre_off - ctx->playback.obsolete.time;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001588 }
1589
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001590 DVR_WRAPPER_DEBUG(1, "seek playback(sn:%ld) (seg:%lld, off:%d)\n",
1591 ctx->sn, segment_id, off);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001592 error = dvr_playback_seek(ctx->playback.player, segment_id, off);
1593 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) seeked(off:%d) (%d)\n", ctx->sn, time_offset, error);
1594
1595 pthread_mutex_unlock(&ctx->lock);
1596
1597 return error;
1598}
1599
1600int dvr_wrapper_update_playback (DVR_WrapperPlayback_t playback, DVR_PlaybackPids_t *p_pids)
1601{
1602 DVR_WrapperCtx_t *ctx;
1603 int error;
1604 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1605
1606 DVR_RETURN_IF_FALSE(playback);
1607 DVR_RETURN_IF_FALSE(p_pids);
1608
1609 ctx = ctx_getPlayback((unsigned long)playback);
1610 DVR_RETURN_IF_FALSE(ctx);
1611
1612 pthread_mutex_lock(&ctx->lock);
1613
1614 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) ...\n",
1615 ctx->sn,
1616 p_pids->video.pid, p_pids->video.format,
1617 p_pids->audio.pid, p_pids->audio.format);
1618 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1619
1620 ctx->playback.pids_req = *p_pids;
1621
1622 error = 0;
1623 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1624 /*should update the whole list of segments*/
1625 /*if (pseg->seg_info.id == ctx->current_segment_id)*/ {
1626 /*list_for_each_entry_from(pseg, &ctx->segments, head)*/ {
1627 /*check udpate for pids*/
1628 if (memcmp(&pseg->playback_info.pids, p_pids, sizeof(*p_pids)) != 0) {
1629 pseg->playback_info.pids = *p_pids;
1630 error = dvr_playback_update_segment_pids(ctx->playback.player, pseg->seg_info.id, p_pids);
1631 if (error) {
1632 DVR_WRAPPER_DEBUG(1, "failed to playback(sn:%ld) update segment(id:%lld) pids (%d)\n",
1633 ctx->sn, pseg->seg_info.id, error);
1634 /*do not break, let list updated*/
1635 }
1636 }
1637 }
1638 /*break;*/
1639 }
1640 }
1641
1642 DVR_WRAPPER_DEBUG(1, "update playback(sn:%ld) v/a(%d:%d/%d:%d) (%d)\n",
1643 ctx->sn,
1644 p_pids->video.pid, p_pids->video.format,
1645 p_pids->audio.pid, p_pids->audio.format,
1646 error);
1647
1648 pthread_mutex_unlock(&ctx->lock);
1649
1650 return error;
1651}
1652
1653int dvr_wrapper_get_playback_status(DVR_WrapperPlayback_t playback, DVR_WrapperPlaybackStatus_t *status)
1654{
1655 DVR_WrapperCtx_t *ctx;
1656 DVR_WrapperPlaybackStatus_t s;
1657 DVR_PlaybackStatus_t play_status;
1658 int error;
1659
1660 DVR_RETURN_IF_FALSE(playback);
1661 DVR_RETURN_IF_FALSE(status);
1662
1663 ctx = ctx_getPlayback((unsigned long)playback);
1664 DVR_RETURN_IF_FALSE(ctx);
1665
1666 pthread_mutex_lock(&ctx->lock);
1667
1668 DVR_WRAPPER_DEBUG(1, "get playback(sn:%ld) status ...\n", ctx->sn);
1669 DVR_RETURN_IF_FALSE_WITH_UNLOCK(ctx_valid(ctx), &ctx->lock);
1670
1671 error = dvr_playback_get_status(ctx->playback.player, &play_status);
1672 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) get status (%d)\n", ctx->sn, error);
1673
1674 ctx->playback.seg_status = play_status;
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001675 error = process_generatePlaybackStatus(ctx, &s);
1676
hualing chenb5cd42e2020-04-15 17:03:34 +08001677 if (ctx->playback.reach_end == DVR_TRUE && ctx->playback.param_open.is_timeshift == DVR_FALSE) {
1678 //reach end need set full time to cur.so app can exist playback.
1679 DVR_WRAPPER_DEBUG(1, "set cur time to full time, reach end occur");
1680 s.info_cur.time = s.info_full.time;
1681 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001682 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) state/cur/full/obsl(%d/%ld/%ld/%ld) (%d)\n",
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001683 ctx->sn,
1684 s.state,
1685 s.info_cur.time,
1686 s.info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001687 s.info_obsolete.time,
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001688 error);
1689
1690 *status = s;
1691
1692 pthread_mutex_unlock(&ctx->lock);
1693
1694 return error;
1695}
1696
hualing chen266b9502020-04-04 17:39:39 +08001697int dvr_wrapper_set_playback_secure_buffer (DVR_WrapperPlayback_t playback, uint8_t *p_secure_buf, uint32_t len)
1698{
1699 DVR_WrapperCtx_t *ctx;
1700 int error;
1701
1702 DVR_RETURN_IF_FALSE(playback);
1703 DVR_RETURN_IF_FALSE(p_secure_buf);
1704
1705 ctx = ctx_getPlayback((unsigned long)playback);
1706 DVR_RETURN_IF_FALSE(ctx);
1707
1708 pthread_mutex_lock(&ctx->lock);
1709 error = dvr_playback_set_secure_buffer(ctx->playback.player, p_secure_buf, len);
1710 pthread_mutex_unlock(&ctx->lock);
1711 return error;
1712}
1713
1714int dvr_wrapper_set_playback_decrypt_callback (DVR_WrapperPlayback_t playback, DVR_CryptoFunction_t func, void *userdata)
1715{
1716 DVR_WrapperCtx_t *ctx;
1717 int error;
1718
1719 DVR_RETURN_IF_FALSE(playback);
1720 DVR_RETURN_IF_FALSE(func);
1721
1722 ctx = ctx_getPlayback((unsigned long)playback);
1723 DVR_RETURN_IF_FALSE(ctx);
1724
1725 pthread_mutex_lock(&ctx->lock);
1726 error = dvr_playback_set_decrypt_callback(ctx->playback.player, func, userdata);
1727 pthread_mutex_unlock(&ctx->lock);
1728 return error;
1729}
1730
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001731static DVR_Result_t wrapper_record_event_handler(DVR_RecordEvent_t event, void *params, void *userdata)
1732{
1733 DVR_WrapperEventCtx_t evt;
1734
1735 DVR_RETURN_IF_FALSE(userdata);
1736
1737 evt.sn = (unsigned long)userdata;
1738 evt.type = W_REC;
1739 evt.record.event = event;
1740 evt.record.status = *(DVR_RecordStatus_t *)params;
1741 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, record, evt:0x%x]\n", evt.sn, evt.record.event);
1742 return ctx_addRecordEvent(&evt);
1743}
1744
1745static DVR_Result_t wrapper_playback_event_handler(DVR_PlaybackEvent_t event, void *params, void *userdata)
1746{
1747 DVR_WrapperEventCtx_t evt;
1748
1749 DVR_RETURN_IF_FALSE(userdata);
1750
1751 evt.sn = (unsigned long)userdata;
1752 evt.type = W_PLAYBACK;
1753 evt.playback.event = event;
1754 evt.playback.status = *(DVR_Play_Notify_t *)params;
1755 DVR_WRAPPER_DEBUG(1, "evt[sn:%ld, playbck, evt:0x%x]\n", evt.sn, evt.playback.event);
1756 return ctx_addPlaybackEvent(&evt);
1757}
1758
1759static inline int process_notifyRecord(DVR_WrapperCtx_t *ctx, DVR_RecordEvent_t evt, DVR_WrapperRecordStatus_t *status)
1760{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001761 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 +08001762 ctx->sn,
1763 evt,
1764 status->info.time,
1765 status->info.size,
1766 status->info.pkts,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001767 status->info_obsolete.time,
1768 status->info_obsolete.size,
1769 status->info_obsolete.pkts);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001770
1771 if (ctx->record.event_fn)
1772 return ctx->record.event_fn(evt, status, ctx->record.event_userdata);
1773 return 0;
1774}
1775
1776static inline int record_startNextSegment(DVR_WrapperCtx_t *ctx)
1777{
1778 DVR_RecordStartParams_t param;
1779 DVR_RecordSegmentInfo_t seg_info;
1780 int i;
1781 int error;
1782
1783 memcpy(&param, &ctx->record.param_update, sizeof(param));
1784 memset(&ctx->record.param_update.segment, 0, sizeof(ctx->record.param_update.segment));
1785 ctx->record.param_update.segment.segment_id = ctx->record.next_segment_id++;
1786 for (i = 0; i < param.segment.nb_pids; i++) {
1787 if (param.segment.pid_action[i] != DVR_RECORD_PID_CLOSE) {
1788 ctx->record.param_update.segment.pids[ctx->record.param_update.segment.nb_pids] = param.segment.pids[i];
1789 ctx->record.param_update.segment.pid_action[ctx->record.param_update.segment.nb_pids] = DVR_RECORD_PID_KEEP;
1790 ctx->record.param_update.segment.nb_pids++;
1791 }
1792 }
1793 error = dvr_record_next_segment(ctx->record.recorder, &ctx->record.param_update, &seg_info);
1794 {
1795 DVR_RecordSegmentInfo_t new_seg_info =
1796 { .id = ctx->record.param_update.segment.segment_id, };
1797 wrapper_updateRecordSegment(ctx, &seg_info, U_ALL);
1798 wrapper_addRecordSegment(ctx, &new_seg_info);
1799 }
1800
Zhiqiang Hand977e972020-05-11 11:30:47 +08001801 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 +08001802 return error;
1803}
1804
1805static inline int record_removeSegment(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordSegmentInfo_t *pseg)
1806{
1807 return wrapper_removeRecordSegment(ctx, pseg);
1808}
1809
1810/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001811static int process_generateRecordStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperRecordStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001812{
1813 /*the current seg is not covered in the statistics*/
1814 DVR_WrapperRecordSegmentInfo_t *pseg;
1815
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001816 /*re-calculate the all segments*/
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001817 memset(&ctx->record.status, 0, sizeof(ctx->record.status));
1818
1819 ctx->record.status.state = ctx->record.seg_status.state;
1820 ctx->record.status.pids.nb_pids = ctx->record.seg_status.info.nb_pids;
1821 memcpy(ctx->record.status.pids.pids,
1822 ctx->record.seg_status.info.pids,
1823 sizeof(ctx->record.status.pids.pids));
1824 ctx->current_segment_id = ctx->record.seg_status.info.id;
1825
1826 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1827 if (pseg->info.id != ctx->record.seg_status.info.id) {
1828 ctx->record.status.info.time += pseg->info.duration;
1829 ctx->record.status.info.size += pseg->info.size;
1830 ctx->record.status.info.pkts += pseg->info.nb_packets;
1831 }
1832 }
1833
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001834 ctx->record.status.info_obsolete = ctx->record.obsolete;
1835
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001836 wrapper_updateRecordSegment(ctx, &ctx->record.seg_status.info, U_ALL);
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001837
1838 if (status) {
1839 *status = ctx->record.status;
1840 status->info.time += ctx->record.seg_status.info.duration;
1841 status->info.size += ctx->record.seg_status.info.size;
1842 status->info.pkts += ctx->record.seg_status.info.nb_packets;
1843 }
1844
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001845 return DVR_SUCCESS;
1846}
1847
1848
1849static int process_handleRecordEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
1850{
1851 DVR_WrapperRecordStatus_t status;
1852
1853 memset(&status, 0, sizeof(status));
1854
1855 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d)\n",
1856 evt->sn, evt->record.event, evt->record.status.state);
1857
1858 switch (evt->record.event)
1859 {
1860 case DVR_RECORD_EVENT_STATUS:
1861 {
1862 switch (evt->record.status.state)
1863 {
1864 case DVR_RECORD_STATE_OPENED:
1865 case DVR_RECORD_STATE_CLOSED:
1866 {
1867 ctx->record.seg_status = evt->record.status;
1868
1869 status.state = evt->record.status.state;
1870 process_notifyRecord(ctx, evt->record.event, &status);
1871 } break;
1872 case DVR_RECORD_STATE_STARTED:
1873 {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001874 ctx->record.seg_status = evt->record.status;
1875
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001876 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001877 process_notifyRecord(ctx, evt->record.event, &status);
1878
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001879 /*restart to next segment*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001880 if (ctx->record.param_open.segment_size
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001881 && evt->record.status.info.size >= ctx->record.param_open.segment_size) {
1882 DVR_WRAPPER_DEBUG(1, "start new segment for record(%lu), reaches segment size limit, cur(%zu) max(%lld)\n",
1883 ctx->sn,
1884 evt->record.status.info.size,
1885 ctx->record.param_open.segment_size);
Zhiqiang Hand977e972020-05-11 11:30:47 +08001886 if (record_startNextSegment(ctx) != DVR_SUCCESS) {
1887 /*should notify the recording's stop*/
1888 int error = dvr_record_close(ctx->record.recorder);
1889 DVR_WRAPPER_DEBUG(1, "stop record(%lu)=%d, failed to start new segment for recording.",
1890 ctx->sn, error);
1891 status.state = DVR_RECORD_STATE_CLOSED;
1892 process_notifyRecord(ctx, DVR_RECORD_EVENT_WRITE_ERROR, &status);
1893 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001894 }
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001895
1896 if (ctx->record.param_open.is_timeshift
1897 && ctx->record.param_open.max_time
1898 && status.info.time >= ctx->record.param_open.max_time) {
1899 DVR_WrapperRecordSegmentInfo_t *pseg;
1900
1901 /*as the player do not support null playlist,
1902 there must be one segment existed at any time,
1903 we have to keep two segments before remove one*/
1904 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1905 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1906 /*only one segment, waiting for more*/
1907 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of max_time(%ld) of record < max size of segment(%lld)\n",
1908 status.info.size,
1909 ctx->record.param_open.max_time,
1910 ctx->record.param_open.segment_size);
1911 } else {
1912 /*timeshifting, remove the 1st segment and notify the player*/
1913 record_removeSegment(ctx, pseg);
1914
1915 process_generateRecordStatus(ctx, &status);
1916 process_notifyRecord(ctx, evt->record.event, &status);
1917 }
1918 }
1919
1920 if (ctx->record.param_open.is_timeshift
1921 && ctx->record.param_open.max_size
1922 && status.info.size >= ctx->record.param_open.max_size) {
1923 DVR_WrapperRecordSegmentInfo_t *pseg;
1924
1925 /*as the player do not support null playlist,
1926 there must be one segment existed at any time,
1927 we have to keep two segments before remove one*/
1928 pseg = list_last_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head);
1929 if (pseg == list_first_entry(&ctx->segments, DVR_WrapperRecordSegmentInfo_t, head)) {
1930 /*only one segment, waiting for more*/
1931 DVR_WRAPPER_DEBUG(1, "warning: the size(%lld) of record < max size of segment(%lld)\n",
1932 status.info.size,
1933 ctx->record.param_open.segment_size);
1934 } else {
1935 record_removeSegment(ctx, pseg);
1936
1937 process_generateRecordStatus(ctx, &status);
1938 process_notifyRecord(ctx, evt->record.event, &status);
1939 }
1940 }
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001941 } break;
1942 case DVR_RECORD_STATE_STOPPED:
1943 {
1944 ctx->record.seg_status = evt->record.status;
1945
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001946 process_generateRecordStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001947 process_notifyRecord(ctx, evt->record.event, &status);
1948 } break;
1949 default:
1950 break;
1951 }
1952 } break;
hualing chen4b7c15d2020-04-07 16:13:48 +08001953 case DVR_RECORD_EVENT_WRITE_ERROR: {
1954 ctx->record.seg_status = evt->record.status;
1955 status.state = evt->record.status.state;
1956 process_notifyRecord(ctx, evt->record.event, &status);
1957 }break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001958 default:
1959 break;
1960 }
1961 return DVR_SUCCESS;
1962}
1963
1964static inline int process_notifyPlayback(DVR_WrapperCtx_t *ctx, DVR_PlaybackEvent_t evt, DVR_WrapperPlaybackStatus_t *status)
1965{
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001966 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 +08001967 ctx->sn,
1968 evt,
1969 status->state,
1970 status->info_cur.time,
1971 status->info_full.time,
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001972 status->info_obsolete.time);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001973
1974 if (ctx->playback.event_fn)
1975 return ctx->playback.event_fn(evt, status, ctx->playback.event_userdata);
1976 return 0;
1977}
1978
1979/*should run periodically to update the current status*/
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08001980static int process_generatePlaybackStatus(DVR_WrapperCtx_t *ctx, DVR_WrapperPlaybackStatus_t *status)
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08001981{
1982 /*the current seg is not covered in the statistics*/
1983 DVR_WrapperPlaybackSegmentInfo_t *pseg;
1984
1985 memset(&ctx->playback.status, 0, sizeof(ctx->playback.status));
1986 ctx->playback.status.pids = ctx->playback.pids_req;
1987
1988 ctx->playback.status.state = ctx->playback.seg_status.state;
1989 ctx->playback.status.speed = ctx->playback.seg_status.speed;
1990 ctx->playback.status.flags = ctx->playback.seg_status.flags;
1991 ctx->current_segment_id = ctx->playback.seg_status.segment_id;
1992
1993 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
1994 if (pseg->seg_info.id == ctx->playback.seg_status.segment_id)
1995 break;
1996 ctx->playback.status.info_cur.time += pseg->seg_info.duration;
1997 ctx->playback.status.info_cur.size += pseg->seg_info.size;
1998 ctx->playback.status.info_cur.pkts += pseg->seg_info.nb_packets;
1999 }
2000 list_for_each_entry_reverse(pseg, &ctx->segments, head) {
2001 ctx->playback.status.info_full.time += pseg->seg_info.duration;
2002 ctx->playback.status.info_full.size += pseg->seg_info.size;
2003 ctx->playback.status.info_full.pkts += pseg->seg_info.nb_packets;
2004 }
2005
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002006 if (status) {
2007 *status = ctx->playback.status;
2008 /*deal with current, lack size and pkts with the current*/
2009 status->info_cur.time += ctx->playback.seg_status.time_cur;
2010 status->info_obsolete.time = ctx->playback.obsolete.time;
2011 }
2012
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002013 return DVR_SUCCESS;
2014}
2015
2016static int process_handlePlaybackEvent(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2017{
2018 DVR_WRAPPER_DEBUG(1, "evt (sn:%ld) 0x%x (state:%d) cur(%lld:%u/%u)\n",
2019 evt->sn, evt->playback.event,
2020 evt->playback.status.play_status.state,
2021 evt->playback.status.play_status.segment_id,
2022 evt->playback.status.play_status.time_cur,
2023 evt->playback.status.play_status.time_end);
2024
2025 /*evt PLAYTIME will break the last logic, do not save*/
hualing chene3797f02021-01-13 14:53:28 +08002026 if (evt->playback.event != DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME
2027 && evt->playback.event != DVR_PLAYBACK_EVENT_NODATA
2028 && evt->playback.event != DVR_PLAYBACK_EVENT_DATARESUME
2029 )
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002030 ctx->playback.last_event = evt->playback.event;
2031
2032 switch (evt->playback.event)
2033 {
2034 case DVR_PLAYBACK_EVENT_FIRST_FRAME:
2035 case DVR_PLAYBACK_EVENT_REACHED_END:
2036 case DVR_PLAYBACK_EVENT_TRANSITION_OK:
2037 case DVR_PLAYBACK_EVENT_NOTIFY_PLAYTIME:
hualing chenb5cd42e2020-04-15 17:03:34 +08002038 case DVR_PLAYBACK_EVENT_ERROR:
hualing chenf291cf32020-06-18 10:50:30 +08002039 case DVR_PLAYBACK_EVENT_REACHED_BEGIN:
hualing chene3797f02021-01-13 14:53:28 +08002040 case DVR_PLAYBACK_EVENT_NODATA:
2041 case DVR_PLAYBACK_EVENT_DATARESUME:
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002042 {
2043 DVR_WrapperPlaybackStatus_t status;
2044
2045 /*copy status of segment*/
2046 ctx->playback.seg_status = evt->playback.status.play_status;
2047
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +08002048 /*generate status of the whole playback*/
2049 process_generatePlaybackStatus(ctx, &status);
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002050
2051 if (evt->playback.event == DVR_PLAYBACK_EVENT_REACHED_END) {
2052 if (ctx->playback.param_open.is_timeshift) {
2053 /*wait for more data in recording*/
2054 } else if ((status.info_cur.time + DVR_PLAYBACK_END_GAP) >= ctx->playback.status.info_full.time) {
2055 process_notifyPlayback(ctx, evt->playback.event, &status);
hualing chenb5cd42e2020-04-15 17:03:34 +08002056 } else {
2057 ctx->playback.reach_end = DVR_TRUE;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002058 }
hualing chenf291cf32020-06-18 10:50:30 +08002059 } else if (evt->playback.event != DVR_PLAYBACK_EVENT_REACHED_BEGIN) {
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002060 process_notifyPlayback(ctx, evt->playback.event, &status);
2061 }
2062 } break;
Zhiqiang Han2d8cd822020-03-16 13:58:10 +08002063 case DVR_PLAYBACK_EVENT_TRANSITION_FAILED:
2064 case DVR_PLAYBACK_EVENT_KEY_FAILURE:
2065 case DVR_PLAYBACK_EVENT_NO_KEY:
2066 {
2067 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) error event:0x%x\n", evt->sn, evt->playback.event);
2068 } break;
2069 default:
2070 {
2071 DVR_WRAPPER_DEBUG(1, "playback(sn:%ld) unknown event:0x%x\n", evt->sn, evt->playback.event);
2072 } break;
2073 }
2074 return 0;
2075}
2076
2077static inline int process_handleEvents(DVR_WrapperEventCtx_t *evt, DVR_WrapperCtx_t *ctx)
2078{
2079 return (evt->type == W_REC)? process_handleRecordEvent(evt, ctx) : process_handlePlaybackEvent(evt, ctx);
2080}
2081