ronaldsampaio / USBStreaming

0 stars 2 forks source link

i don't know use this project #2

Open yubinjin opened 4 months ago

yubinjin commented 4 months ago

Hello! I also need a UVC camera, so I came across your issue inquiry on rootencoder! However, I don't understand how to use this project!! In your inquiry, you mentioned transmitting video to an RTSP server using mtx, but I'm not sure how to do that! If it's not too much trouble, I would like to ask for your help, would that be okay?

yubinjin commented 4 months ago

class StreamingController(private val context: Context) : ClientListener, ConnectChecker { private lateinit var rtspServer : RtspServer private var portNum = 18554 private var prepared = false

fun setUpServer(cameraClient: CameraClient, aspectRatioTextureView: AspectRatioTextureView) {
    rtspServer = RtspServer(this, portNum)
    _aspectRatioTextureView = aspectRatioTextureView
    rtspStream = RtspStream(
        context,
        this,
        USBCameraSource(context,cameraClient,aspectRatioTextureView),
        MicrophoneSource()
    )
    //rtspStream.changeVideoSource(usbCameraSource)

}

fun startStream(){
    if(rtspStream.prepareVideo(1280, 720, 3500) && rtspStream.prepareAudio(48000,false,320)){
        rtspStream.startStream("rtsp://192.168.1.107:8554/mystream")
    }
    //rtspStream.startStream("rtsp://rtspstream:0f0c888313b4e035a937b04a52b505e8@zephyr.rtsp.stream/movie")
    else{
        Log.d("camera_str","ERROR PREPARING VIDEO OR AUDIO")
    }

    Log.d("Streaming", "STARTED SERVER!")
    //else Log.e("Streaming", "Error preparing audio or video")

}

private fun decodeSpsPpsFromBuffer(
    outputBuffer: ByteBuffer,
    length: Int
): Pair<ByteBuffer, ByteBuffer>? {
    val csd = ByteArray(length)
    outputBuffer[csd, 0, length]
    var i = 0
    var spsIndex = -1
    var ppsIndex = -1
    while (i < length - 4) {
        if (csd[i].toInt() == 0 && csd[i + 1].toInt() == 0 && csd[i + 2].toInt() == 0 && csd[i + 3].toInt() == 1) {
            if (spsIndex == -1) {
                spsIndex = i
            } else {
                ppsIndex = i
                break
            }
        }
        i++
    }
    if (spsIndex != -1 && ppsIndex != -1) {
        val sps = ByteArray(ppsIndex)
        System.arraycopy(csd, spsIndex, sps, 0, ppsIndex)
        val pps = ByteArray(length - ppsIndex)
        System.arraycopy(csd, ppsIndex, pps, 0, length - ppsIndex)
        return Pair(ByteBuffer.wrap(sps), ByteBuffer.wrap(pps))
    }
    return null
}

but i don;t know how can i do please can you help me?

yubinjin commented 4 months ago

this code happened error

yubinjin commented 4 months ago

// fun onVideoFrame(frame: ByteArray, size: Int, isIdr: Boolean, timestamp: Long) { // val h264Buffer = ByteBuffer.wrap(frame) // val info = MediaCodec.BufferInfo() // info.size = size // info.offset = 0 // var presentTimeUs = System.nanoTime() / 1000 // info.presentationTimeUs = System.nanoTime() / 1000 - presentTimeUs // info.presentationTimeUs = timestamp // if(isIdr){ // info.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME // if(!rtspServer.isRunning()){ // Log.d("camera_streaming","SERVER NOT RUNNING! SETTING VIDEO INFO") // //PODE DAR MERDA POIS PODE RETORNAR NULL POR ALGUM MOTIVO // val (newSps, newPps) = decodeSpsPpsFromBuffer(h264Buffer, size)!! // rtspServer.setVideoInfo(newSps,newPps,null) // } // } // if(rtspServer.isRunning()){ // Log.d("Streaming","SERVER RUNNING!") // rtspServer.sendVideo(h264Buffer,info) // } // // if (isIdr) { // info.flags = MediaCodec.BUFFER_FLAG_KEY_FRAME // if (!rtspClient.isStreaming) { // presentTimeUs = System.nanoTime() / 1000 // val (newSps, newPps) = decodeSpsPpsFromBuffer(h264Buffer, size) // rtspClient.setVideoInfo(newSps, newPps, null) // rtspClient.connect("rtsp://<rtsp-server-IP:8554/mystream") // } // } // if (rtspClient.isStreaming) { // rtspClient.sendVideo(h264Buffer, info) // } // }

Can I know how to use this part that is commented out? I need this commented-out part, which involves entering an RTSP endpoint and streaming to that endpoint URL. This part is very crucial for me.

yubinjin commented 4 months ago

class StreamingController(private val context: Context) : ClientListener, ConnectChecker { private lateinit var rtspStream: RtspStream private var portNum = 18554 private var prepared = false

fun setUpServer(cameraClient: CameraClient, aspectRatioTextureView: AspectRatioTextureView) {
    rtspStream = RtspStream(
        context,
        this,
        USBCameraSource(context,cameraClient,aspectRatioTextureView),
        MicrophoneSource()
    )
    //rtspStream.changeVideoSource(usbCameraSource)
}

fun startStream(){
    if(rtspStream.prepareVideo(1280, 720, 3500) && rtspStream.prepareAudio(48000,false,320)){
        rtspStream.startStream("rtsp://192.168.0.79:8554/test")
    }
    else{
        Log.d("camera_str","ERROR PREPARING VIDEO OR AUDIO")
    }

    Log.d("Streaming", "STARTED SERVER!")

}

i changed the code this but happened error fun startVideoStreaming( cameraClient: CameraClient, textureView: TextureView ) { streamingController.setUpServer(cameraClient) streamingController.startStream() startPreview(textureView) } No value passed for parameter 'aspectRatioTextureView'

ronaldsampaio commented 4 months ago

Hello. I was with no time those weeks. Are you still with problems?

yubinjin commented 2 months ago

yes i have a one more question package com.aliger.usbstreaming

import android.Manifest import android.content.Context import android.content.pm.PackageManager import android.telephony.TelephonyManager import android.util.Log import android.view.SurfaceView import android.view.TextureView import android.widget.Toast import androidx.core.content.ContextCompat import com.jiangdg.ausbc.CameraClient import com.pedro.common.ConnectChecker import com.pedro.encoder.utils.gl.AspectRatioMode import com.pedro.library.rtsp.RtspStream import com.pedro.library.util.sources.audio.MicrophoneSource import com.pedro.library.view.OrientationForced import com.pedro.rtspserver.ClientListener import com.pedro.rtspserver.ServerClient import org.koin.core.annotation.Factory import java.nio.ByteBuffer

https://github.com/factory class StreamingController(private val context: Context) : ClientListener, ConnectChecker { private lateinit var rtspStream : RtspStream private var myFusedLocationProvider: MyFusedLocationProvider = MyFusedLocationProvider(context)

/private var portNum = 18554/ private var prepared = false

// 기존 코드는 유지하고 스트리밍 상태 확인 메소드 추가

fun setUpServer(cameraClient: CameraClient) { rtspStream = RtspStream( context, this, USBCameraSource(context,cameraClient), MicrophoneSource() ) rtspStream.getGlInterface().setAspectRatioMode(AspectRatioMode.Adjust) rtspStream.getGlInterface().forceOrientation(OrientationForced.LANDSCAPE) //rtspStream.getGlInterface().setPreviewResolution(200, 200) prepared = rtspStream.prepareVideo(1280, 720, 4000000) && rtspStream.prepareAudio(48000,false,128000) //prepared = rtspStream.prepareVideo(1920, 1080, 4000000) && rtspStream.prepareAudio(48000, false, 128000)

//rtspStream.changeVideoSource(usbCameraSource)

}

fun startStream() { if (::rtspStream.isInitialized && prepared) { val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager var streamName = getStreamName() // 기본 스트림 이름 / // READ_PHONE_STATE 권한 확인 if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { val phoneNumber = telephonyManager?.line1Number // 전화번호를 성공적으로 가져왔다면 스트림 이름을 전화번호로 설정 phoneNumber?.let { if (it.startsWith("+8210")) { streamName = it.replace("+8210", "010") // 국가 코드 +82를 010으로 변환 } else { streamName = it } } }/

    // 스트림 시작
    rtspStream.startStream("rtsp://192.168.0.9:8555/$streamName")
    Log.d("Streaming", "Stream started: rtsp://192.168.0.9:8554/$streamName")
    myFusedLocationProvider.setStreamName(streamName)

    myFusedLocationProvider.requestLocationUpdates()

    //myFusedLocationProvider.requestLocationUpdates()

} else {
    Log.d("Streaming", "rtspStream has not been initialized or not prepared")
    //Toast.makeText(context, "rtspStream has not been initialized or not prepared", Toast.LENGTH_LONG).show()
}

} fun changeVideoSource(usbCameraSource: USBCameraSource) { //rtspStream.changeVideoSource(usbCameraSource) rtspStream.changeVideoSource(usbCameraSource) } private fun getStreamName(): String { val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) { val phoneNumber = telephonyManager?.line1Number return phoneNumber?.let { if (it.startsWith("+8210")) it.replace("+8210", "010") else it } ?: "mystream2" // Default stream name if no phone number } return "mystream2" // Default stream name if permission not granted }

private fun decodeSpsPpsFromBuffer( outputBuffer: ByteBuffer, length: Int ): Pair<ByteBuffer, ByteBuffer>? { val csd = ByteArray(length) outputBuffer[csd, 0, length] var i = 0 var spsIndex = -1 var ppsIndex = -1 while (i < length - 4) { if (csd[i].toInt() == 0 && csd[i + 1].toInt() == 0 && csd[i + 2].toInt() == 0 && csd[i + 3].toInt() == 1) { if (spsIndex == -1) { spsIndex = i } else { ppsIndex = i break } } i++ } if (spsIndex != -1 && ppsIndex != -1) { val sps = ByteArray(ppsIndex) System.arraycopy(csd, spsIndex, sps, 0, ppsIndex) val pps = ByteArray(length - ppsIndex) System.arraycopy(csd, ppsIndex, pps, 0, length - ppsIndex) return Pair(ByteBuffer.wrap(sps), ByteBuffer.wrap(pps)) } return null }

fun startPreview(textureView: TextureView){ if (prepared && !rtspStream.isOnPreview) { rtspStream.startPreview(textureView) myFusedLocationProvider.requestLocationUpdates()}

}

fun startPreview(surfaceView: SurfaceView) { if (prepared && !rtspStream.isOnPreview) rtspStream.startPreview(surfaceView) }

fun stopStream(){ if (rtspStream.isStreaming) { rtspStream.stopStream() myFusedLocationProvider.removeLocationUpdates() } }

fun stopPreview() { if (rtspStream.isOnPreview) rtspStream.stopPreview() }

fun release() { rtspStream.release() } init { // MyFusedLocationProvider 인스턴스 생성 } //FROM ClientListener() override fun onClientConnected(client: ServerClient) { Log.d("Streaming","Client Connected: ${client.name}")

}

override fun onClientDisconnected(client: ServerClient) { Log.d("Streaming","Client Disconnected: ${client.name}") }

//FROM ConnectChecker() override fun onAuthError() { Log.e("Streaming","Auth ERROR!") }

override fun onAuthSuccess() { Log.d("Streaming","Auth Success") }

override fun onConnectionFailed(reason: String) { Log.e("Streaming", "Connection failed: $reason") val streamName = getStreamName() if (rtspStream.getStreamClient() .reTry(5000, reason, "rtsp://192.168.0.9:8554/$streamName") ) {

    Toast.makeText(context, "Retrying with secondary server: $streamName", Toast.LENGTH_SHORT).show()
    myFusedLocationProvider.setStreamName(streamName)
    myFusedLocationProvider.requestLocationUpdates()
} else {
    Toast.makeText(context, "Connection permanently failed: $reason", Toast.LENGTH_SHORT).show()
    myFusedLocationProvider.removeLocationUpdates()
    rtspStream.stopStream()
}

}

override fun onConnectionStarted(url: String) { Log.d("Streaming","Connection Started with: $url") }

override fun onConnectionSuccess() { Log.d("Streaming","Connection Successful!") }

override fun onDisconnect() { Log.d("Streaming","onDisconnect!") }

override fun onNewBitrate(bitrate: Long) { } } this is not activity retrybackupurl is this alright?

ronaldsampaio commented 2 months ago

Hi, So to help you I need to know what is your problem and at which part of your code. Please create a code snippet and explain what you wanna do and what is not working.

MrAdi149 commented 3 days ago

Hey @ronaldsampaio why you didn't use CameraUsbSource.kt file in your project, also i cloned your repository for my external web camera and my application stucks on Give all Permission page, can you help me out?