Explorar o código

重新加入均衡器功能

chendeben hai 6 meses
pai
achega
eab4e62582

+ 2 - 4
ijkplayer/oh_modules/@types/libijkplayer_audio_napi.so/index.d.ts

@@ -45,8 +45,6 @@ export declare class newIjkPlayerAudio {
   _getAudioCodecInfo(xcomponentId: string): string;
   _getMediaMeta(xcomponentId: string): string;
   _native_setup(xcomponentId: string): void;
-  // 均衡器功能暂时禁用 - 华为官方库不支持
-  // 后续通过源码编译方式重新添加
-  // _setEqualizerEnabled(xcomponentId: string, enabled: boolean): void;
-  // _setEqualizerGains(xcomponentId: string, gains: number[]): void;
+  _setEqualizerEnabled(xcomponentId: string, enabled: boolean): void;
+  _setEqualizerGains(xcomponentId: string, gains: number[]): void;
 }

+ 1 - 0
ijkplayer/src/main/cpp/ijkplayer/ijkplayer_android.c

@@ -27,6 +27,7 @@
 #include "ff_ffplay.h"
 #include "ijkplayer_internal.h"
 #include <stddef.h>
+#include <stdbool.h>
 #include <string.h>
 #include <hilog/log.h>
 #include "pipeline/ffpipeline_android.h"

+ 9 - 0
ijkplayer/src/main/cpp/ijkplayer/ijkplayer_android.h

@@ -24,9 +24,14 @@
 #ifndef IJKPLAYER_ANDROID__IJKPLAYER_ANDROID_H
 #define IJKPLAYER_ANDROID__IJKPLAYER_ANDROID_H
 
+#include <stdbool.h>
 #include "ijkplayer_android_def.h"
 #include "ijkplayer.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 typedef struct ijkmp_android_media_format_context {
     const char *mime_type;
     int         profile;
@@ -46,4 +51,8 @@ int  ijkmp_android_get_audio_session_id(IjkMediaPlayer *mp);
 void ijkmp_android_set_equalizer_enabled(IjkMediaPlayer *mp, bool enabled);
 void ijkmp_android_set_equalizer_gains(IjkMediaPlayer *mp, const float *gains);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif

+ 13 - 5
ijkplayer/src/main/cpp/ijksdl/audio/ijksdl_aout_android_opensles.c

@@ -128,7 +128,7 @@ static void AudioCalcFramesWrittenNeedTime(OH_AudioRenderer *renderer, SDL_Aout_
 static int32_t AudioRendererOnWriteData(OH_AudioRenderer *renderer, void *userData, void *buffer, int32_t bufferLen)
 {
     SDL_Aout_Opaque *opaque = (SDL_Aout_Opaque*)userData;
-    if ((opaque == NULL) || opaque->abort_request || opaque->pause_on) {
+    if ((opaque == NULL) || opaque->abort_request || opaque->pause_on || opaque->need_flush) {
         return 0;
     }
 
@@ -138,12 +138,13 @@ static int32_t AudioRendererOnWriteData(OH_AudioRenderer *renderer, void *userDa
     }
     AudioCalcFramesWrittenNeedTime(renderer, opaque);
     audioCallback(opaque->spec.userdata, (uint8_t *)buffer, bufferLen);
-    
-    // 均衡器处理
-    if (opaque->equalizer.enabled && opaque->equalizer.initialized) {
+
+    // 均衡器处理 - 增加额外的安全检查
+    if (opaque->equalizer.enabled && opaque->equalizer.initialized &&
+        buffer != NULL && bufferLen > 0 && !opaque->need_flush) {
         equalizer_process_s16(&opaque->equalizer, (int16_t *)buffer, bufferLen);
     }
-    
+
     opaque->writtenLen += bufferLen;
     return 0;
 }
@@ -242,6 +243,7 @@ static int AoutOpenAudio(SDL_Aout *aout, const SDL_AudioSpec *desired, SDL_Audio
     opaque->buffer_capacity = OPENSLES_BUFFERS * opaque->bytes_per_buffer;
     opaque->pause_on = true;
     opaque->abort_request = false;
+    opaque->need_flush = false;
     opaque->writtenLen = 0;
 
     // 初始化均衡器
@@ -324,11 +326,17 @@ static void AudioRendererFlush(SDL_Aout *aout)
         LOGE("audio->AudioRendererFlush audioRendererNormal NULL");
         return;
     }
+    // 设置 flush 标志,阻止音频回调处理数据
+    opaque->need_flush = true;
     OH_AudioRenderer_Flush(opaque->audioRendererNormal); // 丢弃已经写入的音频数据
+    // 重置均衡器滤波器状态,防止 seek 后出现音频问题
+    equalizer_reset(&opaque->equalizer);
     // 清除音频时延所有参数
     opaque->writtenLen = 0;
     opaque->framesWrittenNeedTime = 0;
     opaque->lastCalcTime = av_gettime_relative();
+    // 清除 flush 标志
+    opaque->need_flush = false;
 }
 
 static void AudioRendererRelease(SDL_Aout *aout)

+ 7 - 2
ijkplayer/src/main/cpp/ijksdl/audio/ijksdl_equalizer.c

@@ -176,10 +176,15 @@ void equalizer_set_band_gain(Equalizer *eq, int band_index, float gain_db) {
 void equalizer_process_s16(Equalizer *eq, int16_t *buffer, int buffer_size) {
     if (eq == NULL || buffer == NULL || buffer_size <= 0) return;
     if (!eq->enabled || !eq->initialized) return;
-    
+
     int channels = eq->channels;
+    // 安全检查:确保 channels 有效
+    if (channels <= 0 || channels > 2) return;
+    if (eq->sample_rate <= 0) return;
+
     int sample_count = buffer_size / (sizeof(int16_t) * channels);
-    
+    if (sample_count <= 0) return;
+
     if (channels == 2) {
         // 立体声处理
         for (int i = 0; i < sample_count; i++) {

+ 14 - 8
ijkplayer/src/main/cpp/proxy/ijkplayer_napi_proxy.cpp

@@ -593,15 +593,21 @@ void IJKPlayerNapiProxy::IjkMediaPlayer_native_openlog() {
 }
 
 void IJKPlayerNapiProxy::IjkMediaPlayer_setEqualizerEnabled(bool enabled) {
-    // 均衡器功能暂时禁用 - 华为官方库不支持
-    // 后续通过源码编译方式重新添加
-    OH_LOG_Print(LOG_APP, LOG_WARN, 0xB000, "IjkAudioEq", "Equalizer disabled - using official library without EQ support");
-    (void)enabled; // 避免未使用参数警告
+    IjkMediaPlayer *mp = IJKPlayerNapiProxy::get_media_player(id_);
+    if (!mp) {
+        LOGE("napi_proxy-->IjkMediaPlayer_setEqualizerEnabled mp NULL");
+        return;
+    }
+    ijkmp_android_set_equalizer_enabled(mp, enabled);
+    ijkmp_dec_ref_p(&mp);
 }
 
 void IJKPlayerNapiProxy::IjkMediaPlayer_setEqualizerGains(const float *gains) {
-    // 均衡器功能暂时禁用 - 华为官方库不支持
-    // 后续通过源码编译方式重新添加
-    OH_LOG_Print(LOG_APP, LOG_WARN, 0xB000, "IjkAudioEq", "Equalizer disabled - using official library without EQ support");
-    (void)gains; // 避免未使用参数警告
+    IjkMediaPlayer *mp = IJKPlayerNapiProxy::get_media_player(id_);
+    if (!mp) {
+        LOGE("napi_proxy-->IjkMediaPlayer_setEqualizerGains mp NULL");
+        return;
+    }
+    ijkmp_android_set_equalizer_gains(mp, gains);
+    ijkmp_dec_ref_p(&mp);
 }

+ 2 - 4
ijkplayer/src/main/cpp/types/ijkplayer_audio_napi/index.d.ts

@@ -45,8 +45,6 @@ export declare class newIjkPlayerAudio {
   _getAudioCodecInfo(xcomponentId: string): string;
   _getMediaMeta(xcomponentId: string): string;
   _native_setup(xcomponentId: string): void;
-  // 均衡器功能暂时禁用 - 华为官方库不支持
-  // 后续通过源码编译方式重新添加
-  // _setEqualizerEnabled(xcomponentId: string, enabled: boolean): void;
-  // _setEqualizerGains(xcomponentId: string, gains: number[]): void;
+  _setEqualizerEnabled(xcomponentId: string, enabled: boolean): void;
+  _setEqualizerGains(xcomponentId: string, gains: number[]): void;
 }

+ 10 - 14
ijkplayer/src/main/ets/ijkplayer/IjkMediaPlayer.ets

@@ -252,23 +252,19 @@ export class IjkMediaPlayer {
   }
 
   setEqualizerEnabled(enabled: boolean): void {
-    // 均衡器功能暂时禁用 - 使用华为官方库后不支持
-    // 后续通过源码编译方式重新添加
-    // if (this.ijkplayer_audio_napi) {
-    //   this.ijkplayer_audio_napi._setEqualizerEnabled(this.id, enabled);
-    // } else if (!!this.ijkplayer_napi) {
-    //   this.ijkplayer_napi._setEqualizerEnabled?.(this.id, enabled);
-    // }
+    if (this.ijkplayer_audio_napi) {
+      this.ijkplayer_audio_napi._setEqualizerEnabled(this.id, enabled);
+    } else if (this.ijkplayer_napi) {
+      this.ijkplayer_napi._setEqualizerEnabled(this.id, enabled);
+    }
   }
 
   setEqualizerGains(gains: number[]): void {
-    // 均衡器功能暂时禁用 - 使用华为官方库后不支持
-    // 后续通过源码编译方式重新添加
-    // if (this.ijkplayer_audio_napi) {
-    //   this.ijkplayer_audio_napi._setEqualizerGains(this.id, gains);
-    // } else if (!!this.ijkplayer_napi) {
-    //   this.ijkplayer_napi._setEqualizerGains?.(this.id, gains);
-    // }
+    if (this.ijkplayer_audio_napi) {
+      this.ijkplayer_audio_napi._setEqualizerGains(this.id, gains);
+    } else if (this.ijkplayer_napi) {
+      this.ijkplayer_napi._setEqualizerGains(this.id, gains);
+    }
   }