Demo

这个代码段展示了如何使用Agora SDK在iOS上进行屏幕分享直播。初始化AgoraRtcEngineKit,设置为直播模式并加入频道,对外部视频源进行配置,处理音频采样率和声道数,并提供了发送视频和音频缓冲区的方法。此外,还包括停止广播的函数。
摘要由CSDN通过智能技术生成

// AgoraUploader.swift
// Agora-Screen-Sharing-iOS-Broadcast
//
// Created by GongYuhua on 2017/1/16.
// Copyright © 2017年 Agora. All rights reserved.
//

import Foundation
import CoreMedia
import ReplayKit

class AgoraUploader {
private static let videoDimension : CGSize = {
let screenSize = UIScreen.main.currentMode!.size
var boundingSize = CGSize(width: 720, height: 1280)
let mW = boundingSize.width / screenSize.width
let mH = boundingSize.height / screenSize.height
if( mH < mW ) {
boundingSize.width = boundingSize.height / screenSize.height * screenSize.width
}
else if( mW < mH ) {
boundingSize.height = boundingSize.width / screenSize.width * screenSize.height
}
return boundingSize
}()

private static let audioSampleRate: UInt = 48000
private static let audioChannels: UInt = 2

private static let sharedAgoraEngine: AgoraRtcEngineKit = {
    let kit = AgoraRtcEngineKit.sharedEngine(withAppId: KeyCenter.AppId, delegate: nil)
    kit.setChannelProfile(.liveBroadcasting)
    kit.setClientRole(.broadcaster)

// kit.setLogFilter(0x080f)

    print(AgoraRtcEngineKit.getSdkVersion())
    
    kit.enableVideo()
    kit.setExternalVideoSource(true, useTexture: true, pushMode: true)
    let videoConfig = AgoraVideoEncoderConfiguration(size: videoDimension,
                                                     frameRate: .fps24,
                                                     bitrate: AgoraVideoBitrateStandard,
                                                     orientationMode: .adaptative)
    kit.setVideoEncoderConfiguration(videoConfig)

// kit.setParameters("{“che.audio.use.remoteio”: false}")
// kit.setParameters("{“che.audio.headsetroute.mediavolume”: 0}")
kit.setAudioProfile(.musicStandardStereo, scenario: .chatRoomEntertainment)
AgoraAudioProcessing.registerAudioPreprocessing(kit)
kit.setRecordingAudioFrameParametersWithSampleRate(Int(audioSampleRate),
channel: Int(audioChannels),
mode: .readWrite,
samplesPerCall: 1024)
kit.setParameters("{“che.audio.external_device”:true}")

    kit.setParameters("{\"che.hardware_encoding\":1}")
    kit.setParameters("{\"che.video.enc_auto_adjust\":0}")
    
    kit.muteAllRemoteVideoStreams(true)
    kit.muteAllRemoteAudioStreams(true)
    
    kit.adjustRecordingSignalVolume(0)
    
    return kit
}()

static func startBroadcast(to channel: String) {
    sharedAgoraEngine.joinChannel(byToken: nil, channelId: channel, info: nil, uid: 0, joinSuccess: nil)
}

static func sendVideoBuffer(_ sampleBuffer: CMSampleBuffer) {
    guard let videoFrame = CMSampleBufferGetImageBuffer(sampleBuffer)
         else {
        return
    }
    
    var rotation : Int32 = 0
    if let orientationAttachment = CMGetAttachment(sampleBuffer, key: RPVideoSampleOrientationKey as CFString, attachmentModeOut: nil) as? NSNumber {
        if let orientation = CGImagePropertyOrientation(rawValue: orientationAttachment.uint32Value) {
            switch orientation {
            case .up,    .upMirrored:    rotation = 0
            case .down,  .downMirrored:  rotation = 180
            case .left,  .leftMirrored:  rotation = 90
            case .right, .rightMirrored: rotation = 270
            default:   break
            }
        }
    }
    
    let time = CMSampleBufferGetPresentationTimeStamp(sampleBuffer)
    
    let frame = AgoraVideoFrame()
    frame.format = 12
    frame.time = time
    frame.textureBuf = videoFrame
    frame.rotation = rotation
    sharedAgoraEngine.pushExternalVideoFrame(frame)
}

static func sendAudioAppBuffer(_ sampleBuffer: CMSampleBuffer) {
    AgoraAudioTube.agoraKit(sharedAgoraEngine,
                            pushAudioCMSampleBuffer: sampleBuffer,
                            resampleRate: audioSampleRate,
                            type: .app)
}

static func sendAudioMicBuffer(_ sampleBuffer: CMSampleBuffer) {
    AgoraAudioTube.agoraKit(sharedAgoraEngine,
                            pushAudioCMSampleBuffer: sampleBuffer,
                            resampleRate: audioSampleRate,
                            type: .mic)
}

static func stopBroadcast() {
    sharedAgoraEngine.leaveChannel(nil)
    AgoraRtcEngineKit.destroy()
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值