aws / amazon-chime-sdk-ios

An iOS client library for integrating multi-party communications powered by the Amazon Chime service.
https://aws.amazon.com/chime/chime-sdk/
Apache License 2.0
139 stars 64 forks source link

Memory leak on meetingSession.audioVideo.start() #570

Open rouzbeh-abadi opened 1 year ago

rouzbeh-abadi commented 1 year ago

Hi, I'm using this framework to establish a video call with a custom VideoSource

Here is my video source class :

import Foundation
import AmazonChimeSDK

class CustomVideoSource: VideoSource, VideoSink {

    var videoContentHint = VideoContentHint.none

    private lazy var sinks = NSMutableSet()

    func onVideoFrameReceived(frame: VideoFrame) {

        for sink in sinks {
            (sink as? VideoSink)?.onVideoFrameReceived(frame: frame)
        }
    }

    func addVideoSink(sink: VideoSink) {
        sinks.add(sink)
    }

    func removeVideoSink(sink: VideoSink) {
        sinks.remove(sink)
    }
}

I created this custom video source with frames that come from iOS's vision framework . Without publishing the stream, the memory usage of the app is about 100 mb and there is no memory leak according to the instruments app in Xcode.

But as soon as I start streaming with meetingSession.audioVideo.start() a number of memory leaks occurs

Screenshot 2023-02-13 at 11 18 28

This memory leak doesn't impact that much when no customise video source is using for the stream, but when I feed it with the customize video source the memory starts to increase to >2gb that crashes the app

Screenshot 2023-02-13 at 13 46 44

And here is how I start the stream

  private func setupMeeting() {
        meetingSession = DefaultMeetingSession(configuration: meetingSessionConfig, logger: logger)

        // Start Audio
        do {
            try self.currentMeetingSession?.audioVideo.start()
        } catch PermissionError.audioPermissionError {
            print("microphone permission is not granted")
        } catch {
            print(error)
        }

        // Start local video.
        DispatchQueue.background(background: { [weak self] in
            guard let this = self else { return }

            this.meetingSession?.audioVideo.startLocalVideo(source: this.customVideoSource)
            this.meetingSession?.audioVideo.addVideoTileObserver(observer: this)
            this.meetingSession?.audioVideo.addRealtimeObserver(observer: this)
        })
    }

And here is the logs if it is helpful

2023-02-13 14:14:00.480047+0100 appName[2212:535318] [INFO] Stream log: ->  - API/DefaultAudioVideoFacade/start(audioVideoConfiguration: <AmazonChimeSDK.AudioVideoConfiguration: 0x2830e1420>)
2023-02-13 14:14:00.480184+0100 appName[2212:535537] [INFO] Stream log: ->  - Torch is not available on current camera.
2023-02-13 14:14:00.480348+0100 appName[2212:535537] [INFO] Stream log: ->  - Setting simulcast
2023-02-13 14:14:00.480406+0100 appName[2212:535537] [INFO] Stream log: ->  - Starting local video with custom source and custom config
2023-02-13 14:14:00.501875+0100 appName[2212:535318] [INFO] Stream log: ->  - AudioClient State: connecting Status: ok
2023-02-13 14:14:00.502212+0100 appName[2212:535318] [INFO] Stream log: ->  - AudioClient State: finishConnecting Status: ok
2023-02-13 14:14:00.502384+0100 appName[2212:535318] [INFO] Stream log: ->  - videoClientIsConnecting
2023-02-13 14:14:00.576686+0100 appName[2212:535318] [INFO] Stream log: ->  - attendeesJoined: [<AmazonChimeSDK.AttendeeInfo: 0x283e30300>]
2023-02-13 14:14:01.173810+0100 appName[2212:535318] [INFO] Stream log: ->  - videoClientDidConnect, 0
2023-02-13 14:14:01.233746+0100 appName[2212:535318] [INFO] Stream log: ->  - videoClientCameraSendIsAvailable true
2023-02-13 14:14:01.365681+0100 appName[2212:535318] [INFO] Stream log: ->  - videoClientCameraSendIsAvailable true
2023-02-13 14:14:01.365788+0100 appName[2212:535318] [INFO] Stream log: ->  - videoClientCameraSendIsAvailable true
2023-02-13 14:14:01.432351+0100 appName[2212:535318] [INFO] Stream log: ->  - videoClientCameraSendIsAvailable true

And tested it on different iPhones from iPhone 11 to iPhone 14 pro max

and the podfile:

target 'appName' do
  # Pods for appName
  pod 'AmazonChimeSDK-No-Bitcode'

Thanks and looking forward to your response

linsang21 commented 1 year ago

Hi @rouzbeh-abadi, thanks for reporting this, currently we have an issue tracking the work for memory leak: https://github.com/aws/amazon-chime-sdk-ios/issues/216 Could you confirm if the leak you're seeing is from AmazonChimeSDKMedia?

Also what's the purpose of this CustomVideoSource seems it's simply passing the buffers. Do you see this leak when you're not using a customer video source instead?

rouzbeh-abadi commented 1 year ago

Hi @linsang21, Thanks for your response, It seems the leaks occurs in the start function. It's part of AmazonChimeSDK

/// Start AudioVideo Controller
///
/// - Throws: `PermissionError.audioPermissionError` if `RecordPermission` is not given
/// - Throws: `MediaError.audioFailedToStart` if audio client failed to start
/// - Throws: `MediaError.illegalState` if audio client is already started before calling start()
@objc func start() throws

And I can see these leaks when I use a custom VideoSource and when I don't, in both cases these leak occur.

salimkt commented 5 months ago

Still facing this issue in iOS