123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- using System;
- using System.Runtime.InteropServices;
- using UnityEngine;
- namespace UMP.Wrappers
- {
- internal class WrapperAndroid : IWrapperNative, IWrapperPlayer, IWrapperSpu, IWrapperAudio
- {
- private const string CLASS_PATH_CORE = "unitydirectionkit/universalmediaplayer/core/UniversalMediaPlayer";
- private const string CLASS_PATH_NATIVE = "unitydirectionkit/universalmediaplayer/nativeplayer/MediaPlayerNative";
- private const string CLASS_PATH_LIBVLC = "unitydirectionkit/universalmediaplayer/libvlcplayer/MediaPlayerVLC";
- private const string CLASS_PATH_EXO = "unitydirectionkit/universalmediaplayer/exoplayer/MediaPlayerExo";
- private AndroidJavaObject _coreObj;
- private object _playerExpanded;
- private int _nativeIndex;
- #region Native Imports
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern int UMPNativeInit();
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern void UMPNativeUpdateIndex(int index);
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern void UMPNativeSetTexture(int index, IntPtr texture);
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern void UMPNativeSetPixelsBuffer(int index, IntPtr buffer, int width, int height);
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern void UMPNativeUpdateFrameBuffer(int index);
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern IntPtr UMPNativeGetUnityRenderCallback();
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern IntPtr UMPNativeGetPlayCallback();
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern IntPtr UMPNativeGetUpdateSurfaceTextureCallback();
- [DllImport(UMPSettings.ASSET_NAME)]
- private static extern IntPtr UMPNativeGetUpdateFrameBufferCallback();
- #endregion
- public WrapperAndroid(PlayerOptionsAndroid options)
- {
- _nativeIndex = UMPNativeInit();
- AndroidJavaObject player = null;
- if (options.PlayerType == PlayerOptionsAndroid.PlayerTypes.Native)
- {
- player = new AndroidJavaObject(CLASS_PATH_NATIVE, new object[0]);
- }
- else if (options.PlayerType == PlayerOptionsAndroid.PlayerTypes.LibVLC)
- {
- player = new AndroidJavaObject(CLASS_PATH_LIBVLC, new object[0]);
- _playerExpanded = new ExpandedAndroidLibVLC(player);
- }
- else if (options.PlayerType == PlayerOptionsAndroid.PlayerTypes.Exo)
- {
- player = new AndroidJavaObject(CLASS_PATH_EXO, new object[0]);
- }
- var arg = new object[3];
- arg[0] = _nativeIndex;
- arg[1] = player;
- arg[2] = options.GetOptions('\n');
- _coreObj = new AndroidJavaObject(CLASS_PATH_CORE, arg);
- }
- #region Native
- public int NativeIndex
- {
- get
- {
- return _nativeIndex;
- }
- }
- public void NativeUpdateIndex()
- {
- UMPNativeUpdateIndex(_nativeIndex);
- }
- public void NativeSetTexture(IntPtr texture)
- {
- UMPNativeSetTexture(_nativeIndex, texture);
- }
- public void NativeSetPixelsBuffer(IntPtr buffer, int width, int height)
- {
- UMPNativeSetPixelsBuffer(_nativeIndex, buffer, width, height);
- }
- public void NativeUpdatePixelsBuffer()
- {
- if (SystemInfo.graphicsMultiThreaded)
- GL.IssuePluginEvent(UMPNativeGetUpdateFrameBufferCallback(), _nativeIndex);
- else
- UMPNativeUpdateFrameBuffer(_nativeIndex);
- }
- public IntPtr NativeGetUnityRenderCallback()
- {
- return UMPNativeGetUnityRenderCallback();
- }
- #endregion
- #region Player
- public void PlayerSetDataSource(string path, object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportSetDataSource", path);
- }
- public bool PlayerPlay(object playerObject = null)
- {
- var started = false;
- if (SystemInfo.graphicsMultiThreaded)// && !_isReady)
- {
- GL.IssuePluginEvent(UMPNativeGetPlayCallback(), (int)_coreObj.GetRawObject());
- started = true;
- }
- else
- {
- started = _coreObj.Call<bool>("exportPlay");
- }
- return started;
- }
- public void PlayerPause(object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportPause");
- }
- public void PlayerStop(object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportStop");
- }
- public void PlayerRelease(object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportRelease");
- }
- public bool PlayerIsPlaying(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<bool>("exportIsPlaying");
- return false;
- }
- public bool PlayerIsReady(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<bool>("exportIsReady");
- return false;
- }
- public long PlayerGetLength(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<long>("exportDuration");
- return 0;
- }
- public long PlayerGetTime(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<long>("exportGetTime");
- return 0;
- }
- public void PlayerSetTime(long time, object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportSetTime", time);
- }
- public float PlayerGetPosition(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<float>("exportGetPosition");
- return 0;
- }
- public void PlayerSetPosition(float pos, object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportSetPosition", pos);
- }
- public float PlayerGetRate(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<float>("exportGetPlaybackRate");
- return 1;
- }
- public bool PlayerSetRate(float rate, object playerObject = null)
- {
- if (_coreObj != null)
- {
- _coreObj.Call("exportSetPlaybackRate", rate);
- return true;
- }
- return false;
- }
- public int PlayerGetVolume(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<int>("exportGetVolume");
- return 0;
- }
- public int PlayerSetVolume(int volume, object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportSetVolume", volume);
- return 1;
- }
- public bool PlayerGetMute(object playerObject = null)
- {
- // Not used in this implementation
- return false;
- }
- public void PlayerSetMute(bool mute, object playerObject = null)
- {
- // Not used in this implementation
- }
- public int PlayerVideoWidth(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<int>("exportVideoWidth");
- return 0;
- }
- public int PlayerVideoHeight(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<int>("exportVideoHeight");
- return 0;
- }
- public int PlayerVideoFramesCounter(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<int>("exportVideoFramesCounter");
- return 0;
- }
- public PlayerState PlayerGetState()
- {
- var eventValue = 0;
- if (_coreObj != null)
- eventValue = _coreObj.Call<int>("exportGetState");
- return (PlayerState)eventValue;
- }
- public object PlayerGetStateValue()
- {
- object value = _coreObj.Call<float>("exportGetStateFloatValue");
- if ((float)value < 0)
- {
- value = _coreObj.Call<long>("exportGetStateLongValue");
- if ((long)value < 0)
- value = _coreObj.Call<string>("exportGetStateStringValue");
- }
- return value;
- }
- #endregion
- #region Player Spu
- public MediaTrackInfo[] PlayerSpuGetTracks(object playerObject = null)
- {
- var tracks = new System.Collections.Generic.List<MediaTrackInfo>();
- if (_coreObj != null)
- {
- int tracksLength = _coreObj.Call<int>("exportGetSpuTracksLength");
- for (int i = 0; i < tracksLength; i++)
- {
- int id = _coreObj.Call<int>("exportGetSpuTrackId", i);
- string name = _coreObj.Call<string>("exportGetSpuTrackName", i);
- tracks.Add(new MediaTrackInfo(id, name));
- }
- }
- return tracks.ToArray();
- }
- public int PlayerSpuGetTrack(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<int>("exportGetSpuTrack");
- return 0;
- }
- public int PlayerSpuSetTrack(int spuIndex, object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportSetSpuTrack", spuIndex);
- return 0;
- }
- #endregion
- #region Player Audio
- public MediaTrackInfo[] PlayerAudioGetTracks(object playerObject = null)
- {
- var tracks = new System.Collections.Generic.List<MediaTrackInfo>();
- if (_coreObj != null)
- {
- int tracksLength = _coreObj.Call<int>("exportGetAudioTracksLength");
- for (int i = 0; i < tracksLength; i++)
- {
- int id = _coreObj.Call<int>("exportGetAudioTrackId", i);
- string name = _coreObj.Call<string>("exportGetAudioTrackName", i);
- tracks.Add(new MediaTrackInfo(id, name));
- }
- }
- return tracks.ToArray();
- }
- public int PlayerAudioGetTrack(object playerObject = null)
- {
- if (_coreObj != null)
- return _coreObj.Call<int>("exportGetAudioTrack");
- return 0;
- }
- public int PlayerAudioSetTrack(int audioIndex, object playerObject = null)
- {
- if (_coreObj != null)
- _coreObj.Call("exportSetAudioTrack", audioIndex);
- return 0;
- }
- #endregion
- #region Platform dependent functionality
- public object PlayerExpanded
- {
- get
- {
- return _playerExpanded;
- }
- }
- public void PlayerUpdateSurfaceTexture()
- {
- if (_coreObj != null)
- {
- if (SystemInfo.graphicsMultiThreaded)
- GL.IssuePluginEvent(UMPNativeGetUpdateSurfaceTextureCallback(), (int)_coreObj.GetRawObject());
- else
- _coreObj.Call("exportUpdateSurfaceTexture");
- }
- }
- public bool PlayerSetSubtitleFile(Uri path)
- {
- if (_coreObj != null)
- {
- var arg = new object[1];
- arg[0] = path.AbsoluteUri;
- return _coreObj.Call<bool>("exportSetSubtitleFile", arg);
- }
- return false;
- }
- #endregion
- }
- }
|