add FLVReader for debugging

This commit is contained in:
shogo4405 2017-05-15 00:00:58 +09:00
parent f25ed65cc3
commit 4ed4eebc2d
7 changed files with 100 additions and 13 deletions

View File

@ -518,6 +518,12 @@ final class MP4EditListBox: MP4Box {
final class MP4Reader: MP4ContainerBox {
private(set) var url:URL
var isEmpty:Bool {
return getBoxes(byName: "mdhd").isEmpty
}
private var fileHandle:FileHandle? = nil
init(url:URL) {
do {
self.url = url
@ -528,12 +534,6 @@ final class MP4Reader: MP4ContainerBox {
}
}
var isEmpty:Bool {
return getBoxes(byName: "mdhd").isEmpty
}
private var fileHandle:FileHandle? = nil
func seek(toFileOffset: UInt64) {
return fileHandle!.seek(toFileOffset: toFileOffset)
}

View File

@ -202,6 +202,20 @@ struct FLVTag {
var timestamp:UInt32 = 0
var timestampExtended:UInt8 = 0
var streamId:UInt32 = 0
init?(data:Data) {
let buffer:ByteArray = ByteArray(data: data)
do {
tagType = FLVTagType(rawValue: try buffer.readUInt8()) ?? .data
dataSize = try buffer.readUInt24()
timestamp = try buffer.readUInt24()
timestampExtended = try buffer.readUInt8()
streamId = try buffer.readUInt24()
buffer.clear()
} catch {
return nil
}
}
}
extension FLVTag: CustomStringConvertible {
@ -211,3 +225,38 @@ extension FLVTag: CustomStringConvertible {
}
}
final class FLVReader {
static let header:Data = Data([0x46, 0x4C, 0x56, 1])
private(set) var url:URL
private(set) var hasAudio:Bool = false
private(set) var hasVideo:Bool = false
fileprivate var currentOffSet:UInt64 = 0
fileprivate var fileHandle:FileHandle? = nil
init(url:URL) {
do {
self.url = url
fileHandle = try FileHandle(forReadingFrom: url)
fileHandle?.seek(toFileOffset: 13)
currentOffSet = 13
} catch let error as NSError {
logger.error("\(error)")
}
}
}
extension FLVReader: IteratorProtocol {
func next() -> FLVTag? {
guard let fileHandle:FileHandle = fileHandle else {
return nil
}
let data:Data = fileHandle.readData(ofLength: FLVTag.headerSize)
guard let tag:FLVTag = FLVTag(data: data) else {
return nil
}
currentOffSet += UInt64(FLVTag.headerSize) + UInt64(tag.dataSize) + 4
fileHandle.seek(toFileOffset: currentOffSet)
return tag
}
}

5
Tests/Config.swift Normal file
View File

@ -0,0 +1,5 @@
import Foundation
struct Config {
static let enabledTimerTest:Bool = false
}

View File

@ -4,7 +4,10 @@ import XCTest
@testable import lf
final class MP4SamplerTests: XCTestCase {
func main() {
func testMain() {
guard Config.enabledTimerTest else {
return
}
let bundle:Bundle = Bundle(for: type(of: self))
let url:URL = URL(fileURLWithPath: bundle.path(forResource: "SampleVideo_360x240_5mb", ofType: "mp4")!)
let sampler:MP4Sampler = MP4Sampler()

View File

@ -0,0 +1,18 @@
import Foundation
import XCTest
@testable import lf
final class RTMPReaderTests: XCTestCase {
func testReader() {
let bundle:Bundle = Bundle(for: type(of: self))
let url:URL = URL(fileURLWithPath: bundle.path(forResource: "SampleVideo_360x240_5mb", ofType: "flv")!)
let reader = FLVReader(url: url)
while true {
guard let tag = reader.next() else {
return
}
print(tag)
}
}
}

Binary file not shown.

View File

@ -65,6 +65,9 @@
2942424D1CF4C01300D65DCB /* MD5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2942424C1CF4C01300D65DCB /* MD5.swift */; };
2942EF841DFF4D06008E620C /* lf.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2945CBBD1B4BE66000104112 /* lf.framework */; };
2942EF861DFF4D3C008E620C /* lf.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 2945CBBD1B4BE66000104112 /* lf.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
294637A41EC8961C008EEC71 /* RTMPReaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 294637A31EC8961C008EEC71 /* RTMPReaderTests.swift */; };
294637A81EC89BC9008EEC71 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = 294637A71EC89BC9008EEC71 /* Config.swift */; };
294637AA1EC8A79F008EEC71 /* SampleVideo_360x240_5mb.flv in Resources */ = {isa = PBXBuildFile; fileRef = 294637A91EC8A79F008EEC71 /* SampleVideo_360x240_5mb.flv */; };
294852571D852499002DE492 /* RTMPTSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 294852551D84BFAD002DE492 /* RTMPTSocket.swift */; };
295074301E4620FF007F15A4 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 29205CBD1E461F4E009D3FFF /* Main.storyboard */; };
295074311E462105007F15A4 /* PreferenceViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2950742E1E4620B7007F15A4 /* PreferenceViewController.swift */; };
@ -147,7 +150,7 @@
29B876B01CD70B2800FC07DA /* RTMPConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A41CD70B2800FC07DA /* RTMPConnection.swift */; };
29B876B11CD70B2800FC07DA /* RTMPMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A51CD70B2800FC07DA /* RTMPMessage.swift */; };
29B876B21CD70B2800FC07DA /* RTMPMuxer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A61CD70B2800FC07DA /* RTMPMuxer.swift */; };
29B876B31CD70B2800FC07DA /* FLVTag.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A71CD70B2800FC07DA /* FLVTag.swift */; };
29B876B31CD70B2800FC07DA /* FLVReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A71CD70B2800FC07DA /* FLVReader.swift */; };
29B876B41CD70B2800FC07DA /* RTMPSharedObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A81CD70B2800FC07DA /* RTMPSharedObject.swift */; };
29B876B51CD70B2800FC07DA /* RTMPSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A91CD70B2800FC07DA /* RTMPSocket.swift */; };
29B876B61CD70B2800FC07DA /* RTMPStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876AA1CD70B2800FC07DA /* RTMPStream.swift */; };
@ -187,7 +190,7 @@
29B877131CD70D5A00FC07DA /* RTMPConnection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A41CD70B2800FC07DA /* RTMPConnection.swift */; };
29B877141CD70D5A00FC07DA /* RTMPMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A51CD70B2800FC07DA /* RTMPMessage.swift */; };
29B877151CD70D5A00FC07DA /* RTMPMuxer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A61CD70B2800FC07DA /* RTMPMuxer.swift */; };
29B877161CD70D5A00FC07DA /* FLVTag.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A71CD70B2800FC07DA /* FLVTag.swift */; };
29B877161CD70D5A00FC07DA /* FLVReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A71CD70B2800FC07DA /* FLVReader.swift */; };
29B877171CD70D5A00FC07DA /* RTMPSharedObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A81CD70B2800FC07DA /* RTMPSharedObject.swift */; };
29B877181CD70D5A00FC07DA /* RTMPSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876A91CD70B2800FC07DA /* RTMPSocket.swift */; };
29B877191CD70D5A00FC07DA /* RTMPStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29B876AA1CD70B2800FC07DA /* RTMPStream.swift */; };
@ -377,6 +380,9 @@
293C74361D85D56D001ED43C /* MainInterface.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = MainInterface.storyboard; path = Examples/iOS/ScreencastUI/Base.lproj/MainInterface.storyboard; sourceTree = "<group>"; };
2942424C1CF4C01300D65DCB /* MD5.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MD5.swift; path = Sources/Util/MD5.swift; sourceTree = SOURCE_ROOT; };
2945CBBD1B4BE66000104112 /* lf.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = lf.framework; sourceTree = BUILT_PRODUCTS_DIR; };
294637A31EC8961C008EEC71 /* RTMPReaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPReaderTests.swift; path = RTMP/RTMPReaderTests.swift; sourceTree = "<group>"; };
294637A71EC89BC9008EEC71 /* Config.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
294637A91EC8A79F008EEC71 /* SampleVideo_360x240_5mb.flv */ = {isa = PBXFileReference; lastKnownFileType = file; path = SampleVideo_360x240_5mb.flv; sourceTree = "<group>"; };
294852551D84BFAD002DE492 /* RTMPTSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPTSocket.swift; path = Sources/RTMP/RTMPTSocket.swift; sourceTree = SOURCE_ROOT; };
2950742E1E4620B7007F15A4 /* PreferenceViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PreferenceViewController.swift; path = Examples/iOS/PreferenceViewController.swift; sourceTree = "<group>"; };
2957473B1E34F30300EF056E /* RTMPBroadcaster.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPBroadcaster.swift; path = Examples/iOS/Screencast/RTMPBroadcaster.swift; sourceTree = "<group>"; };
@ -456,7 +462,7 @@
29B876A41CD70B2800FC07DA /* RTMPConnection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPConnection.swift; path = Sources/RTMP/RTMPConnection.swift; sourceTree = SOURCE_ROOT; };
29B876A51CD70B2800FC07DA /* RTMPMessage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPMessage.swift; path = Sources/RTMP/RTMPMessage.swift; sourceTree = SOURCE_ROOT; };
29B876A61CD70B2800FC07DA /* RTMPMuxer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPMuxer.swift; path = Sources/RTMP/RTMPMuxer.swift; sourceTree = SOURCE_ROOT; };
29B876A71CD70B2800FC07DA /* FLVTag.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FLVTag.swift; path = Sources/RTMP/FLVTag.swift; sourceTree = SOURCE_ROOT; };
29B876A71CD70B2800FC07DA /* FLVReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FLVReader.swift; path = Sources/RTMP/FLVReader.swift; sourceTree = SOURCE_ROOT; };
29B876A81CD70B2800FC07DA /* RTMPSharedObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPSharedObject.swift; path = Sources/RTMP/RTMPSharedObject.swift; sourceTree = SOURCE_ROOT; };
29B876A91CD70B2800FC07DA /* RTMPSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPSocket.swift; path = Sources/RTMP/RTMPSocket.swift; sourceTree = SOURCE_ROOT; };
29B876AA1CD70B2800FC07DA /* RTMPStream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = RTMPStream.swift; path = Sources/RTMP/RTMPStream.swift; sourceTree = SOURCE_ROOT; };
@ -612,6 +618,7 @@
290EA89D1DFB61B100053022 /* ASClassTests.swift */,
290EA89E1DFB61B100053022 /* RTMPChunkTests.swift */,
290686021DFDB7A6008EB7ED /* RTMPConnectionTests.swift */,
294637A31EC8961C008EEC71 /* RTMPReaderTests.swift */,
);
name = RTMP;
sourceTree = "<group>";
@ -644,6 +651,7 @@
children = (
29B876D71CD70CE700FC07DA /* SampleVideo_360x240_5mb */,
290686041DFDC19B008EB7ED /* SampleVideo_360x240_5mb-base.mp4 */,
294637A91EC8A79F008EEC71 /* SampleVideo_360x240_5mb.flv */,
29B876D81CD70CE700FC07DA /* SampleVideo_360x240_5mb.m3u8 */,
29B876D91CD70CE700FC07DA /* SampleVideo_360x240_5mb.mp4 */,
);
@ -803,6 +811,7 @@
29798E5A1CE60E5300F5CBD0 /* Tests */ = {
isa = PBXGroup;
children = (
294637A71EC89BC9008EEC71 /* Config.swift */,
291C2AD11CE9FF3E006F042B /* Asset */,
291C2AD21CE9FF48006F042B /* Core */,
291C2AD31CE9FF68006F042B /* HTTP */,
@ -923,7 +932,7 @@
29B8769F1CD70B2800FC07DA /* AMF0Serializer.swift */,
29B876A01CD70B2800FC07DA /* AMF3Serializer.swift */,
29B876A11CD70B2800FC07DA /* ASClass.swift */,
29B876A71CD70B2800FC07DA /* FLVTag.swift */,
29B876A71CD70B2800FC07DA /* FLVReader.swift */,
29B876A31CD70B2800FC07DA /* RTMPChunk.swift */,
29B876A41CD70B2800FC07DA /* RTMPConnection.swift */,
29F6F4841DFB83E200920A3A /* RTMPHandshake.swift */,
@ -1212,6 +1221,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
294637AA1EC8A79F008EEC71 /* SampleVideo_360x240_5mb.flv in Resources */,
29798E751CE614FE00F5CBD0 /* SampleVideo_360x240_5mb in Resources */,
29798E761CE614FE00F5CBD0 /* SampleVideo_360x240_5mb.m3u8 in Resources */,
290686051DFDC19B008EB7ED /* SampleVideo_360x240_5mb-base.mp4 in Resources */,
@ -1350,7 +1360,7 @@
2981B7F41D7345D5002FA821 /* SessionDescription.swift in Sources */,
2931204E1D4522E400B14211 /* RTSPResponse.swift in Sources */,
29B8765D1CD70A7900FC07DA /* H264Encoder.swift in Sources */,
29B876B31CD70B2800FC07DA /* FLVTag.swift in Sources */,
29B876B31CD70B2800FC07DA /* FLVReader.swift in Sources */,
29AF3FCF1D7C744C00E41212 /* NetStream.swift in Sources */,
29DD70E41D68CACA0021904A /* RTSPStream.swift in Sources */,
299B13271D3B751400A1E8F5 /* LFView.swift in Sources */,
@ -1421,8 +1431,10 @@
buildActionMask = 2147483647;
files = (
290EA89B1DFB619600053022 /* TSTests.swift in Sources */,
294637A41EC8961C008EEC71 /* RTMPReaderTests.swift in Sources */,
290EA8AD1DFB61E700053022 /* TimerDriverTests.swift in Sources */,
290EA8A91DFB61E700053022 /* ByteArrayTests.swift in Sources */,
294637A81EC89BC9008EEC71 /* Config.swift in Sources */,
290EA8981DFB619600053022 /* MP4SamplerTests.swift in Sources */,
290EA8A31DFB61C700053022 /* SessionDescriptionTests.swift in Sources */,
290EA8AC1DFB61E700053022 /* MD5Tests.swift in Sources */,
@ -1510,7 +1522,7 @@
29B877131CD70D5A00FC07DA /* RTMPConnection.swift in Sources */,
29B877141CD70D5A00FC07DA /* RTMPMessage.swift in Sources */,
29B877151CD70D5A00FC07DA /* RTMPMuxer.swift in Sources */,
29B877161CD70D5A00FC07DA /* FLVTag.swift in Sources */,
29B877161CD70D5A00FC07DA /* FLVReader.swift in Sources */,
2989C3B61D69C43000038653 /* RTPPacket.swift in Sources */,
29EA87E31E79A1E90043A5F8 /* CMVideoFormatDescription+Extension.swift in Sources */,
29B877171CD70D5A00FC07DA /* RTMPSharedObject.swift in Sources */,