Skip to content

Commit

Permalink
Fix preview (#127)
Browse files Browse the repository at this point in the history
* Fix preview

* Format

* Fix

* Fix

* Fix

* Fix

* Delete swiftlint.yaml
  • Loading branch information
umireon authored Jul 24, 2024
1 parent 97d4ccb commit 033cb51
Show file tree
Hide file tree
Showing 16 changed files with 809 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/scripts/check-swift-format.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
dirs=(
Packages
FragmentedRecordWriter
FragmentedRecordWriterTests
Recoreon
Expand Down
24 changes: 0 additions & 24 deletions .github/workflows/swiftlint.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion FragmentedRecordWriter/MasterPlaylistWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct MasterPlaylistWriter {
\(videoIndexURL.lastPathComponent)
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="App",DEFAULT=YES,URI="\(appAudioIndexURL.lastPathComponent)"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="Mic",DEFAULT=NO,URI="\(micAudioIndexURL.lastPathComponent)"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="Mic",DEFAULT=NO,URI="\(micAudioIndexURL.lastPathComponent)"\n
"""

try masterPlaylistContent.write(to: masterPlaylistURL, atomically: true, encoding: .utf8)
Expand Down
8 changes: 8 additions & 0 deletions Packages/HLSServer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
32 changes: 32 additions & 0 deletions Packages/HLSServer/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "HLSServer",
platforms: [.iOS(.v17)],
products: [
.library(
name: "HLSServer",
targets: ["HLSServer"]
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", exact: "2.68.0")
],
targets: [
.target(
name: "HLSServer",
dependencies: [
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "NIOHTTP1", package: "swift-nio"),
.product(name: "NIOPosix", package: "swift-nio"),
]
),
.testTarget(
name: "HLSServerTests",
dependencies: ["HLSServer"]
),
]
)
66 changes: 66 additions & 0 deletions Packages/HLSServer/Sources/HLSServer/HLSServer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Derived from https://github.com/apple/swift-nio/blob/main/Sources/NIOHTTP1Server/main.swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Foundation
import NIOCore
import NIOHTTP1
import NIOPosix

enum HLSServerError: Error {
case localAddressPortNotAvailable
}

public struct HLSServer {
public let port: Int

private let channel: Channel

public init(htdocs: String, host: String, port: Int = 0) throws {
let fileIO = NonBlockingFileIO(threadPool: .singleton)

func childChannelInitializer(channel: Channel) -> EventLoopFuture<Void> {
channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true).flatMap {
channel.pipeline.addHandler(HTTPHandler(fileIO: fileIO, htdocsPath: htdocs))
}
}

let socketBootstrap = ServerBootstrap(group: MultiThreadedEventLoopGroup.singleton)
// Specify backlog and enable SO_REUSEADDR for the server itself
.serverChannelOption(ChannelOptions.backlog, value: 256)
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)

// Set the handlers that are applied to the accepted Channels
.childChannelInitializer(childChannelInitializer(channel:))

// Enable SO_REUSEADDR for the accepted Channels
.childChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
.childChannelOption(ChannelOptions.maxMessagesPerRead, value: 1)
.childChannelOption(ChannelOptions.allowRemoteHalfClosure, value: true)

channel = try socketBootstrap.bind(host: host, port: port).wait()

guard let port = channel.localAddress?.port else {
throw HLSServerError.localAddressPortNotAvailable
}

self.port = port
}

public func close() async throws {
try await channel.eventLoop.shutdownGracefully()
}

}
Loading

0 comments on commit 033cb51

Please sign in to comment.