Streaming de vídeo no iOS via RTMP

Introdução

Recentemente recebi uma tarefa interessante para trabalhar, fazer um aplicativo para streaming de vídeo, isso é para uma ShopStory inicial (ecomm live streaming). A primeira versão do aplicativo foi implementada usando a biblioteca Open Source para streaming de RTMP HaishinKit . E a segunda versão está no Larix SDK . Neste artigo, analisarei quais problemas surgiram no processo.





Requisitos

 ShopStory.live - B2B live e-commerce, , . , , , , . ShopStory.live  , , beauty- live commerce .





ShopStory, LarixBroadcaster , Android iOS. :





  1. , , , LarixBroadcaster



    , . , , , .





  2. , , .





  3. .





  4. , (, , ).





:









  • ( , )





  • ABR - Adaptive BitRate ( )





  • , fps, ..





  • .





– . Larix SDK



– , .

, :





  • LFLiveKit – 4.2k , 2016. 115 issue, .





  • HaishinKit – 2.1k , 7 . 11 issues.





  • VideoCore – 1.5k , 2015. .





  • KSY Live iOS SDK – 0.8k , 22 2020. README .





HaishinKit. , , .





HaishinKit

, . . /, . AVCaptureSession, AVCaptureDevice, AVCaptureDeviceInput



. View



, attach



RTMPStream







:





protocol BroadcastService: AnyObject {
    func connect()
    func publish()
    func stop()
}
      
      



.





class HaishinBroadcastService: BroadcastService {}
      
      



ABR - Adaptive BitRate

, , ().





ABR, issue. RTMPStreamDelegate



.





extension HaishinBroadcastService: RTMPStreamDelegate {
    func rtmpStream(_ stream: RTMPStream, didPublishInsufficientBW connection: RTMPConnection) {
        guard self.config.adaptiveBitrate else { return }
        guard let bitrate = self.currentBitrate else {
            assertionFailure()
            return
        }
        let newBitrate = max(UInt32(Double(bitrate) * Constants.bitrateDown), Constants.minBitrate)
        self.rtmpStream.videoSettings[.bitrate] = newBitrate
    }

    func rtmpStream(_ stream: RTMPStream, didPublishSufficientBW connection: RTMPConnection) {
        guard self.config.adaptiveBitrate else { return }
        guard let currentBitrate = self.currentBitrate,
              currentBitrate < Constants.maxBitrate else {
            return
        }
        guard self.bitrateRetryCounter >= Constants.retrySecBeforeUpBitrate else {
            self.bitrateRetryCounter += 1
            return
        }

        self.bitrateRetryCounter = 0
        let newBitrate = min(Constants.maxBitrate, UInt32(Double(currentBitrate) * Constants.bitrateUp))
        if newBitrate == currentBitrate { return }

        self.rtmpStream.videoSettings[.bitrate] = newBitrate
    }
}

private struct Constants {
    static let bitrateDown: Double = 0.75
    static let bitrateUp: Double = 1.15
    static let retrySecBeforeUpBitrate = 20
}
      
      



issue – ( 2 ), . didPublishInsufficientBW



, .





:





  • , 0.75





  • , 20 ( ), 1.15





Live update resolution

, , . RTMP . VK Live . Instagram , rtmp , , , ( , ). ShopStory .





. Wi-Fi, LTE. Larix SDK



. LarixBroadcaster – .





Larix SDK

LarixBroadcaster



+ LarixDemo ( ), , StepByStepGuide.





:





  • ,





  • .





:









  • , - LarixBroadcaster



    ( , : over 2000 )









  • connect



    publish







-

… , LarixBroadcaster



ViewController



2100 , Streamer



1100 . SDK. … , . @Aquary ( ):





« " ". — - . , . — . , , .. , .»





, SDK . , . c HaishinKit



, .. ( HaishinKit



).





ABR, ( ), , . . LarixBroadcaster



3 StreamConditionerMode1, 2, 3,



. ABR? ABR ( ).





, . , status = disconnected



. , .





func connectionStateDidChangeId(_ connectionID: Int32, state: ConnectionState, status: ConnectionStatus, info: [AnyHashable: Any]) {}
      
      



Larix



.

: SDK StreamerEngineProxy



bytesSent



bytesDelivered



, , . , , .





Connect Publish

RTMP, publish



connect



, Larix



( ), . - BroadcastService



.





?





  • , , , , , .





  • . , publish



    , , , , ( ). publish



    ( ). .





. , .





A escolha de uma biblioteca gratuita para streaming no iOS não é muito grande e, na verdade, tudo se resume a uma opção - HaishinKit



. Tem uma vantagem indiscutível - código aberto, e se Larix



não conseguirmos alinhar os gráficos e aumentar a estabilidade, vamos mergulhar no código aberto e procurar lugares que podem ser melhorados.





Comprando um SDK pago - não espere que resolva todos os seus problemas, talvez você tenha mais deles (aprenda vc em 2000 linhas)





E algumas conclusões mais globais podem ser feitas somente após executarmos a montagem em um número maior de fluxos.








All Articles