green-code-initiative / ecoCode

Reduce the environmental footprint of your software programs with SonarQube
https://ecocode.io
GNU General Public License v3.0
160 stars 78 forks source link

[EC515] Swift port #310

Closed zippy1978 closed 5 months ago

zippy1978 commented 5 months ago

Creation of an AVAudioRecorder or AVCaptureSession object is used to record audio and video. These classes have methods to stop recording and release resources.

In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to properly stop and release these objects if they are no longer needed may also lead to continuous battery consumption for mobile devices.

Noncompliant Code Examples

import AVFoundation

var audioRecorder: AVAudioRecorder?

func startRecording() {
    let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000,
        AVNumberOfChannelsKey: 1,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

    do {
        audioRecorder = try AVAudioRecorder(url: getDocumentsDirectory().appendingPathComponent("recording.m4a"), settings: settings)
        audioRecorder?.record()
    } catch {
        // Handle error
    }
}
import AVFoundation

var captureSession: AVCaptureSession?

func startVideoRecording() {
    captureSession = AVCaptureSession()
    captureSession?.beginConfiguration()

    let videoDevice = AVCaptureDevice.default(for: .video)
    let audioDevice = AVCaptureDevice.default(for: .audio)

    do {
        let videoInput = try AVCaptureDeviceInput(device: videoDevice!)
        let audioInput = try AVCaptureDeviceInput(device: audioDevice!)

        if (captureSession?.canAddInput(videoInput) ?? false) {
            captureSession?.addInput(videoInput)
        }

        if (captureSession?.canAddInput(audioInput) ?? false) {
            captureSession?.addInput(audioInput)
        }

        captureSession?.commitConfiguration()
        captureSession?.startRunning()
    } catch {
        // Handle error
    }
}

Compliant Solutions

import AVFoundation

var audioRecorder: AVAudioRecorder?

func startRecording() {
    let settings = [
        AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
        AVSampleRateKey: 12000,
        AVNumberOfChannelsKey: 1,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

    do {
        audioRecorder = try AVAudioRecorder(url: getDocumentsDirectory().appendingPathComponent("recording.m4a"), settings: settings)
        audioRecorder?.record()
    } catch {
        // Handle error
    }
}

func stopRecording() {
    if let recorder = audioRecorder, recorder.isRecording {
        recorder.stop()
        audioRecorder = nil
    }
}
import AVFoundation

var captureSession: AVCaptureSession?

func startVideoRecording() {
    captureSession = AVCaptureSession()
    captureSession?.beginConfiguration()

    let videoDevice = AVCaptureDevice.default(for: .video)
    let audioDevice = AVCaptureDevice.default(for: .audio)

    do {
        let videoInput = try AVCaptureDeviceInput(device: videoDevice!)
        let audioInput = try AVCaptureDeviceInput(device: audioDevice!)

        if (captureSession?.canAddInput(videoInput) ?? false) {
            captureSession?.addInput(videoInput)
        }

        if (captureSession?.canAddInput(audioInput) ?? false) {
            captureSession?.addInput(audioInput)
        }

        captureSession?.commitConfiguration()
        captureSession?.startRunning()
    } catch {
        // Handle error
    }
}

func stopVideoRecording() {
    if let session = captureSession, session.isRunning {
        session.stopRunning()
        captureSession = nil
    }
}