diff --git a/omvs_main.c b/omvs_main.c index 3382a82..7e518ca 100644 --- a/omvs_main.c +++ b/omvs_main.c @@ -43,6 +43,9 @@ static gboolean _omvs_wait; static gchar **_omvs_net_dev_names; static gchar **_omvs_net_dev_descs; static gint _omvs_num_net_devs; +static FILE *_omvs_m3u_fp; +static gint _omvs_m3u_idx; +static GMutex _omvs_mutex; static GOptionEntry _omvs_entries[] = { @@ -358,6 +361,21 @@ static gpointer _omvs_start_scan_job(gpointer data) { omvs_gst_close(o_ipaddr->gsts[j]); } g_free(o_ipaddr->gsts); + for (j = 0; j < o_ipaddr->num_ports; j++) { + gchar *filename; + filename = g_strdup_printf("%s/%s-%d.png", _omvs_outdir, addr_str, + o_ipaddr->ports[j]); + if (g_access(filename, F_OK) == 0) { + _omvs_m3u_idx++; + g_mutex_lock(&_omvs_mutex); + fprintf(_omvs_m3u_fp, "#EXTINF:%u,%u\n", _omvs_m3u_idx, _omvs_m3u_idx); + fprintf(_omvs_m3u_fp, "rtp://%s:%u\n", addr_str, o_ipaddr->ports[j]); + fflush(_omvs_m3u_fp); + g_mutex_unlock(&_omvs_mutex); + } + g_free(filename); + } + g_free(o_ipaddr->ports); if (!_omvs_quiet) { g_print("[%p] finish scanning %s\n", (void *)g_thread_self(), addr_str); @@ -426,6 +444,7 @@ static gint _omvs_wait_for_multicast_packet(void) { gint64 time1, time2; OMVSGst gst; gchar *cur_uri; + gchar *cur_filename; pcap = pcap_open_live(_omvs_net_dev_names[_omvs_net_dev_idx], sizeof(OMVSEthHdr) + sizeof(OMVSIPHdr) + sizeof(OMVSUDPHdr), @@ -441,7 +460,8 @@ static gint _omvs_wait_for_multicast_packet(void) { } gst = NULL; - urihashtable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); + urihashtable = + g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); time1 = g_get_monotonic_time(); for (;;) { @@ -472,6 +492,15 @@ static gint _omvs_wait_for_multicast_packet(void) { } omvs_gst_close(gst); gst = NULL; + if (g_access(cur_filename, F_OK) == 0) { + _omvs_m3u_idx++; + g_mutex_lock(&_omvs_mutex); + fprintf(_omvs_m3u_fp, "#EXTINF:%u,%u\n", _omvs_m3u_idx, + _omvs_m3u_idx); + fprintf(_omvs_m3u_fp, "%s\n", cur_uri); + fflush(_omvs_m3u_fp); + g_mutex_unlock(&_omvs_mutex); + } } else if (time2 - time1 > (gint64)((gint64)_omvs_timeout * 1000)) { if (!_omvs_quiet) { g_print("[%p] finish scanning %s\n", (void *)g_thread_self(), @@ -479,6 +508,15 @@ static gint _omvs_wait_for_multicast_packet(void) { } omvs_gst_close(gst); gst = NULL; + if (g_access(cur_filename, F_OK) == 0) { + _omvs_m3u_idx++; + g_mutex_lock(&_omvs_mutex); + fprintf(_omvs_m3u_fp, "#EXTINF:%u,%u\n", _omvs_m3u_idx, + _omvs_m3u_idx); + fprintf(_omvs_m3u_fp, "%s\n", cur_uri); + fflush(_omvs_m3u_fp); + g_mutex_unlock(&_omvs_mutex); + } } else { continue; } @@ -539,20 +577,20 @@ static gint _omvs_wait_for_multicast_packet(void) { g_strdup_printf("%s/%s-%d.png", _omvs_outdir, addr_str, udp_port); g_free(addr_str); - if (!g_hash_table_insert(urihashtable, uri, NULL)) { - g_free(filename); + if (!g_hash_table_insert(urihashtable, uri, filename)) { continue; } cur_uri = uri; + cur_filename = filename; if (!_omvs_quiet) { g_print("[%p] start scanning %s\n", (void *)g_thread_self(), cur_uri); g_print("[%p] trying to save %s to %s\n", (void *)g_thread_self(), - cur_uri, filename); + cur_uri, cur_filename); } time1 = g_get_monotonic_time(); g_assert(gst == NULL); - gst = omvs_gst_open(cur_uri, filename); + gst = omvs_gst_open(cur_uri, cur_filename); } g_hash_table_destroy(urihashtable); @@ -573,6 +611,7 @@ int main(int argc, char *argv[]) { gint num_ipaddrs; gint idx_ipaddr; OMVSScanner omvs_scanner; + gchar *m3u_path; if (_omvs_init_net_devs_info() != 0) { ret = -1; @@ -606,13 +645,23 @@ int main(int argc, char *argv[]) { help_msg = g_option_context_get_help(context, TRUE, NULL); g_print("%s", help_msg); g_free(help_msg); - ret = -2; + ret = -6; goto finish_return; } omvs_gst_plugin_register(); g_mkdir(_omvs_outdir, 0755); + m3u_path = g_strdup_printf("%s/omvs.m3u", _omvs_outdir); + _omvs_m3u_fp = fopen(m3u_path, "a"); + if (_omvs_m3u_fp == NULL) { + g_print("can't create %s", m3u_path); + g_free(m3u_path); + ret = -7; + goto finish_return; + } + g_free(m3u_path); + fprintf(_omvs_m3u_fp, "#EXTM3U\n"); if (_omvs_wait) { _omvs_wait_for_multicast_packet(); @@ -694,6 +743,9 @@ int main(int argc, char *argv[]) { finish_return: g_option_context_free(context); _omvs_deinit_net_devs_info(); + if (_omvs_m3u_fp) { + fclose(_omvs_m3u_fp); + } finish_return2: return ret;