How to stop tokbox screen sharing in SWIFT
To stop screen sharing will need to stop the publisher from streaming. To do that you can call:
[OTSession unpublish:error:]
More info is available on the Video API guides
For your case, where you are adding screen sharing to an existing call, you will need to create an additional publisher for the screen sharing rather than editing the existing one. To use the existing publisher it will require the publisher to be reinitialised to switch between publishing a camera feed vs a screen which will stop publishing audio too.
In addition to creating a new publisher, you need to create a new subscriber for the other user, you can do that in the subscriberDidConnect
delegate function on the OTSubscriberDelegate
.
Additionally, you will need to handle the destruction of both the new publisher and subscriber. This will be done in the delegate functions are you using already on the OTSessionDelegate
and OTPublisherDelegate
.
I have created a demo app which demonstrates this behaviour.
TOK BOX Screen sharing is not working outside my app
I had also faced the same issue and had mailed to tokbox support.
This was their response:
The way our screen capture code works is that it recursively traverses the view hierarchy and copies those images to a buffer and then send that buffer over on the webrtc data pipe. Hence once the app is pushed to the background, we could not traverse the view hierarchy and copy the image, so screen sharing works until we are in the application (Android or iOS native app). If you want to share the screen view of Opentok app only, it will work but outside the app won't work. It's just to take care of the privacy and security aspects of the mobile app users.
So according to them you cannot share screen outside the application. It will only work when app is in foreground.
Update
After constantly asking the tokbox support team I got the following reply from them:
To screenshare the content outside of your application on Android and iOS can be achieved.
For Android, you need to use the Media Projection API together with Vonage/Tokbox Custom Capturer.
For iOS, you need to use the iOS ReplayKit together with Vonage/Tokbox Custom Capturer.
Basically, the implementation is to get a frame from Media Project API or Replaykit and then pass it via a custom capturer.
Following their response, I found Accelerator Core Android repo which showed how to integrate Media Projection API with tokbox.
More specifically these two files: ScreenSharingFragment.java and ScreenSharingCapturer.java
Using these two files I am now able to share screen outside my application.
Note:
Apps that target Android 9 (API level 28) or higher should use Foreground services or else your app will crash due to security reasons.
Opentok : How to kill streams / publishers properly?
On the JS library (officially maintained by TokBox), all publishers and subscribers should be cleaned up automatically after calling session.disconnect()
. Cordova (aka PhoneGap) is a community maintained project (not officially maintained by TokBox) and dangling publisher/subscribers seems to be a bug in the cordova plugin.
There was a pull request a few days ago, can you try updating the cordova plugin and see if the issue goes away? https://github.com/songz/cordova-plugin-opentok/pull/79
If not, you should file an issue on the project page: https://github.com/songz/cordova-plugin-opentok/issues
tokbox moderating toggle off/on subscriber's video/audio
TokBox Developer Evangelist here.
The moderator token doesn't allow you to mute other streams because you can stop publishing audio by calling publishAudio(false)
or stop subscribing to the audio by calling subscribeToAudio(false)
.
You can design your application to send a signal
and make it so that everyone listening to the specific signal in the connect session will stop publishing their audio. You can add custom logic to only have some participants stop publishing their audio.
Let's assume that you're connected to a session and have a reference to the Session
object stored as session
. You can send a signal by calling the signal
method on the session
object like so:
session.signal(
{
data:'mutePublisher',
},
function(error) {
if (error) {
console.log("signal error ("
+ error.name
+ "): " + error.message);
} else {
console.log("signal sent.");
}
}
);
Let's assume that there are other participants connected to the same session and who are also publishing audio. You can set an event listener for the signal
event for them like so:
session.on({
signal: function (event) {
if (event.data === 'mutePublisher') {
// mute publisher
publisher.publishAudio(false);
}
}
As you can see in the code above, anyone listening for the signal
event with data
set to mutePublisher
would stop publishing their audio. You can use the same approach to send another signal for these participants to start publishing audio or video using publisher.publishAudio(true)
and publisher.publishVideo(true)
, respectively.
Please keep in mind the sample above would send a signal to everyone in the session. To send a signal to a specific client in a session, call the signal()
method of the Session
object and set the to
property of the signal parameter.
Related Topics
How to Display an Image by an API Url? Swift
Declaring Conformance to @Objc Protocol in Empty Extension Breaks with Exc_Bad_Instruction
How to Run App in Simulator Xcode 6
How to Integrate Mapbox Sdk with Swiftui
How to Make Struct Lazylist in Swiftui
How to Execute Multiplications And/Or Divisions in the Right Order
Diddiscoverservices Is Not Being Called After a Ble Connection
Swift How to Sort Dict Keys by Byte Value and Not Alphabetically
Hiding Dividers in Nssplitview
How to Stop Tokbox Screen Sharing in Swift
Swift Interstitial Making a Banner Ad
Swift - Protocol as Type as Target of Button Action
Error on Adding Uinavigationbar Button with Image
Unexpectedly Found Nil While Unwrapping an Optional Value Parsing JSON Swift