/indra/media_plugins/example/media_plugin_example.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 412 lines · 304 code · 54 blank · 54 comment · 84 complexity · d05dcf08c56402b0849340d22b13ab62 MD5 · raw file

  1. /**
  2. * @file media_plugin_example.cpp
  3. * @brief Example plugin for LLMedia API plugin system
  4. *
  5. * @cond
  6. * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. * @endcond
  27. */
  28. #include "linden_common.h"
  29. #include "llgl.h"
  30. #include "llplugininstance.h"
  31. #include "llpluginmessage.h"
  32. #include "llpluginmessageclasses.h"
  33. #include "media_plugin_base.h"
  34. #include <time.h>
  35. ////////////////////////////////////////////////////////////////////////////////
  36. //
  37. class MediaPluginExample :
  38. public MediaPluginBase
  39. {
  40. public:
  41. MediaPluginExample( LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data );
  42. ~MediaPluginExample();
  43. /*virtual*/ void receiveMessage( const char* message_string );
  44. private:
  45. bool init();
  46. void update( F64 milliseconds );
  47. void write_pixel( int x, int y, unsigned char r, unsigned char g, unsigned char b );
  48. bool mFirstTime;
  49. time_t mLastUpdateTime;
  50. enum Constants { ENumObjects = 10 };
  51. unsigned char* mBackgroundPixels;
  52. int mColorR[ ENumObjects ];
  53. int mColorG[ ENumObjects ];
  54. int mColorB[ ENumObjects ];
  55. int mXpos[ ENumObjects ];
  56. int mYpos[ ENumObjects ];
  57. int mXInc[ ENumObjects ];
  58. int mYInc[ ENumObjects ];
  59. int mBlockSize[ ENumObjects ];
  60. bool mMouseButtonDown;
  61. bool mStopAction;
  62. };
  63. ////////////////////////////////////////////////////////////////////////////////
  64. //
  65. MediaPluginExample::MediaPluginExample( LLPluginInstance::sendMessageFunction host_send_func, void *host_user_data ) :
  66. MediaPluginBase( host_send_func, host_user_data )
  67. {
  68. mFirstTime = true;
  69. mWidth = 0;
  70. mHeight = 0;
  71. mDepth = 4;
  72. mPixels = 0;
  73. mMouseButtonDown = false;
  74. mStopAction = false;
  75. mLastUpdateTime = 0;
  76. }
  77. ////////////////////////////////////////////////////////////////////////////////
  78. //
  79. MediaPluginExample::~MediaPluginExample()
  80. {
  81. }
  82. ////////////////////////////////////////////////////////////////////////////////
  83. //
  84. void MediaPluginExample::receiveMessage( const char* message_string )
  85. {
  86. // std::cerr << "MediaPluginWebKit::receiveMessage: received message: \"" << message_string << "\"" << std::endl;
  87. LLPluginMessage message_in;
  88. if(message_in.parse(message_string) >= 0)
  89. {
  90. std::string message_class = message_in.getClass();
  91. std::string message_name = message_in.getName();
  92. if(message_class == LLPLUGIN_MESSAGE_CLASS_BASE)
  93. {
  94. if(message_name == "init")
  95. {
  96. LLPluginMessage message("base", "init_response");
  97. LLSD versions = LLSD::emptyMap();
  98. versions[LLPLUGIN_MESSAGE_CLASS_BASE] = LLPLUGIN_MESSAGE_CLASS_BASE_VERSION;
  99. versions[LLPLUGIN_MESSAGE_CLASS_MEDIA] = LLPLUGIN_MESSAGE_CLASS_MEDIA_VERSION;
  100. versions[LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER] = LLPLUGIN_MESSAGE_CLASS_MEDIA_BROWSER_VERSION;
  101. message.setValueLLSD("versions", versions);
  102. std::string plugin_version = "Example plugin 1.0..0";
  103. message.setValue("plugin_version", plugin_version);
  104. sendMessage(message);
  105. }
  106. else if(message_name == "idle")
  107. {
  108. // no response is necessary here.
  109. F64 time = message_in.getValueReal("time");
  110. // Convert time to milliseconds for update()
  111. update((int)(time * 1000.0f));
  112. }
  113. else if(message_name == "cleanup")
  114. {
  115. }
  116. else if(message_name == "shm_added")
  117. {
  118. SharedSegmentInfo info;
  119. info.mAddress = message_in.getValuePointer("address");
  120. info.mSize = (size_t)message_in.getValueS32("size");
  121. std::string name = message_in.getValue("name");
  122. mSharedSegments.insert(SharedSegmentMap::value_type(name, info));
  123. }
  124. else if(message_name == "shm_remove")
  125. {
  126. std::string name = message_in.getValue("name");
  127. SharedSegmentMap::iterator iter = mSharedSegments.find(name);
  128. if(iter != mSharedSegments.end())
  129. {
  130. if(mPixels == iter->second.mAddress)
  131. {
  132. // This is the currently active pixel buffer. Make sure we stop drawing to it.
  133. mPixels = NULL;
  134. mTextureSegmentName.clear();
  135. }
  136. mSharedSegments.erase(iter);
  137. }
  138. else
  139. {
  140. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown shared memory region!" << std::endl;
  141. }
  142. // Send the response so it can be cleaned up.
  143. LLPluginMessage message("base", "shm_remove_response");
  144. message.setValue("name", name);
  145. sendMessage(message);
  146. }
  147. else
  148. {
  149. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown base message: " << message_name << std::endl;
  150. }
  151. }
  152. else if(message_class == LLPLUGIN_MESSAGE_CLASS_MEDIA)
  153. {
  154. if(message_name == "init")
  155. {
  156. // Plugin gets to decide the texture parameters to use.
  157. mDepth = 4;
  158. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "texture_params");
  159. message.setValueS32("default_width", 1024);
  160. message.setValueS32("default_height", 1024);
  161. message.setValueS32("depth", mDepth);
  162. message.setValueU32("internalformat", GL_RGBA);
  163. message.setValueU32("format", GL_RGBA);
  164. message.setValueU32("type", GL_UNSIGNED_BYTE);
  165. message.setValueBoolean("coords_opengl", true);
  166. sendMessage(message);
  167. }
  168. else if(message_name == "size_change")
  169. {
  170. std::string name = message_in.getValue("name");
  171. S32 width = message_in.getValueS32("width");
  172. S32 height = message_in.getValueS32("height");
  173. S32 texture_width = message_in.getValueS32("texture_width");
  174. S32 texture_height = message_in.getValueS32("texture_height");
  175. if(!name.empty())
  176. {
  177. // Find the shared memory region with this name
  178. SharedSegmentMap::iterator iter = mSharedSegments.find(name);
  179. if(iter != mSharedSegments.end())
  180. {
  181. mPixels = (unsigned char*)iter->second.mAddress;
  182. mWidth = width;
  183. mHeight = height;
  184. mTextureWidth = texture_width;
  185. mTextureHeight = texture_height;
  186. };
  187. };
  188. LLPluginMessage message(LLPLUGIN_MESSAGE_CLASS_MEDIA, "size_change_response");
  189. message.setValue("name", name);
  190. message.setValueS32("width", width);
  191. message.setValueS32("height", height);
  192. message.setValueS32("texture_width", texture_width);
  193. message.setValueS32("texture_height", texture_height);
  194. sendMessage(message);
  195. }
  196. else if(message_name == "load_uri")
  197. {
  198. }
  199. else if(message_name == "mouse_event")
  200. {
  201. std::string event = message_in.getValue("event");
  202. if(event == "down")
  203. {
  204. }
  205. else if(event == "up")
  206. {
  207. }
  208. else if(event == "double_click")
  209. {
  210. }
  211. }
  212. }
  213. else
  214. {
  215. // std::cerr << "MediaPluginWebKit::receiveMessage: unknown message class: " << message_class << std::endl;
  216. };
  217. }
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////
  220. //
  221. void MediaPluginExample::write_pixel( int x, int y, unsigned char r, unsigned char g, unsigned char b )
  222. {
  223. // make sure we don't write outside the buffer
  224. if ( ( x < 0 ) || ( x >= mWidth ) || ( y < 0 ) || ( y >= mHeight ) )
  225. return;
  226. if ( mBackgroundPixels != NULL )
  227. {
  228. unsigned char *pixel = mBackgroundPixels;
  229. pixel += y * mWidth * mDepth;
  230. pixel += ( x * mDepth );
  231. pixel[ 0 ] = b;
  232. pixel[ 1 ] = g;
  233. pixel[ 2 ] = r;
  234. setDirty( x, y, x + 1, y + 1 );
  235. };
  236. }
  237. ////////////////////////////////////////////////////////////////////////////////
  238. //
  239. void MediaPluginExample::update( F64 milliseconds )
  240. {
  241. if ( mWidth < 1 || mWidth > 2048 || mHeight < 1 || mHeight > 2048 )
  242. return;
  243. if ( mPixels == 0 )
  244. return;
  245. if ( mFirstTime )
  246. {
  247. for( int n = 0; n < ENumObjects; ++n )
  248. {
  249. mXpos[ n ] = ( mWidth / 2 ) + rand() % ( mWidth / 16 ) - ( mWidth / 32 );
  250. mYpos[ n ] = ( mHeight / 2 ) + rand() % ( mHeight / 16 ) - ( mHeight / 32 );
  251. mColorR[ n ] = rand() % 0x60 + 0x60;
  252. mColorG[ n ] = rand() % 0x60 + 0x60;
  253. mColorB[ n ] = rand() % 0x60 + 0x60;
  254. mXInc[ n ] = 0;
  255. while ( mXInc[ n ] == 0 )
  256. mXInc[ n ] = rand() % 7 - 3;
  257. mYInc[ n ] = 0;
  258. while ( mYInc[ n ] == 0 )
  259. mYInc[ n ] = rand() % 9 - 4;
  260. mBlockSize[ n ] = rand() % 0x30 + 0x10;
  261. };
  262. delete [] mBackgroundPixels;
  263. mBackgroundPixels = new unsigned char[ mWidth * mHeight * mDepth ];
  264. mFirstTime = false;
  265. };
  266. if ( mStopAction )
  267. return;
  268. if ( time( NULL ) > mLastUpdateTime + 3 )
  269. {
  270. const int num_squares = rand() % 20 + 4;
  271. int sqr1_r = rand() % 0x80 + 0x20;
  272. int sqr1_g = rand() % 0x80 + 0x20;
  273. int sqr1_b = rand() % 0x80 + 0x20;
  274. int sqr2_r = rand() % 0x80 + 0x20;
  275. int sqr2_g = rand() % 0x80 + 0x20;
  276. int sqr2_b = rand() % 0x80 + 0x20;
  277. for ( int y1 = 0; y1 < num_squares; ++y1 )
  278. {
  279. for ( int x1 = 0; x1 < num_squares; ++x1 )
  280. {
  281. int px_start = mWidth * x1 / num_squares;
  282. int px_end = ( mWidth * ( x1 + 1 ) ) / num_squares;
  283. int py_start = mHeight * y1 / num_squares;
  284. int py_end = ( mHeight * ( y1 + 1 ) ) / num_squares;
  285. for( int y2 = py_start; y2 < py_end; ++y2 )
  286. {
  287. for( int x2 = px_start; x2 < px_end; ++x2 )
  288. {
  289. int rowspan = mWidth * mDepth;
  290. if ( ( y1 % 2 ) ^ ( x1 % 2 ) )
  291. {
  292. mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 0 ] = sqr1_r;
  293. mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 1 ] = sqr1_g;
  294. mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 2 ] = sqr1_b;
  295. }
  296. else
  297. {
  298. mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 0 ] = sqr2_r;
  299. mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 1 ] = sqr2_g;
  300. mBackgroundPixels[ y2 * rowspan + x2 * mDepth + 2 ] = sqr2_b;
  301. };
  302. };
  303. };
  304. };
  305. };
  306. time( &mLastUpdateTime );
  307. };
  308. memcpy( mPixels, mBackgroundPixels, mWidth * mHeight * mDepth );
  309. for( int n = 0; n < ENumObjects; ++n )
  310. {
  311. if ( rand() % 50 == 0 )
  312. {
  313. mXInc[ n ] = 0;
  314. while ( mXInc[ n ] == 0 )
  315. mXInc[ n ] = rand() % 7 - 3;
  316. mYInc[ n ] = 0;
  317. while ( mYInc[ n ] == 0 )
  318. mYInc[ n ] = rand() % 9 - 4;
  319. };
  320. if ( mXpos[ n ] + mXInc[ n ] < 0 || mXpos[ n ] + mXInc[ n ] >= mWidth - mBlockSize[ n ] )
  321. mXInc[ n ] =- mXInc[ n ];
  322. if ( mYpos[ n ] + mYInc[ n ] < 0 || mYpos[ n ] + mYInc[ n ] >= mHeight - mBlockSize[ n ] )
  323. mYInc[ n ] =- mYInc[ n ];
  324. mXpos[ n ] += mXInc[ n ];
  325. mYpos[ n ] += mYInc[ n ];
  326. for( int y = 0; y < mBlockSize[ n ]; ++y )
  327. {
  328. for( int x = 0; x < mBlockSize[ n ]; ++x )
  329. {
  330. mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 0 ] = mColorR[ n ];
  331. mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 1 ] = mColorG[ n ];
  332. mPixels[ ( mXpos[ n ] + x ) * mDepth + ( mYpos[ n ] + y ) * mDepth * mWidth + 2 ] = mColorB[ n ];
  333. };
  334. };
  335. };
  336. setDirty( 0, 0, mWidth, mHeight );
  337. };
  338. ////////////////////////////////////////////////////////////////////////////////
  339. //
  340. bool MediaPluginExample::init()
  341. {
  342. LLPluginMessage message( LLPLUGIN_MESSAGE_CLASS_MEDIA, "name_text" );
  343. message.setValue( "name", "Example Plugin" );
  344. sendMessage( message );
  345. return true;
  346. };
  347. ////////////////////////////////////////////////////////////////////////////////
  348. //
  349. int init_media_plugin( LLPluginInstance::sendMessageFunction host_send_func,
  350. void* host_user_data,
  351. LLPluginInstance::sendMessageFunction *plugin_send_func,
  352. void **plugin_user_data )
  353. {
  354. MediaPluginExample* self = new MediaPluginExample( host_send_func, host_user_data );
  355. *plugin_send_func = MediaPluginExample::staticReceiveMessage;
  356. *plugin_user_data = ( void* )self;
  357. return 0;
  358. }