| 1 |
/* |
| 2 |
* Copyright (C) 2010 Igalia S.L. |
| 3 |
* |
| 4 |
* Contact: mswl-dm-2009@igalia.com |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <glib.h> |
| 25 |
#include <gst/gst.h> |
| 26 |
#include <signal.h> |
| 27 |
#include <string.h> |
| 28 |
#include <glib/gi18n.h> |
| 29 |
|
| 30 |
#include "config.h" |
| 31 |
|
| 32 |
#include "jmp-mplayer.h" |
| 33 |
|
| 34 |
static gchar *uri = "/usr/share/sounds/ubuntu/stereo/desktop-login.ogg"; |
| 35 |
static guint change_volume = -1; |
| 36 |
static guint query_playback = -1; |
| 37 |
static guint stop_after = -1; |
| 38 |
static GMainLoop *loop; |
| 39 |
|
| 40 |
static GOptionEntry entries[] = { |
| 41 |
{ "uri", 'u', 0, G_OPTION_ARG_STRING, &uri, "uri", "u" }, |
| 42 |
{ "change-volume-time", 'v', 0, G_OPTION_ARG_INT, &change_volume, |
| 43 |
"Change volume time", "nanosecs" }, |
| 44 |
{ "stop-after", 's', 0, G_OPTION_ARG_INT, &stop_after, |
| 45 |
"Stop after", "nanosecs" }, |
| 46 |
{ NULL } |
| 47 |
}; |
| 48 |
|
| 49 |
JmpMplayer *jmplayer; |
| 50 |
|
| 51 |
static gboolean |
| 52 |
change_volume_callback (gpointer user_data) |
| 53 |
{ |
| 54 |
JmpMplayer *jmplayer = JMP_MPLAYER (user_data); |
| 55 |
gdouble current_volume; |
| 56 |
|
| 57 |
current_volume = jmp_mplayer_get_volume (jmplayer); |
| 58 |
jmp_mplayer_set_volume (jmplayer, 0.5 * current_volume); |
| 59 |
|
| 60 |
return TRUE; |
| 61 |
} |
| 62 |
|
| 63 |
static void |
| 64 |
end_of_stream_callback (JmpMplayer *jmplayer, gpointer user_data) |
| 65 |
{ |
| 66 |
g_message (_("The sound has ended!")); |
| 67 |
g_main_loop_quit(loop); |
| 68 |
} |
| 69 |
|
| 70 |
static void |
| 71 |
error_callback (JmpMplayer *jmplayer, gchar *message, gpointer user_data) |
| 72 |
{ |
| 73 |
g_critical (_("An error has occurred: %s"), message); |
| 74 |
g_main_loop_quit (loop); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
tick_callback (JmpMplayer *jmplayer, gint64 position, gint64 duration, |
| 79 |
gpointer user_data) |
| 80 |
{ |
| 81 |
g_print (_("PLAYBACK: %" G_GINT64_FORMAT "/%" G_GINT64_FORMAT "\n"), |
| 82 |
position, duration); |
| 83 |
} |
| 84 |
|
| 85 |
static gboolean |
| 86 |
stop_after_callback (gpointer user_data) |
| 87 |
{ |
| 88 |
JmpMplayer *jmplayer = JMP_MPLAYER (user_data); |
| 89 |
|
| 90 |
jmp_mplayer_stop (jmplayer); |
| 91 |
g_main_loop_quit (loop); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
play_handler (int signum) |
| 96 |
{ |
| 97 |
switch (signum) { |
| 98 |
case SIGUSR1: |
| 99 |
g_message (_("Caught SIGUSR1 - Play request")); |
| 100 |
jmp_mplayer_play (jmplayer); |
| 101 |
break; |
| 102 |
case SIGUSR2: |
| 103 |
g_message (_("Caught SIGUSR2 - Pause request")); |
| 104 |
jmp_mplayer_pause (jmplayer); |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
play_signal_setup(void) |
| 111 |
{ |
| 112 |
struct sigaction action; |
| 113 |
|
| 114 |
memset (&action, 0, sizeof (action)); |
| 115 |
action.sa_handler = play_handler; |
| 116 |
sigaction (SIGUSR1, &action, NULL); |
| 117 |
sigaction (SIGUSR2, &action, NULL); |
| 118 |
} |
| 119 |
|
| 120 |
int |
| 121 |
main (int argc, char **argv) |
| 122 |
{ |
| 123 |
GError *error = NULL; |
| 124 |
GOptionContext *context; |
| 125 |
GOptionGroup *gst_option_group; |
| 126 |
char *uritmp; |
| 127 |
|
| 128 |
if (!g_thread_supported ()) |
| 129 |
g_thread_init (NULL); |
| 130 |
|
| 131 |
context = g_option_context_new (_(" - Tests jmp-mplayer behavior")); |
| 132 |
gst_option_group = gst_init_get_option_group (); |
| 133 |
g_option_context_add_main_entries (context, entries, NULL); |
| 134 |
g_option_context_add_group (context, gst_option_group); |
| 135 |
if (!g_option_context_parse (context, &argc, &argv, &error)) { |
| 136 |
g_critical (_("option parsing failed: %s"), error->message); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
g_printf (_("JAMP Media Player Test:\n")); |
| 140 |
g_printf (_("Send USR1 signal to play and USR2 to pause (pid: %d)\n"), getpid()); |
| 141 |
g_printf (_("kill -USR1 %d ====== kill -USR2 %d\n"), getpid(), getpid()); |
| 142 |
|
| 143 |
loop = g_main_loop_new (NULL, FALSE); |
| 144 |
|
| 145 |
jmplayer = jmp_mplayer_new (); |
| 146 |
g_signal_connect (jmplayer, "end-of-stream", |
| 147 |
G_CALLBACK (end_of_stream_callback), NULL); |
| 148 |
|
| 149 |
g_signal_connect (jmplayer, "error", |
| 150 |
G_CALLBACK (error_callback), NULL); |
| 151 |
g_signal_connect (jmplayer, "playback-tick", |
| 152 |
G_CALLBACK (tick_callback), NULL); |
| 153 |
|
| 154 |
play_signal_setup(); |
| 155 |
|
| 156 |
if (!g_strrstr (uri, "://")) { |
| 157 |
uritmp = g_filename_to_uri(uri, NULL, &error); |
| 158 |
if (uritmp) { |
| 159 |
uri = uritmp; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
jmp_mplayer_set_uri (jmplayer, uri); |
| 164 |
jmp_mplayer_play (jmplayer); |
| 165 |
|
| 166 |
if (change_volume != -1) { |
| 167 |
g_timeout_add (change_volume, change_volume_callback, |
| 168 |
jmplayer); |
| 169 |
} |
| 170 |
|
| 171 |
if (stop_after != -1) { |
| 172 |
g_timeout_add (stop_after, stop_after_callback, jmplayer); |
| 173 |
} |
| 174 |
|
| 175 |
g_main_loop_run (loop); |
| 176 |
|
| 177 |
g_object_unref (jmplayer); |
| 178 |
g_option_context_free (context); |
| 179 |
g_main_loop_unref (loop); |
| 180 |
|
| 181 |
return 0; |
| 182 |
} |