Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine- Just (WIP) #220

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Basic-Video-Chat/Basic-Video-Chat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
1F89D35A2C76A7C800C5BCC8 /* OTCombine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F89D3592C76A7C800C5BCC8 /* OTCombine.swift */; };
1F89D3A82C823F8B00C5BCC8 /* OTSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F89D3A72C823F8B00C5BCC8 /* OTSync.swift */; };
1FC46F0B2C911B1B00559594 /* OTAsyncStreams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FC46F0A2C911B1B00559594 /* OTAsyncStreams.swift */; };
917374CC2EDCE1AEEF738DF3 /* Pods_Basic_Video_Chat.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 866721D2D8E2F702D8CA4F27 /* Pods_Basic_Video_Chat.framework */; };
A05375D71EB1633400645696 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A05375CF1EB1633400645696 /* AppDelegate.swift */; };
A05375D81EB1633400645696 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A05375D01EB1633400645696 /* Assets.xcassets */; };
A05375D91EB1633400645696 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A05375D11EB1633400645696 /* LaunchScreen.storyboard */; };
Expand All @@ -15,12 +19,18 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
05ACCAB5CA1CC410391EA229 /* Pods-Basic-Video-Chat.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Basic-Video-Chat.debug.xcconfig"; path = "Target Support Files/Pods-Basic-Video-Chat/Pods-Basic-Video-Chat.debug.xcconfig"; sourceTree = "<group>"; };
1F89D3592C76A7C800C5BCC8 /* OTCombine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OTCombine.swift; sourceTree = "<group>"; };
1F89D3A72C823F8B00C5BCC8 /* OTSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OTSync.swift; sourceTree = "<group>"; };
1FC46F0A2C911B1B00559594 /* OTAsyncStreams.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OTAsyncStreams.swift; sourceTree = "<group>"; };
866721D2D8E2F702D8CA4F27 /* Pods_Basic_Video_Chat.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Basic_Video_Chat.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A05375CF1EB1633400645696 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
A05375D01EB1633400645696 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
A05375D21EB1633400645696 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
A05375D41EB1633400645696 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
A05375D51EB1633400645696 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A05375D61EB1633400645696 /* ViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
E94E4666DFCCBD88204DA277 /* Pods-Basic-Video-Chat.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Basic-Video-Chat.release.xcconfig"; path = "Target Support Files/Pods-Basic-Video-Chat/Pods-Basic-Video-Chat.release.xcconfig"; sourceTree = "<group>"; };
F86C649A1D5C7C630081846D /* Basic-Video-Chat.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Basic-Video-Chat.app"; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand All @@ -29,12 +39,22 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
917374CC2EDCE1AEEF738DF3 /* Pods_Basic_Video_Chat.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
1C7D17731CEF44EFF259A7A9 /* Pods */ = {
isa = PBXGroup;
children = (
05ACCAB5CA1CC410391EA229 /* Pods-Basic-Video-Chat.debug.xcconfig */,
E94E4666DFCCBD88204DA277 /* Pods-Basic-Video-Chat.release.xcconfig */,
);
path = Pods;
sourceTree = "<group>";
};
A05375CE1EB1633400645696 /* Basic-Video-Chat */ = {
isa = PBXGroup;
children = (
Expand All @@ -44,15 +64,28 @@
A05375D31EB1633400645696 /* Main.storyboard */,
A05375D51EB1633400645696 /* Info.plist */,
A05375D61EB1633400645696 /* ViewController.swift */,
1F89D3592C76A7C800C5BCC8 /* OTCombine.swift */,
1F89D3A72C823F8B00C5BCC8 /* OTSync.swift */,
1FC46F0A2C911B1B00559594 /* OTAsyncStreams.swift */,
);
path = "Basic-Video-Chat";
sourceTree = "<group>";
};
AF3AACD939D1096B7A14C6A3 /* Frameworks */ = {
isa = PBXGroup;
children = (
866721D2D8E2F702D8CA4F27 /* Pods_Basic_Video_Chat.framework */,
);
name = Frameworks;
sourceTree = "<group>";
};
F86C64911D5C7C630081846D = {
isa = PBXGroup;
children = (
A05375CE1EB1633400645696 /* Basic-Video-Chat */,
F86C649B1D5C7C630081846D /* Products */,
1C7D17731CEF44EFF259A7A9 /* Pods */,
AF3AACD939D1096B7A14C6A3 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand All @@ -71,9 +104,11 @@
isa = PBXNativeTarget;
buildConfigurationList = F86C64AC1D5C7C630081846D /* Build configuration list for PBXNativeTarget "Basic-Video-Chat" */;
buildPhases = (
5D77703359C24DC91AC9C61E /* [CP] Check Pods Manifest.lock */,
F86C64961D5C7C630081846D /* Sources */,
F86C64971D5C7C630081846D /* Frameworks */,
F86C64981D5C7C630081846D /* Resources */,
C208F08D1D59572290A8FE00 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -134,13 +169,59 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
5D77703359C24DC91AC9C61E /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputFileListPaths = (
);
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-Basic-Video-Chat-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
C208F08D1D59572290A8FE00 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Basic-Video-Chat/Pods-Basic-Video-Chat-resources.sh",
"${PODS_CONFIGURATION_BUILD_DIR}/OTXCFramework/OTPrivacyResources.bundle",
);
name = "[CP] Copy Pods Resources";
outputPaths = (
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/OTPrivacyResources.bundle",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Basic-Video-Chat/Pods-Basic-Video-Chat-resources.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
F86C64961D5C7C630081846D /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A05375DC1EB1633400645696 /* ViewController.swift in Sources */,
A05375D71EB1633400645696 /* AppDelegate.swift in Sources */,
1FC46F0B2C911B1B00559594 /* OTAsyncStreams.swift in Sources */,
1F89D35A2C76A7C800C5BCC8 /* OTCombine.swift in Sources */,
1F89D3A82C823F8B00C5BCC8 /* OTSync.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -272,10 +353,12 @@
};
F86C64AD1D5C7C630081846D /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 05ACCAB5CA1CC410391EA229 /* Pods-Basic-Video-Chat.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/Basic-Video-Chat/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tokbox.Hello-World";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -287,10 +370,12 @@
};
F86C64AE1D5C7C630081846D /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = E94E4666DFCCBD88204DA277 /* Pods-Basic-Video-Chat.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = "$(SRCROOT)/Basic-Video-Chat/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tokbox.Hello-World";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
74 changes: 74 additions & 0 deletions Basic-Video-Chat/Basic-Video-Chat/OTAsyncStreams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// OTAsyncStreams.swift
// Basic-Video-Chat
//
// Created by Jaideep Shah on 9/10/24.
// Copyright © 2024 tokbox. All rights reserved.
//


import Foundation
import OpenTok

// Define a class that will provide the AsyncStream
class OTSessionManager: NSObject, OTSessionDelegate {

private let streamContinuation: AsyncStream<OTStream>.Continuation
private var session: OTSession?

init(streamContinuation: AsyncStream<OTStream>.Continuation) {
self.streamContinuation = streamContinuation
super.init()
}

func startSession(_ session: OTSession) {
self.session = session
self.session?.delegate = self
}

func sessionDidConnect(_ session: OTSession) {

}
func sessionDidDisconnect(_ session: OTSession) {

}
func session(_ session: OTSession, streamDestroyed stream: OTStream) {

}
func session(_ session: OTSession, didFailWithError error: OTError) {

}
// Objective-C callback for stream creation
func session(_ mySession: OTSession, streamCreated stream: OTStream) {
// Yield the OTStream to the AsyncStream
streamContinuation.yield(stream)
}

// Method to finish the AsyncStream
func finishStream() {
streamContinuation.finish()
}
}

// Function to create an AsyncStream of OTStream
func otStreamAsyncStream() -> AsyncStream<OTStream> {
AsyncStream { continuation in
let manager = OTSessionManager(streamContinuation: continuation)

// Assuming you have a way to start the session, e.g., with a session object
// manager.startSession(yourSessionObject)

// Return the manager, which will handle the lifecycle of the session
// and provide streams
}
}

// Test
//Task {
// let asyncStream = otStreamAsyncStream()
//
// for await stream in asyncStream {
// // Handle the OTStream objects here
// print("Received OTStream: \(stream)")
// }
//}
107 changes: 107 additions & 0 deletions Basic-Video-Chat/Basic-Video-Chat/OTCombine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// OTCombine.swift
// Basic-Video-Chat
//
// Created by Jaideep Shah on 8/21/24.
// Copyright © 2024 tokbox. All rights reserved.
//

import Foundation
import OpenTok
import Combine

class OTSwPublisher: OTPublisher, OTPublisherKitNetworkStatsDelegate {
var namePublisher: Just<String> = Just("")
var subject = PassthroughSubject<[OTPublisherKitVideoNetworkStats], Never>()

override init?(delegate: (any OTPublisherKitDelegate)?) {
super.init(delegate: delegate)
self.namePublisher = Just(self.name ?? "" )
}

override init?(delegate: (any OTPublisherKitDelegate)?, settings: OTPublisherSettings) {
super.init(delegate: delegate, settings: settings)
self.namePublisher = Just(self.name ?? "")
}

func setupNetworkStatsDelegate() {
//TODO: make a combine specific delegate in ObjC , so as to not conflict
self.networkStatsDelegate = self
}

func stopNetworkStatsDelegate() {
self.networkStatsDelegate = nil
subject.send(completion: .finished)
}

// Method to emit values via the subject
private func emitValue(_ stats: [OTPublisherKitVideoNetworkStats]) {
subject.send(stats)
}

func publisher(_ publisher: OTPublisherKit, videoNetworkStatsUpdated stats: [OTPublisherKitVideoNetworkStats]) {
emitValue(stats)
}

}

/*
import Combine

class GenericDelegateHandler<T>: NSObject {
// Generic PassthroughSubject for any type T
var subject = PassthroughSubject<T, Never>()

// Initializer
init(publisher: AnyObject, setup: (AnyObject, GenericDelegateHandler<T>) -> Void) {
super.init()
setup(publisher, self)
}

// Method to emit values via the subject
func emitValue(_ value: T) {
subject.send(value)
}

// Method to stop the subject
func stop() {
subject.send(completion: .finished)
}
}

import Combine

// Define your specific type
typealias VideoNetworkStatsHandler = GenericDelegateHandler<[OTPublisherKitVideoNetworkStats]>

// Usage with OTPublisherKit
let videoNetworkStatsHandler = VideoNetworkStatsHandler(publisher: publisher) { publisher, handler in
guard let publisher = publisher as? OTPublisherKit else { return }
publisher.delegate = handler
}

// Implementing the delegate method
extension VideoNetworkStatsHandler: OTPublisherKitDelegate {
func publisher(_ publisher: OTPublisherKit, videoNetworkStatsUpdated stats: [OTPublisherKitVideoNetworkStats]) {
// Send the stats via the subject when the delegate method is called
emitValue(stats)
}
}

// Subscription example
let subscription = videoNetworkStatsHandler.subject.sink { statsArray in
for stat in statsArray {
print("bytesSend: \(stat.bytesSend)")
}
}

typealias AnotherHandler = GenericDelegateHandler<YourDataType>

// Implement the corresponding delegate method
extension AnotherHandler: YourDelegateType {
func yourDelegateMethod(data: YourDataType) {
emitValue(data)
}
}

*/
Loading