Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tvhdhomerun: lock tuners when in use
  • Loading branch information
bfennema authored and perexg committed Mar 25, 2015
1 parent 266b28c commit 1ccdcd3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/input/mpegts/tvhdhomerun/tvhdhomerun.c
Expand Up @@ -48,7 +48,7 @@ tvhdhomerun_device_class_get_childs ( idnode_t *in )
}

typedef struct tvhdhomerun_discovery {
TAILQ_ENTRY(tvhdhomerun_discovery) disc_link;
TAILQ_ENTRY(tvhdhomerun_discovery) disc_link;
} tvhdhomerun_discovery_t;

TAILQ_HEAD(tvhdhomerun_discovery_queue, tvhdhomerun_discovery);
Expand Down Expand Up @@ -175,7 +175,7 @@ const idclass_t tvhdhomerun_device_class =
.notify = tvhdhomerun_device_class_override_notify,
.list = tvhdhomerun_device_class_override_enum,
.off = offsetof(tvhdhomerun_device_t, hd_type),
},
},
{}
}
};
Expand Down Expand Up @@ -315,7 +315,7 @@ static void tvhdhomerun_device_create(struct hdhomerun_discover_device_t *dInfo)
hd->hd_info.ip_address = strdup(inet_ntoa(ip_addr));
hd->hd_info.uuid = strdup(uuid.hex);
hd->hd_info.friendlyname = strdup(fName);

if (conf)
feconf = htsmsg_get_map(conf, "frontends");
save = !conf || !feconf;
Expand All @@ -327,7 +327,7 @@ static void tvhdhomerun_device_create(struct hdhomerun_discover_device_t *dInfo)
tvhlog(LOG_ERR, "tvhdhomerun", "Unable to create frontend-device. ( %08x-%d )", dInfo->device_id,j);
}
}


if (save)
tvhdhomerun_device_save(hd);
Expand Down Expand Up @@ -360,7 +360,7 @@ tvhdhomerun_device_discovery( void )
tvhdhomerun_device_create(cDev);
}
}
}
}
}
}

Expand Down Expand Up @@ -403,7 +403,7 @@ tvhdhomerun_device_destroy( tvhdhomerun_device_t *hd )

gtimer_disarm(&hd->hd_destroy_timer);

tvhlog(LOG_INFO, "tvhdhomerun", "Releaseing locks for devices");
tvhlog(LOG_INFO, "tvhdhomerun", "Releasing locks for devices");
while ((lfe = TAILQ_FIRST(&hd->hd_frontends)) != NULL) {
tvhdhomerun_frontend_delete(lfe);
}
Expand Down
39 changes: 24 additions & 15 deletions src/input/mpegts/tvhdhomerun/tvhdhomerun_frontend.c
Expand Up @@ -75,7 +75,7 @@ tvhdhomerun_frontend_input_thread ( void *aux )
tvhpoll_event_t ev[2];
tvhpoll_t *efd;

tvhdebug("tvhdhomerun", "starting input thread");
tvhdebug("tvhdhomerun", "starting input thread");

/* Get MMI */
pthread_mutex_lock(&hfe->hf_input_thread_mutex);
Expand All @@ -85,7 +85,7 @@ tvhdhomerun_frontend_input_thread ( void *aux )
pthread_mutex_unlock(&hfe->hf_input_thread_mutex);
if (mmi == NULL) return NULL;

tvhdebug("tvhdhomerun", "opening client socket");
tvhdebug("tvhdhomerun", "opening client socket");

/* One would like to use libhdhomerun for the streaming details,
* but that library uses threads on its own and the socket is put
Expand Down Expand Up @@ -157,7 +157,7 @@ tvhdhomerun_frontend_input_thread ( void *aux )
(unsigned int)(local_ip >> 8) & 0xFF,
(unsigned int)(local_ip >> 0) & 0xFF,
ntohs(sock_addr.sin_port));
tvhdebug("tvhdhomerun", "setting target to: %s", target);
tvhdebug("tvhdhomerun", "setting target to: %s", target);
pthread_mutex_lock(&hfe->hf_hdhomerun_device_mutex);
r = hdhomerun_device_set_tuner_target(hfe->hf_hdhomerun_tuner, target);
pthread_mutex_unlock(&hfe->hf_hdhomerun_device_mutex);
Expand Down Expand Up @@ -269,7 +269,7 @@ tvhdhomerun_frontend_monitor_cb( void *aux )
hfe->hf_status = SIGNAL_GOOD;
else
hfe->hf_status = SIGNAL_NONE;

/* Get current mux */
mm = mmi->mmi_mux;

Expand Down Expand Up @@ -372,6 +372,7 @@ static int tvhdhomerun_frontend_tune(tvhdhomerun_frontend_t *hfe, mpegts_mux_ins
char channel_buf[64];
uint32_t symbol_rate = 0;
int res;
char *perror;

/* resolve the modulation type */
switch (dmc->dmc_fe_type) {
Expand All @@ -396,15 +397,21 @@ static int tvhdhomerun_frontend_tune(tvhdhomerun_frontend_t *hfe, mpegts_mux_ins
}

tvhlog(LOG_INFO, "tvhdhomerun", "tuning to %s", channel_buf);

pthread_mutex_lock(&hfe->hf_hdhomerun_device_mutex);
res = hdhomerun_device_tuner_lockkey_request(hfe->hf_hdhomerun_tuner, &perror);
if(res < 1) {
pthread_mutex_unlock(&hfe->hf_hdhomerun_device_mutex);
tvhlog(LOG_ERR, "tvhdhomerun", "failed to acquire lockkey: %s", perror);
return SM_CODE_TUNING_FAILED;
}
res = hdhomerun_device_set_tuner_channel(hfe->hf_hdhomerun_tuner, channel_buf);
pthread_mutex_unlock(&hfe->hf_hdhomerun_device_mutex);
if(res < 1) {
tvhlog(LOG_ERR, "tvhdhomerun", "failed to tune to %s", channel_buf);
return SM_CODE_TUNING_FAILED;
tvhlog(LOG_ERR, "tvhdhomerun", "failed to tune to %s", channel_buf);
return SM_CODE_TUNING_FAILED;
}

hfe->hf_status = SIGNAL_NONE;

/* start the monitoring */
Expand All @@ -421,7 +428,7 @@ tvhdhomerun_frontend_start_mux
tvhdhomerun_frontend_t *hfe = (tvhdhomerun_frontend_t*)mi;
int res, r;
char buf1[256], buf2[256];

mi->mi_display_name(mi, buf1, sizeof(buf1));
mpegts_mux_nice_name(mmi->mmi_mux, buf2, sizeof(buf2));
tvhdebug("tvhdhomerun", "%s - starting %s", buf1, buf2);
Expand All @@ -445,7 +452,7 @@ tvhdhomerun_frontend_stop_mux
{
tvhdhomerun_frontend_t *hfe = (tvhdhomerun_frontend_t*)mi;
char buf1[256], buf2[256];

mi->mi_display_name(mi, buf1, sizeof(buf1));
mpegts_mux_nice_name(mmi->mmi_mux, buf2, sizeof(buf2));
tvhdebug("tvhdhomerun", "%s - stopping %s", buf1, buf2);
Expand All @@ -459,6 +466,8 @@ tvhdhomerun_frontend_stop_mux
tvhtrace("tvhdhomerun", "%s - input thread stopped", buf1);
}

hdhomerun_device_tuner_lockkey_release(hfe->hf_hdhomerun_tuner);

hfe->hf_locked = 0;
hfe->hf_status = 0;
hfe->hf_ready = 0;
Expand Down Expand Up @@ -505,7 +514,7 @@ tvhdhomerun_frontend_network_list ( mpegts_input_t *mi )
else if (hfe->hf_type == DVB_TYPE_C)
idc = &dvb_network_dvbc_class;
else if (hfe->hf_type == DVB_TYPE_ATSC)
idc = &dvb_network_atsc_class;
idc = &dvb_network_atsc_class;
else
return NULL;

Expand Down Expand Up @@ -591,7 +600,7 @@ tvhdhomerun_frontend_delete ( tvhdhomerun_frontend_t *hfe )

gtimer_disarm(&hfe->hf_monitor_timer);

// hdhomerun_device_tuner_lockkey_release(hfe->hf_hdhomerun_tuner);
hdhomerun_device_tuner_lockkey_release(hfe->hf_hdhomerun_tuner);
hdhomerun_device_destroy(hfe->hf_hdhomerun_tuner);

/* Ensure we're stopped */
Expand All @@ -602,12 +611,12 @@ tvhdhomerun_frontend_delete ( tvhdhomerun_frontend_t *hfe )

pthread_mutex_destroy(&hfe->hf_input_thread_mutex);
pthread_mutex_destroy(&hfe->hf_hdhomerun_device_mutex);

/* Finish */
mpegts_input_delete((mpegts_input_t*)hfe, 0);
}

tvhdhomerun_frontend_t *
tvhdhomerun_frontend_t *
tvhdhomerun_frontend_create(tvhdhomerun_device_t *hd, struct hdhomerun_discover_device_t *discover_info, htsmsg_t *conf, dvb_fe_type_t type, unsigned int frontend_number )
{
const idclass_t *idc;
Expand Down Expand Up @@ -658,7 +667,7 @@ tvhdhomerun_frontend_create(tvhdhomerun_device_t *hd, struct hdhomerun_discover_
if (!hfe->mi_name ||
(strncmp(hfe->mi_name, "HDHomeRun ", 7) == 0 &&
strstr(hfe->mi_name, " Tuner ") &&
strstr(hfe->mi_name, " #"))) {
strstr(hfe->mi_name, " #"))) {
char lname[256];
snprintf(lname, sizeof(lname), "HDHomeRun %s Tuner #%i (%s)",
dvb_type2str(type), hfe->hf_tunerNumber, hd->hd_info.ip_address);
Expand Down
14 changes: 7 additions & 7 deletions src/input/mpegts/tvhdhomerun/tvhdhomerun_private.h
Expand Up @@ -51,7 +51,7 @@ struct tvhdhomerun_device
* Adapter info
*/
tvhdhomerun_device_info_t hd_info;

/*
* Frontends
*/
Expand All @@ -61,14 +61,14 @@ struct tvhdhomerun_device
Flags...
*/
int hd_fullmux_ok;

int hd_pids_max;
int hd_pids_len;
int hd_pids_deladd;

dvb_fe_type_t hd_type;
char *hd_override_type;

};

#define HDHOMERUN_MAX_PIDS 32
Expand All @@ -88,7 +88,7 @@ struct tvhdhomerun_frontend
* Frontend info
*/
int hf_tunerNumber;
dvb_fe_type_t hf_type;
dvb_fe_type_t hf_type;

// libhdhomerun objects.
struct hdhomerun_device_t *hf_hdhomerun_tuner;
Expand Down Expand Up @@ -117,19 +117,19 @@ struct tvhdhomerun_frontend
gtimer_t hf_monitor_timer;

mpegts_mux_instance_t *hf_mmi;

};

/*
* Methods
*/

void tvhdhomerun_device_init ( void );
void tvhdhomerun_device_done ( void );
void tvhdhomerun_device_destroy ( tvhdhomerun_device_t *sd );
void tvhdhomerun_device_destroy_later( tvhdhomerun_device_t *sd, int after_ms );

tvhdhomerun_frontend_t *
tvhdhomerun_frontend_t *
tvhdhomerun_frontend_create( tvhdhomerun_device_t *hd, struct hdhomerun_discover_device_t *discover_info, htsmsg_t *conf, dvb_fe_type_t type, unsigned int frontend_number );

void tvhdhomerun_frontend_delete ( tvhdhomerun_frontend_t *lfe );
Expand Down

0 comments on commit 1ccdcd3

Please sign in to comment.