HaishinKit 1.9.3

HaishinKit 1.9.3

测试已测试
语言语言 SwiftSwift
许可证 BSD-3-Clause
发布最后发布2024 年 8 月
SPM支持 SPM

shogo4405 维护。



  • 作者:
  • shogo4405

HaishinKit for iOS, macOS, tvOS, and Android.

GitHub Stars Release Platform Compatibility Swift Compatibility GitHub license

  • 适用于 iOS、macOS、tvOS 的 RTMP 和 HLS 网络 Camera 和 Microphone 流媒体库。
  • README.md 包含未发布内容,可以在主要分支上测试。
  • API 文档

赞助💖
Stream Chat
用于流媒体和聊天的企业级 API。 尝试 iOS 聊天教程💬

💬交流

  • 如果您需要帮助使用 HaishinKit 进行 LiveStreaming 请求,请在带有 Q&AGitHub 讨论中寻求帮助。
  • 如果您想讨论新功能请求,请在带有 IdeaGitHub 讨论中进行。
  • 如果您遇到 HaishinKit 的错误🐛,请在带有 Bug report templateGitHub Issue 中报告。
    • 跟踪级别的日志非常有用。请设置 LBLogger.with(HaishinKitIdentifier).level = .trace
    • 如果您不使用问题模板,我将立即无评论地关闭您的问题。
  • 如果您 想要贡献,请提交一个 pull request!
  • 如果您想支持无 GitHub 的电子邮件Based通信。
    • 咨询服务费为 $50/1 incident。我可以响应几天。
  • Discord 聊天室.
  • 如果懂日语,请用日语交流!

💖赞助商

Streamlabs

🌏相关项目

项目名称 说明 许可证
SRTHaishinKit for iOS。 通过 SRT 的 Camera 和 Microphone 流媒体库。 BSD 3-Clause "New" 或 "Revised" 许可证
HaishinKit for Android。 适用于 Android 的 RTMP Camera 和 Microphone 流媒体库。 BSD 3-Clause "New" 或 "Revised" 许可证
HaishinKit for Flutter。 适用于 Flutter 的 RTMP Camera 和 Microphone 流媒体库。 BSD 3-Clause "New" 或 "Revised" 许可证

🎨特性

RTMP

  • 身份验证
  • 发布与录制
  • 播放(测试版)
  • 自适应码率流
    • 处理(另见 #1153
  • 操作消息格式
    • AMF0
    • AMF3
  • 共享对象
  • RTMPS
    • 本地(RTMP over SSL/TLS)
    • 隧道(RTMPT over SSL/TLS)(技术预览)
  • RTMPT(技术预览)
  • ReplayKit 直播作为广播上传扩展
  • 支持的编解码器
    • 音频
      • AAC
    • 视频
      • H264/AVC
        • 例如:stream.videoSettings.profileLevel = kVTProfileLevel_H264_Baseline_3_1 as String
      • H265/HEVC(需要服务器端支持。详见 链接
        • 例如:stream.videoSettings.profileLevel = kVTProfileLevel_HEVC_Main_AutoLevel as String

HLS

  • HTTPService
  • HLS 发布

多摄像头

支持两个摄像头视频源。主摄像头的子摄像头图像的画中画显示。支持水平垂直分屏显示。

画中画 分裂
let back = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
stream.attachCamera(back)

if #available(iOS 13.0, *) {
  let front = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
  stream.attachMultiCamera(front)
}

渲染

特性 HKView PIPHKView MTHKView
引擎 AVCaptureVideoPreviewLayer AVSampleBufferDisplayLayer Metal
发布
播放
视觉效果
画中画

多摄像头

其他

🌏需求

- iOS OSX tvOS Xcode Swift
1.5.0+ 11.0+ 10.13+ 10.2+ 14.3+ 5.7+
1.4.0+ 11.0+ 10.13+ 10.2+ 14.0+ 5.7+

🐾示例

示例项目适用于 iOS(UIKit)、iOS(SwiftUI)、macOS 和 tvOS。

  • 相机和麦克风发布。
  • RTMP 播放
git clone https://github.com/shogo4405/HaishinKit.swift.git
cd HaishinKit.swift
carthage bootstrap --use-xcframeworks
open HaishinKit.xcodeproj

Cocoa 键值

请包含 Info.plist。

iOS 10.0+

  • NSMicrophoneUsageDescription
  • NSCameraUsageDescription

macOS 10.14+

  • NSMicrophoneUsageDescription
  • NSCameraUsageDescription

🔧安装

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

def import_pods
    pod 'HaishinKit', '~> 1.5.4
end

target 'Your Target'  do
    platform :ios, '11.0'
    import_pods
end

Carthage

github "shogo4405/HaishinKit.swift" ~> 1.5.4

Swift 包管理器

https://github.com/shogo4405/HaishinKit.swift

🔧准备条件

确保您已设置并激活了您的 AVAudioSession iOS。

import AVFoundation
let session = AVAudioSession.sharedInstance()
do {
    try session.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])
    try session.setActive(true)
} catch {
    print(error)
}

📓RTMP 使用

实时消息协议(RTMP)。

let connection = RTMPConnection()
let stream = RTMPStream(connection: rtmpConnection)
stream.attachAudio(AVCaptureDevice.default(for: .audio)) { error in
    // print(error)
}
stream.attachCamera(AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)) { error in
    // print(error)
}

let hkView = MTHKView(frame: view.bounds)
hkView.videoGravity = AVLayerVideoGravity.resizeAspectFill
hkView.attachStream(stream)

// add ViewController#view
view.addSubview(hkView)

connection.connect("rtmp:///appName/instanceName")
stream.publish("streamName")

RTMP URL 格式

  • rtmp://服务器IP地址[:端口]/应用程序/[appInstance]/[前置:[路径1[/路径2/]]]流名称
    • [] 标记为可选。
    rtmpConneciton.connect("rtmp://server-ip-address[:port]/application/[appInstance]")
    rtmpStream.publish("[prefix:[path1[/path2/]]]streamName")
    
  • rtmp:///live/streamName
    rtmpConneciton.connect("rtmp:///live")
    rtmpStream.publish("streamName")
    

设置

var stream = RTMPStream(connection: rtmpConnection)

stream.frameRate = 30
stream.sessionPreset = AVCaptureSession.Preset.medium

/// Specifies the video capture settings.
stream.videoCapture(for: 0).isVideoMirrored = false
stream.videoCapture(for: 0).preferredVideoStabilizationMode = .auto
// rtmpStream.videoCapture(for: 1).isVideoMirrored = false

// Specifies the audio codec settings.
stream.audioSettings = AudioCodecSettings(
  bitRate: 64 * 1000
)

// Specifies the video codec settings.
stream.videoSettings = VideoCodecSettings(
  videoSize: .init(width: 854, height: 480),
  profileLevel: kVTProfileLevel_H264_Baseline_3_1 as String,
  bitRate: 640 * 1000,
  maxKeyFrameIntervalDuration: 2,
  scalingMode: .trim,
  bitRateMode: .average,
  allowFrameReordering: nil,
  isHardwareEncoderEnabled: true
)

// Specifies the recording settings. 0" means the same of input.
stream.startRecording([
  AVMediaType.audio: [
    AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
    AVSampleRateKey: 0,
    AVNumberOfChannelsKey: 0,
    // AVEncoderBitRateKey: 128000,
  ],
  AVMediaType.video: [
    AVVideoCodecKey: AVVideoCodecH264,
    AVVideoHeightKey: 0,
    AVVideoWidthKey: 0,
    /*
    AVVideoCompressionPropertiesKey: [
      AVVideoMaxKeyFrameIntervalDurationKey: 2,
      AVVideoProfileLevelKey: AVVideoProfileLevelH264Baseline30,
      AVVideoAverageBitRateKey: 512000
    ]
    */
  ]
])

// 2nd arguemnt set false
stream.attachAudio(AVCaptureDevice.default(for: .audio), automaticallyConfiguresApplicationAudioSession: false)
// picrure in picrure settings.
stream.multiCamCaptureSettings = MultiCamCaptureSetting(
  mode: .pip,
  cornerRadius: 16.0,
  regionOfInterest: .init(
    origin: CGPoint(x: 16, y: 16),
    size: .init(width: 160, height: 160)
  )
)
// split settings.
stream.multiCamCaptureSettings = MultiCamCaptureSetting(
  mode: .split(direction: .east),
  cornerRadius: 0.0,
  regionOfInterest: .init(
    origin: .zero,
    size: .zero
  )
)

认证

var connection = RTMPConnection()
connection.connect("rtmp://username:password@localhost/appName/instanceName")

屏幕捕获

// iOS
let screen = IOUIScreenCaptureUnit(shared: UIApplication.shared)
screen.delegate = stream
screen.startRunning()

// macOS
stream.attachScreen(AVCaptureScreenInput(displayID: CGMainDisplayID()))

📓HTTP 使用

HTTP 实时流媒体 (HLS)。您的 iPhone/Mac 变成 IP 摄像头。基本示例。您可以查看 http://ip.address:8080/hello/playlist.m3u8

var stream = HTTPStream()
stream.attachCamera(AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back))
stream.attachAudio(AVCaptureDevice.default(for: .audio))
stream.publish("hello")

var hkView = MTHKView(frame: view.bounds)
hkView.attachStream(httpStream)

var httpService = HLSService(domain: "", type: "_http._tcp", name: "HaishinKit", port: 8080)
httpService.addHTTPStream(stream)
httpService.startRunning()

// add ViewController#view
view.addSubview(hkView)

💠赞助

正在寻找赞助商。赞助我们将能够

  • 购买智能手机或外围设备进行测试。
  • 为特定的流媒体服务付费测试或在移动线路上进行测试。
  • 可能用于私人用途,以继续 OSS 开发

如果您在工作中使用我们的任何库,请看您的雇主是否对赞助感兴趣。我有一些特别的优惠。我会非常感激。谢谢。

  • 如果您请求,我将在我们的 README 中记下您的名字和产品。
  • 如果您在讨论、问题或拉取请求中提到您在赞助我们,我将优先更高的帮助您。

正在募集赞助商。用途包括:

  • 本测试目的中,将进行智能手机及其周边设备的购买。
  • 将用于对特定流媒体服务的测试支付,或移动网络测试的支付。
  • 也有可能用于持续进行所著作品的开源软件(OSS)开发。

如果您在工作中持续使用这个库,请务必询问您的雇主或赞助者是否有兴趣。我们已准备好了一些特别的好处。

  • 在README.md文件中展示企业logo
  • 优先处理问题(Issue)或请求(Pull Request)

赞助

📖参考文献

📜许可证

BSD-3-Clause