/* 
 * Copyright 2015 eje inc.
 * Copyright 2015 Samsung Electronics Co., LTD
 *
 * 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.
 */

package com.eje_c.meganekko.scene_objects;

import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.view.Surface;

import com.eje_c.meganekko.Material;
import com.eje_c.meganekko.Mesh;
import com.eje_c.meganekko.RenderData;
import com.eje_c.meganekko.SceneObject;
import com.eje_c.meganekko.VrFrame;

/**
 * A {@linkplain SceneObject scene object} that shows video, using the Android
 * {@link MediaPlayer}.
 */
public class VideoSceneObject extends SceneObject {
    private SurfaceTexture mSurfaceTexture = null;
    private MediaPlayer mMediaPlayer = null;
    private boolean mActive = true;

    /**
     * Play a video on a {@linkplain SceneObject scene object} with an
     * arbitrarily complex geometry, using the Android {@link MediaPlayer}. You
     * have to call {@code VideoSceneObject#setMediaPlayer(MediaPlayer)}} to
     * play video.
     */
    public VideoSceneObject() {
        // Setup material
        Material material = new Material();
        mSurfaceTexture = material.getSurfaceTexture();
        RenderData renderData = new RenderData();
        renderData.setMaterial(material);
        attachRenderData(renderData);
    }

    /**
     * Play a video on a {@linkplain SceneObject scene object} with an
     * arbitrarily complex geometry, using the Android {@link MediaPlayer}
     *
     * @param mesh        a {@link Mesh} - see
     *                    {@link #loadMesh(com.eje_c.meganekko.AndroidResource)}
     *                    and {@link #createQuad(float, float)}
     * @param mediaPlayer an Android {@link MediaPlayer}
     * @throws IllegalArgumentException on an invalid {@code videoType} parameter
     */
    public VideoSceneObject(Mesh mesh, MediaPlayer mediaPlayer) {
        super(mesh);

        Material material = new Material();
        mSurfaceTexture = material.getSurfaceTexture();
        getRenderData().setMaterial(material);

        mMediaPlayer = mediaPlayer;
    }

    /**
     * Poll the {@link MediaPlayer} once per frame.
     * <p/>
     * This call does not directly affect the {@link MediaPlayer}. In
     * particular, activation is not the same as calling
     * {@link MediaPlayer#start()}.
     */
    public void activate() {
        mActive = true;
    }

    /**
     * Stop polling the {@link MediaPlayer}.
     * <p/>
     * This call does not directly affect the {@link MediaPlayer}. In
     * particular, deactivation is not the same as calling
     * {@link MediaPlayer#pause()}.
     */
    public void deactivate() {
        mActive = false;
    }

    /**
     * Returns the current {@link MediaPlayer} status.
     * <p/>
     * See {@link #activate()} and {@link #deactivate()}: polling activation is
     * not correlated with the {@code MediaPlayer} state.
     *
     * @return Whether or not we polling the {@code MediaPlayer} every frame.
     */
    public boolean isActive() {
        return mActive;
    }

    /**
     * Returns the current {@link MediaPlayer}, if any
     *
     * @return current {@link MediaPlayer}
     */
    public MediaPlayer getMediaPlayer() {
        return mMediaPlayer;
    }

    /**
     * Sets the current {@link MediaPlayer}
     *
     * @param mediaPlayer An Android {@link MediaPlayer}
     */
    public void setMediaPlayer(MediaPlayer mediaPlayer) {
        release();// any current MediaPlayer

        mMediaPlayer = mediaPlayer;
        Surface surface = new Surface(mSurfaceTexture);
        mMediaPlayer.setSurface(surface);
        surface.release();
    }

    /**
     * Reset and {@link MediaPlayer#release() release()} the current
     * {@link MediaPlayer}, if any
     */
    public void release() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    /**
     * Returns the current time stamp, in nanoseconds. This comes from
     * {@link SurfaceTexture#getTimestamp()}: you should read the Android
     * documentation on that before you use this value.
     *
     * @return current time stamp, in nanoseconds
     */
    public long getTimeStamp() {
        return mSurfaceTexture.getTimestamp();
    }

    @Override
    public void update(VrFrame vrFrame) {
        if (mMediaPlayer != null && mActive) {
            mSurfaceTexture.updateTexImage();
        }
        super.update(vrFrame);
    }

    /**
     * Video type constants, for use with {@link VideoSceneObject}
     */
    public enum VideoType {
        MONO, HORIZONTAL_STEREO, VERTICAL_STEREO
    }
}

@@ -823,41 +823,8 @@
 al;%0A
-import com.eje_c.meganekko.Mesh;%0A
 impo
@@ -1793,787 +1793,8 @@
  %7D%0A%0A
-    /**%0A     * Play a video on a %7B@linkplain SceneObject scene object%7D with an%0A     * arbitrarily complex geometry, using the Android %7B@link MediaPlayer%7D%0A     *%0A     * @param mesh        a %7B@link Mesh%7D - see%0A     *                    %7B@link #loadMesh(com.eje_c.meganekko.AndroidResource)%7D%0A     *                    and %7B@link #createQuad(float, float)%7D%0A     * @param mediaPlayer an Android %7B@link MediaPlayer%7D%0A     * @throws IllegalArgumentException on an invalid %7B@code videoType%7D parameter%0A     */%0A    public VideoSceneObject(Mesh mesh, MediaPlayer mediaPlayer) %7B%0A        super(mesh);%0A%0A        Material material = new Material();%0A        mSurfaceTexture = material.getSurfaceTexture();%0A        getRenderData().setMaterial(material);%0A%0A        mMediaPlayer = mediaPlayer;%0A    %7D%0A%0A
     
@@ -4187,174 +4187,7 @@
    %7D
-%0A%0A    /**%0A     * Video type constants, for use with %7B@link VideoSceneObject%7D%0A     */%0A    public enum VideoType %7B%0A        MONO, HORIZONTAL_STEREO, VERTICAL_STEREO%0A    %7D
 %0A%7D%0A

Remove unused methods