ProxyConsumer.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. /**@file ProxyConsumer.c
  23. * @brief Audio/Video proxy consumers.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango.org>
  26. *
  27. */
  28. #include "ProxyConsumer.h"
  29. #include "AudioResampler.h"
  30. #include "tsk_memory.h"
  31. #include "tsk_debug.h"
  32. #include "tinydav/audio/tdav_consumer_audio.h"
  33. #include "tinydav/video/tdav_consumer_video.h"
  34. /* ============ Audio Consumer Interface ================= */
  35. typedef struct twrap_consumer_proxy_audio_s {
  36. TDAV_DECLARE_CONSUMER_AUDIO;
  37. uint64_t id;
  38. tsk_bool_t started;
  39. const ProxyAudioConsumer* pcConsumer; // thread-safe and will be destroyed at the time as the "struct"
  40. }
  41. twrap_consumer_proxy_audio_t;
  42. #define TWRAP_CONSUMER_PROXY_AUDIO(self) ((twrap_consumer_proxy_audio_t*)(self))
  43. int twrap_consumer_proxy_audio_set(tmedia_consumer_t* _self, const tmedia_param_t* param)
  44. {
  45. twrap_consumer_proxy_audio_t* self = (twrap_consumer_proxy_audio_t*)_self;
  46. if(param->plugin_type == tmedia_ppt_consumer) {
  47. // specific proxy consumer
  48. }
  49. return tdav_consumer_audio_set(TDAV_CONSUMER_AUDIO(self), param);
  50. }
  51. int twrap_consumer_proxy_audio_prepare(tmedia_consumer_t* self, const tmedia_codec_t* codec)
  52. {
  53. twrap_consumer_proxy_audio_t* audio = TWRAP_CONSUMER_PROXY_AUDIO(self);
  54. ProxyPluginMgr* manager = NULL;
  55. int ret = -1;
  56. if(codec && (manager = ProxyPluginMgr::getInstance())) {
  57. if((audio->pcConsumer = manager->findAudioConsumer(audio->id)) && audio->pcConsumer->getCallback()) {
  58. self->audio.ptime = TMEDIA_CODEC_PTIME_AUDIO_DECODING(codec);
  59. self->audio.in.channels = TMEDIA_CODEC_CHANNELS_AUDIO_DECODING(codec);
  60. self->audio.in.rate = TMEDIA_CODEC_RATE_DECODING(codec);
  61. ret = audio->pcConsumer->getCallback()->prepare((int)self->audio.ptime, self->audio.in.rate, self->audio.in.channels);
  62. if(ret == 0 && !audio->pcConsumer->getCallback()->isPivotSettings()) {
  63. // say consumer can output these params
  64. // Out "rate" and "channels" must be defined regardless previous values (already the case in other back-ends) to avoid issues on reINVITE with rate change (e.g. Opus -> PCMA).
  65. /*if(!self->audio.out.rate)*/ self->audio.out.rate = self->audio.in.rate;
  66. /*if(!self->audio.out.channels)*/ self->audio.out.channels = self->audio.in.channels;
  67. }
  68. }
  69. }
  70. else {
  71. TSK_DEBUG_ERROR("Invalid parameter/state: codec=%d, manager=%s", codec, manager ? "no-null" : "null");
  72. }
  73. return ret;
  74. }
  75. int twrap_consumer_proxy_audio_start(tmedia_consumer_t* self)
  76. {
  77. ProxyPluginMgr* manager;
  78. int ret = -1;
  79. if((manager = ProxyPluginMgr::getInstance())) {
  80. const ProxyAudioConsumer* audioConsumer;
  81. if((audioConsumer = manager->findAudioConsumer(TWRAP_CONSUMER_PROXY_AUDIO(self)->id)) && audioConsumer->getCallback()) {
  82. ret = audioConsumer->getCallback()->start();
  83. }
  84. }
  85. TWRAP_CONSUMER_PROXY_AUDIO(self)->started = (ret == 0);
  86. return ret;
  87. }
  88. int twrap_consumer_proxy_audio_consume(tmedia_consumer_t* self, const void* buffer, tsk_size_t size, const tsk_object_t* proto_hdr)
  89. {
  90. twrap_consumer_proxy_audio_t* audio = TWRAP_CONSUMER_PROXY_AUDIO(self);
  91. if(!audio->pcConsumer) {
  92. ProxyPluginMgr* manager;
  93. if((manager = ProxyPluginMgr::getInstance())) {
  94. audio->pcConsumer = manager->findAudioConsumer(audio->id);
  95. }
  96. }
  97. ProxyAudioConsumerCallback* callback;
  98. int ret = -1;
  99. if(audio->pcConsumer && (callback = audio->pcConsumer->getCallback())) {
  100. if(callback->putInJitterBuffer()) {
  101. ret = tdav_consumer_audio_put(TDAV_CONSUMER_AUDIO(self), buffer, size, proto_hdr);
  102. }
  103. else {
  104. ret = callback->consume(buffer, size, proto_hdr);
  105. }
  106. }
  107. return ret;
  108. }
  109. int twrap_consumer_proxy_audio_pause(tmedia_consumer_t* self)
  110. {
  111. ProxyPluginMgr* manager;
  112. int ret = -1;
  113. if((manager = ProxyPluginMgr::getInstance())) {
  114. const ProxyAudioConsumer* audioConsumer;
  115. if((audioConsumer = manager->findAudioConsumer(TWRAP_CONSUMER_PROXY_AUDIO(self)->id)) && audioConsumer->getCallback()) {
  116. ret = audioConsumer->getCallback()->pause();
  117. }
  118. }
  119. return ret;
  120. }
  121. int twrap_consumer_proxy_audio_stop(tmedia_consumer_t* self)
  122. {
  123. ProxyPluginMgr* manager;
  124. int ret = -1;
  125. if((manager = ProxyPluginMgr::getInstance())) {
  126. const ProxyAudioConsumer* audioConsumer;
  127. if((audioConsumer = manager->findAudioConsumer(TWRAP_CONSUMER_PROXY_AUDIO(self)->id)) && audioConsumer->getCallback()) {
  128. ret = audioConsumer->getCallback()->stop();
  129. }
  130. }
  131. TWRAP_CONSUMER_PROXY_AUDIO(self)->started = (ret == 0) ? tsk_false : tsk_true;
  132. return ret;
  133. }
  134. //
  135. // Audio consumer object definition
  136. //
  137. /* constructor */
  138. static tsk_object_t* twrap_consumer_proxy_audio_ctor(tsk_object_t * self, va_list * app)
  139. {
  140. twrap_consumer_proxy_audio_t *consumer = (twrap_consumer_proxy_audio_t *)self;
  141. if(consumer) {
  142. /* init base */
  143. tdav_consumer_audio_init(TDAV_CONSUMER_AUDIO(consumer));
  144. /* init self */
  145. /* Add the plugin to the manager */
  146. ProxyPluginMgr* manager = ProxyPluginMgr::getInstance();
  147. if(manager) {
  148. ProxyPlugin* proxyConsumer = new ProxyAudioConsumer(consumer);
  149. uint64_t id = proxyConsumer->getId();
  150. manager->addPlugin(&proxyConsumer);
  151. manager->getCallback()->OnPluginCreated(id, twrap_proxy_plugin_audio_consumer);
  152. }
  153. }
  154. return self;
  155. }
  156. /* destructor */
  157. static tsk_object_t* twrap_consumer_proxy_audio_dtor(tsk_object_t * self)
  158. {
  159. twrap_consumer_proxy_audio_t *consumer = (twrap_consumer_proxy_audio_t *)self;
  160. if(consumer) {
  161. /* stop */
  162. if(consumer->started) {
  163. twrap_consumer_proxy_audio_stop(TMEDIA_CONSUMER(consumer));
  164. }
  165. /* deinit base */
  166. tdav_consumer_audio_deinit(TDAV_CONSUMER_AUDIO(consumer));
  167. /* deinit self */
  168. /* Remove plugin from the manager */
  169. ProxyPluginMgr* manager = ProxyPluginMgr::getInstance();
  170. if(manager) {
  171. manager->getCallback()->OnPluginDestroyed(consumer->id, twrap_proxy_plugin_audio_consumer);
  172. manager->removePlugin(consumer->id);
  173. }
  174. }
  175. return self;
  176. }
  177. /* object definition */
  178. static const tsk_object_def_t twrap_consumer_proxy_audio_def_s = {
  179. sizeof(twrap_consumer_proxy_audio_t),
  180. twrap_consumer_proxy_audio_ctor,
  181. twrap_consumer_proxy_audio_dtor,
  182. tdav_consumer_audio_cmp,
  183. };
  184. /* plugin definition*/
  185. static const tmedia_consumer_plugin_def_t twrap_consumer_proxy_audio_plugin_def_s = {
  186. &twrap_consumer_proxy_audio_def_s,
  187. tmedia_audio,
  188. "Audio Proxy Consumer",
  189. twrap_consumer_proxy_audio_set,
  190. twrap_consumer_proxy_audio_prepare,
  191. twrap_consumer_proxy_audio_start,
  192. twrap_consumer_proxy_audio_consume,
  193. twrap_consumer_proxy_audio_pause,
  194. twrap_consumer_proxy_audio_stop
  195. };
  196. /*TINYWRAP_GEXTERN*/ const tmedia_consumer_plugin_def_t *twrap_consumer_proxy_audio_plugin_def_t = &twrap_consumer_proxy_audio_plugin_def_s;
  197. /* ============ ProxyAudioConsumer Class ================= */
  198. ProxyAudioConsumer::ProxyAudioConsumer(twrap_consumer_proxy_audio_t* pConsumer)
  199. :ProxyPlugin(twrap_proxy_plugin_audio_consumer),
  200. m_pWrappedPlugin(pConsumer),
  201. m_pCallback(tsk_null)
  202. {
  203. memset(&m_PullBuffer, 0, sizeof(m_PullBuffer));
  204. memset(&m_Resampler, 0, sizeof(m_Resampler));
  205. if(m_pWrappedPlugin) {
  206. m_pWrappedPlugin->id = this->getId();
  207. }
  208. }
  209. ProxyAudioConsumer::~ProxyAudioConsumer()
  210. {
  211. TSK_FREE(m_Resampler.pInBufferPtr);
  212. m_Resampler.nInBufferSizeInByte = 0;
  213. if(m_Resampler.pResampler) {
  214. delete m_Resampler.pResampler, m_Resampler.pResampler = tsk_null;
  215. }
  216. }
  217. // Use this function to request resampling when your sound card can't honor negotaited record parameters
  218. bool ProxyAudioConsumer::setActualSndCardPlaybackParams(int nPtime, int nRate, int nChannels)
  219. {
  220. if(m_pWrappedPlugin) {
  221. TSK_DEBUG_INFO("ProxyAudioConsumer::setActualSndCardRecordParams(ptime=%d, rate=%d, channels=%d)", nPtime, nRate, nChannels);
  222. TMEDIA_CONSUMER(m_pWrappedPlugin)->audio.ptime = nPtime;
  223. TMEDIA_CONSUMER(m_pWrappedPlugin)->audio.out.rate = nRate;
  224. TMEDIA_CONSUMER(m_pWrappedPlugin)->audio.out.channels = nChannels;
  225. return true;
  226. }
  227. else {
  228. TSK_DEBUG_ERROR("Invalid state");
  229. return false;
  230. }
  231. }
  232. bool ProxyAudioConsumer::queryForResampler(uint16_t nInFreq, uint16_t nOutFreq, uint16_t nFrameDuration, uint16_t nChannels, uint16_t nResamplerQuality)
  233. {
  234. TSK_DEBUG_INFO("queryForResampler(%hu,%hu,%hu,%hu,%hu)", nInFreq, nOutFreq, nFrameDuration, nChannels, nResamplerQuality);
  235. if(nResamplerQuality > 10) {
  236. TSK_DEBUG_WARN("%d is invalid value for quality", nResamplerQuality);
  237. }
  238. m_Resampler.pResampler = new AudioResampler(nInFreq, nOutFreq, nFrameDuration, nChannels, nResamplerQuality);
  239. if(!m_Resampler.pResampler) {
  240. TSK_DEBUG_ERROR("Failed to create new 'AudioResampler' object");
  241. return false;
  242. }
  243. bool bOK = m_Resampler.pResampler->isValid();
  244. if(!bOK) {
  245. goto bail;
  246. }
  247. m_Resampler.nInBufferSizeInByte = m_Resampler.pResampler->getInputRequiredSizeInShort() * 2;
  248. m_Resampler.pInBufferPtr = tsk_calloc(m_Resampler.nInBufferSizeInByte, 1);
  249. bOK = (m_Resampler.pInBufferPtr != tsk_null);
  250. bail:
  251. if(!bOK) {
  252. if(m_Resampler.pResampler) {
  253. delete m_Resampler.pResampler, m_Resampler.pResampler = tsk_null;
  254. }
  255. TSK_FREE(m_Resampler.pInBufferPtr);
  256. m_Resampler.nInBufferSizeInByte = 0;
  257. }
  258. return bOK;
  259. }
  260. bool ProxyAudioConsumer::setPullBuffer(const void* pPullBufferPtr, unsigned nPullBufferSize)
  261. {
  262. m_PullBuffer.pPullBufferPtr = pPullBufferPtr;
  263. m_PullBuffer.nPullBufferSize = nPullBufferSize;
  264. return true;
  265. }
  266. unsigned ProxyAudioConsumer::pull(void* _pOutput/*=tsk_null*/, unsigned _nSize/*=0*/)
  267. {
  268. if((m_pWrappedPlugin = (twrap_consumer_proxy_audio_t*)tsk_object_ref(m_pWrappedPlugin))) {
  269. void* pOutput;
  270. unsigned nSize;
  271. if(_pOutput && _nSize) {
  272. pOutput = _pOutput, nSize = _nSize;
  273. }
  274. else {
  275. pOutput = (void*)m_PullBuffer.pPullBufferPtr, nSize = m_PullBuffer.nPullBufferSize;
  276. }
  277. tsk_size_t nRetSize = 0;
  278. if(m_Resampler.pResampler && m_Resampler.pInBufferPtr) {
  279. nRetSize = tdav_consumer_audio_get(TDAV_CONSUMER_AUDIO(m_pWrappedPlugin), m_Resampler.pInBufferPtr, m_Resampler.nInBufferSizeInByte);
  280. if(nRetSize) {
  281. nRetSize = m_Resampler.pResampler->process(m_Resampler.pInBufferPtr, nRetSize, pOutput, nSize);
  282. }
  283. }
  284. else {
  285. nRetSize = tdav_consumer_audio_get(TDAV_CONSUMER_AUDIO(m_pWrappedPlugin), pOutput, nSize);
  286. }
  287. tdav_consumer_audio_tick(TDAV_CONSUMER_AUDIO(m_pWrappedPlugin));
  288. m_pWrappedPlugin = (twrap_consumer_proxy_audio_t*)tsk_object_unref(m_pWrappedPlugin);
  289. return nRetSize;
  290. }
  291. return 0;
  292. }
  293. bool ProxyAudioConsumer::setGain(unsigned nGain)
  294. {
  295. if(m_pWrappedPlugin) {
  296. // see also: MediaSessionMgr.consumerSetInt32(org.doubango.tinyWRAP.twrap_media_type_t.twrap_media_audio, "gain", nGain);
  297. TMEDIA_CONSUMER(m_pWrappedPlugin)->audio.gain = TSK_MIN(nGain,14);
  298. return true;
  299. }
  300. return false;
  301. }
  302. unsigned ProxyAudioConsumer::getGain()
  303. {
  304. if(m_pWrappedPlugin) {
  305. return TMEDIA_CONSUMER(m_pWrappedPlugin)->audio.gain;
  306. }
  307. return 0;
  308. }
  309. bool ProxyAudioConsumer::reset()
  310. {
  311. if(m_pWrappedPlugin) {
  312. return (tdav_consumer_audio_reset(TDAV_CONSUMER_AUDIO(m_pWrappedPlugin)) == 0);
  313. }
  314. return false;
  315. }
  316. bool ProxyAudioConsumer::registerPlugin()
  317. {
  318. /* HACK: Unregister all other audio plugins */
  319. tmedia_consumer_plugin_unregister_by_type(tmedia_audio);
  320. /* Register our proxy plugin */
  321. return (tmedia_consumer_plugin_register(twrap_consumer_proxy_audio_plugin_def_t) == 0);
  322. }
  323. /* ============ Video Consumer Interface ================= */
  324. typedef struct twrap_consumer_proxy_video_s {
  325. TDAV_DECLARE_CONSUMER_VIDEO;
  326. uint64_t id;
  327. tsk_bool_t started;
  328. const ProxyVideoConsumer* pcConsumer; // thread-safe and will be destroyed at the time as the "struct"
  329. }
  330. twrap_consumer_proxy_video_t;
  331. #define TWRAP_CONSUMER_PROXY_VIDEO(self) ((twrap_consumer_proxy_video_t*)(self))
  332. int twrap_consumer_proxy_video_set(tmedia_consumer_t* self, const tmedia_param_t* params)
  333. {
  334. return 0;
  335. }
  336. int twrap_consumer_proxy_video_prepare(tmedia_consumer_t* self, const tmedia_codec_t* codec)
  337. {
  338. ProxyPluginMgr* manager;
  339. twrap_consumer_proxy_video_t* video = TWRAP_CONSUMER_PROXY_VIDEO(self);
  340. int ret = -1;
  341. if(codec && (manager = ProxyPluginMgr::getInstance())) {
  342. if((video->pcConsumer = manager->findVideoConsumer(video->id)) && video->pcConsumer->getCallback()) {
  343. self->video.fps = TMEDIA_CODEC_VIDEO(codec)->in.fps;
  344. // in
  345. self->video.in.chroma = tmedia_chroma_yuv420p;
  346. self->video.in.width = TMEDIA_CODEC_VIDEO(codec)->in.width;
  347. self->video.in.height = TMEDIA_CODEC_VIDEO(codec)->in.height;
  348. // display (out)
  349. self->video.display.chroma = video->pcConsumer->getChroma();
  350. self->video.display.auto_resize = video->pcConsumer->getAutoResizeDisplay();
  351. if(!self->video.display.width) {
  352. self->video.display.width = self->video.in.width;
  353. }
  354. if(!self->video.display.height) {
  355. self->video.display.height = self->video.in.height;
  356. }
  357. ret = video->pcConsumer->getCallback()->prepare(TMEDIA_CODEC_VIDEO(codec)->in.width, TMEDIA_CODEC_VIDEO(codec)->in.height, TMEDIA_CODEC_VIDEO(codec)->in.fps);
  358. }
  359. }
  360. return ret;
  361. }
  362. int twrap_consumer_proxy_video_start(tmedia_consumer_t* self)
  363. {
  364. ProxyPluginMgr* manager;
  365. int ret = -1;
  366. if((manager = ProxyPluginMgr::getInstance())) {
  367. const ProxyVideoConsumer* videoConsumer;
  368. if((videoConsumer = manager->findVideoConsumer(TWRAP_CONSUMER_PROXY_VIDEO(self)->id)) && videoConsumer->getCallback()) {
  369. ret = videoConsumer->getCallback()->start();
  370. }
  371. }
  372. TWRAP_CONSUMER_PROXY_VIDEO(self)->started = (ret == 0);
  373. return ret;
  374. }
  375. int twrap_consumer_proxy_video_consume(tmedia_consumer_t* self, const void* buffer, tsk_size_t size, const tsk_object_t* proto_hdr)
  376. {
  377. if(!self || !buffer || !size) {
  378. TSK_DEBUG_ERROR("Invalid parameter");
  379. return -1;
  380. }
  381. twrap_consumer_proxy_video_t* video = TWRAP_CONSUMER_PROXY_VIDEO(self);
  382. if(!video->pcConsumer) {
  383. ProxyPluginMgr* manager;
  384. if((manager = ProxyPluginMgr::getInstance())) {
  385. video->pcConsumer = manager->findVideoConsumer(video->id);
  386. }
  387. }
  388. int ret = -1;
  389. ProxyVideoConsumerCallback* callback;
  390. if(video->pcConsumer && (callback = video->pcConsumer->getCallback())) {
  391. if(tdav_consumer_video_has_jb(TDAV_CONSUMER_VIDEO(self))) {
  392. ret = tdav_consumer_video_put(TDAV_CONSUMER_VIDEO(self), buffer, size, proto_hdr);
  393. }
  394. else {
  395. if(video->pcConsumer->hasConsumeBuffer()) {
  396. unsigned nCopiedSize = video->pcConsumer->copyBuffer(buffer, size);
  397. ret = callback->bufferCopied(nCopiedSize, size);
  398. }
  399. else {
  400. ProxyVideoFrame* frame = new ProxyVideoFrame(buffer, size, const_cast<ProxyVideoConsumer*>(video->pcConsumer)->getDecodedWidth(), const_cast<ProxyVideoConsumer*>(video->pcConsumer)->getDecodedHeight(), proto_hdr);
  401. ret = callback->consume(frame);
  402. delete frame, frame = tsk_null;
  403. }
  404. }
  405. }
  406. else if(!video->pcConsumer) {
  407. TSK_DEBUG_ERROR("Cannot find consumer with id=%lld", TWRAP_CONSUMER_PROXY_VIDEO(self)->id);
  408. }
  409. return ret;
  410. }
  411. int twrap_consumer_proxy_video_pause(tmedia_consumer_t* self)
  412. {
  413. ProxyPluginMgr* manager;
  414. int ret = -1;
  415. if((manager = ProxyPluginMgr::getInstance())) {
  416. const ProxyVideoConsumer* videoConsumer;
  417. if((videoConsumer = manager->findVideoConsumer(TWRAP_CONSUMER_PROXY_VIDEO(self)->id)) && videoConsumer->getCallback()) {
  418. ret = videoConsumer->getCallback()->pause();
  419. }
  420. }
  421. return ret;
  422. }
  423. int twrap_consumer_proxy_video_stop(tmedia_consumer_t* self)
  424. {
  425. ProxyPluginMgr* manager;
  426. int ret = -1;
  427. if((manager = ProxyPluginMgr::getInstance())) {
  428. const ProxyVideoConsumer* videoConsumer;
  429. if((videoConsumer = manager->findVideoConsumer(TWRAP_CONSUMER_PROXY_VIDEO(self)->id)) && videoConsumer->getCallback()) {
  430. ret = videoConsumer->getCallback()->stop();
  431. }
  432. }
  433. TWRAP_CONSUMER_PROXY_VIDEO(self)->started = (ret == 0) ? tsk_false : tsk_true;
  434. return ret;
  435. }
  436. //
  437. // Video consumer object definition
  438. //
  439. /* constructor */
  440. static tsk_object_t* twrap_consumer_proxy_video_ctor(tsk_object_t * self, va_list * app)
  441. {
  442. twrap_consumer_proxy_video_t *consumer = (twrap_consumer_proxy_video_t *)self;
  443. if(consumer) {
  444. /* init base */
  445. tdav_consumer_video_init(TDAV_CONSUMER_VIDEO(consumer));
  446. /* init self */
  447. /* Add the plugin to the manager */
  448. ProxyPluginMgr* manager = ProxyPluginMgr::getInstance();
  449. if(manager) {
  450. ProxyPlugin* proxyConsumer = new ProxyVideoConsumer(ProxyVideoConsumer::getDefaultChroma(), consumer);
  451. uint64_t id = proxyConsumer->getId();
  452. manager->addPlugin(&proxyConsumer);
  453. manager->getCallback()->OnPluginCreated(id, twrap_proxy_plugin_video_consumer);
  454. }
  455. }
  456. return self;
  457. }
  458. /* destructor */
  459. static tsk_object_t* twrap_consumer_proxy_video_dtor(tsk_object_t * self)
  460. {
  461. twrap_consumer_proxy_video_t *consumer = (twrap_consumer_proxy_video_t *)self;
  462. if(consumer) {
  463. /* stop */
  464. if(consumer->started) {
  465. twrap_consumer_proxy_video_stop(TMEDIA_CONSUMER(consumer));
  466. }
  467. /* deinit base */
  468. tdav_consumer_video_deinit(TDAV_CONSUMER_VIDEO(consumer));
  469. /* deinit self */
  470. /* Remove plugin from the manager */
  471. ProxyPluginMgr* manager = ProxyPluginMgr::getInstance();
  472. if(manager) {
  473. manager->getCallback()->OnPluginDestroyed(consumer->id, twrap_proxy_plugin_video_consumer);
  474. manager->removePlugin(consumer->id);
  475. }
  476. }
  477. return self;
  478. }
  479. /* object definition */
  480. static const tsk_object_def_t twrap_consumer_proxy_video_def_s = {
  481. sizeof(twrap_consumer_proxy_video_t),
  482. twrap_consumer_proxy_video_ctor,
  483. twrap_consumer_proxy_video_dtor,
  484. tsk_null,
  485. };
  486. /* plugin definition*/
  487. static const tmedia_consumer_plugin_def_t twrap_consumer_proxy_video_plugin_def_s = {
  488. &twrap_consumer_proxy_video_def_s,
  489. tmedia_video,
  490. "Video Proxy Consumer",
  491. twrap_consumer_proxy_video_set,
  492. twrap_consumer_proxy_video_prepare,
  493. twrap_consumer_proxy_video_start,
  494. twrap_consumer_proxy_video_consume,
  495. twrap_consumer_proxy_video_pause,
  496. twrap_consumer_proxy_video_stop
  497. };
  498. /*TINYWRAP_GEXTERN*/ const tmedia_consumer_plugin_def_t *twrap_consumer_proxy_video_plugin_def_t = &twrap_consumer_proxy_video_plugin_def_s;
  499. /* ============ ProxyVideoConsumer Class ================= */
  500. tmedia_chroma_t ProxyVideoConsumer::s_eDefaultChroma = tmedia_chroma_rgb565le;
  501. bool ProxyVideoConsumer::s_bAutoResizeDisplay = false;
  502. ProxyVideoConsumer::ProxyVideoConsumer(tmedia_chroma_t eChroma, struct twrap_consumer_proxy_video_s* pConsumer)
  503. : m_eChroma(eChroma),
  504. m_bAutoResizeDisplay(ProxyVideoConsumer::getDefaultAutoResizeDisplay()),
  505. m_pWrappedPlugin(pConsumer),
  506. m_pCallback(tsk_null),
  507. ProxyPlugin(twrap_proxy_plugin_video_consumer)
  508. {
  509. m_pWrappedPlugin->id = this->getId();
  510. m_ConsumeBuffer.pConsumeBufferPtr = tsk_null;
  511. m_ConsumeBuffer.nConsumeBufferSize = 0;
  512. }
  513. ProxyVideoConsumer::~ProxyVideoConsumer()
  514. {
  515. }
  516. bool ProxyVideoConsumer::setDisplaySize(unsigned nWidth, unsigned nHeight)
  517. {
  518. if((m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  519. TMEDIA_CONSUMER(m_pWrappedPlugin)->video.display.width = nWidth;
  520. TMEDIA_CONSUMER(m_pWrappedPlugin)->video.display.height = nHeight;
  521. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  522. return true;
  523. }
  524. TSK_DEBUG_ERROR("This consumer doesn't wrap any plugin");
  525. return false;
  526. }
  527. unsigned ProxyVideoConsumer::getDisplayWidth()
  528. {
  529. unsigned displayWidth = 0;
  530. if((m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  531. displayWidth = TMEDIA_CONSUMER(m_pWrappedPlugin)->video.display.width;
  532. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  533. }
  534. else {
  535. TSK_DEBUG_ERROR("This consumer doesn't wrap any plugin");
  536. }
  537. return displayWidth;
  538. }
  539. unsigned ProxyVideoConsumer::getDisplayHeight()
  540. {
  541. unsigned displayHeight = 0;
  542. if((m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  543. displayHeight = TMEDIA_CONSUMER(m_pWrappedPlugin)->video.display.height;
  544. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  545. }
  546. else {
  547. TSK_DEBUG_ERROR("This consumer doesn't wrap any plugin");
  548. }
  549. return displayHeight;
  550. }
  551. unsigned ProxyVideoConsumer::getDecodedWidth()
  552. {
  553. unsigned width = 0;
  554. if((m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  555. width = TMEDIA_CONSUMER(m_pWrappedPlugin)->video.in.width;
  556. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  557. }
  558. else {
  559. TSK_DEBUG_ERROR("This consumer doesn't wrap any plugin");
  560. }
  561. return width;
  562. }
  563. unsigned ProxyVideoConsumer::getDecodedHeight()
  564. {
  565. unsigned height = 0;
  566. if((m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  567. height = TMEDIA_CONSUMER(m_pWrappedPlugin)->video.in.height;
  568. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  569. }
  570. else {
  571. TSK_DEBUG_ERROR("This consumer doesn't wrap any plugin");
  572. }
  573. return height;
  574. }
  575. tmedia_chroma_t ProxyVideoConsumer::getChroma()const
  576. {
  577. return m_eChroma;
  578. }
  579. bool ProxyVideoConsumer::setAutoResizeDisplay(bool bAutoResizeDisplay)
  580. {
  581. if((m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  582. TMEDIA_CONSUMER(m_pWrappedPlugin)->video.display.auto_resize = bAutoResizeDisplay ? tsk_true : tsk_false;
  583. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  584. m_bAutoResizeDisplay = bAutoResizeDisplay;
  585. return true;
  586. }
  587. TSK_DEBUG_ERROR("This consumer doesn't wrap any plugin");
  588. return false;
  589. }
  590. bool ProxyVideoConsumer::getAutoResizeDisplay()const
  591. {
  592. return m_bAutoResizeDisplay;
  593. }
  594. bool ProxyVideoConsumer::setConsumeBuffer(const void* pConsumeBufferPtr, unsigned nConsumeBufferSize)
  595. {
  596. m_ConsumeBuffer.pConsumeBufferPtr = pConsumeBufferPtr;
  597. m_ConsumeBuffer.nConsumeBufferSize = nConsumeBufferSize;
  598. return true;
  599. }
  600. unsigned ProxyVideoConsumer::copyBuffer(const void* pBuffer, unsigned nSize)const
  601. {
  602. unsigned nRetsize = 0;
  603. if(pBuffer && nSize && m_ConsumeBuffer.pConsumeBufferPtr && m_ConsumeBuffer.nConsumeBufferSize) {
  604. nRetsize = (nSize > m_ConsumeBuffer.nConsumeBufferSize) ? m_ConsumeBuffer.nConsumeBufferSize : nSize;
  605. memcpy((void*)m_ConsumeBuffer.pConsumeBufferPtr, pBuffer, nRetsize);
  606. }
  607. return nRetsize;
  608. }
  609. unsigned ProxyVideoConsumer::pull(void* pOutput, unsigned nSize)
  610. {
  611. if(pOutput && nSize && (m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  612. tsk_size_t nRetSize = 0;
  613. if(!tdav_consumer_video_has_jb(TDAV_CONSUMER_VIDEO(m_pWrappedPlugin))) {
  614. TSK_DEBUG_ERROR("This consumer doesn't hold any jitter buffer.\n\nTo pull a buffer you must register a callback ('class ProxyVideoConsumerCallback') and listen for either 'consume' or 'bufferCopied' functions");
  615. goto done;
  616. }
  617. nRetSize = tdav_consumer_video_get(TDAV_CONSUMER_VIDEO(m_pWrappedPlugin), pOutput, nSize);
  618. tdav_consumer_video_tick(TDAV_CONSUMER_VIDEO(m_pWrappedPlugin));
  619. done:
  620. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  621. return nRetSize;
  622. }
  623. return 0;
  624. }
  625. bool ProxyVideoConsumer::reset()
  626. {
  627. bool ret = false;
  628. if((m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_ref(m_pWrappedPlugin))) {
  629. if(tdav_consumer_video_has_jb(TDAV_CONSUMER_VIDEO(m_pWrappedPlugin))) {
  630. ret = (tdav_consumer_video_reset(TDAV_CONSUMER_VIDEO(m_pWrappedPlugin)) == 0);
  631. }
  632. else {
  633. TSK_DEBUG_ERROR("This consumer doesn't hold any jitter buffer");
  634. }
  635. m_pWrappedPlugin = (twrap_consumer_proxy_video_t*)tsk_object_unref(m_pWrappedPlugin);
  636. }
  637. TSK_DEBUG_ERROR("This consumer doesn't wrap any plugin");
  638. return ret;
  639. }
  640. bool ProxyVideoConsumer::registerPlugin()
  641. {
  642. /* HACK: Unregister all other video plugins */
  643. tmedia_consumer_plugin_unregister_by_type(tmedia_video);
  644. /* Register our proxy plugin */
  645. return (tmedia_consumer_plugin_register(twrap_consumer_proxy_video_plugin_def_t) == 0);
  646. }
  647. ProxyVideoFrame::ProxyVideoFrame(const void* pBufferPtr, unsigned nSize, unsigned nFrameWidth, unsigned nFrameHeight, const tsk_object_t* pProtoHdr)
  648. {
  649. m_pBufferPtr = pBufferPtr;
  650. m_nBufferSize = nSize;
  651. m_nFrameWidth = nFrameWidth;
  652. m_nFrameHeight = nFrameHeight;
  653. m_pProtoHdr = pProtoHdr;
  654. }
  655. ProxyVideoFrame::~ProxyVideoFrame()
  656. {
  657. }
  658. unsigned ProxyVideoFrame::getSize()
  659. {
  660. return m_nBufferSize;
  661. }
  662. unsigned ProxyVideoFrame::getContent(void* pOutput, unsigned nMaxsize)
  663. {
  664. unsigned nRetsize = 0;
  665. if(pOutput && nMaxsize && m_pBufferPtr) {
  666. nRetsize = (m_nBufferSize > nMaxsize) ? nMaxsize : m_nBufferSize;
  667. memcpy(pOutput, m_pBufferPtr, nRetsize);
  668. }
  669. return nRetsize;
  670. }