⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctrl.c

📁 采集视频信号进行H264压缩并通过UDP传送
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * ctrl.c
 *
 * ============================================================================
 * Copyright (c) Texas Instruments Inc 2005
 *
 * Use of this software is controlled by the terms and conditions found in the
 * license agreement under which this software has been supplied or provided.
 * ============================================================================
 */

/* Standard Linux headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <pthread.h>

#include <linux/fb.h>

/* Davinci EVM utility libraries */
#include <msp430lib.h>

/* Application header files */
#include "encodedecode.h"
#include "ui.h"

/* Custom Davinci FBDEV ioctl */
#define FBIO_WAITFORVSYNC   _IOW('F', 0x20, u_int32_t)

/* Maximum size of the /proc/stat and /proc/[pid]/stat files. */
#define TEXT_BUF_SIZE    1024

/* The 0-7 transparency value to use for the OSD */
#define OSD_TRANSPARENCY 0x55

/* The levels of initialization */
enum InitLevels {
    OSDINITIALIZED = 1,
    ENGINEOPENED,
    MSP430LIBINITIALIZED,
    UICREATED
};

/* Local function prototypes */
static int  setOsdBuffer(int osdFd, int idx);
static int  osdInit(char *displays[]);
static int  osdExit(int osdFd);
static int  setOsdTransparency(unsigned char trans);
static int  flipOsdBuffers(int fd, int displayIdx);
static int  getArmCpuLoad(int *procLoad, int *cpuLoad);
static int  keyAction(enum msp430lib_keycode key, int displayIdx,
                      int *osdTransPtr);
static void drawDynamicData(Engine_Handle hEngine, int osdFd, int *time,
                            int *displayIdx, int *workingIdx);

/******************************************************************************
 * setOsdBuffer
 ******************************************************************************/
static int setOsdBuffer(int osdFd, int idx)
{
    struct fb_var_screeninfo vInfo;

    if (ioctl(osdFd, FBIOGET_VSCREENINFO, &vInfo) < -1) {
        ERR("Failed FBIOGET_VSCREENINFO (%s)\n", strerror(errno));
        return FAILURE;
    }
  
    vInfo.yoffset = vInfo.yres * idx;
    if (ioctl(osdFd, FBIOPAN_DISPLAY, &vInfo) < -1) {
        ERR("Failed FBIOPAN_DISPLAY (%s)\n", strerror(errno));
        return FAILURE;
    }

    return SUCCESS;
}

/******************************************************************************
 * osdInit
 ******************************************************************************/
static int osdInit(char *displays[])
{
    struct fb_var_screeninfo varInfo;
    int                      fd;
    int                      size;

    fd = open(OSD_DEVICE, O_RDWR);

    if (fd == -1) {
        ERR("Failed to open fb device %s\n", OSD_DEVICE);
        return FAILURE;
    }

    if (ioctl(fd, FBIOGET_VSCREENINFO, &varInfo) == -1) {
        ERR("Failed ioctl FBIOGET_VSCREENINFO on %s\n", OSD_DEVICE);
        return FAILURE;
    }

    /* Try the requested size */
    varInfo.xres = OSD_WIDTH;
    varInfo.yres = OSD_HEIGHT;
    varInfo.bits_per_pixel = OSD_BPP;

    if (ioctl(fd, FBIOPUT_VSCREENINFO, &varInfo) < -1) {
        ERR("Failed ioctl FBIOPUT_VSCREENINFO on %s\n", OSD_DEVICE);
        return FAILURE;
    }

    if (varInfo.xres != OSD_WIDTH ||
        varInfo.yres != OSD_HEIGHT ||
        varInfo.bits_per_pixel != OSD_BPP) {
        ERR("Failed to get the requested screen size: %dx%d at %d bpp\n",
            OSD_WIDTH, OSD_HEIGHT, OSD_BPP);
        return FAILURE;
    }

    size = varInfo.xres * varInfo.yres * varInfo.bits_per_pixel / 8;

    displays[0] = (char *) mmap(NULL, size * NUM_BUFS,
                                   PROT_READ | PROT_WRITE,
                                   MAP_SHARED, fd, 0);

    if (displays[0] == MAP_FAILED) {
        ERR("Failed mmap on %s\n", OSD_DEVICE);
        return FAILURE;
    }

    displays[1] = displays[0] + size;

    setOsdBuffer(fd, 0);

    return fd;
}

/******************************************************************************
 * osdExit
 ******************************************************************************/
static int osdExit(int osdFd)
{
    setOsdBuffer(osdFd, 0);
    close(osdFd);

    return SUCCESS;
}

/******************************************************************************
 * setOsdTransparency
 ******************************************************************************/
static int setOsdTransparency(unsigned char trans)
{
    struct fb_var_screeninfo vInfo;
    unsigned short          *attrDisplay;
    int                      attrSize;
    int                      fd;

    fd = open(ATTR_DEVICE, O_RDWR);

    if (fd == -1) {
        ERR("Failed to open attribute window %s\n", ATTR_DEVICE);
        return FAILURE;
    }

    if (ioctl(fd, FBIOGET_VSCREENINFO, &vInfo) == -1) {
        ERR("Error reading variable information.\n");
        return FAILURE;
    }

    /* One nibble per pixel */
    attrSize = vInfo.xres_virtual * vInfo.yres / 2; 

    attrDisplay = (unsigned short *) mmap(NULL, attrSize,
                                          PROT_READ | PROT_WRITE,
                                          MAP_SHARED, fd, 0);
    if (attrDisplay == MAP_FAILED) {
        ERR("Failed mmap on %s\n", ATTR_DEVICE);
        return FAILURE;
    }

    /* Fill the window with the new attribute value */
    memset(attrDisplay, trans, attrSize);

    munmap(attrDisplay, attrSize);
    close(fd);

    return SUCCESS;
}

/******************************************************************************
 * flipOsdBuffers
 ******************************************************************************/
static int flipOsdBuffers(int fd, int displayIdx)
{
    int                      dummy;

    setOsdBuffer(fd, displayIdx);

    /* Wait for vertical sync */
    if (ioctl(fd, FBIO_WAITFORVSYNC, &dummy) == -1) {
        ERR("Failed FBIO_WAITFORVSYNC (%s)\n", strerror(errno));
        return FAILURE;
    }

    return SUCCESS;
}

/******************************************************************************
 * getArmCpuLoad
 ******************************************************************************/
static int getArmCpuLoad(int *procLoad, int *cpuLoad)
{
    static char          textBuf[TEXT_BUF_SIZE];
    static unsigned long prevIdle = 0;
    static unsigned long prevTotal = 0;
    static unsigned long prevProc = 0;

    char                *var, *val;
    FILE                *fptr;
    unsigned long        user, nice, sys, idle, total, proc;
    unsigned long        uTime = 0;
    unsigned long        sTime = 0;
    unsigned long        cuTime = 0;
    unsigned long        csTime = 0;
    unsigned long        deltaTotal, deltaIdle, deltaProc;
    int                  i;

    char pidpath[sizeof("/proc/") + 10 + sizeof("/stat")];

    /* Read the overall system information */
    fptr = fopen("/proc/stat", "r");

    if (fptr == NULL) {
        ERR("/proc/stat not found. Is the /proc filesystem mounted?\n");
        return FAILURE;
    }

    /* Scan the file line by line */
    while (fgets(textBuf, sizeof(textBuf), fptr) != NULL) {
        var = strtok(textBuf, " \t\n");
        if (!var) {
            fclose(fptr);
            return FAILURE;
        }
        val = strtok(NULL, "\n");
        if (!val) {
            fclose(fptr);
            return FAILURE;
        }

        if (strcmp(var, "cpu") == 0) {
            if (sscanf(val, "%lu %lu %lu %lu",
                       &user, &nice, &sys, &idle) == EOF) {
                fclose(fptr);
                return FAILURE;
            }
            break;
        }
    }

    if (fclose(fptr) != 0) {
        return FAILURE;
    }

    /* Read the current process information */
    sprintf(pidpath, "/proc/%d/stat", getpid());

    fptr = fopen(pidpath, "r");

    if (fptr == NULL) {
        ERR("/proc/%d/stat not found. Is the /proc filesystem mounted?\n",
            getpid()); 
        return FAILURE;
    }

    /* File should only be one line */
    while (fgets(textBuf, sizeof(textBuf), fptr) != NULL) {
        val = strtok(textBuf, " ");
        if (!val) {
            fclose(fptr);
            return FAILURE;
        }
        for (i=0; i<12; i++) {
            val = strtok(NULL, " ");
            if (!val) {
                fclose(fptr);
                return FAILURE;
            }
        }

        val = strtok(NULL, " ");
        if (!val) {
            fclose(fptr);
            return FAILURE;
        }
        uTime = atoi(val);

        val = strtok(NULL, " ");
        if (!val) {
            fclose(fptr);
            return FAILURE;
        }
        sTime = atoi(val);

        val = strtok(NULL, " ");
        if (!val) {
            fclose(fptr);
            return FAILURE;
        }
        cuTime = atoi(val);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -