Skip to content

Commit 63ca2fb

Browse files
committed
using avcodec_free_context instead of avcodec_close
1 parent f4559f1 commit 63ca2fb

5 files changed

Lines changed: 11 additions & 18 deletions

File tree

‎app/src/main/cpp/ffmpeg_pusher.cpp‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <jni.h>
66
#include <string>
7-
#include <android/log.h>
87
#include "ffmpeg_jni_define.h"
98

109
#define TAG "FFmpegPusher"
@@ -19,7 +18,6 @@ extern "C" {
1918

2019
PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
2120

22-
AVOutputFormat *output_format = NULL;
2321
AVFormatContext *in_format = NULL, *out_format = NULL;
2422
AVPacket packet;
2523
const char *file_path, *live_url;
@@ -77,8 +75,8 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
7775
// out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
7876
}
7977
}
80-
output_format = out_format->oformat;
81-
if (!(output_format->flags & AVFMT_NOFILE)) {
78+
79+
if (!(out_format->oformat->flags & AVFMT_NOFILE)) {
8280
ret = avio_open(&out_format->pb, live_url, AVIO_FLAG_WRITE);
8381
if (ret < 0) {
8482
LOGE(TAG, "could not open output url '%s'", live_url);
@@ -98,7 +96,7 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
9896
if (ret < 0) {
9997
break;
10098
}
101-
//计算帧间隔,参考时钟/采样率
99+
// calculate pts and dts
102100
if (packet.pts == AV_NOPTS_VALUE) {
103101
AVRational time_base = in_format->streams[video_index]->time_base;
104102
int64_t cal_duration = (int64_t) (AV_TIME_BASE /
@@ -123,9 +121,9 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
123121

124122
//pts to dts
125123
packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base,
126-
(AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
124+
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
127125
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base,
128-
(AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
126+
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
129127
packet.duration = av_rescale_q(packet.duration, in_stream->time_base,
130128
out_stream->time_base);
131129
packet.pos = -1;
@@ -146,13 +144,12 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
146144
//Write tail
147145
av_write_trailer(out_format);
148146

149-
end:
150-
avformat_close_input(&in_format);
147+
end:
151148
if (out_format && !(out_format->flags & AVFMT_NOFILE)) {
152149
avio_close(out_format->pb);
153150
}
154-
avformat_free_context(in_format);
155151
avformat_free_context(out_format);
152+
avformat_close_input(&in_format);
156153
env->ReleaseStringUTFChars(filePath, file_path);
157154
env->ReleaseStringUTFChars(liveUrl, live_url);
158155
if (ret < 0 && ret != AVERROR_EOF) {

‎app/src/main/cpp/media_player.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <android/native_window_jni.h>
66
#include <stdio.h>
77
#include <unistd.h>
8-
#include <android/log.h>
98
#include <pthread.h>
109
#include <jni.h>
1110

@@ -486,8 +485,8 @@ MEDIA_PLAYER_FUNC(void, release){
486485
av_free(player->yuv_frame);
487486
av_free(player->audio_buffer);
488487
av_free(player->audio_frame);
489-
avcodec_close(player->video_codec_context);
490-
avcodec_close(player->audio_codec_context);
488+
avcodec_free_context(&player->video_codec_context);
489+
avcodec_free_context(&player->audio_codec_context);
491490
avformat_close_input(&player->format_context);
492491
ANativeWindow_release(player->native_window);
493492
delete_queue(player);

‎app/src/main/cpp/openSL_audio_player.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <string.h>
77
#include <SLES/OpenSLES.h>
88
#include <SLES/OpenSLES_Android.h>
9-
#include <android/log.h>
109

1110
#ifdef __cplusplus
1211
extern "C" {
@@ -241,7 +240,7 @@ int releaseAudioPlayer() {
241240
av_packet_unref(&packet);
242241
av_free(outputBuffer);
243242
av_free(aFrame);
244-
avcodec_close(aCodecCtx);
243+
avcodec_free_context(&aCodecCtx);
245244
avformat_close_input(&aFormatCtx);
246245
return 0;
247246
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <stdio.h>
1010
#include <unistd.h>
1111
#include <libavutil/imgutils.h>
12-
#include <android/log.h>
1312
#include <libavfilter/buffersink.h>
1413
#include <libavfilter/buffersrc.h>
1514
#include <libavfilter/avfiltergraph.h>

‎app/src/main/cpp/video_player.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <android/native_window_jni.h>
66
#include <stdio.h>
77
#include <unistd.h>
8-
#include <android/log.h>
98

109
#ifdef __cplusplus
1110
extern "C" {
@@ -130,7 +129,7 @@ VIDEO_PLAYER_FUNC(jint, play, jstring filePath, jobject surface) {
130129
av_free(buffer);
131130
av_free(pFrameRGBA);
132131
av_free(pFrame);
133-
avcodec_close(pCodecCtx);
132+
avcodec_free_context(&pCodecCtx);
134133
avformat_close_input(&pFormatCtx);
135134
return 0;
136135
}

0 commit comments

Comments
 (0)