FileDocCategorySizeDatePackage
DirectVideo.javaAPI DocphoneME MR2 API (J2ME)19287Wed May 02 16:47:08 BST 2007com.sun.mmedia

DirectVideo

public class DirectVideo extends DirectPlayer implements MIDPVideoPainter, ImplicitlyTrustedClass, javax.microedition.media.control.VideoControl
Video direct player it implements VideoControl

Fields Summary
private final int
SCREEN_WIDTH
private final int
SCREEN_HEIGHT
private final int
DEFAULT_WIDTH
private final int
DEFAULT_HEIGHT
private final int
ALPHA_COLOR
private static final int
FAKE_PREVIEW_INTERVAL
private javax.microedition.lcdui.Canvas
canvas
private DVItem
item
private int
sw
private int
sh
private int
dx
private int
dy
private int
dw
private int
dh
private boolean
visible
private boolean
hidden
private boolean
started
private boolean
locationInited
private int
displayMode
private MMHelper
mmh
private Object
boundLock
private static SecurityToken
classSecurityToken
This class has a different security domain than the MIDlet suite
Constructors Summary
public DirectVideo()

    
    // native functions /////////////////////////////////////////////

    // Get video width
        
    // Get video height
        
    // Set display location of video
                
    // Get snapshot
          
    // Set visible
          
    // Get screen full width
       
    // Get screen full height
       
    // Turn on or off alpha channel
          

    // member functions /////////////////////////////////////////////

      
    
Methods Summary
protected voidcheckPermission()
Check snapshot permission

        try {
            Scheduler scheduler = Scheduler.getScheduler();
            MIDletSuite midletSuite = scheduler.getMIDletSuite();
            midletSuite.checkForPermission(Permissions.MM_IMAGE_CAPTURING, null);
        } catch (InterruptedException e) {
            throw new SecurityException(
                    "Interrupted while trying to ask the user permission");
        }
    
protected voidcheckState()
Check mode value

        if (displayMode == -1) {
            throw new IllegalStateException("initDisplayMode not called yet");
        }
    
protected javax.microedition.media.ControldoGetControl(java.lang.String type)
Override doGetControl return VideoControl and GUIControl

        Control c = super.doGetControl(type);

        if (c == null) {
            String prefix = "javax.microedition.media.control.";
            if (type.equals(prefix + vicName)) {        // VideoControl
                return this;
            } else if (type.equals(prefix + guiName)) {  // GUIControl
                return this;
            }
        }
        return c;
    
protected voiddoRealize()
Override doRealize Prepare soure video informations

        super.doRealize();
        sw = nGetWidth(hNative);
        sh = nGetHeight(hNative);
        // initialize default rendering width and height
        if (sw <= 0) dw = DEFAULT_WIDTH;
        else dw = sw;
        if (sh <= 0) dh = DEFAULT_HEIGHT;
        else dh = sh;
    
protected booleandoStart()

        started = true;
        repaintAndWait();
        return super.doStart();
    
protected voiddoStop()

        started = false;
        super.doStop();
    
public intgetDisplayHeight()
Get actual height of rendering

        checkState();
        return dh;
    
public intgetDisplayWidth()
Get actual width of rendering

        checkState();
        return dw;
    
public intgetDisplayX()

        return dx;
    
public intgetDisplayY()

        return dy;
    
public byte[]getSnapshot(java.lang.String imageType)
There is no snap shot support now

        checkState();
        throw new MediaException("No snapshot support");
    
public intgetSourceHeight()
return source video height

        return sh;
    
public intgetSourceWidth()
return source video width

        return sw;
    
public voidhideVideo()
Hide video preview (called from CanvasLFImpl)

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "hideVideoPreview"); 
        }
        hidden = true;
        nSetAlpha(true, ALPHA_COLOR);
        repaint();
    
public java.lang.ObjectinitDisplayMode(int mode, java.lang.Object container)
Init display mode

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "initDisplayMode mode=" + mode + ", container=" + container); 
        }     
        
        Object ret = null;
        
        if (displayMode != -1) {
            throw new IllegalStateException("mode already set");
        }
        if (mode != USE_DIRECT_VIDEO && mode != USE_GUI_PRIMITIVE) {
            throw new IllegalArgumentException("unsupported mode");
        }
        if (mode == USE_DIRECT_VIDEO && !(container instanceof Canvas)) {
            throw new IllegalArgumentException("container needs to be a Canvas");
        }
        if (mode == USE_GUI_PRIMITIVE && container != null) {
            if (!(container instanceof String)) {
                throw new IllegalArgumentException("container not valid");
            }
            if (!(container.equals("javax.microedition.lcdui.Item"))) {
                throw new IllegalArgumentException("container not valid");
            }
        }

        if (mode == USE_DIRECT_VIDEO) {
            canvas = (Canvas)container;
            if (mmh == null) {
                mmh = MMHelper.getMMHelper();
                if (mmh == null) {
                    throw new RuntimeException("initDisplayMode: unable to set the display mode");
                }
            }
            displayMode = mode;
            // register this direct video handler to MMH
            // MMH used to communicate with Canvas
            mmh.registerPlayer(canvas, this);
            setDisplayLocation(dx, dy);
        } else {
            displayMode = mode;
            item = new DVItem(null);
            ret = (Object)item;
            visible = true;
        }
        
        return ret;
    
public final voidinitSecurityToken(SecurityToken token)
Initializes the security token for this class, so it can perform actions that a normal MIDlet Suite cannot.

param
token security token for this class.

        if (classSecurityToken != null) {
            return;
        }
        classSecurityToken = token;
    
private booleanisInClippingArea(javax.microedition.lcdui.Graphics g, int x, int y, int w, int h)
Is in clipping area?

        int diffx = g.getTranslateX();
        int diffy = g.getTranslateY();
        int clipx = g.getClipX();
        int clipy = g.getClipY();
        int clipw = g.getClipWidth();
        int cliph = g.getClipHeight();

        x += diffx;
        y += diffy;
        clipx += diffx;
        clipy += diffy;

        if (x < clipx) return false;
        if (y < clipy) return false;
        if (x + w > clipx + clipw) return false;
        if (y + h > clipy + cliph) return false;

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "isInClippingArea return true - No graphic outside of clipping area"); 
        }  

        return true;
    
protected native intnGetHeight(int handle)

private native intnGetScreenHeight()

private native intnGetScreenWidth()

protected native intnGetWidth(int handle)

private native intnSetAlpha(boolean on, int color)

protected native booleannSetLocation(int handle, int x, int y, int w, int h)

protected native booleannSetVisible(int handle, boolean visible)

protected native byte[]nSnapShot(int handle, java.lang.String imageType)

public voidpaintVideo(javax.microedition.lcdui.Graphics g)
called from Canvas.paint routine We have to paint direct video region on Canvas Notice: This have to be done before device painting action Zoran ESDK use mask color to draw direct video

        int x, y, w, h;
        
        synchronized(boundLock) {
            x = dx;
            y = dy;
            w = dw;
            h = dh;
        }

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "paintVideo x=" + x + ",y=" + y + ",w=" + w + ",h=" + h); 
        }

        if (hidden) {
            prepareClippedPreview(g, x, y, w, h);
        } else if (visible && started) {
            if (true == isInClippingArea(g, x, y, w, h)) {
                prepareVideoSurface(g, x, y, w, h);
            } else {
                int cx = g.getClipX();
                int cy = g.getClipY();
                int cw = g.getClipWidth();
                int ch = g.getClipHeight();
                g.setClip(x, y, w, h);
                prepareClippedPreview(g, x, y, w, h);
                g.setClip(cx, cy, cw, ch);
            }
        }
    
private voidprepareClippedPreview(javax.microedition.lcdui.Graphics g, int x, int y, int w, int h)
Prepare clipped preview region by using alpha channel masking

        if (1 == nSetAlpha(true, ALPHA_COLOR)) {
            g.setColor(0, 0, 8);    // IMPL NOTE - Consider RGB565 conversion
            g.fillRect(x, y, w, h);
            setTranslatedVideoLocation(g, x, y, w, h);
            if (hNative != 0) {
                nSetVisible(hNative, true);
            }
        } else {
            if (hNative != 0) {
                nSetVisible(hNative, false);
            }
        }
    
private voidprepareVideoSurface(javax.microedition.lcdui.Graphics g, int x, int y, int w, int h)
Prepare direct video rendering surface

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "prepareVideoSurface " + x + "," + y + "," + w + "," + h); 
        }    
 
        // Turn off alpha channel
        nSetAlpha(false, ALPHA_COLOR);
        setTranslatedVideoLocation(g, x, y, w, h);

        // set location and size of display region
        if (hNative != 0) {
            nSetVisible(hNative, true);
        }
    
private voidrepaint()
request to repaint

        if (canvas != null) {
            canvas.repaint();
        } else if (item != null) {
            item.forcePaint();
        }
    
private voidrepaintAndWait()
request to repaint canvas and wait until that processed

        if (canvas != null) {
            canvas.repaint();
            canvas.serviceRepaints();
        } else if (item != null) {
            item.forcePaint();
        }
    
public voidsetDisplayFullScreen(boolean fullScreenMode)
There is no full screen mode now

        checkState();
        if (fullScreenMode) {
            throw new MediaException("No Fullscreen mode");
        }
    
public voidsetDisplayLocation(int x, int y)
Set display location

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "setDisplayLocation x=" + x + ",y=" + y); 
        }
        checkState();
        if (displayMode == USE_DIRECT_VIDEO) {
            synchronized(boundLock) {
                dx = x;
                dy = y;
            }
            if (dw != 0 && dh !=0) {
                repaintAndWait();
            }
        }
    
public voidsetDisplaySize(int width, int height)
Set display size

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "setDisplaySize w=" + width + ",h=" + height); 
        }

        boolean sizeChanged = false;
        
        checkState();
        if (width < 1 || height < 1) {
            throw new IllegalArgumentException("invalid size");
        }

        synchronized(boundLock) {
            if (dw != width && dh != height) sizeChanged = true;
            dw = width;
            dh = height;
        }
        if (item != null) {
            // this will raise sizeChanged event
            // and sizeChanged shall raise paint event also
            item.setPreferredSize(width, height);
        }
        repaintAndWait();
        
        if (sizeChanged) {
            sendEvent(PlayerListener.SIZE_CHANGED, this);
        }
    
private voidsetTranslatedVideoLocation(javax.microedition.lcdui.Graphics g, int x, int y, int w, int h)

        int diffx = g.getTranslateX();
        int diffy = g.getTranslateY();
        int px, py, pw, ph;
 
        // Calcurate positions
        // And, do a physical clipping
        // Currently, Zoran chipset does not support negative position and exceed position
        px = x + diffx;
        py = y + diffy;
        pw = w;
        ph = h;

        if (px + pw <= 0) {
            return;
        }
        if (py + ph <= 0) {
            return;
        }
        if (px >= SCREEN_WIDTH) {
            return;
        }
        if (py >= SCREEN_HEIGHT) {
            return;
        }
        if (px < 0) {
            pw += px;
            px = 0;
        }
        if (py < 0) {
            ph += py;
            py = 0;
        }
        if (px + pw > SCREEN_WIDTH) {
            pw = SCREEN_WIDTH - px;
        }
        if (py + ph > SCREEN_HEIGHT) {
            ph = SCREEN_HEIGHT - py;
        }

        if (hNative != 0) {
            nSetLocation(hNative, px, py, pw, ph);
        }
    
public voidsetVisible(boolean visible)
set visible or unvisible

        boolean old = this.visible;
        checkState();
        this.visible = visible;

        if (old != visible) {
            repaintAndWait();
        }

        if (visible == false && hNative != 0) {
            nSetVisible(hNative, false);
        }
    
public voidshowVideo()
Show video preview (called from CanvasLFImpl)

        if (debug) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_MMAPI, 
                "showVideoPreview"); 
        }
        hidden = false;
        nSetAlpha(false, ALPHA_COLOR);        
        repaint();