Merge remote-tracking branch 'origin/master'

This commit is contained in:
tursom 2021-08-17 13:46:42 +08:00
commit bf1ebebb93
32 changed files with 220 additions and 171 deletions

3
.gitignore vendored
View File

@ -6,4 +6,5 @@
build
*/build/
#gradle.properties
local.properties
local.properties
!.idea/codeStyles

View File

@ -1,9 +1,10 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.util.*
ext["netty.version"] = "4.1.59.Final"
ext["excludeTest"] = { project: Project, tasks: TaskContainer ->
if (project.gradle.startParameter.taskNames.firstOrNull { taskName ->
taskName.endsWith(":test")
":test" in taskName
} == null) {
tasks {
test { enabled = false }
@ -14,7 +15,48 @@ ext["excludeTest"] = { project: Project, tasks: TaskContainer ->
}
}
}
ext["publishRepositories"] = { project: Project, p: PublishingExtension ->
p.repositories {
try {
val artifactoryUser: String by rootProject
val artifactoryPassword: String by rootProject
maven {
val releasesRepoUrl = uri("https://nvm.tursom.cn/repository/maven-releases/")
val snapshotRepoUrl = uri("https://nvm.tursom.cn/repository/maven-snapshots/")
url = if (project.version.toString().endsWith("SNAPSHOT")) snapshotRepoUrl else releasesRepoUrl
credentials {
username = artifactoryUser
password = artifactoryPassword
}
}
} catch (e: Exception) {
System.err.println("无法将包推送到tursom仓库上")
}
try {
maven {
val githubUser: String by rootProject
val githubToken: String by rootProject
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/$githubUser/TursomServer")
credentials {
username = githubUser
password = githubToken
}
}
} catch (e: Exception) {
System.err.println("无法将包推送到github仓库上")
}
}
}
try {
val properties = Properties()
properties.load(rootProject.file("local.properties").inputStream())
properties.forEach { (k, v) ->
rootProject.ext.set(k.toString(), v)
}
} catch (e: Exception) {
}
plugins {
kotlin("jvm") version "1.5.21"
@ -23,11 +65,14 @@ plugins {
allprojects {
group = "cn.tursom"
version = "0.2"
version = "1.0-SNAPSHOT"
repositories {
mavenLocal()
// mavenLocal()
mavenCentral()
maven {
url = uri("https://nvm.tursom.cn/repository/maven-public/")
}
}
tasks.withType<JavaCompile> {
@ -52,7 +97,7 @@ allprojects {
}
}
@kotlin.Suppress("UNCHECKED_CAST")
@Suppress("UNCHECKED_CAST")
(rootProject.ext["excludeTest"] as (Project, TaskContainer) -> Unit)(project, tasks)
dependencies {
@ -69,18 +114,20 @@ tasks.register("install") {
finalizedBy(tasks["publishToMavenLocal"])
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = project.name
version = project.version.toString()
from(components["java"])
try {
artifact(tasks["kotlinSourcesJar"])
} catch (e: Exception) {
}
}
}
}
//publishing {
// @Suppress("UNCHECKED_CAST")
// (rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
// publications {
// create<MavenPublication>("maven") {
// groupId = project.group.toString()
// artifactId = project.name
// version = project.version.toString()
//
// from(components["java"])
// try {
// artifact(tasks["kotlinSourcesJar"])
// } catch (e: Exception) {
// }
// }
// }
//}

View File

@ -20,6 +20,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -32,6 +32,8 @@ artifacts {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -21,6 +21,8 @@ artifacts {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -1,151 +0,0 @@
package cn.tursom.core
import cn.tursom.core.stream.InputStream
import cn.tursom.core.stream.OutputStream
import java.io.IOException
/**
* 原理 https://www.cnblogs.com/lidabo/p/9018548.html
*/
class FlvChecker {
// 用于缓冲
private val buffer = ByteArray(1024 * 1024 * 16)
/**
* 从头部开始Check, 重新锚定时间戳, 将最后一帧(不管是否完整)去掉
*
* @param path
* @throws IOException
*/
// 用于统计时间戳
private val lastTimestampRead = intArrayOf(-1, -1)
private val lastTimestampWrite = intArrayOf(-1, -1)
/**
* 从头部开始Check, 重新锚定时间戳, 将最后一帧(不管是否完整)去掉
*/
fun check(raf: InputStream, rafNew: OutputStream) {
// 用于排除无效尾巴帧
// 复制头部
raf.read(buffer, 0, 9)
rafNew.write(buffer, 0, 9)
try {
//var remain = 40
//var timestamp = 0
loop@ while (true) {
//remain--
// 读取前一个tag size
readBytesToInt(raf, 4)
// Logger.print("前一个长度为:" + predataSize);
// 读取tag
// tag 类型
when (val tagType = raf.read().toInt()) {
8, 9 -> {
rafNew.write(buffer, 0, 4)
rafNew.write(tagType.toByte())
// tag data size 3个字节。表示tag data的长度。从streamd id 后算起。
val dataSize = readBytesToInt(raf, 3)
rafNew.write(buffer, 0, 3)
// 时间戳 3
val timestamp = readBytesToInt(raf, 3) + (raf.read().toInt() shl 24)
//timestamp += timestampEx
dealTimestamp(rafNew, timestamp, tagType - 8)
raf.read(buffer, 0, 3 + dataSize)
rafNew.write(buffer, 0, 3 + dataSize)
}
18 -> {
// 18 scripts
// 如果是scripts脚本默认为第一个tag此时将前一个tag Size 置零
val zeroTimestamp = byteArrayOf(0, 0, 0, 0)
rafNew.write(zeroTimestamp)
rafNew.write(tagType.toByte())
val dataSize = readBytesToInt(raf, 3)
rafNew.write(buffer, 0, 3)
raf.read(buffer, 0, 4)
val zeros = byteArrayOf(0, 0, 0)
rafNew.write(zeros) // 时间戳 0
rafNew.write(0) // 时间戳扩展 0
raf.read(buffer, 0, 3 + dataSize)
rafNew.write(buffer, 0, 3 + dataSize)
}
else -> {
break@loop
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
/**
* 处理音/视频时间戳
*
* @param raf
* @param timestamp
* @throws IOException
* @return 是否忽略该tag
*/
private fun dealTimestamp(raf: OutputStream, timestamp: Int, tagType: Int): Boolean {
// 如果是首帧
if (lastTimestampRead[tagType] == -1) {
lastTimestampWrite[tagType] = 0
} else if (timestamp >= lastTimestampRead[tagType]) {
// 如果时序正常
// 间隔十分巨大(1s),那么重新开始即可
if (timestamp > lastTimestampRead[tagType] + 1000) {
lastTimestampWrite[tagType] += 10
} else {
lastTimestampWrite[tagType] = timestamp - lastTimestampRead[tagType] + lastTimestampWrite[tagType]
}
} else {
// 如果出现倒序时间戳
// 如果间隔不大,那么如实反馈
if (lastTimestampRead[tagType] - timestamp < 5 * 1000) {
var tmp = timestamp - lastTimestampRead[tagType] + lastTimestampWrite[tagType]
tmp = if (tmp > 0) tmp else 1
lastTimestampWrite[tagType] = tmp
} else {
// 间隔十分巨大,那么重新开始即可
lastTimestampWrite[tagType] += 10
}
}
lastTimestampRead[tagType] = timestamp
// 低于0xffffff部分
val lowCurrentTime = lastTimestampWrite[tagType] and 0xffffff
raf.write(int2Bytes(lowCurrentTime), 1, 3)
// 高于0xffffff部分
val highCurrentTime = lastTimestampWrite[tagType] shr 24
raf.write(highCurrentTime.toByte())
return true
}
/**
* @param raf
* @param byteLength
* @return
* @throws IOException
*/
private fun readBytesToInt(raf: InputStream, byteLength: Int): Int {
raf.read(buffer, 0, byteLength)
return bytes2Int(buffer, byteLength)
}
private fun int2Bytes(value: Int): ByteArray {
val byteRet = ByteArray(4)
for (i in 0..3) {
byteRet[3 - i] = (value shr 8 * i and 0xff).toByte()
}
return byteRet
}
private fun bytes2Int(bytes: ByteArray, byteLength: Int): Int {
var result = 0
for (i in 0 until byteLength) {
result = result or (bytes[byteLength - 1 - i].toInt() and 0xff shl i * 8)
// System.out.printf("%x ",(bytes[i] & 0xff));
}
return result
}
}

View File

@ -18,6 +18,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -16,6 +16,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -16,6 +16,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -18,6 +18,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -16,6 +16,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -16,6 +16,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -18,6 +18,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -16,6 +16,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -18,6 +18,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -21,6 +21,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -16,6 +16,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -18,6 +18,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -23,6 +23,8 @@ artifacts {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -17,6 +17,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -17,6 +17,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -21,6 +21,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -20,6 +20,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -17,6 +17,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -18,6 +18,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -20,6 +20,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -0,0 +1,34 @@
package cn.tursom.channel.enhance
import cn.tursom.core.ShutdownHook
import cn.tursom.core.pool.HeapMemoryPool
import cn.tursom.core.pool.MemoryPool
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.Closeable
import kotlin.coroutines.CoroutineContext
@OptIn(DelicateCoroutinesApi::class)
class ChannelPipeline<V>(
context: CoroutineContext,
private val reader: ChannelReader<V>,
private val writer: ChannelWriter<V>,
private val pool: MemoryPool = HeapMemoryPool(),
) : Closeable {
private val job = GlobalScope.launch(context) {
while (true) {
val buffer = reader.read(pool)
writer.write(buffer)
}
}
@Suppress("unused")
private val hook = ShutdownHook.addHook(softReference = true) {
close()
}
override fun close() {
job.cancel()
}
}

View File

@ -0,0 +1,21 @@
package cn.tursom.channel.enhance.impl
import cn.tursom.channel.enhance.ChannelReader
import cn.tursom.core.pool.MemoryPool
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
class CoroutineContextChannelReader<T>(
var context: CoroutineContext,
private val prevReader: ChannelReader<T>,
) : ChannelReader<T> {
override suspend fun read(pool: MemoryPool, timeout: Long): T {
return withContext(context) {
prevReader.read(pool, timeout)
}
}
override fun close() {
prevReader.close()
}
}

View File

@ -0,0 +1,45 @@
package cn.tursom.channel.enhance.impl
import cn.tursom.channel.enhance.ChannelWriter
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
class CoroutineContextChannelWriter<T>(
var context: CoroutineContext,
private val prevWriter: ChannelWriter<T>,
) : ChannelWriter<T> {
override suspend fun write(vararg value: T) {
withContext(context) {
prevWriter.write(*value)
}
}
override suspend fun write(value: Collection<T>) {
withContext(context) {
prevWriter.write(value)
}
}
override suspend fun writeAndFlush(value: T, timeout: Long): Long {
return withContext(context) {
prevWriter.writeAndFlush(value, timeout)
}
}
override suspend fun write(value: T) {
withContext(context) {
prevWriter.write(value)
}
}
override suspend fun flush(timeout: Long): Long {
return withContext(context) {
prevWriter.flush()
}
}
override fun close() {
prevWriter.close()
}
}

View File

@ -19,6 +19,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -20,6 +20,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()

View File

@ -20,6 +20,8 @@ tasks.register("install") {
}
publishing {
@Suppress("UNCHECKED_CAST")
(rootProject.ext["publishRepositories"] as (Project, PublishingExtension) -> Unit)(project, this)
publications {
create<MavenPublication>("maven") {
groupId = project.group.toString()