UIVideoPlayer-tizen.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "ui/UIVideoPlayer.h"
  22. #if (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)
  23. #include <stdlib.h>
  24. #include <string>
  25. #include "base/CCDirector.h"
  26. #include "base/CCEventListenerKeyboard.h"
  27. #include "player.h"
  28. #include "platform/tizen/CCApplication-tizen.h"
  29. #include <app.h>
  30. #include <camera.h>
  31. #include <efl_extension.h>
  32. //-----------------------------------------------------------------------------------------------------------
  33. USING_NS_CC;
  34. #define QUIT_FULLSCREEN 1000
  35. using namespace cocos2d::experimental::ui;
  36. class _VideoPlayerTizen
  37. {
  38. public:
  39. _VideoPlayerTizen(VideoPlayer* videoPlayer)
  40. {
  41. _videoPlayer = videoPlayer;
  42. Application* app = Application::getInstance();
  43. _layout = elm_layout_add(app->_win);
  44. Evas *evas = evas_object_evas_get(_layout);
  45. _image = evas_object_image_filled_add(evas);
  46. evas_object_show(_image);
  47. player_create(&_player);
  48. evas_object_event_callback_add(_image, EVAS_CALLBACK_MOUSE_UP, _VideoPlayerTizen::mouse_up_cb, this);
  49. eext_object_event_callback_add(app->_win, EEXT_CALLBACK_BACK, _VideoPlayerTizen::win_back_cb, this);
  50. }
  51. virtual ~_VideoPlayerTizen()
  52. {
  53. Application* app = Application::getInstance();
  54. eext_object_event_callback_del(app->_win, EEXT_CALLBACK_BACK, _VideoPlayerTizen::win_back_cb);
  55. evas_object_event_callback_del(_image, EVAS_CALLBACK_MOUSE_UP, _VideoPlayerTizen::mouse_up_cb);
  56. player_stop(_player);
  57. player_unprepare(_player);
  58. player_destroy(_player);
  59. evas_object_del(_image);
  60. evas_object_del(_layout);
  61. }
  62. static void mouse_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
  63. {
  64. _VideoPlayerTizen* videoPlayerTizen = (_VideoPlayerTizen*)data;
  65. if (videoPlayerTizen->_videoPlayer->isPlaying())
  66. {
  67. videoPlayerTizen->_videoPlayer->pause();
  68. }
  69. else
  70. {
  71. videoPlayerTizen->_videoPlayer->resume();
  72. }
  73. }
  74. static void win_back_cb(void *data, Evas_Object *obj, void *event_info) {
  75. _VideoPlayerTizen* videoPlayerTizen = (_VideoPlayerTizen*)data;
  76. videoPlayerTizen->_videoPlayer->onPlayEvent(QUIT_FULLSCREEN);
  77. evas_object_resize(videoPlayerTizen->_image, videoPlayerTizen->_width, videoPlayerTizen->_height);
  78. evas_object_move(videoPlayerTizen->_image, videoPlayerTizen->_x, videoPlayerTizen->_y);
  79. }
  80. void setRectangle(Evas_Coord x, Evas_Coord y, Evas_Coord width, Evas_Coord height)
  81. {
  82. _x = x;
  83. _y = y;
  84. _width = width;
  85. _height = height;
  86. }
  87. player_h _player;
  88. Evas_Object* _image;
  89. Evas_Object* _layout;
  90. VideoPlayer* _videoPlayer;
  91. Evas_Coord _x;
  92. Evas_Coord _y;
  93. Evas_Coord _width;
  94. Evas_Coord _height;
  95. };
  96. VideoPlayer::VideoPlayer()
  97. : _videoPlayerIndex(-1)
  98. , _eventCallback(nullptr)
  99. , _fullScreenEnabled(false)
  100. , _fullScreenDirty(false)
  101. , _keepAspectRatioEnabled(false)
  102. {
  103. _videoView = (void*) new (std::nothrow) _VideoPlayerTizen(this);
  104. #if CC_VIDEOPLAYER_DEBUG_DRAW
  105. _debugDrawNode = DrawNode::create();
  106. addchild(_debugDrawNode);
  107. #endif
  108. }
  109. VideoPlayer::~VideoPlayer()
  110. {
  111. delete (_VideoPlayerTizen*)_videoView;
  112. }
  113. void VideoPlayer::setFileName(const std::string& fileName)
  114. {
  115. _videoURL = fileName;
  116. _videoSource = VideoPlayer::Source::FILENAME;
  117. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  118. const char* uri = _videoURL.c_str();
  119. std::string fullpath;
  120. fullpath.append(app_get_resource_path());
  121. fullpath.append(fileName.c_str());
  122. player_set_uri(impl->_player, fullpath.c_str());
  123. }
  124. void VideoPlayer::setURL(const std::string& videoUrl)
  125. {
  126. _videoURL = videoUrl;
  127. _videoSource = VideoPlayer::Source::URL;
  128. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  129. player_set_uri(impl->_player, videoUrl.c_str());
  130. }
  131. void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
  132. {
  133. cocos2d::ui::Widget::draw(renderer,transform,flags);
  134. if (flags & FLAGS_TRANSFORM_DIRTY)
  135. {
  136. auto directorInstance = Director::getInstance();
  137. auto glView = directorInstance->getOpenGLView();
  138. auto frameSize = glView->getFrameSize();
  139. auto winSize = directorInstance->getWinSize();
  140. auto leftBottom = convertToWorldSpace(Point::ZERO);
  141. auto rightTop = convertToWorldSpace(Point(_contentSize.width,_contentSize.height));
  142. auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX();
  143. auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
  144. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  145. Evas_Coord width = (rightTop.x - leftBottom.x) * glView->getScaleX();
  146. Evas_Coord height = (rightTop.y - leftBottom.y) * glView->getScaleY();
  147. Evas_Coord x = leftBottom.x * glView->getScaleX();
  148. Evas_Coord y = leftBottom.y * glView->getScaleY();
  149. impl->setRectangle(x, y, width, height);
  150. evas_object_resize(impl->_image, width, height);
  151. evas_object_move(impl->_image, uiLeft, uiTop);
  152. }
  153. #if CC_VIDEOPLAYER_DEBUG_DRAW
  154. _debugDrawNode->clear();
  155. auto size = getContentSize();
  156. Point vertices[4]=
  157. {
  158. Point::ZERO,
  159. Point(size.width, 0),
  160. Point(size.width, size.height),
  161. Point(0, size.height)
  162. };
  163. _debugdrawNode->drawPoly(vertices, 4, true, Color4F(1.0, 1.0, 1.0, 1.0));
  164. #endif
  165. }
  166. void VideoPlayer::setFullScreenEnabled(bool enabled)
  167. {
  168. if (_fullScreenEnabled != enabled)
  169. {
  170. _fullScreenEnabled = enabled;
  171. auto directorInstance = Director::getInstance();
  172. auto glView = directorInstance->getOpenGLView();
  173. auto frameSize = glView->getFrameSize();
  174. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  175. evas_object_resize(impl->_image, frameSize.width, frameSize.height);
  176. evas_object_move(impl->_image, 0, 0);
  177. }
  178. }
  179. bool VideoPlayer::isFullScreenEnabled()const
  180. {
  181. return _fullScreenEnabled;
  182. }
  183. void VideoPlayer::setKeepAspectRatioEnabled(bool enable)
  184. {
  185. if (_keepAspectRatioEnabled != enable)
  186. {
  187. _keepAspectRatioEnabled = enable;
  188. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  189. if (_keepAspectRatioEnabled == enable)
  190. {
  191. player_set_display_mode(impl->_player, PLAYER_DISPLAY_MODE_LETTER_BOX);
  192. }
  193. else
  194. {
  195. player_set_display_mode(impl->_player, PLAYER_DISPLAY_MODE_FULL_SCREEN);
  196. }
  197. }
  198. }
  199. #if CC_VIDEOPLAYER_DEBUG_DRAW
  200. void VideoPlayer::drawDebugData()
  201. {
  202. Director* director = Director::getInstance();
  203. CCASSERT(nullptr != director, "Director is null when setting matrix stack");
  204. director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  205. director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
  206. auto size = getContentSize();
  207. Point vertices[4]=
  208. {
  209. Point::ZERO,
  210. Point(size.width, 0),
  211. Point(size.width, size.height),
  212. Point(0, size.height)
  213. };
  214. DrawPrimitives::drawPoly(vertices, 4, true);
  215. director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
  216. }
  217. #endif
  218. static void _player_completed_cb(void *user_data)
  219. {
  220. VideoPlayer* player = (VideoPlayer*)user_data;
  221. player->onPlayEvent((int)VideoPlayer::EventType::COMPLETED);
  222. }
  223. static void _player_interrupted_cb(player_interrupted_code_e code, void *user_data)
  224. {
  225. VideoPlayer* player = (VideoPlayer*)user_data;
  226. player->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
  227. }
  228. static void _player_video_frame_decoded_cb(unsigned char *data, int width, int height, unsigned int size, void *user_data)
  229. {
  230. VideoPlayer* player = (VideoPlayer*)user_data;
  231. player->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
  232. }
  233. void VideoPlayer::play()
  234. {
  235. if (! _videoURL.empty())
  236. {
  237. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  238. player_set_sound_type(impl->_player, SOUND_TYPE_MEDIA);
  239. player_set_display(impl->_player, PLAYER_DISPLAY_TYPE_EVAS, GET_DISPLAY(impl->_image));
  240. if (_keepAspectRatioEnabled)
  241. {
  242. player_set_display_mode(impl->_player, PLAYER_DISPLAY_MODE_LETTER_BOX);
  243. }
  244. else
  245. {
  246. player_set_display_mode(impl->_player, PLAYER_DISPLAY_MODE_FULL_SCREEN);
  247. }
  248. player_set_completed_cb(impl->_player, _player_completed_cb, this);
  249. player_set_interrupted_cb(impl->_player, _player_interrupted_cb, this);
  250. player_prepare(impl->_player);
  251. int ret = player_start(impl->_player);
  252. if (ret == PLAYER_ERROR_NONE)
  253. {
  254. this->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
  255. }
  256. this->setVisible(true);
  257. }
  258. }
  259. void VideoPlayer::pause()
  260. {
  261. if (! _videoURL.empty())
  262. {
  263. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  264. int ret = player_pause(impl->_player);
  265. if (ret == PLAYER_ERROR_NONE)
  266. {
  267. this->onPlayEvent((int)VideoPlayer::EventType::PAUSED);
  268. }
  269. }
  270. }
  271. void VideoPlayer::resume()
  272. {
  273. if (! _videoURL.empty())
  274. {
  275. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  276. int ret = player_start(impl->_player);
  277. if (ret == PLAYER_ERROR_NONE)
  278. {
  279. this->onPlayEvent((int)VideoPlayer::EventType::PLAYING);
  280. }
  281. }
  282. }
  283. void VideoPlayer::stop()
  284. {
  285. if (! _videoURL.empty())
  286. {
  287. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  288. int ret = player_stop(impl->_player);
  289. if (ret == PLAYER_ERROR_NONE)
  290. {
  291. this->onPlayEvent((int)VideoPlayer::EventType::STOPPED);
  292. }
  293. }
  294. }
  295. void VideoPlayer::seekTo(float sec)
  296. {
  297. if (! _videoURL.empty())
  298. {
  299. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  300. player_set_play_position(impl->_player, sec, false, NULL, NULL);
  301. }
  302. }
  303. bool VideoPlayer::isPlaying() const
  304. {
  305. return _isPlaying;
  306. }
  307. void VideoPlayer::onEnter()
  308. {
  309. Widget::onEnter();
  310. if (isVisible())
  311. {
  312. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  313. player_set_display_visible(impl->_player, true);
  314. }
  315. }
  316. void VideoPlayer::onExit()
  317. {
  318. Widget::onExit();
  319. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  320. player_set_display_visible(impl->_player, false);
  321. }
  322. void VideoPlayer::setVisible(bool visible)
  323. {
  324. cocos2d::ui::Widget::setVisible(visible);
  325. _VideoPlayerTizen* impl = (_VideoPlayerTizen*)_videoView;
  326. if (!visible)
  327. {
  328. player_set_display_visible(impl->_player, false);
  329. }
  330. else if(isRunning())
  331. {
  332. player_set_display_visible(impl->_player, true);
  333. }
  334. }
  335. void VideoPlayer::addEventListener(const VideoPlayer::ccVideoPlayerCallback& callback)
  336. {
  337. _eventCallback = callback;
  338. }
  339. void VideoPlayer::onPlayEvent(int event)
  340. {
  341. if (event == QUIT_FULLSCREEN)
  342. {
  343. _fullScreenEnabled = false;
  344. }
  345. else
  346. {
  347. VideoPlayer::EventType videoEvent = (VideoPlayer::EventType)event;
  348. if (videoEvent == VideoPlayer::EventType::PLAYING) {
  349. _isPlaying = true;
  350. } else {
  351. _isPlaying = false;
  352. }
  353. if (_eventCallback)
  354. {
  355. _eventCallback(this,videoEvent);
  356. }
  357. }
  358. }
  359. cocos2d::ui::Widget* VideoPlayer::createCloneInstance()
  360. {
  361. return VideoPlayer::create();
  362. }
  363. void VideoPlayer::copySpecialProperties(Widget *widget)
  364. {
  365. VideoPlayer* videoPlayer = dynamic_cast<VideoPlayer*>(widget);
  366. if (videoPlayer)
  367. {
  368. _isPlaying = videoPlayer->_isPlaying;
  369. _fullScreenEnabled = videoPlayer->_fullScreenEnabled;
  370. _fullScreenDirty = videoPlayer->_fullScreenDirty;
  371. _videoURL = videoPlayer->_videoURL;
  372. _keepAspectRatioEnabled = videoPlayer->_keepAspectRatioEnabled;
  373. _videoSource = videoPlayer->_videoSource;
  374. _videoPlayerIndex = videoPlayer->_videoPlayerIndex;
  375. _eventCallback = videoPlayer->_eventCallback;
  376. _videoView = videoPlayer->_videoView;
  377. }
  378. }
  379. #endif