/* * Output packet */ static int _timeshift_read ( timeshift_t *ts, timeshift_file_t **cur_file, off_t *cur_off, int *fd, streaming_message_t **sm, int *wait ) { if (*cur_file) { /* Open file */ if (*fd == -1) { tvhtrace("timeshift", "ts %d open file %s", ts->id, (*cur_file)->path); *fd = open((*cur_file)->path, O_RDONLY); } tvhtrace("timeshift", "ts %d seek to %"PRIoff_t, ts->id, *cur_off); lseek(*fd, *cur_off, SEEK_SET); /* Read msg */ ssize_t r = _read_msg(*fd, sm); if (r < 0) { streaming_message_t *e = streaming_msg_create_code(SMT_STOP, SM_CODE_UNDEFINED_ERROR); streaming_target_deliver2(ts->output, e); tvhlog(LOG_ERR, "timeshift", "ts %d could not read buffer", ts->id); return -1; } tvhtrace("timeshift", "ts %d read msg %p (%"PRIssize_t")", ts->id, *sm, r); /* Incomplete */ if (r == 0) { lseek(*fd, *cur_off, SEEK_SET); return 0; } /* Update */ *cur_off += r; /* Special case - EOF */ if (r == sizeof(size_t) || *cur_off > (*cur_file)->size) { close(*fd); *fd = -1; pthread_mutex_lock(&ts->rdwr_mutex); *cur_file = timeshift_filemgr_next(*cur_file, NULL, 0); pthread_mutex_unlock(&ts->rdwr_mutex); *cur_off = 0; // reset *wait = 0; /* Check SMT_START index */ } else { streaming_message_t *ssm = _timeshift_find_sstart(*cur_file, (*sm)->sm_time); if (ssm && ssm->sm_data != ts->smt_start) { streaming_target_deliver2(ts->output, streaming_msg_clone(ssm)); if (ts->smt_start) streaming_start_unref(ts->smt_start); ts->smt_start = ssm->sm_data; atomic_add(&ts->smt_start->ss_refcount, 1); } } } return 0; }
/* * Receive data */ static void timeshift_input ( void *opaque, streaming_message_t *sm ) { int exit = 0; timeshift_t *ts = opaque; pthread_mutex_lock(&ts->state_mutex); /* Control */ if (sm->sm_type == SMT_SKIP) { if (ts->state >= TS_LIVE) timeshift_write_skip(ts->rd_pipe.wr, sm->sm_data); } else if (sm->sm_type == SMT_SPEED) { if (ts->state >= TS_LIVE) timeshift_write_speed(ts->rd_pipe.wr, sm->sm_code); } else { /* Start */ if (sm->sm_type == SMT_START && ts->state == TS_INIT) { ts->state = TS_LIVE; } /* Pass-thru */ if (ts->state <= TS_LIVE) { streaming_target_deliver2(ts->output, streaming_msg_clone(sm)); } /* Check for exit */ if (sm->sm_type == SMT_EXIT || (sm->sm_type == SMT_STOP && sm->sm_code == 0)) exit = 1; /* Buffer to disk */ if ((ts->state > TS_LIVE) || (!ts->ondemand && (ts->state == TS_LIVE))) { sm->sm_time = getmonoclock(); streaming_target_deliver2(&ts->wr_queue.sq_st, sm); } else streaming_msg_free(sm); /* Exit/Stop */ if (exit) { timeshift_write_exit(ts->rd_pipe.wr); ts->state = TS_EXIT; } } pthread_mutex_unlock(&ts->state_mutex); }
void timeshift_destroy(streaming_target_t *pad) { timeshift_t *ts = (timeshift_t*)pad; streaming_message_t *sm; /* Must hold global lock */ lock_assert(&global_lock); /* Ensure the threads exits */ // Note: this is a workaround for the fact the Q might have been flushed // in reader thread (VERY unlikely) pthread_mutex_lock(&ts->state_mutex); sm = streaming_msg_create(SMT_EXIT); streaming_target_deliver2(&ts->wr_queue.sq_st, sm); timeshift_write_exit(ts->rd_pipe.wr); pthread_mutex_unlock(&ts->state_mutex); /* Wait for all threads */ pthread_join(ts->rd_thread, NULL); pthread_join(ts->wr_thread, NULL); /* Shut stuff down */ streaming_queue_deinit(&ts->wr_queue); close(ts->rd_pipe.rd); close(ts->rd_pipe.wr); /* Flush files */ timeshift_filemgr_flush(ts, NULL); free(ts->path); free(ts); }
/* * Receive data */ static void timeshift_input ( void *opaque, streaming_message_t *sm ) { int type = sm->sm_type; timeshift_t *ts = opaque; th_pkt_t *pkt, *pkt2; if (ts->exit) return; /* Control */ if (type == SMT_SKIP) { timeshift_write_skip(ts->rd_pipe.wr, sm->sm_data); streaming_msg_free(sm); } else if (type == SMT_SPEED) { timeshift_write_speed(ts->rd_pipe.wr, sm->sm_code); streaming_msg_free(sm); } else { /* Change PTS/DTS offsets */ if (ts->packet_mode && ts->start_pts && type == SMT_PACKET) { pkt = sm->sm_data; pkt2 = pkt_copy_shallow(pkt); pkt_ref_dec(pkt); sm->sm_data = pkt2; pkt2->pkt_pts += ts->start_pts; pkt2->pkt_dts += ts->start_pts; } /* Check for exit */ else if (type == SMT_EXIT || (type == SMT_STOP && sm->sm_code != SM_CODE_SOURCE_RECONFIGURED)) ts->exit = 1; else if (type == SMT_MPEGTS) ts->packet_mode = 0; /* Send to the writer thread */ if (ts->packet_mode) { sm->sm_time = ts->last_wr_time; if ((type == SMT_PACKET) && !timeshift_packet(ts, sm)) goto _exit; } else { if (ts->ref_time == 0) { ts->ref_time = getfastmonoclock(); sm->sm_time = 0; } else { sm->sm_time = getfastmonoclock() - ts->ref_time; } } streaming_target_deliver2(&ts->wr_queue.sq_st, sm); /* Exit/Stop */ _exit: if (ts->exit) timeshift_write_exit(ts->rd_pipe.wr); } }
static void timeshift_status ( timeshift_t *ts, int64_t current_time ) { streaming_message_t *tsm; timeshift_status_t *status; status = calloc(1, sizeof(timeshift_status_t)); timeshift_fill_status(ts, status, current_time); tsm = streaming_msg_create_data(SMT_TIMESHIFT_STATUS, status); streaming_target_deliver2(ts->output, tsm); }
/* * Flush all data to live */ static int _timeshift_flush_to_live ( timeshift_t *ts, timeshift_seek_t *seek, int *wait ) { streaming_message_t *sm; while (seek->file) { if (_timeshift_read(ts, seek, &sm, wait) == -1) return -1; if (!sm) break; timeshift_packet_log("ouf", ts, sm); streaming_target_deliver2(ts->output, sm); } return 0; }
static int timeshift_packet( timeshift_t *ts, streaming_message_t *sm ) { th_pkt_t *pkt = sm->sm_data; int64_t time; time = ts_rescale(pkt->pkt_pts, 1000000); if (ts->last_wr_time < time) ts->last_wr_time = time; sm->sm_time = ts->last_wr_time; timeshift_packet_log("wr ", ts, sm); streaming_target_deliver2(&ts->wr_queue.sq_st, sm); return 0; }
/* * Output packet */ static int _timeshift_read ( timeshift_t *ts, timeshift_seek_t *seek, streaming_message_t **sm, int *wait ) { timeshift_file_t *tsf = seek->file; ssize_t r; off_t off = 0; *sm = NULL; if (tsf) { /* Open file */ if (tsf->rfd < 0 && !tsf->ram) { tsf->rfd = tvh_open(tsf->path, O_RDONLY, 0); tvhtrace(LS_TIMESHIFT, "ts %d open file %s (fd %i)", ts->id, tsf->path, tsf->rfd); if (tsf->rfd < 0) return -1; } if (tsf->rfd >= 0) if ((off = lseek(tsf->rfd, tsf->roff, SEEK_SET)) != tsf->roff) tvherror(LS_TIMESHIFT, "ts %d seek to %s failed (off %"PRId64" != %"PRId64"): %s", ts->id, tsf->path, (int64_t)tsf->roff, (int64_t)off, strerror(errno)); /* Read msg */ r = _read_msg(tsf, -1, sm); if (r < 0) { streaming_message_t *e = streaming_msg_create_code(SMT_STOP, SM_CODE_UNDEFINED_ERROR); streaming_target_deliver2(ts->output, e); tvhtrace(LS_TIMESHIFT, "ts %d seek to %jd (woff %jd) (fd %i)", ts->id, (intmax_t)off, (intmax_t)tsf->woff, tsf->rfd); tvherror(LS_TIMESHIFT, "ts %d could not read buffer", ts->id); return -1; } tvhtrace(LS_TIMESHIFT, "ts %d seek to %jd (fd %i) read msg %p/%"PRId64" (%"PRId64")", ts->id, (intmax_t)off, tsf->rfd, *sm, *sm ? (*sm)->sm_time : -1, (int64_t)r); /* Special case - EOF */ if (r <= sizeof(size_t) || tsf->roff > tsf->size || *sm == NULL) { timeshift_file_get(seek->file); /* _read_close decreases file reference */ _read_close(seek); _seek_set_file(seek, timeshift_filemgr_next(tsf, NULL, 0), 0); *wait = 0; tvhtrace(LS_TIMESHIFT, "ts %d eof, seek->file %p (prev %p)", ts->id, seek->file, tsf); timeshift_filemgr_dump(ts); } } return 0; }
static void timeshift_packet_deliver ( timeshift_t *ts, streaming_message_t *sm ) { th_pkt_t *pkt = sm->sm_data; tvhtrace("timeshift", "ts %d pkt buf - stream %d type %c pts %10"PRId64 " dts %10"PRId64" dur %10d len %6zu time %14"PRId64, ts->id, pkt->pkt_componentindex, pkt_frametype_to_char(pkt->pkt_frametype), ts_rescale(pkt->pkt_pts, 1000000), ts_rescale(pkt->pkt_dts, 1000000), pkt->pkt_duration, pktbuf_len(pkt->pkt_payload), sm->sm_time); streaming_target_deliver2(&ts->wr_queue.sq_st, sm); }
/* * Flush all data to live */ static int _timeshift_flush_to_live ( timeshift_t *ts, timeshift_file_t **cur_file, streaming_message_t **sm, int *wait ) { time_t pts = 0; while (*cur_file) { if (_timeshift_read(ts, cur_file, sm, wait) == -1) return -1; if (!*sm) break; if ((*sm)->sm_type == SMT_PACKET) { pts = ((th_pkt_t*)(*sm)->sm_data)->pkt_pts; tvhlog(LOG_DEBUG, "timeshift", "ts %d deliver %"PRId64" pts=%"PRItime_t, ts->id, (*sm)->sm_time, pts); } streaming_target_deliver2(ts->output, *sm); *sm = NULL; } return 0; }
void timeshift_destroy(streaming_target_t *pad) { timeshift_t *ts = (timeshift_t*)pad; streaming_message_t *sm; int i; /* Must hold global lock */ lock_assert(&global_lock); /* Ensure the threads exits */ // Note: this is a workaround for the fact the Q might have been flushed // in reader thread (VERY unlikely) pthread_mutex_lock(&ts->state_mutex); sm = streaming_msg_create(SMT_EXIT); streaming_target_deliver2(&ts->wr_queue.sq_st, sm); timeshift_write_exit(ts->rd_pipe.wr); pthread_mutex_unlock(&ts->state_mutex); /* Wait for all threads */ pthread_join(ts->rd_thread, NULL); pthread_join(ts->wr_thread, NULL); /* Shut stuff down */ streaming_queue_deinit(&ts->wr_queue); for (i = 0; i < TIMESHIFT_BACKLOG_MAX; i++) streaming_queue_clear(&ts->backlog[i]); close(ts->rd_pipe.rd); close(ts->rd_pipe.wr); /* Flush files */ timeshift_filemgr_flush(ts, NULL); /* Release SMT_START index */ if (ts->smt_start) streaming_start_unref(ts->smt_start); if (ts->path) free(ts->path); free(ts); }
/* * Timeshift thread */ void *timeshift_reader ( void *p ) { timeshift_t *ts = p; int nfds, end, run = 1, wait = -1; timeshift_file_t *cur_file = NULL; int cur_speed = 100, keyframe_mode = 0; int64_t pause_time = 0, play_time = 0, last_time = 0; int64_t now, deliver, skip_time = 0; streaming_message_t *sm = NULL, *ctrl = NULL; timeshift_index_iframe_t *tsi = NULL; streaming_skip_t *skip = NULL; time_t last_status = 0; tvhpoll_t *pd; tvhpoll_event_t ev = { 0 }; pd = tvhpoll_create(1); ev.fd = ts->rd_pipe.rd; ev.events = TVHPOLL_IN; tvhpoll_add(pd, &ev, 1); /* Output */ while (run) { // Note: Previously we allowed unlimited wait, but we now must wake periodically // to output status message if (wait < 0 || wait > 1000) wait = 1000; /* Wait for data */ if(wait) nfds = tvhpoll_wait(pd, &ev, 1, wait); else nfds = 0; wait = -1; end = 0; skip = NULL; now = getmonoclock(); /* Control */ pthread_mutex_lock(&ts->state_mutex); if (nfds == 1) { if (_read_msg(NULL, ts->rd_pipe.rd, &ctrl) > 0) { /* Exit */ if (ctrl->sm_type == SMT_EXIT) { tvhtrace("timeshift", "ts %d read exit request", ts->id); run = 0; streaming_msg_free(ctrl); ctrl = NULL; /* Speed */ } else if (ctrl->sm_type == SMT_SPEED) { int speed = ctrl->sm_code; int keyframe; /* Bound it */ if (speed > 3200) speed = 3200; if (speed < -3200) speed = -3200; /* Ignore negative */ if (ts->ondemand && (speed < 0)) speed = cur_file ? speed : 0; /* Process */ if (cur_speed != speed) { /* Live playback */ if (ts->state == TS_LIVE) { /* Reject */ if (speed >= 100) { tvhlog(LOG_DEBUG, "timeshift", "ts %d reject 1x+ in live mode", ts->id); speed = 100; /* Set position */ } else { tvhlog(LOG_DEBUG, "timeshift", "ts %d enter timeshift mode", ts->id); timeshift_writer_flush(ts); pthread_mutex_lock(&ts->rdwr_mutex); if ((cur_file = timeshift_filemgr_get(ts, 1))) { cur_file->roff = cur_file->size; pause_time = cur_file->last; last_time = pause_time; } pthread_mutex_unlock(&ts->rdwr_mutex); } /* Buffer playback */ } else if (ts->state == TS_PLAY) { pause_time = last_time; /* Paused */ } else { } /* Check keyframe mode */ keyframe = (speed < 0) || (speed > 400); if (keyframe != keyframe_mode) { tvhlog(LOG_DEBUG, "timeshift", "using keyframe mode? %s", keyframe ? "yes" : "no"); keyframe_mode = keyframe; if (keyframe) { tsi = NULL; } } /* Update */ play_time = getmonoclock(); cur_speed = speed; if (speed != 100 || ts->state != TS_LIVE) ts->state = speed == 0 ? TS_PAUSE : TS_PLAY; tvhlog(LOG_DEBUG, "timeshift", "ts %d change speed %d", ts->id, speed); } /* Send on the message */ ctrl->sm_code = speed; streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; /* Skip/Seek */ } else if (ctrl->sm_type == SMT_SKIP) { skip = ctrl->sm_data; switch (skip->type) { case SMT_SKIP_LIVE: if (ts->state != TS_LIVE) { /* Reset */ if (ts->full) { pthread_mutex_lock(&ts->rdwr_mutex); timeshift_filemgr_flush(ts, NULL); ts->full = 0; pthread_mutex_unlock(&ts->rdwr_mutex); } /* Release */ if (sm) streaming_msg_free(sm); /* Find end */ skip_time = 0x7fffffffffffffffLL; // TODO: change this sometime! } break; case SMT_SKIP_ABS_TIME: if (ts->pts_delta == 0) { tvhlog(LOG_ERR, "timeshift", "ts %d abs skip not possible no PTS delta", ts->id); skip = NULL; break; } /* -fallthrough */ case SMT_SKIP_REL_TIME: /* Convert */ skip_time = ts_rescale(skip->time, 1000000); tvhlog(LOG_DEBUG, "timeshift", "ts %d skip %"PRId64" requested %"PRId64, ts->id, skip_time, skip->time); /* Live playback (stage1) */ if (ts->state == TS_LIVE) { pthread_mutex_lock(&ts->rdwr_mutex); if ((cur_file = timeshift_filemgr_get(ts, !ts->ondemand))) { cur_file->roff = cur_file->size; last_time = cur_file->last; } else { tvhlog(LOG_ERR, "timeshift", "ts %d failed to get current file", ts->id); skip = NULL; } pthread_mutex_unlock(&ts->rdwr_mutex); } /* May have failed */ if (skip) { skip_time += (skip->type == SMT_SKIP_ABS_TIME) ? ts->pts_delta : last_time; tvhlog(LOG_DEBUG, "timeshift", "ts %d skip last_time %"PRId64" pts_delta %"PRId64, ts->id, skip_time - ts->pts_delta, ts->pts_delta); /* Live (stage2) */ if (ts->state == TS_LIVE) { if (skip_time >= now) { tvhlog(LOG_DEBUG, "timeshift", "ts %d skip ignored, already live", ts->id); skip = NULL; } else { ts->state = TS_PLAY; } } } /* OK */ if (skip) { /* Adjust time */ play_time = now; pause_time = skip_time; tsi = NULL; /* Clear existing packet */ if (sm) streaming_msg_free(sm); sm = NULL; } break; default: tvhlog(LOG_ERR, "timeshift", "ts %d invalid/unsupported skip type: %d", ts->id, skip->type); skip = NULL; break; } /* Error */ if (!skip) { ((streaming_skip_t*)ctrl->sm_data)->type = SMT_SKIP_ERROR; streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; } /* Ignore */ } else { streaming_msg_free(ctrl); ctrl = NULL; } } } /* Status message */ if (now >= (last_status + 1000000)) { streaming_message_t *tsm; timeshift_status_t *status; timeshift_index_iframe_t *fst, *lst; status = calloc(1, sizeof(timeshift_status_t)); fst = _timeshift_first_frame(ts); lst = _timeshift_last_frame(ts); status->full = ts->full; status->shift = ts->state <= TS_LIVE ? 0 : ts_rescale_i(now - last_time, 1000000); if (lst && fst && lst != fst && ts->pts_delta != PTS_UNSET) { status->pts_start = ts_rescale_i(fst->time - ts->pts_delta, 1000000); status->pts_end = ts_rescale_i(lst->time - ts->pts_delta, 1000000); } else { status->pts_start = PTS_UNSET; status->pts_end = PTS_UNSET; } tsm = streaming_msg_create_data(SMT_TIMESHIFT_STATUS, status); streaming_target_deliver2(ts->output, tsm); last_status = now; } /* Done */ if (!run || !cur_file || ((ts->state != TS_PLAY && !skip))) { pthread_mutex_unlock(&ts->state_mutex); continue; } /* Calculate delivery time */ deliver = (now - play_time) + TIMESHIFT_PLAY_BUF; deliver = (deliver * cur_speed) / 100; deliver = (deliver + pause_time); /* Determine next packet */ if (!sm) { /* Rewind or Fast forward (i-frame only) */ if (skip || keyframe_mode) { timeshift_file_t *tsf = NULL; int64_t req_time; /* Time */ if (!skip) req_time = last_time + ((cur_speed < 0) ? -1 : 1); else req_time = skip_time; tvhlog(LOG_DEBUG, "timeshift", "ts %d skip to %"PRId64" from %"PRId64, ts->id, req_time - ts->pts_delta, last_time - ts->pts_delta); /* Find */ pthread_mutex_lock(&ts->rdwr_mutex); end = _timeshift_skip(ts, req_time, last_time, cur_file, &tsf, &tsi); pthread_mutex_unlock(&ts->rdwr_mutex); if (tsi) tvhlog(LOG_DEBUG, "timeshift", "ts %d skip found pkt @ %"PRId64, ts->id, tsi->time - ts->pts_delta); /* File changed (close) */ if ((tsf != cur_file) && cur_file && cur_file->rfd >= 0) { close(cur_file->rfd); cur_file->rfd = -1; } /* Position */ if (cur_file) cur_file->refcount--; if ((cur_file = tsf) != NULL) { if (tsi) cur_file->roff = tsi->pos; else cur_file->roff = 0; } } /* Find packet */ if (_timeshift_read(ts, &cur_file, &sm, &wait) == -1) { pthread_mutex_unlock(&ts->state_mutex); break; } } /* Send skip response */ if (skip) { if (sm && sm->sm_type == SMT_PACKET) { th_pkt_t *pkt = sm->sm_data; skip->time = pkt->pkt_pts; skip->type = SMT_SKIP_ABS_TIME; tvhlog(LOG_DEBUG, "timeshift", "ts %d skip to pts %"PRId64" ok, time %"PRId64, ts->id, ts_rescale(skip->time, 1000000), sm->sm_time - ts->pts_delta); } else { /* Report error */ skip->type = SMT_SKIP_ERROR; skip = NULL; tvhlog(LOG_DEBUG, "timeshift", "ts %d skip failed (%d)", ts->id, sm ? sm->sm_type : -1); } streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; } /* Deliver */ if (sm && (skip || (((cur_speed < 0) && (sm->sm_time >= deliver)) || ((cur_speed > 0) && (sm->sm_time <= deliver))))) { if (sm->sm_type == SMT_PACKET && tvhtrace_enabled()) { th_pkt_t *pkt = sm->sm_data; tvhtrace("timeshift", "ts %d pkt out - stream %d type %c pts %10"PRId64 " dts %10"PRId64 " dur %10d len %6zu time %14"PRItime_t, ts->id, pkt->pkt_componentindex, pkt_frametype_to_char(pkt->pkt_frametype), ts_rescale(pkt->pkt_pts, 1000000), ts_rescale(pkt->pkt_dts, 1000000), pkt->pkt_duration, pktbuf_len(pkt->pkt_payload), sm->sm_time - ts->pts_delta); } last_time = sm->sm_time; streaming_target_deliver2(ts->output, sm); sm = NULL; wait = 0; } else if (sm) { if (cur_speed > 0) wait = (sm->sm_time - deliver) / 1000; else wait = (deliver - sm->sm_time) / 1000; if (wait == 0) wait = 1; tvhtrace("timeshift", "ts %d wait %d", ts->id, wait); } /* Terminate */ if (!cur_file || end != 0) { if (!end) end = (cur_speed > 0) ? 1 : -1; /* Back to live (unless buffer is full) */ if (end == 1 && !ts->full) { tvhlog(LOG_DEBUG, "timeshift", "ts %d eob revert to live mode", ts->id); ts->state = TS_LIVE; cur_speed = 100; ctrl = streaming_msg_create_code(SMT_SPEED, cur_speed); streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; /* Flush timeshift buffer to live */ if (_timeshift_flush_to_live(ts, &cur_file, &sm, &wait) == -1) break; /* Close file (if open) */ if (cur_file && cur_file->rfd >= 0) { close(cur_file->rfd); cur_file->rfd = -1; } /* Flush ALL files */ if (ts->ondemand) timeshift_filemgr_flush(ts, NULL); /* Pause */ } else { if (cur_speed <= 0) { cur_speed = 0; ts->state = TS_PAUSE; } else { cur_speed = 100; ts->state = TS_PLAY; play_time = now; } tvhlog(LOG_DEBUG, "timeshift", "ts %d sob speed %d", ts->id, cur_speed); pause_time = last_time; ctrl = streaming_msg_create_code(SMT_SPEED, cur_speed); streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; } /* Flush unwanted */ } else if (ts->ondemand && cur_file) { pthread_mutex_lock(&ts->rdwr_mutex); timeshift_filemgr_flush(ts, cur_file); pthread_mutex_unlock(&ts->rdwr_mutex); } pthread_mutex_unlock(&ts->state_mutex); } /* Cleanup */ tvhpoll_destroy(pd); if (cur_file && cur_file->rfd >= 0) { close(cur_file->rfd); cur_file->rfd = -1; } if (sm) streaming_msg_free(sm); if (ctrl) streaming_msg_free(ctrl); tvhtrace("timeshift", "ts %d exit reader thread", ts->id); return NULL; }
/* * Output packet */ static int _timeshift_read ( timeshift_t *ts, timeshift_file_t **cur_file, streaming_message_t **sm, int *wait ) { timeshift_file_t *tsf = *cur_file; ssize_t r; off_t off, ooff; if (tsf) { /* Open file */ if (tsf->rfd < 0 && !tsf->ram) { tsf->rfd = open(tsf->path, O_RDONLY); tvhtrace("timeshift", "ts %d open file %s (fd %i)", ts->id, tsf->path, tsf->rfd); if (tsf->rfd < 0) return -1; } if (tsf->rfd >= 0) if ((off = lseek(tsf->rfd, tsf->roff, SEEK_SET)) != tsf->roff) tvherror("timeshift", "ts %d seek to %s failed (off %"PRId64" != %"PRId64"): %s", ts->id, tsf->path, (int64_t)tsf->roff, (int64_t)off, strerror(errno)); /* Read msg */ ooff = tsf->roff; r = _read_msg(tsf, -1, sm); if (r < 0) { streaming_message_t *e = streaming_msg_create_code(SMT_STOP, SM_CODE_UNDEFINED_ERROR); streaming_target_deliver2(ts->output, e); tvhtrace("timeshift", "ts %d seek to %jd (fd %i)", ts->id, (intmax_t)tsf->roff, tsf->rfd); tvhlog(LOG_ERR, "timeshift", "ts %d could not read buffer", ts->id); return -1; } tvhtrace("timeshift", "ts %d seek to %jd (fd %i) read msg %p (%"PRId64")", ts->id, (intmax_t)tsf->roff, tsf->rfd, *sm, (int64_t)r); /* Incomplete */ if (r == 0) { if (tsf->rfd >= 0) { tvhtrace("timeshift", "ts %d seek to %jd (fd %i) (incomplete)", ts->id, (intmax_t)tsf->roff, tsf->rfd); if ((off = lseek(tsf->rfd, ooff, SEEK_SET)) != ooff) tvherror("timeshift", "seek to %s failed (off %"PRId64" != %"PRId64"): %s", tsf->path, (int64_t)ooff, (int64_t)off, strerror(errno)); } tsf->roff = ooff; return 0; } /* Special case - EOF */ if (r == sizeof(size_t) || tsf->roff > tsf->size) { if (tsf->rfd >= 0) close(tsf->rfd); tsf->rfd = -1; pthread_mutex_lock(&ts->rdwr_mutex); *cur_file = timeshift_filemgr_next(tsf, NULL, 0); pthread_mutex_unlock(&ts->rdwr_mutex); tsf->roff = 0; // reset *wait = 0; /* Check SMT_START index */ } else { streaming_message_t *ssm = _timeshift_find_sstart(*cur_file, (*sm)->sm_time); if (ssm && ssm->sm_data != ts->smt_start) { streaming_target_deliver2(ts->output, streaming_msg_clone(ssm)); if (ts->smt_start) streaming_start_unref(ts->smt_start); ts->smt_start = ssm->sm_data; atomic_add(&ts->smt_start->ss_refcount, 1); } } } return 0; }
/* * Receive data */ static void timeshift_input ( void *opaque, streaming_message_t *sm ) { int exit = 0; timeshift_t *ts = opaque; pthread_mutex_lock(&ts->state_mutex); /* Control */ if (sm->sm_type == SMT_SKIP) { if (ts->state >= TS_LIVE) timeshift_write_skip(ts->rd_pipe.wr, sm->sm_data); } else if (sm->sm_type == SMT_SPEED) { if (ts->state >= TS_LIVE) timeshift_write_speed(ts->rd_pipe.wr, sm->sm_code); } else { /* Start */ if (sm->sm_type == SMT_START && ts->state == TS_INIT) { ts->state = TS_LIVE; } /* Pass-thru */ if (ts->state <= TS_LIVE) { if (sm->sm_type == SMT_START) { if (ts->smt_start) streaming_start_unref(ts->smt_start); ts->smt_start = sm->sm_data; atomic_add(&ts->smt_start->ss_refcount, 1); } streaming_target_deliver2(ts->output, streaming_msg_clone(sm)); } /* Check for exit */ if (sm->sm_type == SMT_EXIT || (sm->sm_type == SMT_STOP && sm->sm_code == 0)) exit = 1; /* Record (one-off) PTS delta */ if (sm->sm_type == SMT_PACKET && ts->pts_delta == PTS_UNSET) { th_pkt_t *pkt = sm->sm_data; if (pkt->pkt_pts != PTS_UNSET) ts->pts_delta = getmonoclock() - ts_rescale(pkt->pkt_pts, 1000000); } /* Buffer to disk */ if ((ts->state > TS_LIVE) || (!ts->ondemand && (ts->state == TS_LIVE))) { sm->sm_time = getmonoclock(); streaming_target_deliver2(&ts->wr_queue.sq_st, sm); } else streaming_msg_free(sm); /* Exit/Stop */ if (exit) { timeshift_write_exit(ts->rd_pipe.wr); ts->state = TS_EXIT; } } pthread_mutex_unlock(&ts->state_mutex); }
/* * Timeshift thread */ void *timeshift_reader ( void *p ) { timeshift_t *ts = p; int nfds, end, run = 1, wait = -1, state; timeshift_seek_t *seek = &ts->seek; timeshift_file_t *tmp_file; int cur_speed = 100, keyframe_mode = 0; int64_t mono_now, mono_play_time = 0, mono_last_status = 0; int64_t deliver, deliver0, pause_time = 0, last_time = 0, skip_time = 0; int64_t i64; streaming_message_t *sm = NULL, *ctrl = NULL; streaming_skip_t *skip = NULL; tvhpoll_t *pd; tvhpoll_event_t ev = { 0 }; pd = tvhpoll_create(1); ev.fd = ts->rd_pipe.rd; ev.events = TVHPOLL_IN; tvhpoll_add(pd, &ev, 1); /* Output */ while (run) { // Note: Previously we allowed unlimited wait, but we now must wake periodically // to output status message if (wait < 0 || wait > 1000) wait = 1000; /* Wait for data */ if(wait) nfds = tvhpoll_wait(pd, &ev, 1, wait); else nfds = 0; wait = -1; end = 0; skip = NULL; mono_now = getfastmonoclock(); /* Control */ pthread_mutex_lock(&ts->state_mutex); if (nfds == 1) { if (_read_msg(NULL, ts->rd_pipe.rd, &ctrl) > 0) { /* Exit */ if (ctrl->sm_type == SMT_EXIT) { tvhtrace(LS_TIMESHIFT, "ts %d read exit request", ts->id); run = 0; streaming_msg_free(ctrl); ctrl = NULL; /* Speed */ } else if (ctrl->sm_type == SMT_SPEED) { int speed = ctrl->sm_code; int keyframe; /* Bound it */ if (speed > 3200) speed = 3200; if (speed < -3200) speed = -3200; /* Ignore negative */ if (!ts->dobuf && (speed < 0)) speed = seek->file ? speed : 0; /* Process */ if (cur_speed != speed) { /* Live playback */ state = ts->state; if (state == TS_LIVE) { /* Reject */ if (speed >= 100) { tvhdebug(LS_TIMESHIFT, "ts %d reject 1x+ in live mode", ts->id); speed = 100; /* Set position */ } else { tvhdebug(LS_TIMESHIFT, "ts %d enter timeshift mode", ts->id); ts->dobuf = 1; _seek_reset(seek); tmp_file = timeshift_filemgr_newest(ts); if (tmp_file != NULL) { i64 = tmp_file->last; timeshift_file_put(tmp_file); } else { i64 = ts->buf_time; } seek->file = timeshift_filemgr_get(ts, i64); if (seek->file != NULL) { seek->file->roff = seek->file->size; pause_time = seek->file->last; last_time = pause_time; } else { pause_time = i64; last_time = pause_time; } } } /* Check keyframe mode */ keyframe = (speed < 0) || (speed > 400); if (keyframe != keyframe_mode) { tvhdebug(LS_TIMESHIFT, "using keyframe mode? %s", keyframe ? "yes" : "no"); keyframe_mode = keyframe; if (keyframe) seek->frame = NULL; } /* Update */ if (speed != 100 || state != TS_LIVE) { ts->state = speed == 0 ? TS_PAUSE : TS_PLAY; tvhtrace(LS_TIMESHIFT, "reader - set %s", speed == 0 ? "TS_PAUSE" : "TS_PLAY"); } if ((ts->state == TS_PLAY && state != TS_PLAY) || (speed != cur_speed)) { mono_play_time = mono_now; tvhtrace(LS_TIMESHIFT, "update play time TS_LIVE - %"PRId64" play buffer from %"PRId64, mono_now, pause_time); if (speed != cur_speed) pause_time = last_time; } else if (ts->state == TS_PAUSE && state != TS_PAUSE) { pause_time = last_time; } cur_speed = speed; tvhdebug(LS_TIMESHIFT, "ts %d change speed %d", ts->id, speed); } /* Send on the message */ ctrl->sm_code = speed; streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; /* Skip/Seek */ } else if (ctrl->sm_type == SMT_SKIP) { skip = ctrl->sm_data; switch (skip->type) { case SMT_SKIP_LIVE: if (ts->state != TS_LIVE) { /* Reset */ if (ts->full) { timeshift_filemgr_flush(ts, NULL); _seek_reset(seek); ts->full = 0; } /* Release */ if (sm) streaming_msg_free(sm); /* Find end */ skip_time = 0x7fffffffffffffffLL; // TODO: change this sometime! } break; case SMT_SKIP_ABS_TIME: /* -fallthrough */ case SMT_SKIP_REL_TIME: /* Convert */ skip_time = ts_rescale(skip->time, 1000000); tvhdebug(LS_TIMESHIFT, "ts %d skip %"PRId64" requested %"PRId64, ts->id, skip_time, skip->time); /* Live playback (stage1) */ if (ts->state == TS_LIVE) { _seek_reset(seek); tmp_file = timeshift_filemgr_newest(ts); if (tmp_file) { i64 = tmp_file->last; timeshift_file_put(tmp_file); } if (tmp_file && (seek->file = timeshift_filemgr_get(ts, i64)) != NULL) { seek->file->roff = seek->file->size; last_time = seek->file->last; } else { last_time = ts->buf_time; } } /* May have failed */ if (skip->type == SMT_SKIP_REL_TIME) skip_time += last_time; tvhdebug(LS_TIMESHIFT, "ts %d skip time %"PRId64, ts->id, skip_time); /* Live (stage2) */ if (ts->state == TS_LIVE) { if (skip_time >= ts->buf_time - TIMESHIFT_PLAY_BUF) { tvhdebug(LS_TIMESHIFT, "ts %d skip ignored, already live", ts->id); skip = NULL; } else { ts->state = TS_PLAY; ts->dobuf = 1; tvhtrace(LS_TIMESHIFT, "reader - set TS_PLAY"); } } /* OK */ if (skip) { /* seek */ seek->frame = NULL; end = _timeshift_do_skip(ts, skip_time, last_time, seek); if (seek->frame) { pause_time = seek->frame->time; tvhtrace(LS_TIMESHIFT, "ts %d skip - play buffer from %"PRId64" last_time %"PRId64, ts->id, pause_time, last_time); /* Adjust time */ if (mono_play_time != mono_now) tvhtrace(LS_TIMESHIFT, "ts %d update play time skip - %"PRId64, ts->id, mono_now); mono_play_time = mono_now; /* Clear existing packet */ if (sm) { streaming_msg_free(sm); sm = NULL; } } else { skip = NULL; } } break; default: tvherror(LS_TIMESHIFT, "ts %d invalid/unsupported skip type: %d", ts->id, skip->type); skip = NULL; break; } /* Error */ if (!skip) { ((streaming_skip_t*)ctrl->sm_data)->type = SMT_SKIP_ERROR; streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; } /* Ignore */ } else { streaming_msg_free(ctrl); ctrl = NULL; } } } /* Done */ if (!run || !seek->file || ((ts->state != TS_PLAY && !skip))) { if (mono_now >= (mono_last_status + sec2mono(1))) { timeshift_status(ts, last_time); mono_last_status = mono_now; } pthread_mutex_unlock(&ts->state_mutex); continue; } /* Calculate delivery time */ deliver0 = (mono_now - mono_play_time) + TIMESHIFT_PLAY_BUF; deliver = (deliver0 * cur_speed) / 100; deliver = (deliver + pause_time); tvhtrace(LS_TIMESHIFT, "speed %d now %"PRId64" play_time %"PRId64" deliver %"PRId64" deliver0 %"PRId64, cur_speed, mono_now, mono_play_time, deliver, deliver0); /* Determine next packet */ if (!sm) { /* Rewind or Fast forward (i-frame only) */ if (skip || keyframe_mode) { int64_t req_time; /* Time */ if (!skip) req_time = last_time + ((cur_speed < 0) ? -1 : 1); else req_time = skip_time; end = _timeshift_do_skip(ts, req_time, last_time, seek); } /* Clear old message */ if (sm) { streaming_msg_free(sm); sm = NULL; } /* Find packet */ if (_timeshift_read(ts, seek, &sm, &wait) == -1) { pthread_mutex_unlock(&ts->state_mutex); break; } } /* Send skip response */ if (skip) { if (sm) { /* Status message */ skip->time = ts_rescale_inv(sm->sm_time, 1000000); skip->type = SMT_SKIP_ABS_TIME; tvhdebug(LS_TIMESHIFT, "ts %d skip to pts %"PRId64" ok", ts->id, sm->sm_time); /* Update timeshift status */ timeshift_fill_status(ts, &skip->timeshift, sm->sm_time); mono_last_status = mono_now; } else { /* Report error */ skip->type = SMT_SKIP_ERROR; skip = NULL; tvhdebug(LS_TIMESHIFT, "ts %d skip failed (%d)", ts->id, sm ? sm->sm_type : -1); } streaming_target_deliver2(ts->output, ctrl); } else { streaming_msg_free(ctrl); } ctrl = NULL; /* Deliver */ if (sm && (skip || (((cur_speed < 0) && (sm->sm_time >= deliver)) || ((cur_speed > 0) && (sm->sm_time <= deliver))))) { last_time = sm->sm_time; if (!skip && keyframe_mode) /* always send status on keyframe mode */ timeshift_status(ts, last_time); timeshift_packet_log("out", ts, sm); streaming_target_deliver2(ts->output, sm); sm = NULL; wait = 0; } else if (sm) { if (cur_speed > 0) wait = (sm->sm_time - deliver) / 1000; else wait = (deliver - sm->sm_time) / 1000; if (wait == 0) wait = 1; tvhtrace(LS_TIMESHIFT, "ts %d wait %d speed %d sm_time %"PRId64" deliver %"PRId64, ts->id, wait, cur_speed, sm->sm_time, deliver); } /* Periodic timeshift status */ if (mono_now >= (mono_last_status + sec2mono(1))) { timeshift_status(ts, last_time); mono_last_status = mono_now; } /* Terminate */ if (!seek->file || end != 0) { /* Back to live (unless buffer is full) */ if ((end == 1 && !ts->full) || !seek->file) { tvhdebug(LS_TIMESHIFT, "ts %d eob revert to live mode", ts->id); cur_speed = 100; ctrl = streaming_msg_create_code(SMT_SPEED, cur_speed); streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; tvhtrace(LS_TIMESHIFT, "reader - set TS_LIVE"); /* Flush timeshift buffer to live */ if (_timeshift_flush_to_live(ts, seek, &wait) == -1) { pthread_mutex_unlock(&ts->state_mutex); break; } ts->state = TS_LIVE; /* Close file (if open) */ _read_close(seek); /* Pause */ } else { if (cur_speed <= 0) { cur_speed = 0; tvhtrace(LS_TIMESHIFT, "reader - set TS_PAUSE"); ts->state = TS_PAUSE; } else { cur_speed = 100; tvhtrace(LS_TIMESHIFT, "reader - set TS_PLAY"); if (ts->state != TS_PLAY) { ts->state = TS_PLAY; ts->dobuf = 1; if (mono_play_time != mono_now) tvhtrace(LS_TIMESHIFT, "update play time (pause) - %"PRId64, mono_now); mono_play_time = mono_now; } } tvhdebug(LS_TIMESHIFT, "ts %d sob speed %d last time %"PRId64, ts->id, cur_speed, last_time); pause_time = last_time; ctrl = streaming_msg_create_code(SMT_SPEED, cur_speed); streaming_target_deliver2(ts->output, ctrl); ctrl = NULL; } } pthread_mutex_unlock(&ts->state_mutex); } /* Cleanup */ tvhpoll_destroy(pd); _read_close(seek); if (sm) streaming_msg_free(sm); if (ctrl) streaming_msg_free(ctrl); tvhtrace(LS_TIMESHIFT, "ts %d exit reader thread", ts->id); return NULL; }
static void normalize_ts(tsfix_t *tf, tfstream_t *tfs, th_pkt_t *pkt) { int64_t ref, dts, d; if(tf->tf_tsref == PTS_UNSET) { pkt_ref_dec(pkt); return; } pkt->pkt_dts &= PTS_MASK; pkt->pkt_pts &= PTS_MASK; /* Subtract the transport wide start offset */ ref = tfs->tfs_local_ref != PTS_UNSET ? tfs->tfs_local_ref : tf->tf_tsref; dts = pkt->pkt_dts - ref; if(tfs->tfs_last_dts_norm == PTS_UNSET) { if(dts < 0) { /* Early packet with negative time stamp, drop those */ pkt_ref_dec(pkt); return; } } else { int64_t low = 90000; /* one second */ int64_t upper = 180000; /* two seconds */ d = dts + tfs->tfs_dts_epoch - tfs->tfs_last_dts_norm; if (SCT_ISSUBTITLE(tfs->tfs_type)) { /* * special conditions for subtitles, because they may be broadcasted * with large time gaps */ low = PTS_MASK / 2; /* more than 13 hours */ upper = low - 1; } if (d < 0 || d > low) { if(d < -PTS_MASK || d > -PTS_MASK + upper) { tfs->tfs_bad_dts++; if(tfs->tfs_bad_dts < 5) { tvhlog(LOG_ERR, "parser", "transport stream %s, DTS discontinuity. " "DTS = %" PRId64 ", last = %" PRId64, streaming_component_type2txt(tfs->tfs_type), dts, tfs->tfs_last_dts_norm); } } else { /* DTS wrapped, increase upper bits */ tfs->tfs_dts_epoch += PTS_MASK + 1; tfs->tfs_bad_dts = 0; } } else { tfs->tfs_bad_dts = 0; } } dts += tfs->tfs_dts_epoch; tfs->tfs_last_dts_norm = dts; if(pkt->pkt_pts != PTS_UNSET) { /* Compute delta between PTS and DTS (and watch out for 33 bit wrap) */ d = (pkt->pkt_pts - pkt->pkt_dts) & PTS_MASK; pkt->pkt_pts = dts + d; } pkt->pkt_dts = dts; tsfixprintf("TSFIX: %-12s %d %10"PRId64" %10"PRId64" %10d %zd\n", streaming_component_type2txt(tfs->tfs_type), pkt->pkt_frametype, pkt->pkt_dts, pkt->pkt_pts, pkt->pkt_duration, pktbuf_len(pkt->pkt_payload)); streaming_message_t *sm = streaming_msg_create_pkt(pkt); streaming_target_deliver2(tf->tf_output, sm); pkt_ref_dec(pkt); }
/* * Receive data */ static void timeshift_input ( void *opaque, streaming_message_t *sm ) { int exit = 0, type = sm->sm_type; timeshift_t *ts = opaque; th_pkt_t *pkt = sm->sm_data, *pkt2; pthread_mutex_lock(&ts->state_mutex); /* Control */ if (type == SMT_SKIP) { if (ts->state >= TS_LIVE) timeshift_write_skip(ts->rd_pipe.wr, sm->sm_data); streaming_msg_free(sm); } else if (type == SMT_SPEED) { if (ts->state >= TS_LIVE) timeshift_write_speed(ts->rd_pipe.wr, sm->sm_code); streaming_msg_free(sm); } else { /* Start */ if (type == SMT_START && ts->state == TS_INIT) ts->state = TS_LIVE; /* Change PTS/DTS offsets */ if (ts->packet_mode && ts->start_pts && type == SMT_PACKET) { pkt2 = pkt_copy_shallow(pkt); pkt_ref_dec(pkt); sm->sm_data = pkt = pkt2; pkt->pkt_pts += ts->start_pts; pkt->pkt_dts += ts->start_pts; } /* Pass-thru */ if (ts->state <= TS_LIVE) { if (type == SMT_START) { if (ts->smt_start) streaming_start_unref(ts->smt_start); ts->smt_start = sm->sm_data; atomic_add(&ts->smt_start->ss_refcount, 1); if (ts->packet_mode) { timeshift_packet_flush(ts, ts->last_time + MAX_TIME_DELTA + 1000, ts->dobuf); if (ts->last_time) ts->start_pts = ts->last_time + 1000; } } streaming_target_deliver2(ts->output, streaming_msg_clone(sm)); } /* Check for exit */ if (type == SMT_EXIT || (type == SMT_STOP && sm->sm_code != SM_CODE_SOURCE_RECONFIGURED)) exit = 1; if (type == SMT_MPEGTS) ts->packet_mode = 0; /* Buffer to disk */ if ((ts->state > TS_LIVE) || (ts->dobuf && (ts->state == TS_LIVE))) { if (ts->packet_mode) { sm->sm_time = ts->last_time; if (type == SMT_PACKET) { timeshift_packet(ts, pkt, 1); goto msg_free; } } else { if (ts->ref_time == 0) { ts->ref_time = getmonoclock(); sm->sm_time = 0; } else { sm->sm_time = getmonoclock() - ts->ref_time; } } streaming_target_deliver2(&ts->wr_queue.sq_st, sm); } else { if (type == SMT_PACKET) { timeshift_packet(ts, pkt, 0); tvhtrace("timeshift", "ts %d pkt in - stream %d type %c pts %10"PRId64 " dts %10"PRId64" dur %10d len %6zu", ts->id, pkt->pkt_componentindex, pkt_frametype_to_char(pkt->pkt_frametype), ts_rescale(pkt->pkt_pts, 1000000), ts_rescale(pkt->pkt_dts, 1000000), pkt->pkt_duration, pktbuf_len(pkt->pkt_payload)); } msg_free: streaming_msg_free(sm); } /* Exit/Stop */ if (exit) { timeshift_write_exit(ts->rd_pipe.wr); ts->state = TS_EXIT; } } pthread_mutex_unlock(&ts->state_mutex); }
static void normalize_ts(tsfix_t *tf, tfstream_t *tfs, th_pkt_t *pkt) { int64_t dts, d; int checkts = SCT_ISAUDIO(tfs->tfs_type) || SCT_ISVIDEO(tfs->tfs_type); if(tf->tf_tsref == PTS_UNSET) { pkt_ref_dec(pkt); return; } pkt->pkt_dts &= PTS_MASK; pkt->pkt_pts &= PTS_MASK; /* Subtract the transport wide start offset */ dts = pkt->pkt_dts - tf->tf_tsref; if(tfs->tfs_last_dts_norm == PTS_UNSET) { if(dts < 0) { /* Early packet with negative time stamp, drop those */ pkt_ref_dec(pkt); return; } } else if(checkts) { d = dts + tfs->tfs_dts_epoch - tfs->tfs_last_dts_norm; if(d < 0 || d > 90000) { if(d < -PTS_MASK || d > -PTS_MASK + 180000) { tfs->tfs_bad_dts++; if(tfs->tfs_bad_dts < 5) { tvhlog(LOG_ERR, "parser", "transport stream %s, DTS discontinuity. " "DTS = %" PRId64 ", last = %" PRId64, streaming_component_type2txt(tfs->tfs_type), dts, tfs->tfs_last_dts_norm); } } else { /* DTS wrapped, increase upper bits */ tfs->tfs_dts_epoch += PTS_MASK + 1; tfs->tfs_bad_dts = 0; } } else { tfs->tfs_bad_dts = 0; } } dts += tfs->tfs_dts_epoch; tfs->tfs_last_dts_norm = dts; if(pkt->pkt_pts != PTS_UNSET) { /* Compute delta between PTS and DTS (and watch out for 33 bit wrap) */ int64_t ptsoff = (pkt->pkt_pts - pkt->pkt_dts) & PTS_MASK; pkt->pkt_pts = dts + ptsoff; } pkt->pkt_dts = dts; tsfixprintf("TSFIX: %-12s %d %10"PRId64" %10"PRId64" %10d %zd\n", streaming_component_type2txt(tfs->tfs_type), pkt->pkt_frametype, pkt->pkt_dts, pkt->pkt_pts, pkt->pkt_duration, pktbuf_len(pkt->pkt_payload)); streaming_message_t *sm = streaming_msg_create_pkt(pkt); streaming_target_deliver2(tf->tf_output, sm); pkt_ref_dec(pkt); }
/* * Receive data */ static void timeshift_input ( void *opaque, streaming_message_t *sm ) { int exit = 0; timeshift_t *ts = opaque; th_pkt_t *pkt = sm->sm_data; pthread_mutex_lock(&ts->state_mutex); /* Control */ if (sm->sm_type == SMT_SKIP) { if (ts->state >= TS_LIVE) timeshift_write_skip(ts->rd_pipe.wr, sm->sm_data); } else if (sm->sm_type == SMT_SPEED) { if (ts->state >= TS_LIVE) timeshift_write_speed(ts->rd_pipe.wr, sm->sm_code); } else { /* Start */ if (sm->sm_type == SMT_START && ts->state == TS_INIT) { ts->state = TS_LIVE; } if (sm->sm_type == SMT_PACKET) { tvhtrace("timeshift", "ts %d pkt in - stream %d type %c pts %10"PRId64 " dts %10"PRId64" dur %10d len %zu", ts->id, pkt->pkt_componentindex, pkt_frametype_to_char(pkt->pkt_frametype), ts_rescale(pkt->pkt_pts, 1000000), ts_rescale(pkt->pkt_dts, 1000000), pkt->pkt_duration, pktbuf_len(pkt->pkt_payload)); } /* Pass-thru */ if (ts->state <= TS_LIVE) { if (sm->sm_type == SMT_START) { if (ts->smt_start) streaming_start_unref(ts->smt_start); ts->smt_start = sm->sm_data; atomic_add(&ts->smt_start->ss_refcount, 1); } streaming_target_deliver2(ts->output, streaming_msg_clone(sm)); } /* Check for exit */ if (sm->sm_type == SMT_EXIT || (sm->sm_type == SMT_STOP && sm->sm_code == 0)) exit = 1; /* Record (one-off) PTS delta */ if (sm->sm_type == SMT_PACKET && ts->pts_delta == PTS_UNSET) timeshift_set_pts_delta(ts, pkt->pkt_pts); /* Buffer to disk */ if ((ts->state > TS_LIVE) || (!ts->ondemand && (ts->state == TS_LIVE))) { sm->sm_time = getmonoclock(); if (sm->sm_type == SMT_PACKET) { tvhtrace("timeshift", "ts %d pkt buf - stream %d type %c pts %10"PRId64 " dts %10"PRId64" dur %10d len %zu", ts->id, pkt->pkt_componentindex, pkt_frametype_to_char(pkt->pkt_frametype), ts_rescale(pkt->pkt_pts, 1000000), ts_rescale(pkt->pkt_dts, 1000000), pkt->pkt_duration, pktbuf_len(pkt->pkt_payload)); } streaming_target_deliver2(&ts->wr_queue.sq_st, sm); } else streaming_msg_free(sm); /* Exit/Stop */ if (exit) { timeshift_write_exit(ts->rd_pipe.wr); ts->state = TS_EXIT; } } pthread_mutex_unlock(&ts->state_mutex); }
static void _process_msg ( timeshift_t *ts, streaming_message_t *sm, int *run ) { int err; timeshift_file_t *tsf; /* Process */ switch (sm->sm_type) { /* Terminate */ case SMT_EXIT: if (run) *run = 0; break; case SMT_STOP: if (sm->sm_code != SM_CODE_SOURCE_RECONFIGURED && run) *run = 0; goto live; /* Timeshifting */ case SMT_SKIP: case SMT_SPEED: break; /* Status */ case SMT_GRACE: case SMT_NOSTART: case SMT_NOSTART_WARN: case SMT_SERVICE_STATUS: case SMT_TIMESHIFT_STATUS: case SMT_DESCRAMBLE_INFO: goto live; /* Store */ case SMT_SIGNAL_STATUS: case SMT_START: case SMT_MPEGTS: case SMT_PACKET: pthread_mutex_lock(&ts->state_mutex); ts->buf_time = sm->sm_time; if (ts->state == TS_LIVE) { streaming_target_deliver2(ts->output, streaming_msg_clone(sm)); if (sm->sm_type == SMT_PACKET) timeshift_packet_log("liv", ts, sm); } if (sm->sm_type == SMT_START) _update_smt_start(ts, (streaming_start_t *)sm->sm_data); if (ts->dobuf) { if ((tsf = timeshift_filemgr_get(ts, sm->sm_time)) != NULL) { if (tsf->wfd >= 0 || tsf->ram) { if ((err = _process_msg0(ts, tsf, sm)) < 0) { timeshift_filemgr_close(tsf); tsf->bad = 1; ts->full = 1; ///< Stop any more writing } else { timeshift_packet_log("sav", ts, sm); } } timeshift_file_put(tsf); } } pthread_mutex_unlock(&ts->state_mutex); break; } /* Next */ streaming_msg_free(sm); return; live: pthread_mutex_lock(&ts->state_mutex); if (ts->state == TS_LIVE) streaming_target_deliver2(ts->output, sm); else streaming_msg_free(sm); pthread_mutex_unlock(&ts->state_mutex); }