Skip to content

Commit

Permalink
1.1.0 优化回调
Browse files Browse the repository at this point in the history
  • Loading branch information
LxzBUG committed Mar 14, 2023
1 parent 643ce70 commit 9505011
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 36 deletions.
4 changes: 2 additions & 2 deletions screenShareKit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode 10
versionName "1.0.9"
versionCode 11
versionName "1.1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package org.loka.screensharekit

import android.content.res.Configuration
import android.util.Log
import android.os.Build
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import org.loka.screensharekit.callback.ErrorCallBack
import org.loka.screensharekit.callback.H264CallBack
import org.loka.screensharekit.callback.ScreenShareCallBack

class EncodeBuilder(fragment: Fragment?,fragmentActivity: FragmentActivity?):Device.RotationListener{

private lateinit var activity: FragmentActivity
private var fragment: Fragment? = null

@JvmField
var h264CallBack : H264CallBack? = null
var errorCallBack : ErrorCallBack? = null
var shareCallBack : ScreenShareCallBack? = null
internal val encodeConfig = EncodeConfig()
private val device by lazy { Device() }

Expand Down Expand Up @@ -58,21 +55,20 @@ class EncodeBuilder(fragment: Fragment?,fragmentActivity: FragmentActivity?):Dev



fun onH264(callBack: H264CallBack?):EncodeBuilder{
fun onShareCallback(callBack: ScreenShareCallBack?):EncodeBuilder{
return apply {
h264CallBack = callBack
shareCallBack = callBack
}
}

fun onError(callBack: ErrorCallBack?):EncodeBuilder{
return apply {
errorCallBack = callBack
fun start(){
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
invisibleFragment.requestMediaProjection(this)
device.setRotationListener(this)
}else{
shareCallBack?.onError(ErrorInfo(-3,"当前系统版本不支持"))
}
}

fun start(){
invisibleFragment.requestMediaProjection(this)
device.setRotationListener(this)
}

fun stop(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ class InvisibleFragment : Fragment(){
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 5000){
if (resultCode == Activity.RESULT_CANCELED){
encodeBuilder?.errorCallBack?.onError(ErrorInfo(-1,"已取消"))
encodeBuilder?.shareCallBack?.onError(ErrorInfo(-1,"已取消"))
}else{
if (resultCode == Activity.RESULT_OK){
data?.let {
encodeBuilder?.shareCallBack?.onStart()
activity?.startService(ScreenReaderService.getStartIntent(context,resultCode,data))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ class ScreenReaderService : Service() {
configData?.put(outputBuffer)
}else{
val data = createOutputBufferInfo(info,index,outputBuffer!!)
encodeBuilder.h264CallBack?.onH264(data.buffer,data.isKeyFrame,encodeBuilder.encodeConfig.width,encodeBuilder.encodeConfig.height,data.presentationTimestampUs)
encodeBuilder.shareCallBack?.onH264(data.buffer,data.isKeyFrame,encodeBuilder.encodeConfig.width,encodeBuilder.encodeConfig.height,data.presentationTimestampUs)
}
codec.releaseOutputBuffer(index, false)

}

override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
encodeBuilder.errorCallBack?.onError(ErrorInfo(-1,e.message.toString()))
encodeBuilder.shareCallBack?.onError(ErrorInfo(-2,"编码器错误${e.message.toString()}"))
}

override fun onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) {
Expand Down Expand Up @@ -207,14 +207,14 @@ class ScreenReaderService : Service() {
configData?.put(outputBuffer)
}else{
val data = createOutputBufferInfo(info,index,outputBuffer!!)
encodeBuilder.h264CallBack?.onH264(data.buffer,data.isKeyFrame,encodeBuilder.encodeConfig.width,encodeBuilder.encodeConfig.height,data.presentationTimestampUs)
encodeBuilder.shareCallBack?.onH264(data.buffer,data.isKeyFrame,encodeBuilder.encodeConfig.width,encodeBuilder.encodeConfig.height,data.presentationTimestampUs)
}
codec.releaseOutputBuffer(index, false)

}

override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
encodeBuilder.errorCallBack?.onError(ErrorInfo(-1,e.message.toString()))
encodeBuilder.shareCallBack?.onError(ErrorInfo(-2,"编码器错误${e.message.toString()}"))
}

override fun onOutputFormatChanged(codec: MediaCodec, format: MediaFormat) {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.loka.screensharekit.callback

import org.loka.screensharekit.ErrorInfo
import java.nio.ByteBuffer

interface ScreenShareCallBack {
fun onStart()
fun onError(error:ErrorInfo)
fun onH264(buffer: ByteBuffer, isKeyFrame: Boolean, width:Int, height:Int, ts: Long)
}

0 comments on commit 9505011

Please sign in to comment.