1414import java .io .IOException ;
1515import java .nio .ByteBuffer ;
1616import java .util .ArrayList ;
17+ import java .util .HashMap ;
1718import java .util .Iterator ;
19+ import java .util .Map ;
1820import java .util .concurrent .CopyOnWriteArrayList ;
1921
2022/**
@@ -35,7 +37,7 @@ public class MediaDataObserverPlugin implements MediaPreProcessing.ProgressCallb
3537 public ByteBuffer byteBufferBeforeAudioMix = ByteBuffer .allocateDirect (AUDIO_DEFAULT_BUFFER_SIZE );
3638 public ByteBuffer byteBufferAudioMix = ByteBuffer .allocateDirect (AUDIO_DEFAULT_BUFFER_SIZE );
3739
38- private final ArrayList < DecodeDataBuffer > decodeBufferList = new ArrayList <>();
40+ private final Map < Integer , ByteBuffer > decodeBufferList = new HashMap <>();
3941
4042 private static MediaDataObserverPlugin myAgent = null ;
4143
@@ -85,24 +87,18 @@ public void saveRenderVideoSnapshot(String filePath, int uid) {
8587
8688 public void addDecodeBuffer (int uid ) {
8789 ByteBuffer byteBuffer = ByteBuffer .allocateDirect (VIDEO_DEFAULT_BUFFER_SIZE );
88- decodeBufferList .add ( new DecodeDataBuffer ( uid , byteBuffer ) );
90+ decodeBufferList .put ( uid , byteBuffer );
8991 MediaPreProcessing .setVideoDecodeByteBuffer (uid , byteBuffer );
9092 }
9193
9294 public void removeDecodeBuffer (int uid ) {
93- Iterator <DecodeDataBuffer > it = decodeBufferList .iterator ();
94- while (it .hasNext ()) {
95- DecodeDataBuffer buffer = it .next ();
96- if (buffer .getUid () == uid ) {
97- it .remove ();
98- }
99- }
95+ decodeBufferList .remove (uid );
10096
10197 MediaPreProcessing .setVideoDecodeByteBuffer (uid , null );
10298 }
10399
104100 public void removeAllBuffer () {
105- decodeBufferList .removeAll ( decodeBufferList );
101+ decodeBufferList .clear ( );
106102 releaseBuffer ();
107103 }
108104
@@ -130,26 +126,23 @@ public void onCaptureVideoFrame(int videoFrameType, int width, int height, int b
130126 @ Override
131127 public void onRenderVideoFrame (int uid , int videoFrameType , int width , int height , int bufferLength , int yStride , int uStride , int vStride , int rotation , long renderTimeMs ) {
132128 for (MediaDataVideoObserver observer : videoObserverList ) {
133- Iterator <DecodeDataBuffer > it = decodeBufferList .iterator ();
134- while (it .hasNext ()) {
135- DecodeDataBuffer tmp = it .next ();
136- if (tmp .getUid () == uid ) {
137- byte [] buf = new byte [bufferLength ];
138- tmp .getByteBuffer ().limit (bufferLength );
139- tmp .getByteBuffer ().get (buf );
140- tmp .getByteBuffer ().flip ();
141-
142- observer .onRenderVideoFrame (uid , buf , videoFrameType , width , height , bufferLength , yStride , uStride , vStride , rotation , renderTimeMs );
143-
144- tmp .getByteBuffer ().put (buf );
145- tmp .getByteBuffer ().flip ();
146-
147- if (beRenderVideoShot ) {
148- if (uid == renderVideoShotUid ) {
149- beRenderVideoShot = false ;
150-
151- getVideoSnapshot (width , height , rotation , bufferLength , buf , renderFilePath , yStride , uStride , vStride );
152- }
129+ ByteBuffer tmp = decodeBufferList .get (uid );
130+ if (tmp != null ) {
131+ byte [] buf = new byte [bufferLength ];
132+ tmp .limit (bufferLength );
133+ tmp .get (buf );
134+ tmp .flip ();
135+
136+ observer .onRenderVideoFrame (uid , buf , videoFrameType , width , height , bufferLength , yStride , uStride , vStride , rotation , renderTimeMs );
137+
138+ tmp .put (buf );
139+ tmp .flip ();
140+
141+ if (beRenderVideoShot ) {
142+ if (uid == renderVideoShotUid ) {
143+ beRenderVideoShot = false ;
144+
145+ getVideoSnapshot (width , height , rotation , bufferLength , buf , renderFilePath , yStride , uStride , vStride );
153146 }
154147 }
155148 }
@@ -237,7 +230,8 @@ private void getVideoSnapshot(int width, int height, int rotation, int bufferLen
237230 byte [] bytes = baos .toByteArray ();
238231 try {
239232 baos .close ();
240- } catch (IOException e ) {
233+ }
234+ catch (IOException e ) {
241235 e .printStackTrace ();
242236 }
243237 Bitmap bitmap = BitmapFactory .decodeByteArray (bytes , 0 , bytes .length );
@@ -253,14 +247,16 @@ private void getVideoSnapshot(int width, int height, int rotation, int bufferLen
253247
254248 try {
255249 file .createNewFile ();
256- } catch (IOException e ) {
250+ }
251+ catch (IOException e ) {
257252 e .printStackTrace ();
258253 }
259254
260255 FileOutputStream fos = null ;
261256 try {
262257 fos = new FileOutputStream (file );
263- } catch (FileNotFoundException e ) {
258+ }
259+ catch (FileNotFoundException e ) {
264260 e .printStackTrace ();
265261 }
266262
@@ -271,7 +267,8 @@ private void getVideoSnapshot(int width, int height, int rotation, int bufferLen
271267
272268 try {
273269 fos .close ();
274- } catch (IOException e ) {
270+ }
271+ catch (IOException e ) {
275272 e .printStackTrace ();
276273 }
277274 }
0 commit comments