forked from Jackarain/avplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavplay.c
2378 lines (2079 loc) · 56.5 KB
/
avplay.c
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <math.h>
#include "avplay.h"
/* 定义bool值 */
#ifndef _MSC_VER
enum bool_type
{
FALSE, TRUE
};
#endif
/* 队列类型. */
#define QUEUE_PACKET 0
#define QUEUE_AVFRAME 1
#define IO_BUFFER_SIZE 32768
#define MAX_PKT_BUFFER_SIZE 5242880
#define MIN_AUDIO_BUFFER_SIZE MAX_PKT_BUFFER_SIZE /* 327680 */
#define MIN_AV_FRAMES 5
#define AUDIO_BUFFER_MAX_SIZE (AVCODEC_MAX_AUDIO_FRAME_SIZE * 2)
#define AVDECODE_BUFFER_SIZE 2
#define DEVIATION 6
#define AV_SYNC_THRESHOLD 0.01f
#define AV_NOSYNC_THRESHOLD 10.0f
#define AUDIO_DIFF_AVG_NB 20
#define SEEKING_FLAG -1
#define NOSEEKING_FLAG 0
#ifndef _MSC_VER
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
void Sleep(int msec)
{
struct timespec rev,rem;
rev.tv_sec =msec/1000;
rev.tv_nsec = msec * 1000000;
int ret = clock_nanosleep(CLOCK_MONOTONIC,0,&rev,&rem);
while(ret <0 && errno == EINTR){
rev = rem;
ret = clock_nanosleep(CLOCK_MONOTONIC,0,&rev,&rem);
}
}
int64_t av_gettime()
{
struct timespec cltime;
clock_gettime(CLOCK_MONOTONIC,&cltime);
return cltime.tv_sec * 1000000 + cltime.tv_nsec / 1000;
}
#else
#define av_gettime() (timeGetTime() * 1000.0f)
#endif
/* INT64最大最小取值范围. */
#ifndef INT64_MIN
#define INT64_MIN (-9223372036854775807LL - 1)
#endif
#ifndef INT64_MAX
#define INT64_MAX (9223372036854775807LL)
#endif
/* rgb和yuv互换. */
#define _r(c) ((c) & 0xFF)
#define _g(c) (((c) >> 8) & 0xFF)
#define _b(c) (((c) >> 16) & 0xFF)
#define _a(c) ((c) >> 24)
#define rgba2y(c) ( (( 263*_r(c) + 516*_g(c) + 100*_b(c)) >> 10) + 16 )
#define rgba2u(c) ( ((-152*_r(c) - 298*_g(c) + 450*_b(c)) >> 10) + 128 )
#define rgba2v(c) ( (( 450*_r(c) - 376*_g(c) - 73*_b(c)) >> 10) + 128 )
#define MAX_TRANS 255
#define TRANS_BITS 8
typedef struct AVFrameList
{
AVFrame pkt;
struct AVFrameList *next;
} AVFrameList;
/* 队列操作. */
static void queue_init(av_queue *q);
static void queue_flush(av_queue *q);
static void queue_end(av_queue *q);
/* 入队出队列操作. */
static int get_queue(av_queue *q, void *p);
static int put_queue(av_queue *q, void *p);
static void chk_queue(avplay *play, av_queue *q, int size);
/* ffmpeg相关操作函数. */
static int stream_index(enum AVMediaType type, AVFormatContext *ctx);
static int open_decoder(AVCodecContext *ctx);
/* 读取数据线程. */
static void* read_pkt_thrd(void *param);
static void* video_dec_thrd(void *param);
static void* audio_dec_thrd(void *param);
/* 渲染线程. */
static void* audio_render_thrd(void *param);
static void* video_render_thrd(void *param);
/* 视频帧复制. */
static void video_copy(avplay *play, AVFrame *dst, AVFrame *src);
static void audio_copy(avplay *play, AVFrame *dst, AVFrame *src);
/* 更新视频pts. */
static void update_video_pts(avplay *play, double pts, int64_t pos);
/* 时钟函数. */
static double video_clock(avplay *play);
static double audio_clock(avplay *play);
static double external_clock(avplay *play);
static double master_clock(avplay *play);
/* 读写数据函数. */
static int read_packet(void *opaque, uint8_t *buf, int buf_size);
static int write_packet(void *opaque, uint8_t *buf, int buf_size);
static int64_t seek_packet(void *opaque, int64_t offset, int whence);
AVPacket flush_pkt;
AVFrame flush_frm;
static
void
queue_init(av_queue *q)
{
q->abort_request = 0;
q->m_first_pkt = q->m_last_pkt = NULL;
q->m_size = 0;
pthread_mutex_init(&q->m_mutex, NULL);
pthread_cond_init(&q->m_cond, NULL);
if (q->m_type == QUEUE_PACKET)
put_queue(q, (void*) &flush_pkt);
else if (q->m_type == QUEUE_AVFRAME)
put_queue(q, (void*) &flush_frm);
}
static
void queue_flush(av_queue *q)
{
if (q->m_size == 0)
return;
if (q->m_type == QUEUE_PACKET)
{
AVPacketList *pkt, *pkt1;
pthread_mutex_lock(&q->m_mutex);
for (pkt = q->m_first_pkt; pkt != NULL; pkt = pkt1)
{
pkt1 = pkt->next;
if (pkt->pkt.data != flush_pkt.data)
av_free_packet(&pkt->pkt);
av_freep(&pkt);
}
q->m_last_pkt = NULL;
q->m_first_pkt = NULL;
q->m_size = 0;
pthread_mutex_unlock(&q->m_mutex);
}
else if (q->m_type == QUEUE_AVFRAME)
{
AVFrameList *pkt, *pkt1;
pthread_mutex_lock(&q->m_mutex);
for (pkt = q->m_first_pkt; pkt != NULL; pkt = pkt1)
{
pkt1 = pkt->next;
if (pkt->pkt.data[0] != flush_frm.data[0])
av_free(pkt->pkt.data[0]);
av_freep(&pkt);
}
q->m_last_pkt = NULL;
q->m_first_pkt = NULL;
q->m_size = 0;
pthread_mutex_unlock(&q->m_mutex);
}
}
static
void queue_end(av_queue *q)
{
queue_flush(q);
pthread_cond_destroy(&q->m_cond);
pthread_mutex_destroy(&q->m_mutex);
}
#define PRIV_OUT_QUEUE \
pthread_mutex_lock(&q->m_mutex); \
for (;;) \
{ \
if (q->abort_request) \
{ \
ret = -1; \
break; \
} \
pkt1 = q->m_first_pkt; \
if (pkt1) \
{ \
q->m_first_pkt = pkt1->next; \
if (!q->m_first_pkt) \
q->m_last_pkt = NULL; \
q->m_size--; \
*pkt = pkt1->pkt; \
av_free(pkt1); \
ret = 1; \
break; \
} \
else \
{ \
pthread_cond_wait(&q->m_cond, &q->m_mutex); \
} \
} \
pthread_mutex_unlock(&q->m_mutex);
static
int get_queue(av_queue *q, void *p)
{
if (q->m_type == QUEUE_PACKET)
{
AVPacketList *pkt1;
AVPacket *pkt = (AVPacket*) p;
int ret;
PRIV_OUT_QUEUE
return ret;
}
else if (q->m_type == QUEUE_AVFRAME)
{
AVFrameList *pkt1;
AVFrame *pkt = (AVFrame*) p;
int ret;
PRIV_OUT_QUEUE
return ret;
}
return -1;
}
#define PRIV_PUT_QUEUE(type) \
pkt1 = av_malloc(sizeof(type)); \
if (!pkt1) \
return -1; \
pkt1->pkt = *pkt; \
pkt1->next = NULL; \
\
pthread_mutex_lock(&q->m_mutex); \
if (!q->m_last_pkt) \
q->m_first_pkt = pkt1; \
else \
((type*) q->m_last_pkt)->next = pkt1; \
q->m_last_pkt = pkt1; \
q->m_size++; \
pthread_cond_signal(&q->m_cond); \
pthread_mutex_unlock(&q->m_mutex);
static
int put_queue(av_queue *q, void *p)
{
if (q->m_type == QUEUE_PACKET)
{
AVPacketList *pkt1;
AVPacket *pkt = (AVPacket*) p;
/* duplicate the packet */
if (pkt != &flush_pkt && av_dup_packet(pkt) < 0)
return -1;
PRIV_PUT_QUEUE(AVPacketList)
return 0;
}
else if (q->m_type == QUEUE_AVFRAME)
{
AVFrameList *pkt1;
AVFrame *pkt = (AVFrame*) p;
PRIV_PUT_QUEUE(AVFrameList)
return 0;
}
return -1;
}
static
media_info* find_media_info(source_context *sc, int index)
{
media_info *mi = sc->media;
int i = 0;
if (index > sc->media_size)
return NULL;
for (; i < index; i++)
mi++;
return mi;
}
static
int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
int read_bytes = 0;
avplay *play = (avplay*)opaque;
if (play->m_abort)
return 0;
read_bytes = play->m_source_ctx->read_data(play->m_source_ctx,
buf, play->m_source_ctx->offset, buf_size);
if (read_bytes == -1)
return 0;
play->m_source_ctx->offset += read_bytes;
return read_bytes;
}
static
int write_packet(void *opaque, uint8_t *buf, int buf_size)
{
avplay *play = (avplay*)opaque;
return 0;
}
static
int64_t seek_packet(void *opaque, int64_t offset, int whence)
{
avplay *play = (avplay*)opaque;
int64_t old_off = play->m_source_ctx->offset;
if (play->m_abort)
return -1;
switch (whence)
{
case SEEK_SET:
{
media_info *mi = find_media_info(
play->m_source_ctx, play->m_current_play_index);
if (!mi)
assert(0);
/* 从当前文件的起始位置开始计算. */
play->m_source_ctx->offset = mi->start_pos + offset;
}
break;
case SEEK_CUR:
/* 直接根据当前位置计算offset. */
offset += play->m_source_ctx->offset;
play->m_source_ctx->offset = offset;
break;
case SEEK_END:
{
int64_t size = 0;
if (play->m_source_ctx->type != MEDIA_TYPE_BT)
{
size = play->m_source_ctx->media->file_size;
}
else if (play->m_source_ctx->type == MEDIA_TYPE_BT)
{
/* 找到正在播放的文件. */
media_info *mi = find_media_info(
play->m_source_ctx, play->m_current_play_index);
if (mi)
size = mi->file_size;
else
assert(0);
/* 计算文件尾的位置. */
size += mi->start_pos;
}
/* 计算当前位置. */
offset += size;
/* 保存当前位置. */
play->m_source_ctx->offset = offset;
}
break;
case AVSEEK_SIZE:
{
int64_t size = 0;
if (play->m_source_ctx->type != MEDIA_TYPE_BT)
{
size = play->m_source_ctx->media->file_size;
}
else if (play->m_source_ctx->type == MEDIA_TYPE_BT)
{
/* 找到正在播放的文件. */
media_info *mi = find_media_info(
play->m_source_ctx, play->m_current_play_index);
if (mi)
size = mi->file_size;
else
assert(0);
if (mi)
size = mi->file_size;
}
offset = size;
}
break;
// case AVSEEK_FORCE:
// {
//
// }
// break;
default:
{
assert(0);
}
break;
}
return offset;
}
static
int decode_interrupt_cb(void *ctx)
{
avplay *play = (avplay*)ctx;
return play->m_abort;
}
static
int stream_index(enum AVMediaType type, AVFormatContext *ctx)
{
unsigned int i;
for (i = 0; (unsigned int) i < ctx->nb_streams; i++)
if (ctx->streams[i]->codec->codec_type == type)
return i;
return -1;
}
static
int open_decoder(AVCodecContext *ctx)
{
int ret = 0;
AVCodec *codec = NULL;
/* 查找解码器. */
codec = avcodec_find_decoder(ctx->codec_id);
if (!codec)
return -1;
/* 打开解码器. */
ret = avcodec_open2(ctx, codec, NULL);
if (ret != 0)
return ret;
return ret;
}
source_context* alloc_media_source(int type, const char *data, int len, int64_t size)
{
struct source_context *ptr = malloc(sizeof(source_context));
/* 清空结构体. */
memset(ptr, 0, sizeof(source_context));
/* 参数赋值. */
ptr->type = type;
if (type != MEDIA_TYPE_BT)
{
/* 保存文件路径或url信息. */
ptr->media = calloc(1, sizeof(media_info));
ptr->media->start_pos = 0;
ptr->media->file_size = size;
/* 保存文件名. */
ptr->media->name = strdup(data);
/*
* 媒体文件数为1, 只有打开bt种子时, bt中存在多个视频文件,
* media_size才可能大于1.
*/
ptr->media_size = 1;
}
else
{
/* 保存种子文件数据. */
ptr->torrent_data = calloc(1, len);
memcpy(ptr->torrent_data, data, len);
ptr->torrent_len = len;
}
return ptr;
}
void free_media_source(source_context *ctx)
{
int i = 0;
/* 释放io对象. */
if (ctx->io_dev)
ctx->destory(ctx);
/* 释放data. */
if (ctx->torrent_data)
free(ctx->torrent_data);
/* 释放media_info. */
for (; i < ctx->media_size; i++)
{
if (ctx->media[i].name)
free(ctx->media[i].name);
}
if (ctx->media)
free(ctx->media);
/* 最后释放整个source_context. */
free(ctx);
}
avplay* alloc_avplay_context()
{
struct avplay *ptr = malloc(sizeof(avplay));
memset(ptr, 0, sizeof(avplay));
return ptr;
}
void free_avplay_context(avplay *ctx)
{
free(ctx);
}
ao_context* alloc_audio_render()
{
ao_context *ptr = malloc(sizeof(ao_context));
memset(ptr, 0, sizeof(ao_context));
return ptr;
}
void free_audio_render(ao_context *ctx)
{
if (ctx->audio_dev)
ctx->destory_audio(ctx);
free(ctx);
}
vo_context* alloc_video_render(void *user_data)
{
struct vo_context *ptr = malloc(sizeof(vo_context));
memset(ptr, 0, sizeof(vo_context));
ptr->user_data = user_data;
return ptr;
}
void free_video_render(vo_context *ctx)
{
if (ctx->video_dev)
ctx->destory_video(ctx);
free(ctx);
}
int initialize(avplay *play, source_context *sc)
{
int ret = 0, i = 0;
AVInputFormat *iformat = NULL;
av_register_all();
avcodec_register_all();
avformat_network_init();
/* 置0. */
memset(play, 0, sizeof(avplay));
/* 分配一个format context. */
play->m_format_ctx = avformat_alloc_context();
play->m_format_ctx->flags = AVFMT_FLAG_GENPTS;
play->m_format_ctx->interrupt_callback.callback = decode_interrupt_cb;
play->m_format_ctx->interrupt_callback.opaque = play;
/* 保存media_source指针并初始化, 由avplay负责管理释放其内存. */
play->m_source_ctx = sc;
/* 初始化数据源. */
if (play->m_source_ctx->init_source &&
play->m_source_ctx->init_source(sc) < 0)
{
return -1;
}
/* 获得媒体文件列表. */
if (sc->type == MEDIA_TYPE_BT)
{
char name[2048] = {0};
int64_t size = 2048;
int64_t pos = 0;
int i = 0;
media_info *media = NULL;
for (i = 0; ; i++)
{
int ret = play->m_source_ctx->bt_media_info(play->m_source_ctx, name, &pos, &size);
if (ret == -1)
break;
if (i == 0)
{
play->m_source_ctx->media = malloc(sizeof(media_info) * ret);
play->m_source_ctx->media_size = ret;
media = play->m_source_ctx->media;
}
media[i].file_size = size;
media[i].start_pos = pos;
media[i].name = strdup(name);
pos = i + 1;
size = 2048;
}
}
if (sc->type == MEDIA_TYPE_BT || sc->type == MEDIA_TYPE_FILE)
{
/* 分配用于io的缓冲. */
play->m_io_buffer = (unsigned char*)av_malloc(IO_BUFFER_SIZE);
if (!play->m_io_buffer)
{
printf("Create buffer failed!\n");
return -1;
}
/* 分配io上下文. */
play->m_avio_ctx = avio_alloc_context(play->m_io_buffer,
IO_BUFFER_SIZE, 0, (void*)play, read_packet, NULL, seek_packet);
if (!play->m_io_buffer)
{
printf("Create io context failed!\n");
av_free(play->m_io_buffer);
return -1;
}
play->m_avio_ctx->write_flag = 0;
ret = av_probe_input_buffer(play->m_avio_ctx, &iformat, "", NULL, 0, 0);
if (ret < 0)
{
printf("av_probe_input_buffer call failed!\n");
goto FAILED_FLG;
}
/* 打开输入媒体流. */
play->m_format_ctx->pb = play->m_avio_ctx;
ret = avformat_open_input(&play->m_format_ctx, "", iformat, NULL);
if (ret < 0)
{
printf("av_open_input_stream call failed!\n");
goto FAILED_FLG;
}
}
else
{
/* 判断url是否为空. */
if (!play->m_source_ctx->media->name || !play->m_source_ctx->media_size)
goto FAILED_FLG;
/* HTTP和RTSP直接使用ffmpeg来处理. */
ret = avformat_open_input(&play->m_format_ctx,
play->m_source_ctx->media->name, iformat, NULL);
if (ret < 0)
{
printf("av_open_input_stream call failed!\n");
goto FAILED_FLG;
}
}
ret = avformat_find_stream_info(play->m_format_ctx, NULL);
if (ret < 0)
goto FAILED_FLG;
av_dump_format(play->m_format_ctx, 0, NULL, 0);
/* 得到audio和video在streams中的index. */
play->m_video_index =
stream_index(AVMEDIA_TYPE_VIDEO, play->m_format_ctx);
play->m_audio_index =
stream_index(AVMEDIA_TYPE_AUDIO, play->m_format_ctx);
if (play->m_video_index == -1 && play->m_audio_index == -1)
goto FAILED_FLG;
/* 保存audio和video的AVStream指针. */
if (play->m_video_index != -1)
play->m_video_st = play->m_format_ctx->streams[play->m_video_index];
if (play->m_audio_index != -1)
play->m_audio_st = play->m_format_ctx->streams[play->m_audio_index];
/* 保存audio和video的AVCodecContext指针. */
if (play->m_audio_index != -1)
play->m_audio_ctx = play->m_format_ctx->streams[play->m_audio_index]->codec;
if (play->m_video_index != -1)
play->m_video_ctx = play->m_format_ctx->streams[play->m_video_index]->codec;
/* 打开解码器. */
if (play->m_audio_index != -1)
{
ret = open_decoder(play->m_audio_ctx);
if (ret != 0)
goto FAILED_FLG;
}
if (play->m_video_index != -1)
{
ret = open_decoder(play->m_video_ctx);
if (ret != 0)
goto FAILED_FLG;
}
/* 默认同步到音频. */
play->m_av_sync_type = AV_SYNC_AUDIO_MASTER;
play->m_abort = TRUE;
/* 初始化各变量. */
av_init_packet(&flush_pkt);
flush_pkt.data = "FLUSH";
flush_frm.data[0] = "FLUSH";
play->m_abort = 0;
/* 初始化队列. */
if (play->m_audio_index != -1)
{
play->m_audio_q.m_type = QUEUE_PACKET;
queue_init(&play->m_audio_q);
play->m_audio_dq.m_type = QUEUE_AVFRAME;
queue_init(&play->m_audio_dq);
}
if (play->m_video_index != -1)
{
play->m_video_q.m_type = QUEUE_PACKET;
queue_init(&play->m_video_q);
play->m_video_dq.m_type = QUEUE_AVFRAME;
queue_init(&play->m_video_dq);
}
/* 初始化读取文件数据缓冲计数mutex. */
pthread_mutex_init(&play->m_buf_size_mtx, NULL);
/* 打开各线程. */
return 0;
FAILED_FLG:
if (play->m_format_ctx)
avformat_close_input(&play->m_format_ctx);
if (play->m_avio_ctx)
av_free(play->m_avio_ctx);
if (play->m_io_buffer)
av_free(play->m_io_buffer);
return -1;
}
int av_start(avplay *play, double fact, int index)
{
pthread_attr_t attr;
int ret;
/* 保存正在播放的索引号. */
play->m_current_play_index = index;
if (index > play->m_source_ctx->media_size)
return -1;
/* 保存起始播放时间. */
play->m_start_time = fact;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
/* 创建线程. */
ret = pthread_create(&play->m_read_pkt_thrd, &attr, read_pkt_thrd,
(void*) play);
if (ret)
{
printf("ERROR; return code from pthread_create() is %d\n", ret);
return ret;
}
if (play->m_audio_index != -1)
{
/*play->m_audio_current_pts_drift = av_gettime() / 1000000.0f;*/
ret = pthread_create(&play->m_audio_dec_thrd, &attr, audio_dec_thrd,
(void*) play);
if (ret)
{
printf("ERROR; return code from pthread_create() is %d\n", ret);
return ret;
}
}
if (play->m_video_index != -1)
{
ret = pthread_create(&play->m_video_dec_thrd, &attr, video_dec_thrd,
(void*) play);
if (ret)
{
printf("ERROR; return code from pthread_create() is %d\n", ret);
return ret;
}
}
if (play->m_audio_index != -1)
{
ret = pthread_create(&play->m_audio_render_thrd, &attr, audio_render_thrd,
(void*) play);
if (ret)
{
printf("ERROR; return code from pthread_create() is %d\n", ret);
return ret;
}
}
if (play->m_video_index != -1)
{
ret = pthread_create(&play->m_video_render_thrd, &attr, video_render_thrd,
(void*) play);
if (ret)
{
printf("ERROR; return code from pthread_create() is %d\n", ret);
return ret;
}
}
play->m_play_status = playing;
return 0;
}
void configure(avplay *play, void* param, int type)
{
if (type == AUDIO_RENDER)
{
if (play->m_ao_ctx && play->m_ao_ctx->audio_dev)
free_audio_render(play->m_ao_ctx);
play->m_ao_ctx = (ao_context*)param;
}
if (type == VIDEO_RENDER)
{
if (play->m_vo_ctx && play->m_vo_ctx->video_dev)
free_video_render(play->m_vo_ctx);
play->m_vo_ctx = (vo_context*)param;
}
if (type == MEDIA_SOURCE)
{
/* 注意如果正在播放, 则不可以配置应该源. */
if (play->m_play_status == playing ||
play->m_play_status == paused)
return ;
if (play->m_source_ctx)
{
if (play->m_source_ctx && play->m_source_ctx->io_dev)
play->m_source_ctx->close(play->m_source_ctx);
free_media_source(play->m_source_ctx);
play->m_source_ctx = (source_context*)param;
}
}
}
void enable_calc_frame_rate(avplay *play)
{
play->m_enable_calc_frame_rate = 1;
}
void enable_calc_bit_rate(avplay *play)
{
play->m_enable_calc_video_bite = 1;
}
int current_bit_rate(avplay *play)
{
return play->m_real_bit_rate;
}
int current_frame_rate(avplay *play)
{
return play->m_real_frame_rate;
}
void wait_for_completion(avplay *play)
{
while (play->m_play_status == playing ||
play->m_play_status == paused)
{
Sleep(100);
}
}
void wait_for_threads(avplay *play)
{
void *status = NULL;
pthread_join(play->m_read_pkt_thrd, &status);
if (play->m_audio_index != -1)
pthread_join(play->m_audio_dec_thrd, &status);
if (play->m_video_index != -1)
pthread_join(play->m_video_dec_thrd, &status);
if (play->m_audio_index != -1)
pthread_join(play->m_audio_render_thrd, &status);
if (play->m_video_index != -1)
pthread_join(play->m_video_render_thrd, &status);
/* 更改播放状态. */
play->m_play_status = stoped;
}
void av_stop(avplay *play)
{
play->m_abort = TRUE;
if (play->m_source_ctx)
play->m_source_ctx->abort = TRUE;
/* 通知各线程退出. */
play->m_audio_q.abort_request = TRUE;
pthread_cond_signal(&play->m_audio_q.m_cond);
play->m_video_q.abort_request = TRUE;
pthread_cond_signal(&play->m_video_q.m_cond);
play->m_audio_dq.abort_request = TRUE;
pthread_cond_signal(&play->m_audio_dq.m_cond);
play->m_video_dq.abort_request = TRUE;
pthread_cond_signal(&play->m_video_dq.m_cond);
/* 先等线程退出, 再释放资源. */
wait_for_threads(play);
queue_end(&play->m_audio_q);
queue_end(&play->m_video_q);
queue_end(&play->m_audio_dq);
queue_end(&play->m_video_dq);
/* 关闭解码器以及渲染器. */
if (play->m_audio_ctx)
avcodec_close(play->m_audio_ctx);
if (play->m_video_ctx)
avcodec_close(play->m_video_ctx);
if (play->m_format_ctx)
avformat_close_input(&play->m_format_ctx);
if (play->m_swr_ctx)
swr_free(play->m_swr_ctx);
if (play->m_resample_ctx)
audio_resample_close(play->m_resample_ctx);
pthread_mutex_destroy(&play->m_buf_size_mtx);
if (play->m_ao_ctx)
{
free_audio_render(play->m_ao_ctx);
play->m_ao_ctx = NULL;
}
if (play->m_vo_ctx)
{
free_video_render(play->m_vo_ctx);
play->m_vo_ctx = NULL;
}
if (play->m_avio_ctx)
{
av_free(play->m_avio_ctx);
play->m_avio_ctx = NULL;
}
avformat_network_deinit();
}
void av_pause(avplay *play)
{
/* 一直等待为渲染状态时才控制为暂停, 原因是这样可以在暂停时继续渲染而不至于黑屏. */
while (!play->m_rendering)
Sleep(0);
/* 更改播放状态. */
play->m_play_status = paused;
}
void av_resume(avplay *play)
{
/* 更改播放状态. */
play->m_play_status = playing;
}
void av_seek(avplay *play, double fact)
{
double duration = (double)play->m_format_ctx->duration / AV_TIME_BASE;
/* 正在seek中, 只保存当前sec, 在seek完成后, 再seek. */
if (play->m_seeking == SEEKING_FLAG ||
(play->m_seeking > NOSEEKING_FLAG && play->m_seek_req))
{
play->m_seeking = fact * 1000;
return ;
}
/* 正常情况下的seek. */
if (play->m_format_ctx->duration <= 0)
{
uint64_t size = avio_size(play->m_format_ctx->pb);
if (!play->m_seek_req)
{
play->m_seek_req = 1;
play->m_seeking = SEEKING_FLAG;
play->m_seek_pos = fact * size;
play->m_seek_rel = 0;
play->m_seek_flags &= ~AVSEEK_FLAG_BYTE;
play->m_seek_flags |= AVSEEK_FLAG_BYTE;
}
}
else
{
if (!play->m_seek_req)
{
play->m_seek_req = 1;
play->m_seeking = SEEKING_FLAG;
play->m_seek_pos = fact * duration;
play->m_seek_rel = 0;
play->m_seek_flags &= ~AVSEEK_FLAG_BYTE;
/* play->m_seek_flags |= AVSEEK_FLAG_BYTE; */
}
}
}