forked from Jackarain/avplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.cpp
396 lines (326 loc) · 8.69 KB
/
source.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include "internal.h"
#include "globals.h"
#include "source.h"
#include "av_source.h"
#include "file_source.h"
#ifdef USE_TORRENT
#include <boost/any.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <fstream>
#include "torrent_source.h"
#include "libtorrent/extern_read_op.hpp"
#endif // USE_TORRENT
#ifdef USE_YK
#include "yk_source.h"
#endif // USE_YK
// 默认使用avhttp来转换字符编码.
#include "../third_party/avhttp/include/avhttp/detail/utf8.hpp"
#ifdef __cplusplus
extern "C" {
#endif
EXPORT_API int file_init_source(struct source_context *ctx)
{
// 得到文件信息的引用, 方便取出文件名信息.
file_source_info &file = ctx->info.file;
// 检查文件名长度是否有效.
if (strlen(file.file_name) <= 0)
return -1;
// 创建file_source对象.
file_source *fs = new file_source();
// 保存到priv指针.
ctx->priv = (void*)fs;
// new open_file_data 由file_source内部管理内存.
open_file_data *od = new open_file_data;
// 保存文件名到open_file_data中.
od->filename = std::string(file.file_name);
// 设置线程标识.
od->is_multithread = false;
// 打开文件.
return fs->open(od) ? 0 : -1;
}
EXPORT_API int64_t file_read_data(struct source_context *ctx, char* buff, size_t buf_size)
{
file_source *fs = (file_source *)ctx->priv;
size_t read_size = 0;
bool ret = fs->read_data(buff, buf_size, read_size);
if (ret)
return read_size;
return -1;
}
EXPORT_API int64_t file_read_seek(struct source_context *ctx, int64_t offset, int whence)
{
file_source *fs = (file_source *)ctx->priv;
return fs->read_seek(offset, whence);
}
EXPORT_API void file_close(struct source_context *ctx)
{
file_source *fs = (file_source *)ctx->priv;
fs->close();
}
EXPORT_API void file_destory(struct source_context *ctx)
{
file_source *fs = (file_source *)ctx->priv;
if (fs)
{
fs->close();
delete fs;
ctx->priv = NULL;
}
}
EXPORT_API int bt_init_source(struct source_context *ctx)
{
#ifdef USE_TORRENT
torrent_source *ts = new torrent_source();
open_torrent_data *otd = new open_torrent_data;
bt_source_info &bt_info = ctx->info.bt;
// 保存torrent种子数据.
otd->is_file = false;
// 分配空间.
char *dst = new char[bt_info.torrent_length];
otd->torrent_data.reset(dst);
// 复制torrent数据到open_torrent_data中.
memcpy(dst, bt_info.torrent_data, bt_info.torrent_length);
// 更新种子数据长度.
otd->data_size = bt_info.torrent_length;
// 得到当前路径, 并以utf8编码.
// windows平台才需要,Linux下就是utf8,无需转化.
std::string ansi;
// 得到保存路径, 如果为空, 则下载数据保存在当前目录下.
if (!strlen(bt_info.save_path))
{
ansi = boost::filesystem::current_path().string();
#ifdef _WIN32
ansi = avhttp::detail::ansi_utf8(ansi);
#endif
// 更新保存路径.
otd->save_path = ansi;
strcpy(bt_info.save_path, ansi.c_str());
}
else
{
ansi = std::string(bt_info.save_path);
#ifdef _WIN32
ansi = avhttp::detail::ansi_utf8(ansi);
#endif
// 更新保存路径.
otd->save_path = ansi;
strcpy(bt_info.save_path, ansi.c_str());
}
// 保存到priv指针.
ctx->priv = (void*)ts;
// 打开种子.
if (ts->open(otd))
{
// 更新视频信息, 保存到bt_info当中.
std::vector<video_file_info> vfi = ts->video_list();
bt_info.info_size = vfi.size();
bt_info.info = (media_info*)malloc(sizeof(media_info) * bt_info.info_size);
for (int i = 0; i < bt_info.info_size; i++)
{
video_file_info &f = vfi[i];
strcpy(bt_info.info[i].file_name, f.filename.c_str());
bt_info.info[i].file_size = f.data_size;
bt_info.info[i].start_pos = f.base_offset;
}
return 0;
}
return -1;
#else
return -1;
#endif // USE_TORRENT
}
EXPORT_API int64_t bt_read_data(struct source_context *ctx, char* buff, size_t buf_size)
{
#ifdef USE_TORRENT
torrent_source *ts = (torrent_source*)ctx->priv;
size_t readbytes = 0;
if (!ts->read_data(buff, buf_size, readbytes))
return -1;
return readbytes;
#else
return -1;
#endif // USE_TORRENT
}
EXPORT_API int64_t bt_read_seek(struct source_context *ctx, int64_t offset, int whence)
{
#ifdef USE_TORRENT
torrent_source *ts = (torrent_source*)ctx->priv;
// 如果返回true, 则表示数据不够, 需要缓冲.
int64_t ret = ts->read_seek(offset, whence);
if (whence == SEEK_SET || whence == SEEK_CUR || whence == SEEK_END)
{
if (!ts->has_data(ret))
{
// 表示seek位置没有数据, 上层应该根据dl_info.not_enough判断是否暂时进行缓冲.
ctx->dl_info.not_enough = 1;
}
}
return ret;
#else
return -1;
#endif // USE_TORRENT
}
EXPORT_API void bt_close(struct source_context *ctx)
{
#ifdef USE_TORRENT
torrent_source *ts = (torrent_source*)ctx->priv;
ts->close();
bt_source_info &bt_info = ctx->info.bt;
if (bt_info.info)
{
free(bt_info.info);
bt_info.info = NULL;
}
#endif // USE_TORRENT
}
EXPORT_API void bt_destory(struct source_context *ctx)
{
#ifdef USE_TORRENT
torrent_source *ts = (torrent_source*)ctx->priv;
if (ts)
{
ts->close();
bt_source_info &bt_info = ctx->info.bt;
if (bt_info.info)
{
free(bt_info.info);
bt_info.info = NULL;
}
delete ts;
ctx->priv = NULL;
}
#endif // USE_TORRENT
}
EXPORT_API int yk_init_source(struct source_context *ctx)
{
#ifdef USE_YK
yk_source_info &yk_info = ctx->info.yk;
// 检查参数有效性.
if (!strlen(yk_info.url))
return -1;
yk_source *ys = new yk_source();
open_yk_data *yd = new open_yk_data;
// 更新priv, 把yk_source对象保存到priv中.
ctx->priv = (void*)ys;
// 保存url.
yd->url = std::string(yk_info.url);
// 得到当前路径, 并以utf8编码.
// windows平台才需要,Linux下就是utf8,无需转化.
std::string ansi;
#ifdef _WIN32
std::wstring path;
setlocale(LC_ALL, "chs");
#endif
// 得到保存路径, 如果为空, 则下载数据保存在当前目录下.
if (!strlen(yk_info.save_path))
{
ansi = boost::filesystem::current_path().string();
#ifdef _WIN32
path = avhttp::detail::ansi_wide(ansi);
ansi = avhttp::detail::wide_utf8(path);
#endif
// 更新保存路径.
yd->save_path = ansi;
strcpy(yk_info.save_path, ansi.c_str());
}
else
{
ansi = std::string(yk_info.save_path);
#ifdef _WIN32
path = avhttp::detail::ansi_wide(ansi);
ansi = avhttp::detail::wide_utf8(path);
#endif
// 更新保存路径.
yd->save_path = ansi;
strcpy(yk_info.save_path, ansi.c_str());
}
// 保存请求的视频类型.
yd->type = yk_info.type;
// 打开视频.
if (ys->open(yd))
return 0;
return -1;
#else
return -1;
#endif // USE_YK
return 0;
}
EXPORT_API int yk_media_info(struct source_context *ctx, youku_video **list, int *size)
{
#ifdef USE_YK
return -1;
#else
return -1;
#endif // USE_YK
}
EXPORT_API int yk_free_media_info(struct source_context *ctx, youku_video **list)
{
#ifdef USE_YK
return -1;
#else
return -1;
#endif // USE_YK
}
EXPORT_API int yk_change_file(struct source_context *ctx, int index)
{
#ifdef USE_YK
return -1;
#else
return -1;
#endif // USE_YK
}
EXPORT_API int64_t yk_read_data(struct source_context *ctx, char* buff, size_t buf_size)
{
#ifdef USE_YK
// source_context *sc = (source_context*)ctx;
// torrent_source *ts = (torrent_source*)sc->io_dev;
size_t readbytes = 0;
//
// if (!ts->read_data(buff, offset, buf_size, readbytes))
// return -1;
//
return readbytes;
#else
return -1;
#endif // USE_YK
}
EXPORT_API int64_t yk_read_seek(struct source_context *ctx, int64_t offset, int whence)
{
#ifdef USE_YK
// source_context *sc = (source_context*)ctx;
// torrent_source *ts = (torrent_source*)sc->io_dev;
//
// // 如果返回true, 则表示数据不够, 需要缓冲.
// if (ts->read_seek(offset, whence))
// {
// printf("!!!!!!!!!!! data is not enough: %lld, whence: %d !!!!!!!!!!!\n", offset, whence);
// sc->info.not_enough = 1;
// }
//
// // 此处的返回值无意义.
return 0;
#else
return -1;
#endif // USE_YK
}
EXPORT_API void yk_close(struct source_context *ctx)
{
#ifdef USE_YK
// source_context *sc = (source_context*)ctx;
// torrent_source *ts = (torrent_source*)sc->io_dev;
// ts->close();
#endif // USE_YK
}
EXPORT_API void yk_destory(struct source_context *ctx)
{
#ifdef USE_YK
// source_context *sc = (source_context*)ctx;
// torrent_source *ts = (torrent_source*)sc->io_dev;
// ts->close();
// delete ts;
#endif // USE_YK
}
#ifdef __cplusplus
}
#endif