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

Zoom feature #61

Open
wants to merge 4 commits into
base: master
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
69 changes: 49 additions & 20 deletions ScreenShare/SampleHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
import ReplayKit
import WebRTCiOSSDK
import WebRTC
import CoreVideo
import CoreImage
import CoreMedia

class SampleHandler: RPBroadcastSampleHandler, AntMediaClientDelegate {

class SampleHandler: RPBroadcastSampleHandler, AntMediaClientDelegate {
let sharedDefault = UserDefaults(suiteName: "group.io.antmedia.sbd.webrtc.sample")! // for test

func clientHasError(_ message: String) {
let userInfo = [NSLocalizedFailureReasonErrorKey: message]

Expand All @@ -21,6 +26,32 @@ class SampleHandler: RPBroadcastSampleHandler, AntMediaClientDelegate {

func publishStarted(streamId: String) {
NSLog("Publish has started");
// self.client.setFrameCapturer { [weak self] buffer, frame in
// guard let self, let requiredFrame = self.sharedDefault.object(forKey: "screenShareFrame") as? [CGFloat] else {
// return frame
// }
//
// // topSafeArea iPhone 12 mini
// let topSafeArea: CGFloat = 44
// let convertedFrame = convertRectToBufferFrame(
// rect: .init(x: requiredFrame[0], y: requiredFrame[1] + topSafeArea, width: requiredFrame[2], height: requiredFrame[3]),
// screenSize: UIScreen.main.bounds.size,
// bufferSize: frame.size
// )
// return convertedFrame
// }
}

func convertRectToBufferFrame(rect: CGRect, screenSize: CGSize, bufferSize: CGSize) -> CGRect {
let scaleX = bufferSize.width / screenSize.width
let scaleY = bufferSize.height / screenSize.height

let projectedX = rect.origin.x * scaleX
let projectedY = rect.origin.y * scaleY
let projectedWidth = rect.size.width * scaleX
let projectedHeight = rect.size.height * scaleY

return CGRect(x: projectedX, y: projectedY, width: projectedWidth, height: projectedHeight)
}

func publishFinished(streamId: String) {
Expand All @@ -40,15 +71,14 @@ class SampleHandler: RPBroadcastSampleHandler, AntMediaClientDelegate {
override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) {
// User has requested to start the broadcast. Setup info from the UI extension can be supplied but optional.

let sharedDefault = UserDefaults(suiteName: "group.io.antmedia.ios.webrtc.sample")!
// original: group.io.antmedia.ios.webrtc.sample

streamId = sharedDefault.object(forKey: "streamId") as! String;
let url = sharedDefault.object(forKey: "url");
let token = sharedDefault.object(forKey: "token");
// let token = sharedDefault.object(forKey: "token") ;

let videoEnabledObject = sharedDefault.object(forKey:"videoEnabled") as! String;
if videoEnabledObject == "false"
{
if videoEnabledObject == "false" {
videoEnabled = false;
}

Expand All @@ -57,9 +87,10 @@ class SampleHandler: RPBroadcastSampleHandler, AntMediaClientDelegate {
audioEnabled = false;
}

if ((streamId) == nil)
{
let userInfo = [NSLocalizedFailureReasonErrorKey: "StreamId is not specified. Please specify stream id in the container app"]
if ((streamId).isEmpty) {
let userInfo = [
NSLocalizedFailureReasonErrorKey: "StreamId is not specified. Please specify stream id in the container app"
]

finishBroadcastWithError(NSError(domain: "ScreenShare", code: -1, userInfo: userInfo));
}
Expand All @@ -69,36 +100,34 @@ class SampleHandler: RPBroadcastSampleHandler, AntMediaClientDelegate {
finishBroadcastWithError(NSError(domain: "ScreenShare", code: -2, userInfo: userInfo));
}
else {
NSLog("----> streamId: %@ , websocket url: %@, videoEnabled: %d , audioEnabled: %d", streamId as! String, url as! String,
NSLog("----> streamId: %@ , websocket url: %@, videoEnabled: %d , audioEnabled: %d", streamId, url as! String,
videoEnabled, audioEnabled);

self.client.delegate = self
self.client.setDebug(true)
self.client.setUseExternalCameraSource(useExternalCameraSource: true)
self.client.setWebSocketServerUrl(url: url as! String)
if (videoEnabled != nil) {
self.client.setVideoEnable(enable: videoEnabled as! Bool);

if (videoEnabled != false) {
self.client.setVideoEnable(enable: videoEnabled);
self.client.setExternalVideoCapture(externalVideoCapture: true);
}

//in some ipad versions, resolution/aspect ratio is critical to set, otherwise iOS encoder may not encode the frames and
//server side reports publishTimeout because server cannot get the video frames
self.client.setTargetResolution(width: 1280, height: 720);
self.client.setMaxVideoBps(videoBitratePerSecond: 2000000)

self.client.setTargetResolution(width: 1920, height: 1080);
self.client.setMaxVideoBps(videoBitratePerSecond: 5000000)
self.client.setExternalAudio(externalAudioEnabled: true)

//In some devices iphone version, frames are dropped due to encoder queue and it causes glitches in the playback
//Decreasing the fps provides a better playback expeience.
//Alternatively, target resolution can be decreased above to let encoder work faster
self.client.setTargetFps(fps: 10)
self.client.setTargetFps(fps: 30)

self.client.publish(streamId: streamId as! String);
self.client.publish(streamId: streamId);

}


}

override func broadcastPaused() {
Expand All @@ -119,7 +148,7 @@ class SampleHandler: RPBroadcastSampleHandler, AntMediaClientDelegate {
// Handle video sample buffer
//NSLog("processSamplebuffer video");
if videoEnabled {
self.client.deliverExternalVideo(sampleBuffer: sampleBuffer);
self.client.deliverExternalVideo(sampleBuffer: sampleBuffer)
}
break
case RPSampleBufferType.audioApp:
Expand Down
3 changes: 2 additions & 1 deletion ScreenShare/ScreenShare.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.io.antmedia.ios.webrtc.sample</string>
<string>group.io.antmedia.sbd.webrtc.sample</string>
<string>group.io.sbd.chatdemo</string>
</array>
</dict>
</plist>
29 changes: 15 additions & 14 deletions WebRTC-Sample-App/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_0" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22685"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="collection view cell content view" minToolsVersion="11.0"/>
Expand All @@ -19,25 +19,25 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="logo" translatesAutoresizingMaskIntoConstraints="NO" id="U8S-UW-oXC">
<rect key="frame" x="95" y="87" width="200" height="200"/>
<rect key="frame" x="87.666666666666686" y="87" width="200" height="200"/>
<constraints>
<constraint firstAttribute="width" constant="200" id="1mh-o1-svn"/>
<constraint firstAttribute="width" secondItem="U8S-UW-oXC" secondAttribute="height" multiplier="1:1" id="KfE-gR-cN6"/>
<constraint firstAttribute="height" constant="200" id="uGw-jw-7QC"/>
</constraints>
</imageView>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="dMz-6A-4JU">
<rect key="frame" x="65" y="257" width="260" height="439"/>
<rect key="frame" x="20" y="257" width="335" height="325"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" usesAttributedText="YES" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="wtb-z3-Op3">
<rect key="frame" x="12.666666666666686" y="10" width="235" height="62"/>
<rect key="frame" x="50" y="10" width="235" height="62"/>
<constraints>
<constraint firstAttribute="width" constant="235" id="0c1-KP-HAt"/>
<constraint firstAttribute="height" constant="62" id="4qM-Ld-Ubq"/>
</constraints>
<attributedString key="attributedText">
<fragment>
<string key="content">Low Latency
<string key="content">Low Latency
WebRTC Live Streaming</string>
<attributes>
<font key="NSFont" size="20" name="HelveticaNeue-Medium"/>
Expand All @@ -48,7 +48,7 @@ WebRTC Live Streaming</string>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Room name" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="WPm-03-EZ0">
<rect key="frame" x="6" y="90" width="248" height="30"/>
<rect key="frame" x="6" y="90" width="323" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="lDx-qO-ch4"/>
</constraints>
Expand All @@ -59,14 +59,14 @@ WebRTC Live Streaming</string>
</connections>
</textField>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Sor-To-6JJ">
<rect key="frame" x="3" y="124" width="254" height="1.6666666666666714"/>
<rect key="frame" x="3" y="124" width="329" height="1.6666666666666714"/>
<color key="backgroundColor" red="0.8666666666666667" green="0.13725490196078433" blue="0.12941176470588237" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="1.5" id="K1Q-Vw-hNs"/>
</constraints>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="5ym-6R-Akx">
<rect key="frame" x="3" y="374" width="254" height="35"/>
<rect key="frame" x="3" y="260" width="329" height="35"/>
<color key="backgroundColor" red="0.86666666670000003" green="0.13725490200000001" blue="0.12941176469999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="height" constant="35" id="th5-xa-8Fc"/>
Expand All @@ -81,16 +81,17 @@ WebRTC Live Streaming</string>
</connections>
</button>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="tve-qy-d3T">
<rect key="frame" x="3" y="155.66666666666669" width="254" height="32"/>
<rect key="frame" x="3" y="155.66666666666669" width="329" height="32"/>
<segments>
<segment title="Publish"/>
<segment title="Play"/>
<segment title="Conference"/>
<segment title="Screen share"/>
</segments>
<color key="tintColor" white="0.33333333333333331" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</segmentedControl>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Inj-Ay-988">
<rect key="frame" x="6" y="340" width="248" height="36"/>
<rect key="frame" x="6" y="226" width="323" height="36"/>
<constraints>
<constraint firstAttribute="height" constant="36" id="vla-6X-HS0"/>
</constraints>
Expand Down Expand Up @@ -126,7 +127,7 @@ WebRTC Live Streaming</string>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="GZ9-KN-4BS" customClass="RPSystemBroadcastPickerView">
<rect key="frame" x="162.66666666666666" y="696" width="65" height="50"/>
<rect key="frame" x="162.66666666666666" y="582" width="50" height="50"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstAttribute="width" constant="50" id="27V-Uq-gnp"/>
Expand All @@ -141,9 +142,9 @@ WebRTC Live Streaming</string>
<constraint firstItem="U8S-UW-oXC" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" constant="40" id="6zy-xl-qt8"/>
<constraint firstItem="dMz-6A-4JU" firstAttribute="top" secondItem="U8S-UW-oXC" secondAttribute="bottom" constant="-30" id="7Hj-IJ-Nx0"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="GZ9-KN-4BS" secondAttribute="bottom" constant="64" id="94n-LQ-tGw"/>
<constraint firstItem="dMz-6A-4JU" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="65" id="TdP-c0-uMi"/>
<constraint firstItem="dMz-6A-4JU" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="20" id="TdP-c0-uMi"/>
<constraint firstItem="GZ9-KN-4BS" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="162.5" id="WHS-f1-PY7"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="dMz-6A-4JU" secondAttribute="trailing" constant="65" id="Wda-0o-WzD"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="dMz-6A-4JU" secondAttribute="trailing" constant="20" id="Wda-0o-WzD"/>
<constraint firstItem="GZ9-KN-4BS" firstAttribute="top" secondItem="dMz-6A-4JU" secondAttribute="bottom" id="kvg-nc-YLD"/>
<constraint firstItem="U8S-UW-oXC" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="swK-2M-D2s"/>
</constraints>
Expand Down
Loading