Runt
2025-04-14 d0aec90013f06ed4b258235bcabe02e50550271a
app/src/main/java/com/runt/live/ui/stream/LiveViewModel.kt
@@ -1,25 +1,18 @@
package com.runt.live.ui.stream
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Point
import android.graphics.Rect
import android.os.Handler
import android.util.Log
import android.view.SurfaceHolder
import com.runt.live.R
import com.runt.live.cpp.LiveMiniView
import com.runt.live.cpp.LiveMiniView.FRAME_HEIGHT
import com.runt.live.cpp.LiveMiniView.FRAME_WIDTH
import com.runt.live.data.StreamWindow
import com.runt.live.enum.LiveState
import com.runt.live.enum.StreamType
import com.runt.live.native.LivePuller
import com.runt.live.native.LivePusher
import com.runt.live.native.MediaPlayer
import com.runt.live.ui.stream.LiveLayoutView.Companion.subStreamsState
import com.runt.live.util.BitmapUtils
import com.runt.open.mvi.base.model.BaseViewModel
import java.nio.ByteBuffer
import java.util.concurrent.ConcurrentSkipListMap
import kotlin.concurrent.thread
@@ -145,57 +138,13 @@
            streamWindow.audioState.value == LiveState.IN_LIVE,streamWindow.videoState.value == LiveState.IN_LIVE,
            streamWindow.videoDelay,streamWindow.streamType.value,
            streamWindow.mainPositionRateState.value, streamWindow.viewRateState.value)
    }
    fun removeMiniView(streamCode :Int){
        LiveMiniView.native_remove_mini_view(streamCode)
    }
    fun updateText(streamWindow : StreamWindow){
        thread {
            var bytes = LiveMiniView.native_get_cut_frame(subStreamsState.value.indexOf(streamWindow),streamWindow.id)
            var bitmap = BitmapUtils.instance!!.textToBitmap(streamWindow.remark!!,130f,getActivity().resources.getColor(R.color.white)!!,getActivity().resources.getColor(R.color.transparent)!!)
            var cutwidth : Int = (FRAME_WIDTH * streamWindow.viewRateState.value).toInt()
            var cutHeight : Int = (FRAME_WIDTH / ( streamWindow.sizeState.value.x * 1.0 /  streamWindow.sizeState.value.y ) * streamWindow.viewRateState.value).toInt()
            //涉及到 YUV 4:2:0 格式的图像时,宽度和高度通常需要是 2 的倍数
            if (cutwidth % 2 == 1) {
                cutwidth += 1
            }
            if (cutHeight % 2 == 1) {
                cutHeight += 1
            }
            if (cutwidth > FRAME_WIDTH) {
                cutwidth = FRAME_WIDTH
            }
            if (cutHeight > FRAME_HEIGHT) {
                cutHeight = FRAME_HEIGHT
            }
            val cutBitmap = Bitmap.createBitmap(cutwidth, cutHeight, Bitmap.Config.ARGB_8888)
            Log.e(TAG , "updateText: ${cutwidth}x${cutHeight} ${streamWindow.sizeState.value} ${( streamWindow.sizeState.value.x * 1.0 /  streamWindow.sizeState.value.y )}", )
            val buffer = ByteBuffer.wrap(bytes)
            cutBitmap.copyPixelsFromBuffer(buffer)
            val dstBitmap = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)
            val canvas = Canvas(dstBitmap)
            // 构建目标区域
            val destRect = Rect(0,0,dstBitmap.width,dstBitmap.height)
            // 绘制小图到大图
            canvas.drawBitmap(cutBitmap, null, destRect, null)
            //canvas.drawBitmap(bitmap, null, destRect, null)
            val width : Int = bitmap.getWidth()
            val height : Int = bitmap.getHeight()
            val argb = IntArray(width * height)
            dstBitmap.getPixels(argb , 0 , width , 0 , 0 , width , height)
            //LiveMiniView.native_push_nv21(streamWindow.id,BitmapUtils.instance!!.argbToNV21(argb,width,height));
        }
    }
    fun openText(streamWindow : StreamWindow){
        var bitmap = BitmapUtils.instance!!.textToBitmap(streamWindow.remark!!,130f,getActivity().resources.getColor(R.color.white)!!,getActivity().resources.getColor(R.color.transparent)!!)
@@ -212,11 +161,6 @@
                streamWindow.listener?.onSurfaceUpdate?.let { it(holder.surface) }
                thread {
                    BitmapUtils.instance!!.cavansSurface(holder,bitmap);
                    val width : Int = bitmap.getWidth()
                    val height : Int = bitmap.getHeight()
                    val argb = IntArray(width * height)
                    bitmap.getPixels(argb , 0 , width , 0 , 0 , width , height)
                    LiveMiniView.native_push_nv21(streamWindow.id,BitmapUtils.instance!!.argbToNV21(argb,width,height));
                }
                //Log.w(TAG , "surfaceChanged: ${holder.hashCode()}" , )
            }
@@ -227,7 +171,7 @@
                //Log.w(TAG , "surfaceDestroyed: ${holder.hashCode()} ${id}" , )
            }
        }
        streamWindow.listener?.onStarted?.invoke()
        Handler().postDelayed({streamWindow.listener?.onStarted?.invoke()},500)
    }
    /**