Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
DVR: another try to fix dvr_thread_epilog() locking, proper thread sh…
…utdown
  • Loading branch information
perexg committed Oct 1, 2015
1 parent c05a478 commit 7d2ffc7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/dvr/dvr.h
Expand Up @@ -213,6 +213,7 @@ typedef struct dvr_entry {
* Fields for recording
*/
pthread_t de_thread;
uint8_t de_thread_shutdown;

th_subscription_t *de_s;

Expand Down
36 changes: 21 additions & 15 deletions src/dvr/dvr_rec.c
Expand Up @@ -53,7 +53,7 @@
*
*/
static void *dvr_thread(void *aux);
static void dvr_thread_epilog(dvr_entry_t *de);
static void dvr_thread_epilog(dvr_entry_t *de, const char *dvr_postproc);


const static int prio2weight[6] = {
Expand Down Expand Up @@ -148,6 +148,7 @@ dvr_rec_subscribe(dvr_entry_t *de)

de->de_chain = prch;

de->de_thread_shutdown = 0;
tvhthread_create(&de->de_thread, NULL, dvr_thread, de);
return 0;
}
Expand All @@ -159,12 +160,17 @@ void
dvr_rec_unsubscribe(dvr_entry_t *de)
{
profile_chain_t *prch = de->de_chain;
streaming_queue_t *sq = &prch->prch_sq;

assert(de->de_s != NULL);
assert(prch != NULL);

streaming_target_deliver(prch->prch_st, streaming_msg_create(SMT_EXIT));

pthread_mutex_lock(&sq->sq_mutex);
de->de_thread_shutdown = 1;
pthread_mutex_unlock(&sq->sq_mutex);

pthread_join(de->de_thread, NULL);

subscription_unsubscribe(de->de_s, 0);
Expand Down Expand Up @@ -938,9 +944,11 @@ dvr_thread(void *aux)
th_pkt_t *pkt;
int run = 1, started = 0, comm_skip;
int commercial = COMMERCIAL_UNKNOWN;
char *postproc;

pthread_mutex_lock(&global_lock);
comm_skip = de->de_config->dvr_skip_commercials;
postproc = de->de_config->dvr_postproc ? strdup(de->de_config->dvr_postproc) : NULL;
pthread_mutex_unlock(&global_lock);

pthread_mutex_lock(&sq->sq_mutex);
Expand Down Expand Up @@ -973,6 +981,9 @@ dvr_thread(void *aux)
}

streaming_queue_remove(sq, sm);
/* we don't want to start new recordings at this point */
if (sm->sm_type == SMT_START && de->de_thread_shutdown)
break;
pthread_mutex_unlock(&sq->sq_mutex);

switch(sm->sm_type) {
Expand Down Expand Up @@ -1017,12 +1028,12 @@ dvr_thread(void *aux)

// Try to restart the recording if the muxer doesn't
// support reconfiguration of the streams.
dvr_thread_epilog(de);
dvr_thread_epilog(de, postproc);
started = 0;
pthread_mutex_lock(&global_lock);
if (de->de_config->dvr_clone)
de = dvr_entry_clone(de);
pthread_mutex_unlock(&global_lock);
pthread_mutex_unlock(&global_lock);
}

if(!started) {
Expand All @@ -1048,7 +1059,7 @@ dvr_thread(void *aux)
"dvr", "Recording completed: \"%s\"",
dvr_get_filename(de) ?: lang_str_get(de->de_title, NULL));

dvr_thread_epilog(de);
dvr_thread_epilog(de, postproc);
started = 0;

}else if(de->de_last_error != sm->sm_code) {
Expand All @@ -1060,7 +1071,7 @@ dvr_thread(void *aux)
dvr_get_filename(de) ?: lang_str_get(de->de_title, NULL),
streaming_code2txt(sm->sm_code));

dvr_thread_epilog(de);
dvr_thread_epilog(de, postproc);
started = 0;
}
break;
Expand Down Expand Up @@ -1119,8 +1130,9 @@ dvr_thread(void *aux)
pthread_mutex_unlock(&sq->sq_mutex);

if(prch->prch_muxer)
dvr_thread_epilog(de);
dvr_thread_epilog(de, postproc);

free(postproc);
return NULL;
}

Expand Down Expand Up @@ -1151,31 +1163,25 @@ dvr_spawn_postproc(dvr_entry_t *de, const char *dvr_postproc)
return;
}

pthread_mutex_unlock(&global_lock);
spawnv(args[0], (void *)args, NULL, 1, 1);

htsstr_argsplit_free(args);
pthread_mutex_lock(&global_lock);
}

/**
*
*/
static void
dvr_thread_epilog(dvr_entry_t *de)
dvr_thread_epilog(dvr_entry_t *de, const char *dvr_postproc)
{
profile_chain_t *prch = de->de_chain;
dvr_config_t *cfg;

muxer_close(prch->prch_muxer);
muxer_destroy(prch->prch_muxer);
prch->prch_muxer = NULL;

pthread_mutex_lock(&global_lock);
cfg = de->de_config;
if(cfg && cfg->dvr_postproc)
dvr_spawn_postproc(de, cfg->dvr_postproc);
pthread_mutex_unlock(&global_lock);
if(dvr_postproc)
dvr_spawn_postproc(de, dvr_postproc);
}

/**
Expand Down

0 comments on commit 7d2ffc7

Please sign in to comment.