Deinitialize the Camera

Hello everyone,

I’m working on a project that uses a Realtek-based camera module for both RTSP streaming and local recording. I need to run these features one at a time, and my goal is to fully deinitialize the camera after either streaming or recording so that I can reliably switch between modes.

Here’s what I’m facing:

  • After stopping streaming or recording, the camera doesn’t reinitialize correctly when I try to switch to the other mode.
  • I’m looking for the recommended procedure to properly deinitialize the camera. Are there specific reset routines or additional steps that I should follow to ensure a clean shutdown of the current mode?
  • Is it possible to toggle between streaming and recording without performing a full system reset?

Any insights, code examples, or references to relevant documentation would be greatly appreciated.

Thank you in advance for your help!

Hi @amirsohel059,

Is it possible to share your existing Arduino sketch?

Thank you.

Thanks for you reply.
it is not possible to share full code but i have added part of code whre im initilizing and deinitilizing the camera and what issues im facing

Here is the code

// ============================================================================
// software Configuration for Streaming
// ============================================================================

void startStreamingSession() {
lastActivityTime = millis();
Serial.println(“\n===== Starting Streaming Session =====”);
if (isRecording) {
Serial.println(“\n[Streaming] recording is active. Skipping Streaming.”);
return; // Skip the recording session if streaming is already active
}
setupStreamingHardware();
isStreaming = true;
Serial.println(“[Streaming] Live streaming started.”);
}

void stopStreamingSession() {
if (!isStreaming) {
Serial.println(“\n[streaming] Streaming is not active. Skipping teardown.”);
return; // Skip
}
Serial.println(“\n===== Stopping Live Streaming Session =====”);
teardownStreamingHardware();
isStreaming = false;
Serial.println(“[Streaming] Live streaming stopped.”);
delay(1000);

}

// ============================================================================
// Hardware Configuration for Streaming
// ============================================================================

void setupStreamingHardware() {
Serial.println(“[Streaming] Initializing hardware for live streaming…”);
Camera.configVideoChannel(CHANNEL, configV);
Camera.videoInit();
delay(100);
MMFModule camStream = Camera.getStream(CHANNEL);

audio.configAudio(configA);
audio.begin();
aac.configAudio(configA);
aac.begin();

rtsp.configVideo(configV);
rtsp.configAudio(configA, CODEC_AAC);
rtsp.begin();

audioStreamer.end();
avMixStreamer.end();

audioStreamer.registerInput(audio);
audioStreamer.registerOutput(aac);
if (audioStreamer.begin() != 0) {
Serial.println(“[Streaming] Audio StreamIO link start failed!”);
} else {
Serial.println(“[Streaming] Audio StreamIO link started.”);
}

avMixStreamer.registerInput1(camStream);
avMixStreamer.registerInput2(aac);
avMixStreamer.registerOutput(rtsp);
if (avMixStreamer.begin() != 0) {
Serial.println(“[Streaming] AV Mix StreamIO link start failed!”);
} else {
Serial.println(“[Streaming] AV Mix StreamIO link started.”);
}

Camera.channelBegin(CHANNEL);
IPAddress ip = WiFi.localIP();
Serial.print(“[Streaming] RTSP URL: rtsp://”);
Serial.print(ip);
Serial.print(“:”);
Serial.println(rtsp.getPort());
Serial.println(“[Streaming] Streaming hardware initialization complete.”);

// String rtspURL = “rtsp://” + ip.toString() + “:” + String(rtsp.getPort());
String rtspURL = “rtsp://” + String(ip[0]) + “.” + String(ip[1]) + “.” + String(ip[2]) + “.” + String(ip[3]) + “:” + String(rtsp.getPort());

// Publishing MQTT message after successful connection
String mqttMessage = rtspURL;
// mqttClient.publish(MQTT_STATUS_TOPIC, mqttMessage.c_str());
mqttClient.publish(mqttstatusTopic, mqttMessage.c_str());

Serial.print("[Streaming] RTSP URL: ");
Serial.println(rtspURL); // This will print the RTSP URL
}

void teardownStreamingHardware() {
Serial.println(“[Streaming] Tearing down streaming hardware…”);
rtsp.end();
audioStreamer.end();
avMixStreamer.end();
Camera.channelEnd(CHANNEL);
aac.end();
audio.end();
delay(500);
Serial.println(“[Streaming] Streaming hardware stopped.”);
}

// ============================================================================
// Software Configuration for Recording
// ============================================================================

void startRecordingSession() {
lastActivityTime = millis();
Serial.println(“\n===== Starting Recording Session =====”);
if (isStreaming) {
// Serial.println(“[Recording] Stopping active streaming before starting recording…”);
// stopStreamingSession();
Serial.println(“\n[Recording] Streaming is active. Skipping recording.”);
return; // Skip the recording session if streaming is already active
}
setupRecordingHardware();
recordingBaseFileName = generateFileName();
nextSegmentToUpload = 0;
Serial.print("[Recording] Base file name: ");
Serial.println(recordingBaseFileName);

mp4.setRecordingFileName(recordingBaseFileName.c_str());
mp4.setRecordingDuration(RECORDING_DURATION);
mp4.setRecordingFileCount(1000);
mp4.begin();
Serial.println(“[Recording] MP4 recording started.”);
isRecording = true;
}

void stopRecordingSession() {
if (!isRecording) {
Serial.println(“\n[Recording] recording is not active. Skipping teardown.”);
return; // Skip
}
Serial.println(“\n===== Stopping Recording Session =====”);
mp4.end();
teardownRecordingHardware();
isRecording = false;
Serial.println(“[Recording] MP4 recording stopped.”);
// Before rebooting, store the upload info so pending files can be resumed
storeLastUploadInfo();
delay(1000);
}

// ============================================================================
// Hardware Configuration for Recording
// ============================================================================
void setupRecordingHardware() {
Serial.println(“[Recording] Initializing hardware for recording…”);
Camera.configVideoChannel(CHANNEL, configV);
Camera.videoInit();
delay(100); // Allow camera to stabilize
MMFModule camStream = Camera.getStream(CHANNEL);

audio.configAudio(configA);
audio.begin();
aac.configAudio(configA);
aac.begin();

mp4.configVideo(configV);
mp4.configAudio(configA, CODEC_AAC);

audioStreamer.end();
avMixStreamer.end();

audioStreamer.registerInput(audio);
audioStreamer.registerOutput(aac);
if (audioStreamer.begin() != 0) {
Serial.println(“[Recording] Audio StreamIO link start failed!”);
} else {
Serial.println(“[Recording] Audio StreamIO link started.”);
}

avMixStreamer.registerInput1(camStream);
avMixStreamer.registerInput2(aac);
avMixStreamer.registerOutput(mp4);
if (avMixStreamer.begin() != 0) {
Serial.println(“[Recording] AV Mix StreamIO link start failed!”);
} else {
Serial.println(“[Recording] AV Mix StreamIO link started.”);
}

Camera.channelBegin(CHANNEL);
Serial.println(“[Recording] Recording hardware initialization complete.”);
}

void teardownRecordingHardware() {
Serial.println(“[Recording] Tearing down recording hardware…”);
mp4.end();
audioStreamer.end();
avMixStreamer.end();
Camera.channelEnd(CHANNEL);
aac.end();
audio.end();
delay(500); // Allow flush of pending frames
Serial.println(“[Recording] Recording hardware stopped.”);
}
termianl output

[VID Wrn]

CH 1 MMF ENC Queue full

I have shared Please check

Hi @amirsohel059,

Thanks for your reply, can I check if you tried increasing the delay between the toggling of different modes between streaming and recording?

Thank you.

1 Like

Hi @Kelvin_Huang ,

Thanks for your input!

Yes, I’ve already added delays between switching modes and included conditions to ensure that streaming and recording don’t run simultaneously.

I’m building a simple CCTV camera that can perform either RTSP streaming or local recording — one at a time. However, I’m still facing issues when switching between the two. I’m not sure if the method I’m using for deinitialization and reinitialization is correct.

Could you please share an example or point me to any documentation that outlines the proper sequence for cleanly shutting down and restarting the camera in a different mode?

Thanks again for your help!

I am able to switch in between recordign and streaming but not sure fi the camera is really getting close

Hi @amirsohel059

There will be a API for init and deinint in next release. Allow me some time and will provide some test patches.

Thanks for sharing the example. The new early release version is out today.

for switching the mode, with recording and streaming. Yes, Camera.channelEnd(CHANNEL); is not turn off the camera. Recording and streaming should keep camera on as there is continuously using the camera. videoDeinitis the actual off camera function.

please refer to my following test code for deinit camera test

#include “WiFi.h”
#include “StreamIO.h”
#include “VideoStream.h”
#include “RTSP.h”

#define CHANNEL 0

// Default preset configurations for each video channel:
// Channel 0 : 1920 x 1080 30FPS H264
// Channel 1 : 1280 x 720  30FPS H264
// Channel 2 : 1280 x 720  30FPS MJPEG

VideoSetting config(CHANNEL);
RTSP rtsp;
StreamIO videoStreamer(1, 1);    // 1 Input Video → 1 Output RTSP

char ssid
 = “Network_SSID5”;    // your network SSID (name)
char pass
 = “Password”;        // your network password
int status = WL_IDLE_STATUS;

const int pin_stop = 9;
const int pin_start = 11;

int pin_state = 0;

int end_check = 0;
int resrart_check = 0;

void setup()
{
pinMode(pin_stop, INPUT);
pinMode(pin_start, INPUT);

Serial.begin(115200);

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);

    // wait 2 seconds for connection:
    delay(2000);
}

// Configure camera video channel with video format information
// Adjust the bitrate based on your WiFi network quality
// config.setBitrate(2 * 1024 * 1024);     // Recommend to use 2Mbps for RTSP streaming to prevent network congestion
Camera.configVideoChannel(CHANNEL, config);
Camera.videoInit();

// Configure RTSP with identical video format information
rtsp.configVideo(config);
rtsp.begin();

// Configure StreamIO object to stream data from video channel to RTSP
videoStreamer.registerInput(Camera.getStream(CHANNEL));
videoStreamer.registerOutput(rtsp);
if (videoStreamer.begin() != 0) {
    Serial.println("StreamIO link start failed");
}

// Start data stream from video channel
Camera.channelBegin(CHANNEL);

delay(1000);
printInfo();

}

void loop()
{
pin_state = digitalRead(pin_stop);

if (pin_state == HIGH) { // stop
    if (end_check != 1) {
        videoStreamer.end();
        Camera.channelEnd(CHANNEL);
        Camera.videoDeinit(CHANNEL);
        Serial.println("End camera");
        end_check = 1;
        resrart_check = 0;
    }
} else {
    pin_state = digitalRead(pin_start);
    if (pin_state == HIGH) { // restart
        if (resrart_check != 1) {
            Camera.configVideoChannel(CHANNEL, config);
            Camera.videoInit();
            // Configure StreamIO object to stream data from video channel to RTSP
            videoStreamer.registerInput(Camera.getStream(CHANNEL));
            videoStreamer.registerOutput(rtsp);
            if (videoStreamer.begin() != 0) {
                Serial.println("StreamIO link start failed");
            }

            Camera.channelBegin(CHANNEL);
            Serial.println(" Restart Camera");
            resrart_check = 1;
            end_check = 0;
        }
    }
}

}

void printInfo(void)
{
Serial.println(" “);
Serial.println(”------------------------------“);
Serial.println(”- Summary of Streaming -“);
Serial.println(”------------------------------");
Camera.printInfo();

IPAddress ip = WiFi.localIP();

Serial.println("- RTSP -");
rtsp.printInfo(ip.get_address());

}