export SwfdecPlayer structure
[swfdec.git] / libswfdec-gtk / swfdec_gtk_player.c
blobc494a716751292a3092c9006fdf4f57b7d01f4cb
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "libswfdec-gtk/swfdec_gtk_loader.h"
25 #include "libswfdec-gtk/swfdec_gtk_player.h"
26 #include "libswfdec-gtk/swfdec_playback.h"
27 #include "libswfdec-gtk/swfdec_source.h"
29 struct _SwfdecGtkPlayer
31 SwfdecPlayer player;
33 GSource * source; /* source if playing, NULL otherwise */
34 SwfdecPlayback * playback; /* audio playback object */
35 gboolean audio_enabled; /* TRUE if audio should be played */
36 double speed; /* desired playback speed */
39 struct _SwfdecGtkPlayerClass
41 SwfdecPlayerClass player_class;
44 enum {
45 PROP_0,
46 PROP_PLAYING,
47 PROP_AUDIO,
48 PROP_SPEED
51 /*** gtk-doc ***/
53 /**
54 * SECTION:SwfdecGtkPlayer
55 * @title: SwfdecGtkPlayer
56 * @short_description: an improved #SwfdecPlayer
58 * The #SwfdecGtkPlayer adds common functionality to the rather barebones
59 * #SwfdecPlayer class, such as automatic playback and audio handling. Note
60 * that by default, the player will be paused, so you need to call
61 * swfdec_gtk_player_set_playing () on the new player.
63 * @see_also: SwfdecPlayer
66 /**
67 * SwfdecGtkPlayer:
69 * The structure for the Swfdec Gtk player contains no public fields.
72 /*** SWFDEC_GTK_PLAYER ***/
74 G_DEFINE_TYPE (SwfdecGtkPlayer, swfdec_gtk_player, SWFDEC_TYPE_PLAYER)
76 static void
77 swfdec_gtk_player_get_property (GObject *object, guint param_id, GValue *value,
78 GParamSpec * pspec)
80 SwfdecGtkPlayer *player = SWFDEC_GTK_PLAYER (object);
82 switch (param_id) {
83 case PROP_PLAYING:
84 g_value_set_boolean (value, player->source != NULL);
85 break;
86 case PROP_AUDIO:
87 g_value_set_boolean (value, player->audio_enabled);
88 break;
89 case PROP_SPEED:
90 g_value_set_double (value, player->speed);
91 break;
92 default:
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
94 break;
98 static void
99 swfdec_gtk_player_set_property (GObject *object, guint param_id, const GValue *value,
100 GParamSpec *pspec)
102 SwfdecGtkPlayer *player = SWFDEC_GTK_PLAYER (object);
104 switch (param_id) {
105 case PROP_PLAYING:
106 swfdec_gtk_player_set_playing (player, g_value_get_boolean (value));
107 break;
108 case PROP_AUDIO:
109 swfdec_gtk_player_set_audio_enabled (player, g_value_get_boolean (value));
110 break;
111 case PROP_SPEED:
112 swfdec_gtk_player_set_speed (player, g_value_get_double (value));
113 break;
114 default:
115 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
116 break;
120 static void
121 swfdec_gtk_player_dispose (GObject *object)
123 SwfdecGtkPlayer *player = SWFDEC_GTK_PLAYER (object);
125 swfdec_gtk_player_set_playing (player, FALSE);
126 g_assert (player->playback == NULL);
128 G_OBJECT_CLASS (swfdec_gtk_player_parent_class)->dispose (object);
131 static void
132 swfdec_gtk_player_class_init (SwfdecGtkPlayerClass * g_class)
134 GObjectClass *object_class = G_OBJECT_CLASS (g_class);
136 object_class->dispose = swfdec_gtk_player_dispose;
137 object_class->get_property = swfdec_gtk_player_get_property;
138 object_class->set_property = swfdec_gtk_player_set_property;
140 g_object_class_install_property (object_class, PROP_PLAYING,
141 g_param_spec_boolean ("playing", "playing", "TRUE if the player is playing (d'oh)",
142 FALSE, G_PARAM_READWRITE));
143 g_object_class_install_property (object_class, PROP_AUDIO,
144 g_param_spec_boolean ("audio-enabled", "audio enabled", "TRUE if automatic audio handling is enabled",
145 TRUE, G_PARAM_READWRITE));
146 g_object_class_install_property (object_class, PROP_SPEED,
147 g_param_spec_double ("speed", "speed", "desired playback speed",
148 G_MINDOUBLE, G_MAXDOUBLE, 1.0, G_PARAM_READWRITE));
151 static void
152 swfdec_gtk_player_init (SwfdecGtkPlayer * player)
154 player->speed = 1.0;
155 player->audio_enabled = TRUE;
158 /*** PUBLIC API ***/
161 * swfdec_gtk_player_new:
162 * @debugger: %NULL or a #SwfdecAsDebugger to debug this player
164 * Creates a new Swfdec Gtk player.
165 * This function calls swfdec_init () for you if it wasn't called before.
167 * Returns: The new player
169 SwfdecPlayer *
170 swfdec_gtk_player_new (SwfdecAsDebugger *debugger)
172 SwfdecPlayer *player;
174 swfdec_init ();
175 player = g_object_new (SWFDEC_TYPE_GTK_PLAYER, "debugger", debugger, NULL);
177 return player;
181 * swfdec_gtk_player_new_from_uri:
182 * @uri: URI to play
184 * Create a player to play back the given URI. If compiled with gnome-vfs
185 * support, it will use gnome-vfs to resolve the given URI. Note that there
186 * is no way to figure out if the referenced URI really references a file that
187 * Swfdec can play back. If you need this, you should use #SwfdecGtkLoader
188 * directly and use its error property.
189 * This function calls swfdec_init () for you if it wasn't called before.
191 * Returns: a new player.
193 SwfdecPlayer *
194 swfdec_gtk_player_new_from_uri (const char *uri)
196 SwfdecLoader *loader;
197 SwfdecPlayer *player;
199 g_return_val_if_fail (uri != NULL, NULL);
201 player = swfdec_gtk_player_new (NULL);
202 loader = swfdec_gtk_loader_new (uri);
203 swfdec_player_set_loader (player, loader);
205 return player;
208 static void
209 swfdec_gtk_player_update_audio (SwfdecGtkPlayer *player)
211 gboolean should_play = player->audio_enabled;
213 should_play &= (player->source != NULL);
214 should_play &= (player->speed == 1.0);
216 if (should_play && player->playback == NULL) {
217 player->playback = swfdec_playback_open (SWFDEC_PLAYER (player),
218 g_main_context_default ());
219 } else if (!should_play && player->playback != NULL) {
220 swfdec_playback_close (player->playback);
221 player->playback = NULL;
226 * swfdec_gtk_player_set_playing:
227 * @player: a #SwfdecGtkPlayer
228 * @playing: if the player should play or not
230 * Sets if @player should be playing or not. If the player is playing, a timer
231 * will be attached to the default main loop that periodically calls
232 * swfdec_player_advance().
234 void
235 swfdec_gtk_player_set_playing (SwfdecGtkPlayer *player, gboolean playing)
237 g_return_if_fail (SWFDEC_IS_GTK_PLAYER (player));
239 if (playing && player->source == NULL) {
240 player->source = swfdec_iterate_source_new (SWFDEC_PLAYER (player), player->speed);
241 g_source_attach (player->source, NULL);
242 } else if (!playing && player->source != NULL) {
243 g_source_destroy (player->source);
244 g_source_unref (player->source);
245 player->source = NULL;
247 swfdec_gtk_player_update_audio (player);
248 g_object_notify (G_OBJECT (player), "playing");
252 * swfdec_gtk_player_get_playing:
253 * @player: a #SwfdecGtkPlayer
255 * Queries if the player is playing.
257 * Returns: %TRUE if the player is playing
259 gboolean
260 swfdec_gtk_player_get_playing (SwfdecGtkPlayer *player)
262 g_return_val_if_fail (SWFDEC_IS_GTK_PLAYER (player), FALSE);
264 return player->source != NULL;
268 * swfdec_gtk_player_set_audio_enabled:
269 * @player: a #SwfdecGtkPlayer
270 * @enabled: %TRUE to enable audio
272 * Enables or disables automatic audio playback.
274 void
275 swfdec_gtk_player_set_audio_enabled (SwfdecGtkPlayer *player, gboolean enabled)
277 g_return_if_fail (SWFDEC_IS_GTK_PLAYER (player));
279 if (player->audio_enabled == enabled)
280 return;
281 player->audio_enabled = enabled;
282 swfdec_gtk_player_update_audio (player);
283 g_object_notify (G_OBJECT (player), "audio-enabled");
287 * swfdec_gtk_player_get_audio_enabled:
288 * @player: a #SwfdecGtkPlayer
290 * Queries if audio playback for @player is enabled or not.
292 * Returns: %TRUE if audio playback is enabled
294 gboolean
295 swfdec_gtk_player_get_audio_enabled (SwfdecGtkPlayer *player)
297 g_return_val_if_fail (SWFDEC_IS_GTK_PLAYER (player), FALSE);
299 return player->audio_enabled;
303 * swfdec_gtk_player_set_speed:
304 * @player: a #SwfdecGtkPlayer
305 * @speed: the new playback speed
307 * Sets the new playback speed. 1.0 means the default speed, bigger values
308 * make playback happen faster. Audio will only play back if the speed is
309 * 1.0. Note that if the player is not playing when calling this function,
310 * it will not automatically start.
312 void
313 swfdec_gtk_player_set_speed (SwfdecGtkPlayer *player, double speed)
315 g_return_if_fail (SWFDEC_IS_GTK_PLAYER (player));
316 g_return_if_fail (speed > 0.0);
318 player->speed = speed;
319 swfdec_gtk_player_update_audio (player);
320 if (player->source)
321 swfdec_iterate_source_set_speed (player->source, player->speed);
322 g_object_notify (G_OBJECT (player), "speed");
326 * swfdec_gtk_player_get_speed:
327 * @player: a #SwfdecGtkPlayer
329 * Queries the current playback speed. If the player is currently paused, it
330 * will report the speed that it would use if playing.
332 * Returns: the current playback speed.
334 double
335 swfdec_gtk_player_get_speed (SwfdecGtkPlayer *player)
337 g_return_val_if_fail (SWFDEC_IS_GTK_PLAYER (player), FALSE);
339 return player->speed;