libstreaming icon indicating copy to clipboard operation
libstreaming copied to clipboard

Fail to connect to camera service

Open Zlate87 opened this issue 9 years ago • 9 comments

I have the following application:

MainActivity:

import net.majorkernelpanic.streaming.Session;
import net.majorkernelpanic.streaming.SessionBuilder;
import net.majorkernelpanic.streaming.gl.SurfaceView;
import net.majorkernelpanic.streaming.rtsp.RtspServer;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.WindowManager;


/**
 * A straightforward example of how to use the RTSP server included in libstreaming.
 */
public class MainActivity extends Activity implements Session.Callback {

  private final static String TAG = "MainActivity";

  private SurfaceView mSurfaceView;

  private Session mSession;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    mSurfaceView = (SurfaceView) findViewById(R.id.surface);

    // Sets the port of the RTSP server to 8086
    Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
    editor.putString(RtspServer.KEY_PORT, String.valueOf(8087));
    editor.commit();

    // Configures the SessionBuilder
    mSession = SessionBuilder.getInstance()
            .setSurfaceView(mSurfaceView)
            .setPreviewOrientation(90)
            .setContext(getApplicationContext())
            .setCallback(this)
            .setAudioEncoder(SessionBuilder.AUDIO_NONE)
            .setVideoEncoder(SessionBuilder.VIDEO_H264).build();
    mSession.configure();

    // Starts the RTSP server
    this.startService(new Intent(this,RtspServer.class));

  }

  @Override
  public void onSessionConfigured() {
    Log.d(TAG, "Preview configured.");
    mSession.start();
  }

  @Override
  public void onBitrateUpdate(long bitrate) {
    Log.d(TAG,"Bitrate: "+bitrate);
  }

  @Override
  public void onSessionError(int reason, int streamType, Exception e) {
    Log.d(TAG,"onSessionError");

  }

  @Override
  public void onPreviewStarted() {
    Log.d(TAG, "onPreviewStarted");

  }

  @Override
  public void onSessionStarted() {
    Log.d(TAG, "onSessionStarted");

  }

  @Override
  public void onSessionStopped() {
    Log.d(TAG,"onSessionStopped");
  }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.streamingprototype"
          xmlns:android="http://schemas.android.com/apk/res/android">

  <!-- Needed for the streaming -->
  <uses-feature android:name="android.hardware.camera"/>
  <uses-feature android:name="android.hardware.camera.autofocus"/>

  <!-- Needed for the streaming -->
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>
  <uses-permission android:name="android.permission.CAMERA"/>
  <uses-permission android:name="android.permission.WAKE_LOCK"/>

  <!-- Needed for the kiosk mode-->
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>

    <service android:name="net.majorkernelpanic.streaming.rtsp.RtspServer" />

  </application>

When I test the app on Nexus 5 with Android M preview it works as expected. If I test it on Nexus 6 with Android 5 the moment I connect to the stream I get the following error:

I/RtspServer﹕ Connection from 10.7.8.19
E/RtspServer﹕ OPTIONS rtsp://10.7.8.236:8088
D/RtspServer﹕ RTSP/1.0 200 OK
    Server: MajorKernelPanic RTSP Server
    Cseq: 2
    Content-Length: 0
    Public: DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE
06-24 11:07:49.115  E/RtspServer﹕ DESCRIBE rtsp://10.7.8.236:8088
 W/CameraBase﹕ An error occurred while connecting to camera: 0
 D/class com.streaming.StreamingActivity﹕ onSessionError
E/RtspServer﹕ Fail to connect to camera service
W/System.err﹕ net.majorkernelpanic.streaming.exceptions.CameraInUseException: Fail to connect to camera service
W/System.err﹕ at net.majorkernelpanic.streaming.video.VideoStream.openCamera(VideoStream.java:565)
W/System.err﹕ at net.majorkernelpanic.streaming.video.VideoStream.createCamera(VideoStream.java:575)
W/System.err﹕ at net.majorkernelpanic.streaming.video.H264Stream.testMediaCodecAPI(H264Stream.java:125)
W/System.err﹕ at net.majorkernelpanic.streaming.video.H264Stream.testH264(H264Stream.java:119)
W/System.err﹕ at net.majorkernelpanic.streaming.video.H264Stream.configure(H264Stream.java:111)
W/System.err﹕ at net.majorkernelpanic.streaming.Session.syncConfigure(Session.java:395)
W/System.err﹕ at net.majorkernelpanic.streaming.rtsp.RtspServer$WorkerThread.processRequest(RtspServer.java:439)
W/System.err﹕ at net.majorkernelpanic.streaming.rtsp.RtspServer$WorkerThread.run(RtspServer.java:390)
D/RtspServer﹕ RTSP/1.0 500 Internal Server Error
    Server: MajorKernelPanic RTSP Server
    Cseq: 3
    Content-Length: 0
D/class com.streaming.StreamingActivity﹕ onSessionStopped
06-24 11:07:49.162  I/RtspServer﹕ Client disconnected
I/RtspServer﹕ Connection from 10.7.8.19
E/RtspServer﹕ OPTIONS rtsp://10.7.8.236:8088
D/RtspServer﹕ RTSP/1.0 200 OK
    Server: MajorKernelPanic RTSP Server
    Cseq: 1
    Content-Length: 0
    Public: DESCRIBE,SETUP,TEARDOWN,PLAY,PAUSE
I/RtspServer﹕ Client disconnected

Zlate87 avatar Jun 24 '15 09:06 Zlate87

I saw the exact same issue in my app. You use MediaRecorder api or MediaCodec api?

12flying avatar Jun 25 '15 18:06 12flying

I just followed the example 1 from https://github.com/fyhertz/libstreaming-examples. So I use what comes out of the box (I have no idea what I use :)). For more details please see the MainActivity class above.

Zlate87 avatar Jun 26 '15 06:06 Zlate87

The key warning is

CameraInUseException: Fail to connect to camera service

This often happens when you forget to "release" the camera... Some times an app that uses the camera, probably your own app, doesn't "disconnect" the camera when you close the app or change activity... So the camera can't be used by another activity or action.

for a quick fix you can reboot your device but in your app be sure to add this to the Activity that uses it

add this to your activity

// important to garbage collect, release the camera and the stream
    @Override
    public void onDestroy(){
        super.onDestroy();
        mClient.release();
        mSession.release();
    }

lyricalpolymath avatar Aug 30 '15 22:08 lyricalpolymath

I got the same error in Pre-Marshmallow devices. I got the preview to display correctly. but when I try to start streaming, is when I get the error

03-03 16:13:45.548 W/System.err: net.majorkernelpanic.streaming.exceptions.CameraInUseException: Fail to connect to camera service 03-03 16:13:45.548 W/System.err: at net.majorkernelpanic.streaming.video.VideoStream.openCamera(VideoStream.java:568) 03-03 16:13:45.548 W/System.err: at net.majorkernelpanic.streaming.video.VideoStream.createCamera(VideoStream.java:578) 03-03 16:13:45.548 W/System.err: at net.majorkernelpanic.streaming.video.H264Stream.testMediaCodecAPI(H264Stream.java:125) 03-03 16:13:45.548 W/System.err: at net.majorkernelpanic.streaming.video.H264Stream.testH264(H264Stream.java:119) 03-03 16:13:45.548 W/System.err: at net.majorkernelpanic.streaming.video.H264Stream.configure(H264Stream.java:111) 03-03 16:13:45.548 W/System.err: at net.majorkernelpanic.streaming.Session.syncConfigure(Session.java:395) 03-03 16:13:45.548 W/System.err: at net.majorkernelpanic.streaming.Session$2.run(Session.java:371) 03-03 16:13:45.548 W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 03-03 16:13:45.548 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 03-03 16:13:45.548 W/System.err: at android.os.Looper.loop(Looper.java:145) 03-03 16:13:45.548 W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)

serpro avatar Mar 03 '16 16:03 serpro

There is a workaround. You can a delay between destroyCamera() and createCamera() in VideoStream.java. It works for me. Example:

protected void encodeWithMediaRecorder() throws IOException, ConfNotSupportedException {
    ...

    // We need a local socket to forward data output by the camera to the packetizer
    createSockets();

    // Reopens the camera if needed
    destroyCamera();
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    createCamera();
    ...
}

genehuangtaiwan avatar Aug 29 '16 06:08 genehuangtaiwan

The same issue on pre-Marshmallow devices

Fail to connect to camera service

in this place mCamera = Camera.open(mCameraId);

jamesvanhallen avatar Sep 14 '16 09:09 jamesvanhallen

Has anyone managed to solve this problem on pre-Marshmallow devices?

helloimcarmine avatar Dec 03 '18 16:12 helloimcarmine

i had the same problem using the example 1 from https://github.com/fyhertz/libstreaming-examples. I my case the Problem was simple. I forgot to enable the camera permission for the app on the phone. It should be self explanatory but its not mentioned in the Readme...

Solution for example on the Android Studio Emulator: Settings --> Apps --> advanced --> Permission Manager --> Camera --> "here you have to add your app"

oscarisolin avatar Mar 24 '21 13:03 oscarisolin

Hi All, I am using the example 1, I am also facing the same issue, I tried all the workaround mentioned in previous comments. Still facing the issue, CameraInUseException. Did anybody able to fix the issue?

Thanks

vikashkumar avatar Nov 20 '21 19:11 vikashkumar