Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from iaomw/#4
Browse files Browse the repository at this point in the history
Fixes the parsing logic of audio clip, remove unnecessary code in MediaOverlayNode
  • Loading branch information
aferditamuriqi authored Apr 13, 2018
2 parents 597dbd8 + 321fca9 commit b0b8442
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 58 deletions.
60 changes: 8 additions & 52 deletions r2-shared-swift/MediaOverlayNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public struct Clip {
/// End time in seconds.
public var end: Double!
/// Total clip duration in seconds (end - start).
@available(iOS, deprecated: 9.0, message: "Don't use it when the value is negative, because some information is missing in the original SMIL file. Try to get the duration from file system or APIs in Fetcher, then minus the start value.")
public var duration: Double!

public init() {}
}

/// The Error enumeration of the MediaOverlayNode class.
Expand All @@ -39,13 +42,15 @@ public enum MediaOverlayNodeError: Error {
/// Represents a MediaOverlay XML node.
public class MediaOverlayNode {
public var text: String?
public var audio: String?
public var clip: Clip?

public var role = [String]()
public var children = [MediaOverlayNode]()

public init(_ text: String? = nil, audio: String? = nil) {
public init(_ text: String? = nil, clip: Clip? = nil) {
self.text = text
self.audio = audio
self.clip = clip
self.clip?.fragmentId = self.fragmentId()
}

// Mark: - Internal Methods.
Expand All @@ -59,53 +64,4 @@ public class MediaOverlayNode {
}
return text.components(separatedBy: "#").last
}

/// Generate a `Clip` from self.
///
/// - Returns: The generated `Clip`.
/// - Throws: `MediaOverlayNodeError.audio`,
/// `MediaOverlayNodeError.timersParsing`.
public func clip() throws -> Clip {
var newClip = Clip()

// Retrieve the audioString (containing timers + audiofile url), then
// retrieve both.
guard let audioString = self.audio,
let audioFileString = audioString.components(separatedBy: "#").first,
let audioFileUrl = URL(string: audioFileString) else
{
throw MediaOverlayNodeError.audio
}
// Relative audio file URL.
newClip.relativeUrl = audioFileUrl
guard let times = audioString.components(separatedBy: "#").last else {
throw MediaOverlayNodeError.timersParsing
}
try parseTimer(times, into: &newClip)
newClip.fragmentId = fragmentId()
return newClip
}

/// Parse the time String to fill `clip`.
///
/// - Parameters:
/// - times: The time string ("t=S.MS,S.MS")
/// - clip: The Clip instance where to fill the parsed data.
/// - Throws: `MediaOverlayNodeError.timersParsing`.
fileprivate func parseTimer(_ times: String, into clip: inout Clip) throws {
var times = times
// Remove "t=" prefix from times string.
times = times.substring(from: times.index(times.startIndex, offsetBy: 2))
// Parse start and end times.
guard let start = times.components(separatedBy: ",").first,
let end = times.components(separatedBy: ",").last,
let startTimer = Double(start),
let endTimer = Double(end) else
{
throw MediaOverlayNodeError.timersParsing
}
clip.start = startTimer
clip.end = endTimer
clip.duration = endTimer - startTimer
}
}
12 changes: 6 additions & 6 deletions r2-shared-swift/MediaOverlays.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public class MediaOverlays {
/// - Returns: The `Clip`, representation of the associated SMIL element.
/// - Throws: `MediaOverlayNodeError.audio`,
/// `MediaOverlayNodeError.timersParsing`.
public func clip(forFragmentId id: String) throws -> Clip {
let clip: Clip
public func clip(forFragmentId id: String) throws -> Clip? {
let clip: Clip?

do {
let fragmentNode = try node(forFragmentId: id)

clip = try fragmentNode.clip()
clip = fragmentNode.clip
}
return clip
}
Expand All @@ -68,12 +68,12 @@ public class MediaOverlays {
/// one designated by `id`.
/// - Throws: `MediaOverlayNodeError.audio`,
/// `MediaOverlayNodeError.timersParsing`.
public func clip(nextAfterFragmentId id: String) throws -> Clip {
let clip: Clip
public func clip(nextAfterFragmentId id: String) throws -> Clip? {
let clip: Clip?

do {
let fragmentNextNode = try node(nextAfterFragmentId: id)
clip = try fragmentNextNode.clip()
clip = fragmentNextNode.clip
}
return clip
}
Expand Down

0 comments on commit b0b8442

Please sign in to comment.