File: player.c

package info (click to toggle)
alarm-clock-applet 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,720 kB
  • sloc: ansic: 5,121; sh: 1,382; makefile: 189
file content (277 lines) | stat: -rw-r--r-- 6,985 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * player.c - Simple media player based on GStreamer
 * 
 * Copyright (C) 2007-2008 Johannes H. Jensen <joh@pseudoberries.com>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Authors:
 * 		Johannes H. Jensen <joh@pseudoberries.com>
 */

#include <gst/gst.h>

#include "player.h"

/**
 * Create a new media player.
 * 
 * @uri				The file to play.
 * @loop			Wether to loop or not.
 * @state_callback	An optional #MediaPlayerStateChangeCallback which will be
 * 					notified when the state of the player changes.
 * @data			Data for the state_callback
 * @error_handler	An optional #MediaPlayerErrorHandler which will be notified
 * 					if an error occurs.
 * @error_data		Data for the error_handler.
 */
MediaPlayer *
media_player_new (const gchar *uri, gboolean loop,
				  MediaPlayerStateChangeCallback state_callback, gpointer data,
				  MediaPlayerErrorHandler error_handler, gpointer error_data)
{
	MediaPlayer *player;
	GstElement *audiosink, *videosink;
	
	// Initialize struct
	player = g_new (MediaPlayer, 1);
	
	player->loop	 = loop;
	player->state	 = MEDIA_PLAYER_STOPPED;
	player->watch_id = 0;
	
	player->state_changed 		= state_callback;
	player->state_changed_data	= data;
	player->error_handler		= error_handler;
	player->error_handler_data	= error_data;
	
	// Initialize GStreamer
	gst_init (NULL, NULL);
	
	/* Set up player */
	player->player	= gst_element_factory_make ("playbin", "player");
	audiosink 		= gst_element_factory_make ("autoaudiosink", "player-audiosink");
	videosink 		= gst_element_factory_make ("autovideosink", "player-videosink");
	
	if (!player->player || !audiosink || !videosink) {
		g_critical ("Could not create player.");
		return NULL;
	}
	
	// Set uri and sinks
	g_object_set (player->player,
				  "uri", uri,
				  "audio-sink", audiosink,
				  "video-sink", videosink,
				  NULL);
	
	return player;
}

/**
 * Free a media player.
 */
void
media_player_free (MediaPlayer *player)
{
	g_assert(player);

	if (player->player)
		gst_object_unref (GST_OBJECT (player->player));
	
	g_free (player);
}

/**
 * Set the uri of player.
 */
void
media_player_set_uri (MediaPlayer *player, const gchar *uri)
{
	g_assert(player);

	g_object_set (player->player, "uri", uri, NULL);
}

/**
 * Get the uri of player.
 * 
 * Free with g_free()
 */
gchar *
media_player_get_uri (MediaPlayer *player)
{
	gchar *uri;

	g_assert(player);
	
	g_object_get (player->player, "uri", &uri, NULL);
	
	return uri;
}

/**
 * Set media player state.
 */
void
media_player_set_state (MediaPlayer *player, MediaPlayerState state)
{
	g_assert(player);

	MediaPlayerState old = player->state;
	
	player->state = state;
	
	// Notify state change handler
	if (old != state && player->state_changed)
		player->state_changed(player, player->state, player->state_changed_data);
}


/**
 * Check for errors & call error handler
 */
static gboolean
media_player_bus_check_errors (MediaPlayer *player, GstMessage *message)
{
//	g_debug ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));
	
	switch (GST_MESSAGE_TYPE (message)) {
	case GST_MESSAGE_ERROR: {
		GError *err;
		gchar *debug;
		
		gst_message_parse_error (message, &err, &debug);
		
		if (player->error_handler)
			player->error_handler (player, err, player->error_handler_data);
		
		g_error_free (err);
		g_free (debug);
		
		return FALSE;
		break;
	}
	default:
		break;
	}
	
	// No errors
	return TRUE;
}

/**
 * GST bus callback.
 */
static gboolean
media_player_bus_cb (GstBus     *bus,
                     GstMessage *message,
                     MediaPlayer *player)
{
    GstState state;
//	g_debug ("Got %s message\n", GST_MESSAGE_TYPE_NAME (message));

    if (!media_player_bus_check_errors (player, message)) {
        // There were errors
        media_player_stop (player);

        return FALSE;
    }

    switch (GST_MESSAGE_TYPE(message))
    {
        case GST_MESSAGE_ASYNC_DONE:
            g_debug("GST_MESSAGE_ASYNC_DONE");
            gst_element_get_state(player->player, &state, NULL, GST_CLOCK_TIME_NONE);
            if (state == GST_STATE_PAUSED) {
                gst_element_seek (player->player, 1.0, GST_FORMAT_TIME,
                                  GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_SEGMENT,
                                  GST_SEEK_TYPE_SET, 0,
                                  GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
                gst_element_set_state (player->player, GST_STATE_PLAYING);
            }
            break;
        case GST_MESSAGE_SEGMENT_DONE:
            g_debug("GST_MESSAGE_SEGMENT_DONE");
            // End of segment. Do we loop?
            if (player->loop) {
                // Perform a segment seek to the beginning of the stream
                gst_element_seek (player->player, 1.0, GST_FORMAT_TIME,
                                  GST_SEEK_FLAG_SEGMENT,
                                  GST_SEEK_TYPE_SET, 0,
                                  GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
            } else {
                // Perform a normal seek so we reach EOS
                gst_element_seek (player->player, 1.0, GST_FORMAT_TIME,
                                  GST_SEEK_FLAG_NONE,
                                  GST_SEEK_TYPE_NONE, 0,
                                  GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE);
            }

            break;
        case GST_MESSAGE_EOS:
            g_debug("GST_MESSAGE_EOS");
            media_player_stop (player);
            break;
        default:
            break;
    }

    return TRUE;
}

/**
 * Start media player
 */
void
media_player_start (MediaPlayer *player)
{
	GstBus *bus;
	
	g_assert(player);

	// Attach bus watcher
	bus = gst_pipeline_get_bus (GST_PIPELINE (player->player));
	player->watch_id = gst_bus_add_watch (bus, (GstBusFunc) media_player_bus_cb, player);
	gst_object_unref (bus);
	
	gst_element_set_state (player->player, GST_STATE_PAUSED);
	media_player_set_state (player, MEDIA_PLAYER_PLAYING);
}

/**
 * Stop player
 */
void
media_player_stop (MediaPlayer *player)
{
	g_assert(player);

	if (player->watch_id) {
		g_source_remove (player->watch_id);
		
		player->watch_id = 0;
	}
	
	if (player->player != NULL) {
		gst_element_set_state (player->player, GST_STATE_NULL);
	}
	
	media_player_set_state (player, MEDIA_PLAYER_STOPPED);
}

/*
 * }} Media player
 */