public static VideoHandle Create(string uri, Action loaded, Action <string> error) { var handle = new VideoHandle(); var url = new NSUrl(uri); handle.Asset = new AVUrlAsset(url, (AVUrlAssetOptions)null); handle.Asset.LoadValuesAsynchronously(new string[] { "tracks" }, () => DispatchQueue.MainQueue.DispatchAsync( () => { NSError e; var status = handle.Asset.StatusOfValue("tracks", out e); if (status == AVKeyValueStatus.Loaded) { handle.Output = new AVPlayerItemVideoOutput( new CVPixelBufferAttributes { PixelFormatType = CVPixelFormatType.CV32BGRA, }); handle.PlayerItem = AVPlayerItem.FromAsset(handle.Asset); handle.PlayerItem.AddOutput(handle.Output); handle.Player = AVPlayer.FromPlayerItem(handle.PlayerItem); PollReadyState(handle, loaded, error); } else { error("Failed to load: " + status.ToString()); } })); return(handle); }
public static void Dispose(VideoHandle handle) { if (handle.AudioPlayer != null) { handle.AudioPlayer.Dispose(); } if (handle.Player != null) { handle.Player.Dispose(); } if (handle.PlayerItem != null) { handle.PlayerItem.Dispose(); } if (handle.Output != null) { handle.Output.Dispose(); } if (handle.Asset != null) { handle.Asset.Dispose(); } }
/*public void UpdateTexture(int textureName) * { * GL.BindTexture(TextureTarget.Texture2D, textureName); * PixelBufferImpl.CVPixelBufferLockBaseAddress(_handle, CVPixelBufferLockFlags.kCVPixelBufferLock_ReadOnly); * GL.TexImage2D( * TextureTarget.Texture2D, * 0, * PixelInternalFormat.Rgba, * Width, * Height, * 0, * PixelFormat.Bgra, * PixelType.UnsignedByte, * PixelBufferImpl.CVPixelBufferGetBaseAddress(_handle)); * PixelBufferImpl.CVPixelBufferUnlockBaseAddress(_handle, CVPixelBufferLockFlags.kCVPixelBufferLock_ReadOnly); * }*/ public void UpdateTexture(int textureName, VideoHandle handle) { var width = Width; var height = Height; var bytesPerRow = BytesPerRow; var actualBytesPerRow = width * 4; PixelBufferImpl.CVPixelBufferLockBaseAddress(_handle, CVPixelBufferLockFlags.kCVPixelBufferLock_ReadOnly); var baseAddress = PixelBufferImpl.CVPixelBufferGetBaseAddress(_handle); var sourceAddress = baseAddress; var sourceOffset = 0; var destOffset = 0; for (int y = 0; y < height; y++) { sourceOffset = y * bytesPerRow; destOffset = y * actualBytesPerRow; sourceAddress = new IntPtr(baseAddress.ToInt64() + (Int64)sourceOffset); Marshal.Copy(sourceAddress, handle.Pixels, destOffset, actualBytesPerRow); } PixelBufferImpl.CVPixelBufferUnlockBaseAddress(_handle, CVPixelBufferLockFlags.kCVPixelBufferLock_ReadOnly); var pinnedArray = GCHandle.Alloc(handle.Pixels, GCHandleType.Pinned); GL.BindTexture(TextureTarget.Texture2D, textureName); if (width != handle.WidthCache || height != handle.HeightCache) { handle.WidthCache = width; handle.HeightCache = height; GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, width, height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, pinnedArray.AddrOfPinnedObject()); } else { GL.TexSubImage2D( TextureTarget.Texture2D, 0, 0, 0, width, height, PixelFormat.Bgra, PixelType.UnsignedByte, pinnedArray.AddrOfPinnedObject()); } pinnedArray.Free(); }
public static void UpdateTexture(VideoHandle handle, System.Int32 textureHandle) { var pixelBufferSize = GetWidth(handle) * GetHeight(handle) * 4; if (handle.Pixels == null || handle.Pixels.Length != pixelBufferSize) { handle.Pixels = new byte[pixelBufferSize]; } var rt = new CMTime(); using (var buffer = CopyPixelBuffer(handle.Output, handle.PlayerItem.CurrentTime, ref rt)) buffer.UpdateTexture(textureHandle, handle); }
public static int GetRotation(VideoHandle handle) { var degrees = 0; var tracks = handle.Asset.Tracks; foreach (var track in tracks) { if (track.MediaType.Equals(AVMediaType.Video)) { var transform = track.PreferredTransform; var angle = Math.Atan2(transform.yx, transform.xx); degrees = (int)(angle * (180.0 / Math.PI)); break; } } return(degrees); }
static void PollReadyState(VideoHandle handle, Action ready, Action <string> error) { switch (handle.PlayerItem.Status) { case AVPlayerItemStatus.ReadyToPlay: ready(); break; case AVPlayerItemStatus.Failed: error("Failed to load: " + handle.PlayerItem.Status.ToString()); break; default: DispatchQueue.MainQueue.DispatchAsync(() => PollReadyState(handle, ready, error)); break; } }
public static void Stop(VideoHandle handle) { Pause(handle); SetPosition(handle, 0.0); }
public static void SetVolume(VideoHandle handle, float volume) { handle.Player.Volume = volume; }
public static float GetVolume(VideoHandle handle) { return(handle.Player.Volume); }
public static int GetHeight(VideoHandle handle) { return(handle.PlayerItem.PresentationSize.ToSize().Height); }
public static void Pause(VideoHandle handle) { handle.Player.Pause(); }
public static double GetDuration(VideoHandle handle) { return(handle.Asset.Duration.Seconds); }
public static int GetHeight(VideoHandle handle) { return((int)handle.PlayerItem.PresentationSize.ToRoundedCGSize().Height); }
public static int GetWidth(VideoHandle handle) { return((int)handle.PlayerItem.PresentationSize.ToRoundedCGSize().Width); }
public static void SetPosition(VideoHandle handle, double position) { handle.PlayerItem.Seek(CMTime.FromSeconds(position, 1000)); }
public static double GetPosition(VideoHandle handle) { return(handle.PlayerItem.CurrentTime.Seconds); }
public static void Play(VideoHandle handle) { handle.Player.Play(); }
public static bool HasNewPixelBuffer(VideoHandle handle) { return(handle.Output.HasNewPixelBufferForItemTime(handle.PlayerItem.CurrentTime)); }
MonoImpl(string uri, Action loaded, Action <string> error) { _handle = VideoImpl.Create(uri, loaded, error); }
public static int GetWidth(VideoHandle handle) { return(handle.PlayerItem.PresentationSize.ToSize().Width); }