基於Platinum庫的DMR實現(android)

轉載地址:http://blog.csdn.net/lancees/article/details/8951679

本例所採用的upnp框架是Platinum SDK

官方網址是http://www.plutinosoft.com/platinum 

該庫是一個跨平臺的C++庫,利用該庫,可以很容易就構建出DLNA/UPnP控制點 (DLNA/UPnP Control Point)DLNA/UPnP設備(DLNA/UPnP Device),其中包括有UPnP AV Media Server, Media Render & Control Point的例子 

該庫穩定強大,被很多知名產品所沿用,口碑較好自然也爲樓主所青睞

關於該庫如何編譯,請參考這篇博文:NDK下 將Platinum SDK 編譯成so庫 


下面給出運行效果圖:









JNI接口文件:

[java] view plaincopy
  1. public class PlatinumJniProxy {  
  2.   
  3.     static {  
  4.         System.loadLibrary("git-platinum");  
  5.     }     
  6.    
  7.     public static native int startMediaRender(byte[] friendname ,byte[] uuid);  
  8.     public static native int stopMediaRender();    
  9.     public static native boolean responseGenaEvent(int cmd, byte[] value ,byte[] data);    
  10.     public static native boolean enableLogPrint(boolean flag);  
  11.    
  12.       
  13.     public static  int startMediaRender(String friendname ,String uuid){  
  14.         if (friendname == null)friendname = "";  
  15.         if (uuid == null)uuid = "";  
  16.         int ret = -1;  
  17.         try {  
  18.             ret = startMediaRender(friendname.getBytes("utf-8"), uuid.getBytes("utf-8"));  
  19.         } catch (UnsupportedEncodingException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.         return ret;  
  23.     }  
  24.       
  25.     public static  boolean responseGenaEvent(int cmd, String value, String data){  
  26.         if (value == null)value = "";  
  27.         if (data == null)data = "";  
  28.         boolean ret = false;  
  29.         try {  
  30.             ret = responseGenaEvent(cmd, value.getBytes("utf-8"), data.getBytes("utf-8"));  
  31.         } catch (UnsupportedEncodingException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         return ret;  
  35.     }  
  36.       
  37.       
  38. }  

反射類:

[java] view plaincopy
  1. public class PlatinumReflection {  
  2.       
  3.     private static final CommonLog log = LogFactory.createLog();  
  4.       
  5.     private static final int MEDIA_RENDER_CTL_MSG_BASE = 0x100;  
  6.     /*----------------------------------------------------------------*/  
  7.     public static final int MEDIA_RENDER_CTL_MSG_SET_AV_URL = (MEDIA_RENDER_CTL_MSG_BASE+0);  
  8.     public static final int MEDIA_RENDER_CTL_MSG_STOP = (MEDIA_RENDER_CTL_MSG_BASE+1);  
  9.     public static final int MEDIA_RENDER_CTL_MSG_PLAY = (MEDIA_RENDER_CTL_MSG_BASE+2);  
  10.     public static final int MEDIA_RENDER_CTL_MSG_PAUSE = (MEDIA_RENDER_CTL_MSG_BASE+3);  
  11.     public static final int MEDIA_RENDER_CTL_MSG_SEEK = (MEDIA_RENDER_CTL_MSG_BASE+4);  
  12.     public static final int MEDIA_RENDER_CTL_MSG_SETVOLUME = (MEDIA_RENDER_CTL_MSG_BASE+5);  
  13.     public static final int MEDIA_RENDER_CTL_MSG_SETMUTE = (MEDIA_RENDER_CTL_MSG_BASE+6);  
  14.     public static final int MEDIA_RENDER_CTL_MSG_SETPLAYMODE = (MEDIA_RENDER_CTL_MSG_BASE+7);  
  15.     public static final int MEDIA_RENDER_CTL_MSG_PRE = (MEDIA_RENDER_CTL_MSG_BASE+8);  
  16.     public static final int MEDIA_RENDER_CTL_MSG_NEXT = (MEDIA_RENDER_CTL_MSG_BASE+9);  
  17.     /*----------------------------------------------------------------*/      
  18.       
  19.       
  20.       
  21.     /*----------------------------------------------------------------*/      
  22.     /* 
  23.      *       
  24.      *  
  25.      * */  
  26.     public static final int MEDIA_RENDER_TOCONTRPOINT_SET_MEDIA_DURATION = (MEDIA_RENDER_CTL_MSG_BASE+0);  
  27.     public static final int MEDIA_RENDER_TOCONTRPOINT_SET_MEDIA_POSITION = (MEDIA_RENDER_CTL_MSG_BASE+1);  
  28.     public static final int MEDIA_RENDER_TOCONTRPOINT_SET_MEDIA_PLAYINGSTATE = (MEDIA_RENDER_CTL_MSG_BASE+2);  
  29.     /*----------------------------------------------------------------*/  
  30.     public static final String RENDERER_TOCONTRPOINT_CMD_INTENT_NAME="com.geniusgithub.platinum.tocontrolpointer.cmd.intent";  
  31.     public static final String GET_RENDERER_TOCONTRPOINT_CMD="get_dlna_renderer_tocontrolpointer.cmd";  
  32.     public static final String GET_PARAM_MEDIA_DURATION="get_param_media_duration";  
  33.     public static final String GET_PARAM_MEDIA_POSITION="get_param_media_position";  
  34.     public static final String GET_PARAM_MEDIA_PLAYINGSTATE="get_param_media_playingstate";  
  35.     /*----------------------------------------------------------------*/  
  36.       
  37.   
  38.     public static final String MEDIA_PLAYINGSTATE_STOP="STOPPED";  
  39.     public static final String MEDIA_PLAYINGSTATE_PAUSE="PAUSED_PLAYBACK";  
  40.     public static final String MEDIA_PLAYINGSTATE_PLAYING="PLAYING";  
  41.     public static final String MEDIA_PLAYINGSTATE_TRANSTION="TRANSITIONING";  
  42.     public static final String MEDIA_PLAYINGSTATE_NOMEDIA="NO_MEDIA_PRESENT";  
  43.       
  44.     /*----------------------------------------------------------------*/  
  45.     public static final String MEDIA_SEEK_TIME_TYPE_REL_TIME="REL_TIME";      
  46.     public static final String MEDIA_SEEK_TIME_TYPE_TRACK_NR="TRACK_NR";  
  47.       
  48.       
  49.     public static interface ActionReflectionListener{  
  50.         public void onActionInvoke(int cmd,String value,String data);  
  51.     }  
  52.       
  53.     private static ActionReflectionListener mListener;  
  54.       
  55.       
  56.     public static void onActionReflection(int cmd,String value,String data){  
  57.         if (mListener != null){  
  58.             mListener.onActionInvoke(cmd, value, data);  
  59.         }  
  60.     }  
  61.       
  62.     public static void setActionInvokeListener(ActionReflectionListener listener){  
  63.         mListener = listener;  
  64.     }  
  65. }  


工作線程 DMRWorkThread

[java] view plaincopy
  1. public class DMRWorkThread extends Thread implements IBaseEngine{  
  2.   
  3.   
  4.     private static final CommonLog log = LogFactory.createLog();  
  5.       
  6.     private static final int CHECK_INTERVAL = 30 * 1000;   
  7.       
  8.     private Context mContext = null;  
  9.     private boolean mStartSuccess = false;  
  10.     private boolean mExitFlag = false;  
  11.       
  12.     private String mFriendName = "";  
  13.     private String mUUID = "";    
  14.     private RenderApplication mApplication;  
  15.       
  16.     public DMRWorkThread(Context context){  
  17.         mContext = context;  
  18.         mApplication = RenderApplication.getInstance();  
  19.     }  
  20.       
  21.     public void  setFlag(boolean flag){  
  22.         mStartSuccess = flag;  
  23.     }  
  24.       
  25.     public void setParam(String friendName, String uuid){  
  26.         mFriendName = friendName;  
  27.         mUUID = uuid;  
  28.         mApplication.updateDevInfo(mFriendName, mUUID);  
  29.     }  
  30.       
  31.     public void awakeThread(){  
  32.         synchronized (this) {  
  33.             notifyAll();  
  34.         }  
  35.     }  
  36.       
  37.     public void exit(){  
  38.         mExitFlag = true;  
  39.         awakeThread();  
  40.     }  
  41.   
  42.     @Override  
  43.     public void run() {  
  44.   
  45.         log.e("DMRWorkThread run...");  
  46.           
  47.         while(true)  
  48.         {  
  49.             if (mExitFlag){  
  50.                 stopEngine();  
  51.                 break;  
  52.             }  
  53.             refreshNotify();  
  54.             synchronized(this)  
  55.             {                 
  56.                 try  
  57.                 {  
  58.                     wait(CHECK_INTERVAL);  
  59.                 }  
  60.                 catch(Exception e)  
  61.                 {  
  62.                     e.printStackTrace();  
  63.                 }                                 
  64.             }  
  65.             if (mExitFlag){  
  66.                 stopEngine();  
  67.                 break;  
  68.             }  
  69.         }  
  70.           
  71.         log.e("DMRWorkThread over...");  
  72.           
  73.     }  
  74.       
  75.     public void refreshNotify(){  
  76.         if (!CommonUtil.checkNetworkState(mContext)){  
  77.             return ;  
  78.         }  
  79.           
  80.         if (!mStartSuccess){  
  81.             stopEngine();  
  82.             try {  
  83.                 Thread.sleep(200);  
  84.             } catch (InterruptedException e) {  
  85.                 e.printStackTrace();  
  86.             }  
  87.             boolean ret = startEngine();  
  88.             if (ret){  
  89.                 mStartSuccess = true;  
  90.             }  
  91.         }  
  92.   
  93.     }  
  94.       
  95.     @Override  
  96.     public boolean startEngine() {  
  97.         if (mFriendName.length() == 0){  
  98.             return false;  
  99.         }  
  100.   
  101.         int ret = PlatinumJniProxy.startMediaRender(mFriendName, mUUID);  
  102.         boolean result = (ret == 0 ? true : false);  
  103.         mApplication.setDevStatus(result);  
  104.         return result;  
  105.     }  
  106.   
  107.     @Override  
  108.     public boolean stopEngine() {  
  109.         PlatinumJniProxy.stopMediaRender();  
  110.         mApplication.setDevStatus(false);  
  111.         return true;  
  112.     }  
  113.   
  114.     @Override  
  115.     public boolean restartEngine() {  
  116.         setFlag(false);  
  117.         awakeThread();  
  118.         return true;  
  119.     }  
  120.   
  121. }  


通過startMediaRender開啓設備後就可以被外界所發現,控制點發送控制信息後

動作action的回調通過反射類PlatinumReflection的靜態方法onActionReflection執行

GENA事件則通過PlatinumJniProxy類的responseGenaEvent傳遞

詳情大家down code去了解吧

Github下載頁:https://github.com/geniusgithub/MediaRender

DLNA開發文檔鏈接:http://download.csdn.net/detail/geniuseoe2012/4969961


關於SO庫的具體C++實現可參考這篇博文:

http://blog.csdn.net/lancees/article/details/9178385


DMS的實現請參考這篇博文基於Platinum庫的DMS實現(android)


more brilliantPlease pay attention to my CSDN blog -->http://blog.csdn.net/geniuseoe2012 

發佈了0 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章