Browse Source

add libcallback sources

Alfonso Gamboa 4 năm trước cách đây
mục cha
commit
4444fd9ad2

+ 11 - 0
libcallback_avonly/Makefile

@@ -0,0 +1,11 @@
+# libcallback.so
+
+CC = /openmiko/build/mips-gcc472-glibc216-64bit/bin/mips-linux-uclibc-gnu-gcc
+CFLAGS = -fPIC -std=gnu99 -shared -ldl  -lm
+CC_SRCS = video_callback.c audio_callback.c
+TARGET = libcallback.so
+
+all: ${TARGET}
+
+${TARGET}: ${CC_SRCS}
+	${CC} ${CFLAGS} -o ${TARGET} ${CC_SRCS}

+ 77 - 0
libcallback_avonly/audio_callback.c

@@ -0,0 +1,77 @@
+#define _GNU_SOURCE
+#include <dlfcn.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <tinyalsa/pcm.h>
+
+struct frames_st {
+  void *buf;
+  size_t length;
+};
+typedef int (* framecb)(struct frames_st *);
+
+static uint32_t (*real_local_sdk_audio_set_pcm_frame_callback)(int ch, void *callback);
+static void *audio_pcm_cb = NULL;
+static int AudioCaptureEnable = 1;
+
+static uint32_t audio_pcm_capture(struct frames_st *frames) {
+
+  static struct pcm *pcm = NULL;
+  static int firstEntry = 0;
+  uint32_t *buf = frames->buf;
+
+  if(!firstEntry) {
+    firstEntry++;
+    unsigned int card = 0;
+    unsigned int device = 1;
+    int flags = PCM_OUT | PCM_MMAP;
+    const struct pcm_config config = {
+      .channels = 1,
+      .rate = 16000,
+      .format = PCM_FORMAT_S16_LE,
+      .period_size = 1024,
+      .period_count = 4,
+      .start_threshold = 320,
+      .silence_threshold = 0,
+      .silence_size = 0,
+      .stop_threshold = 320 * 4
+    };
+    pcm = pcm_open(card, device, flags, &config);
+    if(pcm == NULL) {
+        fprintf(stderr, "failed to allocate memory for PCM\n");
+    } else if(!pcm_is_ready(pcm)) {
+      pcm_close(pcm);
+      fprintf(stderr, "failed to open PCM\n");
+    }
+  }
+
+  if(pcm && AudioCaptureEnable) {
+    int avail = pcm_mmap_avail(pcm);
+    int delay = pcm_get_delay(pcm);
+    int ready = pcm_is_ready(pcm);
+    int err = pcm_writei(pcm, buf, pcm_bytes_to_frames(pcm, frames->length));
+    if(err < 0) fprintf(stderr, "pcm_writei err=%d\n", err);
+  }
+  return ((framecb)audio_pcm_cb)(frames);
+}
+
+uint32_t local_sdk_audio_set_pcm_frame_callback(int ch, void *callback) {
+
+  fprintf(stderr, "local_sdk_audio_set_pcm_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
+  if(ch == 0) {
+    audio_pcm_cb = callback;
+    fprintf(stderr,"enc func injection save audio_pcm_cb=0x%x\n", audio_pcm_cb);
+    callback = audio_pcm_capture;
+  }
+  return real_local_sdk_audio_set_pcm_frame_callback(ch, callback);
+}
+
+static void __attribute ((constructor)) audio_callback_init(void) {
+
+  real_local_sdk_audio_set_pcm_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_audio_set_pcm_frame_callback");
+}

BIN
libcallback_avonly/libcallback.so


+ 74 - 0
libcallback_avonly/video_callback.c

@@ -0,0 +1,74 @@
+#define _GNU_SOURCE
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <linux/videodev2.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <pthread.h>
+
+struct frames_st {
+  void *buf;
+  size_t length;
+};
+typedef int (* framecb)(struct frames_st *);
+
+static int (*real_local_sdk_video_set_encode_frame_callback)(int ch, void *callback);
+static void *video_encode_cb = NULL;
+static int VideoCaptureEnable = 1;
+
+static uint32_t video_encode_capture(struct frames_st *frames) {
+
+  static int firstEntry = 0;
+  static int v4l2Fd = -1;
+
+  if(!firstEntry) {
+    firstEntry++;
+    int err;
+    const char *v4l2_device_path = "/dev/video1";
+    fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
+    v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
+    if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
+    struct v4l2_format vid_format;
+    memset(&vid_format, 0, sizeof(vid_format));
+    vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
+    vid_format.fmt.pix.width = 1920;
+    vid_format.fmt.pix.height = 1080;
+    vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
+    vid_format.fmt.pix.sizeimage = 0;
+    vid_format.fmt.pix.field = V4L2_FIELD_NONE;
+    vid_format.fmt.pix.bytesperline = 0;
+    vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
+    err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
+    if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
+    err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
+    if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
+  }
+
+  if( (v4l2Fd >= 0) && VideoCaptureEnable) {
+    uint32_t *buf = frames->buf;
+    int size = write(v4l2Fd, frames->buf, frames->length);
+    if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
+  }
+  return ((framecb)video_encode_cb)(frames);
+}
+
+int local_sdk_video_set_encode_frame_callback(int ch, void *callback) {
+
+  fprintf(stderr, "local_sdk_video_set_encode_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
+  if(ch == 0) {
+    video_encode_cb = callback;
+    fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb);
+    callback = video_encode_capture;
+  }
+  return real_local_sdk_video_set_encode_frame_callback(ch, callback);
+}
+
+static void __attribute ((constructor)) video_callback_init(void) {
+
+  real_local_sdk_video_set_encode_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_set_encode_frame_callback");
+}