<commit_before>/*  vim: set sts=2 sw=2 et :
 *
 *  Copyright (C) 2015 Centricular Ltd
 *  Author(s): <NAME> <<EMAIL>>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "lib.h"
#include "utils.h"

#include <glib-unix.h>

static GMainLoop *loop = NULL;

static gboolean
on_app_exit (OneVideoLocalPeer * local)
{
  one_video_local_peer_stop (local);
  g_main_loop_quit (loop);

  /* Remove the source so it's not called again */
  return FALSE;
}

static gboolean
_parse_test_mode_ports (gchar * test_mode_ports, guint * audio_port,
    guint * video_port)
{
  gchar **split;
  gboolean ret = FALSE;

  if (!test_mode_ports) {
    *audio_port = *video_port = 0;
    return TRUE;
  }
  
  split = g_strsplit (test_mode_ports, ",", -1);
  if (g_strv_length (split) != 2) {
    g_printerr ("Invalid format for test_mode_ports: %s\n", test_mode_ports);
    goto out;
  }

  *audio_port = g_ascii_strtoull (split[0], NULL, 10);
  if (*audio_port == 0) {
    g_printerr ("Invalid format for audio port: %s\n", split[0]);
    goto out;
  }

  *video_port = g_ascii_strtoull (split[1], NULL, 10);
  if (*video_port == 0) {
    g_printerr ("Invalid format for video port: %s\n", split[1]);
    goto out;
  }

  ret = TRUE;
out:
  g_strfreev (split);
  return ret;
}

int
main (int   argc,
      char *argv[])
{
  OneVideoLocalPeer *local;
  GOptionContext *optctx;
  GInetAddress *listen_addr = NULL;
  GError *error = NULL;
  guint audio_port, video_port, index = 0;

  guint exit_after = 0;
  gchar *iface_name = NULL;
  gchar *test_mode_ports = NULL;
  gchar *remotes[] = {"127.0.0.1", NULL};
  GOptionEntry entries[] = {
    {"test-mode", 0, 0, G_OPTION_ARG_STRING, &test_mode_ports, "Run in test"
          " mode on localhost with the given ports", "AUDIO_PORT,VIDEO_PORT"},
    {"exit-after", 0, 0, G_OPTION_ARG_INT, &exit_after, "Exit cleanly after N"
          " seconds (default: never exit)", "SECONDS"},
    {"interface", 'i', 0, G_OPTION_ARG_STRING, &iface_name, "Network interface"
          " to listen on (default: any)", "NAME"},
    {"peer", 'p', 0, G_OPTION_ARG_STRING_ARRAY, &remotes, "Peer(s) to connect to"
          ". Specify multiple times to connect to multiple peers.", "PEER"},
    {NULL}
  };

  gst_init (&argc, &argv);
  optctx = g_option_context_new (" - Peer-to-Peer low-latency high-bandwidth "
      "VoIP application");
  g_option_context_add_main_entries (optctx, entries, NULL);
  g_option_context_add_group (optctx, gst_init_get_option_group ());
  if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
    g_printerr ("Error parsing options: %s\n", error->message);
    return -1;
  }
  g_option_context_free (optctx);

  if (!_parse_test_mode_ports (test_mode_ports, &audio_port, &video_port))
    return -1;

  loop = g_main_loop_new (NULL, FALSE);

  if (iface_name != NULL)
    listen_addr = one_video_get_ip_for_interface (iface_name);
  local = one_video_local_peer_new (listen_addr);

  for (index = 0; index < g_strv_length (remotes); index++) {
    OneVideoRemotePeer *remote;<commit_msg>one-video-app: Verify no memory leaks when a remote peer is removed<commit_after>/*  vim: set sts=2 sw=2 et :
 *
 *  Copyright (C) 2015 Centricular Ltd
 *  Author(s): <NAME> <<EMAIL>>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "lib.h"
#include "utils.h"

#include <glib-unix.h>

static GMainLoop *loop = NULL;

static gboolean
kill_remote_peer (OneVideoRemotePeer * remote)
{
  one_video_remote_peer_remove (remote);
  g_main_loop_quit (loop);

  return FALSE;
}

static gboolean
on_app_exit (OneVideoLocalPeer * local)
{
  one_video_local_peer_stop (local);
  g_main_loop_quit (loop);

  /* Remove the source so it's not called again */
  return FALSE;
}

static gboolean
_parse_test_mode_ports (gchar * test_mode_ports, guint * audio_port,
    guint * video_port)
{
  gchar **split;
  gboolean ret = FALSE;

  if (!test_mode_ports) {
    *audio_port = *video_port = 0;
    return TRUE;
  }
  
  split = g_strsplit (test_mode_ports, ",", -1);
  if (g_strv_length (split) != 2) {
    g_printerr ("Invalid format for test_mode_ports: %s\n", test_mode_ports);
    goto out;
  }

  *audio_port = g_ascii_strtoull (split[0], NULL, 10);
  if (*audio_port == 0) {
    g_printerr ("Invalid format for audio port: %s\n", split[0]);
    goto out;
  }

  *video_port = g_ascii_strtoull (split[1], NULL, 10);
  if (*video_port == 0) {
    g_printerr ("Invalid format for video port: %s\n", split[1]);
    goto out;
  }

  ret = TRUE;
out:
  g_strfreev (split);
  return ret;
}

int
main (int   argc,
      char *argv[])
{
  OneVideoLocalPeer *local;
  OneVideoRemotePeer *remote;
  GOptionContext *optctx;
  GInetAddress *listen_addr = NULL;
  GError *error = NULL;
  guint audio_port, video_port, index = 0;

  guint exit_after = 0;
  gchar *iface_name = NULL;
  gchar *test_mode_ports = NULL;
  gchar *remotes[] = {"127.0.0.1", NULL};
  GOptionEntry entries[] = {
    {"test-mode", 0, 0, G_OPTION_ARG_STRING, &test_mode_ports, "Run in test"
          " mode on localhost with the given ports", "AUDIO_PORT,VIDEO_PORT"},
    {"exit-after", 0, 0, G_OPTION_ARG_INT, &exit_after, "Exit cleanly after N"
          " seconds (default: never exit)", "SECONDS"},
    {"interface", 'i', 0, G_OPTION_ARG_STRING, &iface_name, "Network interface"
          " to listen on (default: any)", "NAME"},
    {"peer", 'p', 0, G_OPTION_ARG_STRING_ARRAY, &remotes, "Peer(s) to connect to"
          ". Specify multiple times to connect to multiple peers.", "PEER"},
    {NULL}
  };

  gst_init (&argc, &argv);
  optctx = g_option_context_new (" - Peer-to-Peer low-latency high-bandwidth "
      "VoIP application");
  g_option_context_add_main_entries (optctx, entries, NULL);
  g_option_context_add_group (optctx, gst_init_get_option_group ());
  if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
    g_printerr ("Error parsing options: %s\n", error->message);
    return -1;
  }
  g_option_context_free (optctx);

  if (!_parse_test_mode_ports (test_mode_ports, &audio_port, &video_port))
    return -1;

  loop = g_main_loop_new (NULL, FALSE);

  if (iface_name != NULL)
    listen_addr = one_video_get_ip_for_interface (iface_name);
  local = one_video_local_peer_new (listen_addr);

  for (index = 0; index < g_strv_length (remotes); index++) {
{'fraction_non_alphanumeric': 0.11014126564832893, 'fraction_numerical': 0.008728609165039623, 'mean_word_length': 3.9902578796561605, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4753784', 'n_tokens_mistral': 3025, 'n_tokens_neox': 2867, 'n_words': 946}