DrmService.java 4.79 KB
package com.micro.player.service;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.xgimi.gimicinema.cctvsix.activity.M1905VideoListActivity;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @ClassName: DrmService
 * @Version: V1.0
 */
public class DrmService {
    private String path = "/sdcard/log.txt";
    private int port = 9000;
    private String drm_service = "http://127.0.0.1";
    private RequestQueue mQueueSts;

    public DrmService(Context context) {
        mQueueSts = Volley.newRequestQueue(context);
    }


    public void tryToStartService() {
        StringRequest request = new StringRequest(Request.Method.GET,
                "http://127.0.0.1:" + port + "/1.ts?alive=1",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String arg0) {
                        Log.d("lovely", "startserver:" + port + "1.ts?alive=1 OK");
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Log.d("lovely", "startserver:" + port + "1.ts?alive=1 NOT OK");
                initDrmdecoder();
            }
        }) {
        };
        mQueueSts.add(request);
    }

    public boolean initDrmdecoder() {
        boolean rtn = false;
        try {
            port = startDecoder(path.getBytes());
            Log.d("lovely", "DrmService  Is try to start port:" + port + "127.0.0.1:" + port);
            // port <=0 时,视作开启失败
            if (port <= 0) {
                Log.d("lovely", "DrmService  Start port failure:" + port + "");
            } else {
                Log.d("lovely", "DrmService running checked port:" + port + "127.0.0.1:" + port);
                rtn = true;
            }

        } catch (Exception e) {
            Log.d("lovely", "DrmService initDrmdecoder erro  " + e.toString());
        }
        return rtn;
    }

    public boolean startService() {
        boolean rtn = false;
        int errorcount = 0;
        try {
            while (!IsStart()) {
                port = startDecoder(path.getBytes());
//				AppInfo.setErrorinfos("DrmService  Is try to start port:" + port ,"127.0.0.1:" + port);
                // port <=0 时,视作开启失败
                if (port <= 0) {
                    Log.d("lovely", "DrmService  Start port failure:" + port + "");
                }
                Thread.sleep(200);
                //如果开始
                errorcount++;
                if (errorcount > 5) {
                    Log.d("lovely", "DrmService  Start port over 5 times:" + errorcount + "");
                    break;
                }
            }

            if (IsStart()) {
                Log.d("TTTTTTTTTTTTT", "Is Start");
                rtn = true;
            } else {
                Log.d("TTTTTTTTTTTTT", "Is NOT Start");
            }

        } catch (Exception e) {
            Log.d("lovely", "DrmService initDrmdecoder erro  " + e.toString());
        }
        return rtn;
    }

    public Uri getUrl(String url) {
        Uri rtn_uri = Uri.EMPTY;
        try {
            rtn_uri = Uri.parse("http://127.0.0.1:"
                    + Integer.toString(port) + "/zqc.ts?ip=" + M1905VideoListActivity.SERVER_IP + "&clientid=" + 18 + "&source=" + url);
            Log.d("lovely", "DrmService last getUrl:" + rtn_uri.toString() + "");
        } catch (Exception e) {
            Log.d("lovely", "DrmService getUrl error port :" + port + "");
        }

        if (IsStart()) {
            Log.d("lovely", "DrmService getUrl running checked port:" + port + "127.0.0.1:" + port);
        } else {
            Log.d("lovely", "DrmService getUrl not running checked port:" + port + "127.0.0.1:" + port);
        }

        return rtn_uri;
    }

    public boolean IsStart() {
        boolean isstart = false;
        try {
            Process process = Runtime.getRuntime().exec("netstat");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()), 1024);
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains("127.0.0.1:" + port))
                    isstart = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("lovely", "isStart:" + isstart);
        return isstart;

    }

    public static native int startDecoder(byte[] pathbytes);

    static {
        System.loadLibrary("decode");
    }
}