Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to get the camera info. #144

Open
kishorpise opened this issue Dec 27, 2024 · 4 comments
Open

Failed to get the camera info. #144

kishorpise opened this issue Dec 27, 2024 · 4 comments

Comments

@kishorpise
Copy link

Hello,
I am using latest libraries

library = { module = "com.github.pedroSG94.RootEncoder:library", version.ref = "library" } rtsp-server = { module = "com.github.pedroSG94:RTSP-Server", version.ref = "rtspServer" }

and converted kt. sample to java. using only 3 classes to preview and stream sample. I encountered following error.

image

Can some one help me ? below is entire class.

`package com.camtest;

import static android.os.Looper.prepare;

import android.graphics.SurfaceTexture;
import android.os.Build;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.pedro.common.ConnectChecker;
import com.pedro.encoder.input.video.CameraHelper;
import com.pedro.encoder.input.video.CameraOpenException;
import com.pedro.library.base.recording.RecordController;
import com.pedro.library.view.AutoFitTextureView;
import com.pedro.rtsp.rtsp.RtspClient;
import com.pedro.rtsp.utils.* ;
import com.pedro.rtspserver.RtspServerCamera1;
import com.pedro.rtspserver.server.ClientListener;
import com.pedro.rtspserver.server.ServerClient;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class pedroact extends AppCompatActivity implements ConnectChecker, ClientListener, TextureView.SurfaceTextureListener {

private RtspServerCamera1 rtspServerCamera1;
private ImageView bStream;
private ImageView bRecord;
private ImageView bSwitchCamera;
private AutoFitTextureView surfaceView;
private TextView tvUrl;
private String recordPath = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    EdgeToEdge.enable(this);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_pedro);


    tvUrl = findViewById(R.id.tv_url);
    bStream = findViewById(R.id.b_start_stop);
    bRecord = findViewById(R.id.b_record);
    bSwitchCamera = findViewById(R.id.switch_camera);
    surfaceView = findViewById(R.id.surfaceView);

    try {

        rtspServerCamera1 = new RtspServerCamera1(surfaceView, this, 1935);

    } catch (RuntimeException e) {
        Log.d("Camera", "onCreate: " + e.getMessage());
    }


    rtspServerCamera1.getStreamClient().setClientListener(this);
    surfaceView.setSurfaceTextureListener(this);


    bStream.setOnClickListener(v -> {
        if (rtspServerCamera1.isStreaming()) {
            bStream.setImageResource(R.drawable.stream_icon);
            rtspServerCamera1.stopStream();
            if (!rtspServerCamera1.isRecording()) {
                ScreenOrientation.unlockScreen(pedroact.this);
            }
        } else if (rtspServerCamera1.isRecording()  ) {

            rtspServerCamera1.startStream();
            tvUrl.setText(rtspServerCamera1.getStreamClient().getEndPointConnection());
            ScreenOrientation.lockScreen(pedroact.this);
        } else {
            toast("Error preparing stream, This device cant do it");
        }
    });

    bRecord.setOnClickListener(v -> {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            if (rtspServerCamera1.isRecording()) {
                rtspServerCamera1.stopRecord();
                bRecord.setImageResource(R.drawable.record_icon);
                PathUtils.updateGallery(pedroact.this, recordPath);
                if (!rtspServerCamera1.isStreaming()) {
                    ScreenOrientation.unlockScreen(pedroact.this);
                }
            } else if (rtspServerCamera1.isStreaming()  ) {
                File folder = PathUtils.getRecordPath();
                if (!folder.exists()) folder.mkdir();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
                recordPath = folder.getAbsolutePath() + "/" + sdf.format(new Date()) + ".mp4";
                bRecord.setImageResource(R.drawable.pause_icon);
                try {
                    rtspServerCamera1.startRecord(recordPath, status -> {
                        if (status == RecordController.Status.RECORDING) {
                            bRecord.setImageResource(R.drawable.stop_icon);
                        }
                    });
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                ScreenOrientation.lockScreen(pedroact.this);
            } else {
                toast("Error preparing stream, This device cant do it");
            }
        } else {
            toast("You need min JELLY_BEAN_MR2(API 18) for do it...");
        }
    });

    bSwitchCamera.setOnClickListener(v -> {
        try {
            rtspServerCamera1.switchCamera();
        } catch (CameraOpenException e) {
            Toast.makeText(pedroact.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

}

@Override
public void onConnectionSuccess() {
    toast("Connected");
}

@Override
public void onConnectionFailed(String reason) {
    toast("Failed: " + reason);
    rtspServerCamera1.stopStream();
    if (!rtspServerCamera1.isRecording()) {
        ScreenOrientation.unlockScreen(pedroact.this);
    }
    bStream.setImageResource(R.drawable.stream_icon);
}

@Override
public void onConnectionStarted(String url) {
}

@Override
public void onDisconnect() {
    toast("Disconnected");
}

@Override
public void onAuthError() {
    toast("Auth error");
    rtspServerCamera1.stopStream();
    if (!rtspServerCamera1.isRecording()) {
        ScreenOrientation.unlockScreen(pedroact.this);
    }
    bStream.setImageResource(R.drawable.stream_icon);
}

@Override
public void onAuthSuccess() {
    toast("Auth success");
}

@Override
public void onClientConnected(ServerClient client) {
    toast("Client connected: " + client.getAddress());
}

@Override
public void onClientDisconnected(ServerClient client) {
    toast("Client disconnected: " + client.getAddress());
}

@Override
public void onClientNewBitrate(long bitrate, ServerClient client) {
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    if (!rtspServerCamera1.isOnPreview()) {
        rtspServerCamera1.startPreview();
        adaptPreview();
    }
}

private void adaptPreview() {
    boolean isPortrait = CameraHelper.isPortrait(pedroact.this);
    int w = isPortrait ? rtspServerCamera1.getStreamHeight() : rtspServerCamera1.getStreamWidth();
    int h = isPortrait ? rtspServerCamera1.getStreamWidth() : rtspServerCamera1.getStreamHeight();
    surfaceView.setAspectRatio(w, h);
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && rtspServerCamera1.isRecording()) {
        rtspServerCamera1.stopRecord();
        bRecord.setBackgroundResource(R.drawable.record_icon);
        PathUtils.updateGallery(pedroact.this, recordPath);
    }
    if (rtspServerCamera1.isStreaming()) {
        rtspServerCamera1.stopStream();
        bStream.setImageResource(R.drawable.stream_icon);
    }
    if (rtspServerCamera1.isOnPreview()) {
        rtspServerCamera1.stopPreview();
    }
    ScreenOrientation.unlockScreen(pedroact.this);
    return true;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}

private void toast(String message) {
    Toast.makeText(pedroact.this, message, Toast.LENGTH_SHORT).show();
}

}`

@pedroSG94
Copy link
Owner

Hello,

Make sure that you have camera permission accepted. You should declare it in the manifest and request it in runtime before use the library.

@kishorpise
Copy link
Author

kishorpise commented Jan 3, 2025

This is already given... copied from your example.

`

<uses-permission android:name="android.permission.RECORD_AUDIO" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
<uses-feature android:glEsVersion="0x00030001" />
<uses-feature android:name="android.hardware.camera2.full" />
<uses-feature android:name="android.hardware.usb.accessory" />
<uses-feature android:name="android.hardware.camera.any" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
    android:name="android.permission.WRITE_SETTINGS"
    tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />

<application
    android:allowBackup="true"
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:hardwareAccelerated="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Camtest"
    tools:targetApi="31">
    <activity
        android:name=".Claude"
        android:exported="true" >

    </activity>
    <activity
        android:name=".pedroact"
        android:exported="true"/>`............................ and so on.

@pedroSG94
Copy link
Owner

Hello,

Remember that Android need request camera permission in the code not only declare it in the manifest. You can also go to app settings in the device and accept it manually to test.
Can you tell me if my example if working for you? If it is not working...

Did you test with other device? can you tell me your device model?

@kishorpise
Copy link
Author

Your example also crashing... camera permission are given for sure.. I am able to see preview and save the recorded video..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants