音视频积累-Wakeups 解决
一、现象
ReplayKit2运行过程中经常会被系统杀掉,在USB连接手机后可以导出crash log
Date/Time: 2017-09-29 12:16:59.321071 +0800 OS Version: iPhone OS 11.0.1 (Build 15A402) Architecture: arm64 Report Version: 19 Command: LABroadcastUpload Path: /private/var/containers/Bundle/Application/1BB63CDA-9704-44C7-9D61-5BC6106F9E0E/LiveAssistant.app/PlugIns/LABroadcastUpload.appex/LABroadcastUpload Version: 2.3.0.0 (2.3.0.0) Parent: launchd [1] PID: 1557 Event: wakeups Wakeups: 45003 wakeups over the last 92 seconds (487 wakeups per second average), exceeding limit of 150 wakeups per second over 300 seconds Action taken: none Duration: 92.35s Steps: 46 Hardware model: iPhone8,1 Active cpus: 2 Powerstats for: LABroadcastUploa [1557] UUID: F91308C6-C89C-3571-94C0-BBE49DFB2013 Start time: 2017-09-29 12:18:04 +0800 End time: 2017-09-29 12:18:31 +0800 Microstackshots: 46 samples (100%) Primary state: 30 samples Frontmost App, User mode, Effective Thread QoS Default, Requested Thread QoS Default, Override Thread QoS Unspecified User Activity: 0 samples Idle, 46 samples Active Power Source: 0 samples on Battery, 46 samples on AC 33 _pthread_start + 311 (libsystem_pthread.dylib + 8696) [0x18618c1f8] 33 _pthread_body + 307 (libsystem_pthread.dylib + 9004) [0x18618c32c] 15 __NSThread__start__ + 995 (Foundation + 1103968) [0x186f4f860] 5 -[LAAudioMix mixAudioLoop:] (LAAudioMix.m:174 in LABroadcastUpload + 276808) [0x10264f948] 4 -[LAUploadManager pushCustomPCMData:len:] (LAUploadManager.m:164 in LABroadcastUpload + 157864) [0x1026328a8] 4 -[LAExLogger addOneLog:inFile:function:line:module:message:] (LAExLogger.m:115 in LABroadcastUpload + 77892) [0x10261f044] 2 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:143 in LABroadcastUpload + 78616) [0x10261f318] 2 +[NSString stringWithFormat:] + 67 (Foundation + 10420) [0x186e448b4] 2 _CFStringCreateWithFormatAndArgumentsAux2 + 131 (CoreFoundation + 1054296) [0x18651f658] 2 __CFStringAppendFormatCore + 9436 (CoreFoundation + 1048212) [0x18651de94] 2 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:161 in LABroadcastUpload + 78664) [0x10261f348] 2 NSLog + 31 (Foundation + 73876) [0x186e54094] 2 _NSLogv + 127 (Foundation + 972068) [0x186f2f524] 2 _CFLogvEx3 + 227 (CoreFoundation + 1239416) [0x18654c978] 2 __proc_info + 8 (libsystem_kernel.dylib + 135252) [0x186079054] 2 <Kernel mode> 1 -[LAUploadManager pushCustomPCMData:len:] (LAUploadManager.m:165 in LABroadcastUpload + 157936) [0x1026328f0] 1 -[LAEncodeRTMPCore pushCustomPCMData:len:] (LAEncodeRTMPCore.m:676 in LABroadcastUpload + 376948) [0x102668074] 1 -[TXLivePush sendCustomPCMData:len:] (TXLivePush.mm:197 in LABroadcastUpload + 1895364) [0x1027dabc4] 1 -[TXLivePushSingleton sendCustomPCMData:len:] (TXLivePushSingleton.mm:1508 in LABroadcastUpload + 1862324) [0x1027d2ab4] 1 +[TXRtmpApi sendAudioWithPCM:len:customMode:isMute:] (TXRTMPAPI.mm:800 in LABroadcastUpload + 2942868) [0x1028da794] 1 CTXRtmpSdkPublish::SendPCMData (RTMPSDKPublish.cpp:680) (RTMPSDKPublish.cpp:680 in LABroadcastUpload + 1699412) [0x1027aae54] 1 CTXRtmpSdkPublish::SendPCMDataInternal (RTMPSDKPublish.cpp:717) (RTMPSDKPublish.cpp:717 in LABroadcastUpload + 1699680) [0x1027aaf60] 1 CTXAudioEncProcessModel::ConvertPCM2AAC (AudioEncProcessModel.cpp:334) (AudioEncProcessModel.cpp:334 in LABroadcastUpload + 2992800) [0x1028e6aa0] 1 CTXHWAudioEncoder::ConvertPCM2AAC (HWAudioEncoder.mm:217) (HWAudioEncoder.mm:217 in LABroadcastUpload + 1253684) [0x10273e134] 1 -[TXHWAudioEncoder encodeBuffer:length:] (HWAudioEncoder.mm:66 in LABroadcastUpload + 1250232) [0x10273d3b8] 1 AudioConverterFillComplexBuffer + 1047 (AudioToolbox + 3341464) [0x18a206c98] 1 BufferedAudioConverter::FillBuffer + 799 (AudioToolbox + 1166536) [0x189ff3cc8] 1 AudioConverterChain::RenderOutput + 119 (AudioToolbox + 1312348) [0x18a01765c] 1 CodecConverter::EncoderFillBuffer + 2103 (AudioToolbox + 2249648) [0x18a0fc3b0] 1 ProduceOutputPackets + 27 (AudioCodecs + 30440) [0x1a59936e8] 1 ACMP4AACBaseEncoder::ProduceOutputPackets + 239 (AudioCodecs + 902992) [0x1a5a68750] 1 AACEncoder::EncodeFrame + 487 (AudioCodecs + 447852) [0x1a59f956c] 1 AACPsychoacoustic + 119 (AudioCodecs + 677008) [0x1a5a31490] 1 AdvancePsychoacoustic + 2631 (AudioCodecs + 591376) [0x1a5a1c610] 1 pamMonaural + 3115 (AudioCodecs + 348768) [0x1a59e1260] 1 log10ApproxVect_Neon + 35 (AudioCodecs + 431044) [0x1a59f53c4] 1 log10ApproxVect_Neon_sub + 108 (AudioCodecs + 1190172) [0x1a5aae91c] 4 -[TXLivePushSingleton sampleBufferLoopFun:] (TXLivePushSingleton.mm:1829 in LABroadcastUpload + 1872652) [0x1027d530c] 4 -[TXLivePushSingleton sendCustomSampleBufferInternal:] (TXLivePushSingleton.mm:2004 in LABroadcastUpload + 1876740) [0x1027d6304] 2 tx_NV12ToI420Rotate (rotate.cc:527 in LABroadcastUpload + 1248624) [0x10273cd70] 2 tx_RotatePlane270 (rotate.cc:130 in LABroadcastUpload + 1245332) [0x10273c094] 2 tx_TransposeWx8_NEON (rotate_neon64.cc:34 in LABroadcastUpload + 2792160) [0x1028b5ae0] 2 tx_NV12ToI420Rotate (rotate.cc:528 in LABroadcastUpload + 1248660) [0x10273cd94] 2 tx_RotateUV270 (rotate.cc:332 in LABroadcastUpload + 1246448) [0x10273c4f0] 2 tx_TransposeUVWx8_NEON (rotate_neon64.cc:266 in LABroadcastUpload + 2792616) [0x1028b5ca8] 3 -[LAAudioMix mixAudioLoop:] (LAAudioMix.m:205 in LABroadcastUpload + 277924) [0x10264fda4] 3 -[LAExLogger addOneLog:inFile:function:line:module:message:] (LAExLogger.m:115 in LABroadcastUpload + 77892) [0x10261f044] 2 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:143 in LABroadcastUpload + 78616) [0x10261f318] 2 +[NSString stringWithFormat:] + 67 (Foundation + 10420) [0x186e448b4] 2 _CFStringCreateWithFormatAndArgumentsAux2 + 131 (CoreFoundation + 1054296) [0x18651f658] 2 __CFStringAppendFormatCore + 10771 (CoreFoundation + 1049548) [0x18651e3cc] 2 snprintf_l + 27 (libsystem_c.dylib + 9996) [0x185f8870c] 2 _platform_memset + 0 (libsystem_platform.dylib + 7680) [0x186181e00] 1 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:161 in LABroadcastUpload + 78664) [0x10261f348] 1 NSLog + 31 (Foundation + 73876) [0x186e54094] 1 _NSLogv + 127 (Foundation + 972068) [0x186f2f524] 1 _CFLogvEx3 + 187 (CoreFoundation + 1239376) [0x18654c950] 1 _os_log_with_args_impl + 387 (libsystem_trace.dylib + 63096) [0x1861b5678] 1 _os_log_impl_flatten_and_send + 2787 (libsystem_trace.dylib + 56588) [0x1861b3d0c] 1 _os_log_impl_stream + 259 (libsystem_trace.dylib + 61120) [0x1861b4ec0] 1 _os_activity_stream_reflect + 531 (libsystem_trace.dylib + 9596) [0x1861a857c] 1 _xpc_dispose + 27 (libxpc.dylib + 10112) [0x1861c3780] 1 _xpc_dictionary_dispose + 43 (libxpc.dylib + 10184) [0x1861c37c8] 1 _xpc_dictionary_node_free + 71 (libxpc.dylib + 10424) [0x1861c38b8] 1 object_dispose + 15 (libobjc.A.dylib + 83600) [0x185a80690] 1 objc_destructInstance + 99 (libobjc.A.dylib + 83524) [0x185a80644] 1 _object_remove_assocations + 76 (libobjc.A.dylib + 105308) [0x185a85b5c] 3 -[LAAudioMix mixAudioLoop:] (LAAudioMix.m:209 in LABroadcastUpload + 278004) [0x10264fdf4]
二、Crash分析
该Crash是因为线程调度太频繁导致,推流以及编码SDK中为了稳定帧率,通常会启动一个线程,进入一个while循环,循环中通过sleep一定的时间实现每秒钟稳定在一定的帧率处理音视频数据。
通常移动端视频数据处理FPS不超过30,音频不超过42(跟音频的采样率有关系),iOS每秒中线程唤醒的次数不能超过150,按道理这个数据没有超过啊,是为什么呢、?
5 -[LAAudioMix mixAudioLoop:] (LAAudioMix.m:174 in LABroadcastUpload + 276808) [0x10264f948] 4 -[LAUploadManager pushCustomPCMData:len:] (LAUploadManager.m:164 in LABroadcastUpload + 157864) [0x1026328a8] 4 -[LAExLogger addOneLog:inFile:function:line:module:message:] (LAExLogger.m:115 in LABroadcastUpload + 77892) [0x10261f044] 2 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:143 in LABroadcastUpload + 78616) [0x10261f318] 2 +[NSString stringWithFormat:] + 67 (Foundation + 10420) [0x186e448b4] 2 _CFStringCreateWithFormatAndArgumentsAux2 + 131 (CoreFoundation + 1054296) [0x18651f658] 2 __CFStringAppendFormatCore + 9436 (CoreFoundation + 1048212) [0x18651de94] 2 -[LAExLogger addOneLog:inFile:function:line:module:fullMessage:] (LAExLogger.m:161 in LABroadcastUpload + 78664) [0x10261f348] 2 NSLog + 31 (Foundation + 73876) [0x186e54094] 2 _NSLogv + 127 (Foundation + 972068) [0x186f2f524] 2 _CFLogvEx3 + 227 (CoreFoundation + 1239416) [0x18654c978] 2 __proc_info + 8 (libsystem_kernel.dylib + 135252) [0x186079054] 2 <Kernel mode>
观察crash中的堆栈,发现一个现象,堆栈的最上层都是while循环中的,最下层一般因为NSLog进入到了内核态
这里猜测应该是因为由用户态进入内核态,一次系统调用过程中,用户线程被休眠,系统调用结果返回时又回到用户线程,用户线程被唤醒
因此在高频的循环中,要注意关键、耗时的方法调用,同时也要注意多线程下面访问一些变量