@@ -157,6 +157,7 @@ private void prepareDecoder(StringBuilder log) {
157157 if (mVideoFormat == null ) {
158158 return ;
159159 }
160+ boolean maybeSwitchWH = false ;
160161
161162 String mime = mVideoFormat .getString (MediaFormat .KEY_MIME );
162163 int width = mVideoFormat .getInteger (MediaFormat .KEY_WIDTH );
@@ -166,6 +167,9 @@ private void prepareDecoder(StringBuilder log) {
166167 rotation = mVideoFormat .getInteger (MediaFormat .KEY_ROTATION );
167168 } else {
168169 rotation = 0 ;
170+ if (width < height ) {
171+ maybeSwitchWH = true ;
172+ }
169173 }
170174 int maxCache ;
171175 if (mVideoFormat .containsKey (MediaFormat .KEY_MAX_INPUT_SIZE )) {
@@ -190,6 +194,8 @@ public void run() {
190194
191195 String codecName = MediaCodecUtils .findDecoderByFormat (mVideoFormat );
192196 if (TextUtils .isEmpty (codecName )) {
197+ log .append ("prepareDecoder: 完整format没有找到解码器!\n " );
198+ log .append ("prepareDecoder: 尝试降级!\n " );
193199 if (MediaFormat .MIMETYPE_VIDEO_DOLBY_VISION .equals (mime )) {
194200 //如果是杜比视界,那么尝试用HEVC的解码器去解
195201 mVideoFormat .setString (MediaFormat .KEY_MIME , MediaFormat .MIMETYPE_VIDEO_HEVC );
@@ -201,15 +207,28 @@ public void run() {
201207 codecName = MediaCodecUtils .findDecoderByFormat (mVideoFormat );
202208 } else if (MediaFormat .MIMETYPE_VIDEO_HEVC .equals (mime )) {
203209 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
210+ log .append ("prepareDecoder: 移除profile和level\n " );
204211 //HEVC的话,尝试移除Profile和Level
205212 mVideoFormat .removeKey (MediaFormat .KEY_PROFILE );
206213 mVideoFormat .removeKey (MediaFormat .KEY_LEVEL );
207214 }
208215 codecName = MediaCodecUtils .findDecoderByFormat (mVideoFormat );
216+ if (TextUtils .isEmpty (codecName )) {
217+ log .append ("prepareDecoder: 移除profile、level后format没有找到解码器!:" ).append (mVideoFormat ).append ("\n " );
218+ if (maybeSwitchWH ) {
219+ //Oppo有某些SB的设备,竖屏拍摄的视频,不写rotation到metadata中去,导致这里因为解码器的宽高限制,无法获取到解码器.
220+ log .append ("prepareDecoder: 尝试交换Width和Height\n " );
221+ MediaFormat simpleFormat = MediaFormat .createVideoFormat (mime , height , width );
222+ codecName = MediaCodecUtils .findDecoderByFormat (simpleFormat );
223+ if (TextUtils .isEmpty (codecName )) {
224+ log .append ("prepareDecoder: 交换width、height也没有找到解码器!" ).append (simpleFormat ).append ("\n " );
225+ }
226+ }
227+ }
209228 }
210229 }
211230 if (TextUtils .isEmpty (codecName )) {
212- log .append ("没有找到解码器 !" ).append ("\n " );
231+ log .append ("最终没有找到解码器 !" ).append ("\n " );
213232 setDebugLog (log .toString ());
214233 return ;
215234 }
@@ -281,9 +300,11 @@ public void run() {
281300 mMediaExtractor .release ();
282301 }
283302 log .append ("解码完成,释放资源!" ).append ("\n " );
284- setDebugLog (log .toString ());
285- } catch (IOException e ) {
303+ } catch (Exception e ) {
286304 e .printStackTrace ();
305+ log .append ("解码过程报错:" + e .getMessage ());
306+ } finally {
307+ setDebugLog (log .toString ());
287308 }
288309 }
289310
0 commit comments