Recherche avancée

Médias (1)

Mot : - Tags -/bug

Autres articles (59)

  • Submit bugs and patches

    13 avril 2011

    Unfortunately a software is never perfect.
    If you think you have found a bug, report it using our ticket system. Please to help us to fix it by providing the following information : the browser you are using, including the exact version as precise an explanation as possible of the problem if possible, the steps taken resulting in the problem a link to the site / page in question
    If you think you have solved the bug, fill in a ticket and attach to it a corrective patch.
    You may also (...)

  • Les autorisations surchargées par les plugins

    27 avril 2010, par

    Mediaspip core
    autoriser_auteur_modifier() afin que les visiteurs soient capables de modifier leurs informations sur la page d’auteurs

  • Gestion des droits de création et d’édition des objets

    8 février 2011, par

    Par défaut, beaucoup de fonctionnalités sont limitées aux administrateurs mais restent configurables indépendamment pour modifier leur statut minimal d’utilisation notamment : la rédaction de contenus sur le site modifiables dans la gestion des templates de formulaires ; l’ajout de notes aux articles ; l’ajout de légendes et d’annotations sur les images ;

Sur d’autres sites (11254)

  • lavf/http: fix incorrect warning in range requests

    19 octobre 2015, par Rodger Combs
    lavf/http: fix incorrect warning in range requests
    
    • [DH] libavformat/http.c
  • Tap to record like in vine using javacv

    8 décembre 2015, par human123

    I am trying to implement a tap to record feature like in vine. A sample for handling recording (not touch to record) provided in javacv is https://github.com/bytedeco/javacv/blob/master/samples/RecordActivity.java. I am trying to modify it so that in onPreviewFrame method frames are added to buffer only when user has his finger placed on screen. These frames are then tried to be combined into final video in stopRecording method.

    The issue is that if I set the timestamp as given in below code snippet (in stopRecording method)

    if (t > recorder.getTimestamp())
    {
       recorder.setTimestamp(t);
    }

    the behavior is as below

    Case 1

    If I tap on screen to record for 2 seconds and take the finger away from screen for 3 seconds and then again place finger back on screen to record for another 4 seconds the resulting video is like,

    For 1st 2 seconds video has recorded content. For next 3 seconds (time when finger is put away from screen). video just shows the last frame recorded when finger was placed on screen last. Then the video has recorded video content for next 4 seconds. So there seems to be an issue in handling video recording when finger is removed from screen.

    Case 2

    Next I removed the code setting time stamp to recorder(the code snippet given above) in stopRecording method.

    Now the resulting video (for the same steps tried in case 1) does not contain the middle 3 seconds(which is what is required) when finger was taken away from screen. But video is playing at a faster rate. So it seems that we need to set time stamp so that video plays at normal rate.

    Full code of my activity is given below. (Please note that video recording is mainly handled from onPreviewFrame and stopRecording methods)

    public class TouchToRecordActivity extends Activity implements OnClickListener, View.OnTouchListener {

    private final static String CLASS_LABEL = "TouchToRecordActivity";
    private final static String LOG_TAG = CLASS_LABEL;

    private String ffmpeg_link = "/mnt/sdcard/stream.mp4";

    long startTime = 0;
    boolean recording = false;
    boolean rec = false;

    private FFmpegFrameRecorder recorder;

    private boolean isPreviewOn = false;

    private int sampleAudioRateInHz = 44100;
    private int imageWidth = 640;
    private int imageHeight = 480;
    private int destWidth = 480;
    private int frameRate = 30;

    /* audio data getting thread */
    private AudioRecord audioRecord;
    private AudioRecordRunnable audioRecordRunnable;
    private Thread audioThread;
    volatile boolean runAudioThread = true;

    /* video data getting thread */
    private Camera cameraDevice;
    private CameraView cameraView;

    private Frame yuvImage = null;

    /* layout setting */
    private final int bg_screen_bx = 232;
    private final int bg_screen_by = 128;
    private final int bg_screen_width = 700;
    private final int bg_screen_height = 500;
    private final int bg_width = 1123;
    private final int bg_height = 715;
    private final int live_width = 640;
    private final int live_height = 480;
    private int screenWidth, screenHeight;
    private Button btnRecorderControl;

    /* The number of seconds in the continuous record loop (or 0 to disable loop). */
    final int RECORD_LENGTH = 20;
    Frame[] images;
    long[] timestamps;
    ShortBuffer[] samples;
    int imagesIndex, samplesIndex;

    long firstTime = 0;
    long startPauseTime = 0;
    long totalPauseTime = 0;
    long pausedTime = 0;
    long stopPauseTime = 0;
    long totalTime = 0;

    long totalRecordedTS = 0;

    private TextView txtTimer;
    private Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

       setContentView(R.layout.touch_main);

       initLayout();
    }

    @Override
    protected void onDestroy() {
       super.onDestroy();

       recording = false;

       if (cameraView != null) {
           cameraView.stopPreview();
       }

       if (cameraDevice != null) {
           cameraDevice.stopPreview();
           cameraDevice.release();
           cameraDevice = null;
       }
    }


    private void initLayout() {

       /* get size of screen */
       Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
       screenWidth = display.getWidth();
       screenHeight = display.getHeight();
       RelativeLayout.LayoutParams layoutParam = null;
       LayoutInflater myInflate = null;
       myInflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       RelativeLayout topLayout = new RelativeLayout(this);
       setContentView(topLayout);
       LinearLayout preViewLayout = (LinearLayout) myInflate.inflate(R.layout.touch_main, null);
       layoutParam = new RelativeLayout.LayoutParams(screenWidth, screenHeight);
       topLayout.addView(preViewLayout, layoutParam);

       txtTimer = (TextView) preViewLayout.findViewById(R.id.txtTimer);

       /* add control button: start and stop */
       btnRecorderControl = (Button) findViewById(R.id.recorder_control);
       btnRecorderControl.setText("Start");
       btnRecorderControl.setOnClickListener(this);

       /* add camera view */
       int display_width_d = (int) (1.0 * bg_screen_width * screenWidth / bg_width);
       int display_height_d = (int) (1.0 * bg_screen_height * screenHeight / bg_height);
       int prev_rw, prev_rh;
       if (1.0 * display_width_d / display_height_d > 1.0 * live_width / live_height) {
           prev_rh = display_height_d;
           prev_rw = (int) (1.0 * display_height_d * live_width / live_height);
       } else {
           prev_rw = display_width_d;
           prev_rh = (int) (1.0 * display_width_d * live_height / live_width);
       }
       layoutParam = new RelativeLayout.LayoutParams(prev_rw, prev_rh);
       layoutParam.topMargin = (int) (1.0 * bg_screen_by * screenHeight / bg_height);
       layoutParam.leftMargin = (int) (1.0 * bg_screen_bx * screenWidth / bg_width);

       cameraDevice = Camera.open();
       Log.i(LOG_TAG, "cameara open");
       cameraView = new CameraView(this, cameraDevice);
       topLayout.addView(cameraView, layoutParam);
       topLayout.setOnTouchListener(this);
       Log.i(LOG_TAG, "cameara preview start: OK");
    }

    //---------------------------------------
    // initialize ffmpeg_recorder
    //---------------------------------------
    private void initRecorder() {

       Log.w(LOG_TAG, "init recorder");

       if (RECORD_LENGTH > 0) {
           imagesIndex = 0;
           images = new Frame[RECORD_LENGTH * frameRate];
           timestamps = new long[images.length];
           for (int i = 0; i < images.length; i++) {
               images[i] = new Frame(destWidth, imageHeight, Frame.DEPTH_UBYTE, 2);
               timestamps[i] = -1;
           }
       } else if (yuvImage == null) {
           yuvImage = new Frame(destWidth, imageHeight, Frame.DEPTH_UBYTE, 2);
           Log.i(LOG_TAG, "create yuvImage");
       }
       Log.i(LOG_TAG, "ffmpeg_url: " + ffmpeg_link);
       recorder = new FFmpegFrameRecorder(ffmpeg_link, destWidth, imageHeight, 1);
       recorder.setFormat("mp4");
       recorder.setVideoCodecName("libx264");
       recorder.setSampleRate(sampleAudioRateInHz);
       // Set in the surface changed method
       recorder.setFrameRate(frameRate);

       Log.i(LOG_TAG, "recorder initialize success");

       audioRecordRunnable = new AudioRecordRunnable();
       audioThread = new Thread(audioRecordRunnable);
       runAudioThread = true;
    }

    public void startRecording() {

       initRecorder();

       mHandler.removeCallbacks(mUpdateTimeTask);
       mHandler.postDelayed(mUpdateTimeTask, 100);

       try {
           recorder.start();
           startTime = System.currentTimeMillis();
           recording = true;
           audioThread.start();

       } catch (FFmpegFrameRecorder.Exception e) {
           e.printStackTrace();
       }
    }

    public void stopRecording() {

       runAudioThread = false;
       try {
           audioThread.join();
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
       audioRecordRunnable = null;
       audioThread = null;

       if (recorder != null && recording) {
           if (RECORD_LENGTH > 0) {
               Log.v(LOG_TAG, "Writing frames");
               try {
                   int firstIndex = imagesIndex % samples.length;
                   int lastIndex = (imagesIndex - 1) % images.length;
                   if (imagesIndex <= images.length) {
                       firstIndex = 0;
                       lastIndex = imagesIndex - 1;
                   }
                   if ((startTime = timestamps[lastIndex] - RECORD_LENGTH * 1000000L) < 0) {
                       startTime = 0;
                   }
                   if (lastIndex < firstIndex) {
                       lastIndex += images.length;
                   }
                   int videoCounter = 0;
                   for (int i = firstIndex; i <= lastIndex; i++) {
                       if (timestamps[i] == -1) {
                           Log.v(LOG_TAG, "frame not recorded");
                       }
                       if (timestamps[i] != -1) {
                           long t = timestamps[i % timestamps.length] - startTime;
                           if (t >= 0) {

                               videoCounter++;

                               /*if (((i % images.length) != 0) && images[i % images.length] != images[(i % images.length) - 1]) {
                                   if (t > recorder.getTimestamp()) {
                                       recorder.setTimestamp(t);
                                   }*/
                                   Log.v(LOG_TAG, "imageIndex=" + (i % images.length));
                                   recorder.record(images[i % images.length]);
                           /*    }*/
                               Log.v(LOG_TAG, "videoCounter=" + videoCounter);
                           }
                       }
                   }

                   firstIndex = samplesIndex % samples.length;
                   lastIndex = (samplesIndex - 1) % samples.length;
                   if (samplesIndex <= samples.length) {
                       firstIndex = 0;
                       lastIndex = samplesIndex - 1;
                   }
                   if (lastIndex < firstIndex) {
                       lastIndex += samples.length;
                   }
                   for (int i = firstIndex; i <= lastIndex; i++) {
                       if (timestamps[i] != -1) {
                           recorder.recordSamples(samples[i % samples.length]);
                       }
                   }
               } catch (FFmpegFrameRecorder.Exception e) {
                   Log.v(LOG_TAG, e.getMessage());
                   e.printStackTrace();
               }
           }

           recording = false;
           Log.v(LOG_TAG, "Finishing recording, calling stop and release on recorder");
           try {
               recorder.stop();
               recorder.release();
           } catch (FFmpegFrameRecorder.Exception e) {
               e.printStackTrace();
           }
           recorder = null;

       }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

       if (keyCode == KeyEvent.KEYCODE_BACK) {
           if (recording) {
               stopRecording();
           }

           finish();

           return true;
       }

       return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
       switch (motionEvent.getAction()) {
           case MotionEvent.ACTION_DOWN:
               Log.v(LOG_TAG, "ACTION_DOWN" + recording);

               if (!recording) {
                   startRecording();
               } else {
                   stopPauseTime = System.currentTimeMillis();
                   totalPauseTime = stopPauseTime - startPauseTime - ((long) (1.0 / (double) frameRate) * 1000);
                   pausedTime += totalPauseTime;
               }
               rec = true;
               setTotalVideoTime();
               btnRecorderControl.setText(getResources().getString(R.string.stop));
               break;
           case MotionEvent.ACTION_MOVE:
               rec = true;
               setTotalVideoTime();
               break;
           case MotionEvent.ACTION_UP:
               Log.v(LOG_TAG, "ACTION_UP");
               rec = false;
               startPauseTime = System.currentTimeMillis();
               break;
       }
       return true;
    }

    private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           if (recording) {
               setTotalVideoTime();
           }
           mHandler.postDelayed(this, 500);
       }
    };

    private synchronized void setTotalVideoTime() {
       totalTime = System.currentTimeMillis() - firstTime - pausedTime - ((long) (1.0 / (double) frameRate) * 1000);
       if (totalTime > 0)
           txtTimer.setText(getRecordingTimeFromMillis(totalTime));
    }

    private String getRecordingTimeFromMillis(long millis) {
       String strRecordingTime = null;
       int seconds = (int) (millis / 1000);
       int minutes = seconds / 60;
       int hours = minutes / 60;

       if (hours >= 0 && hours < 10)
           strRecordingTime = "0" + hours + ":";
       else
           strRecordingTime = hours + ":";

       if (hours > 0)
           minutes = minutes % 60;

       if (minutes >= 0 && minutes < 10)
           strRecordingTime += "0" + minutes + ":";
       else
           strRecordingTime += minutes + ":";

       seconds = seconds % 60;

       if (seconds >= 0 && seconds < 10)
           strRecordingTime += "0" + seconds;
       else
           strRecordingTime += seconds;

       return strRecordingTime;

    }


    //---------------------------------------------
    // audio thread, gets and encodes audio data
    //---------------------------------------------
    class AudioRecordRunnable implements Runnable {

       @Override
       public void run() {
           android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

           // Audio
           int bufferSize;
           ShortBuffer audioData;
           int bufferReadResult;

           bufferSize = AudioRecord.getMinBufferSize(sampleAudioRateInHz,
                   AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
           audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleAudioRateInHz,
                   AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);

           if (RECORD_LENGTH > 0) {
               samplesIndex = 0;
               samples = new ShortBuffer[RECORD_LENGTH * sampleAudioRateInHz * 2 / bufferSize + 1];
               for (int i = 0; i < samples.length; i++) {
                   samples[i] = ShortBuffer.allocate(bufferSize);
               }
           } else {
               audioData = ShortBuffer.allocate(bufferSize);
           }

           Log.d(LOG_TAG, "audioRecord.startRecording()");
           audioRecord.startRecording();

           /* ffmpeg_audio encoding loop */
           while (runAudioThread) {
               if (RECORD_LENGTH > 0) {
                   audioData = samples[samplesIndex++ % samples.length];
                   audioData.position(0).limit(0);
               }
               //Log.v(LOG_TAG,"recording? " + recording);
               bufferReadResult = audioRecord.read(audioData.array(), 0, audioData.capacity());
               audioData.limit(bufferReadResult);
               if (bufferReadResult > 0) {
                   Log.v(LOG_TAG, "bufferReadResult: " + bufferReadResult);
                   // If "recording" isn't true when start this thread, it never get's set according to this if statement...!!!
                   // Why?  Good question...
                   if (recording && rec) {
                       Log.v(LOG_TAG, "Recording audio");
                       if (RECORD_LENGTH <= 0) try {
                           recorder.recordSamples(audioData);
                           //Log.v(LOG_TAG,"recording " + 1024*i + " to " + 1024*i+1024);
                       } catch (FFmpegFrameRecorder.Exception e) {
                           Log.v(LOG_TAG, e.getMessage());
                           e.printStackTrace();
                       }
                   }
               }
           }
           Log.v(LOG_TAG, "AudioThread Finished, release audioRecord");

           /* encoding finish, release recorder */
           if (audioRecord != null) {
               audioRecord.stop();
               audioRecord.release();
               audioRecord = null;
               Log.v(LOG_TAG, "audioRecord released");
           }
       }
    }

    //---------------------------------------------
    // camera thread, gets and encodes video data
    //---------------------------------------------
    class CameraView extends SurfaceView implements SurfaceHolder.Callback, PreviewCallback {

       private SurfaceHolder mHolder;
       private Camera mCamera;

       public CameraView(Context context, Camera camera) {
           super(context);
           Log.w("camera", "camera view");
           mCamera = camera;
           mHolder = getHolder();
           mHolder.addCallback(CameraView.this);
           mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
           mCamera.setPreviewCallback(CameraView.this);
       }

       @Override
       public void surfaceCreated(SurfaceHolder holder) {
           try {
               stopPreview();
               mCamera.setPreviewDisplay(holder);
           } catch (IOException exception) {
               mCamera.release();
               mCamera = null;
           }
       }

       public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
           stopPreview();

           Camera.Parameters camParams = mCamera.getParameters();
           List sizes = camParams.getSupportedPreviewSizes();
           // Sort the list in ascending order
           Collections.sort(sizes, new Comparator() {

               public int compare(final Camera.Size a, final Camera.Size b) {
                   return a.width * a.height - b.width * b.height;
               }
           });

           camParams.setPreviewSize(imageWidth, imageHeight);

           Log.v(LOG_TAG, "Setting imageWidth: " + imageWidth + " imageHeight: " + imageHeight + " frameRate: " + frameRate);

           camParams.setPreviewFrameRate(frameRate);
           Log.v(LOG_TAG, "Preview Framerate: " + camParams.getPreviewFrameRate());

           mCamera.setParameters(camParams);

           List videoSizes = mCamera.getParameters().getSupportedVideoSizes();

           // Set the holder (which might have changed) again
           try {
               mCamera.setPreviewDisplay(holder);
               mCamera.setPreviewCallback(CameraView.this);
               startPreview();
           } catch (Exception e) {
               Log.e(LOG_TAG, "Could not set preview display in surfaceChanged");
           }
       }

       @Override
       public void surfaceDestroyed(SurfaceHolder holder) {
           try {
               mHolder.addCallback(null);
               mCamera.setPreviewCallback(null);
           } catch (RuntimeException e) {
               // The camera has probably just been released, ignore.
           }
       }

       public void startPreview() {
           if (!isPreviewOn && mCamera != null) {
               isPreviewOn = true;
               mCamera.startPreview();
           }
       }

       public void stopPreview() {
           if (isPreviewOn && mCamera != null) {
               isPreviewOn = false;
               mCamera.stopPreview();
           }
       }

       @Override
       public void onPreviewFrame(byte[] data, Camera camera) {
           if (audioRecord == null || audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
               startTime = System.currentTimeMillis();
               return;
           }
           if (RECORD_LENGTH > 0) {
               int i = imagesIndex++ % images.length;
               Log.v(LOG_TAG, "recording:" + recording + "rec:" + rec);
               if (recording && rec) {
                   yuvImage = images[i];
                   timestamps[i] = 1000 * (System.currentTimeMillis() - startTime);
                   totalRecordedTS++;
               } else {
                   Log.v(LOG_TAG, "recording is paused");
                   yuvImage = null;
                   timestamps[i] = -1;
               }
           }

           /* get video data */
           if (yuvImage != null && recording && rec) {
               if (data.length != imageWidth * imageHeight) {
                   Camera.Size sz = camera.getParameters().getPreviewSize();
                   imageWidth = sz.width;
                   imageHeight = sz.height;
                   destWidth = imageHeight;
                   Log.v(LOG_TAG, "data length:" + data.length);
               }

               ByteBuffer bb = (ByteBuffer) yuvImage.image[0].position(0); // resets the buffer
               int start = 2 * ((imageWidth - destWidth) / 4); // this must be even
               for (int row = 0; row < imageHeight * 3 / 2; row++) {
                   bb.put(data, start, destWidth);
                   start += imageWidth;
               }

           }
       }
    }

    @Override
    public void onClick(View v) {
       if (!recording) {
           startRecording();
           Log.w(LOG_TAG, "Start Button Pushed");
           btnRecorderControl.setText("Stop");
       } else {
           // This will trigger the audio recording loop to stop and then set isRecorderStart = false;
           stopRecording();
           Log.w(LOG_TAG, "Stop Button Pushed");
           btnRecorderControl.setText("Start");
       }
    }}

    Changes made as per Alex Cohn’s suggestions

    Suggestion 1 - Estimate average frame rate

       public void stopRecording() {

      ..............................

                               if (((i % images.length) != 0) && images[i % images.length] != images[(i % images.length) - 1]) {
                                   if (t > recorder.getTimestamp()) {
                                       t += 1000000 / frameRate;
                                       recorder.setTimestamp(t);
                                   }

                                   recorder.record(images[i % images.length]);
                               }
                ..........................................


    }

    Change made was adding t += 1000000 / frameRate ; But this caused the video to freeze (as in case 1 described above) in portions when finger was placed away from screen.

    Suggestion 2 - Modification in onPreviewFrame()

    long[] timestampsForRecorder;
    private void initRecorder() {

       Log.w(LOG_TAG, "init recorder");

       if (RECORD_LENGTH > 0) {
          .......................................................
           timestampsForRecorder = new long[images.length];
           for (int i = 0; i < images.length; i++) {
               images[i] = new Frame(destWidth, imageHeight, Frame.DEPTH_UBYTE, 2);
               timestamps[i] = -1;
               timestampsForRecorder[i] = -1;
           }
       } else if (yuvImage == null) {
           yuvImage = new Frame(destWidth, imageHeight, Frame.DEPTH_UBYTE, 2);
           Log.i(LOG_TAG, "create yuvImage");
       }
       ...................................................
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
           if (audioRecord == null || audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
               startTime = SystemClock.elapsedRealtime();
               return;
           }
           if (RECORD_LENGTH > 0) {
               int i = imagesIndex++ % images.length;
               Log.v(LOG_TAG, "recording:" + recording + "rec:" + rec);
               if (recording && rec) {
                   yuvImage = images[i];
                   long thisFrameTime = SystemClock.elapsedRealtime();
                   timestamps[i] = thisFrameTime;
                   long lastFrameTime = timestamps[(int) (imagesIndex == 0 ? startTime : ((imagesIndex-1) % images.length))];
                   Log.v(LOG_TAG, "lastFrameTime:" + lastFrameTime+",stopPauseTime:" + stopPauseTime);
                   if (lastFrameTime > stopPauseTime) {
                       timestampsForRecorder[i] = 1000 * (thisFrameTime - Math.max(stopPauseTime, lastFrameTime));
                   }
               }
           }

          .....................................................
       }

    public void stopRecording() {

       .......................................................

       if (recorder != null && recording) {
           if (RECORD_LENGTH > 0) {
               Log.v(LOG_TAG, "Writing frames");
               try {
                   int firstIndex = imagesIndex % samples.length;
                   int lastIndex = (imagesIndex - 1) % images.length;
                   if (imagesIndex <= images.length) {
                       firstIndex = 0;
                       lastIndex = imagesIndex - 1;
                   }
                   if ((startTime = timestampsForRecorder[lastIndex] - RECORD_LENGTH * 1000000L) < 0) {
                       startTime = 0;
                   }
                   if (lastIndex < firstIndex) {
                       lastIndex += images.length;
                   }
                   for (int i = firstIndex; i <= lastIndex; i++) {

                       if (timestampsForRecorder[i] != -1) {
                           long t = timestampsForRecorder[i % timestampsForRecorder.length] - startTime;
                           if (t >= 0) {

                               if (((i % images.length) != 0) && images[i % images.length] != images[(i % images.length) - 1]) {
                                   if (t > recorder.getTimestamp()) {
                                       recorder.setTimestamp(t);
                                   }
                                   Log.v(LOG_TAG, "imageIndex=" + (i % images.length));
                                   recorder.record(images[i % images.length]);
                               }
                           }
                       }
                   }
                   .............................................
               } catch (FFmpegFrameRecorder.Exception e) {
                  .................................
               }
           }

           ...........................................

       }
    }

    The video recorded using this was having the issue in case 2 mentioned above. ie,It was playing at a faster rate

  • Record rtsp stream with ffmpeg in iOS

    27 janvier 2017, par Wei Wen Hsiao

    I’ve followed iFrameExtractor to successfully stream rtsp in my swift project. In this project, it also has recording function. It basically use avformat_write_header
    , av_interleaved_write_frame and av_write_trailer to save the rtsp source into mp4 file.

    When I used this project in my device, the rtsp streaming works fine, but recording function will always generate a blank mp4 file with no image and sound.

    Could anyone tell me what step that I miss ?

    I’m using iPhone5 with iOS 9.1 and XCode 7.1.1.
    The ffmpeg is 2.8.3 version and followed the compile instruction by CompilationGuide – FFmpeg

    Following is the sample code in this project

    The function that generate every frame :

    -(BOOL)stepFrame {
    // AVPacket packet;
    int frameFinished=0;
    static bool bFirstIFrame=false;
    static int64_t vPTS=0, vDTS=0, vAudioPTS=0, vAudioDTS=0;

    while(!frameFinished && av_read_frame(pFormatCtx, &packet)>=0) {
       // Is this a packet from the video stream?
       if(packet.stream_index==videoStream) {

           // 20130525 albert.liao modified start

           // Initialize a new format context for writing file
           if(veVideoRecordState!=eH264RecIdle)
           {
               switch(veVideoRecordState)
               {
                   case eH264RecInit:
                   {                        
                       if ( !pFormatCtx_Record )
                       {
                           int bFlag = 0;
                           //NSString *videoPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.mp4"];
                           NSString *videoPath = @"/Users/liaokuohsun/iFrameTest.mp4";

                           const char *file = [videoPath UTF8String];
                           pFormatCtx_Record = avformat_alloc_context();
                           bFlag = h264_file_create(file, pFormatCtx_Record, pCodecCtx, pAudioCodecCtx,/*fps*/0.0, packet.data, packet.size );

                           if(bFlag==true)
                           {
                               veVideoRecordState = eH264RecActive;
                               fprintf(stderr, "h264_file_create success\n");                                
                           }
                           else
                           {
                               veVideoRecordState = eH264RecIdle;
                               fprintf(stderr, "h264_file_create error\n");
                           }
                       }
                   }
                   //break;

                   case eH264RecActive:
                   {
                       if((bFirstIFrame==false) &&(packet.flags&AV_PKT_FLAG_KEY)==AV_PKT_FLAG_KEY)
                       {
                           bFirstIFrame=true;
                           vPTS = packet.pts ;
                           vDTS = packet.dts ;
    #if 0
                           NSRunLoop *pRunLoop = [NSRunLoop currentRunLoop];
                           [pRunLoop addTimer:RecordingTimer forMode:NSDefaultRunLoopMode];
    #else
                           [NSTimer scheduledTimerWithTimeInterval:5.0//2.0
                                                            target:self
                                                          selector:@selector(StopRecording:)
                                                          userInfo:nil
                                                           repeats:NO];
    #endif
                       }

                       // Record audio when 1st i-Frame is obtained
                       if(bFirstIFrame==true)
                       {
                           if ( pFormatCtx_Record )
                           {
    #if PTS_DTS_IS_CORRECT==1
                               packet.pts = packet.pts - vPTS;
                               packet.dts = packet.dts - vDTS;

    #endif
                                   h264_file_write_frame( pFormatCtx_Record, packet.stream_index, packet.data, packet.size, packet.dts, packet.pts);

                           }
                           else
                           {
                               NSLog(@"pFormatCtx_Record no exist");
                           }
                       }
                   }
                   break;

                   case eH264RecClose:
                   {
                       if ( pFormatCtx_Record )
                       {
                           h264_file_close(pFormatCtx_Record);
    #if 0
                           // 20130607 Test
                           dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
                           {
                               ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
                               NSString *filePathString = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test.mp4"];
                               NSURL *filePathURL = [NSURL fileURLWithPath:filePathString isDirectory:NO];
                               if(1)// ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:filePathURL])
                               {
                                   [library writeVideoAtPathToSavedPhotosAlbum:filePathURL completionBlock:^(NSURL *assetURL, NSError *error){
                                       if (error) {
                                           // TODO: error handling
                                           NSLog(@"writeVideoAtPathToSavedPhotosAlbum error");
                                       } else {
                                           // TODO: success handling
                                           NSLog(@"writeVideoAtPathToSavedPhotosAlbum success");
                                       }
                                   }];
                               }
                               [library release];
                           });
    #endif
                           vPTS = 0;
                           vDTS = 0;
                           vAudioPTS = 0;
                           vAudioDTS = 0;
                           pFormatCtx_Record = NULL;
                           NSLog(@"h264_file_close() is finished");
                       }
                       else
                       {
                           NSLog(@"fc no exist");
                       }
                       bFirstIFrame = false;
                       veVideoRecordState = eH264RecIdle;

                   }
                   break;

                   default:
                       if ( pFormatCtx_Record )
                       {
                           h264_file_close(pFormatCtx_Record);
                           pFormatCtx_Record = NULL;
                       }
                       NSLog(@"[ERROR] unexpected veVideoRecordState!!");
                       veVideoRecordState = eH264RecIdle;
                       break;
               }
           }

           // Decode video frame
           avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
       }
       else if(packet.stream_index==audioStream)
       {
           // 20131024 albert.liao modfied start
           static int vPktCount=0;
           BOOL bIsAACADTS = FALSE;
           int ret = 0;

           if(aPlayer.vAACType == eAAC_UNDEFINED)
           {
               tAACADTSHeaderInfo vxAACADTSHeaderInfo = {0};
               bIsAACADTS = [AudioUtilities parseAACADTSHeader:(uint8_t *)packet.data ToHeader:&vxAACADTSHeaderInfo];
           }

           @synchronized(aPlayer)
           {
               if(aPlayer==nil)
               {
                   aPlayer = [[AudioPlayer alloc]initAudio:nil withCodecCtx:(AVCodecContext *) pAudioCodecCtx];
                   NSLog(@"aPlayer initAudio");

                   if(bIsAACADTS)
                   {
                       aPlayer.vAACType = eAAC_ADTS;
                       //NSLog(@"is ADTS AAC");
                   }
               }
               else
               {
                   if(vPktCount<5) // The voice is listened once image is rendered
                   {
                       vPktCount++;
                   }
                   else
                   {
                       if([aPlayer getStatus]!=eAudioRunning)
                       {
                           dispatch_async(dispatch_get_main_queue(), ^(void) {
                               @synchronized(aPlayer)
                               {
                                   NSLog(@"aPlayer start play");
                                   [aPlayer Play];
                               }

                           });
                       }
                   }
               }
           };

           @synchronized(aPlayer)
           {
               int ret = 0;

               ret = [aPlayer putAVPacket:&packet];
               if(ret <= 0)
                   NSLog(@"Put Audio Packet Error!!");

           }

           // 20131024 albert.liao modfied end

           if(bFirstIFrame==true)
           {
               switch(veVideoRecordState)
               {
                   case eH264RecActive:
                   {
                       if ( pFormatCtx_Record )
                       {
                           h264_file_write_audio_frame(pFormatCtx_Record, pAudioCodecCtx, packet.stream_index, packet.data, packet.size, packet.dts, packet.pts);

                       }
                       else
                       {
                           NSLog(@"pFormatCtx_Record no exist");
                       }
                   }
               }
           }
       }
       else
       {
           //fprintf(stderr, "packet len=%d, Byte=%02X%02X%02X%02X%02X\n",\
                   packet.size, packet.data[0],packet.data[1],packet.data[2],packet.data[3], packet.data[4]);
       }
       // 20130525 albert.liao modified end
    }
    return frameFinished!=0;
    }

    avformat_write_header :

    int h264_file_create(const char *pFilePath, AVFormatContext *fc, AVCodecContext *pCodecCtx,AVCodecContext *pAudioCodecCtx, double fps, void *p, int len )
    {
    int vRet=0;
    AVOutputFormat *of=NULL;
    AVStream *pst=NULL;
    AVCodecContext *pcc=NULL, *pAudioOutputCodecContext=NULL;

    avcodec_register_all();
    av_register_all();
    av_log_set_level(AV_LOG_VERBOSE);

    if(!pFilePath)
    {
       fprintf(stderr, "FilePath no exist");
       return -1;
    }

    if(!fc)
    {
       fprintf(stderr, "AVFormatContext no exist");
       return -1;
    }
    fprintf(stderr, "file=%s\n",pFilePath);

    // Create container
    of = av_guess_format( 0, pFilePath, 0 );
    fc->oformat = of;
    strcpy( fc->filename, pFilePath );

    // Add video stream
    pst = avformat_new_stream( fc, 0 );
    vVideoStreamIdx = pst->index;
    fprintf(stderr,"Video Stream:%d",vVideoStreamIdx);

    pcc = pst->codec;
    avcodec_get_context_defaults3( pcc, AVMEDIA_TYPE_VIDEO );

    // Save the stream as origin setting without convert
    pcc->codec_type = pCodecCtx->codec_type;
    pcc->codec_id = pCodecCtx->codec_id;
    pcc->bit_rate = pCodecCtx->bit_rate;
    pcc->width = pCodecCtx->width;
    pcc->height = pCodecCtx->height;

    if(fps==0)
    {
       double fps=0.0;
       AVRational pTimeBase;
       pTimeBase.num = pCodecCtx->time_base.num;
       pTimeBase.den = pCodecCtx->time_base.den;
       fps = 1.0/ av_q2d(pCodecCtx->time_base)/ FFMAX(pCodecCtx->ticks_per_frame, 1);
       fprintf(stderr,"fps_method(tbc): 1/av_q2d()=%g",fps);
       pcc->time_base.num = 1;
       pcc->time_base.den = fps;
    }
    else
    {
       pcc->time_base.num = 1;
       pcc->time_base.den = fps;
    }
    // reference ffmpeg\libavformat\utils.c

    // For SPS and PPS in avcC container
    pcc->extradata = malloc(sizeof(uint8_t)*pCodecCtx->extradata_size);
    memcpy(pcc->extradata, pCodecCtx->extradata, pCodecCtx->extradata_size);
    pcc->extradata_size = pCodecCtx->extradata_size;

    // For Audio stream
    if(pAudioCodecCtx)
    {
       AVCodec *pAudioCodec=NULL;
       AVStream *pst2=NULL;
       pAudioCodec = avcodec_find_encoder(AV_CODEC_ID_AAC);

       // Add audio stream
       pst2 = avformat_new_stream( fc, pAudioCodec );
       vAudioStreamIdx = pst2->index;
       pAudioOutputCodecContext = pst2->codec;
       avcodec_get_context_defaults3( pAudioOutputCodecContext, pAudioCodec );
       fprintf(stderr,"Audio Stream:%d",vAudioStreamIdx);
       fprintf(stderr,"pAudioCodecCtx->bits_per_coded_sample=%d",pAudioCodecCtx->bits_per_coded_sample);

       pAudioOutputCodecContext->codec_type = AVMEDIA_TYPE_AUDIO;
       pAudioOutputCodecContext->codec_id = AV_CODEC_ID_AAC;

       // Copy the codec attributes
       pAudioOutputCodecContext->channels = pAudioCodecCtx->channels;
       pAudioOutputCodecContext->channel_layout = pAudioCodecCtx->channel_layout;
       pAudioOutputCodecContext->sample_rate = pAudioCodecCtx->sample_rate;
       pAudioOutputCodecContext->bit_rate = 12000;//pAudioCodecCtx->sample_rate * pAudioCodecCtx->bits_per_coded_sample;
       pAudioOutputCodecContext->bits_per_coded_sample = pAudioCodecCtx->bits_per_coded_sample;
       pAudioOutputCodecContext->profile = pAudioCodecCtx->profile;
       //FF_PROFILE_AAC_LOW;
       // pAudioCodecCtx->bit_rate;

       // AV_SAMPLE_FMT_U8P, AV_SAMPLE_FMT_S16P
       //pAudioOutputCodecContext->sample_fmt = AV_SAMPLE_FMT_FLTP;//pAudioCodecCtx->sample_fmt;
       pAudioOutputCodecContext->sample_fmt = pAudioCodecCtx->sample_fmt;
       //pAudioOutputCodecContext->sample_fmt = AV_SAMPLE_FMT_U8;

       pAudioOutputCodecContext->sample_aspect_ratio = pAudioCodecCtx->sample_aspect_ratio;

       pAudioOutputCodecContext->time_base.num = pAudioCodecCtx->time_base.num;
       pAudioOutputCodecContext->time_base.den = pAudioCodecCtx->time_base.den;
       pAudioOutputCodecContext->ticks_per_frame = pAudioCodecCtx->ticks_per_frame;
       pAudioOutputCodecContext->frame_size = 1024;

       fprintf(stderr,"profile:%d, sample_rate:%d, channles:%d", pAudioOutputCodecContext->profile, pAudioOutputCodecContext->sample_rate, pAudioOutputCodecContext->channels);
       AVDictionary *opts = NULL;
       av_dict_set(&opts, "strict", "experimental", 0);

       if (avcodec_open2(pAudioOutputCodecContext, pAudioCodec, &opts) < 0) {
           fprintf(stderr, "\ncould not open codec\n");
       }

       av_dict_free(&opts);

    #if 0
       // For Audio, this part is no need
       if(pAudioCodecCtx->extradata_size!=0)
       {
           NSLog(@"extradata_size !=0");
           pAudioOutputCodecContext->extradata = malloc(sizeof(uint8_t)*pAudioCodecCtx->extradata_size);
           memcpy(pAudioOutputCodecContext->extradata, pAudioCodecCtx->extradata, pAudioCodecCtx->extradata_size);
           pAudioOutputCodecContext->extradata_size = pAudioCodecCtx->extradata_size;
       }
       else
       {
           // For WMA test only
           pAudioOutputCodecContext->extradata_size = 0;
           NSLog(@"extradata_size ==0");
       }
    #endif
    }

    if(fc->oformat->flags & AVFMT_GLOBALHEADER)
    {
       pcc->flags |= CODEC_FLAG_GLOBAL_HEADER;
       pAudioOutputCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }

    if ( !( fc->oformat->flags & AVFMT_NOFILE ) )
    {
       vRet = avio_open( &fc->pb, fc->filename, AVIO_FLAG_WRITE );
       if(vRet!=0)
       {
           fprintf(stderr,"avio_open(%s) error", fc->filename);
       }
    }

    // dump format in console
    av_dump_format(fc, 0, pFilePath, 1);

    vRet = avformat_write_header( fc, NULL );
    if(vRet==0)
       return 1;
    else
       return 0;
    }

    av_interleaved_write_frame :

    void h264_file_write_frame(AVFormatContext *fc, int vStreamIdx, const void* p, int len, int64_t dts, int64_t pts )
    {
       AVStream *pst = NULL;
       AVPacket pkt;

    if ( 0 > vVideoStreamIdx )
       return;

    // may be audio or video
    pst = fc->streams[ vStreamIdx ];

    // Init packet
    av_init_packet( &pkt );

    if(vStreamIdx ==vVideoStreamIdx)
    {
       pkt.flags |= ( 0 >= getVopType( p, len ) ) ? AV_PKT_FLAG_KEY : 0;
       //pkt.flags |= AV_PKT_FLAG_KEY;
       pkt.stream_index = pst->index;
       pkt.data = (uint8_t*)p;
       pkt.size = len;


       pkt.dts = AV_NOPTS_VALUE;
       pkt.pts = AV_NOPTS_VALUE;

       // TODO: mark or unmark the log
       //fprintf(stderr, "dts=%lld, pts=%lld\n",dts,pts);
       // av_write_frame( fc, &pkt );
    }
    av_interleaved_write_frame( fc, &pkt );
    }

    av_write_trailer :

    void h264_file_close(AVFormatContext *fc)
    {
    if ( !fc )
       return;

    av_write_trailer( fc );


    if ( fc->oformat && !( fc->oformat->flags & AVFMT_NOFILE ) && fc->pb )
       avio_close( fc->pb );

    av_free( fc );
    }

    Thanks.