项目作者: shogo4405

项目描述 :
Camera and Microphone streaming library via RTMP, HLS for iOS, macOS, tvOS.
高级语言: Swift
项目地址: git://github.com/shogo4405/HaishinKit.swift.git
创建时间: 2015-07-07T10:35:08Z
项目社区:https://github.com/shogo4405/HaishinKit.swift

开源协议:BSD 3-Clause "New" or "Revised" License

下载


HaishinKit for iOS, macOS, tvOS, visionOS and Android.

GitHub Stars
Release
Platform Compatibility
Swift Compatibility
GitHub license
GitHub Sponsor

  • Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.
  • README.md contains unreleased content, which can be tested on the main branch.
  • API Documentation

💖 Sponsors

Do you need additional support? Technical support on Issues and Discussions is provided only to contributors and academic researchers of HaishinKit. By becoming a sponsor, we can provide the support you need.

Sponsor: $50 per month: Technical support via GitHub Issues/Discussions with priority response.

💬 Communication

  • GitHub Issues and Discussions are open spaces for communication among users and are available to everyone as long as the code of conduct is followed.
  • Whether someone is a contributor to HaishinKit is mainly determined by their GitHub profile icon. If you are using the default icon, there is a chance your input might be overlooked, so please consider setting a custom one. It could be a picture of your pet, for example. Personally, I like cats.
  • If you want to support e-mail based communication without GitHub.
    • Consulting fee is $50/1 incident. I’m able to response a few days.
Project name Notes License
HaishinKit for Android. Camera and Microphone streaming library via RTMP for Android. BSD 3-Clause “New” or “Revised” License
HaishinKit for Flutter. Camera and Microphone streaming library via RTMP for Flutter. BSD 3-Clause “New” or “Revised” License

🎨 Features

✏️ RTMP

  • FMLE-compatible Authentication
  • Ingest
    • H264, HEVC, AAC and OPUS support.
  • Playback (Beta)
    • H264, HEVC and AAC support.
  • Action Message Format
    • AMF0
    • AMF3
  • SharedObject
  • RTMPS
    • Native (RTMP over SSL/TLS)
  • Enhanced RTMP

✏️ SRT(beta)

  • Ingest
    • H264, HEVC and AAC support.
  • Playback
    • H264, HEVC and AAC support.
  • SRT Mode
    • caller
    • listener
    • rendezvous

✏️ MOQT(alpha)

[!NOTE]
I am working on a preliminary implementation of MOQT for research purposes. If you’re interested, please check out the repository.

📹 Multi Streaming.

Starting from version 2.0.0, multiple streams are supported, allowing live streaming to separate services. Views also support this, enabling the verification of raw video data

  1. let mixer = MediaMixer()
  2. let stream0 = RTMPStream() // for Y Service.
  3. let stream1 = RTMPStream() // for F Service.
  4. let view = MTHKView()
  5. view.track = 0 // Video Track Number 0 or 1, UInt8.max.
  6. mixer.addOutput(stream0)
  7. mixer.addOutput(stream1)
  8. mixer.addOutput(view)
  9. let view2 = MTHKView()
  10. stream0.addOutput(view2)

Offscreen Rendering.

Through off-screen rendering capabilities, it is possible to display any text or bitmap on a video during broadcasting or viewing. This allows for various applications such as watermarking and time display.
|Ingest|Playback|
|:—-:|:—-:|
|||

Rendering

Features PiPHKView MTHKView
Engine AVSampleBufferDisplayLayer Metal
Publish
Playback
VisualEffect
MultiCamera
PictureInPicture

Others

🐾 Examples

Examples project are available for iOS, macOS(ARM), tvOS and visionOS.

[!IMPORTANT]
Please check if the same issue occurs with the latest Examples before posting it on GitHub Issues.

Usage

You can verify by changing the URL of the following file.
https://github.com/HaishinKit/HaishinKit.swift/blob/abf1883d25d0ba29e1d1d67ea9e3a3b5be61a196/Examples/Preference.swift#L1-L7

  1. git clone https://github.com/HaishinKit/HaishinKit.swift.git
  2. cd HaishinKit.swift
  3. open Examples/Examples.xcodeproj

🌏 Requirements

Development

Version Xcode Swift
2.0.0+ 16.0+ 5.10+
1.9.0+ 15.4+ 5.10+

OS

- iOS tvOS macOS visionOS watchOS
HaishinKit 13.0+ 13.0+ 10.15+ 1.0+ -
SRTHaishinKit 13.0+ 13.0+ 10.15+ 1.0+ -

Cocoa Keys

Please contains Info.plist.

iOS 10.0+

  • NSMicrophoneUsageDescription
  • NSCameraUsageDescription

macOS 10.14+

  • NSMicrophoneUsageDescription
  • NSCameraUsageDescription

tvOS 17.0+

  • NSMicrophoneUsageDescription
  • NSCameraUsageDescription

🔧 Installation

📓 Settings

🔧 Prerequisites

Make sure you setup and activate your AVAudioSession iOS.

  1. import AVFoundation
  2. let session = AVAudioSession.sharedInstance()
  3. do {
  4. try session.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])
  5. try session.setActive(true)
  6. } catch {
  7. print(error)
  8. }

📹 AVCaptureSession

  1. let mixer = MediaMixer()
  2. await mixer.setFrameRate(30)
  3. await mixer.setSessionPreset(AVCaptureSession.Preset.medium)
  4. // Do not call beginConfiguration() and commitConfiguration() internally within the scope of the method, as they are called internally.
  5. await mixer.configuration { session in
  6. session.automaticallyConfiguresApplicationAudioSession = true
  7. }

🔊 Audio

Device

Specifies the audio device settings.

  1. let front = AVCaptureDevice.default(for: .audio)
  2. try? await mixer.attachAudio(front, track: 0) { audioDeviceUnit in }

AudioMixerSettings

If you want to mix multiple audio tracks, please enable the feature flag.

  1. await mixer.setMultiTrackAudioMixingEnabled(true)

When you specify the sampling rate, it will perform resampling. Additionally, in the case of multiple channels, downsampling can be applied.

  1. // Setting the value to 0 will be the same as the value specified in mainTrack.
  2. var settings = AudioMixerSettings(
  3. sampleRate: Float64 = 44100,
  4. channels: UInt32 = 0,
  5. )
  6. settings.tracks = [
  7. 0: .init(
  8. isMuted: Bool = false,
  9. downmix: Bool = true,
  10. channelMap: [Int]? = nil
  11. )
  12. ]
  13. async mixer.setAudioMixerSettings(settings)

AudioCodecSettings

  1. var audioSettings = AudioCodecSettings()
  2. /// Specifies the bitRate of audio output.
  3. audioSettings.bitrate = 64 * 1000
  4. /// Specifies the mixes the channels or not. Currently, it supports input sources with 4, 5, 6, and 8 channels.
  5. audioSettings.downmix = true
  6. /// Specifies the map of the output to input channels.
  7. audioSettings.channelMap: [Int]? = nil
  8. await stream.setAudioSettings(audioSettings)

🎥 Video

Device

Specifies the video capture settings.

  1. let front = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
  2. do {
  3. try await mixer.attachCamera(front, track: 0) { videoUnit in
  4. videoUnit.isVideoMirrored = true
  5. videoUnit.preferredVideoStabilizationMode = .standard
  6. videoUnit.colorFormat = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
  7. }
  8. } catch {
  9. print(error)
  10. }

VideoMixerSettings

  1. var videoMixerSettings = VideoMixerSettings()
  2. /// Specifies the image rendering mode.
  3. videoMixerSettings.mode = .passthrough or .offscreen
  4. /// Specifies the muted indicies whether freeze video signal or not.
  5. videoMixerSettings.isMuted = false
  6. /// Specifies the main track number.
  7. videoMixerSettings.mainTrack = 0
  8. await mixer.setVideoMixerSettings(videoMixerSettings)

VideoCodecSettings

  1. var videoSettings = VideoCodecSettings(
  2. videoSize: .init(width: 854, height: 480),
  3. profileLevel: kVTProfileLevel_H264_Baseline_3_1 as String,
  4. bitRate: 640 * 1000,
  5. maxKeyFrameIntervalDuration: 2,
  6. scalingMode: .trim,
  7. bitRateMode: .average,
  8. allowFrameReordering: nil,
  9. isHardwareEncoderEnabled: true
  10. )
  11. await stream.setVideoSettings(videoSettings)

⏺️ Recording

  1. // Specifies the recording settings. 0" means the same of input.
  2. let recorder = HKStreamRecorder()
  3. stream.addOutput(recorder)
  4. try await recorder.startRecording(fileName, settings: [
  5. AVMediaType.audio: [
  6. AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
  7. AVSampleRateKey: 0,
  8. AVNumberOfChannelsKey: 0,
  9. // AVEncoderBitRateKey: 128000,
  10. ],
  11. AVMediaType.video: [
  12. AVVideoCodecKey: AVVideoCodecH264,
  13. AVVideoHeightKey: 0,
  14. AVVideoWidthKey: 0,
  15. /*
  16. AVVideoCompressionPropertiesKey: [
  17. AVVideoMaxKeyFrameIntervalDurationKey: 2,
  18. AVVideoProfileLevelKey: AVVideoProfileLevelH264Baseline30,
  19. AVVideoAverageBitRateKey: 512000
  20. ]
  21. */
  22. ]
  23. ])
  24. try await recorder.stopRecording()

📜 License

BSD-3-Clause