File: banshee-player-cdda.c

package info (click to toggle)
banshee 2.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 47,160 kB
  • ctags: 25,019
  • sloc: xml: 163,694; cs: 137,409; sh: 11,650; ansic: 4,790; makefile: 2,928; python: 38
file content (202 lines) | stat: -rw-r--r-- 7,237 bytes parent folder | download | duplicates (3)
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
//
// banshee-player-cdda.c
//
// Author:
//   Aaron Bockover <abockover@novell.com>
//   Julien Moutte <julien@fluendo.com>
//
// Copyright (C) 2005-2008 Novell, Inc.
// Copyright (C) 2010 Fluendo S.A.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#include <stdlib.h>
#include <gst/audio/gstaudiocdsrc.h>
#include "banshee-player-cdda.h"

// ---------------------------------------------------------------------------
// Private Functions
// ---------------------------------------------------------------------------

static GstElement *
bp_cdda_get_cdda_source (GstElement *playbin)
{
    GstElement *source = NULL;
    
    if (playbin == NULL) {
        return NULL;
    }
    
    g_object_get (playbin, "source", &source, NULL);
    
    if (source == NULL || !GST_IS_AUDIO_CD_SRC (source)) {
        if (source != NULL) {
            g_object_unref (source);
        }
        
        return NULL;
    }
    
    return source;
}

static void
bp_cdda_on_notify_source (GstElement *playbin, gpointer unknown, BansheePlayer *player)
{
    GstElement *cdda_src = NULL;
    
    g_return_if_fail (IS_BANSHEE_PLAYER (player));
    
    if (player->cdda_device == NULL) {
        return;
    }
    
    cdda_src = bp_cdda_get_cdda_source (playbin);
    if (cdda_src == NULL) {
        return;
    }
    
    // Technically don't need to check the class, since GstCddaBaseSrc elements will always have this
    if (G_LIKELY (g_object_class_find_property (G_OBJECT_GET_CLASS (cdda_src), "device"))) {
        bp_debug2 ("bp_cdda: setting device property on source (%s)", player->cdda_device);
        g_object_set (cdda_src, "device", player->cdda_device, NULL);
    }
    
    // If the GstCddaBaseSrc is cdparanoia, it will have this property, so set it
    if (g_object_class_find_property (G_OBJECT_GET_CLASS (cdda_src), "paranoia-mode")) {
        g_object_set (cdda_src, "paranoia-mode", 0, NULL);
    }
    
    g_object_unref (cdda_src);
}

static gboolean
bp_cdda_source_seek_to_track (GstElement *playbin, guint track)
{
    static GstFormat format = GST_FORMAT_UNDEFINED;
    GstElement *cdda_src = NULL;
    GstState state;
    
    format = gst_format_get_by_nick ("track");
    if (G_UNLIKELY (format == GST_FORMAT_UNDEFINED)) {
        return FALSE;
    }
    
    gst_element_get_state (playbin, &state, NULL, 0);
    if (state < GST_STATE_PAUSED) {
        // We can only seek if the pipeline is playing or paused, otherwise
        // we just allow playbin to do its thing, which will re-start the
        // device and start at the desired track
        return FALSE;
    }

    cdda_src = bp_cdda_get_cdda_source (playbin);
    if (G_UNLIKELY (cdda_src == NULL)) {
        return FALSE;
    }
    
    if (gst_element_seek (playbin, 1.0, format, GST_SEEK_FLAG_FLUSH, 
        GST_SEEK_TYPE_SET, track - 1, GST_SEEK_TYPE_NONE, -1)) {
        bp_debug2 ("bp_cdda: seeking to track %d, avoiding playbin", track);
        g_object_unref (cdda_src);
        return TRUE;
    }
    
    g_object_unref (cdda_src);
    return FALSE;
}

// ---------------------------------------------------------------------------
// Internal Functions
// ---------------------------------------------------------------------------

void
_bp_cdda_pipeline_setup (BansheePlayer *player)
{
    if (player != NULL && player->playbin != NULL) {
        g_signal_connect (player->playbin, "notify::source", G_CALLBACK (bp_cdda_on_notify_source), player);
    }
}

gboolean
_bp_cdda_handle_uri (BansheePlayer *player, const gchar *uri)
{
    // Processes URIs like cdda://<track-number>#<device-node> and overrides
    // track transitioning through playbin if playback was already happening
    // from the device node by seeking directly to the track since the disc
    // is already spinning; playbin doesn't handle CDDA URIs with device nodes
    // so we have to handle setting the device property on GstCddaBaseSrc 
    // through the notify::source signal on playbin

    const gchar *new_cdda_device;
    const gchar *p;
    
    if (player == NULL || uri == NULL || !g_str_has_prefix (uri, "cdda://")) {
        // Something is hosed or the URI isn't actually CDDA
        if (player->cdda_device != NULL) {
            bp_debug2 ("bp_cdda: finished using device (%s)", player->cdda_device);
            g_free (player->cdda_device);
            player->cdda_device = NULL;
        }
        
        return FALSE;
    }

    p = g_utf8_strchr (uri, -1, '#');
    if (p == NULL || strlen (p) < 2) {
        // Unset the cached device node if the URI doesn't
        // have its own valid device node
        g_free (player->cdda_device);
        player->cdda_device = NULL;
        bp_debug2 ("bp_cdda: invalid device node in URI (%s)", uri);
        return FALSE;
    }
    
    new_cdda_device = p + 1;
            
    if (player->cdda_device == NULL) {
        // If we weren't already playing from a CD, cache the
        // device and allow playbin to begin playing it
        player->cdda_device = g_strdup (new_cdda_device);
        bp_debug2 ("bp_cdda: storing device node for fast seeks (%s)", player->cdda_device);
        return FALSE;
    }
    
    if (strcmp (new_cdda_device, player->cdda_device) == 0) {
        // Parse the track number from the URI and seek directly to it
        // since we are already playing from the device; prevent playbin
        // from stopping/starting the CD, which can take many many seconds
        gchar *track_str = g_strndup (uri + 7, strlen (uri) - strlen (new_cdda_device) - 8);
        gint track_num = atoi (track_str);
        g_free (track_str);
        bp_debug2 ("bp_cdda: fast seeking to track on already playing device (%s)", player->cdda_device);
        
        return bp_cdda_source_seek_to_track (player->playbin, track_num);
    }
    
    // We were already playing some CD, but switched to a different device node, 
    // so unset and re-cache the new device node and allow playbin to do its thing
    bp_debug3 ("bp_cdda: switching devices for CDDA playback (from %s, to %s)", player->cdda_device, new_cdda_device);
    g_free (player->cdda_device);
    player->cdda_device = g_strdup(new_cdda_device);
    
    return FALSE;
}