GvrVideoPlayerTexture.cs 38.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
//-----------------------------------------------------------------------
// <copyright file="GvrVideoPlayerTexture.cs" company="Google Inc.">
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

/// <summary>Plays video using Exoplayer rendering it on the main texture.</summary>
[HelpURL("https://developers.google.com/vr/unity/reference/class/GvrVideoPlayerTexture")]
public class GvrVideoPlayerTexture : MonoBehaviour
{
    /// <summary>Attach a text component to get some debug status info.</summary>
    public Text statusText;

    /// <summary>The type of the video.</summary>
    public VideoType videoType;

    /// <summary>The video URL.</summary>
    public string videoURL;

    /// <summary>The video content ID.</summary>
    public string videoContentID;

    /// <summary>The video provider ID.</summary>
    public string videoProviderId;

    /// <summary>The video resolution used when streaming begins.</summary>
    /// <remarks><para>
    /// For multi-rate streams like Dash and HLS, the stream used at the beginning of playback is
    /// selected such that its vertical resolution is greater than or equal to this value.
    /// </para><para>
    /// After streaming begins, the player auto-selects the highest rate stream the network
    /// connection is capable of delivering.
    /// </para></remarks>
    public VideoResolution initialResolution = VideoResolution.Highest;

    /// <summary>
    /// Value `true` indicates that the aspect ratio of the renderer needs adjusting.
    /// </summary>
    public bool adjustAspectRatio;

    /// <summary>Whether to use secure path for DRM protected video.</summary>
    public bool useSecurePath;

    private const string DLL_NAME = "gvrvideo";

#if !UNITY_ANDROID || UNITY_EDITOR
    private const string NOT_IMPLEMENTED_MSG = "Not implemented on this platform";
#endif // !UNITY_ANDROID || UNITY_EDITOR

    private static Queue<Action> executeOnMainThread = new Queue<Action>();

    /// <summary>The video player pointer used to uniquely identify the player instance.</summary>
    private IntPtr videoPlayerPtr;

    /// <summary>The video player event base.</summary>
    /// <remarks>This is added to the event id when issues events to the plugin.</remarks>
    private int videoPlayerEventBase;

    private Texture initialTexture;
    private Texture surfaceTexture;
    private float[] videoMatrixRaw;
    private Matrix4x4 videoMatrix;
    private int videoMatrixPropertyId;
    private long lastVideoTimestamp;

    private bool initialized;
    private int texWidth = 1024;
    private int texHeight = 1024;
    private long lastBufferedPosition;
    private float framecount = 0;

    private Renderer screen;

    /// <summary>The render event function.</summary>
    private IntPtr renderEventFunction;
    private bool playOnResume;

    /// <summary>List of callbacks to invoke when the video is ready.</summary>
    private List<Action<int>> onEventCallbacks;

    /// <summary>List of callbacks to invoke on exception.</summary>
    /// <remarks>The first parameter is the type of exception, the second is the message.</remarks>
    private List<Action<string, string>> onExceptionCallbacks;

    /// <summary>A delegate to be triggered by event callbacks.</summary>
    /// <param name="cbdata">An integer pointer to the Video Player.</param>
    /// <param name="eventId">The ID of the triggering event.</param>
    internal delegate void OnVideoEventCallback(IntPtr cbdata, int eventId);

    /// <summary>A delegate to be triggered by exception callbacks.</summary>
    /// <param name="type">The type of exception.</param>
    /// <param name="msg">The message associated with the exception.</param>
    /// <param name="cbdata">An integer pointer to the Video Player.</param>
    internal delegate void OnExceptionCallback(string type, string msg, IntPtr cbdata);

    /// <summary>Video type.</summary>
    public enum VideoType
    {
        /// <summary>Dynamic Adaptive Streaming over HTTP.</summary>
        Dash = 0,

        /// <summary>HTTP Live Streaming.</summary>
        HLS = 2,

        /// <summary>Another video type.</summary>
        Other = 3,
    }

    /// <summary>
    /// Video resolutions which can be selected as the initial resolution when streaming begins.
    /// See `initialResolution` for more information.
    /// </summary>
    public enum VideoResolution
    {
        /// <summary>The lowest available resolution.</summary>
        Lowest = 1,

        /// <summary>720p resolution.</summary>
        _720 = 720,

        /// <summary>1080p resolution.</summary>
        _1080 = 1080,

        /// <summary>2K resolution.</summary>
        _2048 = 2048,

        /// <summary>4K resolution.</summary>
        Highest = 4096,
    }

    /// <summary>Video player state.</summary>
    public enum VideoPlayerState
    {
        /// <summary>An idle state.</summary>
        Idle = 1,

        /// <summary>Preparing for video.</summary>
        Preparing = 2,

        /// <summary>Buffering video.</summary>
        Buffering = 3,

        /// <summary>Ready for video.</summary>
        Ready = 4,

        /// <summary>Done with video.</summary>
        Ended = 5,
    }

    /// <summary>Video events.</summary>
    public enum VideoEvents
    {
        /// <summary>Indicates that video is ready.</summary>
        VideoReady = 1,

        /// <summary>Indicates that the video playback should begin.</summary>
        VideoStartPlayback = 2,

        /// <summary>Indicates that the video format has changed.</summary>
        VideoFormatChanged = 3,

        /// <summary>Indicates that the video surface has been set.</summary>
        VideoSurfaceSet = 4,

        /// <summary>Indicates that the video size has changed.</summary>
        VideoSizeChanged = 5,
    }

    /// <summary>Stereo mode formats.</summary>
    public enum StereoMode
    {
        /// <summary>An error-state indicating that no value has been set.</summary>
        NoValue = -1,

        /// <summary>Mono sound.</summary>
        Mono = 0,

        /// <summary>Top-and-bottom stereo sound.</summary>
        TopBottom = 1,

        /// <summary>Left-and-right stereo sound.</summary>
        LeftRight = 2,
    }

    /// <summary>Plugin render commands.</summary>
    /// <remarks>
    /// These are added to the eventbase for the specific player object and issued to the plugin.
    /// </remarks>
    private enum RenderCommand
    {
        None = -1,
        InitializePlayer = 0,
        UpdateVideo = 1,
        RenderMono = 2,
        RenderLeftEye = 3,
        RenderRightEye = 4,
        Shutdown = 5,
    }

    /// <summary>Gets a value indicating whether the video is ready to be played.</summary>
    /// <value>Value `true` if the video is ready to be played.</value>
    public bool VideoReady
    {
        get
        {
            return videoPlayerPtr != IntPtr.Zero && IsVideoReady(videoPlayerPtr);
        }
    }

    /// <summary>Gets or sets the current position in seconds in the video stream.</summary>
    /// <value>The current position in seconds in the video stream.</value>
    public long CurrentPosition
    {
        get
        {
            return videoPlayerPtr != IntPtr.Zero ? GetCurrentPosition(videoPlayerPtr) : 0;
        }

        set
        {
            // If the position is being set to 0, reset the framecount as well.
            // This allows the texture swapping to work correctly at the beginning
            // of the stream.
            if (value == 0)
            {
                framecount = 0;
            }

            SetCurrentPosition(videoPlayerPtr, value);
        }
    }

    /// <summary>Gets the duration in seconds of the video stream.</summary>
    /// <value>The duration in seconds of the video stream.</value>
    public long VideoDuration
    {
        get { return videoPlayerPtr != IntPtr.Zero ? GetDuration(videoPlayerPtr) : 0; }
    }

    /// <summary>Gets the buffered position in seconds of the video stream.</summary>
    /// <value>The buffered position in seconds of the video stream.</value>
    public long BufferedPosition
    {
        get { return videoPlayerPtr != IntPtr.Zero ? GetBufferedPosition(videoPlayerPtr) : 0; }
    }

    /// <summary>Gets the buffered percentage of the video stream.</summary>
    /// <value>The buffered percentage of the video stream.</value>
    public int BufferedPercentage
    {
        get { return videoPlayerPtr != IntPtr.Zero ? GetBufferedPercentage(videoPlayerPtr) : 0; }
    }

    /// <summary>Gets a value indicating whether the video is paused.</summary>
    /// <value>Value `true` if the video is paused, `false` otherwise.</value>
    public bool IsPaused
    {
        get
        {
            return !initialized || videoPlayerPtr == IntPtr.Zero || IsVideoPaused(videoPlayerPtr);
        }
    }

    /// <summary>Gets the player state.</summary>
    /// <value>The player state.</value>
    public VideoPlayerState PlayerState
    {
        get
        {
            return videoPlayerPtr != IntPtr.Zero ?
                (VideoPlayerState)GetPlayerState(videoPlayerPtr) : VideoPlayerState.Idle;
        }
    }

    /// <summary>Gets the maximum volume value which can be set.</summary>
    /// <value>The maximum volume value which can be set.</value>
    public int MaxVolume
    {
        get { return videoPlayerPtr != IntPtr.Zero ? GetMaxVolume(videoPlayerPtr) : 0; }
    }

    /// <summary>Gets or sets the current volume setting.</summary>
    /// <value>The current volume setting.</value>
    public int CurrentVolume
    {
        get { return videoPlayerPtr != IntPtr.Zero ? GetCurrentVolume(videoPlayerPtr) : 0; }
        set { SetCurrentVolume(value); }
    }

    /// <summary>Gets the current stereo mode.</summary>
    /// <value>The current stereo mode.</value>
    public StereoMode CurrentStereoMode
    {
        get
        {
            return videoPlayerPtr != IntPtr.Zero ?
                (StereoMode)GetStereoMode(videoPlayerPtr) : StereoMode.NoValue;
        }
    }

    /// <summary>Gets a value indicating whether the video has a projection.</summary>
    /// <value>Value `true` if the video has a projection, `false` otherwise.</value>
    public bool HasProjection
    {
        get { return videoPlayerPtr != IntPtr.Zero ? HasProjectionData(videoPlayerPtr) : false; }
    }

    /// <summary>Gets or sets the renderer for the video texture.</summary>
    /// <value>The renderer for the video texture.</value>
    public Renderer Screen
    {
        get
        {
            return screen;
        }

        set
        {
            if (screen == value)
            {
                return;
            }

            if (screen != null && initialTexture != null)
            {
                screen.sharedMaterial.mainTexture = initialTexture;
            }

            screen = value;

            if (screen != null)
            {
                initialTexture = screen.sharedMaterial.mainTexture;
            }
        }
    }

    /// <summary>Gets the current frame texture.</summary>
    /// <value>The current frame texture.</value>
    public Texture CurrentFrameTexture
    {
        get { return surfaceTexture; }
    }

    /// <summary>Gets the width of the texture.</summary>
    /// <value>The width of the texture.</value>
    public int Width
    {
        get { return texWidth; }
    }

    /// <summary>Gets the height of the texture.</summary>
    /// <value>The height of the texture.</value>
    public int Height
    {
        get { return texHeight; }
    }

    /// <summary>Gets the aspect ratio of the texture.</summary>
    /// <value>The aspect ratio of the texture.</value>
    public float AspectRatio
    {
        get
        {
            if (texHeight == 0)
            {
                return 0.0f;
            }

            return (float)texWidth / (float)texHeight;
        }
    }

#if UNITY_ANDROID && !UNITY_EDITOR
    // Keep public so we can check for the dll being present at runtime.

    /// <summary>Creates the video player.</summary>
    /// <returns>A pointer to the newly-created video player.</returns>
    [DllImport(DLL_NAME)]
    public static extern IntPtr CreateVideoPlayer();

    // Keep public so we can check for the dll being present at runtime.

    /// <summary>Destroys the video player.</summary>
    /// <param name="videoPlayerPtr">A pointer to the video player.</param>
    [DllImport(DLL_NAME)]
    public static extern void DestroyVideoPlayer(IntPtr videoPlayerPtr);
#else
    /// @cond
    /// <summary>Creates the Video Player.</summary>
    /// <remarks>Make this public so we can test the loading of the DLL.</remarks>
    /// <returns>An integer pointer to the Video Player.</returns>
    public static IntPtr CreateVideoPlayer()
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return IntPtr.Zero;
    }

    /// @endcond
    /// @cond
    /// <summary>Destroys the Video Player.</summary>
    /// <remarks>Make this public so we can test the loading of the DLL.</remarks>
    /// <param name="videoPlayerPtr">A pointer to the video player.</param>
    public static void DestroyVideoPlayer(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }

    /// @endcond
#endif  // UNITY_ANDROID && !UNITY_EDITOR

    /// <summary>Sets the display texture.</summary>
    /// <param name="texture">
    /// Texture to display.  If `null`, the initial texture of the renderer is used.
    /// </param>
    public void SetDisplayTexture(Texture texture)
    {
        if (texture == null)
        {
            texture = initialTexture;
        }

        if (texture == null)
        {
            return;
        }

        if (screen != null)
        {
            screen.sharedMaterial.mainTexture = texture;
        }
    }

    /// <summary>Cleans up the current video player and texture.</summary>
    public void CleanupVideo()
    {
        Debug.Log("Cleaning Up video!");
        if (videoPlayerPtr != IntPtr.Zero)
        {
            DestroyVideoPlayer(videoPlayerPtr);
            videoPlayerPtr = IntPtr.Zero;
        }

        if (surfaceTexture != null)
        {
            Destroy(surfaceTexture);
            surfaceTexture = null;
        }

        if (screen != null)
        {
            screen.sharedMaterial.mainTexture = initialTexture;
        }
    }

    /// <summary>
    /// Reinitializes the current video player or creates one if there is no player.
    /// </summary>
    public void ReInitializeVideo()
    {
        if (screen != null)
        {
            screen.sharedMaterial.mainTexture = initialTexture;
        }

        if (videoPlayerPtr == IntPtr.Zero)
        {
            CreatePlayer();
        }

        Init();
    }

    /// <summary>Resets the video player.</summary>
    public void RestartVideo()
    {
        SetOnVideoEventCallback(OnRestartVideoEvent);

        string theUrl = ProcessURL();

        InitVideoPlayer(videoPlayerPtr, (int)videoType, theUrl,
            videoContentID,
            videoProviderId,
            useSecurePath,
            true);
        framecount = 0;
        lastVideoTimestamp = -1;
    }

    /// <summary>Set the volume level.</summary>
    /// <param name="val">The new volume level.</param>
    public void SetCurrentVolume(int val)
    {
        SetCurrentVolume(videoPlayerPtr, val);
    }

    /// <summary>Initialize the video player.</summary>
    /// <returns>Returns `true` if successful.</returns>
    public bool Init()
    {
        if (initialized)
        {
            Debug.Log("Skipping initialization: video player already loaded");
            return true;
        }

        if (videoURL == null || videoURL.Length == 0)
        {
            Debug.LogError("Cannot initialize with null videoURL");
            return false;
        }

        videoURL = videoURL == null ? "" : videoURL.Trim();
        videoContentID = videoContentID == null ? "" : videoContentID.Trim();
        videoProviderId = videoProviderId == null ? "" : videoProviderId.Trim();

        SetInitialResolution(videoPlayerPtr, (int)initialResolution);

        string theUrl = ProcessURL();
        Debug.Log("Playing " + videoType + " " + theUrl);
        Debug.Log("videoContentID = " + videoContentID);
        Debug.Log("videoProviderId = " + videoProviderId);
        videoPlayerPtr = InitVideoPlayer(videoPlayerPtr, (int)videoType, theUrl,
            videoContentID, videoProviderId,
            useSecurePath, false);
        IssuePlayerEvent(RenderCommand.InitializePlayer);
        initialized = true;
        framecount = 0;
        lastVideoTimestamp = -1;
        return videoPlayerPtr != IntPtr.Zero;
    }

    /// <summary>Play the video.</summary>
    /// <returns>Returns `true` if the video plays successfully, `false` otherwise.</returns>
    public bool Play()
    {
        if (!initialized)
        {
            Init();
        }

        if (videoPlayerPtr != IntPtr.Zero && IsVideoReady(videoPlayerPtr))
        {
            return PlayVideo(videoPlayerPtr) == 0;
        }
        else
        {
            Debug.LogError("Video player not ready to Play!");
            return false;
        }
    }

    /// <summary>Pauses video playback.</summary>
    /// <returns>Returns `true` if the operation is successful, `false` otherwise.</returns>
    public bool Pause()
    {
        if (!initialized)
        {
            Init();
        }

        if (VideoReady)
        {
            return PauseVideo(videoPlayerPtr) == 0;
        }
        else
        {
            Debug.LogError("Video player not ready to Pause!");
            return false;
        }
    }

    /// <summary>Removes the callback for exceptions.</summary>
    /// <param name="callback">The callback to remove.</param>
    public void RemoveOnVideoEventCallback(Action<int> callback)
    {
        if (onEventCallbacks != null)
        {
            onEventCallbacks.Remove(callback);
        }
    }

    /// <summary>Sets the callback for video events.</summary>
    /// <param name="callback">The callback to set for video events.</param>
    public void SetOnVideoEventCallback(Action<int> callback)
    {
        if (onEventCallbacks == null)
        {
            onEventCallbacks = new List<Action<int>>();
        }

        onEventCallbacks.Add(callback);
        SetOnVideoEventCallback(videoPlayerPtr, InternalOnVideoEventCallback,
            ToIntPtr(this));
    }

    /// <summary>Sets the callback for exceptions.</summary>
    /// <param name="callback">The callback to set.</param>
    public void SetOnExceptionCallback(Action<string, string> callback)
    {
        if (onExceptionCallbacks == null)
        {
            onExceptionCallbacks = new List<Action<string, string>>();
            SetOnExceptionCallback(videoPlayerPtr, InternalOnExceptionCallback,
                ToIntPtr(this));
        }

        onExceptionCallbacks.Add(callback);
    }

    /// <summary>Generates an integer pointer for a given object.</summary>
    /// <param name="obj">The object to generate an integer pointer for.</param>
    /// <returns>An integer pointer.</returns>
    internal static IntPtr ToIntPtr(System.Object obj)
    {
        GCHandle handle = GCHandle.Alloc(obj);
        return GCHandle.ToIntPtr(handle);
    }

    /// <summary>Fires off a video event.</summary>
    /// <param name="eventId">The ID of the event to fire.</param>
    internal void FireVideoEvent(int eventId)
    {
        if (onEventCallbacks == null)
        {
            return;
        }

        // Copy the collection so the callbacks can remove themselves from the list.
        Action<int>[] cblist = onEventCallbacks.ToArray();
        foreach (Action<int> cb in cblist)
        {
            try
            {
                cb(eventId);
            }
            catch (Exception e)
            {
                Debug.LogError("exception calling callback: " + e);
            }
        }
    }

    /// <summary>Fires all callbacks registered to `onExceptionCallbacks`.</summary>
    /// <param name="type">The `type` parameter for the callback.</param>
    /// <param name="msg">The `msg` parameter for the callback.</param>
    internal void FireOnException(string type, string msg)
    {
        if (onExceptionCallbacks == null)
        {
            return;
        }

        foreach (Action<string, string> cb in onExceptionCallbacks)
        {
            try
            {
                cb(type, msg);
            }
            catch (Exception e)
            {
                Debug.LogError("exception calling callback: " + e);
            }
        }
    }

    /// <summary>Processes the URL.</summary>
    /// <returns>The processed URL.</returns>
    internal string ProcessURL()
    {
        return videoURL.Replace("${Application.dataPath}", Application.dataPath);
    }

#if UNITY_ANDROID && !UNITY_EDITOR

    [DllImport(DLL_NAME)]
    private static extern IntPtr GetRenderEventFunc();

    [DllImport(DLL_NAME)]
    private static extern void SetExternalTextures(IntPtr videoPlayerPtr,
                                                   int[] texIds,
                                                   int size,
                                                   int w,
                                                   int h);

    [DllImport(DLL_NAME)]
    private static extern IntPtr GetRenderableTextureId(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int GetExternalSurfaceTextureId(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern void GetVideoMatrix(IntPtr videoPlayerPtr,
                                              float[] videoMatrix);

    [DllImport(DLL_NAME)]
    private static extern long GetVideoTimestampNs(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int GetVideoPlayerEventBase(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern IntPtr InitVideoPlayer(IntPtr videoPlayerPtr,
                                                 int videoType,
                                                 string videoURL,
                                                 string contentID,
                                                 string providerId,
                                                 bool useSecurePath,
                                                 bool useExisting);

    [DllImport(DLL_NAME)]
    private static extern void SetInitialResolution(IntPtr videoPlayerPtr,
                                                    int initialResolution);

    [DllImport(DLL_NAME)]
    private static extern int GetPlayerState(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int GetWidth(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int GetHeight(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int PlayVideo(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int PauseVideo(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern bool IsVideoReady(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern bool IsVideoPaused(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern long GetDuration(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern long GetBufferedPosition(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern long GetCurrentPosition(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern void SetCurrentPosition(IntPtr videoPlayerPtr,
                                                  long pos);

    [DllImport(DLL_NAME)]
    private static extern int GetBufferedPercentage(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int GetMaxVolume(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern int GetCurrentVolume(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern void SetCurrentVolume(IntPtr videoPlayerPtr,
                                                int value);

    [DllImport(DLL_NAME)]
    private static extern int GetStereoMode(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern bool HasProjectionData(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern bool SetVideoPlayerSupportClassname(
        IntPtr videoPlayerPtr,
        string classname);

    [DllImport(DLL_NAME)]
    private static extern IntPtr GetRawPlayer(IntPtr videoPlayerPtr);

    [DllImport(DLL_NAME)]
    private static extern void SetOnVideoEventCallback(IntPtr videoPlayerPtr,
                                                       OnVideoEventCallback callback,
                                                       IntPtr callback_arg);

    [DllImport(DLL_NAME)]
    private static extern void SetOnExceptionCallback(IntPtr videoPlayerPtr,
                                                      OnExceptionCallback callback,
                                                      IntPtr callback_arg);
#else

    private static IntPtr GetRenderEventFunc()
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return IntPtr.Zero;
    }

    private static void SetExternalTextures(IntPtr videoPlayerPtr,
                                            int[] texIds,
                                            int size,
                                            int w,
                                            int h)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }

    private static IntPtr GetRenderableTextureId(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return IntPtr.Zero;
    }

    private static int GetExternalSurfaceTextureId(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return 0;
    }

    private static void GetVideoMatrix(IntPtr videoPlayerPtr,
                                       float[] videoMatrix)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }

    private static long GetVideoTimestampNs(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    /// <summary>A pure-virtual method for getting a video player event base.</summary>
    /// <param name="videoPlayerPtr">A pointer to the video player.</param>
    /// <returns>An integer representing the success status.</returns>
    private static int GetVideoPlayerEventBase(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return 0;
    }

    private static IntPtr InitVideoPlayer(IntPtr videoPlayerPtr, int videoType,
                                          string videoURL,
                                          string contentID,
                                          string providerId,
                                          bool useSecurePath,
                                          bool useExisting)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return IntPtr.Zero;
    }

    private static void SetInitialResolution(IntPtr videoPlayerPtr,
                                             int initialResolution)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }

    private static int GetPlayerState(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    private static int GetWidth(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    private static int GetHeight(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    private static int PlayVideo(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return 0;
    }

    private static int PauseVideo(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return 0;
    }

    private static bool IsVideoReady(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return false;
    }

    private static bool IsVideoPaused(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return true;
    }

    private static long GetDuration(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    private static long GetBufferedPosition(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    private static long GetCurrentPosition(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    private static void SetCurrentPosition(IntPtr videoPlayerPtr, long pos)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }

    private static int GetBufferedPercentage(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return 0;
    }

    private static int GetMaxVolume(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return 0;
    }

    private static int GetCurrentVolume(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return 0;
    }

    private static void SetCurrentVolume(IntPtr videoPlayerPtr, int value)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }

    private static int GetStereoMode(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return -1;
    }

    private static bool HasProjectionData(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return false;
    }

    private static bool SetVideoPlayerSupportClassname(IntPtr videoPlayerPtr,
                                                       string classname)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return false;
    }

    private static IntPtr GetRawPlayer(IntPtr videoPlayerPtr)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
        return IntPtr.Zero;
    }

    private static void SetOnVideoEventCallback(IntPtr videoPlayerPtr,
                                                OnVideoEventCallback callback,
                                                IntPtr callback_arg)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }

    private static void SetOnExceptionCallback(IntPtr videoPlayerPtr,
                                               OnExceptionCallback callback,
                                               IntPtr callback_arg)
    {
        Debug.Log(NOT_IMPLEMENTED_MSG);
    }
#endif  // UNITY_ANDROID && !UNITY_EDITOR

    [AOT.MonoPInvokeCallback(typeof(OnVideoEventCallback))]
    private static void InternalOnVideoEventCallback(IntPtr cbdata, int eventId)
    {
        if (cbdata == IntPtr.Zero)
        {
            return;
        }

        GvrVideoPlayerTexture player;
        var gcHandle = GCHandle.FromIntPtr(cbdata);
        try
        {
            player = (GvrVideoPlayerTexture)gcHandle.Target;
        }
        catch (InvalidCastException e)
        {
            Debug.LogError("GC Handle pointed to unexpected type: " +
            gcHandle.Target + ". Expected " +
            typeof(GvrVideoPlayerTexture));
            throw e;
        }

        if (player != null)
        {
            executeOnMainThread.Enqueue(() => player.FireVideoEvent(eventId));
        }
    }

    [AOT.MonoPInvokeCallback(typeof(OnExceptionCallback))]
    private static void InternalOnExceptionCallback(string type, string msg,
                                            IntPtr cbdata)
    {
        if (cbdata == IntPtr.Zero)
        {
            return;
        }

        GvrVideoPlayerTexture player;
        var gcHandle = GCHandle.FromIntPtr(cbdata);
        try
        {
            player = (GvrVideoPlayerTexture)gcHandle.Target;
        }
        catch (InvalidCastException e)
        {
            Debug.LogError("GC Handle pointed to unexpected type: " +
            gcHandle.Target + ". Expected " +
            typeof(GvrVideoPlayerTexture));
            throw e;
        }

        if (player != null)
        {
            executeOnMainThread.Enqueue(() => player.FireOnException(type, msg));
        }
    }

    // Create the video player instance and the event base id.
    private void Awake()
    {
        videoMatrixRaw = new float[16];
        videoMatrixPropertyId = Shader.PropertyToID("video_matrix");

        // Defaults the Screen to the Renderer component on the same object as this script.
        // The Screen can also be set explicitly.
        Screen = GetComponent<Renderer>();

        CreatePlayer();
    }

    private void CreatePlayer()
    {
        videoPlayerPtr = CreateVideoPlayer();
        videoPlayerEventBase = GetVideoPlayerEventBase(videoPlayerPtr);
        Debug.Log(" -- " + gameObject.name + " created with base " +
        videoPlayerEventBase);

        SetOnVideoEventCallback((eventId) =>
        {
            Debug.Log("------------- E V E N T " + eventId + " -----------------");
            UpdateStatusText();
        });

        SetOnExceptionCallback((type, msg) =>
        {
            Debug.LogError("Exception: " + type + ": " + msg);
        });

        initialized = false;
    }

    private void OnDisable()
    {
        if (videoPlayerPtr != IntPtr.Zero)
        {
            if (GetPlayerState(videoPlayerPtr) == (int)VideoPlayerState.Ready)
            {
                PauseVideo(videoPlayerPtr);
            }
        }
    }

    private void OnDestroy()
    {
        CleanupVideo();
    }

    private void OnApplicationPause(bool bPause)
    {
        if (videoPlayerPtr != IntPtr.Zero)
        {
            if (bPause)
            {
                playOnResume = !IsPaused;
                PauseVideo(videoPlayerPtr);
            }
            else
            {
                if (playOnResume)
                {
                    PlayVideo(videoPlayerPtr);
                }
            }
        }
    }

    private void UpdateMaterial()
    {
        // Don't render if not initialized.
        if (videoPlayerPtr == IntPtr.Zero)
        {
            return;
        }

        texWidth = GetWidth(videoPlayerPtr);
        texHeight = GetHeight(videoPlayerPtr);

        int externalTextureId = GetExternalSurfaceTextureId(videoPlayerPtr);
        if (surfaceTexture != null
            && surfaceTexture.GetNativeTexturePtr().ToInt32() != externalTextureId)
        {
            Destroy(surfaceTexture);
            surfaceTexture = null;
        }

        if (surfaceTexture == null && externalTextureId != 0)
        {
            Debug.Log("Creating external texture with surface texture id " + externalTextureId);

            // Size of this texture doesn't really matter and can change on the fly anyway.
            surfaceTexture = Texture2D.CreateExternalTexture(4, 4, TextureFormat.RGBA32,
                false, false, new System.IntPtr(externalTextureId));
        }

        if (surfaceTexture == null)
        {
            return;
        }

        // Don't swap the textures if the video ended.
        if (PlayerState == VideoPlayerState.Ended)
        {
            return;
        }

        if (screen == null)
        {
            Debug.LogError("GvrVideoPlayerTexture: No screen to display the video is set.");
            return;
        }

        if (screen != null)
        {
            // Unity may build new a new material instance when assigning
            // material.x which can lead to duplicating materials each frame
            // whereas using the shared material will modify the original material.
            // Update the material's texture if it is different.
            if (screen.sharedMaterial.mainTexture == null ||
                screen.sharedMaterial.mainTexture.GetNativeTexturePtr() !=
                    surfaceTexture.GetNativeTexturePtr())
            {
                screen.sharedMaterial.mainTexture = surfaceTexture;
            }

            screen.sharedMaterial.SetMatrix(videoMatrixPropertyId, videoMatrix);
        }
    }

    private void OnRestartVideoEvent(int eventId)
    {
        if (eventId == (int)VideoEvents.VideoReady)
        {
            Debug.Log("Restarting video complete.");
            RemoveOnVideoEventCallback(OnRestartVideoEvent);
        }
    }

    /// <summary>Adjusts the aspect ratio.</summary>
    /// <remarks>
    /// This adjusts the transform scale to match the aspect ratio of the texture.
    /// </remarks>
    private void AdjustAspectRatio()
    {
        float aspectRatio = AspectRatio;
        if (aspectRatio == 0.0f)
        {
            return;
        }

        // set the y scale based on the x value
        Vector3 newscale = transform.localScale;
        newscale.y = Mathf.Min(newscale.y, newscale.x / aspectRatio);

        transform.localScale = newscale;
    }

    private void UpdateStatusText()
    {
        float fps = CurrentPosition > 0 ?
            (framecount / (CurrentPosition / 1000f)) : CurrentPosition;

        string status = texWidth + " x " + texHeight + " buffer: " +
                        (BufferedPosition / 1000) + " " + PlayerState + " fps: " + fps;
        if (statusText != null)
        {
            if (statusText.text != status)
            {
                statusText.text = status;
            }
        }
    }

    /// <summary>Issues the player event.</summary>
    /// <param name="evt">The event to send to the video player instance.</param>
    private void IssuePlayerEvent(RenderCommand evt)
    {
        if (renderEventFunction == IntPtr.Zero)
        {
            renderEventFunction = GetRenderEventFunc();
        }

        if (renderEventFunction == IntPtr.Zero || evt == RenderCommand.None)
        {
            Debug.LogError("Attempt to IssuePlayerEvent before renderEventFunction ready.");
            return;
        }

        GL.IssuePluginEvent(renderEventFunction, videoPlayerEventBase + (int)evt);
    }

    private void Update()
    {
        while (executeOnMainThread.Count > 0)
        {
            executeOnMainThread.Dequeue().Invoke();
        }

        if (VideoReady)
        {
            IssuePlayerEvent(RenderCommand.UpdateVideo);
            GetVideoMatrix(videoPlayerPtr, videoMatrixRaw);
            videoMatrix = GvrMathHelpers.ConvertFloatArrayToMatrix(videoMatrixRaw);
            long vidTimestamp = GetVideoTimestampNs(videoPlayerPtr);
            if (vidTimestamp != lastVideoTimestamp)
            {
                framecount++;
            }

            lastVideoTimestamp = vidTimestamp;

            UpdateMaterial();

            if (adjustAspectRatio)
            {
                AdjustAspectRatio();
            }

            if ((int)framecount % 30 == 0)
            {
                UpdateStatusText();
            }

            long bp = BufferedPosition;
            if (bp != lastBufferedPosition)
            {
                lastBufferedPosition = bp;
                UpdateStatusText();
            }
        }
    }
}