用ffmpeg的socket接口

有时候,自己懒得封装socket接口,可以自己用ffmpeg里面的,代码如下:

server端代码:


int main(int argc, char* argv[])
{
    AVDictionary *options = NULL;
    AVIOContext *client = NULL, *server = NULL;
    URLContext *puc = NULL;
    const char *in_uri, *out_uri;
    int ret, pid;
    av_log_set_level(AV_LOG_TRACE);
    if (argc < 3) {
        printf("usage: %s input http://hostname[:port]\n"
               "API example program to serve http to multiple clients.\n"
               "\n", argv[0]);
        return 1;
    }

    in_uri = argv[1];
    out_uri = argv[2];

    av_register_all();

    avformat_network_init();

    ret = ffurl_open(&puc, in_uri, AVIO_FLAG_READ, NULL, NULL);
    if (ret < 0) {
        printf("ffurl open error\n");
        return -1;
    }

    printf("ffurl open success\n");

    char buffer[128];
    int buffer_size = 128;
    while(1)
    {
        memset(&buffer, 0, buffer_size);
        ret = ffurl_read(puc, (unsigned char*)buffer, buffer_size);
        if (ret < 0)
        {
            printf("ffurl read error, ret = %d\n", ret);
            return -1;
        }

        printf("recv %s\n", buffer);
    }
    
    getchar();

    return 0;
}

client端代码:


int main(int argc, char* argv[])
{

    AVDictionary *options = NULL;
    AVIOContext *client = NULL, *server = NULL;
    URLContext *puc = NULL;
    const char *in_uri, *out_uri;
    int ret, pid;
    av_log_set_level(AV_LOG_TRACE);
    if (argc < 3) {
        printf("usage: %s input http://hostname[:port]\n"
               "API example program to serve http to multiple clients.\n"
               "\n", argv[0]);
        return 1;
    }

    in_uri = argv[1];
    out_uri = argv[2];

    av_register_all();

    avformat_network_init();

    ret = ffurl_open(&puc, in_uri, AVIO_FLAG_WRITE, NULL, NULL);
    if (ret < 0) {
        printf("ffurl open error, ret = %d\n", ret);
        return -1;
    }

    printf("ffurl open success\n");

    char buffer[128];
    int buffer_size = 128;
    int count = 1;
    while(1)
    {
        memset(&buffer, 0, buffer_size);
        snprintf(buffer,127, "hell world -- %lld", count++);
        ret = ffurl_write(puc, (unsigned char*)buffer, strlen(buffer));
        if (ret < 0)
        {
            printf("ffurl read error, ret = %d\n", ret);
            return -1;
        }

        //printf("recv %s\n", buffer);
        av_usleep(1000*1000);
    }
    
}

如果出现

undefined reference to `ffurl_read'

参考

ffmpeg库导出自己需要的函数

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值