基于ffmpeg网络播放器的教程与总结

一、         概述

为了解决在线无广告播放youku网上的视频。(youku把每个视频切换成若干个小视频)。

视频资源解析可以从www.flvcd.com获取,此网站根据你输入的优酷的播放网页地址解析成若干个真实的视频地址。

二、         实现

首先搜索关闭网络播放器(流媒体播放器的实现方法)

得出的结论,目前主流的播放器分三大阵营微软,苹果,基于FFmpeg内核的。所以我决定从ffmpeg开源的播放器入手。

最出名的ffmpeg播放器vcl播放器,开源免费。最后选择放弃。

原因

1 依赖于vcl的68M的plugins和libvlccore.dll,libvlc.dll项目生成文件过大。

2即使这样不能解决播放多段视频卡顿现象。

 

最后决定使用ffmpeg官方的ffpaly播放器只有1000多行 (很激动),使用ffmpeg编解码,使用sdl做显示。本想只修改下就行了。结果发现里面代码结构过于复杂,搞懂每行很是吃力。而且是用sdl做显 示,sdl需要句柄。而我这个是为wpf项目量身定做的。Wpf只有顶层窗口有句柄。如果是使用wpf嵌入winform控件。导致此winform控件 只能最上层显示(原因是wpf是directui思想实现的)。所以也放弃了。

 

决定使用ffmpeg库,自己开发

查看http://www.cnblogs.com/Alberl/p/3369187.html 关于ffmpeg开发的总结。对ffmpeg开发有个总体方向。

 

首先我们先把视频搞出来,参考

http://blog.csdn.net/leixiaohua1020/article/details/38868499  100行代码搞定视频。

然后100行搞定音频

http://blog.csdn.net/leixiaohua1020/article/details/38979615

 

这样视频音频都已经搞出来了。但是我们怎么把视频音频一起搞出来呢?

 

Csdn有一份文档

http://download.csdn.net/detail/u012832497/7340751

此文档介绍了用ffmpeg开发视频播放器的详细方法,有注解。但是已经过时了。最新的代码在https://github.com/chelyaev/ffmpeg-tutorial  

但是文档中的思想还是挺受用的。代码不同,思想是通的。

结论,视频包含视频流,音频流,字幕流(一般没有),

音视频同步跟进播放时间戳pts来做的。 视频和音频得出pts的方式有所不同。具体看文档。

 

如果按文档的注释,然后根据github的代码,编译我们发现视频可以显示,音频出现乌拉乌拉的杂音。 此时我参考100行搞定音频http://blog.csdn.net/leixiaohua1020/article/details/38979615

源码修改了github的音频部分。调试运行,可以播放了。

 

 

至此 我们的视频播放器可以播放了 ,使用sdl做显示。那现在我们还是没解决问题。网络播放器,多段无卡顿。

在此基础上我们分析,可以开辟一个线程从网络上下载视频,音频,放入到缓冲队列。音视频播放线程从缓冲区读取数据解析。

这就是网络播放器的原理,而且不会卡顿。其中音视频同步用音频驱动视频的方式实现。显示目前暂用sdl。

 

 

经过上面这些,我们的网络播放器终于可以工作了。那现在只剩下一个wpf句柄问题了。

好在我看到了http://www.cnblogs.com/viki117/archive/2013/05/29/3105417.html

文章里面介绍了vlc播放器c#开源代码,可以使用共享内存。但是说的不够详细

http://libvlcnet.codeplex.com

http://wpfcap.codeplex.com/SourceControl/latest

这两个开源项目都是用共享内存实现的。 参考此两篇文章。我的播放器终于可以播放网络的视频,音频,然后才wpf播放了。

中间有wpf调用c方法的一些细节。

至此我们的问题真的解决了吗?

 

 

NO,因为我们回调函数调用共享内存显示,里面有很多问题,比如当我们关闭程序时会出现访问锁定内存等问题。此问题肯定是可以解决的。但是我们东拼西凑把问题解决了。 当此方案不是最好的。

 

http://www.cnblogs.com/wdysunflower/archive/2011/05/27/2060035.html

http://www.cnblogs.com/scottwong/archive/2010/05/30/1747522.html

 

http://social.msdn.microsoft.com/Forums/zh-CN/a190e03d-f7f7-4ed5-b844-0a51d6eee434/silverlightmediaelementyv12?forum=silverlightzhchs

这3篇文章介绍了怎么使用mediaelement完美解决播放视频问题。

播放器源码可以用http://blog.csdn.net/leixiaohua1020/article/details/28685327

 

下面是我的播放器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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/*
本播放器主要是解决 从优酷上播放视频。 是有多段网络视频组成一个完整视频。
解决方案,开辟两个线程,一个线程从网络中读取数据包放入缓冲池(视频缓冲池和音频缓冲池)
一个线程从音频缓冲池读取数据播放。一个从视频缓冲池中读取播放.
 
难点1:av_read_frame是读取packet(包) 数据, 几包数据 组成avframe(帧)
音频帧转换成byte[] 存储起来 放入缓冲池 吃音频byte[]可以直接放入音频流中播放
视频帧也是byte[]  存储起来,此视频byte[]数组可以转换为图片 PIX_FMT_RGB24
为了同步音视频,我们把没帧的最后一包的pts记录下来放入缓冲区
 
*/
#include "stdafx.h"
#include "BonkerPlayer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "libswscale/swscale.h"
#include <libavutil/avstring.h>
    //SDL
#include "sdl/SDL.h"
#include "sdl/SDL_thread.h"
};
 
#define VideoBufferMaxSize 80//2048  //视频缓冲区最大值,大于此值 则不下载数据
#define VideoBufferMinSize 20//1024  //视频缓冲区最小值,小于此值,则唤醒下载
#define AudioBufferMaxSize 80//2048  //音频缓冲区最大值,大于此值 则不下载数据
#define AudioBufferMinSize 20//1024  //音频缓冲区最小值,小于此值,则唤醒下载
#define SDL_AUDIO_BUFFER_SIZE 1024 //音频流的缓冲区
 
//#define VideoType PIX_FMT_YUV420P //视频转换的格式
#define VideoType PIX_FMT_BGR24 //视频转换的格式
 
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio 
 
//static char ErrorMsg[100]="";//错误的提示信息
int FileDuration=0;//视频的长度  单位秒
int flag=100;//标识 播放,暂停,退出 0退出,1标识,2暂停
 
//声明了函数指针
DispalyVideoDele Fn=NULL;
 
 
Uint32  audio_len;
Uint8  *audio_pos;
double currentAudioClock=0;//当前音频播放时间
double currentVideoClock=0;//当前视频播放时间
double currentBufferClock=0;//当前以缓冲的时间,用于缓冲进度条
//double currentPlayClock=0;//当前播放的时间,用于播放进度条
double diffClock=0.2;//音视频相差的死区
int CurrentVolume=SDL_MIX_MAXVOLUME/2;//当前声音的大小
 
 
 
 
SDL_Thread *decodeTid=NULL;//解码线程
SDL_Thread *PlayVideoTid=NULL;//视频播放线程
SDL_Thread *PlayAudioTid=NULL;//音频播放线程
 
 
//快进的参数
bool isSeek=false;//是否在快进
int global_seek_index=0;//文件索引 快进
double globle_seek_pos=0;//快进的地方
 
//存储音频的队列
typedef struct AudioItem
{
    Uint8 *AudioData;//音频数据
    int Length;//音频长度
    double Pts;//时间戳
    AudioItem *Next;//尾部
    SDL_AudioSpec *wanted_spec;
}AudioQueueItem;
typedef struct
{
    AudioQueueItem *FirstItem;//队列头
    AudioQueueItem *LastItem;//队列位
    int Length;//队列长度
    SDL_mutex *audioMutex;//用于同步两个线程同时操作队列的 互斥量
    SDL_cond *audioCond;//唤醒线程
}AudioQueue;
 
//存储视频的队列
typedef struct VideoItem
{
    Uint8 *VideoData;//音频数据
    int Width;//视频图片的宽度
    int Height;//视频图片的高度
    int Length;//视频长度
    double Pts;//时间戳
    VideoItem *Next;//尾部
}VideoQueueItem;
typedef struct
{
    VideoQueueItem *FirstItem;//队列头
    VideoQueueItem *LastItem;//队列位
    int Length;//队列长度
    double BufferPts;//缓冲的pts
    SDL_mutex *videoMutex;//用于同步两个线程同时操作队列的 互斥量
    SDL_cond *videoCond;//唤醒线程
}VideoQueue;
 
 
VideoQueue *videoQueue=NULL;//视频队列
AudioQueue *audioQueue=NULL;//音频队列
 
//清空视频队列
void VideoQueueClear(VideoQueue *vq)
{
    VideoItem *item,*temp;
    SDL_LockMutex(vq->videoMutex);
    for (item=vq->FirstItem; item!=NULL; item=temp)
    {
        temp=item->Next;//
        av_free(item->VideoData);//释放video里面的数据
        av_free(item);
        vq->Length--;
    }
    vq->FirstItem=NULL;
    vq->LastItem=NULL;
    SDL_UnlockMutex(vq->videoMutex);
}
//清空音频队列
void AudioQueueClear(AudioQueue *aq)
{
    AudioItem *item,*temp;
    SDL_LockMutex(aq->audioMutex);
    for (item=aq->FirstItem; item!=NULL; item=temp)
    {
        temp=item->Next;//
        av_free(item->AudioData);//释放video里面的数据
        av_free(item->wanted_spec);
        av_free(item);
        aq->Length--;
    }
    aq->FirstItem=NULL;
    aq->LastItem=NULL;
    SDL_UnlockMutex(aq->audioMutex);
}
//初始化视频队列
void VideoQueueInit(VideoQueue *vq)
{
    memset(vq, 0, sizeof(VideoQueue));//初始化首地址为0
    vq->videoMutex=SDL_CreateMutex();
    vq->videoCond=SDL_CreateCond();
}
//初始化音频队列
void AudioQueueInit(AudioQueue *aq)
{
    memset(aq,0,sizeof(AudioQueue));
    aq->audioMutex=SDL_CreateMutex();
    aq->audioCond=SDL_CreateCond();
}
//向队列添加数据
int VideoQueuePut(VideoQueue *vq,VideoQueueItem *item)
{
    int result=0;
    SDL_LockMutex(vq->videoMutex);//加锁
    if(vq->Length<VideoBufferMaxSize)
    {
        if(!vq->FirstItem)//第一个item为null 则队列是空的
        {
            vq->FirstItem=item;
            vq->LastItem=item;
            vq->Length=1;
            vq->BufferPts=item->Pts;
        }
        else
        {
            vq->LastItem->Next=item;//添加到队列后面
            vq->Length++;
            vq->LastItem=item;//此item变成队列尾部
            vq->BufferPts=item->Pts;
        }
        if(vq->Length>=VideoBufferMinSize)
        {
            SDL_CondSignal(vq->videoCond);//唤醒其他线程  如果缓冲区里面有几个数据后再唤醒 较好
        }
        result=1;
    }
    else
    {
        SDL_CondWait(vq->videoCond,vq->videoMutex);//解锁  等待被唤醒
    }
    SDL_UnlockMutex(vq->videoMutex);//解锁
    return result;
}
//向队列中取出数据,放入item中
int  VideoQueueGet(VideoQueue *vq,VideoQueueItem *item)
{
    int result=0;
    SDL_LockMutex(vq->videoMutex);
    if(vq->Length>0)
    {
        if(vq->FirstItem)//有数据
        {
            *item=*(vq->FirstItem);
            if(!vq->FirstItem->Next)//只有一个
            {
                vq->FirstItem=NULL;
                vq->LastItem=NULL;
            }else
            {
                vq->FirstItem=vq->FirstItem->Next;
            }
            vq->Length--;
            item->Next=NULL;
            result= 1;
        }
        if(vq->Length<=VideoBufferMinSize)
        {
            SDL_CondSignal(vq->videoCond);//唤醒下载线程
        }
    }
    else
    {
        SDL_CondWait(vq->videoCond,vq->videoMutex);//解锁  等待被唤醒
    }
 
    SDL_UnlockMutex(vq->videoMutex);
    return result;
}
 
//向队列添加数据
int AudioQueuePut(AudioQueue *aq,AudioQueueItem *item)
{
    int result=0;
    SDL_LockMutex(aq->audioMutex);//加锁
    if(aq->Length<AudioBufferMaxSize)
    {
        if(!aq->FirstItem)//第一个item为null 则队列是空的
        {
            aq->FirstItem=item;
            aq->LastItem=item;
            aq->Length=1;
        }
        else
        {
            aq->LastItem->Next=item;//添加到队列后面
            aq->Length++;
            aq->LastItem=item;//此item变成队列尾部
        }
 
        if(aq->Length>=AudioBufferMinSize)
        {
            SDL_CondSignal(aq->audioCond);//唤醒其他线程  如果缓冲区里面有几个数据后再唤醒 较好
        }
        result=1;
    }
    else///音频缓冲区的大小 大于设定值 则让线程等待
    {
        SDL_CondWait(aq->audioCond,aq->audioMutex);//解锁  等待被唤醒
    }
    SDL_UnlockMutex(aq->audioMutex);//解锁
    return result;
}
//向队列中取出数据,放入item中
int AudioQueueGet(AudioQueue *aq,AudioQueueItem *item)
{
    int result=0;
    SDL_LockMutex(aq->audioMutex);
    if(aq->Length>0)
    {
        if(aq->FirstItem)//有数据
        {
            *item=*(aq->FirstItem);
            if(!aq->FirstItem->Next)//只有一个
            {
                aq->FirstItem=NULL;
                aq->LastItem=NULL;
            }else
            {
                aq->FirstItem=aq->FirstItem->Next;
            }
            aq->Length--;
            item->Next=NULL;
            result=1;
        }
        if(aq->Length<=AudioBufferMinSize)
        {
            SDL_CondSignal(aq->audioCond);//唤醒下载线程
        }
    }else
    {
        SDL_CondWait(aq->audioCond,aq->audioMutex);//解锁  等待被唤醒
    }
    SDL_UnlockMutex(aq->audioMutex);
    return result;
}
 
 
 
 
//输出声音的回调函数
void AudioCallback(void *udata,Uint8 *stream,int len)
{  
    //SDL 2.0
    SDL_memset(stream, 0, len);
    if(audio_len==0)        /*  Only  play  if  we  have  data  left  */
        return;
    len=(len>audio_len?audio_len:len);   /*  Mix  as  much  data  as  possible  */
 
    SDL_MixAudio(stream,audio_pos,len,CurrentVolume);
    audio_pos += len;
    audio_len -= len;
}  
//下载视频和音频流并 解码  并放入相应的队列中
int DecodePacket(void *arg)
{  
    VideoState *vs=(VideoState *)arg;
    int length=vs->Length;
    double currentAllFilePts=0;
    av_register_all();  //注册所有解码器
    avformat_network_init();  //初始化流媒体格式   
    for (int j = 0; j < length; j++)
    {
        double currentFilePts=0;
 
 
        char* url=vs->Urls[j];
        AVFormatContext *pFormatCtx;
        pFormatCtx = avformat_alloc_context(); 
        //打卡文件
        if(avformat_open_input(&pFormatCtx,url,NULL,NULL)!=0)
        
            //strcpy(ErrorMsg,"无法打开网络流");
            return -1;
        }
        //avformat_close_input
        if(av_find_stream_info(pFormatCtx)<0) 
        
            //strcpy(ErrorMsg,"无法获取流信息"); 
            return -1; 
        }
        //获取此视频的总时间 微妙转化为妙
        //FileDuration+= pFormatCtx->duration/1000000;
        //把一个文件拆分为视频流和音频流
        int videoIndex=-1,audioIndex=-1; 
        int i=0;
        //获取音频流和视频流的索引
        for(i=0; i<pFormatCtx->nb_streams; i++)  
        {
            if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO) 
            
                videoIndex=i; 
            
            else if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
            {
                audioIndex=i;
            }
        }
 
        AVCodecContext *pCodecCtx,*aCodecCtx;//视频,音频的解码器上下文
        AVCodec *pCodec,*aCodec;//视频,音频解码器
        if(videoIndex!=-1)
        {
            //视频解码器上下文,
            pCodecCtx=pFormatCtx->streams[videoIndex]->codec; 
            pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
        }
        else
        {
        }
 
        if(audioIndex!=-1)
        {
            //音频解码器上下文
            aCodecCtx=pFormatCtx->streams[audioIndex]->codec; 
            aCodec=avcodec_find_decoder(aCodecCtx->codec_id);
        }
        else
        {
        }
 
        if(videoIndex!=-1)
        {
            //打开解码器
            if(avcodec_open2(pCodecCtx, pCodec,NULL)<0) 
            
                //strcpy(ErrorMsg,"无法打开视频解码器"); 
                return -1; 
            
        }
        else
        {
        }
 
        if(audioIndex!=-1)
        {
            if(avcodec_open2(aCodecCtx, aCodec,NULL)<0) 
            
                //strcpy(ErrorMsg,"无法打开音频解码器"); 
                return -1; 
            }  
        }
        else
        {
        }
 
        AVPacket *packet=(AVPacket *)av_mallocz(sizeof(AVPacket)); 
        AVFrame *pFrame=avcodec_alloc_frame();
        AVFrame *pFrameRGB=avcodec_alloc_frame();
        int frameFinished=0;//是否凑成一帧数据
        int result=0;//标识一个视频是否解码完毕
        int audioLength=0;//音频数组的长度
        int videoLength=0;//视频数组的长度
 
 
        //把视频帧转化为数组参数
        struct SwsContext *img_convert_ctx;
        if(videoIndex!=-1)
        {
            videoLength=avpicture_get_size(VideoType, pCodecCtx->width, pCodecCtx->height);
 
            img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, VideoType, SWS_BICUBIC, NULL, NULL, NULL);
        }
 
        //把音频帧转化为数组的参数
        //uint64_t out_channel_layout=AV_CH_LAYOUT_STEREO; 
        AVSampleFormat out_sample_fmt=AV_SAMPLE_FMT_S16;
        int out_sample_rate=44100;
        int64_t in_channel_layout=av_get_channel_layout_nb_channels(aCodecCtx->channels);
 
        int out_channels=av_get_channel_layout_nb_channels(aCodecCtx->channels);
        int out_nb_samples=1024;
        audioLength=av_samples_get_buffer_size(NULL,out_channels ,out_nb_samples,out_sample_fmt, 1);
 
        struct SwrContext *au_convert_ctx; 
        au_convert_ctx = swr_alloc(); 
        au_convert_ctx=swr_alloc_set_opts(au_convert_ctx,aCodecCtx->channels, out_sample_fmt, out_sample_rate, 
            in_channel_layout,aCodecCtx->sample_fmt , aCodecCtx->sample_rate,0, NULL); 
        swr_init(au_convert_ctx); 
        int sample=SDL_AUDIO_BUFFER_SIZE;
 
 
        //解码一包数据,一帧数据有多包
        while(flag!=0&&av_read_frame(pFormatCtx, packet)>=0) 
        {
            if(isSeek)//要快进
            {
                //做快进
                if(j==global_seek_index)
                {
                    int seekFlag=avformat_seek_file(pFormatCtx, -1, (globle_seek_pos-10)* AV_TIME_BASE, globle_seek_pos * AV_TIME_BASE, (globle_seek_pos+10)* AV_TIME_BASE, AVSEEK_FLAG_ANY);
                    if(seekFlag>=0)
                    {
                        currentAllFilePts=0;
                        for (int k = 0; k < j; k++)
                        {
                            currentAllFilePts+=vs->times[k];
                        }
                    }
                    //av_seek_frame(pFormatCtx, -1 , globle_seek_pos * AV_TIME_BASE, AVSEEK_FLAG_ANY);
                    isSeek=false;
                }else
                {
                    j=global_seek_index-1;
                    break;
                }
 
            }
            if(flag==0)//退出
            {
                break;
            }else if(flag==1)//播放
            {
 
            }else if(flag==2)
            {
                SDL_Delay(1);
                continue;
            }
            frameFinished=0;
            //视频数据包 添加到视频队列中
            if(packet->stream_index==videoIndex) 
            
 
                //把数据包转换为数据帧
                result=avcodec_decode_video2(pCodecCtx,pFrame,&frameFinished,packet);
 
                double pts=0;
                if(packet->dts == AV_NOPTS_VALUE
                    && pFrame->opaque && *(uint64_t*)pFrame->opaque != AV_NOPTS_VALUE) {
                        pts = *(uint64_t *)pFrame->opaque;
                } else if(packet->dts != AV_NOPTS_VALUE) {
                    pts = packet->dts;
                } else {
                    pts = 0;
                }
                pts *= av_q2d(pFormatCtx->streams[videoIndex]->time_base);
 
                //printf("+readVideo %d ",videoQueue->Length);
                if(result<0)//一个视频解码结束了
                {
                    break;//跳出循环,继续解码下一个视频
                }
 
                if(frameFinished)//解析成了一帧数据,转化为字节数组存放队列中
                {
 
                    uint8_t *bufferRGB=(uint8_t *)av_mallocz(videoLength);
                    avpicture_fill((AVPicture *)pFrameRGB, bufferRGB, VideoType, pCodecCtx->width, pCodecCtx->height);
 
                    sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
 
                    //创建视频item
                    VideoQueueItem *videoItem;
                    videoItem=(VideoQueueItem *)av_mallocz(sizeof(VideoQueueItem));
                    videoItem->Height=pCodecCtx->height;
                    videoItem->Width=pCodecCtx->width;
                    videoItem->VideoData=bufferRGB;
                    //videoItem->Length=videoLength;
                    //videoItem->VideoData=pFrameRGB->data[0];
                    videoItem->Length=pFrameRGB->linesize[0];
                    //获取显示时间戳pts
 
 
                    currentFilePts=pts;
                    videoItem->Pts = currentAllFilePts+currentFilePts;//音频绝对pts;
                    videoItem->Next=NULL;
                    //添加到队列中
                    while(flag!=0&&!VideoQueuePut(videoQueue,videoItem));
                    //av_free(bufferRGB);//释放
                }
            }//音频数据包 ,添加到音频队列中
            else if(packet->stream_index==audioIndex)
            {
                result= avcodec_decode_audio4( aCodecCtx, pFrame,&frameFinished, packet);
 
 
                double pts=0;
                if(packet->dts == AV_NOPTS_VALUE
                    && pFrame->opaque && *(uint64_t*)pFrame->opaque != AV_NOPTS_VALUE) {
                        pts = *(uint64_t *)pFrame->opaque;
                } else if(packet->dts != AV_NOPTS_VALUE) {
                    pts = packet->dts;
                } else {
                    pts = 0;
                }
                pts *= av_q2d(pFormatCtx->streams[videoIndex]->time_base);
                //printf("+readAudio %d ",audioQueue->Length);
                if(result<0)//一个视频解码结束了
                {
                    break;//跳出循环,继续解码下一个视频
                }
 
 
                if(frameFinished)//解析成了一帧数据,转化为字节数组存放队列中
                {
                    uint8_t *out_buffer=(uint8_t *)av_mallocz(MAX_AUDIO_FRAME_SIZE*2);
                    swr_convert(au_convert_ctx,&out_buffer, MAX_AUDIO_FRAME_SIZE,(const uint8_t **)pFrame->data , pFrame->nb_samples);
                    //创建音频Item
                    AudioItem *audioItem;
                    audioItem=(AudioItem *)av_mallocz(sizeof(AudioItem));
                    audioItem->AudioData=out_buffer;
                    audioItem->Length=audioLength;
                    //获取显示时间戳pts
 
                    currentFilePts=pts;
                    audioItem->Pts =currentAllFilePts+currentFilePts;//音频绝对pts
 
 
                    SDL_AudioSpec *wanted_spec=(SDL_AudioSpec *)av_mallocz(sizeof(SDL_AudioSpec));;  //音频设置
                    //初始化音频设置
                    wanted_spec->silence = 0;  
                    wanted_spec->samples = sample;   
                    wanted_spec->format = AUDIO_S16SYS;   
 
                    wanted_spec->freq =aCodecCtx->sample_rate;
                    wanted_spec->channels = out_channels;  
                    wanted_spec->userdata = aCodecCtx;
 
                    wanted_spec->callback = AudioCallback; 
                    if(wanted_spec->samples!=pFrame->nb_samples){ 
                        //SDL_CloseAudio(); 
                        out_nb_samples=pFrame->nb_samples; 
                        audioLength=av_samples_get_buffer_size(NULL,out_channels ,out_nb_samples,out_sample_fmt, 1); 
 
                        wanted_spec->samples=out_nb_samples; 
                        wanted_spec->freq=aCodecCtx->sample_rate;
                        //SDL_OpenAudio(&wanted_spec, NULL); 
                    
                    audioItem->wanted_spec=wanted_spec;
                    //添加到队列中
 
                    audioItem->Next=NULL;
                    while(flag!=0&&!AudioQueuePut(audioQueue,audioItem));
                }
            }
 
            av_free_packet(packet);  //释放内存
        }
        av_free(img_convert_ctx);
        av_free(au_convert_ctx);
        av_free(pFrame);
        av_free(pFrameRGB);
 
        avcodec_close(pCodecCtx);
        avcodec_close(aCodecCtx);
        avformat_close_input(&pFormatCtx);
        currentAllFilePts+=currentFilePts;//把这一段视频地址 累加
        if(flag==0)
        {
            return 1;
        }
 
    }
 
 
    avformat_network_deinit();
    flag=3;//解码结束了
    return 1;
}
 
 
//播放视频线程
int PlayVideo(void *arg)
{
    while (flag!=0&&true)
    {
        if(flag==2)// 暂停
        {
            SDL_Delay(1);
            continue;
        }else if(flag==0)//退出
        {
            return -1;
        }else if(flag==1)//播放
        {
 
        }else if(flag==3)//解码结束了
        {
            //播放结束了
            if(audioQueue->Length<=0&&videoQueue->Length<=0)
            {
                break;
            }
        }
        //视频快于音频 则等待
        if(currentVideoClock>=currentAudioClock+diffClock)
        {
            //音频队中有数据,这样判断是因为,当只有视频时,视频满了,就可以播放了
            if(audioQueue->Length>0&&videoQueue->Length<VideoBufferMaxSize)
            {
                SDL_Delay(1);
                continue;
            }
        }
        VideoItem *videoItem=(VideoItem *)av_mallocz(sizeof(VideoItem));
        //从队列中拿出视频数据
        if(VideoQueueGet( videoQueue,videoItem))
        {
            currentVideoClock=videoItem->Pts;//当前视频时间戳
            if(Fn)
            {
                Fn((unsigned char *)videoItem->VideoData,videoItem->Width,videoItem->Height,videoItem->Pts,videoQueue->BufferPts);
            }
 
            av_free(videoItem->VideoData);
        }
        av_free(videoItem);
 
    }
    return 1;
}
//播放音频线程
int PlayAudio(void *arg)
{
    if(SDL_Init( SDL_INIT_AUDIO | SDL_INIT_TIMER)) {   
        printf( "Could not initialize SDL - %s ", SDL_GetError());  
        return -1; 
    }  
    bool isOpenAudio=false;
    int samples=0;
    SDL_AudioSpec  spec;
    while (true&&flag!=0)
 
    {
        if(flag==2)// 暂停
        {
            SDL_Delay(1);
            continue;
        }else if(flag==0)//退出
        {
            return -1;
        }else if(flag==1)//播放
        {
 
        }else if(flag==3)//解码结束了
        {
            //播放结束了
            if(audioQueue->Length<=0&&videoQueue->Length<=0)
            {
                break;
            }
        }
        //音频快于视频 则加锁
        if(currentAudioClock>=currentVideoClock+diffClock)
        {
            if(videoQueue->Length>0&&audioQueue->Length<AudioBufferMaxSize)
            {
                SDL_Delay(1);
                continue;
            }
        }
        AudioItem *audioItem=(AudioItem *)av_mallocz(sizeof(AudioItem));
        //从队列中拿出音频数据
        if( AudioQueueGet( audioQueue,audioItem))
        {
            if(!isOpenAudio)
            {
                SDL_CloseAudio();
                int re=SDL_OpenAudio(audioItem->wanted_spec, &spec);
                samples=audioItem->wanted_spec->samples;
                isOpenAudio=true;
            }
            else
            {
                if(audioItem==NULL)
                {
                    continue;
                }
                if(samples!=audioItem->wanted_spec->samples)
                {
                    SDL_CloseAudio();
                    int re=SDL_OpenAudio(audioItem->wanted_spec, &spec);
                    samples=audioItem->wanted_spec->samples;
                    isOpenAudio=true;
                }
            }
            currentAudioClock=audioItem->Pts;//当前音频时间戳
 
 
            audio_pos=audioItem->AudioData;
            audio_len=audioItem->Length;
            SDL_PauseAudio(0);
 
 
            while(audio_len>0&&flag!=0)
                SDL_Delay(5);
 
            av_free(audioItem->AudioData);
            av_free(audioItem->wanted_spec);
 
        }
        av_free(audioItem);
    }
 
    SDL_CondSignal(audioQueue->audioCond);//唤醒其他线程  如果缓冲区里面有几个数据后再唤醒 较好
 
    SDL_CondSignal(videoQueue->videoCond);//唤醒其他线程  如果缓冲区里面有几个数据后再唤醒 较好
    return 1;
}
 
int _tmain1(VideoState *vs)
{
    currentAudioClock=0;
    currentVideoClock=0;
    currentBufferClock=0;
    //currentPlayClock=0;
    CurrentVolume=SDL_MIX_MAXVOLUME/2;
 
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
        fprintf(stderr, "Unable to initialize SDL:  %s ", SDL_GetError());
        return 1;
    }
    atexit(SDL_Quit);
    //atexit(SDL_Quit);// 注册SDL_Quit,当退出时调用,使得退出时程序自动清理
    //flag=2;
    //给音视频队列分配空间
    videoQueue=(VideoQueue *)av_mallocz(sizeof(VideoQueue));
    audioQueue=(AudioQueue *)av_mallocz(sizeof(AudioQueue));
 
    //初始化音视频队列
    VideoQueueInit(videoQueue);
    AudioQueueInit(audioQueue);
 
 
    decodeTid=SDL_CreateThread(DecodePacket,"DecodePacket",vs);
 
    PlayVideoTid=SDL_CreateThread(PlayVideo,"PlayVideo",NULL);
    PlayAudioTid=SDL_CreateThread(PlayAudio,"PlayAudioTid",NULL);
 
    return 1;
}
//获取视频的总长度
void InitAllTime(VideoState *vs)
{
    FileDuration=0;
    int length=vs->Length;
    av_register_all();  //注册所有解码器
    avformat_network_init();  //初始化流媒体格式   
    for (int j = 0; j < length; j++)
    {
        char* url=vs->Urls[j];
        AVFormatContext *pFormatCtx = avformat_alloc_context(); 
        //打卡文件
        if(avformat_open_input(&pFormatCtx,url,NULL,NULL)!=0)
        
            //strcpy(ErrorMsg,"无法打开网络流");
            return;
        }
        if(av_find_stream_info(pFormatCtx)<0) 
        
            //strcpy(ErrorMsg,"无法获取流信息"); 
            return
        }
        //保存每个文件的播放长度
        vs->times[j]=pFormatCtx->duration/1000000;
        //获取此视频的总时间 微妙转化为妙
        FileDuration+= vs->times[j];
 
        avformat_close_input(&pFormatCtx);
    }
    avformat_network_deinit();
}
void bonker_pause()
{
    flag=2;
}
 
VideoState *_vs;
//获取视频总长度
double bonker_gettime()
{
    FileDuration=0;
    if(_vs!=NULL)
    {
        InitAllTime(_vs);
    }
    return FileDuration;
}
void bonker_open()
{
    if(_vs!=NULL)
    {      
        _tmain1(_vs);
    }else
    {  
    }
}
void bonker_play()
{
    flag=1;
}
 
void bonker_quit()
{
    bonker_close();
}
void bonker_init(DispalyVideoDele _fn)
{
 
    FileDuration=0;//视频的长度  单位秒
    flag=2;//标识 播放,暂停,退出 0退出,1标识,2暂停
 
 
 
    audio_len=0;
    currentAudioClock=0;//当前音频播放时间
    currentVideoClock=0;//当前视频播放时间
    currentBufferClock=0;//当前以缓冲的时间,用于缓冲进度条
    //currentPlayClock=0;//当前播放的时间,用于播放进度条
    diffClock=0.2;//音视频相差的死区
    CurrentVolume=SDL_MIX_MAXVOLUME/2;//当前声音的大小
 
 
 
    //快进的参数
    isSeek=false;//是否在快进
    global_seek_index=0;//文件索引 快进
    globle_seek_pos=0;//快进的地方
 
 
    Fn=_fn;
}
void bonker_addurl(char *vs)
{
    if(_vs==NULL)
    {
        _vs=(VideoState *)av_mallocz(sizeof(VideoState));
        _vs->Length=0;
    }
    av_strlcpy(_vs->Urls[_vs->Length],vs,UrlLength);
    _vs->Length++;
}
//释放内存资源
void bonker_close()
{
    flag=0;//退出
    SDL_CloseAudio();
    if(videoQueue!=NULL)
    {
        SDL_CondSignal(videoQueue->videoCond);
    }
    if(audioQueue!=NULL)
    {
        SDL_CondSignal(audioQueue->audioCond);
    }
    SDL_Delay(10);
 
    SDL_WaitThread(PlayVideoTid,NULL);
    SDL_WaitThread(PlayAudioTid,NULL);
    SDL_WaitThread(decodeTid,NULL);
 
    if(videoQueue!=NULL)
    {
        VideoQueueClear(videoQueue);//释放视频队列
        //videoQueue=NULL;
    }
    if(audioQueue!=NULL)
    {
        AudioQueueClear(audioQueue);
        //audioQueue=NULL;
    }
 
 
 
 
    SDL_DestroyMutex(audioQueue->audioMutex);
    SDL_DestroyCond(audioQueue->audioCond);
    SDL_DestroyMutex(videoQueue->videoMutex);
    SDL_DestroyCond(videoQueue->videoCond);
    av_free(audioQueue);
    av_free(videoQueue);
    flag=2;
 
    /*SDL_DetachThread(decodeTid);
    SDL_DetachThread(PlayVideoTid);
    SDL_DetachThread(PlayAudioTid);*/
 
    if(_vs!=NULL)
    {
        av_free(_vs);
    }
    _vs=NULL;
    SDL_Quit();
    //关闭解码线程,视频播放线程,音频播放线程
    /*if(decodeTid!=NULL)
    {
    decodeTid=NULL;
    }
    if(PlayVideoTid!=NULL)
    {
    PlayVideoTid=NULL;
    }
    if(PlayAudioTid!=NULL)
    {
    PlayAudioTid=NULL;
    }*/
}
//设置声音
void bonker_set_volumn(int volume)
{
    if(volume>=0&&volume<=128)
    {
        CurrentVolume=volume;
    }
}
//获取音量
int bonker_get_volume()
{
    return CurrentVolume;
}
 
 
//快进 快退
void bonker_seek(double seek_pos)
{
    bool flagTemp=flag;
    flag=2;
    SDL_Delay(50);
    //当快进的时间在缓冲区内
    if(seek_pos>=currentVideoClock&&seek_pos<=videoQueue->BufferPts)
    {
        //清空之前的音频
        AudioItem *item,*temp;
        SDL_LockMutex(audioQueue->audioMutex);
        for (item=audioQueue->FirstItem; item!=NULL; item=temp)
        {
            temp=item->Next;//
            av_free(item->AudioData);//释放video里面的数据
            av_free(item->wanted_spec);
            av_free(item);
            audioQueue->Length--;
            if(temp!=NULL)
            {
                if(temp->Pts>=seek_pos)//目前缓冲区,大于此跳转位置
                {
                    break;
                }
            }else
            {
                audioQueue->FirstItem=NULL;
                audioQueue->LastItem=NULL;
            }
        }
        SDL_UnlockMutex(audioQueue->audioMutex);
 
        //清空之前的视频
        VideoItem *item1,*temp1;
        SDL_LockMutex(videoQueue->videoMutex);
        for (item1=videoQueue->FirstItem; item1!=NULL; item1=temp1)
        {
            temp1=item1->Next;//
            av_free(item1->VideoData);//释放video里面的数据
            av_free(item1);
 
            videoQueue->Length--;
            if(temp1!=NULL)
            {
                if(temp1->Pts>=seek_pos)//目前缓冲区,大于此跳转位置
                {
                    break;
                }
            }
            else
            {
                videoQueue->FirstItem=NULL;
                videoQueue->LastItem=NULL;
 
            }
        }
        SDL_UnlockMutex(videoQueue->videoMutex);
    }
    else if(seek_pos>=0&&seek_pos<=FileDuration)//用av_seek_file,计算那个文件,
    {
        double pos=0;
        for (int i = 0; i < _vs->Length; i++)
        {
            pos+=_vs->times[i];
            if(pos>seek_pos)//就在这个文件内
            {
                isSeek=true;
                global_seek_index=i;
                globle_seek_pos=seek_pos- pos+_vs->times[i];//此文件快进到的位置
                break;
            }
        }
        //清空缓冲区
        VideoQueueClear(videoQueue);
        AudioQueueClear(audioQueue);
 
    }
    flag=flagTemp;
    SDL_CondSignal(audioQueue->audioCond);//唤醒其他线程  如果缓冲区里面有几个数据后再唤醒 较好
 
    SDL_CondSignal(videoQueue->videoCond);//唤醒其他线程  如果缓冲区里面有几个数据后再唤醒 较好
}

  

原文地址:https://www.cnblogs.com/sunminmin/p/4512590.html