summaryrefslogtreecommitdiff
path: root/indra/media_plugins/libvlc/media_plugin_libvlc.cpp
blob: 43d4e952433db716882092330c00c282563fa889 (plain)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/**
 * @file media_plugin_libvlc.cpp
 * @brief LibVLC plugin for LLMedia API plugin system
 *
 * @cond
 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 * @endcond
 */

#include "linden_common.h"

#include "llgl.h"
#include "llplugininstance.h"
#include "llpluginmessage.h"
#include "llpluginmessageclasses.h"
#include "media_plugin_base.h"

#include "vlc/vlc.h"
#include "vlc/libvlc_version.h"

////////////////////////////////////////////////////////////////////////////////
//
class MediaPluginLibVLC :
        public MediaPluginBase
{
    public:
        MediaPluginLibVLC( LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data );
        ~MediaPluginLibVLC();

        /*virtual*/ void receiveMessage( const char* message_string );

    private:
        bool init();

		void initVLC();
		void playMedia();
		void resetVLC();

		static void* lock(void* data, void** p_pixels);
		static void unlock(void* data, void* id, void* const* raw_pixels);
		static void display(void* data, void* id);

		libvlc_instance_t* gLibVLC;
		libvlc_media_t* gLibVLCMedia;
		libvlc_media_player_t* gLibVLCMediaPlayer;

		struct gVLCContext
		{
			unsigned char* texture_pixels;
			libvlc_media_player_t* mp;
			MediaPluginLibVLC* parent;
		};
		struct gVLCContext gVLCCallbackContext;

		std::string mURL;
};

////////////////////////////////////////////////////////////////////////////////
//
MediaPluginLibVLC::MediaPluginLibVLC( LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data ) :
    MediaPluginBase( host_send_func, host_user_data )
{
	mTextureWidth = 0;
	mTextureHeight = 0;
    mWidth = 0;
    mHeight = 0;
    mDepth = 4;
    mPixels = 0;

	gLibVLC = 0;
	gLibVLCMedia = 0;
	gLibVLCMediaPlayer = 0;

	mURL = std::string();
}

////////////////////////////////////////////////////////////////////////////////
//
MediaPluginLibVLC::~MediaPluginLibVLC()
{
}

/////////////////////////////////////////////////////////////////////////////////
//
void* MediaPluginLibVLC::lock(void* data, void** p_pixels)
{
	struct gVLCContext* context = (gVLCContext*)data;

	*p_pixels = context->texture_pixels;

	return NULL;
}

/////////////////////////////////////////////////////////////////////////////////
//
void MediaPluginLibVLC::unlock(void* data, void* id, void* const* raw_pixels)
{
	// nothing to do here for the moment.
	// we can modify the raw_pixels here if we want to.
}

////////////////////////////////////////////////////////////////////////////////
//
void MediaPluginLibVLC::display(void* data, void* id)
{
	struct gVLCContext* context = (gVLCContext*)data;

	context->parent->setDirty(0, 0, context->parent->mWidth, context->parent->mHeight);
}

////////////////////////////////////////////////////////////////////////////////
//
void MediaPluginLibVLC::initVLC()
{
	char const* vlc_argv[] =
	{
		"--no-xlib",
	};

	int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
	gLibVLC = libvlc_new(vlc_argc, vlc_argv);

	if (!gLibVLC)
	{
		// for the moment, if this fails, the plugin will fail and 
		// the media sub-system will tell the viewer something went wrong.
	}
}

////////////////////////////////////////////////////////////////////////////////
//
void MediaPluginLibVLC::resetVLC()
{
	libvlc_media_player_stop(gLibVLCMediaPlayer);
	libvlc_media_player_release(gLibVLCMediaPlayer);
	libvlc_release(gLibVLC);
}

////////////////////////////////////////////////////////////////////////////////
//
void MediaPluginLibVLC::playMedia()
{
	if (mURL.length() == 0)
	{
		return;
	}

	if (gLibVLCMediaPlayer)
	{
		libvlc_media_player_stop(gLibVLCMediaPlayer);
		libvlc_media_player_release(gLibVLCMediaPlayer);
	}

	gLibVLCMedia = libvlc_media_new_location(gLibVLC, mURL.c_str());
	if (!gLibVLCMedia)
	{
		gLibVLCMediaPlayer = 0;
		return;
	}

	gLibVLCMediaPlayer = libvlc_media_player_new_from_media(gLibVLCMedia);
	if (!gLibVLCMediaPlayer)
	{
		return;
	}

	libvlc_media_release(gLibVLCMedia);

	gVLCCallbackContext.parent = this;
	gVLCCallbackContext.texture_pixels = mPixels;
	gVLCCallbackContext.mp = gLibVLCMediaPlayer;

	libvlc_video_set_callbacks(gLibVLCMediaPlayer, lock, unlock, display, &gVLCCallbackContext);
	libvlc_video_set_format(gLibVLCMediaPlayer, "RV32", mWidth, mHeight, mWidth * mDepth);
	libvlc_media_player_play(gLibVLCMediaPlayer);
}

////////////////////////////////////////////////////////////////////////////////
//
void MediaPluginLibVLC::receiveMessage( const char* message_string )
{
    LLPluginMessage message_in;

    if(message_in.parse(message_string) >= 0)
    {
        std::string message_class = message_in.getClass();
        std::string message_name = message_in.getName();
        if(message_class == LLPLUGIN_MESSAGE_CLASS_BASE)
        {
            if(message_name == "init")
            {
				initVLC();

                LLPluginMessage message("base", "init_response");
                LLSD versions = LLSD::emptyMap();
                versions[LLPLUGIN_MESSAGE_CLASS_BASE] = LLPLUGIN_MESSAGE_CLASS_BASE_VERSION;
                versions[LLPLUGIN_MESSAGE_CLASS_MEDIA] = LLPLUGIN_MESSAGE_CLASS_MEDIA_VERSION;
                versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER] = LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER_VERSION;
                message.setValueLLSD("versions", versions);

				std::ostringstream s;
				s << "LibVLC plugin ";
				s << LIBVLC_VERSION_MAJOR;
				s << ".";
				s << LIBVLC_VERSION_MINOR;
				s << ".";
				s << LIBVLC_VERSION_REVISION;

                message.setValue("plugin_version", s.str());
                sendMessage(message);
            }
            else if(message_name == "idle")
            {
            }
            else if(message_name == "cleanup")
            {
				resetVLC();
            }
            else if(message_name == "shm_added")
            {
                SharedSegmentInfo info;
                info.mAddress = message_in.getValuePointer("address");
                info.mSize = (size_t)message_in.getValueS32("size");
                std::string name = message_in.getValue("name");

                mSharedSegments.insert(SharedSegmentMap::value_type(name, info));

            }
            else if(message_name == "shm_remove")
            {
                std::string name = message_in.getValue("name");

                SharedSegmentMap::iterator iter = mSharedSegments.find(name);
                if(iter != mSharedSegments.end())
                {
                    if(mPixels == iter->second.mAddress)
                    {
						libvlc_media_player_stop(gLibVLCMediaPlayer);
						libvlc_media_player_release(gLibVLCMediaPlayer);
						gLibVLCMediaPlayer = 0;

                        mPixels = NULL;
                        mTextureSegmentName.clear();
                    }
                    mSharedSegments.erase(iter);
                }
                else
                {
					//std::cerr << "MediaPluginWebKit::receiveMessage: unknown shared memory region!" << std::endl;
                }

                // Send the response so it can be cleaned up.
                LLPluginMessage message("base", "shm_remove_response");
                message.setValue("name", name);
                sendMessage(message);
            }
            else
            {
				//std::cerr << "MediaPluginWebKit::receiveMessage: unknown base message: " << message_name << std::endl;
            }
        }
        else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA)
        {
            if(message_name == "init")
            {
                LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "texture_params");
                message.setValueS32("default_width", 1024);
                message.setValueS32("default_height", 1024);
                message.setValueS32("depth", mDepth);
				message.setValueU32("internalformat", GL_RGB);
                message.setValueU32("format", GL_BGRA_EXT);
                message.setValueU32("type", GL_UNSIGNED_BYTE);
                message.setValueBoolean("coords_opengl", false);
                sendMessage(message);
            }
            else if(message_name == "size_change")
            {
                std::string name = message_in.getValue("name");
                S32 width = message_in.getValueS32("width");
                S32 height = message_in.getValueS32("height");
                S32 texture_width = message_in.getValueS32("texture_width");
                S32 texture_height = message_in.getValueS32("texture_height");

                if(!name.empty())
                {
                    // Find the shared memory region with this name
                    SharedSegmentMap::iterator iter = mSharedSegments.find(name);
                    if(iter != mSharedSegments.end())
                    {
                        mPixels = (unsigned char*)iter->second.mAddress;
                        mWidth = width;
                        mHeight = height;
                        mTextureWidth = texture_width;
                        mTextureHeight = texture_height;

						playMedia();
					};
                };

				LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "size_change_response");
				message.setValue("name", name);
				message.setValueS32("width", width);
				message.setValueS32("height", height);
				message.setValueS32("texture_width", texture_width);
				message.setValueS32("texture_height", texture_height);
				sendMessage(message);
            }
            else if(message_name == "load_uri")
            {
				mURL = message_in.getValue("uri");
				playMedia();
            }
        }
		else 
		if (message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA_TIME)
		{
			if (message_name == "stop")
			{
				if (gLibVLCMediaPlayer)
				{
					libvlc_media_player_stop(gLibVLCMediaPlayer);
				}
			}
			else if (message_name == "start")
			{
				if (gLibVLCMediaPlayer)
				{
					libvlc_media_player_play(gLibVLCMediaPlayer);
				}
			}
			else if (message_name == "pause")
			{
				if (gLibVLCMediaPlayer)
				{
					libvlc_media_player_pause(gLibVLCMediaPlayer);
				}
			}
			else if (message_name == "seek")
			{
			}
			else if (message_name == "set_loop")
			{
			}
			else if (message_name == "set_volume")
			{
				if (gLibVLCMediaPlayer)
				{
					F64 volume = message_in.getValueReal("volume");
					libvlc_audio_set_volume(gLibVLCMediaPlayer, (int)(volume * 100));
				}
			}
		}
    }
}

////////////////////////////////////////////////////////////////////////////////
//
bool MediaPluginLibVLC::init()
{
    LLPluginMessage message( LLPLUGIN_MESSAGE_CLASS_MEDIA, "name_text" );
    message.setValue( "name", "LibVLC Plugin" );
    sendMessage( message );

    return true;
};

////////////////////////////////////////////////////////////////////////////////
//
int init_media_plugin( LLPluginInstance::sendMessageFunction host_send_func,
                        void* host_user_data,
                        LLPluginInstance::sendMessageFunction *plugin_send_func,
                        void **plugin_user_data )
{
    MediaPluginLibVLC* self = new MediaPluginLibVLC( host_send_func, host_user_data );
    *plugin_send_func = MediaPluginLibVLC::staticReceiveMessage;
    *plugin_user_data = ( void* )self;

    return 0;
}