~ubuntu-branches/ubuntu/precise/rygel/precise

« back to all changes in this revision

Viewing changes to src/plugins/media-export/rygel-media-export-item.vala

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Henriksson
  • Date: 2009-09-26 14:04:05 UTC
  • Revision ID: james.westby@ubuntu.com-20090926140405-d5x3j13k10psa1gu
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 
3
 * Copyright (C) 2008 Nokia Corporation.
 
4
 *
 
5
 * Author: Zeeshan Ali <zeenix@gmail.com>
 
6
 *
 
7
 * This file is part of Rygel.
 
8
 *
 
9
 * Rygel is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU Lesser General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * Rygel is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU Lesser General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public License
 
20
 * along with this program; if not, write to the Free Software Foundation,
 
21
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
22
 */
 
23
 
 
24
using GUPnP;
 
25
using Gst;
 
26
 
 
27
/**
 
28
 * Represents MediaExport item.
 
29
 */
 
30
public class Rygel.MediaExportItem : Rygel.MediaItem {
 
31
    public MediaExportItem (MediaContainer parent,
 
32
                            File           file,
 
33
                            FileInfo       info) {
 
34
        string content_type = info.get_content_type ();
 
35
        string item_class = null;
 
36
        string id = Checksum.compute_for_string (ChecksumType.MD5,
 
37
                                                 info.get_name ());
 
38
 
 
39
        // use heuristics based on content type; will use MediaHarvester
 
40
        // when it's ready
 
41
 
 
42
        if (content_type.has_prefix ("video/")) {
 
43
            item_class = MediaItem.VIDEO_CLASS;
 
44
        } else if (content_type.has_prefix ("audio/")) {
 
45
            item_class = MediaItem.AUDIO_CLASS;
 
46
        } else if (content_type.has_prefix ("image/")) {
 
47
            item_class = MediaItem.IMAGE_CLASS;
 
48
        }
 
49
 
 
50
        if (item_class == null) {
 
51
            item_class = MediaItem.AUDIO_CLASS;
 
52
            warning ("Failed to detect UPnP class for '%s', assuming it's '%s'",
 
53
                     file.get_uri (), item_class);
 
54
        }
 
55
 
 
56
        base (id, parent, info.get_name (), item_class);
 
57
 
 
58
        this.mime_type = content_type;
 
59
        this.add_uri (file.get_uri (), null);
 
60
    }
 
61
 
 
62
    private void fill_from_tags_as_image (Gst.TagList tag_list) {
 
63
 
 
64
        tag_list.get_string (MetadataExtractor.TAG_RYGEL_MIME, out this.mime_type);
 
65
        int64 size;
 
66
        tag_list.get_int64 (MetadataExtractor.TAG_RYGEL_SIZE, out size);
 
67
        this.size = (long) size;
 
68
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_WIDTH, out this.width);
 
69
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_HEIGHT, out this.height);
 
70
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_DEPTH, out this.color_depth);
 
71
    }
 
72
 
 
73
    private void fill_from_tags_as_audio (Gst.TagList tag_list) {
 
74
        int64 duration;
 
75
        tag_list.get_int64 (MetadataExtractor.TAG_RYGEL_DURATION, out duration);
 
76
        this.duration = (long) (duration / 1000000000);
 
77
 
 
78
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_CHANNELS, out this.n_audio_channels);
 
79
        tag_list.get_string (MetadataExtractor.TAG_RYGEL_MIME, out this.mime_type);
 
80
 
 
81
        int64 size;
 
82
        tag_list.get_int64 (MetadataExtractor.TAG_RYGEL_SIZE, out size);
 
83
        this.size = (long) size;
 
84
 
 
85
        tag_list.get_string (TAG_ARTIST, out this.author);
 
86
        tag_list.get_string (TAG_ALBUM, out this.album);
 
87
 
 
88
        uint tmp;
 
89
        tag_list.get_uint (TAG_TRACK_NUMBER, out tmp);
 
90
        this.track_number = (int)tmp;
 
91
        tag_list.get_uint (TAG_BITRATE, out tmp);
 
92
        this.bitrate = (int)tmp / 8;
 
93
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_RATE, out this.sample_freq);
 
94
 
 
95
        GLib.Date? date;
 
96
        if (tag_list.get_date (TAG_DATE, out date)) {
 
97
            char[] datestr = new char[30];
 
98
            date.strftime(datestr, "%F");
 
99
            this.date = (string)datestr;
 
100
        }
 
101
    }
 
102
 
 
103
    private void fill_from_tags_as_video (Gst.TagList tag_list) {
 
104
        tag_list.get_string (MetadataExtractor.TAG_RYGEL_MIME,
 
105
                out this.mime_type);
 
106
        int64 size;
 
107
        tag_list.get_int64 (MetadataExtractor.TAG_RYGEL_SIZE,
 
108
                out size);
 
109
        this.size = (long) size;
 
110
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_WIDTH,
 
111
                out this.width);
 
112
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_HEIGHT,
 
113
                out this.height);
 
114
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_DEPTH,
 
115
                out this.color_depth);
 
116
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_CHANNELS,
 
117
                out this.n_audio_channels);
 
118
        tag_list.get_int (MetadataExtractor.TAG_RYGEL_RATE,
 
119
                out this.sample_freq);
 
120
    }
 
121
 
 
122
    public static MediaItem? create_from_taglist (MediaContainer parent,
 
123
                                                  File file,
 
124
                                                  Gst.TagList tag_list) {
 
125
        string id = Checksum.compute_for_string (ChecksumType.MD5,
 
126
                                                 file.get_uri ());
 
127
        int width = -1;
 
128
        int height = -1;
 
129
        string class_guessed = null;
 
130
 
 
131
        if (tag_list != null) {
 
132
            string codec;
 
133
 
 
134
            if (!tag_list.get_string (TAG_VIDEO_CODEC, out codec)) {
 
135
                if (!tag_list.get_string (TAG_AUDIO_CODEC, out codec)) {
 
136
                    if (tag_list.get_int (MetadataExtractor.TAG_RYGEL_WIDTH, out width) ||
 
137
                        tag_list.get_int (MetadataExtractor.TAG_RYGEL_HEIGHT, out height)) {
 
138
                        class_guessed = MediaItem.IMAGE_CLASS;
 
139
                    } else {
 
140
                        warning("There's no codec inside and file is no image: " +
 
141
                                "%s", file.get_uri ());
 
142
                        return null;
 
143
                    }
 
144
                } else {
 
145
                    class_guessed = MediaItem.AUDIO_CLASS;
 
146
                }
 
147
            } else {
 
148
                class_guessed = MediaItem.VIDEO_CLASS;
 
149
            }
 
150
        } else {
 
151
            // throw error. Taglist can't be empty
 
152
            warning("Got empty taglist for file %s", file.get_uri ());
 
153
            return null;
 
154
        }
 
155
 
 
156
        return new MediaExportItem.from_taglist (parent,
 
157
                                                 id,
 
158
                                                 file,
 
159
                                                 tag_list,
 
160
                                                 class_guessed);
 
161
    }
 
162
 
 
163
    private MediaExportItem.from_taglist (MediaContainer parent,
 
164
                                          string id,
 
165
                                          File file,
 
166
                                          Gst.TagList tag_list,
 
167
                                          string upnp_class) {
 
168
        string title = null;
 
169
        if (upnp_class == MediaItem.AUDIO_CLASS ||
 
170
            upnp_class == MediaItem.MUSIC_CLASS) {
 
171
 
 
172
            if (!tag_list.get_string (TAG_TITLE, out title)) {
 
173
                title = file.get_basename ();
 
174
            }
 
175
 
 
176
        } else {
 
177
            title = file.get_basename ();
 
178
        }
 
179
        base (id, parent, title, upnp_class);
 
180
        switch (upnp_class) {
 
181
            case MediaItem.AUDIO_CLASS:
 
182
            case MediaItem.MUSIC_CLASS:
 
183
                fill_from_tags_as_audio (tag_list);
 
184
                break;
 
185
            case MediaItem.VIDEO_CLASS:
 
186
                fill_from_tags_as_video (tag_list);
 
187
                break;
 
188
            case MediaItem.IMAGE_CLASS:
 
189
                fill_from_tags_as_image (tag_list);
 
190
                break;
 
191
            default:
 
192
                break;
 
193
        }
 
194
 
 
195
        uint64 mtime;
 
196
        tag_list.get_uint64 (MetadataExtractor.TAG_RYGEL_MTIME,
 
197
                             out mtime);
 
198
 
 
199
        this.modified = (int64) mtime;
 
200
 
 
201
        this.add_uri (file.get_uri (), null);
 
202
    }
 
203
}
 
204