public
Description: An embedded media player plugin for Firefox that leverages Moonlight to provide native Windows Media support for Linux.
Homepage: http://go-mono.com/moonlight
Clone URL: git://github.com/abock/moonshine.git
jstedfast (author)
Wed Sep 02 12:33:04 -0700 2009
Aaron Bockover (committer)
Wed Sep 02 12:53:06 -0700 2009
moonshine / plugin / mmp-plugin-proxy.c
100644 334 lines (265 sloc) 8.971 kb
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
//
// This file is licensed under the MIT X11 open source license.
// http://www.opensource.org/licenses/mit-license.php
//
// Authors: Aaron Bockover <abockover@novell.com>
//
// Copyright 2009 Novell, Inc.
//
 
#include <string.h>
#include <unistd.h>
 
#include <config.h>
 
#include "mmp-plugin.h"
#include "mmp-binder.h"
 
#define MOON_CHECK_LOAD_PLUGIN() { \
if (G_UNLIKELY (MMP_HANDLE ()->module == NULL)) { \
mmp_plugin_proxy_load_moonlight (); \
} \
}
 
static struct {
const gchar *mime_type;
const gchar *extensions;
} mmp_plugin_proxy_mime_types [] = {
// Special application type for this plugin
{ "application/x-moonshine-player", "*" },
 
// Generic/WMP types?
{ "application/x-mplayer2", "*" },
{ "application/x-ms-wms", "*" },
{ "application/x-ms-wmp", "*" },
{ "application/asx", "*" },
{ "video/x-ms-asf-plugin", "*" },
{ "video/x-msvideo", "*" },
{ "video/x-ms-wmp", "*" },
 
// Actual WM types?
{ "video/x-ms-asf", "asf,asx,*" },
{ "video/x-ms-wm", "wm,*" },
{ "video/x-ms-wmv", "wmv,*" },
{ "video/x-ms-wvx", "wvx,*" },
{ "audio/x-ms-wma", "wma,*" },
{ "audio/x-ms-wax", "wax,*" },
};
 
static gboolean moon_module_load_attempted = FALSE;
 
static gboolean
mmp_plugin_proxy_load_symbol (const gchar *symbol_name, gpointer *symbol)
{
MoonlightPlugin *plugin_host = MMP_HANDLE ();
 
if (!g_module_symbol (plugin_host->module, symbol_name, symbol)) {
mp_error ("Could not locate '%s' symbol in Moonlight plugin (%s)",
symbol_name, g_module_error ());
 
g_module_close (plugin_host->module);
plugin_host->module = NULL;
 
return FALSE;
}
 
return TRUE;
}
 
static gboolean
mmp_plugin_proxy_load_module (gchar *prefix)
{
MoonlightPlugin *plugin_host = MMP_HANDLE ();
gchar *path = g_module_build_path (prefix, "moonloader");
 
if (g_file_test (path, G_FILE_TEST_EXISTS)) {
plugin_host->module = g_module_open (path, G_MODULE_BIND_LOCAL | G_MODULE_BIND_LAZY);
 
if (plugin_host->module != NULL
&& mmp_plugin_proxy_load_symbol ("NP_Initialize", (gpointer *)&plugin_host->np_initialize)
&& mmp_plugin_proxy_load_symbol ("NP_Shutdown", (gpointer *)&plugin_host->np_shutdown)
&& mmp_plugin_proxy_load_symbol ("NP_GetValue", (gpointer *)&plugin_host->np_getvalue)) {
mp_debug ("Loaded Moonlight plugin: %s", path);
g_free (path);
return TRUE;
} else if (plugin_host->module != NULL) {
if (!g_module_close (plugin_host->module)) {
mp_error ("Could not unload library that was loaded but had invalid symbols: %s (%s)",
path, g_module_error ());
}
plugin_host->module = NULL;
}
 
mp_error ("Could not load Moonlight plugin: %s (%s)", path, g_module_error ());
}
 
g_free (path);
return FALSE;
}
 
static gchar *
mmp_plugin_proxy_get_xpi_moonlight_path ()
{
// If Moonlight is installed by the user into their Firefox profile (XPI),
// we need to find the directory. Originally I used XPCOM and the directory
// service provided by Mozilla, but I really wanted to avoid libstdc++
// and linking against libxpcom, etc.
//
// Instead I use an lsof inspired hack to look for the profile directory
// of the running process.
//
 
gchar pid_path[32];
gchar fd_path[sizeof (pid_path) * 2];
const gchar *fd_name = NULL;
gchar *xpi_dir = NULL;
GDir *dir;
 
if ((gsize)g_snprintf (pid_path, sizeof (pid_path),
"/proc/%d/fd", getpid ()) > sizeof (pid_path)) {
return NULL;
}
 
if ((dir = g_dir_open (pid_path, 0, NULL)) == NULL) {
return NULL;
}
 
while (xpi_dir == NULL && (fd_name = g_dir_read_name (dir)) != NULL) {
gchar *fd_resolved_path;
gchar *file_name;
gchar *dir_name;
gint ext_offset;
 
if ((gsize)g_snprintf (fd_path, sizeof (fd_path),
"%s/%s", pid_path, fd_name) > sizeof (fd_path) ||
(fd_resolved_path = g_file_read_link (fd_path, NULL)) == NULL) {
continue;
}
 
file_name = g_path_get_basename (fd_resolved_path);
ext_offset = strlen (file_name) - strlen (".sqlite");
if (strcmp (file_name, ".parentlock") == 0
|| (ext_offset > 0 && strcmp (file_name + ext_offset, ".sqlite") == 0)) {
dir_name = g_path_get_dirname (fd_resolved_path);
xpi_dir = g_build_filename (dir_name,
"extensions",
"moonlight@novell.com",
"plugins",
NULL);
g_free (dir_name);
}
 
g_free (file_name);
g_free (fd_resolved_path);
}
 
g_dir_close (dir);
 
return xpi_dir;
}
 
static NPError
mmp_plugin_proxy_load_moonlight ()
{
static gchar *search_prefixes [] = {
NULL,
NULL,
NULL,
(gchar *)"/usr/lib/moon/plugin",
(gchar *)"/usr/lib64/moon/plugin",
(gchar *)"/usr/local/lib/moon/plugin",
(gchar *)"/usr/local/lib64/moon/plugin",
(gchar *)INSTALL_PREFIX "/lib/moon/plugin",
(gchar *)INSTALL_PREFIX "/lib64/moon/plugin"
};
 
MoonlightPlugin *plugin_host = MMP_HANDLE ();
guint i;
 
if (plugin_host->module != NULL) {
return NPERR_NO_ERROR;
} else if (moon_module_load_attempted) {
return NPERR_GENERIC_ERROR;
}
 
search_prefixes[0] = (gchar *)g_getenv ("MOON_LOADER_PATH");
search_prefixes[1] = mmp_plugin_proxy_get_xpi_moonlight_path ();
search_prefixes[2] = g_build_filename (g_get_home_dir (), ".mozilla", "plugins", NULL);
 
for (i = 0; i < G_N_ELEMENTS (search_prefixes)
&& !mmp_plugin_proxy_load_module (search_prefixes[i]); i++);
 
if (search_prefixes[1] != NULL) {
g_free (search_prefixes[1]);
}
 
g_free (search_prefixes[2]);
 
moon_module_load_attempted = TRUE;
 
if (plugin_host->module == NULL) {
mp_error ("Could not find Moonlight's libmoonloader plugin");
return NPERR_GENERIC_ERROR;
}
 
return NPERR_NO_ERROR;
}
 
// Mozilla Plugin Entry Points
 
NPError
NP_Initialize (NPNetscapeFuncs *mozilla_funcs, NPPluginFuncs *plugin_funcs)
{
MoonlightPlugin *moon_host = MMP_HANDLE ();
gsize mozilla_funcs_size;
 
MOON_CHECK_LOAD_PLUGIN ();
 
mp_debug ("NP_Initialize (%p, %p)", mozilla_funcs, plugin_funcs);
 
// Copy the Mozilla function table
mozilla_funcs_size = sizeof (NPNetscapeFuncs);
memset (&moon_host->mozilla_funcs, 0, mozilla_funcs_size);
mozilla_funcs_size = mozilla_funcs->size < mozilla_funcs_size
? mozilla_funcs->size
: mozilla_funcs_size;
memcpy (&moon_host->mozilla_funcs, mozilla_funcs, mozilla_funcs_size);
moon_host->mozilla_funcs.size = sizeof (moon_host->mozilla_funcs);
 
// Proxy NP_Initialize to Moonlight
if (MMP_HANDLE ()->np_initialize != NULL) {
NPError result = MMP_HANDLE ()->np_initialize (&moon_host->mozilla_funcs, plugin_funcs);
if (result == NPERR_NO_ERROR) {
// Override some Moonlight NPP functions
moon_host->moon_npp_new = plugin_funcs->newp;
plugin_funcs->newp = mmp_binder_npp_new;
 
moon_host->moon_npp_destroy = plugin_funcs->destroy;
plugin_funcs->destroy = mmp_binder_npp_destroy;
 
moon_host->moon_npp_stream_as_file = plugin_funcs->asfile;
plugin_funcs->asfile = mmp_binder_npp_stream_as_file;
} else {
mp_error ("Unknown error in libmoonloader's NP_Initialize: %d", result);
}
 
return result;
}
 
mp_error ("Could not call NP_Initialize from libmoonloader (NULL)");
 
return NPERR_GENERIC_ERROR;
}
 
NPError
NP_Shutdown ()
{
MoonlightPlugin *plugin_host = MMP_HANDLE ();
 
mp_debug ("NP_Shutdown");
 
if (plugin_host->np_shutdown != NULL) {
plugin_host->np_shutdown ();
}
 
if (plugin_host->module != NULL) {
g_module_close (plugin_host->module);
}
 
g_free (plugin_host->mime_description);
memset (plugin_host, 0, sizeof (MoonlightPlugin));
 
moon_module_load_attempted = FALSE;
 
return NPERR_NO_ERROR;
}
 
NPError
NP_GetValue (gpointer future, NPPVariable variable, gpointer value)
{
switch (variable) {
case NPPVpluginNameString:
*((gchar **)value) = (gchar *)"Windows Media Player Plug-in 10 (compatible; Moonshine Media Player)";
return NPERR_NO_ERROR;
case NPPVpluginDescriptionString:
*((gchar **)value) = (gchar *)"A media player powered by Moonlight, largely "
"compatible with the Windows Media Player ActiveX control.";
return NPERR_NO_ERROR;
default: {
MoonlightPlugin *plugin_host = MMP_HANDLE ();
if (plugin_host->np_getvalue != NULL) {
return plugin_host->np_getvalue (future, variable, value);
}
 
return NPERR_INVALID_PARAM;
}
}
 
g_assert_not_reached ();
}
 
gchar *
NP_GetMIMEDescription ()
{
MoonlightPlugin *plugin_host = MMP_HANDLE ();
GString *str;
guint i;
 
if (plugin_host->mime_description != NULL) {
return plugin_host->mime_description;
}
 
str = g_string_new ("");
for (i = 0; i < G_N_ELEMENTS (mmp_plugin_proxy_mime_types); i++) {
if (i > 0) {
g_string_append_c (str, ';');
}
 
g_string_append (str, mmp_plugin_proxy_mime_types[i].mime_type);
g_string_append_c (str, ':');
 
if (mmp_plugin_proxy_mime_types[i].extensions) {
g_string_append (str, mmp_plugin_proxy_mime_types[i].extensions);
}
 
g_string_append (str, ":Media Files");
}
 
plugin_host->mime_description = str->str;
g_string_free (str, false);
 
return plugin_host->mime_description;
}