mnakada / atomcam_tools

Hack tool for atomcam and wyzecam.
Other
183 stars 22 forks source link

mp4write opportunity #31

Closed gtxaspec closed 2 years ago

gtxaspec commented 2 years ago

Thank you for your work on mp4write.c, it will solve lots of issues that we have experienced.

A question, right now the way iCamera works:

  1. iCamera writes to /tmp/XX.mp4
  2. iCamera then does mv /tmp/XX.mp4 /media/mmc/record/20220519/XX.mp4

with the modification you have in mp4write.c, now the process is:

  1. iCamera writes to /media/mmc/tmp/XX.mp4
  2. iCamera when does mv /media/mmc/tmp/XX.mp4 /media/mmc/record/20220519/XX.mp4

how can we skip step #2, so prevent additional load caused by mv moving the file? or does mv cause no load on the sdcard / cpu?

thank you for your time!!!

thematrixdev commented 2 years ago

/media/mmc should the the SD-Card as far as I understand. There is nearly no IO impact to move a file within a same media. It is just like updaing attribute in the file system.

gtxaspec commented 2 years ago

I see. I wonder if that performance also applies to NFS shares?

Most users I know including my self bind mount an NFS share at /media/mmc/record to store all videos on the server instead of locally on the camera. I can create /media/mmc/record/tmp and change the source code to point to this to also write the files here, and test performance.

gtxaspec commented 2 years ago

@mnakada do you think its a good idea to check for the directory to see if it exists, if not, create the directory?

something like (example code, havent tested):

  const char* folder;
    folder = "/media/mmc/record/tmp";
    struct stat sb;

   printf("[command] mp4write.c: checking for temporary record directory\n");

    if (stat(folder, &sb) == 0 && S_ISDIR(sb.st_mode)) {
      printf("[command] mp4write.c: temporary directory exists.\n");
    } else {
      printf("[command] mp4write.c: directory missing, creating directory\n");
      mkdir("/media/mmc/record/tmp", 0700);
    }
gtxaspec commented 2 years ago

I have modified the mp4write also to add on/off function and folder detection, perhaps its useful?

#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

static int (*original_mp4write_start_handler)(void *handler, char *file, void *config);

static int mp4WriteEnable = 0;

char *mp4Write(int fd, char *tokenPtr) {

  char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  if(!p) return mp4WriteEnable ? "on" : "off";
  if(!strcmp(p, "on")) {
    mp4WriteEnable = 1;
    fprintf(stderr, "[command] mp4write on\n", p);
    return "ok";
  }
  if(!strcmp(p, "off")) {
    mp4WriteEnable = 0;
    fprintf(stderr, "[command] mp4write off\n", p);
    return "ok";
  }
  return "error in mp4write.c";
}

int mp4write_start_handler(void *handler, char *file, void *config, char *tokenPtr) {

if(mp4WriteEnable) {

  const char* folder;
    folder = "/media/mmc/record/tmp";
    struct stat sb;

   printf("[command] mp4write.c: checking for temporary record directory\n");

    if (stat(folder, &sb) == 0 && S_ISDIR(sb.st_mode)) {
    printf("[command] mp4write.c: temporary directory exists.\n");
    } else {
      printf("[command] mp4write.c: directory missing, creating directory\n");
      mkdir("/media/mmc/record/tmp", 0700);
    }

  if(!strncmp(file, "/tmp/", 5)) {
    char buf[64];
    strncpy(buf, file + 5, 30);
    strcpy(file, "/media/mmc/record/tmp/");
    strcat(file, buf);
  }
}
  return (original_mp4write_start_handler)(handler, file, config);
}

static void __attribute ((constructor)) mp4write_init(void) {

  original_mp4write_start_handler = dlsym(dlopen("/system/lib/libmp4rw.so", RTLD_LAZY), "mp4write_start_handler");
}
mnakada commented 2 years ago

The purpose of moving /tmp/*.mp4 to SD-Card is not to reduce the write or mv load.

The tmpfs allocates memory from the heap area, so it is shared with each process, and when it runs out of memory, it is swapped out.

This system is prone to thrashing when large files such as mp4 are generated in /tmp because of its low memory.

Moving the destination for writing mp4 files to SD-Card prevents thrashing.

If output to SD-Card is on, the load on mv is simply to move the VFAT32FS directory-entry.

If output to SD-Card is off and there is mv or cp to CIFS or NAS, the load will be higher than tmpfs, but in the current system configuration, it is better to output to SD-Card because there is not enough memory and it is likely to be swapped out.

mnakada commented 2 years ago

@mnakada do you think its a good idea to check for the directory to see if it exists, if not, create the directory?

In atomcam_tools, the /media/mmc/tmp directory is created in /etc/init.d/S38atomcam at startup and the files inside are deleted.

gtxaspec commented 2 years ago

ah yes, thank you for the detailed explanation!