Release wowza on service

hi I have a question

https://stackoverflow.com/questions/46700031/destroy-all-instance-from-a-service-release-camera-using-wowza

Hello @John Mart Belamide ,

Did you perform live stream using GoCoder SDK from an android service? If yes, could you share your piece of code to do it.

Thank you very much,

Tu

you can not directly make a service with out preview but you can display the wowzacameraview by putting the width and height as 1dp and get that findviewbyid refence and intialize in gocoder inside of service and start streaming just replace Asynctask with service


<com.wowza.gocoder.sdk.api.devices.WOWZCameraViewandroid:id="@+id/wow_camera_preview"android:layout_width="50dp"android:layout_height="match_parent"wowza:scaleMode="fill"wowza:defaultCamera="back"wowza:frameSizePreset="frameSize1280x720"/>

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;

import com.wowza.gocoder.sdk.api.WowzaGoCoder;
import com.wowza.gocoder.sdk.api.broadcast.WOWZBroadcast;
import com.wowza.gocoder.sdk.api.broadcast.WOWZBroadcastConfig;
import com.wowza.gocoder.sdk.api.configuration.WOWZMediaConfig;
import com.wowza.gocoder.sdk.api.data.WOWZDataMap;
import com.wowza.gocoder.sdk.api.data.WOWZDataScope;
import com.wowza.gocoder.sdk.api.devices.WOWZAudioDevice;
import com.wowza.gocoder.sdk.api.devices.WOWZCamera;
import com.wowza.gocoder.sdk.api.devices.WOWZCameraView;
import com.wowza.gocoder.sdk.api.errors.WOWZError;
import com.wowza.gocoder.sdk.api.status.WOWZState;
import com.wowza.gocoder.sdk.api.status.WOWZStatus;
import com.wowza.gocoder.sdk.api.status.WOWZStatusCallback;

import java.util.Date;

import static android.content.Context.SENSOR_SERVICE;

public class CameraStreamingService extends AsyncTask<Void,Void,Void> implements WOWZStatusCallback{

    private String m_tag ="CameraStreamingService";

    private Context mContext;

    MainActivity mainActivity;

    public WOWZCameraView goCoderCameraView;

    private WowzaGoCoder goCoder;

    // The GoCoder SDK audio devicepublic WOWZAudioDevice goCoderAudioDevice;

    // The GoCoder SDK broadcasterpublic WOWZBroadcast goCoderBroadcaster;

    // The broadcast configuration settingspublic WOWZBroadcastConfig goCoderBroadcastConfig;

     //  private SensorManager sensorManager;private Sensor proximitySensor;
    private SensorEventListener proximitySensorListener;
    private SensorManager sensorManager;

   private ProximitySensorModule proximitySensorModule;

    public CameraStreamingService(Context context,MainActivity mainActivity, WOWZCameraView wowzCameraView){

        mContext =context;
        this.mainActivity =mainActivity;
       this.goCoderCameraView =wowzCameraView;

       intializeGoCoderSdk();

        Log.d(m_tag,"camerastreamingservice constructor called");

    }

    @Overrideprotected Void doInBackground(Void... voids) {
  //      intializeGoCoderSdk();return null;
    }

    @Override
    public void onWZStatus(WOWZStatus wowzStatus) {

        // A successful status transition has been reported by the GoCoder SDKfinal StringBuffer statusMessage = new StringBuffer("Broadcast status: ");

        switch (wowzStatus.getState()) {
            case WOWZState.STARTING:
                statusMessage.append("Broadcast initialization");
                break;

            case WOWZState.READY:
                statusMessage.append("Ready to begin streaming");
                break;

            case WOWZState.RUNNING:
                statusMessage.append("Streaming is active");
                break;

            case WOWZState.STOPPING:
                statusMessage.append("Broadcast shutting down");
                break;

            case WOWZState.IDLE:
                statusMessage.append("The broadcast is stopped");
                break;

            default:
                return;

        }
        Log.d(m_tag, "wowza-" + statusMessage.toString());

    }

    @Override
    public void onWZError(WOWZStatus wowzStatus) {

        Log.d(m_tag, "wowza-" + wowzStatus.getLastError().getErrorDescription());

    }

   public boolean startStreaming()
   {
       // Start streamingif(goCoderCameraView!=null)
       {
           goCoderCameraView.startPreview();
           goCoderBroadcaster.startBroadcast(goCoderBroadcastConfig, this);
           Log.d(m_tag,"broadcast is started");

           proximitySensorModule = new ProximitySensorModule(mContext,this);

       }else {
           Log.d(m_tag,"gocoder camera preview is null canot started stream");

       }

        return goCoderBroadcaster.getStatus().isRunning();
   }

     public void stopStreaming()
     {
            if (goCoderBroadcaster.getStatus().isRunning())
            {
                // Stop the broadcast that is currently runninggoCoderBroadcaster.endBroadcast(this);
                Log.d(m_tag,"broadcast is ended");

            }
     }

        public void sendMotionSensorData(String eventname,String distance,String SENSOR_TYPE, String direction)
        {
            if(goCoderBroadcaster.getStatus().isRunning())
            {
                WOWZDataMap dataEventParams = new WOWZDataMap();
                dataEventParams.put("StreamName", goCoderBroadcastConfig.getStreamName());
                dataEventParams.put("Timestamp", new Date(System.currentTimeMillis()));
                dataEventParams.put("SensorStatus",distance+"cm");
                dataEventParams.put("SENSOR_TYPE",SENSOR_TYPE);
                dataEventParams.put("Directin",direction);

                goCoderBroadcaster.sendDataEvent(WOWZDataScope.STREAM,eventname,dataEventParams);
                goCoderBroadcaster.sendDataEvent(WOWZDataScope.MODULE,eventname,dataEventParams);

            }
        }

    public void intializeGoCoderSdk() {

        // Initialize the GoCoder SDKgoCoder = WowzaGoCoder.init(mContext, Misc.SDK_SAMPLE_APP_LICENSE_KEY);
        Log.d(m_tag, "gocoder is intialized-:" + goCoder);

        if (goCoder == null) {
            // If initialization failed, retrieve the last error and display itWOWZError goCoderInitError = WowzaGoCoder.getLastError();

            Log.d(m_tag,"GoCoder SDK error: " + goCoderInitError.getErrorDescription());
            return;

        } else {

            // Enable the switch camera button if more than one camera is availableint numCameras = WOWZCamera.getNumberOfDeviceCameras();

            // Set the active camera to the front camera if it's not activeif(numCameras>1)
            if (!goCoderCameraView.getCamera().isFront())
                goCoderCameraView.switchCamera();

            //            goCoderCameraView = goCoderCameraView;            //     goCoderCameraView.setCamera(WOWZCamera.DIRECTION_FRONT);            //    goCoderCameraView.setVideoEnabled(false);            // Create an audio device instance for capturing and broadcasting audiogoCoderAudioDevice = new WOWZAudioDevice();

            // Create a broadcaster instancegoCoderBroadcaster = new WOWZBroadcast();

            // Create a configuration instance for the broadcastergoCoderBroadcastConfig = new WOWZBroadcastConfig(WOWZMediaConfig.FRAME_SIZE_1280x720);

            // goCoderBroadcastConfig.setVideoRotation(90);            // Set the connection properties for the target Wowza Streaming Engine server or Wowza Streaming Cloud live streamgoCoderBroadcastConfig.setHostAddress(Misc.m_kHostAddress);
            //goCoderBroadcastConfig.setHostAddress("172.16.33.76");goCoderBroadcastConfig.setPortNumber(Misc.m_kPortNumber);
            goCoderBroadcastConfig.setApplicationName(Misc.m_kApplicationName);
            goCoderBroadcastConfig.setStreamName(Misc.m_kStreamName);
            goCoderBroadcastConfig.setABREnabled(true);

            // Designate the camera preview as the video broadcastergoCoderBroadcastConfig.setVideoBroadcaster(goCoderCameraView);
            goCoderBroadcastConfig.setAudioBroadcaster(goCoderAudioDevice);

            goCoderBroadcastConfig.setAudioEnabled(false);

        }

    }

}