static int do_open(cmyth_proginfo_t prog, struct fuse_file_info *fi, int i) { cmyth_conn_t c = NULL; cmyth_file_t f = NULL; char *host; if ((host=cmyth_proginfo_host(prog)) == NULL) { return -1; } if ((c=cmyth_conn_connect_ctrl(host, port, 16*1024, tcp_control)) == NULL) { return -1; } if ((f=cmyth_conn_connect_file(prog, c, MAX_BSIZE, tcp_program)) == NULL) { return -1; } ref_release(host); ref_release(c); files[i].file = f; files[i].buf = malloc(MAX_BSIZE); files[i].start = 0; files[i].n = 0; fi->fh = i; return 0; }
/* * cmyth_livetv_wait() * * After starting live TV or after a channel change wait here until some * recording data is available. */ static int cmyth_livetv_wait(cmyth_recorder_t rec) { int i = 0, rc = -1; cmyth_conn_t conn; static int failures = 0; usleep(250000*failures); conn = cmyth_conn_reconnect(rec->rec_conn); while (i++ < 10) { int len; cmyth_proginfo_t prog; cmyth_file_t file; if (!cmyth_recorder_is_recording(rec)) { usleep(1000); continue; } prog = cmyth_recorder_get_cur_proginfo(rec); if (prog == NULL) { usleep(1000); continue; } file = cmyth_conn_connect_file(prog, conn, 4096, 4096); ref_release(prog); if (file == NULL) { if (failures < 4) { failures++; } usleep(1000); continue; } len = cmyth_file_request_block(file, 512); ref_release(file); if (len == 512) { rc = 0; break; } if (failures < 4) { failures++; } usleep(1000); } ref_release(conn); return rc; }
int cmyth_chain_switch_to_locked(cmyth_chain_t chain, int index) { int rc = -1; if ((index >= 0) && (index < (int)chain->chain_count)) { cmyth_conn_t conn; cmyth_file_t file; cmyth_proginfo_t prog; char *path, *title; if (chain->chain_list[index]->file != NULL) { chain->chain_current = index; return 0; } prog = chain->chain_list[index]->prog; if (prog == NULL) { return -1; } conn = cmyth_conn_connect_ctrl(prog->proginfo_hostname, prog->proginfo_port, 16*1024, 4096); if (conn == NULL) { return -1; } path = cmyth_proginfo_pathname(prog); title = cmyth_proginfo_title(prog); cmyth_dbg(CMYTH_DBG_DEBUG, "%s(): connect to file %s [%s]\n", __FUNCTION__, path, title); ref_release(path); ref_release(title); file = cmyth_conn_connect_file(prog, conn, 128*1024, 128*1024); if (file) { chain->chain_current = index; chain->chain_list[index]->file = file; } ref_release(conn); rc = 0; } return rc; }
/* * cmyth_livetv_chain_setup(cmyth_recorder_t old_rec) * * Scope: PUBLIC * * Description * * Set up the file information the recorder needs to watch live * tv. The recorder is supplied. This will be duplicated and * released, so the caller can re-use the same variable to hold the * return. The new copy of the recorder will have a livetv chain * within it. * * Return Value: * * Success: A pointer to a new recorder structure with a livetvchain * * Failure: NULL but the recorder passed in is not released the * caller needs to do this on a failure. */ cmyth_recorder_t cmyth_livetv_chain_setup(cmyth_recorder_t rec, int tcp_rcvbuf, void (*prog_update_callback)(cmyth_proginfo_t)) { cmyth_recorder_t new_rec = NULL; char url[1024]; cmyth_conn_t control; cmyth_proginfo_t loc_prog, loc_prog2; cmyth_file_t ft; int i=0; if (!rec) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: no recorder connection\n", __FUNCTION__); return NULL; } control = rec->rec_conn; /* Get the current recording information */ loc_prog = cmyth_recorder_get_cur_proginfo(rec); /* Since backend will pretty much lockup when trying to open an * * empty file, like the dummy file first generated by dvd */ loc_prog2 = (cmyth_proginfo_t)ref_hold(loc_prog); while(i++<5 && (loc_prog2 == NULL || (loc_prog2 && loc_prog2->proginfo_Length == 0))) { usleep(200000); if(loc_prog2) ref_release(loc_prog2); loc_prog2 = cmyth_recorder_get_cur_proginfo(rec); loc_prog2 = cmyth_proginfo_get_detail(control, loc_prog2); } if (loc_prog == NULL) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: could not get current filename\n", __FUNCTION__); goto out; } pthread_mutex_lock(&mutex); sprintf(url, "myth://%s:%d%s",loc_prog->proginfo_hostname, rec->rec_port, loc_prog->proginfo_pathname); new_rec = cmyth_recorder_dup(rec); if (new_rec == NULL) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: cannot create recorder\n", __FUNCTION__); goto out; } ref_release(rec); if(new_rec->rec_livetv_chain == NULL) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: error no livetv_chain\n", __FUNCTION__); new_rec = NULL; goto out; } if(cmyth_livetv_chain_has_url(new_rec, url) == -1) { ft = cmyth_conn_connect_file(loc_prog, new_rec->rec_conn, 16*1024, tcp_rcvbuf); if (!ft) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_conn_connect_file(%s) failed\n", __FUNCTION__, url); new_rec = NULL; goto out; } if(cmyth_livetv_chain_add(new_rec, url, ft, loc_prog) == -1) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_livetv_chain_add(%s) failed\n", __FUNCTION__, url); new_rec = NULL; goto out; } new_rec->rec_livetv_chain->prog_update_callback = prog_update_callback; ref_release(ft); cmyth_livetv_chain_switch(new_rec, 0); } ref_release(loc_prog); out: pthread_mutex_unlock(&mutex); return new_rec; }
/* * cmyth_livetv_chain_update(cmyth_recorder_t rec, char * chainid, int buff) * * Scope: PUBLIC * * Description * * Called in response to the backend's notification of a chain update. * The recorder is supplied and will be queried for the current recording * to determine if a new file needs to be added to the chain of files * in the live tv instance. * * Return Value: * * Success: 0 * * Failure: -1 */ int cmyth_livetv_chain_update(cmyth_recorder_t rec, char * chainid, int tcp_rcvbuf) { int ret=0; char url[1024]; cmyth_conn_t control; cmyth_proginfo_t loc_prog; cmyth_file_t ft; if (!rec) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: rec is NULL\n", __FUNCTION__); goto out; } control = rec->rec_conn; loc_prog = cmyth_recorder_get_cur_proginfo(rec); pthread_mutex_lock(&mutex); if(rec->rec_livetv_chain) { if(strncmp(rec->rec_livetv_chain->chainid, chainid, strlen(chainid)) == 0) { sprintf(url, "myth://%s:%d%s",loc_prog->proginfo_hostname, rec->rec_port, loc_prog->proginfo_pathname); /* Now check if this file is in the recorder chain and if not then open a new file transfer and add it to the chain. */ if(cmyth_livetv_chain_has_url(rec, url) == -1) { ft = cmyth_conn_connect_file(loc_prog, rec->rec_conn, 16*1024, tcp_rcvbuf); if (!ft) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_conn_connect_file(%s) failed\n", __FUNCTION__, url); ret = -1; goto out; } if(cmyth_livetv_chain_add(rec, url, ft, loc_prog) == -1) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_livetv_chain_add(%s) failed\n", __FUNCTION__, url); ret = -1; goto out; } ref_release(ft); if(rec->rec_livetv_chain->chain_switch_on_create) { cmyth_livetv_chain_switch(rec, LAST); rec->rec_livetv_chain->chain_switch_on_create = 0; } } } else { cmyth_dbg(CMYTH_DBG_ERROR, "%s: chainid doesn't match recorder's chainid!!\n", __FUNCTION__, url); ret = -1; } } else { cmyth_dbg(CMYTH_DBG_ERROR, "%s: rec_livetv_chain is NULL!!\n", __FUNCTION__, url); ret = -1; } ref_release(loc_prog); out: pthread_mutex_unlock(&mutex); return ret; }
/* * cmyth_livetv_chain_setup(cmyth_recorder_t old_rec) * * Scope: PUBLIC * * Description * * Set up the file information the recorder needs to watch live * tv. The recorder is supplied. This will be duplicated and * released, so the caller can re-use the same variable to hold the * return. The new copy of the recorder will have a livetv chain * within it. * * Return Value: * * Success: A pointer to a new recorder structure with a livetvchain * * Failure: NULL but the recorder passed in is not released the * caller needs to do this on a failure. */ cmyth_recorder_t cmyth_livetv_chain_setup(cmyth_recorder_t rec, int tcp_rcvbuf, void (*prog_update_callback)(cmyth_proginfo_t)) { cmyth_recorder_t new_rec = NULL; char url[1024]; cmyth_conn_t control; cmyth_proginfo_t loc_prog; cmyth_file_t ft; if (!rec) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: no recorder connection\n", __FUNCTION__); return NULL; } control = rec->rec_conn; /* Get the current recording information */ loc_prog = cmyth_recorder_get_cur_proginfo(rec); if (loc_prog == NULL) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: could not get current filename\n", __FUNCTION__); goto out; } pthread_mutex_lock(&mutex); new_rec = cmyth_recorder_dup(rec); if (new_rec == NULL) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: cannot create recorder\n", __FUNCTION__); goto out; } ref_release(rec); if(new_rec->rec_livetv_chain == NULL) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: error no livetv_chain\n", __FUNCTION__); new_rec = NULL; goto out; } sprintf(url, "myth://%s:%d%s", loc_prog->proginfo_hostname, rec->rec_port, loc_prog->proginfo_pathname); if(cmyth_livetv_chain_has_url(new_rec, url) == -1) { new_rec->rec_livetv_chain->livetv_tcp_rcvbuf = tcp_rcvbuf; ft = cmyth_conn_connect_file(loc_prog, new_rec->rec_conn, 4096, new_rec->rec_livetv_chain->livetv_tcp_rcvbuf); if (!ft) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_conn_connect_file(%s) failed\n", __FUNCTION__, url); new_rec = NULL; goto out; } } if(cmyth_livetv_chain_add(new_rec, url, ft, loc_prog) == -1) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_livetv_chain_add(%s) failed\n", __FUNCTION__, url); new_rec = NULL; ref_release(ft); goto out; } new_rec->rec_livetv_chain->prog_update_callback = prog_update_callback; ref_release(ft); /* JLB: Manage program breaks * Switch ON watch signal */ new_rec->rec_livetv_chain->livetv_watch = 1; cmyth_livetv_chain_switch(new_rec, 0); out: pthread_mutex_unlock(&mutex); ref_release(loc_prog); return new_rec; }
/* * cmyth_livetv_chain_update(cmyth_recorder_t rec, char * chainid, int buff) * * Scope: PUBLIC * * Description * * Called in response to the backend's notification of a chain update. * The recorder is supplied and will be queried for the current recording * to determine if a new file needs to be added to the chain of files * in the live tv instance. * * Return Value: * * Success: 0 * * Failure: -1 */ int cmyth_livetv_chain_update(cmyth_recorder_t rec, char * chainid) { int ret; char url[1024]; cmyth_proginfo_t loc_prog; cmyth_file_t ft; ret = 0; if (!rec) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: rec is NULL\n", __FUNCTION__); return -1; } /* JLB: Manage program break * Skip chain update, it will doing later */ if (!rec->rec_livetv_chain) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: rec_livetv_chain is NULL\n", __FUNCTION__, url); return -1; } else { if (rec->rec_livetv_chain->livetv_watch != 1) { cmyth_dbg(CMYTH_DBG_DEBUG, "%s: skip chain update\n", __FUNCTION__); return 0; } } loc_prog = cmyth_recorder_get_cur_proginfo(rec); if (!loc_prog) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: recorder is not recording\n", __FUNCTION__); return -1; } pthread_mutex_lock(&mutex); if (strncmp(rec->rec_livetv_chain->chainid, chainid, strlen(chainid)) == 0) { sprintf(url, "myth://%s:%d%s", loc_prog->proginfo_hostname, rec->rec_port, loc_prog->proginfo_pathname); /* Now check if this file is in the recorder chain and if not then open a new file transfer and add it to the chain. */ if (cmyth_livetv_chain_has_url(rec, url) == -1) { ft = cmyth_conn_connect_file(loc_prog, rec->rec_conn, 4096, rec->rec_livetv_chain->livetv_tcp_rcvbuf); if (!ft) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_conn_connect_file(%s) failed\n", __FUNCTION__, url); ret = -1; goto out; } if (cmyth_livetv_chain_add(rec, url, ft, loc_prog) == -1) { cmyth_dbg(CMYTH_DBG_ERROR, "%s: cmyth_livetv_chain_add(%s) failed\n", __FUNCTION__, url); ret = -1; ref_release(ft); goto out; } ref_release(ft); if (rec->rec_livetv_chain->chain_switch_on_create) { cmyth_livetv_chain_switch(rec, LAST); rec->rec_livetv_chain->chain_switch_on_create = 0; } } } else { cmyth_dbg(CMYTH_DBG_ERROR, "%s: chainid doesn't match recorder's chainid!!\n", __FUNCTION__, url); ret = -1; } out: pthread_mutex_unlock(&mutex); ref_release(loc_prog); return ret; }