Skip to content

Instantly share code, notes, and snippets.

@mekya
Created July 8, 2023 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mekya/163a90523f7e7796cf97a0b966f2d61b to your computer and use it in GitHub Desktop.
Save mekya/163a90523f7e7796cf97a0b966f2d61b to your computer and use it in GitHub Desktop.
Broadcast Extension
//
// SampleHandler.swift
// BroadcastScreen
//
// Created by mekya on 08/07/2023.
//
import ReplayKit
import WebRTCiOSSDK
class SampleHandler: RPBroadcastSampleHandler , AntMediaClientDelegate{
func clientHasError(_ message: String) {
}
func publishStarted(streamId: String) {
NSLog("Publish has started");
}
func publishFinished(streamId: String) {
NSLog("Publish has stopped");
}
func dataReceivedFromDataChannel(streamId: String, data: Data, binary: Bool) {
}
let client: AntMediaClient = AntMediaClient.init()
var videoEnabled: Bool = true;
var audioEnabled: Bool = true;
var streamId:String = "";
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.
streamId = "any_stream_id";
let url = "ws://test.antmedia.io:5080/WebRTCAppEE/websocket";
NSLog("----> streamId: %@ , websocket url: %@, videoEnabled: %d , audioEnabled: %d", streamId , url ,
videoEnabled, audioEnabled);
self.client.delegate = self
self.client.setUseExternalCameraSource(useExternalCameraSource: true)
self.client.setWebSocketServerUrl(url: url )
self.client.setVideoEnable(enable: videoEnabled );
self.client.setExternalVideoCapture(externalVideoCapture: true);
self.client.setTargetResolution(width: 1280, height: 720);
self.client.setExternalAudio(externalAudioEnabled: true)
self.client.publish(streamId: streamId );
}
override func broadcastPaused() {
// User has requested to pause the broadcast. Samples will stop being delivered.
}
override func broadcastResumed() {
// User has requested to resume the broadcast. Samples delivery will resume.
}
override func broadcastFinished() {
self.client.stop(streamId: self.streamId);
}
override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
switch sampleBufferType {
case RPSampleBufferType.video:
// Handle video sample buffer
//NSLog("processSamplebuffer video");
if videoEnabled {
self.client.deliverExternalVideo(sampleBuffer: sampleBuffer);
}
break
case RPSampleBufferType.audioApp:
// Handle audio sample buffer for app audio
//NSLog("processSamplebuffer audio");
if audioEnabled {
self.client.deliverExternalAudio(sampleBuffer: sampleBuffer);
}
break
case RPSampleBufferType.audioMic:
// Handle audio sample buffer for mic audio.
// You can choose
// NSLog("processSamplebuffer audio mic");
// if audioEnabled {
// self.client.deliverExternalAudio(sampleBuffer: sampleBuffer);
// }
break
@unknown default:
// Handle other sample buffer types
fatalError("Unknown type of sample buffer")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment