有时候,自己懒得封装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'
参考