Skip to content

Commit

Permalink
1.1.2 try fix崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
LxzBUG committed Mar 15, 2023
1 parent dd63013 commit b2042b4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 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 11
versionName "1.1.0"
versionCode 12
versionName "1.1.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ScreenReaderService : Service() {
private var configData: ByteBuffer?=null

private val encodeBuilder by lazy { ScreenShareKit.encodeBuilder }

private var isStop = false

override fun onCreate() {
super.onCreate()
Expand Down Expand Up @@ -80,35 +80,45 @@ class ScreenReaderService : Service() {
index: Int,
info: MediaCodec.BufferInfo
) {
isCodecRunning = true
val outputBuffer:ByteBuffer?
try {
outputBuffer = codec.getOutputBuffer(index)
if (outputBuffer == null){
synchronized(codec) {

isCodecRunning = true
val outputBuffer: ByteBuffer?
try {
outputBuffer = codec.getOutputBuffer(index)
if (outputBuffer == null) {
return
}
} catch (e: IllegalStateException) {
return
}
}catch (e:IllegalStateException){
return
}
val keyFrame = (info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0
if (keyFrame){
configData = ByteBuffer.allocate(info.size)
configData?.put(outputBuffer)
}else{
val data = createOutputBufferInfo(info,index,outputBuffer!!)
encodeBuilder.shareCallBack?.onH264(data.buffer,data.isKeyFrame,encodeBuilder.encodeConfig.width,encodeBuilder.encodeConfig.height,data.presentationTimestampUs)
}
if (index >= 0) {
// 判断缓冲区是否已经被释放
if ((info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) == 0) {
// 判断编码器是否处于运行状态
if (isCodecRunning) {
codec.releaseOutputBuffer(index, false)
val keyFrame = (info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0
if (keyFrame) {
configData = ByteBuffer.allocate(info.size)
configData?.put(outputBuffer)
} else {
val data = createOutputBufferInfo(info, index, outputBuffer!!)
encodeBuilder.shareCallBack?.onH264(
data.buffer,
data.isKeyFrame,
encodeBuilder.encodeConfig.width,
encodeBuilder.encodeConfig.height,
data.presentationTimestampUs
)
}
if (index >= 0) {
// 判断缓冲区是否已经被释放
if ((info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) == 0) {
// 判断编码器是否处于运行状态
if (isCodecRunning) {
if (!isStop) {
codec.releaseOutputBuffer(index, false)
}
}
}
}
}


}

override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
Expand Down Expand Up @@ -154,6 +164,7 @@ class ScreenReaderService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
intent?.let {
if(isStartCommand(it)){
isStop = false
val notification = NotificationUtils.getNotification(this)
startForeground(notification.first, notification.second)
startProjection(
Expand All @@ -162,6 +173,7 @@ class ScreenReaderService : Service() {
)!!
)
}else if (isStopCommand(it)){
isStop = true
stopProjection()
stopSelf()
}else if (isResetCommand(it)){
Expand Down Expand Up @@ -202,34 +214,43 @@ class ScreenReaderService : Service() {
index: Int,
info: MediaCodec.BufferInfo
) {
isCodecRunning = true
val outputBuffer:ByteBuffer?
try {
outputBuffer = codec.getOutputBuffer(index)
if (outputBuffer == null){
synchronized(codec) {
isCodecRunning = true
val outputBuffer: ByteBuffer?
try {
outputBuffer = codec.getOutputBuffer(index)
if (outputBuffer == null) {
return
}
} catch (e: IllegalStateException) {
return
}
}catch (e:IllegalStateException){
return
}
val keyFrame = (info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0
if (keyFrame){
configData = ByteBuffer.allocate(info.size)
configData?.put(outputBuffer)
}else{
val data = createOutputBufferInfo(info,index,outputBuffer!!)
encodeBuilder.shareCallBack?.onH264(data.buffer,data.isKeyFrame,encodeBuilder.encodeConfig.width,encodeBuilder.encodeConfig.height,data.presentationTimestampUs)
}
if (index >= 0) {
// 判断缓冲区是否已经被释放
if ((info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) == 0) {
// 判断编码器是否处于运行状态
if (isCodecRunning) {
codec.releaseOutputBuffer(index, false)
val keyFrame = (info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0
if (keyFrame) {
configData = ByteBuffer.allocate(info.size)
configData?.put(outputBuffer)
} else {
val data = createOutputBufferInfo(info, index, outputBuffer!!)
encodeBuilder.shareCallBack?.onH264(
data.buffer,
data.isKeyFrame,
encodeBuilder.encodeConfig.width,
encodeBuilder.encodeConfig.height,
data.presentationTimestampUs
)
}
if (index >= 0) {
// 判断缓冲区是否已经被释放
if ((info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM) == 0) {
// 判断编码器是否处于运行状态
if (isCodecRunning) {
if (!isStop) {
codec.releaseOutputBuffer(index, false)
}
}
}
}
}

}

override fun onError(codec: MediaCodec, e: MediaCodec.CodecException) {
Expand Down

0 comments on commit b2042b4

Please sign in to comment.