Skip to content

Latest commit

 

History

History

iOS

Beauty API Demo

English | 中文

This document provides a comprehensive guide on how to quickly set up and run the Beauty API sample code.

Demo Effect


Prerequisites

  • iOS Version: 11 and above
  • Xcode Version: 13 and above

Running the Project

Step 1: Obtain Agora Credentials

  1. Create an Account: Sign up at agora.io.
  2. Access Dashboard: Navigate to Projects > Project List.
  3. Save App ID: Copy your App ID for later use.

Step 2: Set Up FaceUnity (Optional)

  • Contact Support: Reach out to FaceUnity for the latest Beauty SDK, resources, and certificate.
  • Download Resources: Extract and add the following files to your project:
Resource Files Project Path
Model Files iOS/FULib/Resources/model/*.bundle
Graphics Files iOS/FULib/Resources/graphics/*.bundle
Makeup Resources (e.g., naicha.bundle) iOS/FULib/Resources/makeup/*.bundle
Sticker Resources (e.g., fashi.bundle) iOS/FULib/Resources/stickers/*.bundle
authpack.h iOS/BeautyAPi/FUBeauty/authpack.h

Step 3: Configure the Project

  1. Add Pod Dependency: Update your Podfile with:

    pod 'fuLib', :path => 'fu.podspec'
  2. Install Pods: Run the following command:

    pod install
  3. Configure App ID: Edit KeyCenter.swift:

    static let AppId: String = "YOUR_APP_ID"
  4. Update Bundle Identifier: Change the Bundle Identifier in the BeautyAPI Project settings.

  5. Run the Project: Launch the project in Xcode.


Integrating BeautyAPI into Your Project

Step 1: Add BeautyAPI Files

  • Include Files: Add the iOS/BeautyAPi/BeautyAPI directory, Render/FURender folder, and BeautyAPI.h & BeautyAPI.m files to your project.

Step 2: Install Dependencies

  • Update Podfile: Modify your Podfile as follows:

    platform :ios, '9.0'
    target 'Your_App_Name' do
        pod 'AgoraRtcEngine_iOS', 'x.y.z'
        pod 'fuLib', :path => 'fu.podspec'
    end
  • Install Pods: Execute:

    pod install

Step 3: Implement Beauty Features

  1. Initialize AgoraRtcEngineKit:

    private lazy var rtcEngine: AgoraRtcEngineKit = {
        let config = AgoraRtcEngineConfig()
        config.appId = KeyCenter.AppId
        config.channelProfile = .liveBroadcasting
        let rtc = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
        rtc.setClientRole(.broadcaster)
        rtc.enableAudio()
        rtc.enableVideo()
        rtc.setDefaultAudioRouteToSpeakerphone(true)
        return rtc
    }()
  2. Initialize Beauty API:

    private lazy var fuRender = FUBeautyRender()
    private lazy var beautyAPI = BeautyAPI()
    
    let config = BeautyConfig()
    config.rtcEngine = rtcEngine
    config.captureMode = capture == "Custom" ? .custom : .agora
    config.cameraConfig = cameraConfig
    config.beautyRender = fuRender
    config.statsEnable = false
    config.statsDuration = 1
    config.eventCallback = { stats in
        print("Stats: min=\(stats.minCostMs), max=\(stats.maxCostMs), avg=\(stats.averageCostMs)")
    }
    
    let result = beautyAPI.initialize(config)
    if result != 0 {
        print("Initialization error: \(result)")
    }
  3. Enable Beauty Effects:

    beautyAPI.enable(true)
  4. Start Video Capture:

    beautyAPI.setupLocalVideo(localView, renderMode: .hidden)
  5. Custom Video Capture:

    • Register a raw video data observer when using custom video capture:
    if capture == "Custom" {
        rtcEngine.setVideoFrameDelegate(self)
    }
    
    extension BeautyViewController: AgoraVideoFrameDelegate {
        func onCapture(_ videoFrame: AgoraOutputVideoFrame, sourceType: AgoraVideoSourceType) -> Bool {
            guard let pixelBuffer = videoFrame.pixelBuffer else { return true }
            beautyAPI.onFrame(pixelBuffer) { pixelBuffer in
                videoFrame.pixelBuffer = pixelBuffer
            }
            return true
        }
    
        func getMirrorApplied() -> Bool {
            beautyAPI.getMirrorApplied()
        }
    
        func getObservedFramePosition() -> AgoraVideoFramePosition {
            .postCapture
        }
    }
  6. Join a Channel:

    let mediaOption = AgoraRtcChannelMediaOptions()
    mediaOption.clientRoleType = isBroadcast ? .broadcaster : .audience
    mediaOption.autoSubscribeAudio = true
    mediaOption.autoSubscribeVideo = true
    mediaOption.publishCameraTrack = mediaOption.clientRoleType == .broadcaster
    mediaOption.publishCustomVideoTrack = false
    mediaOption.publishMicrophoneTrack = mediaOption.clientRoleType == .broadcaster
    
    let result = rtcEngine.joinChannel(byToken: nil, channelId: channelName ?? "", uid: 0, mediaOptions: mediaOption)
    if result != 0 {
        print("Join channel failed")
    }
  7. Update Camera Configuration (Optional):

    beautyAPI.switchCamera()
    
    let cameraConfig = BeautyManager.shareManager.cameraConfig
    if self.beautyAPI?.isFrontCamera ?? false {
        cameraConfig.frontMirror = Configs.mirrorTypes[value] ?? .LOCAL_REMOTE
    } else {
        cameraConfig.backMirror = Configs.mirrorTypes[value] ?? .NONE
    }
    self.beautyAPI?.update(cameraConfig)
  8. Leave the Channel:

    rtcEngine.leaveChannel()
  9. Destroy Resources:

    beautyAPI.destroy()
    AgoraRtcEngineKit.destroy()

Feedback

If you have any problems or suggestions regarding the sample projects, feel free to file an issue.

Related Resources

  • Check our FAQ to see if your issue has been recorded.
  • Dive into Agora SDK Samples to see more tutorials.
  • Take a look at Agora Use Case for more complicated real use cases.
  • Repositories managed by developer communities can be found at Agora Community.
  • If you encounter problems during integration, feel free to ask questions in Stack Overflow.

5. License

The sample projects are under the MIT license.