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

Bug related to the preview. #1689

Open
Samokovsky opened this issue Dec 23, 2024 · 0 comments
Open

Bug related to the preview. #1689

Samokovsky opened this issue Dec 23, 2024 · 0 comments

Comments

@Samokovsky
Copy link

Samokovsky commented Dec 23, 2024

Hi!

Could you please help me with an issue? I'm encountering an intermittent bug related to the preview.

About 3 out of 10 times, the preview shows a "black screen." I've attached the logs and code for reference.

Android 11
Hardware acceleration is enabled by default.


class RTSPFragment : Fragment(), ConnectChecker, ClientListener,
    SurfaceHolder.Callback {

    private lateinit var binding: FragmentRTSPBinding
    private lateinit var surfaceView: OpenGlView
    private lateinit var rtspServerCamera: RtspServerCamera2
    private var clickCount = 0

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentRTSPBinding.inflate(inflater, container, false)
        return binding.root
    }

    @SuppressLint("SimpleDateFormat")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        surfaceView = binding.surfaceViewCam2
        surfaceView.holder.addCallback(this)
        rtspServerCamera = RtspServerCamera2(surfaceView, this, 8554)
        rtspServerCamera.streamClient.setOnlyVideo(true)
        rtspServerCamera.streamClient.setClientListener(this)
        rtspServerCamera.streamClient.setAuthorization("admin", "admin")
        rtspServerCamera.setVideoCodec(VideoCodec.H264)
        surfaceView.setIsStreamHorizontalFlip(true)
        lifecycleScope.launch {
            try {
                while (true) {
                    binding.overlayTimeRtsp.text = SimpleDateFormat("HH:mm:ss").format(Date())
                    delay(500)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        binding.logoCameraActivity.setOnClickListener{
            clickCount++
            if (clickCount >= 20) {
                findNavController().navigate(R.id.action_rtsp_to_login)
                clickCount = 0
            }
        }
    }

    private fun prepare(): Boolean {
        Log.d("CameraActivity", "prepare: front ${rtspServerCamera.resolutionsFront} back ${rtspServerCamera.resolutionsBack}")
        val prepared = rtspServerCamera.prepareVideo(1280, 720, 24, 2000 * 1024, 90)
        return prepared
    }

    override fun onAuthError() {
        Log.i("@@@@", "Auth error")
    }

    override fun onAuthSuccess() {
        Log.i("@@@@", "Auth success")
    }

    override fun onConnectionFailed(reason: String) {
        Log.i("@@@@", "Connection failed")
    }

    override fun onConnectionStarted(url: String) {
        Log.i("@@@@", "Connection started")
    }

    override fun onConnectionSuccess() {
        Log.i("@@@@", "Connection success")
    }

    override fun onDisconnect() {
        Log.i("@@@@", "Client disconnect")
    }

    override fun onClientConnected(client: ServerClient) {
        Log.i("@@@@", "Client connected")
    }

    override fun onClientDisconnected(client: ServerClient) {
        Log.i("@@@@", "Client disconnected")
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        if(prepare())
            rtspServerCamera.startStream()
        rtspServerCamera.switchCamera("0")
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        if (!holder.surface.isValid)
            Log.e("@@@@", "Surface is invalid!")
        if (!rtspServerCamera.isOnPreview)
            rtspServerCamera.startPreview()
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        Log.i("@@@@", "Surface destroyed")
        if (rtspServerCamera.isStreaming) {
            rtspServerCamera.stopStream()
        }
        if (rtspServerCamera.isOnPreview)
            rtspServerCamera.stopPreview()
    }
}

18_12_24.txt

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

1 participant