客户端播放视频

一、以下是一个简单的代码示例,展示了如何使用FFmpeg API播放TCP视频流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>

int main(int argc, char* argv[]) {
AVFormatContext* pFormatCtx = NULL;
int videoStream;
AVCodecContext* pCodecCtx = NULL;
AVCodec* pCodec = NULL;
AVFrame* pFrame = NULL;
AVPacket packet;

// 初始化FFmpeg
avformat_network_init();

// 打开视频流
if (avformat_open_input(&pFormatCtx, "udp://10.0.3.1:1234", NULL, NULL) != 0) {
return -1; // Couldn't open stream
}

// 获取流信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
return -1; // Couldn't find stream information
}

// 查找视频流
videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
}
if (videoStream == -1) {
return -1; // Didn't find a video stream
}

// 获取解码器参数
AVCodecParameters* pCodecPar = pFormatCtx->streams[videoStream]->codecpar;

// 找到解码器
pCodec = avcodec_find_decoder(pCodecPar->codec_id);
if (pCodec == NULL) {
return -1; // Codec not found
}

// 分配解码器上下文
pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
return -1; // Could not allocate codec context
}

// 从参数复制解码器上下文
if (avcodec_parameters_to_context(pCodecCtx, pCodecPar) < 0) {
return -1; // Could not copy codec context
}

// 打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
return -1; // Could not open codec
}

// 分配视频帧
pFrame = av_frame_alloc();

// 读取帧并解码
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
if (avcodec_send_packet(pCodecCtx, &packet) == 0) {
while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
// 在这里显示视频帧
// display_frame(pFrame);
}
}
}
av_packet_unref(&packet);
}

// 清理
av_frame_free(&pFrame);
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);

return 0;
}

二、C语言使用FFMPEG与SDL播放视频流

播放成功,但是播放的视频速度不对。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <SDL2/SDL.h>
#undef main
int main(int argc, char* argv[]) {
AVFormatContext* pFormatCtx = NULL;
int videoStream;
AVCodecContext* pCodecCtx = NULL;
AVCodec* pCodec = NULL;
AVFrame* pFrame = NULL;
AVFrame* pFrameRGB = NULL;
AVPacket packet;
struct SwsContext* sws_ctx = NULL;
uint8_t* buffer = NULL;
int numBytes;

SDL_Window* screen = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture = NULL;
SDL_Rect rect;

// 初始化FFmpeg
avformat_network_init();

// 初始化SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
exit(1);
}

// 打开视频流
if (avformat_open_input(&pFormatCtx, "http://cc.weichennet.com/26%20%E5%AE%9E%E6%88%98%E8%BF%87TP%E4%B8%B6NP%20%E8%BF%87%E5%9B%BE%E6%A0%87.mp4", NULL, NULL) != 0) {
return -1; // Couldn't open stream
}

// 获取流信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
return -1; // Couldn't find stream information
}

// 查找视频流
videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
}
if (videoStream == -1) {
return -1; // Didn't find a video stream
}

// 获取解码器参数
AVCodecParameters* pCodecPar = pFormatCtx->streams[videoStream]->codecpar;

// 找到解码器
pCodec = avcodec_find_decoder(pCodecPar->codec_id);
if (pCodec == NULL) {
return -1; // Codec not found
}

// 分配解码器上下文
pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
return -1; // Could not allocate codec context
}

// 从参数复制解码器上下文
if (avcodec_parameters_to_context(pCodecCtx, pCodecPar) < 0) {
return -1; // Could not copy codec context
}

// 打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
return -1; // Could not open codec
}

// 分配视频帧
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
if (pFrameRGB == NULL) {
return -1;
}

// 确定所需的缓冲区大小并分配缓冲区
numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 32);
buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 32);

// 初始化SWS上下文以进行软件缩放
sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
SWS_BILINEAR, NULL, NULL, NULL);

// 创建SDL窗口
screen = SDL_CreateWindow("FFmpeg Video Player",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
pCodecCtx->width,
pCodecCtx->height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!screen) {
fprintf(stderr, "SDL: could not set video mode - exiting\n");
exit(1);
}

renderer = SDL_CreateRenderer(screen, -1, 0);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height);

// 读取帧并解码
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
if (avcodec_send_packet(pCodecCtx, &packet) == 0) {
while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
// 转换图像格式
sws_scale(sws_ctx, (uint8_t const* const*)pFrame->data,
pFrame->linesize, 0, pCodecCtx->height,
pFrameRGB->data, pFrameRGB->linesize);

// 在这里显示视频帧
SDL_UpdateTexture(texture, NULL, pFrameRGB->data[0], pFrameRGB->linesize[0]);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
}
}
}
av_packet_unref(&packet);

// 处理SDL事件
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_QUIT:
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(screen);
SDL_Quit();
av_free(buffer);
av_frame_free(&pFrameRGB);
av_frame_free(&pFrame);
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);
return 0;
break;
default:
break;
}
}

// 清理
av_free(buffer);
av_frame_free(&pFrameRGB);
av_frame_free(&pFrame);
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);

SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(screen);
SDL_Quit();

return 0;
}

三、修复播放速度的问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/frame.h> // av_frame_get_best_effort_timestamp is declared here
#include <libavutil/time.h>
#include <libswscale/swscale.h>
#include <SDL2/SDL.h>

#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "swscale.lib")


#undef main
int main(int argc, char* argv[]) {
AVFormatContext* pFormatCtx = NULL;
int videoStream;
AVCodecContext* pCodecCtx = NULL;
AVCodec* pCodec = NULL;
AVFrame* pFrame = NULL;
AVFrame* pFrameRGB = NULL;
AVPacket packet;
struct SwsContext* sws_ctx = NULL;
uint8_t* buffer = NULL;
int numBytes;
double pts, actual_delay;
int64_t start_time, frame_time;

SDL_Window* screen = NULL;
SDL_Renderer* renderer = NULL;
SDL_Texture* texture = NULL;

// Initialize FFmpeg
avformat_network_init();

// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
exit(1);
}

// Open video stream
const char* inputURL = "http://cc.weichennet.com/26%20%E5%AE%9E%E6%88%98%E8%BF%87TP%E4%B8%B6NP%20%E8%BF%87%E5%9B%BE%E6%A0%87.mp4";
if (avformat_open_input(&pFormatCtx, inputURL, NULL, NULL) != 0) {
fprintf(stderr, "Could not open stream.\n");
exit(1);
}

// Get stream info
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
fprintf(stderr, "Couldn't find stream information.\n");
exit(1);
}

// Find the first video stream
videoStream = -1;
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
}
if (videoStream == -1) {
fprintf(stderr, "Didn't find a video stream.\n");
exit(1);
}

// Get codec parameters
AVCodecParameters* pCodecPar = pFormatCtx->streams[videoStream]->codecpar;
// Find the decoder for the video stream
pCodec = avcodec_find_decoder(pCodecPar->codec_id);
if (pCodec == NULL) {
fprintf(stderr, "Codec not found.\n");
exit(1);
}

// Allocate codec context
pCodecCtx = avcodec_alloc_context3(pCodec);
if (!pCodecCtx) {
fprintf(stderr, "Could not allocate codec context.\n");
exit(1);
}

// Copy codec parameters to context
if (avcodec_parameters_to_context(pCodecCtx, pCodecPar) < 0) {
fprintf(stderr, "Could not copy codec context.\n");
exit(1);
}

// Open codec
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
fprintf(stderr, "Could not open codec.\n");
exit(1);
}

// Allocate video frames
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
if (!pFrameRGB) {
fprintf(stderr, "Could not allocate RGB frame.\n");
exit(1);
}

// Determine required buffer size and allocate buffer
numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height, 32);
buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height, 32);

// Initialize SWS context for software scaling
sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
SWS_BILINEAR, NULL, NULL, NULL);

// Create SDL window
screen = SDL_CreateWindow("FFmpeg Video Player",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
pCodecCtx->width,
pCodecCtx->height,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!screen) {
fprintf(stderr, "SDL: could not set video mode - %s\n", SDL_GetError());
exit(1);
}

renderer = SDL_CreateRenderer(screen, -1, 0);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STREAMING,
pCodecCtx->width, pCodecCtx->height);

// Get start time
start_time = av_gettime();

// Read frames and decode
// Inside the main decoding loop:
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
if (avcodec_send_packet(pCodecCtx, &packet) == 0) {
while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) {
// Convert the image
sws_scale(sws_ctx,
(uint8_t const* const*)pFrame->data,
pFrame->linesize,
0,
pCodecCtx->height,
pFrameRGB->data,
pFrameRGB->linesize);

// Update texture
SDL_UpdateTexture(texture, NULL, pFrameRGB->data[0], pFrameRGB->linesize[0]);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);

// Timing control
pts = pFrame->best_effort_timestamp * av_q2d(pFormatCtx->streams[videoStream]->time_base);
frame_time = start_time + (int64_t)(pts * 1000000);
actual_delay = frame_time - av_gettime();
if (actual_delay > 0) {
av_usleep((unsigned)actual_delay);
}
}
}
}
av_packet_unref(&packet);

// Handle SDL events
SDL_Event event;
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
break;
}
}

end:
// Cleanup
av_free(buffer);
av_frame_free(&pFrameRGB);
av_frame_free(&pFrame);
avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx);

SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(screen);
SDL_Quit();

return 0;
}