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!