XMMS2
mediainfo.c
Go to the documentation of this file.
1 /* XMMS2 - X Music Multiplexer System
2  * Copyright (C) 2003-2011 XMMS2 Team
3  *
4  * PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  */
16 
17 
18 
19 
20 /** @file
21  * This file controls the mediainfo reader thread.
22  *
23  */
24 
25 #include <stdlib.h>
26 
27 #include "xmms/xmms_log.h"
28 #include "xmms/xmms_ipc.h"
30 #include "xmmspriv/xmms_medialib.h"
31 #include "xmmspriv/xmms_xform.h"
33 
34 
35 #include <glib.h>
36 
37 /** @defgroup MediaInfoReader MediaInfoReader
38  * @ingroup XMMSServer
39  * @brief The mediainfo reader.
40  *
41  * When a item is added to the playlist the mediainfo reader will
42  * start extracting the information from this entry and update it
43  * if additional information is found.
44  * @{
45  */
46 
47 struct xmms_mediainfo_reader_St {
48  xmms_object_t object;
49 
50  GThread *thread;
51  GMutex *mutex;
52  GCond *cond;
53 
54  gboolean running;
55 };
56 
57 static void xmms_mediainfo_reader_stop (xmms_object_t *o);
58 static gpointer xmms_mediainfo_reader_thread (gpointer data);
59 
60 #include "mediainfo_ipc.c"
61 
62 /**
63  * Start a new mediainfo reader thread
64  */
65 
68 {
70 
72  xmms_mediainfo_reader_stop);
73 
74  xmms_mediainfo_reader_register_ipc_commands (XMMS_OBJECT (mrt));
75 
76  mrt->mutex = g_mutex_new ();
77  mrt->cond = g_cond_new ();
78  mrt->running = TRUE;
79  mrt->thread = g_thread_create (xmms_mediainfo_reader_thread, mrt, TRUE, NULL);
80 
81  return mrt;
82 }
83 
84 /**
85  * Kill the mediainfo reader thread
86  */
87 
88 static void
89 xmms_mediainfo_reader_stop (xmms_object_t *o)
90 {
92 
93  g_mutex_lock (mir->mutex);
94  mir->running = FALSE;
95  g_cond_signal (mir->cond);
96  g_mutex_unlock (mir->mutex);
97 
98  xmms_mediainfo_reader_unregister_ipc_commands ();
99 
100  g_thread_join (mir->thread);
101 
102  g_cond_free (mir->cond);
103  g_mutex_free (mir->mutex);
104 }
105 
106 /**
107  * Wake the reader thread and start process the entries.
108  */
109 
110 void
112 {
113  g_return_if_fail (mr);
114 
115  g_mutex_lock (mr->mutex);
116  g_cond_signal (mr->cond);
117  g_mutex_unlock (mr->mutex);
118 }
119 
120 /** @} */
121 
122 static gpointer
123 xmms_mediainfo_reader_thread (gpointer data)
124 {
125  GList *goal_format;
126  GTimeVal timeval;
128  guint num = 0;
129 
130  xmms_set_thread_name ("x2 media info");
131 
133 
138 
139 
142  "audio/pcm",
144  goal_format = g_list_prepend (NULL, f);
145 
146  while (mrt->running) {
147  xmms_medialib_session_t *session;
148  xmmsc_medialib_entry_status_t prev_status;
149  guint lmod = 0;
150  xmms_medialib_entry_t entry;
151  xmms_xform_t *xform;
152 
153  session = xmms_medialib_begin_write ();
154  entry = xmms_medialib_entry_not_resolved_get (session);
155  XMMS_DBG ("got %d as not resolved", entry);
156 
157  if (!entry) {
158  xmms_medialib_end (session);
159 
164 
165  g_mutex_lock (mrt->mutex);
166  g_cond_wait (mrt->cond, mrt->mutex);
167  g_mutex_unlock (mrt->mutex);
168 
169  num = 0;
170 
175  continue;
176  }
177 
180 
182 
183  if (num == 0) {
188  num = 10;
189  } else {
190  num--;
191  }
192 
193  xmms_medialib_end (session);
194  xform = xmms_xform_chain_setup (entry, goal_format, TRUE);
195 
196  if (!xform) {
197  if (prev_status == XMMS_MEDIALIB_ENTRY_STATUS_NEW) {
199  } else {
200  session = xmms_medialib_begin_write ();
202  xmms_medialib_end (session);
204  }
205  continue;
206  }
207 
208  xmms_object_unref (xform);
209  g_get_current_time (&timeval);
210 
211  session = xmms_medialib_begin_write ();
213  xmms_medialib_entry_property_set_int (session, entry,
215  timeval.tv_sec);
216  xmms_medialib_end (session);
218 
219  }
220 
221  g_list_free (goal_format);
222  xmms_object_unref (f);
223 
224  return NULL;
225 }
xmms_mediainfo_reader_t * xmms_mediainfo_reader_start(void)
Start a new mediainfo reader thread.
Definition: mediainfo.c:67
G_BEGIN_DECLS typedef gint32 xmms_medialib_entry_t
Definition: xmms_medialib.h:86
struct xmms_mediainfo_reader_St xmms_mediainfo_reader_t
void xmms_set_thread_name(const gchar *name)
#define XMMS_OBJECT(p)
Definition: xmms_object.h:77
#define xmms_object_unref(obj)
Definition: xmms_object.h:109
xmms_xform_t * xmms_xform_chain_setup(xmms_medialib_entry_t entry, GList *goal_formats, gboolean rehash)
Definition: xform.c:1388
gint xmms_medialib_entry_property_get_int(xmms_medialib_session_t *session, xmms_medialib_entry_t entry, const gchar *property)
Retrieve a property as a int from a entry.
Definition: medialib.c:543
struct xmms_stream_type_St xmms_stream_type_t
xmmsc_medialib_entry_status_t
struct xmms_xform_St xmms_xform_t
xmms_stream_type_t * _xmms_stream_type_new(const gchar *begin,...)
Definition: streamtype.c:362
void xmms_object_emit_f(xmms_object_t *object, guint32 signalid, xmmsv_type_t type,...)
Emits a signal on the current object.
Definition: object.c:256
#define XMMS_MEDIALIB_ENTRY_PROPERTY_LMOD
Definition: xmms_medialib.h:44
#define XMMS_MEDIALIB_ENTRY_PROPERTY_ADDED
Definition: xmms_medialib.h:54
#define XMMS_STREAM_TYPE_BEGIN
void xmms_mediainfo_reader_wakeup(xmms_mediainfo_reader_t *mr)
Wake the reader thread and start process the entries.
Definition: mediainfo.c:111
#define XMMS_MEDIALIB_ENTRY_PROPERTY_STATUS
Definition: xmms_medialib.h:67
struct xmms_medialib_session_St xmms_medialib_session_t
Definition: xmms_medialib.h:87
void xmms_medialib_entry_send_update(xmms_medialib_entry_t entry)
Trigger a update signal to the client.
Definition: medialib.c:674
#define XMMS_DBG(fmt,...)
Definition: xmms_log.h:32
#define xmms_medialib_begin_write()
#define xmms_object_new(objtype, destroyfunc)
Definition: xmms_object.h:115
#define xmms_medialib_entry_status_set(session, e, st)
guint xmms_medialib_num_not_resolved(xmms_medialib_session_t *session)
Definition: medialib.c:1458
gboolean xmms_medialib_entry_property_set_int(xmms_medialib_session_t *session, xmms_medialib_entry_t entry, const gchar *property, gint value)
Set a entry property to a new value, overwriting the old value.
Definition: medialib.c:571
void xmms_medialib_entry_remove(xmms_medialib_entry_t entry)
Remove a medialib entry from the database.
Definition: medialib.c:710
void xmms_medialib_end(xmms_medialib_session_t *session)
Definition: medialib.c:425
xmms_medialib_entry_t xmms_medialib_entry_not_resolved_get(xmms_medialib_session_t *session)
Definition: medialib.c:1440