Skip to content

Commit

Permalink
Update tvOS Example.
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo4405 committed Oct 1, 2023
1 parent 858899b commit 682b208
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 85 deletions.
16 changes: 15 additions & 1 deletion Examples/tvOS/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import AVFAudio
import HaishinKit
import Logboard
import UIKit

let logger = LBLogger.with("com.haishinkit.Exsample.iOS")

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
true
// LBLogger.with(HaishinKitIdentifier).level = .trace
let session = AVAudioSession.sharedInstance()
do {
// If you set the "mode" parameter, stereo capture is not possible, so it is left unspecified.
try session.setCategory(.playAndRecord)
try session.setActive(true)
} catch {
logger.error(error)
}
return true
}
}
43 changes: 0 additions & 43 deletions Examples/tvOS/Base.lproj/Base.lproj/Main.storyboard

This file was deleted.

73 changes: 73 additions & 0 deletions Examples/tvOS/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder.AppleTV.Storyboard" version="3.0" toolsVersion="22154" targetRuntime="AppleTV" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="appleTV" appearance="light"/>
<dependencies>
<deployment identifier="tvOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22130"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Example_tvOS" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="1920" height="1080"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<glkView opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" enableSetNeedsDisplay="NO" translatesAutoresizingMaskIntoConstraints="NO" id="nrq-v4-hto" customClass="MTHKView" customModule="HaishinKit">
<rect key="frame" x="0.0" y="0.0" width="1920" height="1080"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nJN-5s-dow">
<rect key="frame" x="1781" y="16" width="123" height="75"/>
<inset key="contentEdgeInsets" minX="40" minY="20" maxX="40" maxY="20"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="filled" title="GO"/>
<connections>
<action selector="go:" destination="BYZ-38-t0r" eventType="primaryActionTriggered" id="HV0-Gk-0Ba"/>
</connections>
</button>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="a9E-qK-rmc">
<rect key="frame" x="1375" y="16" width="398" height="70"/>
<color key="backgroundColor" white="0.0" alpha="0.10000000000000001" colorSpace="calibratedWhite"/>
<segments>
<segment title="Playback"/>
<segment title="Publish"/>
</segments>
<connections>
<action selector="segmentedControl:" destination="BYZ-38-t0r" eventType="valueChanged" id="h3x-le-O5p"/>
</connections>
</segmentedControl>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="nJN-5s-dow" secondAttribute="trailing" constant="16" id="CNW-q4-rdA"/>
<constraint firstItem="nJN-5s-dow" firstAttribute="leading" secondItem="a9E-qK-rmc" secondAttribute="trailing" constant="8" id="O3d-xV-4yt"/>
<constraint firstItem="nJN-5s-dow" firstAttribute="leading" secondItem="a9E-qK-rmc" secondAttribute="trailing" constant="8" id="dLH-Cj-PMB"/>
<constraint firstItem="nJN-5s-dow" firstAttribute="top" secondItem="nrq-v4-hto" secondAttribute="top" constant="16" id="nIc-5r-Bf8"/>
<constraint firstItem="a9E-qK-rmc" firstAttribute="top" secondItem="nrq-v4-hto" secondAttribute="top" constant="16" id="sgY-2o-hQc"/>
</constraints>
</glkView>
</subviews>
<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="nrq-v4-hto" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="Bhi-7w-Rn8"/>
<constraint firstAttribute="bottom" secondItem="nrq-v4-hto" secondAttribute="bottom" id="hf0-Ch-bY1"/>
<constraint firstAttribute="trailing" secondItem="nrq-v4-hto" secondAttribute="trailing" id="rer-u3-a6H"/>
<constraint firstItem="nrq-v4-hto" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="s2H-j5-wkW"/>
</constraints>
</view>
<connections>
<outlet property="lfView" destination="nrq-v4-hto" id="CQd-Eu-2C3"/>
<outlet property="playbackOrPublishSegment" destination="a9E-qK-rmc" id="aHG-Pe-YP8"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-32" y="-4"/>
</scene>
</scenes>
</document>
4 changes: 4 additions & 0 deletions Examples/tvOS/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSMicrophoneUsageDescription</key>
<string></string>
<key>NSCameraUsageDescription</key>
<string></string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand Down
71 changes: 63 additions & 8 deletions Examples/tvOS/ViewController.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,62 @@
import AVFoundation
import AVKit
import HaishinKit
import UIKit

enum Mode {
case publish
case playback
}

final class ViewController: UIViewController {
@IBOutlet private weak var lfView: MTHKView!

var rtmpConnection = RTMPConnection()
var rtmpStream: RTMPStream!
@IBOutlet private weak var playbackOrPublishSegment: UISegmentedControl! {
didSet {
guard AVContinuityDevicePickerViewController.isSupported else {
return
}
playbackOrPublishSegment.removeSegment(at: 1, animated: false)
}
}
private var mode: Mode = .playback {
didSet {
logger.info(mode)
}
}
private var connection = RTMPConnection()
private var stream: RTMPStream!

override func viewDidLoad() {
super.viewDidLoad()
rtmpStream = RTMPStream(connection: rtmpConnection)
rtmpConnection.addEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
rtmpConnection.connect(Preference.defaultInstance.uri!)
stream = RTMPStream(connection: connection)
connection.addEventListener(.rtmpStatus, selector: #selector(rtmpStatusHandler), observer: self)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
lfView?.attachStream(rtmpStream)
lfView?.attachStream(stream)
}

@IBAction func segmentedControl(_ sender: UISegmentedControl) {
switch sender.titleForSegment(at: sender.selectedSegmentIndex) {
case "Publish":
mode = .publish
case "Playback":
mode = .playback
default:
break
}
}

@IBAction func go(_ sender: UIButton) {
switch mode {
case .publish:
let picker = AVContinuityDevicePickerViewController()
picker.delegate = self
present(picker, animated: true)
case .playback:
connection.connect(Preference.defaultInstance.uri!)
}
}

@objc
Expand All @@ -31,9 +71,24 @@ final class ViewController: UIViewController {

switch code {
case RTMPConnection.Code.connectSuccess.rawValue:
rtmpStream!.play(Preference.defaultInstance.streamName)
switch mode {
case .publish:
stream.publish(Preference.defaultInstance.streamName)
case .playback:
stream.play(Preference.defaultInstance.streamName)
}
default:
break
}
}
}

extension ViewController: AVContinuityDevicePickerViewControllerDelegate {
// MARK: AVContinuityDevicePickerViewControllerDelegate
func continuityDevicePicker( _ pickerViewController: AVContinuityDevicePickerViewController, didConnect device: AVContinuityDevice) {
if let camera = device.videoDevices.first {
logger.info(camera)
stream.attachCamera(camera)
}
}
}
23 changes: 10 additions & 13 deletions HaishinKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,9 @@
290907CE1C3961BC00F2E80C /* Util */ = {
isa = PBXGroup;
children = (
BC701F312AAC676C00C4BEFE /* AVAudioFormatFactory.swift */,
298BCF321DD4C44A007FF86A /* AnyUtil.swift */,
29DC17B221D0CC0600E26CED /* Atomic.swift */,
BC701F312AAC676C00C4BEFE /* AVAudioFormatFactory.swift */,
29B876B81CD70B3900FC07DA /* ByteArray.swift */,
29B876631CD70AB300FC07DA /* Constants.swift */,
BC0D236C26331BAB001DDA0C /* DataBuffer.swift */,
Expand Down Expand Up @@ -1105,7 +1105,7 @@
29D3D4FE1ED0536700DD4AA6 /* tvOS */ = {
isa = PBXGroup;
children = (
29D3D5011ED053C000DD4AA6 /* Base.lproj */,
29D3D5021ED053C000DD4AA6 /* Main.storyboard */,
29D3D4FF1ED053C000DD4AA6 /* AppDelegate.swift */,
29D3D5001ED053C000DD4AA6 /* Assets.xcassets */,
29D3D5041ED053C000DD4AA6 /* Info.plist */,
Expand All @@ -1114,14 +1114,6 @@
path = tvOS;
sourceTree = "<group>";
};
29D3D5011ED053C000DD4AA6 /* Base.lproj */ = {
isa = PBXGroup;
children = (
29D3D5021ED053C000DD4AA6 /* Main.storyboard */,
);
path = Base.lproj;
sourceTree = "<group>";
};
29EA87D11E799EFF0043A5F8 /* Extension */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1490,7 +1482,6 @@
29D3D4EE1ED0531500DD4AA6 = {
CreatedOnToolsVersion = 8.3.2;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
BCCC45952AA289FA0016EFE8 = {
CreatedOnToolsVersion = 14.3;
Expand Down Expand Up @@ -2499,6 +2490,8 @@
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = SUEQ2SZ2L5;
ENABLE_USER_SCRIPT_SANDBOXING = NO;
Expand All @@ -2512,14 +2505,15 @@
OTHER_SWIFT_FLAGS = "-enable-upcoming-feature ExistentialAny";
PRODUCT_BUNDLE_IDENTIFIER = "com.haishinkit.Example-tvOS";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = appletvos;
SUPPORTED_PLATFORMS = "appletvos appletvsimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 12.0;
TVOS_DEPLOYMENT_TARGET = 17.0;
};
name = Debug;
};
Expand All @@ -2532,6 +2526,8 @@
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = SUEQ2SZ2L5;
ENABLE_USER_SCRIPT_SANDBOXING = NO;
FRAMEWORK_SEARCH_PATHS = "$(inherited)";
Expand All @@ -2544,13 +2540,14 @@
OTHER_SWIFT_FLAGS = "-enable-upcoming-feature ExistentialAny";
PRODUCT_BUNDLE_IDENTIFIER = "com.haishinkit.Example-tvOS";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SDKROOT = appletvos;
SUPPORTED_PLATFORMS = "appletvos appletvsimulator";
SUPPORTS_MACCATALYST = NO;
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = 3;
TVOS_DEPLOYMENT_TARGET = 12.0;
TVOS_DEPLOYMENT_TARGET = 17.0;
};
name = Release;
};
Expand Down
16 changes: 8 additions & 8 deletions Sources/Codec/VideoCodecSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,6 @@ public struct VideoCodecSettings: Codable {
public var frameInterval: Double
/// Specifies the keyframeInterval.
public var maxKeyFrameIntervalDuration: Int32
/// Specifies the scalingMode.
public var scalingMode: ScalingMode
/// Specifies the allowFrameRecording.
public var allowFrameReordering: Bool? // swiftlint:disable:this discouraged_optional_boolean
/// Specifies the bitRateMode.
public var bitRateMode: BitRateMode
/// Specifies the H264 profileLevel.
public var profileLevel: String {
didSet {
Expand All @@ -102,6 +96,12 @@ public struct VideoCodecSettings: Codable {
}
}
}
/// Specifies the scalingMode.
public var scalingMode: ScalingMode
/// Specifies the bitRateMode.
public var bitRateMode: BitRateMode
/// Specifies the allowFrameRecording.
public var allowFrameReordering: Bool? // swiftlint:disable:this discouraged_optional_boolean
/// Specifies the HardwareEncoder is enabled(TRUE), or not(FALSE) for macOS.
public var isHardwareEncoderEnabled = true

Expand All @@ -110,13 +110,13 @@ public struct VideoCodecSettings: Codable {
/// Creates a new VideoCodecSettings instance.
public init(
videoSize: CGSize = .init(width: 854, height: 480),
profileLevel: String = kVTProfileLevel_H264_Baseline_3_1 as String,
bitRate: Int = 640 * 1000,
frameInterval: Double = 0.0,
maxKeyFrameIntervalDuration: Int32 = 2,
profileLevel: String = kVTProfileLevel_H264_Baseline_3_1 as String,
scalingMode: ScalingMode = .trim,
bitRateMode: BitRateMode = .average,
allowFrameReordering: Bool? = nil, // swiftlint:disable:this discouraged_optional_boolean
allowFrameReordering: Bool? = nil, // swiftlint:disable:this discouraged_optional_boolean,
isHardwareEncoderEnabled: Bool = true
) {
self.videoSize = videoSize
Expand Down
Loading

0 comments on commit 682b208

Please sign in to comment.