Skip to content

Commit c1fb401

Browse files
committed
FFmpeg音视频解码播放
1 parent 64e00b1 commit c1fb401

13 files changed

Lines changed: 805 additions & 18 deletions

‎app/CMakeLists.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ add_library( # Sets the name of the library.
2626
src/main/cpp/audio_player.c
2727
src/main/cpp/openSL_audio_player.c
2828
src/main/cpp/video_player.c
29-
src/main/cpp/ffmpeg_pusher.cpp)
29+
src/main/cpp/ffmpeg_pusher.cpp
30+
src/main/cpp/AVpacket_queue.c
31+
src/main/cpp/media_player.c)
3032

3133
add_library( ffmpeg
3234
SHARED

‎app/src/main/AndroidManifest.xml‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@
2727
<category android:name="android.intent.category.LAUNCHER" />
2828
</intent-filter>
2929
</activity>
30-
<!--音频处理-->
30+
<!-- 音频处理 -->
3131
<activity android:name=".activity.AudioHandleActivity" />
32-
<!--音视频处理-->
32+
<!-- 音视频处理 -->
3333
<activity android:name=".activity.MediaHandleActivity" />
34-
<!--视频处理-->
34+
<!-- 视频处理 -->
3535
<activity android:name=".activity.VideoHandleActivity" />
36-
<!--视频解码播放-->
36+
<!-- 视频解码播放 -->
3737
<activity
3838
android:name=".activity.VideoPlayerActivity"
39+
android:screenOrientation="landscape" />
40+
<!-- 推流直播 -->
41+
<activity android:name=".activity.PushActivity" />
42+
<!--音视频解码播放-->
43+
<activity android:name=".activity.MediaPlayerActivity"
3944
android:screenOrientation="landscape"/>
40-
<!--推流直播-->
41-
<activity android:name=".activity.PushActivity"/>
42-
4345
</application>
4446

4547
</manifest>

‎app/src/main/cpp/AVpacket_queue.c‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Created by frank on 2018/2/3.
3+
//
4+
#include "AVpacket_queue.h"
5+
#include <stdlib.h>
6+
#include <libavcodec/avcodec.h>
7+
8+
9+
AVPacketQueue* queue_init(int size){
10+
AVPacketQueue* queue = malloc(sizeof(AVPacketQueue));
11+
queue->size = size;
12+
queue->next_to_read = 0;
13+
queue->next_to_write = 0;
14+
int i;
15+
queue->packets = malloc(sizeof(*queue->packets) * size);
16+
for(i = 0; i < size; i++){
17+
queue->packets[i] = malloc(sizeof(AVPacket));
18+
}
19+
return queue;
20+
}
21+
22+
void queue_free(AVPacketQueue *queue){
23+
int i;
24+
for(i=0; i<queue->size; i++){
25+
free(queue->packets[i]);
26+
}
27+
free(queue->packets);
28+
free(queue);
29+
}
30+
31+
int queue_next(AVPacketQueue* queue, int current){
32+
return (current+1) % queue->size;
33+
}
34+
35+
void* queue_push(AVPacketQueue* queue, pthread_mutex_t* mutex, pthread_cond_t* cond){
36+
int current = queue->next_to_write;
37+
int next_to_write;
38+
for(;;){
39+
next_to_write = queue_next(queue, current);
40+
//写的不等于读的,跳出循环
41+
if(next_to_write != queue->next_to_read){
42+
break;
43+
}
44+
pthread_cond_wait(cond, mutex);
45+
}
46+
queue->next_to_write = next_to_write;
47+
pthread_cond_broadcast(cond);
48+
return queue->packets[current];
49+
}
50+
51+
void* queue_pop(AVPacketQueue* queue, pthread_mutex_t* mutex, pthread_cond_t* cond){
52+
int current = queue->next_to_read;
53+
for(;;){
54+
//写的不等于读的,跳出循环
55+
if(queue->next_to_write != queue->next_to_read){
56+
break;
57+
}
58+
pthread_cond_wait(cond, mutex);
59+
}
60+
queue->next_to_read = queue_next(queue, current);
61+
pthread_cond_broadcast(cond);
62+
return queue->packets[current];
63+
}

‎app/src/main/cpp/AVpacket_queue.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Created by frank on 2018/2/3.
3+
//
4+
5+
#ifndef VIDEOPLAYER_AVPACKET_QUEUE_H
6+
#define VIDEOPLAYER_AVPACKET_QUEUE_H
7+
#include <pthread.h>
8+
9+
typedef struct AVPacketQueue{
10+
//队列大小
11+
int size;
12+
//指针数组
13+
void ** packets;
14+
//下一个写入的packet
15+
int next_to_write;
16+
//下一个读取的packet
17+
int next_to_read;
18+
}AVPacketQueue;
19+
20+
AVPacketQueue* queue_init(int size);
21+
void queue_free(AVPacketQueue *queue);
22+
void* queue_push(AVPacketQueue* queue, pthread_mutex_t* mutex, pthread_cond_t* cond);
23+
void* queue_pop(AVPacketQueue* queue, pthread_mutex_t* mutex, pthread_cond_t* cond);
24+
25+
#endif //VIDEOPLAYER_AVPACKET_QUEUE_H

0 commit comments

Comments
 (0)