commit ca0a7963b4b46576bc9eec8e40a481ffe18aaa93 Author: cbcao Date: Sun Nov 6 18:41:02 2016 +0800 init diff --git a/compile_gcc.sh b/compile_gcc.sh new file mode 100755 index 0000000..6806f61 --- /dev/null +++ b/compile_gcc.sh @@ -0,0 +1,4 @@ +#! /bin/sh +gcc simplest_ffmpeg_mem_player.cpp -g -o simplest_ffmpeg_mem_player.out -lstdc++ \ +-I /usr/local/include -L /usr/local/lib -lSDLmain -lSDL -lavformat -lavcodec -lavutil -lswscale + diff --git a/doctor_strange.flv b/doctor_strange.flv new file mode 100644 index 0000000..e69de29 diff --git a/simplest_ffmpeg_mem_player.cpp b/simplest_ffmpeg_mem_player.cpp new file mode 100755 index 0000000..6e31944 --- /dev/null +++ b/simplest_ffmpeg_mem_player.cpp @@ -0,0 +1,227 @@ +/** + * 本程序实现对内存中数据的读写并播放 + * 2016-11-03 + * + */ + +#include + +#define __STDC_CONSTANT_MACROS + +#ifdef _WIN32 +//Windows +extern "C" +{ +#include "libavcodec/avcodec.h" +#include "libavformat/avformat.h" +#include "libswscale/swscale.h" +#include "SDL/SDL.h" +}; +#else +//Linux... +#ifdef __cplusplus +extern "C" +{ +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __cplusplus +}; +#endif +#endif +#define MAXBUF 1024 +//Output YUV420P +#define OUTPUT_YUV420P 0 +//FILE *fp_open=NULL; +int sockfd; + +//Callback +int read_buffer(void *opaque, uint8_t *buf, int buf_size){ + /* 接收对方发过来的文件*/ + int true_size; + if (true_size=recv(sockfd,buf,buf_size,0)){ + return true_size; + }else{ + return -1; + } + +} + +int main(int argc, char* argv[]) +{ + //socket + struct sockaddr_in6 dest; // IPv6 + + if (argc != 3) { + printf + ("参数格式错误!正确用法如下:\n\t\t%s IP地址 端口\n\t比如:\t%s 127.0.0.1 80\n此程序用来从某个 IP 地址的服务器某个端口接收最多 MAXBUF 个字节的消息", + argv[0], argv[0]); + exit(0); + } + if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) { // IPv6 + perror("Socket"); + exit(errno); + } + printf("socket created\n"); + + /* 初始化服务器端(对方)的地址和端口信息 */ + bzero(&dest, sizeof(dest)); + dest.sin6_family = AF_INET6; // IPv6 + dest.sin6_port = htons(atoi(argv[2])); // IPv6 + if ( inet_pton(AF_INET6, argv[1], &dest.sin6_addr) < 0 ) { // IPv6 + perror(argv[1]); + exit(errno); + } + printf("address created\n"); + + /* 连接服务器 */ + if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) != 0) { + perror("Connect "); + exit(errno); + } + printf("server connected\n"); + AVFormatContext *pFormatCtx; + int i, videoindex; + AVCodecContext *pCodecCtx; + AVCodec *pCodec; + //char filepath[]="1.mp4"; + + av_register_all(); + avformat_network_init(); + pFormatCtx = avformat_alloc_context(); + + //fp_open=fopen(filepath,"rb+"); + //Init AVIOContext + unsigned char *aviobuffer=(unsigned char *)av_malloc(32768); + AVIOContext *avio =avio_alloc_context(aviobuffer, 32768,0,NULL,read_buffer,NULL,NULL); + pFormatCtx->pb=avio; + + if(avformat_open_input(&pFormatCtx,NULL,NULL,NULL)!=0){ + printf("Couldn't open input stream.\n"); + return -1; + } + if(avformat_find_stream_info(pFormatCtx,NULL)<0){ + printf("Couldn't find stream information.\n"); + return -1; + } + videoindex=-1; + for(i=0; inb_streams; i++) + if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){ + videoindex=i; + break; + } + if(videoindex==-1){ + printf("Didn't find a video stream.\n"); + return -1; + } + pCodecCtx=pFormatCtx->streams[videoindex]->codec; + pCodec=avcodec_find_decoder(pCodecCtx->codec_id); + if(pCodec==NULL){ + printf("Codec not found.\n"); + return -1; + } + if(avcodec_open2(pCodecCtx, pCodec,NULL)<0){ + printf("Could not open codec.\n"); + return -1; + } + AVFrame *pFrame,*pFrameYUV; + pFrame=av_frame_alloc(); + pFrameYUV=av_frame_alloc(); + //uint8_t *out_buffer=(uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)); + //avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); + //SDL---------------------------- + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { + printf( "Could not initialize SDL - %s\n", SDL_GetError()); + return -1; + } + + int screen_w=0,screen_h=0; + SDL_Surface *screen; + screen_w = pCodecCtx->width; + screen_h = pCodecCtx->height; + screen = SDL_SetVideoMode(screen_w, screen_h, 0,0); + + if(!screen) { + printf("SDL: could not set video mode - exiting:%s\n",SDL_GetError()); + return -1; + } + SDL_Overlay *bmp; + bmp = SDL_CreateYUVOverlay(pCodecCtx->width, pCodecCtx->height,SDL_YV12_OVERLAY, screen); + SDL_Rect rect; + rect.x = 0; + rect.y = 0; + rect.w = screen_w; + rect.h = screen_h; + //SDL End------------------------ + int ret, got_picture; + + AVPacket *packet=(AVPacket *)av_malloc(sizeof(AVPacket)); + +#if OUTPUT_YUV420P + FILE *fp_yuv=fopen("output.yuv","wb+"); +#endif + SDL_WM_SetCaption("Simplest FFmpeg Mem Player",NULL); + + struct SwsContext *img_convert_ctx; + img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); + //------------------------------ + while(av_read_frame(pFormatCtx, packet)>=0){ + if(packet->stream_index==videoindex){ + ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); + if(ret < 0){ + printf("Decode Error.\n"); + return -1; + } + if(got_picture){ + SDL_LockYUVOverlay(bmp); + pFrameYUV->data[0]=bmp->pixels[0]; + pFrameYUV->data[1]=bmp->pixels[2]; + pFrameYUV->data[2]=bmp->pixels[1]; + pFrameYUV->linesize[0]=bmp->pitches[0]; + pFrameYUV->linesize[1]=bmp->pitches[2]; + pFrameYUV->linesize[2]=bmp->pitches[1]; + sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); +#if OUTPUT_YUV420P + int y_size=pCodecCtx->width*pCodecCtx->height; + fwrite(pFrameYUV->data[0],1,y_size,fp_yuv); //Y + fwrite(pFrameYUV->data[1],1,y_size/4,fp_yuv); //U + fwrite(pFrameYUV->data[2],1,y_size/4,fp_yuv); //V +#endif + SDL_UnlockYUVOverlay(bmp); + + SDL_DisplayYUVOverlay(bmp, &rect); + //Delay 40ms + SDL_Delay(40); + } + } + av_free_packet(packet); + } + sws_freeContext(img_convert_ctx); + +#if OUTPUT_YUV420P + fclose(fp_yuv); +#endif + + //fclose(fp_open); + + SDL_Quit(); + + //av_free(out_buffer); + av_free(pFrameYUV); + avcodec_close(pCodecCtx); + avformat_close_input(&pFormatCtx); + + return 0; +} + diff --git a/simplest_ffmpeg_mem_player.out b/simplest_ffmpeg_mem_player.out new file mode 100755 index 0000000..42b9749 Binary files /dev/null and b/simplest_ffmpeg_mem_player.out differ diff --git a/socket/client b/socket/client new file mode 100755 index 0000000..3c8f7f3 Binary files /dev/null and b/socket/client differ diff --git a/socket/client.cpp b/socket/client.cpp new file mode 100644 index 0000000..22a74bf --- /dev/null +++ b/socket/client.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAXBUF 1024 + + +int main(int argc, char **argv) +{ + int sockfd, len; + /* struct sockaddr_in dest; */ // IPv4 + struct sockaddr_in6 dest; // IPv6 + char buffer[MAXBUF + 1]; + FILE *stream; + + if (argc != 3) { + printf + ("参数格式错误!正确用法如下:\n\t\t%s IP地址 端口\n\t比如:\t%s 127.0.0.1 80\n此程序用来从某个 IP 地址的服务器某个端口接收最多 MAXBUF 个字节的消息", + argv[0], argv[0]); + exit(0); + } + /* 创建一个 socket 用于 tcp 通信 */ + /* if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { */ // IPv4 + if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) { // IPv6 + perror("Socket"); + exit(errno); + } + printf("socket created\n"); + + /* 初始化服务器端(对方)的地址和端口信息 */ + bzero(&dest, sizeof(dest)); + /* dest.sin_family = AF_INET; */ // IPv4 + dest.sin6_family = AF_INET6; // IPv6 + /* dest.sin_port = htons(atoi(argv[2])); */ // IPv4 + dest.sin6_port = htons(atoi(argv[2])); // IPv6 + /* if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0) { */ // IPv4 + if ( inet_pton(AF_INET6, argv[1], &dest.sin6_addr) < 0 ) { // IPv6 + perror(argv[1]); + exit(errno); + } + printf("address created\n"); + + /* 连接服务器 */ + if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) != 0) { + perror("Connect "); + exit(errno); + } + printf("server connected\n"); + + /* 接收对方发过来的文件*/ + bzero(buffer, MAXBUF); + FILE * fp = fopen("2.mp4","wb"); + if(NULL == fp ) + { + printf("File:\t%s Can Not Open To Write\n", "2.mp4"); + exit(1); + } + int length=0; + while (length=recv(sockfd,buffer,MAXBUF,0)) + { + if(length < 0) + { + printf("Recieve Data From Server Failed!\n"); + break; + } + int write_length = fwrite(buffer,sizeof(char),length,fp); + if (write_length +#include +#include +#include +#include +#include +#include +#include +#include +#define MAXBUF 1024 +int main(int argc, char **argv) +{ + int sockfd, len; + /* struct sockaddr_in dest; */ // IPv4 + struct sockaddr_in6 dest; // IPv6 + char buffer[MAXBUF + 1]; + + if (argc != 3) { + printf + ("参数格式错误!正确用法如下:/n/t/t%s IP地址 端口/n/t比如:/t%s 127.0.0.1 80/n此程序用来从某个 IP 地址的服务器某个端口接收最多 MAXBUF 个字节的消息", + argv[0], argv[0]); + exit(0); + } + /* 创建一个 socket 用于 tcp 通信 */ + /* if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { */ // IPv4 + if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) { // IPv6 + perror("Socket"); + exit(errno); + } + printf("socket created/n"); + + /* 初始化服务器端(对方)的地址和端口信息 */ + bzero(&dest, sizeof(dest)); + /* dest.sin_family = AF_INET; */ // IPv4 + dest.sin6_family = AF_INET6; // IPv6 + /* dest.sin_port = htons(atoi(argv[2])); */ // IPv4 + dest.sin6_port = htons(atoi(argv[2])); // IPv6 + /* if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0) { */ // IPv4 + if ( inet_pton(AF_INET6, argv[1], &dest.sin6_addr) < 0 ) { // IPv6 + perror(argv[1]); + exit(errno); + } + printf("address created/n"); + + /* 连接服务器 */ + if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) != 0) { + perror("Connect "); + exit(errno); + } + printf("server connected/n"); + + /* 接收对方发过来的消息,最多接收 MAXBUF 个字节 */ + bzero(buffer, MAXBUF + 1); + /* 接收服务器来的消息 */ + len = recv(sockfd, buffer, MAXBUF, 0); + if (len > 0) + printf("接收消息成功:'%s',共%d个字节的数据/n", + buffer, len); + else + printf + ("消息接收失败!错误代码是%d,错误信息是'%s'/n", + errno, strerror(errno)); + + bzero(buffer, MAXBUF + 1); + strcpy(buffer, "这是客户端发给服务器端的消息/n"); + /* 发消息给服务器 */ + len = send(sockfd, buffer, strlen(buffer), 0); + if (len < 0) + printf + ("消息'%s'发送失败!错误代码是%d,错误信息是'%s'/n", + buffer, errno, strerror(errno)); + else + printf("消息'%s'发送成功,共发送了%d个字节!/n", + buffer, len); + + /* 关闭连接 */ + close(sockfd); + return 0; +} diff --git a/socket/doctor_strange.flv b/socket/doctor_strange.flv new file mode 100644 index 0000000..07a8b42 Binary files /dev/null and b/socket/doctor_strange.flv differ diff --git a/socket/server b/socket/server new file mode 100755 index 0000000..7afdd7c Binary files /dev/null and b/socket/server differ diff --git a/socket/server.cpp b/socket/server.cpp new file mode 100644 index 0000000..69c188f --- /dev/null +++ b/socket/server.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAXBUF 1024 +int main(int argc, char **argv) +{ + int sockfd, new_fd; + socklen_t len; + FILE *stream; + + /* struct sockaddr_in my_addr, their_addr; */ // IPv4 + struct sockaddr_in6 my_addr, their_addr; // IPv6 ,my_addr服务器地址 + + unsigned int myport, lisnum; + char buf[MAXBUF + 1]; + //argv[1]服务器端口号 + if (argv[1]) + myport = atoi(argv[1]); + else + myport = 7838; + + if (argv[2]) + lisnum = atoi(argv[2]); + else + lisnum = 2; + + /* if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { */ // IPv4 + if ((sockfd = socket(PF_INET6, SOCK_STREAM, 0)) == -1) { // IPv6 + perror("socket"); + exit(1); + } else + printf("socket created\n"); + + bzero(&my_addr, sizeof(my_addr)); + /* my_addr.sin_family = PF_INET; */ // IPv4 + my_addr.sin6_family = PF_INET6; // IPv6 + /* my_addr.sin_port = htons(myport); */ // IPv4 + my_addr.sin6_port = htons(myport); // IPv6 + + if (argv[3]) + /* my_addr.sin_addr.s_addr = inet_addr(argv[3]); */ // IPv4 + inet_pton(AF_INET6, argv[3], &my_addr.sin6_addr); // IPv6 + else + /* my_addr.sin_addr.s_addr = INADDR_ANY; */ // IPv4 + my_addr.sin6_addr = in6addr_any; // IPv6 + + /* if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) */ // IPv4 + if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in6))== -1) { + perror("bind"); + exit(1); + } else + printf("binded\n"); + + if (listen(sockfd, lisnum) == -1) { + perror("listen"); + exit(1); + } else + printf("begin listen\n"); + + while (1) { + len = sizeof(struct sockaddr); + if ((new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &len)) == -1) { + perror("accept"); + exit(errno); + } else + printf("server: got connection from %s, port %d, socket %d\n", + /* inet_ntoa(their_addr.sin_addr), */ // IPv4 + inet_ntop(AF_INET6, &their_addr.sin6_addr, buf, sizeof(buf)), // IPv6 + /* ntohs(their_addr.sin_port), new_fd); */ // IPv4 + their_addr.sin6_port, new_fd); // IPv6 + + + //打开文件 + if ((stream = fopen("doctor_strange.flv", "rb")) == NULL) { + printf("The file doctor_strange.flv not opend!"); + exit(1); + } else + printf("open file:doctor_strange.flv"); + /* 开始处理每个新连接上的数据收发 */ + bzero(buf, MAXBUF); + + int lengsize = 0; + while ((lengsize = fread(buf, 1, 1024, stream)) > 0) { + if (send(new_fd, buf, lengsize, 0) < 0) { + printf("send file failed!"); + break; + } + bzero(buf, MAXBUF); + } + + if (fclose(stream)) { + printf("File data not closed\n"); + exit(1); + } + close(new_fd); + } + close(sockfd); + return 0; +} + diff --git a/socket/server.cpp~ b/socket/server.cpp~ new file mode 100644 index 0000000..99fff42 --- /dev/null +++ b/socket/server.cpp~ @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define MAXBUF 1024 +int main(int argc, char **argv) +{ + int sockfd, new_fd; + socklen_t len; + FILE *stream; + + /* struct sockaddr_in my_addr, their_addr; */ // IPv4 + struct sockaddr_in6 my_addr, their_addr; // IPv6 ,my_addr服务器地址 + + unsigned int myport, lisnum; + char buf[MAXBUF + 1]; + //argv[1]服务器端口号 + if (argv[1]) + myport = atoi(argv[1]); + else + myport = 7838; + + if (argv[2]) + lisnum = atoi(argv[2]); + else + lisnum = 2; + + /* if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { */ // IPv4 + if ((sockfd = socket(PF_INET6, SOCK_STREAM, 0)) == -1) { // IPv6 + perror("socket"); + exit(1); + } else + printf("socket created\n"); + + bzero(&my_addr, sizeof(my_addr)); + /* my_addr.sin_family = PF_INET; */ // IPv4 + my_addr.sin6_family = PF_INET6; // IPv6 + /* my_addr.sin_port = htons(myport); */ // IPv4 + my_addr.sin6_port = htons(myport); // IPv6 + + if (argv[3]) + /* my_addr.sin_addr.s_addr = inet_addr(argv[3]); */ // IPv4 + inet_pton(AF_INET6, argv[3], &my_addr.sin6_addr); // IPv6 + else + /* my_addr.sin_addr.s_addr = INADDR_ANY; */ // IPv4 + my_addr.sin6_addr = in6addr_any; // IPv6 + + /* if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) */ // IPv4 + if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in6))== -1) { + perror("bind"); + exit(1); + } else + printf("binded\n"); + + if (listen(sockfd, lisnum) == -1) { + perror("listen"); + exit(1); + } else + printf("begin listen\n"); + + while (1) { + len = sizeof(struct sockaddr); + if ((new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &len)) == -1) { + perror("accept"); + exit(errno); + } else + printf("server: got connection from %s, port %d, socket %d\n", + /* inet_ntoa(their_addr.sin_addr), */ // IPv4 + inet_ntop(AF_INET6, &their_addr.sin6_addr, buf, sizeof(buf)), // IPv6 + /* ntohs(their_addr.sin_port), new_fd); */ // IPv4 + their_addr.sin6_port, new_fd); // IPv6 + + + //打开文件 + if ((stream = fopen("cuc60anniversary_start.mkv", "rb")) == NULL) { + printf("The file cuc60anniversary_start.mkv not opend!"); + exit(1); + } else + printf("open file:cuc60anniversary_start.mkv"); + /* 开始处理每个新连接上的数据收发 */ + bzero(buf, MAXBUF); + + int lengsize = 0; + while ((lengsize = fread(buf, 1, 1024, stream)) > 0) { + if (send(new_fd, buf, lengsize, 0) < 0) { + printf("send file failed!"); + break; + } + bzero(buf, MAXBUF); + } + + if (fclose(stream)) { + printf("File data not closed\n"); + exit(1); + } + close(new_fd); + } + close(sockfd); + return 0; +} +