File: media_player.c

package info (click to toggle)
gtkpod 2.1.5-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,428 kB
  • ctags: 6,307
  • sloc: ansic: 51,604; xml: 16,135; sh: 11,916; cpp: 7,545; perl: 1,449; makefile: 1,376; lex: 638; awk: 73; python: 35
file content (642 lines) | stat: -rw-r--r-- 19,116 bytes parent folder | download | duplicates (2)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/* Time-stamp: <2008-01-07 07:07:33 Sonic1>
 |
 |  Copyright (C) 2002-2003 Ryan Houdek <Sonicadvance1 at users.sourceforge.net>
 |  Modified by Paul Richardson <phantom_sf at users.sourceforge.net>
 |  Part of the gtkpod project.
 |
 |  URL: http://gtkpod.sourceforge.net/
 |
 |  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 |
 |  iTunes and iPod are trademarks of Apple
 |
 |  This product is not supported/written/published by Apple!
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <math.h>
#include "libgtkpod/itdb.h"
#include "libgtkpod/file.h"
#include "libgtkpod/directories.h"
#include "libgtkpod/misc.h"
#include "libgtkpod/prefs.h"
#include "plugin.h"
#include "media_player.h"

#ifndef M_LN10
#define M_LN10 (log(10.0))
#endif

#define MEDIA_PLAYER_VOLUME_KEY "media_player_volume_key"
#define MEDIA_PLAYER_VOLUME_MUTE "media_player_volume_mute"

static MediaPlayer *player;

// Declarations
static void thread_play_song();
static gint thread_stop_song(void *data);
static gint thread_next_song(void *data);

static int pipeline_bus_watch_cb(GstBus *bus, GstMessage *msg, gpointer data);

static gboolean set_scale_range(GstElement *pipeline) {
    gint64 len;

    if (!player)
        return FALSE;

    if (!player->loop)
        return FALSE;

    if (g_main_loop_is_running(player->loop)) {
        if (gst_element_query_duration(pipeline, GST_FORMAT_TIME, &len)) {
            gtk_range_set_range(GTK_RANGE(player->song_scale), 0, (((GstClockTime) (len)) / GST_SECOND));
            //            if (icon)
            //                gtk_status_icon_set_tooltip(icon, (gchar *) g_object_get_data(G_OBJECT (song_label), "tr_title"));
            return FALSE;
        }
    }
    else
        return FALSE;

    return TRUE;
}

static gboolean set_scale_position(GstElement *pipeline) {
    gint64 pos;
    gint64 len;

    if (player == NULL)
        return FALSE;

    if (!player->loop || !player->thread)
        return FALSE;

    if (!g_main_loop_is_running(player->loop))
        return FALSE;

    if (gst_element_query_position(pipeline, GST_FORMAT_TIME, &pos)) {
        gint seconds = ((GstClockTime) pos) / GST_SECOND;
        gint length;
        gchar *label;

        gst_element_query_duration(pipeline, GST_FORMAT_TIME, &len);
        length = ((GstClockTime) len) / GST_SECOND;

        label = g_strdup_printf(_("%d:%02d of %d:%02d"), seconds / 60, seconds % 60, length / 60, length % 60);
        gtk_range_set_value(GTK_RANGE(player->song_scale), seconds);
        gtk_label_set_text(GTK_LABEL(player->song_time_label), label);
        g_free(label);
        return TRUE;
    }

    return FALSE;
}

static void update_volume(gdouble value) {
    if (!player)
        return;

    if (value < 0) {
        value = 0;
    }

    player->volume_level = value / 10;
    prefs_set_double(MEDIA_PLAYER_VOLUME_KEY, player->volume_level);
    prefs_set_double(MEDIA_PLAYER_VOLUME_MUTE, player->volume_level == 0 ? 1 : 0);

    if (player->play_element) {
        g_object_set(player->play_element, "volume", player->volume_level, NULL);
    }
}

static void set_song_label(Track *track) {
    if (!track) {
        gtk_label_set_markup(GTK_LABEL(player->song_label), "");
        return;
    }

    gchar *label;

    // title by artist from album
    const gchar *track_title = track->title ? track->title : _("No Track Title");
    gboolean have_track_artist = track->artist && strlen(track->artist) > 0;
    gboolean have_track_album = track->album && strlen(track->album) > 0;

    if (have_track_artist && have_track_album)
        label = g_markup_printf_escaped(_("<b>%s</b> by %s from %s"), track_title, track->artist, track->album);
    else if (have_track_artist)
        label = g_markup_printf_escaped(_("<b>%s</b> by %s"), track_title, track->artist);
    else if (have_track_album)
        label = g_markup_printf_escaped(_("<b>%s</b> from %s"), track_title, track->album);
    else
        label = g_markup_printf_escaped("<b>%s</b>", track_title);

    gtk_label_set_markup(GTK_LABEL(player->song_label), label);
    g_object_set_data(G_OBJECT (player->song_label), "tr_title", track->title);
    g_object_set_data(G_OBJECT (player->song_label), "tr_artist", track->artist);
    g_free(label);
}

static void set_control_state(GstState state) {

    Track *tr = g_list_nth_data(player->tracks, player->track_index);
    if (tr) {
        set_song_label(tr);
    }

    switch (state) {
    case GST_STATE_PLAYING:
        gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(player->play_button), GTK_STOCK_MEDIA_PAUSE);
        break;
    case GST_STATE_PAUSED:
        gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(player->play_button), GTK_STOCK_MEDIA_PLAY);
        break;
    default:
        gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(player->play_button), GTK_STOCK_MEDIA_PLAY);
        gtk_range_set_range(GTK_RANGE(player->song_scale), 0, 1);
        gtk_range_set_value(GTK_RANGE(player->song_scale), 0);
        gtk_label_set_text(GTK_LABEL(player->song_time_label), "");
    }
}

static void waitforpipeline(int state) {
    if (!player)
        return;

    if (!player->loop || !player->thread)
        return;

    if (!player->play_element)
        return;

    GstState istate, ipending;
    gst_element_get_state(player->play_element, &istate, &ipending, GST_CLOCK_TIME_NONE);

    if (istate == GST_STATE_VOID_PENDING) {
        return;
    }

    if (istate == state)
        return;

    gst_element_set_state(player->play_element, state);

    do {
        gst_element_get_state(player->play_element, &istate, &ipending, GST_CLOCK_TIME_NONE);

        if (istate == GST_STATE_VOID_PENDING) {
            return;
        }
    }
    while (istate != state);
}

static gboolean is_playing() {
    if (!player || !player->loop || !player->play_element || !player->thread || !g_main_loop_is_running(player->loop))
        return FALSE;

    GstState state, pending;
    gst_element_get_state(player->play_element, &state, &pending, GST_CLOCK_TIME_NONE);

    if (state == GST_STATE_PLAYING)
        return TRUE;

    return FALSE;
}

static gboolean is_paused() {
    if (!player || !player->loop || !player->play_element || !player->thread || !g_main_loop_is_running(player->loop))
        return FALSE;

    GstState state, pending;
    gst_element_get_state(player->play_element, &state, &pending, GST_CLOCK_TIME_NONE);

    if (state == GST_STATE_PAUSED)
        return TRUE;

    return FALSE;
}

static gboolean is_stopped() {
    if (!player || !player->loop || !player->play_element || !player->thread || !g_main_loop_is_running(player->loop)) {
        return TRUE;
    }

    GstState state, pending;
    gst_element_get_state(player->play_element, &state, &pending, GST_CLOCK_TIME_NONE);

    if (state == GST_STATE_NULL)
        return TRUE;

    return FALSE;
}

static void stop_song() {
    if (!player)
        return;

    if (player->loop && g_main_loop_is_running(player->loop)) {
        g_main_loop_quit(player->loop);
    }

    waitforpipeline(GST_STATE_NULL);

    player->thread = NULL;

    set_control_state(GST_STATE_NULL);
}

static void pause_or_play_song() {
    if (!player || !player->tracks)
        return;

    if (is_stopped()) {
        GError *err1 = NULL;

        set_control_state(GST_STATE_PLAYING);

        player->thread = g_thread_try_new ("play-song-thread", (GThreadFunc)thread_play_song, NULL, &err1);
        if (!player->thread) {
            /* Translators: %s is an error message */
            gtkpod_statusbar_message(_("GStreamer thread creation failed: %s\n"), err1->message);
            g_error_free(err1);
        }
    } else if (is_playing()) {
        waitforpipeline(GST_STATE_PAUSED);
        set_control_state(GST_STATE_PAUSED);
    } else if (is_paused()) {
        waitforpipeline(GST_STATE_PLAYING);
        set_control_state(GST_STATE_PLAYING);
    }
}

static void next_song() {
    gboolean playing = is_playing() || is_paused();

    if (playing) {
        stop_song();
    }

    if (player->track_index < g_list_length(player->tracks) - 1) {
        player->track_index++;
    } else {
        player->track_index = 0;
    }

    set_song_label(g_list_nth_data(player->tracks, player->track_index));

    if (playing) {
        pause_or_play_song();
    }
}

static void previous_song() {
    gboolean playing = is_playing() || is_paused();

    if (playing) {
        stop_song();
    }

    if (player->track_index > 0) {
        player->track_index--;
    } else {
        player->track_index = g_list_length(player->tracks) - 1;
    }

    set_song_label(g_list_nth_data(player->tracks, player->track_index));

    if (playing)
        pause_or_play_song();
}

void seek_to_time(gint64 time_seconds) {
    if (is_stopped())
        return;

    if (!gst_element_seek(player->play_element, 1.0, GST_FORMAT_TIME, GST_SEEK_FLAG_FLUSH, GST_SEEK_TYPE_SET, time_seconds
            * 1000000000, GST_SEEK_TYPE_NONE, GST_CLOCK_TIME_NONE))
        gtkpod_statusbar_message(_("Seek failed!\n"));
}

static int pipeline_bus_watch_cb(GstBus *bus, GstMessage *msg, gpointer data) {
    switch (GST_MESSAGE_TYPE (msg)) {
    case GST_MESSAGE_EOS:
        gdk_threads_add_idle(thread_next_song, NULL);
        break;
    case GST_MESSAGE_ERROR: {
        gdk_threads_add_idle(thread_stop_song, NULL);

        GError *err = NULL;
        gchar *dbg_info = NULL;

        gst_message_parse_error (msg, &err, &dbg_info);
        g_warning ("ERROR from element %s: %s\n",
                GST_OBJECT_NAME (msg->src), err->message);
        g_warning ("Debugging info: %s\n", (dbg_info) ? dbg_info : "none");
        g_error_free (err);
        g_free (dbg_info);
        break;
    }
    default:
        break;
    }

    return TRUE;
}

static void thread_play_song() {
    gchar *track_name;
    gchar *uri;
    GstBus *bus;
    GError *error;

    if (!player || !player->tracks)
        return;

    Track *tr = g_list_nth_data(player->tracks, player->track_index);
    if (!tr) {
        gtkpod_statusbar_message(_("Failed to play track: Track is no longer available"));
        return;
    }

    error = NULL;
    track_name = get_file_name_from_source(tr, SOURCE_PREFER_LOCAL);
    if (!track_name) {
        gtkpod_statusbar_message(_("Failed to play track: Unable to find the file for the track '%s'"), tr->title);
        stop_song();
        return;
    }

    /* init GStreamer */
    player->loop = g_main_loop_new(NULL, FALSE); // make new loop
    uri = g_filename_to_uri (track_name, NULL, &error);
    g_free(track_name);
    if (error) {
        /* Translators: %s is an error message */
        gtkpod_statusbar_message(_("Failed to play track: %s"), error->message);
        stop_song();
        g_free(uri);
        return;
    }

    player->play_element = gst_element_factory_make("playbin", "play");
    if (!player->play_element) {
        gtkpod_statusbar_message(_("Failed to play track: Cannot create a play element. Ensure that all gstreamer plugins are installed"));
        stop_song();
        return;
    }

    g_object_set(G_OBJECT (player->play_element), "uri", uri, NULL);
    g_object_set(player->play_element, "volume", player->volume_level, NULL);

    bus = gst_pipeline_get_bus(GST_PIPELINE (player->play_element));
    gst_bus_add_watch(bus, pipeline_bus_watch_cb, player->loop); //Add a watch to the bus
    gst_object_unref(bus); //unref the bus

    /* run */
    gst_element_set_state(player->play_element, GST_STATE_PLAYING);// set state
    g_timeout_add(250, (GSourceFunc) set_scale_range, GST_PIPELINE (player->play_element));
    g_timeout_add(1000, (GSourceFunc) set_scale_position, GST_PIPELINE (player->play_element));
    g_main_loop_run(player->loop);

    g_free(uri);

    gst_element_set_state(player->play_element, GST_STATE_NULL);
    g_thread_exit(0);
}

static gint thread_stop_song(void *data) {
    stop_song();
    return FALSE; // call only once
}

static gint thread_next_song(void *data) {
    next_song();
    return FALSE; // call only once
}

void set_selected_tracks(GList *tracks) {
    if (! tracks)
        return;

    if (is_playing() || is_paused())
        return;

    if (player->tracks) {
        g_list_free(player->tracks);
        player->tracks = NULL;
        set_song_label(NULL);
    }

    GList *l = g_list_copy(tracks);
    //Does the same thing as generate_random_playlist()
    if (player->shuffle) {
        GRand *grand = g_rand_new();
        while (l || (g_list_length(l) != 0)) {
            /* get random number between 0 and g_list_length()-1 */
            gint rn = g_rand_int_range(grand, 0, g_list_length(l));
            GList *random = g_list_nth(l, rn);
            player->tracks = g_list_append(player->tracks, random->data);
            l = g_list_delete_link(l, random);
        }
        g_rand_free(grand);
    }
    else
        player->tracks = l;

    Track *track = player->tracks->data;
    set_song_label(track);

    // Reset the track index
    player->track_index = 0;
}

void init_media_player(GtkWidget *parent) {
    GtkWidget *window;
    GtkBuilder *builder;
    gst_init_check(0, NULL, NULL);
    srand(time(NULL));

    player = g_new0(MediaPlayer, 1);
    player->glade_path = g_build_filename(get_glade_dir(), "media_player.xml", NULL);
    builder = gtkpod_builder_xml_new(player->glade_path);

    window = gtkpod_builder_xml_get_widget(builder, "media_window");
    player->media_panel = gtkpod_builder_xml_get_widget(builder, "media_panel");
    player->song_label = gtkpod_builder_xml_get_widget(builder, "song_label");
    player->song_time_label = gtkpod_builder_xml_get_widget(builder, "song_time_label");
    player->media_toolbar = gtkpod_builder_xml_get_widget(builder, "media_toolbar");

    player->play_button = gtkpod_builder_xml_get_widget(builder, "play_button");
    player->stop_button = gtkpod_builder_xml_get_widget(builder, "stop_button");
    player->previous_button = gtkpod_builder_xml_get_widget(builder, "previous_button");
    player->next_button = gtkpod_builder_xml_get_widget(builder, "next_button");
    player->song_scale = gtkpod_builder_xml_get_widget(builder, "song_scale");

    g_object_ref(player->media_panel);
    gtk_container_remove(GTK_CONTAINER (window), player->media_panel);
    gtk_widget_destroy(window);

    if (GTK_IS_SCROLLED_WINDOW(parent))
        gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(parent), player->media_panel);
    else
        gtk_container_add(GTK_CONTAINER (parent), player->media_panel);

    gtk_builder_connect_signals(builder, NULL);

    player->thread = NULL;
    player->loop = NULL;
    player->shuffle = FALSE;
    player->play_element = NULL;
    player->track_index = 0;

    /* Set the volume based on preference */
    gint volume_mute = prefs_get_int(MEDIA_PLAYER_VOLUME_MUTE);
    if (volume_mute == 1) {
        player->volume_level = 0;
    }
    else {
        gdouble volume = prefs_get_double(MEDIA_PLAYER_VOLUME_KEY);
        if (volume == 0) {
            /*
             * The preference is at its default value so set it to
             * the default of level 0.5
             */
            player->volume_level = 0.5;
        }
        else {
            player->volume_level = volume;
        }
    }

    gtk_widget_show_all(player->media_panel);

    g_object_unref(builder);
}

void destroy_media_player() {
    gtk_widget_destroy(player->media_panel);
    g_free(player->glade_path);
    player = NULL;
}

static gboolean on_volume_changed_cb(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data) {
    update_volume(value);
    return FALSE;
}

G_MODULE_EXPORT gboolean on_volume_window_focus_out(GtkWidget *widget, GdkEventFocus *event, gpointer data) {
    GtkWidget *vol_scale = (GtkWidget *) g_object_get_data(G_OBJECT(widget), "scale");
    update_volume(gtk_range_get_value(GTK_RANGE(vol_scale)));
    gtk_widget_destroy(widget);
    return TRUE;
}

G_MODULE_EXPORT void on_volume_button_clicked_cb(GtkToolButton *toolbutton, gpointer *userdata) {
    GtkBuilder *builder;
    GtkWidget *vol_window;
    GtkWidget *vol_scale;

    builder = gtkpod_builder_xml_new(player->glade_path);
    vol_window = gtkpod_builder_xml_get_widget(builder, "volume_window");
    vol_scale = gtkpod_builder_xml_get_widget(builder, "volume_scale");
    g_object_set_data(G_OBJECT(vol_window), "scale", vol_scale);

    gtk_range_set_value(GTK_RANGE(vol_scale), (player->volume_level * 10));
    g_signal_connect(G_OBJECT (vol_scale),
            "change-value",
            G_CALLBACK(on_volume_changed_cb),
            NULL);

    g_signal_connect (G_OBJECT (vol_window),
            "focus-out-event",
            G_CALLBACK (on_volume_window_focus_out),
            NULL);

    gtk_widget_show_all(vol_window);
    gtk_widget_grab_focus(vol_window);

    g_object_unref(builder);
}

G_MODULE_EXPORT void on_previous_button_clicked_cb(GtkToolButton *toolbutton, gpointer *userdata) {
    previous_song();
}

G_MODULE_EXPORT void on_play_button_clicked_cb(GtkToolButton *toolbutton, gpointer *userdata) {
    pause_or_play_song();
}

G_MODULE_EXPORT void on_stop_button_clicked_cb(GtkToolButton *toolbutton, gpointer *userdata) {
    stop_song();
}

G_MODULE_EXPORT void on_next_button_clicked_cb(GtkToolButton *toolbutton, gpointer *userdata) {
    next_song();
}

G_MODULE_EXPORT gboolean on_song_scale_change_value_cb(GtkRange *range, GtkScrollType scroll, gdouble value, gpointer user_data) {
    seek_to_time(value);
    return FALSE;
}

void media_player_play_tracks(GList *tracks) {
    if (!player)
        return;

    if (is_playing())
        stop_song();

    set_selected_tracks(tracks);

    pause_or_play_song();
}

void media_player_track_removed_cb(GtkPodApp *app, gpointer tk, gpointer data) {
    Track *old_track = tk;
    if (!player)
        return;

    if (g_list_index(player->tracks, old_track) == -1)
        return;

    set_selected_tracks(gtkpod_get_selected_tracks());
}

void media_player_set_tracks_cb(GtkPodApp *app, gpointer tks, gpointer data) {
    GList *tracks = tks;

    if (!player)
        return;

    set_selected_tracks(tracks);
}

void media_player_track_updated_cb(GtkPodApp *app, gpointer tk, gpointer data) {
    Track *track = tk;

    if (!player)
        return;

    if (g_list_index(player->tracks, track) == -1)
        return;

    set_selected_tracks(gtkpod_get_selected_tracks());
}