Skip to content

Commit

Permalink
Add workground feature RTMPStream.send.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Mar 16, 2024
1 parent 2e4171e commit 0a48aee
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Sources/RTMP/RTMPStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ open class RTMPStream: IOStream {
private var videoWasSent = false
private var pausedStatus = PausedStatus(hasAudio: false, hasVideo: false)
private var howToPublish: RTMPStream.HowToPublish = .live
private var dataTimeStamps: [String: Date] = .init()
private var dataTimestamps: [String: Date] = .init()
private weak var connection: RTMPConnection?

/// Creates a new stream.
Expand Down Expand Up @@ -366,13 +366,19 @@ open class RTMPStream: IOStream {
}

/// Sends a message on a published stream to all subscribing clients.
public func send(handlerName: String, arguments: Any?...) {
/// @param handlerName
/// @param arguemnts
/// @param isResetTimestamp A workaround option for sending timestamps as 0 in some services.
public func send(handlerName: String, arguments: Any?..., isResetTimestamp: Bool = false) {
lockQueue.async {
guard let connection = self.connection, self.readyState == .publishing(muxer: self.muxer) else {
return
}
let dataWasSent = self.dataTimeStamps[handlerName] == nil ? false : true
let timestmap: UInt32 = dataWasSent ? UInt32((self.dataTimeStamps[handlerName]?.timeIntervalSinceNow ?? 0) * -1000) : UInt32(self.startedAt.timeIntervalSinceNow * -1000)
if isResetTimestamp {
self.dataTimestamps[handlerName] = nil
}
let dataWasSent = self.dataTimestamps[handlerName] == nil ? false : true
let timestmap: UInt32 = dataWasSent ? UInt32((self.dataTimestamps[handlerName]?.timeIntervalSinceNow ?? 0) * -1000) : UInt32(self.startedAt.timeIntervalSinceNow * -1000)
let chunk = RTMPChunk(
type: dataWasSent ? RTMPChunkType.one : RTMPChunkType.zero,
streamId: RTMPChunk.StreamID.data.rawValue,
Expand All @@ -384,7 +390,7 @@ open class RTMPStream: IOStream {
arguments: arguments
))
let length = connection.socket?.doOutput(chunk: chunk) ?? 0
self.dataTimeStamps[handlerName] = .init()
self.dataTimestamps[handlerName] = .init()
self.info.byteCount.mutate { $0 += Int64(length) }
}
}
Expand Down Expand Up @@ -463,7 +469,7 @@ open class RTMPStream: IOStream {
startedAt = .init()
videoWasSent = false
audioWasSent = false
dataTimeStamps.removeAll()
dataTimestamps.removeAll()
FCPublish()
case .publishing:
let metadata = makeMetaData()
Expand Down

0 comments on commit 0a48aee

Please sign in to comment.