Example #1
0
/*
 * cmyth_livetv_chain_request_block(cmyth_recorder_t file, unsigned long len)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Request a file data block of a certain size, and return when the
 * block has been transfered.
 *
 * Return Value:
 *
 * Sucess: number of bytes transfered
 *
 * Failure: an int containing -errno
 */
static int
cmyth_livetv_chain_request_block(cmyth_recorder_t rec, unsigned long len)
{
	int ret, retry;

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s [%s:%d]: (trace) {\n", __FUNCTION__,
				__FILE__, __LINE__);

	if (!rec) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: no connection\n",
			  __FUNCTION__);
		return -EINVAL;
	}

	pthread_mutex_lock(&mutex);

	do {
		retry = 0;
		ret = cmyth_file_request_block(rec->rec_livetv_file, len);
		if (ret == 0) { /* We've gotten to the end, need to progress in the chain */
			/* Switch if there are files left in the chain */
			PRINTF("**SSDEBUG:(cmyth_livetv_request_block): %s\n",
			"reached end of stream must dooSwitcheroo");
			retry = cmyth_livetv_chain_switch(rec, 1);
		}
	}
	while (retry);

	pthread_mutex_unlock(&mutex);

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s [%s:%d]: (trace) }\n",
				__FUNCTION__, __FILE__, __LINE__);

	return ret;
}
Example #2
0
int cmyth_livetv_chain_read(cmyth_recorder_t rec, char *buf, unsigned long len)
{
	int ret, retry;

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s [%s:%d]: (trace) {\n", 
        __FUNCTION__,	__FILE__, __LINE__);

	if (rec == NULL) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: no connection\n",
			  __FUNCTION__);
		return -EINVAL;
	}

	do {
		retry = 0;
		ret = cmyth_file_read(rec->rec_livetv_file, buf, len);	
		if (ret == 0) {
			/* eof, switch to next file */
			PRINTF("**SSDEBUG:(cmyth_livetv_chain_read): %s\n",
			"reached end of stream must dooSwitcheroo");
			retry = cmyth_livetv_chain_switch(rec, 1);
		}
	} while(retry);

	cmyth_dbg(CMYTH_DBG_DEBUG, "%s [%s:%d]: (trace) }\n",
				__FUNCTION__, __FILE__, __LINE__);

	return ret;
}
Example #3
0
/* for calls from other modules where the mutex isn't set */
int
cmyth_livetv_chain_switch_last(cmyth_recorder_t rec)
{
	int dir;

	if (!rec) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: invalid args rec = %p\n",
			  __FUNCTION__, rec);
		return 0;
	}

	if (!rec->rec_conn) {
		cmyth_dbg(CMYTH_DBG_ERROR, "%s: invalid args rec->rec_conn = %p\n",
			  __FUNCTION__, rec->rec_conn);
		return 0;
	}

	if(rec->rec_conn->conn_version < 26)
		return 1;

	pthread_mutex_lock(&mutex);
	dir = rec->rec_livetv_chain->chain_ct
			- rec->rec_livetv_chain->chain_current - 1;
	PRINTF("#@@@@#SSDEBUG: switch file changing adjusted dir: %d\n", dir);
	if(dir != 0) {
		cmyth_livetv_chain_switch(rec, dir);
	}
	else {
		rec->rec_livetv_chain->chain_switch_on_create=1;
	}
	pthread_mutex_unlock(&mutex);
	return 1;
}
Example #4
0
/*
 * cmyth_livetv_chain_seek(cmyth_recorder_t file, long long offset, int whence)
 * 
 * Scope: PUBLIC
 *
 * Description
 *
 * Seek to a new position in the file based on the value of whence:
 *	SEEK_SET
 *		The offset is set to offset bytes.
 *	SEEK_CUR
 *		The offset is set to the current position plus offset bytes.
 *	SEEK_END
 *		The offset is set to the size of the file minus offset bytes.
 *
 * Return Value:
 *
 * Sucess: 0
 *
 * Failure: an int containing -errno
 */
static long long
cmyth_livetv_chain_seek(cmyth_recorder_t rec, long long offset, int whence)
{
	long long ret;
	cmyth_file_t fp;
	int cur, ct;

	if (rec == NULL)
		return -EINVAL;

	ct  = rec->rec_livetv_chain->chain_ct;

	if (whence == SEEK_END) {

		offset -= rec->rec_livetv_file->file_req;
		for (cur = rec->rec_livetv_chain->chain_current; cur < ct; cur++) {
			offset += rec->rec_livetv_chain->chain_files[cur]->file_length;
		}

		cur = rec->rec_livetv_chain->chain_current;
		fp  = rec->rec_livetv_chain->chain_files[cur];
		whence = SEEK_CUR;
	}

	if (whence == SEEK_SET) {

		for (cur = 0; cur < ct; cur++) {
    			fp = rec->rec_livetv_chain->chain_files[cur];
			if (offset < (long long)fp->file_length)
				break;
			offset -= fp->file_length;
		}
	}

	if (whence == SEEK_CUR) {

	if (offset == 0) {
		cur     = rec->rec_livetv_chain->chain_current;
		offset += rec->rec_livetv_chain->chain_files[cur]->file_req;
		for (; cur > 0; cur--) {
			offset += rec->rec_livetv_chain->chain_files[cur-1]->file_length;
		}
		return offset;
	}

	offset += fp->file_req;

	while (offset > (long long)fp->file_length) {
		cur++;
		offset -= fp->file_length;
		if(cur == ct)
			return -1;
		fp = rec->rec_livetv_chain->chain_files[cur];
	}

	while (offset < 0) {
		cur--;
		if(cur < 0)
			return -1;
		fp = rec->rec_livetv_chain->chain_files[cur];
		offset += fp->file_length;
	}

	offset -= fp->file_req;
  }

	pthread_mutex_lock(&mutex);

	ret = cmyth_file_seek(fp, offset, whence);

	PRINTF("** SSDEBUG: new pos %lld after seek command\n", ret);

	cur -= rec->rec_livetv_chain->chain_current;
	if (ret >= 0 && cur) {
		cmyth_livetv_chain_switch(rec, cur);
	}

	pthread_mutex_unlock(&mutex);

	return ret;
}
Example #5
0
/*
 * 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;
}
Example #6
0
/*
 * 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;
}
Example #7
0
/*
 * 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;
}
Example #8
0
/*
 * 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;
}