Skip to content

Commit

Permalink
Replaced Ktor with SwiftNIO, Removed all Ktor references
Browse files Browse the repository at this point in the history
  • Loading branch information
yuroyami committed Mar 31, 2024
1 parent a97852c commit 1d559fb
Show file tree
Hide file tree
Showing 19 changed files with 639 additions and 357 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ class WatchActivity : ComponentActivity() {
setContent {
RoomUI()
}

Thread.setDefaultUncaughtExceptionHandler { t, t2 ->
loggy(t2.stackTraceToString())
}
}

/** the onStart() follows the onCreate(), it means all the UI is ready
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.yuroyami.syncplay.utils.RoomUtils.sendPlayback
import com.yuroyami.syncplay.utils.collectInfoLocalAndroid
import com.yuroyami.syncplay.utils.getFileName
import com.yuroyami.syncplay.utils.loggy
import com.yuroyami.syncplay.utils.lookForNeighborSub
import com.yuroyami.syncplay.utils.timeStamper
import com.yuroyami.syncplay.watchroom.dispatchOSD
import com.yuroyami.syncplay.watchroom.isSoloMode
Expand Down Expand Up @@ -251,19 +250,21 @@ class MpvPlayer : BasePlayer() {
if (!isUrl) {
ctx.resolveUri(it.toUri())?.let { it2 ->
loggy("Final path $it2", 301)
if (!ismpvInit) {
mpvView.initialize(ctx.filesDir.path, ctx.cacheDir.path)
ismpvInit = true
if (ismpvInit) {
MPVLib.destroy()
}
mpvView.initialize(ctx.filesDir.path, ctx.cacheDir.path)
ismpvInit = true
mpvObserverAttach()
mpvView.playFile(it2)
mpvView.surfaceCreated(mpvView.holder)
}
} else {
if (!ismpvInit) {
mpvView.initialize(ctx.filesDir.path, ctx.cacheDir.path)
ismpvInit = true
if (ismpvInit) {
MPVLib.destroy()
}
mpvView.initialize(ctx.filesDir.path, ctx.cacheDir.path)
ismpvInit = true
mpvObserverAttach()
mpvView.playFile(uri.toString())
mpvView.surfaceCreated(mpvView.holder)
Expand Down Expand Up @@ -326,7 +327,7 @@ class MpvPlayer : BasePlayer() {
loggy("currentAspect: $currentAspect and currentPanscan: $currentPanscan", 0)

val aspectRatios = listOf(
"-1.000000" to "Original" ,
"-1.000000" to "Original",
"1.777778" to "16:9",
"1.600000" to "16:10",
"1.333333" to "4:3",
Expand Down
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ plugins {
id("org.jetbrains.compose") version compose apply false

id("com.google.devtools.ksp") version "2.0.0-Beta4-1.0.17" apply false

//id("io.github.ttypic.swiftklib") version "0.5.1"
}
111 changes: 111 additions & 0 deletions iosApp/iosApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
objects = {

/* Begin PBXBuildFile section */
0404ED202BB9374F00FCDAB5 /* NIO in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED1F2BB9374F00FCDAB5 /* NIO */; };
0404ED222BB9374F00FCDAB5 /* NIOConcurrencyHelpers in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED212BB9374F00FCDAB5 /* NIOConcurrencyHelpers */; };
0404ED242BB9374F00FCDAB5 /* NIOCore in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED232BB9374F00FCDAB5 /* NIOCore */; };
0404ED262BB9374F00FCDAB5 /* NIOEmbedded in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED252BB9374F00FCDAB5 /* NIOEmbedded */; };
0404ED282BB9374F00FCDAB5 /* NIOFoundationCompat in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED272BB9374F00FCDAB5 /* NIOFoundationCompat */; };
0404ED2A2BB9375000FCDAB5 /* NIOPosix in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED292BB9375000FCDAB5 /* NIOPosix */; };
0404ED2C2BB9375000FCDAB5 /* NIOTLS in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED2B2BB9375000FCDAB5 /* NIOTLS */; };
0404ED2E2BB9375000FCDAB5 /* NIOTestUtils in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED2D2BB9375000FCDAB5 /* NIOTestUtils */; };
0404ED302BB9375000FCDAB5 /* _NIOConcurrency in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED2F2BB9375000FCDAB5 /* _NIOConcurrency */; };
0404ED322BB9375000FCDAB5 /* _NIOFileSystem in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED312BB9375000FCDAB5 /* _NIOFileSystem */; };
0404ED342BB9375000FCDAB5 /* _NIOFileSystemFoundationCompat in Frameworks */ = {isa = PBXBuildFile; productRef = 0404ED332BB9375000FCDAB5 /* _NIOFileSystemFoundationCompat */; };
0404ED362BB9379800FCDAB5 /* SpProtocolIApple.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0404ED352BB9379800FCDAB5 /* SpProtocolIApple.swift */; };
0424B733C25F1A6509DE1F07 /* Pods_iosApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E94BC4DE8C9E41F2FD82ABC4 /* Pods_iosApp.framework */; };
045FEB1D2B91A34200F9C842 /* SyncplayScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 045FEB1C2B91A34200F9C842 /* SyncplayScreen.swift */; };
04BF80022B9704B9002E2EDE /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 04BF80012B9704B9002E2EDE /* AudioToolbox.framework */; };
Expand All @@ -20,6 +32,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
0404ED352BB9379800FCDAB5 /* SpProtocolIApple.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpProtocolIApple.swift; sourceTree = "<group>"; };
045FEB1C2B91A34200F9C842 /* SyncplayScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncplayScreen.swift; sourceTree = "<group>"; };
04BF80012B9704B9002E2EDE /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
04BF80032B9704C1002E2EDE /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -56,10 +69,21 @@
buildActionMask = 2147483647;
files = (
04BF80242B97056B002E2EDE /* UIKit.framework in Frameworks */,
0404ED322BB9375000FCDAB5 /* _NIOFileSystem in Frameworks */,
0424B733C25F1A6509DE1F07 /* Pods_iosApp.framework in Frameworks */,
0404ED342BB9375000FCDAB5 /* _NIOFileSystemFoundationCompat in Frameworks */,
0404ED2A2BB9375000FCDAB5 /* NIOPosix in Frameworks */,
04BF80022B9704B9002E2EDE /* AudioToolbox.framework in Frameworks */,
0404ED262BB9374F00FCDAB5 /* NIOEmbedded in Frameworks */,
0404ED202BB9374F00FCDAB5 /* NIO in Frameworks */,
0404ED222BB9374F00FCDAB5 /* NIOConcurrencyHelpers in Frameworks */,
0404ED282BB9374F00FCDAB5 /* NIOFoundationCompat in Frameworks */,
0404ED2E2BB9375000FCDAB5 /* NIOTestUtils in Frameworks */,
04BF80122B9704FA002E2EDE /* Foundation.framework in Frameworks */,
0404ED242BB9374F00FCDAB5 /* NIOCore in Frameworks */,
04BF80042B9704C1002E2EDE /* AVFoundation.framework in Frameworks */,
0404ED2C2BB9375000FCDAB5 /* NIOTLS in Frameworks */,
0404ED302BB9375000FCDAB5 /* _NIOConcurrency in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -126,6 +150,7 @@
7555FF8C242A565B00829871 /* Info.plist */,
2152FB032600AC8F00CF470E /* iOSApp.swift */,
045FEB1C2B91A34200F9C842 /* SyncplayScreen.swift */,
0404ED352BB9379800FCDAB5 /* SpProtocolIApple.swift */,
058557D7273AAEEB004C7B11 /* Preview Content */,
);
path = iosApp;
Expand Down Expand Up @@ -159,6 +184,19 @@
dependencies = (
);
name = iosApp;
packageProductDependencies = (
0404ED1F2BB9374F00FCDAB5 /* NIO */,
0404ED212BB9374F00FCDAB5 /* NIOConcurrencyHelpers */,
0404ED232BB9374F00FCDAB5 /* NIOCore */,
0404ED252BB9374F00FCDAB5 /* NIOEmbedded */,
0404ED272BB9374F00FCDAB5 /* NIOFoundationCompat */,
0404ED292BB9375000FCDAB5 /* NIOPosix */,
0404ED2B2BB9375000FCDAB5 /* NIOTLS */,
0404ED2D2BB9375000FCDAB5 /* NIOTestUtils */,
0404ED2F2BB9375000FCDAB5 /* _NIOConcurrency */,
0404ED312BB9375000FCDAB5 /* _NIOFileSystem */,
0404ED332BB9375000FCDAB5 /* _NIOFileSystemFoundationCompat */,
);
productName = iosApp;
productReference = 7555FF7B242A565900829871 /* iosApp.app */;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -187,6 +225,9 @@
Base,
);
mainGroup = 7555FF72242A565900829871;
packageReferences = (
0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */,
);
productRefGroup = 7555FF7C242A565900829871 /* Products */;
projectDirPath = "";
projectRoot = "";
Expand Down Expand Up @@ -275,6 +316,7 @@
files = (
2152FB042600AC8F00CF470E /* iOSApp.swift in Sources */,
045FEB1D2B91A34200F9C842 /* SyncplayScreen.swift in Sources */,
0404ED362BB9379800FCDAB5 /* SpProtocolIApple.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -473,6 +515,75 @@
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-nio.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 2.64.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
0404ED1F2BB9374F00FCDAB5 /* NIO */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIO;
};
0404ED212BB9374F00FCDAB5 /* NIOConcurrencyHelpers */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIOConcurrencyHelpers;
};
0404ED232BB9374F00FCDAB5 /* NIOCore */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIOCore;
};
0404ED252BB9374F00FCDAB5 /* NIOEmbedded */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIOEmbedded;
};
0404ED272BB9374F00FCDAB5 /* NIOFoundationCompat */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIOFoundationCompat;
};
0404ED292BB9375000FCDAB5 /* NIOPosix */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIOPosix;
};
0404ED2B2BB9375000FCDAB5 /* NIOTLS */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIOTLS;
};
0404ED2D2BB9375000FCDAB5 /* NIOTestUtils */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = NIOTestUtils;
};
0404ED2F2BB9375000FCDAB5 /* _NIOConcurrency */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = _NIOConcurrency;
};
0404ED312BB9375000FCDAB5 /* _NIOFileSystem */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = _NIOFileSystem;
};
0404ED332BB9375000FCDAB5 /* _NIOFileSystemFoundationCompat */ = {
isa = XCSwiftPackageProductDependency;
package = 0404ED1E2BB9374F00FCDAB5 /* XCRemoteSwiftPackageReference "swift-nio" */;
productName = _NIOFileSystemFoundationCompat;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 7555FF73242A565900829871 /* Project object */;
}
41 changes: 41 additions & 0 deletions iosApp/iosApp.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"pins" : [
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "cd142fd2f64be2100422d658e7411e39489da985",
"version" : "1.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections.git",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio.git",
"state" : {
"revision" : "fc63f0cf4e55a4597407a9fc95b16a2bc44b4982",
"version" : "2.64.0"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system.git",
"state" : {
"revision" : "025bcb1165deab2e20d4eaba79967ce73013f496",
"version" : "1.2.1"
}
}
],
"version" : 2
}
106 changes: 106 additions & 0 deletions iosApp/iosApp/SpProtocolIApple.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Foundation
import NIO
import NIOFoundationCompat
import shared

class SpProtocolApple: SyncplayProtocol {
private var channel: Channel?
private var eventLoopGroup: EventLoopGroup?


override func connectSocket() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
eventLoopGroup = group

print("Bootstrapping...")

let bootstrap = ClientBootstrap(group: group)
.connectTimeout(TimeAmount.seconds(10))
.channelInitializer { channel in
channel.pipeline.addHandler(Reader(p: self))
}

let host = session.serverHost
let port = Int(session.serverPort)

do {
channel = try bootstrap.connect(host: host, port: port).wait()
} catch {
print(error)
syncplayCallback?.onConnectionFailed()
return
}

if channel != nil {
print("Connected!")
syncplayCallback?.onConnected()
}
}


override func isSocketValid() -> Bool {
return channel?.isActive ?? false
}

override func endConnection(terminating: Bool) {
try? channel?.close().wait()
try? eventLoopGroup?.syncShutdownGracefully()

if terminating {
terminateScope()
}
}

override func writeActualString(s: String) {
guard let channel = channel else {
syncplayCallback?.onDisconnected()
return
}

let data = s.data(using: .utf8)!
let buffer = channel.allocator.buffer(bytes: data)

channel.writeAndFlush(buffer).whenComplete { result in
do {
try result.get()
} catch {
self.syncplayCallback?.onDisconnected()
print("Error writing to channel: \(error)")
}
}
}

override func upgradeTls() {
// TLS setup for SwiftNIO
}
}

class Reader: ChannelInboundHandler {
typealias InboundIn = ByteBuffer
let p: SpProtocolApple

init(p: SpProtocolApple) {
self.p = p
}

func channelRead(context: ChannelHandlerContext, data: NIOAny) {
var buffer = unwrapInboundIn(data)
let readableBytes = buffer.readableBytes

if let received = buffer.readString(length: readableBytes) {
self.p.routeInScope() {
JsonHandlerKt.handleJson(protocol: self.p, json: received)
}
}
}

func channelReadComplete(context: ChannelHandlerContext) {
context.flush()
}

func errorCaught(context: ChannelHandlerContext, error: Error) {
print("Reader exception: \(error)")
p.syncplayCallback?.onDisconnected()
context.close(promise: nil)
}
}
5 changes: 5 additions & 0 deletions iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ struct iOSApp: App {

init() {
UIApplication.shared.isIdleTimerDisabled = true //Keep screen on

DatastoreUtilsKt.initializeDatastoreIOS()

SomeMoreUtilsKt.instantiateSyncplayProtocolSwiftNIO = {
return SpProtocolApple()
}
}

var body: some Scene {
Expand Down
Loading

0 comments on commit 1d559fb

Please sign in to comment.