playlist.c 4.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
 * Copyright 2015,      Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
 */

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

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

17 18 19 20
/* for free() and strdup() */
#include <stdlib.h>
#include <string.h>

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
#include "playlist.h"

/* for XMLSTR() */
#include "cfgfile.h"

#include "logging.h"
#define CATMODULE "playlist"

typedef struct playlist_track_tag playlist_track_t;

struct playlist_tag {
    size_t refc;
    ssize_t max_tracks;
    playlist_track_t *first;
};

struct playlist_track_tag {
    char *title;
    char *creator;
    char *album;
    char *trackNum;
    playlist_track_t *next;
};

static void __free_track(playlist_track_t *track)
{
    if (track->title)
        free(track->title);
    if (track->creator)
        free(track->creator);
    if (track->album)
        free(track->album);
    if (track->trackNum)
        free(track->trackNum);
    free(track);
}

static char * __query_vc(vorbis_comment *vc, const char *key)
{
    char *value = vorbis_comment_query(vc, key, 0);
    if (!value)
        return NULL;
    return strdup(value);
}

playlist_t * playlist_new(ssize_t max_tracks)
{
    playlist_t *playlist = calloc(1, sizeof(playlist_t));

    if (!playlist)
       return NULL;

    playlist->refc = 1;
    playlist->max_tracks = max_tracks;

    return playlist;
}

int          playlist_ref(playlist_t *playlist)
{
    if (!playlist)
        return -1;
    playlist->refc++;
    return 0;
}

int          playlist_release(playlist_t *playlist)
{
    playlist_track_t *track;

    if (!playlist)
        return -1;
    playlist->refc--;
    if (playlist->refc)
        return 0;

    while ((track = playlist->first)) {
        playlist->first = track->next;
        __free_track(track);
    }

    free(playlist);
    return 0;
}

int          playlist_set_max_tracks(playlist_t *playlist, ssize_t max_tracks)
{
    if (!playlist)
        return -1;
    playlist->max_tracks = max_tracks;
    return 0;
}


int          playlist_push_track(playlist_t *playlist, vorbis_comment *vc)
{
    playlist_track_t *track, **cur;
    ssize_t num = 0;

    if (!playlist)
        return -1;

    track = calloc(1, sizeof(playlist_track_t));
    if (!track)
        return -1;

    cur = &playlist->first;
    while (*cur) {
        cur = &(*cur)->next;
        num++;
    }
    *cur = track;

134 135
    num++; /* the extra track we just inserted */

136 137 138 139 140 141 142 143 144 145 146 147
    while (playlist->max_tracks > 0 && num > playlist->max_tracks) {
        playlist_track_t *to_free = playlist->first;
        playlist->first = to_free->next;
        __free_track(to_free);
        num--;
    }

    if (vc) {
        track->title = __query_vc(vc, "TITLE");
        track->creator = __query_vc(vc, "ARTIST");
        track->album = __query_vc(vc, "ALBUM");
        track->trackNum = __query_vc(vc, "TRACKNUMBER");
148 149 150 151 152

        if (!track->title && !track->creator && !track->album && !track->trackNum) {
            /* no useful meta data has been found. try ICY style meta data */
            track->title = __query_vc(vc, "X_ICY_TITLE");
        }
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
    }

    return 0;
}

xmlNodePtr   playlist_render_xspf(playlist_t *playlist)
{
    xmlNodePtr rootnode, tracklist, tracknode;
    playlist_track_t *track;

    if (!playlist)
        return NULL;

    rootnode = xmlNewNode(NULL, XMLSTR("playlist"));
    xmlSetProp(rootnode, XMLSTR("version"), XMLSTR("1"));
    xmlSetProp(rootnode, XMLSTR("xmlns"), XMLSTR("http://xspf.org/ns/0/"));

    tracklist = xmlNewNode(NULL, XMLSTR("trackList"));
    xmlAddChild(rootnode, tracklist);

    track = playlist->first;
    while (track) {
        tracknode = xmlNewNode(NULL, XMLSTR("track"));
        xmlAddChild(tracklist, tracknode);
        /* TODO: Handle meta data */
        if (track->title)
            xmlNewTextChild(tracknode, NULL, XMLSTR("title"), XMLSTR(track->title));
        if (track->creator)
            xmlNewTextChild(tracknode, NULL, XMLSTR("creator"), XMLSTR(track->creator));
        if (track->album)
            xmlNewTextChild(tracknode, NULL, XMLSTR("album"), XMLSTR(track->album));
        if (track->trackNum)
            xmlNewTextChild(tracknode, NULL, XMLSTR("trackNum"), XMLSTR(track->trackNum));
        track = track->next;
    }

    return rootnode;
}