Skip to content

Commit

Permalink
Provide sample to update hero view
Browse files Browse the repository at this point in the history
  • Loading branch information
mekya committed Jul 16, 2024
1 parent 9cfa341 commit 8384fbf
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
20 changes: 17 additions & 3 deletions WebRTC-Sample-App/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ WebRTC Live Streaming</string>
<rect key="frame" x="0.0" y="0.0" width="390" height="834"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="v8Q-eO-C1v">
<rect key="frame" x="0.0" y="0.0" width="390" height="834"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="v8Q-eO-C1v">
<rect key="frame" x="0.0" y="200" width="390" height="634"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<collectionViewFlowLayout key="collectionViewLayout" automaticEstimatedItemSize="YES" minimumLineSpacing="10" minimumInteritemSpacing="10" id="2Nz-ci-6za">
<size key="itemSize" width="272" height="256"/>
Expand Down Expand Up @@ -217,17 +216,32 @@ WebRTC Live Streaming</string>
<constraint firstAttribute="width" constant="90" id="tMd-w9-0ro"/>
</constraints>
</view>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="9Y7-Gu-YHc" customClass="RTCMTLVideoView">
<rect key="frame" x="0.0" y="0.0" width="390" height="200"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstAttribute="height" constant="200" id="nbV-00-tZd"/>
</constraints>
</view>
</subviews>
<viewLayoutGuide key="safeArea" id="dqn-Tz-Jj5"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="dqn-Tz-Jj5" firstAttribute="bottom" secondItem="Zwp-DO-5T7" secondAttribute="bottom" constant="20" id="Fpc-aE-wjW" userLabel="Safe Area.bottom = Local View.bottom"/>
<constraint firstItem="9Y7-Gu-YHc" firstAttribute="top" secondItem="dqn-Tz-Jj5" secondAttribute="top" id="GSq-UA-gQi"/>
<constraint firstItem="dqn-Tz-Jj5" firstAttribute="trailing" secondItem="v8Q-eO-C1v" secondAttribute="trailing" id="HDn-DF-pPN"/>
<constraint firstItem="9Y7-Gu-YHc" firstAttribute="leading" secondItem="dqn-Tz-Jj5" secondAttribute="leading" id="Moc-Ar-up1"/>
<constraint firstItem="v8Q-eO-C1v" firstAttribute="leading" secondItem="dqn-Tz-Jj5" secondAttribute="leading" id="O2j-tb-89J"/>
<constraint firstItem="v8Q-eO-C1v" firstAttribute="bottom" secondItem="dqn-Tz-Jj5" secondAttribute="bottom" id="Rlb-vc-OLv"/>
<constraint firstItem="v8Q-eO-C1v" firstAttribute="top" secondItem="9Y7-Gu-YHc" secondAttribute="bottom" id="ZD8-86-4wP"/>
<constraint firstItem="dqn-Tz-Jj5" firstAttribute="trailing" secondItem="Zwp-DO-5T7" secondAttribute="trailing" constant="33" id="pND-Zi-qjI" userLabel="Safe Area.trailing = Local View.trailing"/>
<constraint firstItem="9Y7-Gu-YHc" firstAttribute="trailing" secondItem="dqn-Tz-Jj5" secondAttribute="trailing" id="zg8-Rt-pHw"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="l7b-W5-MXv"/>
<connections>
<outlet property="collectionView" destination="v8Q-eO-C1v" id="tlK-Cg-lCn"/>
<outlet property="heroView" destination="9Y7-Gu-YHc" id="D6o-t3-s6g"/>
<outlet property="localView" destination="Zwp-DO-5T7" id="ada-FF-Y7z"/>
</connections>
</viewController>
Expand Down
51 changes: 49 additions & 2 deletions WebRTC-Sample-App/ConferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,24 @@ open class ConferenceViewController: UIViewController , AVCaptureVideoDataOutpu
var clientUrl: String!
var roomId: String!
var publisherStreamId: String!

@IBOutlet var localView: UIView!

@IBOutlet weak var collectionView: UICollectionView!

@IBOutlet weak var heroView: RTCMTLVideoView!
//keeps which remoteView renders which track according to the index
var remoteViewTrackMap: [RTCVideoTrack?] = [];

var heroVideoTrack: RTCVideoTrack?;

var conferenceClient: AntMediaClient?;

//key is the videoTrack in WebRTC
//value is the streamId in the server
var videoTrackMap: [String: String] = [:]


func generateRandomAlphanumericString(length: Int) -> String {
let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in characters.randomElement()! })
Expand Down Expand Up @@ -84,7 +92,7 @@ open class ConferenceViewController: UIViewController , AVCaptureVideoDataOutpu
extension ConferenceViewController: AntMediaClientDelegate
{
public func clientHasError(_ message: String) {

debugPrint("Error is \(message)");
}

public func dataReceivedFromDataChannel(streamId: String, data: Data, binary: Bool) {
Expand Down Expand Up @@ -177,6 +185,8 @@ extension ConferenceViewController: AntMediaClientDelegate
{

print("videoLabel:\(videoLabel) plays the trackId:\(trackId)")

videoTrackMap["ARDAMSv" + videoLabel] = trackId;
//On the server side, we create WebRTC tracks with ARDAMSv{VIDEO_LABEL}
//It's useful in limiting/dynamic allocation of the streams and tracks in a conference call
//If you want to make sure, which webrtc track is playing which real streamId,
Expand All @@ -189,6 +199,9 @@ extension ConferenceViewController: AntMediaClientDelegate
}
}
}
else if (eventType == RESOLUTION_CHANGE_INFO_COMMAND) {
AntMediaClient.printf("Resolution has changed");
}
}
}

Expand All @@ -206,6 +219,40 @@ extension ConferenceViewController: UICollectionViewDataSource {
return cell
}


public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
debugPrint("Item selected at \(indexPath.item)")
if let videoTrack = heroVideoTrack {
//if there is already heroVideoTrack, remove from renderer
videoTrack.remove(heroView);
//add to the current grid
remoteViewTrackMap.append(videoTrack)

//decrease the quality
if let videoTrackId = videoTrackMap[videoTrack.trackId] {
conferenceClient?.forceStreamQuality(resolutionHeight: 240, streamId: videoTrackId )
}

}

//get new track
heroVideoTrack = remoteViewTrackMap.remove(at: indexPath.item)
//render it in hero view
heroVideoTrack?.add(heroView);
//increase the resolution to 720
if let videoTrack = heroVideoTrack
{
if let videoTrackId = videoTrackMap[videoTrack.trackId] {
conferenceClient?.forceStreamQuality(resolutionHeight: 720, streamId: videoTrackId);
}
}

//update the grid
collectionView.reloadData()


}

}

extension ConferenceViewController: UICollectionViewDelegateFlowLayout {
Expand Down

0 comments on commit 8384fbf

Please sign in to comment.