using avcodec_free_context instead of avcodec_close

dev
xufuji456 3 years ago
parent f4559f157d
commit 63ca2fbb2d
  1. 15
      app/src/main/cpp/ffmpeg_pusher.cpp
  2. 5
      app/src/main/cpp/media_player.cpp
  3. 3
      app/src/main/cpp/openSL_audio_player.cpp
  4. 1
      app/src/main/cpp/video_filter.c
  5. 3
      app/src/main/cpp/video_player.cpp

@ -4,7 +4,6 @@
#include <jni.h> #include <jni.h>
#include <string> #include <string>
#include <android/log.h>
#include "ffmpeg_jni_define.h" #include "ffmpeg_jni_define.h"
#define TAG "FFmpegPusher" #define TAG "FFmpegPusher"
@ -19,7 +18,6 @@ extern "C" {
PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) { PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
AVOutputFormat *output_format = NULL;
AVFormatContext *in_format = NULL, *out_format = NULL; AVFormatContext *in_format = NULL, *out_format = NULL;
AVPacket packet; AVPacket packet;
const char *file_path, *live_url; const char *file_path, *live_url;
@ -77,8 +75,8 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
// out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; // out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
} }
} }
output_format = out_format->oformat;
if (!(output_format->flags & AVFMT_NOFILE)) { if (!(out_format->oformat->flags & AVFMT_NOFILE)) {
ret = avio_open(&out_format->pb, live_url, AVIO_FLAG_WRITE); ret = avio_open(&out_format->pb, live_url, AVIO_FLAG_WRITE);
if (ret < 0) { if (ret < 0) {
LOGE(TAG, "could not open output url '%s'", live_url); LOGE(TAG, "could not open output url '%s'", live_url);
@ -98,7 +96,7 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
if (ret < 0) { if (ret < 0) {
break; break;
} }
//计算帧间隔,参考时钟/采样率 // calculate pts and dts
if (packet.pts == AV_NOPTS_VALUE) { if (packet.pts == AV_NOPTS_VALUE) {
AVRational time_base = in_format->streams[video_index]->time_base; AVRational time_base = in_format->streams[video_index]->time_base;
int64_t cal_duration = (int64_t) (AV_TIME_BASE / int64_t cal_duration = (int64_t) (AV_TIME_BASE /
@ -123,9 +121,9 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
//pts to dts //pts to dts
packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base,
(AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base,
(AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
packet.duration = av_rescale_q(packet.duration, in_stream->time_base, packet.duration = av_rescale_q(packet.duration, in_stream->time_base,
out_stream->time_base); out_stream->time_base);
packet.pos = -1; packet.pos = -1;
@ -147,12 +145,11 @@ PUSHER_FUNC(jint, pushStream, jstring filePath, jstring liveUrl) {
av_write_trailer(out_format); av_write_trailer(out_format);
end: end:
avformat_close_input(&in_format);
if (out_format && !(out_format->flags & AVFMT_NOFILE)) { if (out_format && !(out_format->flags & AVFMT_NOFILE)) {
avio_close(out_format->pb); avio_close(out_format->pb);
} }
avformat_free_context(in_format);
avformat_free_context(out_format); avformat_free_context(out_format);
avformat_close_input(&in_format);
env->ReleaseStringUTFChars(filePath, file_path); env->ReleaseStringUTFChars(filePath, file_path);
env->ReleaseStringUTFChars(liveUrl, live_url); env->ReleaseStringUTFChars(liveUrl, live_url);
if (ret < 0 && ret != AVERROR_EOF) { if (ret < 0 && ret != AVERROR_EOF) {

@ -5,7 +5,6 @@
#include <android/native_window_jni.h> #include <android/native_window_jni.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <android/log.h>
#include <pthread.h> #include <pthread.h>
#include <jni.h> #include <jni.h>
@ -486,8 +485,8 @@ MEDIA_PLAYER_FUNC(void, release){
av_free(player->yuv_frame); av_free(player->yuv_frame);
av_free(player->audio_buffer); av_free(player->audio_buffer);
av_free(player->audio_frame); av_free(player->audio_frame);
avcodec_close(player->video_codec_context); avcodec_free_context(&player->video_codec_context);
avcodec_close(player->audio_codec_context); avcodec_free_context(&player->audio_codec_context);
avformat_close_input(&player->format_context); avformat_close_input(&player->format_context);
ANativeWindow_release(player->native_window); ANativeWindow_release(player->native_window);
delete_queue(player); delete_queue(player);

@ -6,7 +6,6 @@
#include <string.h> #include <string.h>
#include <SLES/OpenSLES.h> #include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h> #include <SLES/OpenSLES_Android.h>
#include <android/log.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -241,7 +240,7 @@ int releaseAudioPlayer() {
av_packet_unref(&packet); av_packet_unref(&packet);
av_free(outputBuffer); av_free(outputBuffer);
av_free(aFrame); av_free(aFrame);
avcodec_close(aCodecCtx); avcodec_free_context(&aCodecCtx);
avformat_close_input(&aFormatCtx); avformat_close_input(&aFormatCtx);
return 0; return 0;
} }

@ -9,7 +9,6 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <libavutil/imgutils.h> #include <libavutil/imgutils.h>
#include <android/log.h>
#include <libavfilter/buffersink.h> #include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h> #include <libavfilter/buffersrc.h>
#include <libavfilter/avfiltergraph.h> #include <libavfilter/avfiltergraph.h>

@ -5,7 +5,6 @@
#include <android/native_window_jni.h> #include <android/native_window_jni.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <android/log.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -130,7 +129,7 @@ VIDEO_PLAYER_FUNC(jint, play, jstring filePath, jobject surface) {
av_free(buffer); av_free(buffer);
av_free(pFrameRGBA); av_free(pFrameRGBA);
av_free(pFrame); av_free(pFrame);
avcodec_close(pCodecCtx); avcodec_free_context(&pCodecCtx);
avformat_close_input(&pFormatCtx); avformat_close_input(&pFormatCtx);
return 0; return 0;
} }

Loading…
Cancel
Save