javauis/mmapi_qt/baseline/javasrc/com/nokia/microedition/media/PlayerImpl.java
changeset 23 98ccebc37403
child 26 dc7c549001d5
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This class is native player implementation of Player interface
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.microedition.media;
       
    20 
       
    21 import javax.microedition.media.PlayerListener;
       
    22 import javax.microedition.media.MediaException;
       
    23 import com.nokia.microedition.media.NativeError;
       
    24 import com.nokia.mj.impl.rt.support.Finalizer;
       
    25 import com.nokia.mj.impl.utils.Logger;
       
    26 
       
    27 public class PlayerImpl extends PlayerBase
       
    28 {
       
    29     // CMMAPlayer handle
       
    30     protected int iPlayerHandle;
       
    31 
       
    32     // Controls container
       
    33     protected ControlContainer iControls;
       
    34 
       
    35     // object for waiting native asynchronous method calls
       
    36     protected Object iLockObject = new Object();
       
    37      protected Object iPrefetchLockObject = new Object();
       
    38 
       
    39     // native error code
       
    40     protected int iError;
       
    41 
       
    42     protected PlayerListenerImpl iPlayerListenerImpl;
       
    43 
       
    44     private Finalizer mFinalizer;
       
    45     
       
    46     private boolean iWaitFlag = true;
       
    47     
       
    48     private boolean iStartWaitFlag = true;
       
    49 
       
    50     /**
       
    51      * Constructor
       
    52      * @param aPlayerHandle handle to player (CMMAPlayer)
       
    53      */
       
    54     public PlayerImpl(int aPlayerHandle)
       
    55     {
       
    56         iPlayerHandle = aPlayerHandle;
       
    57 
       
    58         iPlayerListenerImpl = new PlayerListenerImpl(this);
       
    59 
       
    60         // Initialize player
       
    61         int err = _initPlayer(iPlayerListenerImpl,
       
    62                               ManagerImpl.getEventSource(),
       
    63                               iPlayerHandle);
       
    64         if (err < NativeError.KErrNone)
       
    65         {
       
    66             throw new OutOfMemoryError(NativeError.errorMessage(err));
       
    67         }
       
    68 
       
    69         mFinalizer = new Finalizer()
       
    70         {
       
    71             public void finalizeImpl()
       
    72             {
       
    73                 doFinalize();
       
    74             }
       
    75         };
       
    76 
       
    77         iControls = ControlContainer.populateControls(this,
       
    78                     ManagerImpl.getEventSource(),
       
    79                     iPlayerHandle);
       
    80     }
       
    81 
       
    82     private void doFinalize()
       
    83     {
       
    84         if (mFinalizer != null)
       
    85         {
       
    86             registeredFinalize();
       
    87             mFinalizer = null;
       
    88         }
       
    89     }
       
    90 
       
    91     /**
       
    92      * This method is called when garbage collection occurs
       
    93      * to delete native object
       
    94      */
       
    95     protected void registeredFinalize()
       
    96     {
       
    97         close();
       
    98     }
       
    99 
       
   100     /**
       
   101      * Returns native player handle.
       
   102      * @return Native player handle.
       
   103      */
       
   104     public int getPlayerHandle()
       
   105     {
       
   106         return iPlayerHandle;
       
   107     }
       
   108 
       
   109     /**
       
   110      * @return Player's PlayerListenerImpl
       
   111      */
       
   112     public PlayerListenerImpl getPlayerListenerImpl()
       
   113     {
       
   114         return iPlayerListenerImpl;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Adds control to the player.
       
   119      * @param aControl Player's control
       
   120      * @param aControlName Identifies the control. Name must contain
       
   121      * full package information
       
   122      */
       
   123     public void addControl(javax.microedition.media.Control aControl,
       
   124                            String aControlName)
       
   125     {
       
   126         iControls.addControl(aControl, aControlName);
       
   127     }
       
   128 
       
   129     /**
       
   130      * interface Controllable
       
   131      * Return controls
       
   132      * @return list of controls
       
   133      * @see Controllable
       
   134      */
       
   135     public javax.microedition.media.Control[] getControls()
       
   136     {
       
   137         closeCheck();
       
   138         unrealizedCheck();
       
   139         return iControls.getControls();
       
   140     }
       
   141 
       
   142     /**
       
   143      * interface Controllable
       
   144      * get control by content type
       
   145      * @param aControlType content type of wanted control
       
   146      * @return control
       
   147      * @see Controllable
       
   148      * @see Control
       
   149      */
       
   150     public javax.microedition.media.Control getControl(String aControlType)
       
   151     {
       
   152         closeCheck();
       
   153         unrealizedCheck();
       
   154         return iControls.getControl(aControlType);
       
   155     }
       
   156 
       
   157     /**
       
   158      * interface Player
       
   159      * @see Player
       
   160      */
       
   161     public void addPlayerListener(PlayerListener aPlayerListener)
       
   162     {
       
   163         closeCheck();
       
   164         iPlayerListenerImpl.addPlayerListener(aPlayerListener);
       
   165     }
       
   166 
       
   167     /**
       
   168      * From Player
       
   169      * @see Player
       
   170      */
       
   171     public void removePlayerListener(PlayerListener aPlayerListener)
       
   172     {
       
   173         closeCheck();
       
   174         iPlayerListenerImpl.removePlayerListener(aPlayerListener);
       
   175     }
       
   176 
       
   177     /**
       
   178      * From Player
       
   179      * @see Player
       
   180      */
       
   181     public int getState()
       
   182     {
       
   183         int state = CLOSED;
       
   184         if (iState != CLOSED)
       
   185         {
       
   186             state = _getState(ManagerImpl.getEventSource(), iPlayerHandle);
       
   187         }
       
   188         return state;
       
   189     }
       
   190 
       
   191 
       
   192     /**
       
   193      * From PlayerBase
       
   194      * @see PlayerBase
       
   195      */
       
   196     public void doRealize() throws MediaException
       
   197     {
       
   198         int err = _realize(ManagerImpl.getEventSource(), iPlayerHandle);
       
   199 
       
   200         if (err < NativeError.KErrNone)
       
   201         {
       
   202             throw new MediaException(
       
   203                 "Realize failed, Symbian OS error: " + err);
       
   204         }
       
   205     }
       
   206 
       
   207     /**
       
   208      * From PlayerBase
       
   209      * @see PlayerBase
       
   210      */
       
   211     public void doPrefetch() throws MediaException
       
   212     {
       
   213     	Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doPrefetch");
       
   214         synchronized(iPrefetchLockObject)
       
   215         { 
       
   216         	if (PREFETCHED == getState())
       
   217         	return;
       
   218          synchronized (iLockObject)
       
   219          {
       
   220         	Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doPrefetch inside try"+Thread.currentThread().getName());
       
   221           //int retval[] = new int[1];
       
   222       		 int err = _prefetch(ManagerImpl.getEventSource(), iPlayerHandle);
       
   223           Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doPrefetch err = " + err);
       
   224           if (err < NativeError.KErrNone)
       
   225           {
       
   226               throw new MediaException(
       
   227               "Prefetch failed, Symbian OS error: " + err);
       
   228           }
       
   229           if(iWaitFlag)
       
   230           {
       
   231            try
       
   232             {
       
   233                 // wait until actionCompleted( int aError ) is called
       
   234                 iLockObject.wait();
       
   235             }
       
   236             catch (InterruptedException ie)
       
   237             {
       
   238                 // cannot occur
       
   239             }
       
   240           }
       
   241          Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doPrefetch Sync end");
       
   242 	        } // end   synchronized (iLockObject)
       
   243    	   }
       
   244         if (iError < NativeError.KErrNone)
       
   245         {
       
   246             throw new MediaException(
       
   247                 "Prefetch failed, Symbian OS error: " + iError);
       
   248         }
       
   249     }
       
   250 
       
   251     /**
       
   252      * Notify that action is completed. Called from native side.
       
   253      * @param aError error code
       
   254      */
       
   255     private void actionCompleted(int aError)
       
   256     {
       
   257     	  Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"actionCompleted callback11 aError ="+ aError + "    "+Thread.currentThread().getName());
       
   258         iError = aError;
       
   259 
       
   260         synchronized (iLockObject)
       
   261        {
       
   262         	Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"actionCompleted callback inside synchronized (iLockObject)");
       
   263             iLockObject.notify();
       
   264         }
       
   265     }
       
   266     
       
   267     private void actionCompletedFile()
       
   268     {
       
   269         Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : actionCompletedFile");
       
   270         iWaitFlag = false;       
       
   271         
       
   272    }
       
   273     
       
   274     private void actionCompletedStart()
       
   275     {
       
   276         Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : actionCompletedStart");
       
   277         iStartWaitFlag = false;       
       
   278         
       
   279     }
       
   280 
       
   281     /**
       
   282      * From PlayerBase
       
   283      * @see PlayerBase
       
   284      */
       
   285     public void doDeallocate()
       
   286     {
       
   287         int ret = _deallocate(ManagerImpl.getEventSource(), iPlayerHandle);
       
   288         if (ret < NativeError.KErrNone)
       
   289         {
       
   290             Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,
       
   291                        "_deallocate() failed: err=" + ret);
       
   292         }
       
   293     }
       
   294 
       
   295     /**
       
   296      * From Player.
       
   297      * @see Player
       
   298      */
       
   299     public void start() throws MediaException
       
   300     {
       
   301     Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : start()");
       
   302     prefetch();
       
   303 			 Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl, start, state = "+getState());
       
   304     // Only preteched player may be started. If player is already started
       
   305     // this method returns silently.
       
   306     if (getState() == PREFETCHED)
       
   307     {
       
   308          synchronized (iLockObject)
       
   309          {        
       
   310              Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl, before calling native _start() thread is =" +Thread.currentThread().getName());
       
   311             	Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl, before calling native _start()");
       
   312              int err = _start(ManagerImpl.getEventSource(), iPlayerHandle);
       
   313              Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl, before calling native _start( ) returned : err = "+err);
       
   314              if (err < NativeError.KErrNone)
       
   315              {
       
   316                 throw new MediaException(
       
   317                 "Start failed, Symbian OS error: " + err);
       
   318              }
       
   319              if (iError < NativeError.KErrNone)
       
   320              {
       
   321                     throw new MediaException(
       
   322                     "Start failed, Symbian OS error: " + iError);
       
   323              }
       
   324              if(iStartWaitFlag)
       
   325              {
       
   326                 try
       
   327                 {
       
   328                       // wait until actionCompleted( int aError ) is called
       
   329                        iLockObject.wait();   // only for tck run
       
   330                  }
       
   331                  catch (InterruptedException ie)
       
   332                  {
       
   333                     // cannot occur
       
   334                  }
       
   335                 
       
   336              }
       
   337           }
       
   338     }
       
   339     }
       
   340 
       
   341     /**
       
   342      * From PlayerBase
       
   343      * @see PlayerBase
       
   344      */
       
   345     public void doStop() throws MediaException
       
   346     {
       
   347         /*
       
   348         Stops the Player. It will pause the playback at the current media time.
       
   349         Calling start will resume the playback from where it is stopped.
       
   350         If stop is called on a stopped Player, the request is ignored.
       
   351         */
       
   352         Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doStop()");
       
   353         int err = _stop(ManagerImpl.getEventSource(), iPlayerHandle);
       
   354         if (err < NativeError.KErrNone)
       
   355         {
       
   356             throw new MediaException(
       
   357                 "Stop failed, Symbian OS error: " + err);
       
   358         }
       
   359         // After this request is completed,
       
   360         // the Player is in the PREFETCHED state.
       
   361     }
       
   362 
       
   363     /**
       
   364      * From PlayerBase
       
   365      * @see PlayerBase
       
   366      */
       
   367     public void doClose()
       
   368     {
       
   369     	Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doclose() 1");
       
   370         _close(ManagerImpl.getEventSource(), iPlayerHandle);
       
   371 
       
   372         if (iPlayerHandle > NativeError.KErrNone)  // construction has succeed
       
   373         {
       
   374             Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doclose() 2");
       
   375             // notify the controls
       
   376             iControls.dispose();
       
   377             Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doclose() 3");
       
   378 
       
   379             // delete the native object
       
   380             _dispose(ManagerImpl.getEventSource(), iPlayerHandle);
       
   381             Logger.LOG(Logger.EJavaMMAPI, Logger.EInfo,"PlayerImpl.java : doclose() 4");
       
   382             iPlayerHandle = 0; // Do not use native object anymore
       
   383         }
       
   384     }
       
   385     /**
       
   386      * interface Player
       
   387      * @see Player
       
   388      */
       
   389     public void setLoopCount(int aCount)
       
   390     {
       
   391         super.setLoopCount(aCount);
       
   392         _setLoopCount(ManagerImpl.getEventSource(), iPlayerHandle, aCount);
       
   393     }
       
   394 
       
   395     /**
       
   396      * interface Player
       
   397      * @see Player
       
   398      */
       
   399     public long getDuration()
       
   400     {
       
   401         closeCheck();
       
   402         long duration = _duration(ManagerImpl.getEventSource(), iPlayerHandle);
       
   403         if (duration < NativeError.KErrNone)
       
   404         {
       
   405             duration = TIME_UNKNOWN;
       
   406         }
       
   407         return duration;
       
   408     }
       
   409 
       
   410     /**
       
   411      * From PlayerBase
       
   412      * @see PlayerBase
       
   413      */
       
   414     public long setMediaTime(long aNow) throws MediaException
       
   415     {
       
   416         long time = _setMediaTime(ManagerImpl.getEventSource(),
       
   417                                   iPlayerHandle,
       
   418                                   super.setMediaTime(aNow));
       
   419         if (time < NativeError.KErrNone)
       
   420         {
       
   421             throw new MediaException(
       
   422                 "Could not set media time, Symbian OS error: " + time);
       
   423         }
       
   424         return time;
       
   425     }
       
   426 
       
   427     /**
       
   428      * interface Player
       
   429      * @see Player
       
   430      */
       
   431     public long getMediaTime()
       
   432     {
       
   433         closeCheck();
       
   434         long time = _getMediaTime(ManagerImpl.getEventSource(), iPlayerHandle);
       
   435         if (time < NativeError.KErrNone)
       
   436         {
       
   437             time = TIME_UNKNOWN;
       
   438         }
       
   439         return time;
       
   440     }
       
   441 
       
   442 
       
   443     /**
       
   444      * interface Player
       
   445      * @see Player
       
   446      */
       
   447     public String getContentType()
       
   448     {
       
   449         closeCheck();
       
   450         unrealizedCheck();
       
   451 
       
   452         return _getContentType(iPlayerHandle);
       
   453     }
       
   454 
       
   455     // JNI
       
   456     private native int _initPlayer(Object aPlayerListenerObject,
       
   457                                    int aEventSource,
       
   458                                    int aPlayerHandle);
       
   459 
       
   460     private static native int _start(int aEventPoster, int aPlayer);
       
   461     private static native int _stop(int aEventPoster, int aPlayer);
       
   462     private static native int _close(int aEventPoster, int aPlayer);
       
   463     private static native int _prefetch(int aEventPoster, int aPlayer);
       
   464 
       
   465     private static native int _setLoopCount(int aEventPoster, int aPlayer,
       
   466                                             int aLoopCount);
       
   467 
       
   468     private static native int _realize(int aEventPoster, int aPlayer);
       
   469     private static native long _duration(int aEventPoster, int aPlayer);
       
   470     private static native long _setMediaTime(int aEventPoster, int aPlayer,
       
   471             long aNow);
       
   472 
       
   473     private static native int _deallocate(int aEventPoster, int aPlayer);
       
   474     private static native long _getMediaTime(int aEventPoster, int aPlayer);
       
   475     private static native int _getState(int aEventPoster, int aPlayer);
       
   476 
       
   477     protected static native int _addSourceStream(int aEventPoster,
       
   478             int aPlayerHandle,
       
   479             Object aSourceStreamReader);
       
   480 
       
   481     private static native String _getContentType(int aPlayerHandle);
       
   482 
       
   483     private static native void _dispose(int aEventSourceHandle,
       
   484                                         int aObjectHandle);
       
   485 }
       
   486 
       
   487 // End of File