title: i got crash when record : "required condition is false: format.sampleRate == hwFormat.sampleRate" afterweb rtc call
body: <p>my record work normally, but the problem is after WebRTC call, i got crash</p>
  <p><strong>required condition is false: format.sampleRate == hwFormat.sampleRate</strong></p>
  <p>here is how i start crash and installTap:</p>
  <pre><code>func startRecord() {
          self.filePath = nil
          
          print(&quot;last format: \(audioEngine.inputNode.inputFormat(forBus: 0).sampleRate)&quot;)
          let session = AVAudioSession.sharedInstance()
          do {
              try session.setCategory(.playAndRecord, options: .mixWithOthers)
          } catch {
              print(&quot;======== Error setting setCategory \(error.localizedDescription)&quot;)
          }
          do {
              try session.setPreferredSampleRate(44100.0)
          } catch {
              print(&quot;======== Error setting rate \(error.localizedDescription)&quot;)
          }
          do {
              try session.setPreferredIOBufferDuration(0.005)
          } catch {
              print(&quot;======== Error IOBufferDuration \(error.localizedDescription)&quot;)
          }
          do {
              try session.setActive(true, options: .notifyOthersOnDeactivation)
          } catch {
              print(&quot;========== Error starting session \(error.localizedDescription)&quot;)
          }
          let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatInt16,
              sampleRate: 44100.0,
  //            sampleRate: audioEngine.inputNode.inputFormat(forBus: 0).sampleRate,
              channels: 1,
              interleaved: true)
          audioEngine.connect(audioEngine.inputNode, to: mixer, format: format)
          audioEngine.connect(mixer, to: audioEngine.mainMixerNode, format: format)
  
          let dir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! as String
          filePath =  dir.appending(&quot;/\(UUID.init().uuidString).wav&quot;)
  
          _ = ExtAudioFileCreateWithURL(URL(fileURLWithPath: filePath!) as CFURL,
              kAudioFileWAVEType,(format?.streamDescription)!,nil,AudioFileFlags.eraseFile.rawValue,&amp;outref)
  
          mixer.installTap(onBus: 0, bufferSize: AVAudioFrameCount((format?.sampleRate)!), format: format, block: { (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -&gt; Void in
  
              let audioBuffer : AVAudioBuffer = buffer
              _ = ExtAudioFileWrite(self.outref!, buffer.frameLength, audioBuffer.audioBufferList)
          })
  
          try! audioEngine.start()
          startMP3Rec(path: filePath!, rate: 128)
      }
  
      func stopRecord() {
  
          self.audioFilePlayer.stop()
          self.audioEngine.stop()
          self.mixer.removeTap(onBus: 0)
  
          self.stopMP3Rec()
          ExtAudioFileDispose(self.outref!)
  
          try? AVAudioSession.sharedInstance().setActive(false)
      }
      
      func startMP3Rec(path: String, rate: Int32) {
  
          self.isMP3Active = true
          var total = 0
          var read = 0
          var write: Int32 = 0
  
          let mp3path = path.replacingOccurrences(of: &quot;wav&quot;, with: &quot;mp3&quot;)
          var pcm: UnsafeMutablePointer&lt;FILE&gt; = fopen(path, &quot;rb&quot;)
          fseek(pcm, 4*1024, SEEK_CUR)
          let mp3: UnsafeMutablePointer&lt;FILE&gt; = fopen(mp3path, &quot;wb&quot;)
          let PCM_SIZE: Int = 8192
          let MP3_SIZE: Int32 = 8192
          let pcmbuffer = UnsafeMutablePointer&lt;Int16&gt;.allocate(capacity: Int(PCM_SIZE*2))
          let mp3buffer = UnsafeMutablePointer&lt;UInt8&gt;.allocate(capacity: Int(MP3_SIZE))
  
          let lame = lame_init()
          lame_set_num_channels(lame, 1)
          lame_set_mode(lame, MONO)
          lame_set_in_samplerate(lame, 44100)
          lame_set_brate(lame, rate)
          lame_set_VBR(lame, vbr_off)
          lame_init_params(lame)
  
          DispatchQueue.global(qos: .default).async {
              while true {
                  pcm = fopen(path, &quot;rb&quot;)
                  fseek(pcm, 4*1024 + total, SEEK_CUR)
                  read = fread(pcmbuffer, MemoryLayout&lt;Int16&gt;.size, PCM_SIZE, pcm)
                  if read != 0 {
                      write = lame_encode_buffer(lame, pcmbuffer, nil, Int32(read), mp3buffer, MP3_SIZE)
                      fwrite(mp3buffer, Int(write), 1, mp3)
                      total += read * MemoryLayout&lt;Int16&gt;.size
                      fclose(pcm)
                  } else if !self.isMP3Active {
                      _ = lame_encode_flush(lame, mp3buffer, MP3_SIZE)
                      _ = fwrite(mp3buffer, Int(write), 1, mp3)
                      break
                  } else {
                      fclose(pcm)
                      usleep(50)
                  }
              }
              lame_close(lame)
              fclose(mp3)
              fclose(pcm)
              self.filePathMP3 = mp3path
          }
      }
      
      func stopMP3Rec() {
          self.isMP3Active = false
      }
  </code></pre>
  <p>as first time run app, i log the last format using</p>
  <pre><code>print(&quot;last format: \(audioEngine.inputNode.inputFormat(forBus: 0).sampleRate)&quot;)
  </code></pre>
  <p>--&gt; return 0 -&gt; record normally
  next time return 44100 -&gt; record normally</p>
  <p>but after webrtc call, i got 48000, then it make crash in this line</p>
  <pre><code>self.audioEngine.connect(self.audioEngine.inputNode, to: self.mixer, format: format)
  </code></pre>
  <p><em><strong>i spend 4 hour in stackoverflow but no solution work for me.</strong></em></p>
  <p>i dont want 48000 format, because i have set the sample to</p>
  <pre><code>sampleRate: audioEngine.inputNode.inputFormat(forBus: 0).sampleRate,
  </code></pre>
  <p>-&gt; my output is hard to hear, i can recognize my voice :(</p>
  <p>So i think 44100 is the best</p>
  <p>can someone give me some advices? Thanks</p>