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

📄 main.c

📁 采集视频信号进行H264压缩并通过UDP传送
💻 C
字号:
/*
 * main.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.
 * ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>

#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>

#include "encodedecode.h"
#include "global.h"

#ifdef __DEBUG
#define TRACE_MASK "**=4567"
#else
#define TRACE_MASK "**=567"
#endif

enum InitLevels {
    VIDEOTHREADCREATED = 1,
};

/* Global variable declarations for this application */
pthread_t       encodeThread;
pthread_t   	decodeThread;
pthread_t 	videoThread;
pthread_t       videosendThread;
pthread_t       videorecvThread;

pthread_cond_t videoInit;
pthread_cond_t  encodeInit;
pthread_cond_t  decodeInit;
pthread_cond_t  allInit;
pthread_cond_t  networkInit;

pthread_mutex_t videoMutex;
pthread_mutex_t encodeMutex;
pthread_mutex_t decodeMutex;
pthread_mutex_t allMutex;
pthread_mutex_t networkMutex;

pthread_mutex_t dataMutex;

GlobalData      gbl = { 0, 1, 0, 0, NTSC };


/******************************************************************************
 * usage
 ******************************************************************************/
static void usage(void)
{
    printf("Usage: encodedecode [options]\n\n"
           "Options:\n"
           "-b | --bitrate     encoder bitrate\n"
	   "-a | --address     Remote client address\n"
	   "-r | --resolution  Resolution of demo (CIF, D1 or ZOOM) [D1]\n"
           "-t | --time        Number of seconds to run the demo [infinite]\n"
           "-i | --interface   Launch the demo interface when exiting [off]\n"
           "-h | --help        Print this message\n\n");
}

/******************************************************************************
 * parseArgs
 ******************************************************************************/
static void parseArgs(int argc, char *argv[], DemoEnv *envp)
{
    const char shortOptions[] = "b:a:r:t:ih";
    const struct option longOptions[] = {
	{"bitrate",required_argument, NULL, 'b'},
	{"address", required_argument, NULL, 'a'},
	{"resolution", required_argument, NULL, 'r'},
        {"time", required_argument, NULL, 't'},
        {"interface", no_argument, NULL, 'i'},
        {"help", no_argument, NULL, 'h'},
        {0, 0, 0, 0}
    };
    int     index;
    int     c;

    envp->bitrate=0;
    for (;;) {

        c = getopt_long(argc, argv, shortOptions, longOptions, &index);
        if (c == -1) {
            break;
        }
        switch (c) {
            case 0:
                break;
   		
 	    case 'b':
		envp->bitrate=atoi(optarg);
		break;
 
	    case 'a':
		envp->address=optarg;
	        break;
        
	    case 'r':
                if (strcmp(optarg, "D1") == 0) {
                    envp->resolution = D1;
                }
                else if (strcmp(optarg, "CIF") == 0) {
                    envp->resolution = CIF;
                }
                else if (strcmp(optarg, "ZOOM") == 0) {
                    envp->resolution = ZOOM;
                }
                else {
                    ERR("Invalid resolution supplied (%s)\n\n", optarg);
                    usage();
                    exit(EXIT_FAILURE);
                }
                break;

            case 't':
                envp->time = atoi(optarg);
                break;

            case 'i':
                envp->interface = TRUE;
                break;

            case 'h':
                usage();
                exit(EXIT_SUCCESS);

            default:
                usage();
                exit(EXIT_FAILURE);
        }
    }

}

QUE_handle send_queue=NULL;
QUE_handle recv_queue=NULL;

void buffer_init()
{
    if((send_queue=queue_init(8,D1BUFLEN))==NULL)
    {
        DBG("alloc send_queue error\n");
        return;
    }
    if((recv_queue=queue_init(8,D1BUFLEN))==NULL)
    {
        DBG("alloc recv_queue error\n");
        return;
    }
    return;
}
/******************************************************************************
 * main
 ******************************************************************************/
int main(int argc, char *argv[])
{
    enum InitLevels initLevel = 0;
    int             status    = EXIT_SUCCESS;
    DemoEnv         env       = {
        FOREVER,
        FALSE,
        D1
    };
    /* Parse the arguments given to the app and set the app environment */
       gbl.yFactor = NTSC;
	
    parseArgs(argc, argv, &env);
    printf("Encodedecode demo started.\n");

    /* Initialize Codec Engine runtime first */
    CERuntime_init();

    /* Initialize CE tracing and enable it for all modules (except GL) */
 //   GT_set(TRACE_MASK);

    /* Initialize the mutexes */
    pthread_mutex_init(&encodeMutex, NULL);
    pthread_mutex_init(&decodeMutex, NULL);
    pthread_mutex_init(&allMutex, NULL);
    pthread_mutex_init(&dataMutex, NULL);

    /* Initialize the conditionals */
    pthread_cond_init(&encodeInit, NULL);
    pthread_cond_init(&decodeInit, NULL);
    pthread_cond_init(&allInit, NULL);
 
    //buffer_init();
#if 1 
    /* Start the encode thread */
#if 1
    DBG("ENCODE THREAD CREATE\n");
    if (pthread_create(&encodeThread, NULL, encodeThrFxn, (void *) &env)) {
        ERR("Failed to create encode thread\n");
        CLEANUP(EXIT_FAILURE);
    }
#endif
#endif
usleep(300000);
    initLevel = VIDEOTHREADCREATED;
sleep(1);
     ctrlThrFxn(&env);
cleanup:
/* Wait until the other threads terminate */
    if (initLevel >= VIDEOTHREADCREATED) {
        pthread_join(encodeThread, NULL);
	pthread_join(decodeThread, NULL);
    }

    /* Clean up the mutexes and conditions */
    pthread_mutex_destroy(&dataMutex);
    pthread_mutex_destroy(&allMutex);
    pthread_mutex_destroy(&encodeMutex);
    pthread_mutex_destroy(&decodeMutex);

    pthread_cond_destroy(&allInit);
    pthread_cond_destroy(&encodeInit);
    pthread_cond_destroy(&decodeInit);

    if (status == EXIT_SUCCESS && env.interface) {
        if (execl("./interface", "interface", "-l 1", NULL) == -1) {
            status = EXIT_FAILURE;
        }
    }

    exit(status);
}

⌨️ 快捷键说明

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