1
0

UIVideoPlayer-tizen.cpp 13 KB

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