Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class AVSubtitlesLoader: NSObject {
let timestamp: SSTextTrackDescription.WebVttTimestamp? = (trackDescription as? SSTextTrackDescription)?.vttTimestamp
let autosync: Bool? = (trackDescription as? SSTextTrackDescription)?.automaticTimestampSyncEnabled
let subtitlesMediaURL: String
if (timestamp?.localTime == nil && timestamp?.pts == nil && format == .WebVTT && autosync == nil) {
if (timestamp?.localTime == nil && timestamp?.pts == nil && format == .WebVTT && autosync != true) {
subtitlesMediaURL = originalURL.absoluteString
} else {
subtitlesMediaURL = self.transformer.composeTranformationUrl(with: originalURL.absoluteString, format: format, timestamp: timestamp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AVFoundation
@_spi(WebVTT) import THEOplayerSDK

protocol SubtitlesSynchronizerDelegate: AnyObject {
func didUpdateTimestamp(timestamp: SSTextTrackDescription.WebVttTimestamp)
func didUpdateTimestamp(timestamp: SSTextTrackDescription.WebVttTimestamp, forContentUrl contentUrl: String)
}

class SubtitlesSynchronizer {
Expand Down Expand Up @@ -79,7 +79,7 @@ class SubtitlesSynchronizer {

let pts: String = .init(delta * 90000)
let localTime: String = textTrackDescription.vttTimestamp.localTime ?? "00:00:00.000"
self?.delegate?.didUpdateTimestamp(timestamp: .init(pts: pts, localTime: localTime))
self?.delegate?.didUpdateTimestamp(timestamp: .init(pts: pts, localTime: localTime), forContentUrl: textTrackDescription.src.absoluteString)

welf.trackSyncMap[textTrack.label]?.status = .resolving
welf.trackSyncMap[textTrack.label]?.mode = mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SubtitlesTransformer {
private static let PORT_RANGE: Range<in_port_t> = 8000..<49151
private var retryAttemptsLeft: Int = 9
private var port: in_port_t
private var parameters: Parameters?
private var parametersMap: [String: Parameters] = [:]

init() {
self.port = .random(in: Self.PORT_RANGE)
Expand All @@ -39,16 +39,23 @@ class SubtitlesTransformer {
}

func composeTranformationUrl(with subtitlesURL: String, format: THEOplayerSDK.TextTrackFormat, timestamp: SSTextTrackDescription.WebVttTimestamp?) -> String {
self.parameters = Parameters(contentUrl: subtitlesURL, format: format, timestamp: timestamp)
let urlComps = URLComponents(string: "http://\(self.host):\(self.port)/\(SubtitlesTransformer.TRANSFORM_ROUTE)")!
self.parametersMap[subtitlesURL] = Parameters(contentUrl: subtitlesURL, format: format, timestamp: timestamp)
var urlComps = URLComponents(string: "http://\(self.host):\(self.port)/\(SubtitlesTransformer.TRANSFORM_ROUTE)")!
urlComps.queryItems = [URLQueryItem(name: "url", value: subtitlesURL)]
Comment thread
therama marked this conversation as resolved.
return urlComps.url?.absoluteString ?? subtitlesURL
}

private func setupServerRoutes() {
let handler: (HttpRequest) -> HttpResponse = { req in
// Always return with HttpResponse.ok to fail gracefully. Otherwise player will stall.
guard let contentURLString: String = self.parameters?.contentUrl,
let decodedContentUrlString: String = contentURLString.removingPercentEncoding,
guard let subtitlesURLParam: String = req.queryParams.first(where: { $0.0 == "url" })?.1,
let parameters: Parameters = self.parametersMap[subtitlesURLParam.removingPercentEncoding ?? subtitlesURLParam] else {
let errorMessage: String = "Missing subtitle content URL."
print("[AVSubtitlesLoader] ERROR: \(errorMessage)")
return HttpResponse.ok(.text(errorMessage))
}

guard let decodedContentUrlString: String = parameters.contentUrl.removingPercentEncoding,
let contentUrl: URL = URL(string: decodedContentUrlString) else {
let errorMessage: String = "Missing subtitle content URL."
print("[AVSubtitlesLoader] ERROR: \(errorMessage)")
Expand All @@ -70,7 +77,7 @@ class SubtitlesTransformer {
var contentString: String = _contentString.replacingOccurrences(of: "\r\n", with: "\n")

// SRT to VTT
if self.parameters?.format == THEOplayerSDK.TextTrackFormat.SRT {
if parameters.format == THEOplayerSDK.TextTrackFormat.SRT {
do {
let subtitles: Subtitles = try Subtitles.Coder.SRT().decode(contentString)
contentString = try Subtitles.Coder.VTT().encode(subtitles: subtitles)
Expand All @@ -81,8 +88,8 @@ class SubtitlesTransformer {
}

// Add/replace VTT timestamp
if let timestampPts: String = self.parameters?.timestamp?.pts,
let timestampLocalTime: String = self.parameters?.timestamp?.localTime {
if let timestampPts: String = parameters.timestamp?.pts,
let timestampLocalTime: String = parameters.timestamp?.localTime {
// if timestamp exists replace it, else add
if let modifiedString: String = TimestampStringUtils.overrideTimestamp(in: contentString, with: (timestampPts, timestampLocalTime)) {
contentString = modifiedString
Expand Down Expand Up @@ -118,7 +125,7 @@ class SubtitlesTransformer {
}

private func reset() {
self.parameters = nil
self.parametersMap.removeAll()
}

deinit {
Expand All @@ -128,7 +135,7 @@ class SubtitlesTransformer {
}

extension SubtitlesTransformer: SubtitlesSynchronizerDelegate {
func didUpdateTimestamp(timestamp: SSTextTrackDescription.WebVttTimestamp) {
self.parameters?.timestamp = timestamp
func didUpdateTimestamp(timestamp: SSTextTrackDescription.WebVttTimestamp, forContentUrl contentUrl: String) {
self.parametersMap[contentUrl]?.timestamp = timestamp
}
}
Loading