Skip to content

Commit

Permalink
Merge pull request #50 from ant-media/add_broadcast_object
Browse files Browse the repository at this point in the history
Implement getBroadcastObject Websocket message
  • Loading branch information
mekya authored May 25, 2024
2 parents 747e76a + b3c4f08 commit bd79a0f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
19 changes: 18 additions & 1 deletion WebRTC-Sample-App/ConferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,12 @@ extension ConferenceViewController: AntMediaClientDelegate

public func publishStarted(streamId: String) {
AntMediaClient.printf("Publish started for stream:\(streamId)")

//this plays the streams in the room
self.conferenceClient?.play(streamId: self.roomId);

conferenceClient?.getBroadcastObject(forStreamId: self.roomId)

}

public func publishFinished(streamId: String) {
Expand All @@ -236,5 +239,19 @@ extension ConferenceViewController: AntMediaClientDelegate
public func videoView(_ videoView: RTCVideoRenderer, didChangeVideoSize size: CGSize) {
AntMediaClient.printf("Video size changed to " + String(Int(size.width)) + "x" + String(Int(size.height)) + ". These changes are not handled in Simulator for now")
}

public func onLoadBroadcastObject(streamId: String, message: [String : Any]) {
debugPrint(streamId)
debugPrint(message)
}

public func eventHappened(streamId: String, eventType: String, payload: [String : Any]?)
{
debugPrint("Event: \(streamId) - \(eventType) \(payload ?? [:])")

if eventType == EVENT_TYPE_TRACK_LIST_UPDATED {
conferenceClient?.getBroadcastObject(forStreamId: streamId)
}
}
}

41 changes: 41 additions & 0 deletions WebRTCiOSSDK/api/AntMediaClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,14 @@ open class AntMediaClient: NSObject, AntMediaClientProtocol {
let streams = message[STREAMS] as! [String];
self.joinedRoom(streamId: streamId, streams:streams);
}
else if definition == BROADCAST_OBJECT_NOTIFICATION { // broadcastObject
let broadcastString = message["broadcast"] as! String
let broadcastObject = broadcastString.toJSON()
self.delegate?.onLoadBroadcastObject(
streamId: message[STREAM_ID] as! String,
message: broadcastObject ?? [:]
)
}

break;
case ROOM_INFORMATION_COMMAND:
Expand Down Expand Up @@ -1209,6 +1217,25 @@ open class AntMediaClient: NSObject, AntMediaClientProtocol {

}

func sendCommand(command: String, streamId: String) {
let command = [
COMMAND: command,
STREAM_ID: streamId
].json;

webSocket?.write(string: command)
}

public func getBroadcastObject(forStreamId id: String)
{
AntMediaClient.printf("GetBroadcastObject for \(id)")

sendCommand(
command: GET_BROADCAST_OBJECT_COMMAND,
streamId: id
)
}

}

extension AntMediaClient: WebRTCClientDelegate {
Expand Down Expand Up @@ -1270,6 +1297,18 @@ extension AntMediaClient: WebRTCClientDelegate {
//event happened
if let incomingStreamId = json?[STREAM_ID] {
self.delegate?.eventHappened(streamId:incomingStreamId as! String , eventType:eventType as! String);

self.delegate?.eventHappened(
streamId: incomingStreamId as! String,
eventType: eventType as! String
)

self.delegate?.eventHappened(
streamId: incomingStreamId as! String,
eventType: eventType as! String,
payload: json
)

}
else {
AntMediaClient.printf("Incoming message does not have streamId:\(json)")
Expand All @@ -1280,6 +1319,8 @@ extension AntMediaClient: WebRTCClientDelegate {
}
}



}

extension AntMediaClient: WebSocketDelegate {
Expand Down
24 changes: 24 additions & 0 deletions WebRTCiOSSDK/api/AntMediaClientDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,17 @@ public protocol AntMediaClientDelegate: AnyObject {
- streamId: The id of the stream that the event happened
- evenType: The type of the event
*/
@available(*, deprecated, message: "Will be removed soon. Use eventHappened(streamId, eventType, payload)")
func eventHappened(streamId:String, eventType:String);

/**
It's called when there is an event happen such microphone is muted or unmuted for the specific streamId
- Parameters
- streamId: The id of the stream that the event happened
- evenType: The type of the event
- payload: The payload of the event
*/
func eventHappened(streamId:String, eventType:String, payload: [String:Any]?)

/**
It's called when a new track is added to the stream. It works both on multirack streaming and conferencing
Expand All @@ -132,6 +142,11 @@ public protocol AntMediaClientDelegate: AnyObject {
*/
func trackRemoved(track:RTCMediaStreamTrack)

/**
It's called when server responses to getBroadcastObject method in AntMediaClient.
*/
func onLoadBroadcastObject(streamId: String, message: [String: Any])

/**
It's called after join to the room.
- streamId: the id of the stream tha can be used to publish stream.
Expand Down Expand Up @@ -236,5 +251,14 @@ public extension AntMediaClientDelegate {
}
}

func onLoadBroadcastObject(streamId: String, message: [String: Any]) {
AntMediaClient.printf("onLoadBroadcastObject is called for \(streamId) and incoming response: \(message)")
}

func eventHappened(streamId:String, eventType:String, payload: [String:Any]?) {
AntMediaClient.printf("\(streamId) \(eventType) with")
AntMediaClient.printf(payload?.json ?? "")
}

}

8 changes: 8 additions & 0 deletions WebRTCiOSSDK/api/AntMediaClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ let EVENT_TYPE_MIC_MUTED = "MIC_MUTED"
let EVENT_TYPE_MIC_UNMUTED = "MIC_UNMUTED";
let EVENT_TYPE_CAM_TURNED_OFF = "CAM_TURNED_OFF";
let EVENT_TYPE_CAM_TURNED_ON = "CAM_TURNED_ON";
let GET_BROADCAST_OBJECT_COMMAND = "getBroadcastObject"
let BROADCAST_OBJECT_NOTIFICATION = "broadcastObject"
public let EVENT_TYPE_TRACK_LIST_UPDATED = "TRACK_LIST_UPDATED"

let ENABLE_TRACK_COMMAND = "enableTrack"
let ENABLE_VIDEO_TRACK_COMMAND = "toggleVideo"
Expand Down Expand Up @@ -350,6 +353,11 @@ public protocol AntMediaClientProtocol {
*/
func setDegradationPreference(_ degradationPreference: RTCDegradationPreference);

/**
Get the broadcast object from the server. After the broadcast object has been received, it calls AntMediaClientDelegat:onLoadBroadcastObject method
*/
func getBroadcastObject(forStreamId id: String)

/**
Disconnects websocket connection
*/
Expand Down

0 comments on commit bd79a0f

Please sign in to comment.