Android camera2 api galaxy s7

I am writing an application that records video from a phone and uploads it to a server. Works great on any device except the Galaxy S7. On the Galaxy S7 record, a video file with sound is created, and there is also no video or one video frame. This is true in the temporary file created on the phone, and not just on what you uploaded to the server. I am using Camera2 API and I tried using front and rear cameras.

I tried with my code and these two sample applications: https://developer.android.com/samples/Camera2Video/project.html https://github.com/googlesamples/android-Camera2Video/blob/master/Application/src /main/java/com/example/android/camera2video/Camera2VideoFragment.java

The created video file looks fine, here is the information about the codec: Stream 0 Type: Video Codec: H264 - MPEG-4 AVC (part 10) (avc1) English Resolution: 960x720 Display Resolution: 960x720 Frame Rate: 29.055091

Stream 1 Type: Audio Codec: MPEG AAC Audio (mp4a) English Channels: Stereo Sampling frequency: 16000 Hz

+5
source share
1 answer

After several days of work, I found the answer.

The Samsung Galaxy S7 (and S6, I think) has a bug that messed up the encoding. The fix is ​​to reuse using the following function.

Please note that you need this dependency in your gradle: compile 'com.googlecode.mp4parser: isoparser: 1.1.22'

public void fixSamsungBug() { DataSource channel = null; try { channel = new FileDataSourceImpl(app.dataMgr.videoFileURL); } catch (FileNotFoundException e) { e.printStackTrace(); } IsoFile isoFile = null; try { isoFile = new IsoFile(channel); } catch (IOException e) { e.printStackTrace(); } List<TrackBox> trackBoxes = isoFile.getMovieBox().getBoxes(TrackBox.class); boolean sampleError = false; for (TrackBox trackBox : trackBoxes) { TimeToSampleBox.Entry firstEntry = trackBox.getMediaBox().getMediaInformationBox().getSampleTableBox().getTimeToSampleBox().getEntries().get(0); // Detect if first sample is a problem and fix it in isoFile // This is a hack. The audio deltas are 1024 for my files, and video deltas about 3000 // 10000 seems sufficient since for 30 fps the normal delta is about 3000 if(firstEntry.getDelta() > 10000) { sampleError = true; firstEntry.setDelta(3000); } } if(sampleError) { Log.d("gpinterviewandroid", "Sample error! correcting..."); Movie movie = new Movie(); for (TrackBox trackBox : trackBoxes) { movie.addTrack(new Mp4TrackImpl(channel.toString() + "[" + trackBox.getTrackHeaderBox().getTrackId() + "]" , trackBox)); } movie.setMatrix(isoFile.getMovieBox().getMovieHeaderBox().getMatrix()); Container out = new DefaultMp4Builder().build(movie); //delete file first! File file = new File(app.dataMgr.videoFileURL); boolean deleted = file.delete(); FileChannel fc = null; try { //fc = new FileOutputStream(new File(app.dataMgr.videoFileURL)).getChannel(); fc = new RandomAccessFile(app.dataMgr.videoFileURL, "rw").getChannel(); } catch (FileNotFoundException e) { e.printStackTrace(); } try { out.writeContainer(fc); fc.close(); } catch (IOException e) { e.printStackTrace(); } Log.d("gpinterviewandroid", "Finished correcting raw video"); } } 
+2
source

Source: https://habr.com/ru/post/1270290/


All Articles