VideoPlayerPlugin.mm 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "CustomVideoPlayer.h"
  2. //#include "iPhone_View.h"
  3. #import <UIKit/UIKit.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdint.h>
  7. extern "C" __attribute__((visibility ("default"))) NSString *const kUnityViewDidRotate;
  8. @interface CustomVideoPlayerInterface : NSObject <CustomVideoPlayerDelegate> {
  9. @public
  10. CustomVideoPlayer *player;
  11. CGRect margin;
  12. bool bLoop;
  13. bool m_bFinish;
  14. bool m_bUnload;
  15. bool m_bLoading;
  16. bool m_bLoopPlay;
  17. NSURL* m_videoURL;
  18. }
  19. - (void)onPlayerReady;
  20. - (void)onPlayerDidFinishPlayingVideo;
  21. @end
  22. @implementation CustomVideoPlayerInterface
  23. - (void)loadVideo:(NSURL *)videoURL {
  24. m_bFinish = false;
  25. [player loadVideo:videoURL];
  26. }
  27. - (void)playVideo{
  28. if ([player readyToPlay])
  29. [self play];
  30. }
  31. - (void)onPlayerReady {
  32. m_bLoading = false;
  33. if(m_bUnload == true)
  34. {
  35. m_bUnload = false;
  36. [self unload];
  37. }
  38. if( m_bLoopPlay == true)
  39. {
  40. [self play];
  41. m_bLoopPlay =false;
  42. }
  43. }
  44. - (void)resizeView {
  45. /* //FIXME Orientationが変更された時にうまくリサイズされていない view frame更新
  46. CGFloat scale = UnityGetGLView().contentScaleFactor;
  47. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  48. CGRect bounds;
  49. if (orientation) {
  50. bounds.size.width = UnityGetGLView().bounds.size.width - (margin.origin.x + margin.size.width) / scale;
  51. bounds.size.height = UnityGetGLView().bounds.size.height - (margin.origin.y + margin.size.height) / scale;
  52. } else {
  53. bounds.size.width = UnityGetGLView().bounds.size.height - (margin.origin.x + margin.size.width) / scale;
  54. bounds.size.height = UnityGetGLView().bounds.size.width - (margin.origin.y + margin.size.height) / scale;
  55. }
  56. view.bounds = bounds;
  57. view.center = CGPointMake(view.bounds.size.width / 2 + margin.origin.x / scale, view.bounds.size.height / 2 + margin.origin.y / scale);*/
  58. }
  59. - (void)play {
  60. m_bFinish = false;
  61. [player playToTexture];
  62. }
  63. - (void)playloop {
  64. m_bFinish = false;
  65. [player playToTextureloop];
  66. }
  67. - (void)unload {
  68. if( m_bLoading == true)
  69. {
  70. m_bUnload = true;
  71. return;
  72. }
  73. [player unloadPlayer];
  74. }
  75. - (void)onPlayerDidFinishPlayingVideo {
  76. if(bLoop)
  77. {
  78. if( [m_videoURL isFileURL])
  79. {
  80. [player seekTo:0.0f];
  81. [self playloop];
  82. }
  83. else
  84. {
  85. [self unload];
  86. [self loadVideo:m_videoURL];
  87. m_bLoopPlay = true;
  88. }
  89. }
  90. else
  91. {
  92. //[self unload];
  93. m_bFinish = true;
  94. }
  95. }
  96. @end
  97. const int PLAYER_MAX = 8;
  98. static CustomVideoPlayerInterface * _Player[PLAYER_MAX];
  99. static bool _PlayerUsed[PLAYER_MAX] = {0,0,0,0,0,0,0,0};
  100. static CustomVideoPlayerInterface *_GetPlayer(int iID) {
  101. if(iID < 0 || iID >= PLAYER_MAX)
  102. return nil;
  103. if (!_Player[iID]) {
  104. _Player[iID] = [[CustomVideoPlayerInterface alloc] init];
  105. _Player[iID]->player = [[CustomVideoPlayer alloc] init];
  106. _Player[iID]->player.delegate = _Player[iID].self;
  107. _PlayerUsed[iID] = true;
  108. }
  109. if (!_Player[iID]->player) {
  110. _Player[iID]->player = [[CustomVideoPlayer alloc] init];
  111. _Player[iID]->player.delegate = _Player[iID]->player.self;
  112. }
  113. return _Player[iID];
  114. }
  115. static NSURL *_GetUrl(const char *videoURL) {
  116. NSURL *url = nil;
  117. if (::strstr(videoURL, "://"))
  118. url = [NSURL URLWithString:[NSString stringWithUTF8String:videoURL]];
  119. else
  120. url = [NSURL fileURLWithPath:[[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Data/Raw/"] stringByAppendingPathComponent:[NSString stringWithUTF8String:videoURL]]];
  121. return url;
  122. }
  123. extern "C" int VideoPlayerPluginCreateInstance()
  124. {
  125. for(int i = 0; i < PLAYER_MAX; i++)
  126. {
  127. if(_PlayerUsed[i] == false)
  128. {
  129. CustomVideoPlayerInterface * player = _GetPlayer(i);
  130. player->bLoop = false;
  131. return i;
  132. }
  133. }
  134. return -1;
  135. }
  136. extern "C" void VideoPlayerPluginDestroyInstance(int iID)
  137. {
  138. if(iID < 0 || iID >= PLAYER_MAX)
  139. return;
  140. if(_Player[iID])
  141. {
  142. if(_Player[iID]->player)
  143. {
  144. [_Player[iID]->player unloadPlayer];
  145. [_Player[iID]->player cleanCache];
  146. //[_Player[iID]->player dealloc];
  147. _Player[iID]->player = NULL;
  148. }
  149. //[_Player[iID] dealloc];
  150. _Player[iID] = NULL;
  151. }
  152. _PlayerUsed[iID] = false;
  153. }
  154. extern "C" void VideoPlayerPluginLoadVideo(int iID,const char *videoURL) {
  155. if(iID < 0 || iID >= PLAYER_MAX)
  156. return;
  157. if (_GetPlayer(iID)->player.isPlaying) {
  158. [_GetPlayer(iID)->player unloadPlayer];
  159. }
  160. _GetPlayer(iID)->m_bFinish = false;
  161. _GetPlayer(iID)->m_bLoading = true;
  162. _GetPlayer(iID)->m_videoURL = _GetUrl(videoURL);
  163. [_GetPlayer(iID) loadVideo:_GetPlayer(iID)->m_videoURL];
  164. }
  165. extern "C" void VideoPlayerPluginPlayVideo(int iID) {
  166. if(iID < 0 || iID >= PLAYER_MAX)
  167. return;
  168. _GetPlayer(iID)->m_bFinish = false;
  169. [_GetPlayer(iID) playVideo];
  170. }
  171. extern "C" void VideoPlayerPluginSetLoop(int iID,bool bLoop) {
  172. if(iID < 0 || iID >= PLAYER_MAX)
  173. return;
  174. _GetPlayer(iID)->bLoop = bLoop;
  175. }
  176. extern "C" void VideoPlayerPluginSetVolume(int iID,float fVolume) {
  177. if(iID < 0 || iID >= PLAYER_MAX)
  178. return;
  179. [_GetPlayer(iID)->player setAudioVolume:fVolume];
  180. }
  181. extern "C" void VideoPlayerPluginPauseVideo(int iID) {
  182. if(iID < 0 || iID >= PLAYER_MAX)
  183. return;
  184. [_GetPlayer(iID)->player pause];
  185. }
  186. extern "C" void VideoPlayerPluginResumeVideo(int iID) {
  187. if(iID < 0 || iID >= PLAYER_MAX)
  188. return;
  189. [_GetPlayer(iID)->player resume];
  190. }
  191. extern "C" void VideoPlayerPluginRewindVideo(int iID) {
  192. if(iID < 0 || iID >= PLAYER_MAX)
  193. return;
  194. }
  195. extern "C" bool VideoPlayerPluginCanOutputToTexture(const char *videoURL) {
  196. return [CustomVideoPlayer CanPlayToTexture:_GetUrl(videoURL)];
  197. }
  198. extern "C" bool VideoPlayerPluginPlayerReady(int iID) {
  199. if(iID < 0 || iID >= PLAYER_MAX)
  200. return false;
  201. return [_GetPlayer(iID)->player readyToPlay];
  202. }
  203. extern "C" float VideoPlayerPluginDurationSeconds(int iID) {
  204. if(iID < 0 || iID >= PLAYER_MAX)
  205. return 0.0f;
  206. return [_GetPlayer(iID)->player durationSeconds];
  207. }
  208. extern "C" int VideoPlayerPluginGetCurrentSeekPercent(int iID) {
  209. if(iID < 0 || iID >= PLAYER_MAX)
  210. return 0;
  211. if(_Player[iID])
  212. {
  213. if(_Player[iID]->player)
  214. {
  215. if (_GetPlayer(iID)->player.isPlaying) {
  216. return (int)( [_GetPlayer(iID)->player availableDuration] / [_GetPlayer(iID)->player durationSeconds] * 100);
  217. }
  218. }
  219. }
  220. return 0;
  221. }
  222. extern "C" int VideoPlayerPluginGetAudioTrack(int iID) {
  223. if(iID < 0 || iID >= PLAYER_MAX)
  224. return 0;
  225. return [_GetPlayer(iID)->player getAudioTrack];
  226. }
  227. extern "C" void VideoPlayerPluginSetAudioTrack(int iID, int trackID) {
  228. if(iID < 0 || iID >= PLAYER_MAX)
  229. return;
  230. return [_GetPlayer(iID)->player setAudioTrack:trackID];
  231. }
  232. extern "C" int VideoPlayerPluginGetAudioTrackCount(int iID) {
  233. if(iID < 0 || iID >= PLAYER_MAX)
  234. return 0;
  235. return [_GetPlayer(iID)->player getAudioTrack];
  236. }
  237. extern "C" void VideoPlayerPluginExtents(int iID,int *w, int *h) {
  238. if(iID < 0 || iID >= PLAYER_MAX)
  239. return;
  240. CGSize sz = [_GetPlayer(iID)->player videoSize];
  241. *w = (int) sz.width;
  242. *h = (int) sz.height;
  243. if(sz.width == 0)
  244. {
  245. [_GetPlayer(iID)->player curFrameTexture];
  246. }
  247. }
  248. extern "C" void VideoPlayerPluginSetTexture(int iID,int iTextureID)
  249. {
  250. if(iID < 0 || iID >= PLAYER_MAX)
  251. return;
  252. [_GetPlayer(iID)->player setTextureID:iTextureID];
  253. }
  254. extern "C" intptr_t VideoPlayerPluginCurFrameTexture(int iID) {
  255. if(iID < 0 || iID >= PLAYER_MAX)
  256. return 0;
  257. return [_GetPlayer(iID)->player curFrameTexture];
  258. }
  259. extern "C" void VideoPlayerPluginSeekToVideo(int iID,float time) {
  260. if(iID < 0 || iID >= PLAYER_MAX)
  261. return;
  262. [_GetPlayer(iID)->player seekTo:time];
  263. }
  264. extern "C" float VideoPlayerPluginCurTimeSeconds(int iID) {
  265. if(iID < 0 || iID >= PLAYER_MAX)
  266. return 0.0f;
  267. return [_GetPlayer(iID)->player curTimeSeconds];
  268. }
  269. extern "C" bool VideoPlayerPluginIsPlaying(int iID) {
  270. if(iID < 0 || iID >= PLAYER_MAX)
  271. return false;
  272. if (!_GetPlayer(iID)->player)return false;
  273. return [_GetPlayer(iID)->player isPlaying];
  274. }
  275. extern "C" void VideoPlayerPluginStopVideo(int iID) {
  276. if(iID < 0 || iID >= PLAYER_MAX)
  277. return;
  278. if (_GetPlayer(iID)->player) {
  279. [_GetPlayer(iID) unload];
  280. }
  281. }
  282. extern "C" bool VideoPlayerPluginFinish(int iID) {
  283. if(iID < 0 || iID >= PLAYER_MAX)
  284. return false;
  285. if (_GetPlayer(iID)->player) {
  286. return _GetPlayer(iID)->m_bFinish;
  287. }
  288. return false;
  289. }
  290. extern "C" bool VideoPlayerPluginError(int iID) {
  291. if(iID < 0 || iID >= PLAYER_MAX)
  292. return false;
  293. if (_GetPlayer(iID)->player) {
  294. return [_GetPlayer(iID)->player getError ];
  295. //return _GetPlayer(iID)->player get;
  296. }
  297. return false;
  298. }
  299. extern "C" void VideoPlayerPluginSetSpeed(int iID,float fSpeed) {
  300. if(iID < 0 || iID >= PLAYER_MAX)
  301. return;
  302. if (_GetPlayer(iID)->player) {
  303. [_GetPlayer(iID)->player setSpeed:fSpeed ];
  304. //return _GetPlayer(iID)->player get;
  305. }
  306. }