shogo4405 / HaishinKit.swift

Camera and Microphone streaming library via RTMP and SRT for iOS, macOS, tvOS and visionOS.
BSD 3-Clause "New" or "Revised" License
2.78k stars 616 forks source link

Problems when registering a VisualEffect #133

Closed lfarah closed 7 years ago

lfarah commented 8 years ago

I am not able to set a VisualEffect to my rtmpStream.

Filter class

final class MonochromeEffect: VisualEffect {
    let filter:CIFilter? = CIFilter(name: "CIColorMonochrome")

    override func execute(_ image: CIImage) -> CIImage {
        guard let filter:CIFilter = filter else {
            return image
        }
        filter.setValue(image, forKey: "inputImage")
        filter.setValue(CIColor(red: 0.75, green: 0.75, blue: 0.75), forKey: "inputColor")
        filter.setValue(1.0, forKey: "inputIntensity")
        return filter.outputImage!
    }
}

My ViewController

//
//  ViewController.swift
//  FBLiveAPI
//
//  Created by LeeSunhyoup on 2016. 9. 17..
//  Copyright © 2016년 Lee Sun-Hyoup. All rights reserved.
//

import UIKit
import lf
import AVFoundation
import FBSDKLoginKit

class ViewController: UIViewController {
    @IBOutlet var contentView: UIView!
    @IBOutlet var liveButton: UIButton!
    @IBOutlet var livePrivacyControl: UISegmentedControl!

    var livePrivacy: FBLivePrivacy = .closed
    let rtmpConnection:RTMPConnection = RTMPConnection()
    var rtmpStream: RTMPStream?

    override func viewDidLoad() {
        super.viewDidLoad()

        liveButton.layer.cornerRadius = 15

        rtmpStream = RTMPStream(connection: rtmpConnection)
        rtmpStream?.syncOrientation = true

        rtmpStream?.attachAudio(AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeAudio))
        rtmpStream?.attachCamera(DeviceUtil.device(withPosition: .front))

        rtmpStream?.captureSettings = [
            "sessionPreset": AVCaptureSessionPreset1280x720,
            "continuousAutofocus": true,
            "continuousExposure": true,
        ]
        let _:Bool = rtmpStream!.registerEffect(video: MonochromeEffect())

        let lfView:LFView = LFView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 600))
        //lfView.videoGravity = AVLayerVideoGravityResizeAspectFill
        lfView.attachStream(rtmpStream)

        // add ViewController#view
        view.addSubview(lfView)
    }

    @IBAction func live() {

        startFBLive()
    }

    func startFBLive() {
        if FBSDKAccessToken.current() != nil {
            FBLiveAPI.shared.startLive(privacy: livePrivacy) { result in
                guard let streamUrlString = (result as? NSDictionary)?.value(forKey: "stream_url") as? String else {
                    return
                }
                let streamUrl = URL(string: streamUrlString)

                guard let lastPathComponent = streamUrl?.lastPathComponent,
                    let query = streamUrl?.query else {
                        return
                }

                self.rtmpConnection.connect("rtmp://rtmp-api.facebook.com:80/rtmp/")
                self.rtmpStream?.publish("\(lastPathComponent)?\(query)")

                self.livePrivacyControl.isUserInteractionEnabled = false
            }
        } else {
            fbLogin()
        }
    }

    func endFBLive() {
        if FBSDKAccessToken.current() != nil {
            FBLiveAPI.shared.endLive { _ in

                self.rtmpStream?.close()
                self.livePrivacyControl.isUserInteractionEnabled = true
            }
        } else {
            fbLogin()
        }
    }

    func fbLogin() {
        let loginManager = FBSDKLoginManager()
        loginManager.logIn(withPublishPermissions: ["publish_actions"], from: self) { (result, error) in
            if error != nil {
                print("Error")
            } else if result?.isCancelled == true {
                print("Cancelled")
            } else {
                print("Logged in")
            }
        }
    }

    @IBAction func changeLivePrivacy(sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
        case 0:
            livePrivacy = .closed
        case 1:
            livePrivacy = .everyone
        case 2:
            livePrivacy = .allFriends
        case 3:
            livePrivacy = .friendsOfFriends
        default:
            break
        }
    }

    // MARK: VCSessionDelegate

}
lfarah commented 8 years ago

Hey @shogo4405, can you give me a help on this one?

shogo4405 commented 7 years ago

@lfarah Please use GLLFView. LFView dose not support VisualEffect.