forked from Jackarain/avplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
309 lines (256 loc) · 7.28 KB
/
player.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Copyright (C) 2012 microcai <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <stdio.h>
#include <cstdio>
#include <stdint.h>
#include <SDL/SDL.h>
#include <boost/filesystem.hpp>
namespace fs=boost::filesystem;
#include "player.h"
#include "source/source.h"
#include "video/video_out.h"
#include "audio/audio_out.h"
void player::init_file_source(source_context* sc)
{
sc->init_source = file_init_source;
sc->read_data = file_read_data;
sc->read_seek = file_read_seek;
sc->close = file_close;
sc->destory = file_destory;
}
void player::init_torrent_source(source_context* sc)
{
sc->init_source = bt_init_source;
sc->read_data = bt_read_data;
sc->read_seek = bt_read_seek;
sc->read_seek = bt_read_seek;
sc->close = bt_close;
sc->destory = bt_destory;
}
int player::open(const char* movie, int media_type)
{ // 如果未关闭原来的媒体, 则先关闭.
if (m_avplay || m_source);
//close();
// 未创建窗口, 无法播放, 返回失败.
if (!HasWindow())
return -1;
std::string filename(movie);
uint64_t file_lentgh = 0;
if (media_type == MEDIA_TYPE_FILE || media_type == MEDIA_TYPE_BT)
{
file_lentgh =fs::file_size(filename);
}
do {
// 创建avplay.
m_avplay = alloc_avplay_context();
if (!m_avplay)
{
::logger("allocate avplay context failed!\n");
break;
}
// 根据打开的文件类型, 创建不同媒体源.
if (media_type == MEDIA_TYPE_FILE)
{
//size_t len = strlen(filename);
m_source = alloc_media_source(MEDIA_TYPE_FILE, filename.c_str(), filename.length()+1, file_lentgh);
if (!m_source)
{
::logger("allocate media source failed, type is file.\n");
break;
}
// 插入到媒体列表.
m_media_list.insert(std::make_pair(filename, filename));
// 初始化文件媒体源.
init_file_source(m_source);
}
if (media_type == MEDIA_TYPE_HTTP)
{
m_source = alloc_media_source(MEDIA_TYPE_HTTP, filename.c_str(), filename.length()+1, 0);
if (!m_source)
{
::logger("allocate media source failed, type is http.\n");
break;
}
// 插入到媒体列表.
m_media_list.insert(std::make_pair(filename, filename));
}
if (media_type == MEDIA_TYPE_BT)
{
// 先读取bt种子数据, 然后作为附加数据保存到媒体源.
FILE *fp = std::fopen(filename.c_str(), "r+b");
if (!fp)
{
::logger("open torrent file \'%s\' failed!\n", filename.c_str());
break;
}
char *torrent_data = (char*)malloc(file_lentgh);
int readbytes = fread(torrent_data, 1, file_lentgh, fp);
if (readbytes != file_lentgh)
{
::logger("read torrent file \'%s\' failed!\n", filename.c_str());
break;
}
m_source = alloc_media_source(MEDIA_TYPE_BT, torrent_data, file_lentgh, 0);
if (!m_source)
{
::logger("allocate media source failed, type is torrent.\n");
break;
}
free(torrent_data);
// 初始化torrent媒体源.
init_torrent_source(m_source);
}
if (media_type == MEDIA_TYPE_RTSP)
{
m_source = alloc_media_source(MEDIA_TYPE_RTSP, filename.c_str(), filename.length()+1, 0);
if (!m_source)
{
::logger("allocate media source failed, type is rtsp.\n");
break;
}
// 插入到媒体列表.
m_media_list.insert(std::make_pair(filename, filename));
}
// 分配音频和视频的渲染器.
m_audio = alloc_audio_render();
if (!m_audio)
{
::logger("allocate audio render failed!\n");
break;
}
m_video = alloc_video_render(0);
if (!m_video)
{
::logger("allocate video render failed!\n");
break;
}
// 初始化音频和视频渲染器.
init_audio(m_audio);
init_video(m_video);
// 初始化avplay.
if (initialize(m_avplay, m_source) != 0)
{
::logger("initialize avplay failed!\n");
break;
}
// 如果是bt类型, 则在此得到视频文件列表, 并添加到m_media_list.
if (media_type == MEDIA_TYPE_BT)
{
bt_source_info *bt_info = &m_avplay->m_source_ctx->info.bt;
for (int i = 0; i < bt_info->info_size; i++)
{
std::string name = std::string(bt_info->info[i].file_name);
m_media_list.insert(std::make_pair(filename, name));
}
}
// 配置音频视频渲染器.
configure(m_avplay, m_video, VIDEO_RENDER);
configure(m_avplay, m_audio, AUDIO_RENDER);
// 得到视频宽高.
if (m_avplay->m_video_ctx)
{
m_video_width = m_avplay->m_video_ctx->width;
m_video_height = m_avplay->m_video_ctx->height;
}
// 打开视频实时码率和帧率计算.
enable_calc_frame_rate(m_avplay);
enable_calc_bit_rate(m_avplay);
return 0;
} while (0);
if (m_avplay)
free_avplay_context(m_avplay);
m_avplay = NULL;
if (m_source)
free_media_source(m_source);
if (m_audio)
free_audio_render(m_audio);
if (m_video)
free_video_render(m_video);
::logger("open avplay failed!\n");
return -1;
}
void player::init_video(vo_context* vo)
{
//创建第一个窗口
vo->user_data = SDL_SetVideoMode(800, 600, 32, SDL_RESIZABLE);
vo->init_video = sdl_init_video;
//m_draw_frame = sdl_render_one_frame;
vo->re_size = sdl_re_size;
vo->aspect_ratio = sdl_aspect_ratio;
vo->use_overlay = sdl_use_overlay;
vo->destory_video = sdl_destory_render;
vo->render_one_frame = sdl_render_one_frame;//() &draw_frame;
::logger("init video render to sdl.\n");
}
void player::init_audio(ao_context* ao)
{
ao->init_audio = sdl_init_audio;
ao->play_audio = sdl_play_audio;
ao->audio_control = sdl_audio_control;
ao->mute_set = sdl_mute_set;
ao->destory_audio = sdl_destory_audio;
logger("using audio output sdl.\n");
}
bool player::play(double fact, int index)
{
// 重复播放, 返回错误.
if (m_cur_index == index)
return false;
// 如果是文件数据, 则直接播放.
if (::av_start(m_avplay, fact, index) != 0)
return false;
m_cur_index = index;
return true;
}
bool player::wait_for_completion()
{
if (m_avplay)
{
::wait_for_completion(m_avplay);
::logger("play completed.\n");
return true;
}
return false;
}
void player::fwd()
{
av_seek(m_avplay,av_curr_play_time(m_avplay) / av_duration(m_avplay) + 0.05);
}
void player::bwd()
{
double p = av_curr_play_time(m_avplay) / av_duration(m_avplay) - 0.05;
if ( p < 0.0 )
p = 0.0;
av_seek(m_avplay,p);
}
void player::resize(int w, int h)
{
m_video->re_size(m_video, w, h);
}
void player::togglefs()
{
if ( m_fs = ! m_fs ){
m_avplay->m_video_st->codec->width;
m_avplay->m_video_st->codec->height;
this->resize(m_avplay->m_video_st->codec->width,m_avplay->m_video_st->codec->height);
}else{
SDL_Rect** mode = SDL_ListModes(NULL,SDL_FULLSCREEN);
this->resize(mode[0]->w, mode[0]->h);
}
}