Recherche avancée

Médias (0)

Mot : - Tags -/interaction

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (23)

  • Qu’est ce qu’un éditorial

    21 juin 2013, par

    Ecrivez votre de point de vue dans un article. Celui-ci sera rangé dans une rubrique prévue à cet effet.
    Un éditorial est un article de type texte uniquement. Il a pour objectif de ranger les points de vue dans une rubrique dédiée. Un seul éditorial est placé à la une en page d’accueil. Pour consulter les précédents, consultez la rubrique dédiée.
    Vous pouvez personnaliser le formulaire de création d’un éditorial.
    Formulaire de création d’un éditorial Dans le cas d’un document de type éditorial, les (...)

  • Contribute to translation

    13 avril 2011

    You can help us to improve the language used in the software interface to make MediaSPIP more accessible and user-friendly. You can also translate the interface into any language that allows it to spread to new linguistic communities.
    To do this, we use the translation interface of SPIP where the all the language modules of MediaSPIP are available. Just subscribe to the mailing list and request further informantion on translation.
    MediaSPIP is currently available in French and English (...)

  • HTML5 audio and video support

    13 avril 2011, par

    MediaSPIP uses HTML5 video and audio tags to play multimedia files, taking advantage of the latest W3C innovations supported by modern browsers.
    The MediaSPIP player used has been created specifically for MediaSPIP and can be easily adapted to fit in with a specific theme.
    For older browsers the Flowplayer flash fallback is used.
    MediaSPIP allows for media playback on major mobile platforms with the above (...)

Sur d’autres sites (5444)

  • Recording video on Android using JavaCV (Updated)

    12 février 2014, par Fabio Bergmann

    I'm trying to record a video in Android using the JavaCV lib.
    I need to record the video in 640x360.

    I have installed everything as described in README.txt file and I followed the example as below :
    https://code.google.com/p/javacv/source/browse/samples/RecordActivity.java
    In this example, the video size is this :
    private int imageWidth = 320 ;
    private int imageHeight = 240 ;

    In my case, I need to record a video in 640x360 H.264.

    (UPDATE) I have reverted my code and kept exactly like in the example, just changing imageWidth and imageHeight to 640x360.
    Now I'm getting the video like this image :
    http://bergmann.net.br/img/screenshot_video_error.png

    Here is my code :

    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.ActivityInfo;
    import android.hardware.Camera;
    import android.hardware.Camera.PreviewCallback;
    import android.media.AudioFormat;
    import android.media.AudioRecord;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.os.PowerManager;
    import android.util.Log;
    import android.view.Display;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;

    import java.io.IOException;
    import java.nio.ShortBuffer;

    import com.googlecode.javacv.FFmpegFrameRecorder;

    import static com.googlecode.javacv.cpp.opencv_core.*;

    public class FFmpegRecordActivity extends Activity implements OnClickListener {

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

       private PowerManager.WakeLock mWakeLock;

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

       long startTime = 0;
       boolean recording = false;

       private volatile FFmpegFrameRecorder recorder;

       private boolean isPreviewOn = false;

       private int sampleAudioRateInHz = 44100;
       private int imageWidth = 640;
       private int imageHeight = 360;
       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 IplImage yuvIplimage = 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 = 1280;
       private final int live_height = 720;
       private int screenWidth, screenHeight;
       private Button btnRecorderControl;

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

           setContentView(R.layout.main);

           PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
           mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
                   CLASS_LABEL);
           mWakeLock.acquire();

           initLayout();
           initRecorder();
       }

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

           if (mWakeLock == null) {
               PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
               mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
                       CLASS_LABEL);
               mWakeLock.acquire();
           }
       }

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

           if (mWakeLock != null) {
               mWakeLock.release();
               mWakeLock = null;
           }
       }

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

           recording = false;

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

           if (mWakeLock != null) {
               mWakeLock.release();
               mWakeLock = 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.main, null);
           layoutParam = new RelativeLayout.LayoutParams(screenWidth, screenHeight);
           topLayout.addView(preViewLayout, layoutParam);

           /* 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);
           }
           prev_rw = 640;
           prev_rh = 360;
           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);
           Log.i(LOG_TAG, "cameara preview start: OK");
       }

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

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

           if (yuvIplimage == null) {
               yuvIplimage = IplImage.create(imageWidth, imageHeight,
                       IPL_DEPTH_8U, 2);
               Log.i(LOG_TAG, "create yuvIplimage");
           }

           Log.i(LOG_TAG, "ffmpeg_url: " + ffmpeg_link);
           recorder = new FFmpegFrameRecorder(ffmpeg_link, imageWidth,
                   imageHeight, 1);
           recorder.setFormat("flv");
           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);
       }

       public void startRecording() {

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

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

       public void stopRecording() {

           runAudioThread = false;

           if (recorder != null && recording) {
               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);
       }

       // ---------------------------------------------
       // 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;
               short[] 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);

               audioData = new short[bufferSize];

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

               /* ffmpeg_audio encoding loop */
               while (runAudioThread) {
                   // Log.v(LOG_TAG,"recording? " + recording);
                   bufferReadResult = audioRecord.read(audioData, 0,
                           audioData.length);
                   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) {
                           try {
                               recorder.record(ShortBuffer.wrap(audioData, 0,
                                       bufferReadResult));
                               // 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) {
               Log.v(LOG_TAG, "Setting imageWidth: " + imageWidth
                       + " imageHeight: " + imageHeight + " frameRate: "
                       + frameRate);
               Camera.Parameters camParams = mCamera.getParameters();
               camParams.setPreviewSize(imageWidth, imageHeight);

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

               camParams.setPreviewFrameRate(frameRate);
               mCamera.setParameters(camParams);
               startPreview();
           }

           @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) {
               /* get video data */
               if (yuvIplimage != null && recording) {
                   yuvIplimage.getByteBuffer().put(data);

                   Log.v(LOG_TAG, "Writing Frame");
                   try {
                       long t = 1000 * (System.currentTimeMillis() - startTime);
                       if (t > recorder.getTimestamp()) {
                           recorder.setTimestamp(t);
                       }
                       recorder.record(yuvIplimage);
                   } catch (FFmpegFrameRecorder.Exception e) {
                       Log.v(LOG_TAG, e.getMessage());
                       e.printStackTrace();
                   }
               }
           }
       }

       @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");
           }
       }
    }

    Anyone know how to fix it ?

    Thanks in advance !

  • Recording video on Android using JavaCV

    11 décembre 2013, par Fabio Bergmann

    I'm trying to record a video in Android using the JavaCV lib.
    I need to record the video in 640x360.

    I have installed everything as described in README.txt file and I followed the example as below :
    https://code.google.com/p/javacv/source/browse/samples/RecordActivity.java
    In this example, the video size is this :
    private int imageWidth = 320 ;
    private int imageHeight = 240 ;

    In my case, I need to record a video in 640x360 H.264.
    But, or I get an exception or the video records with no correct image.

    I think that the fix is arround a correct combination on :
    yuvIplimage = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_8S, 2) ;
    changing the depth and :
    recorder.record(yuvIplimage, AV_PIX_FMT_VDPAU_H264) ;
    the pixel format.

    Here is my code :

    import static com.googlecode.javacv.cpp.avutil.AV_PIX_FMT_NONE;
    import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;

    import java.io.IOException;
    import java.nio.ShortBuffer;

    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.ActivityInfo;
    import android.hardware.Camera;
    import android.hardware.Camera.PreviewCallback;
    import android.media.AudioFormat;
    import android.media.AudioRecord;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.os.PowerManager;
    import android.util.Log;
    import android.view.Display;
    import android.view.KeyEvent;
    import android.view.LayoutInflater;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;

    import com.autosonvideo.logic.CameraHelpers;
    import com.googlecode.javacv.FFmpegFrameRecorder;
    import com.googlecode.javacv.cpp.opencv_core.IplImage;

    public class FFmpegRecordActivity extends Activity implements OnClickListener {

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

       private PowerManager.WakeLock mWakeLock;

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

       long startTime = 0;
       boolean recording = false;

       private volatile FFmpegFrameRecorder recorder;

       private boolean isPreviewOn = false;

       private int sampleAudioRateInHz = 44100;
       private int imageWidth = 640;
       private int imageHeight = 360;
       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 IplImage yuvIplimage = 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 = 360;
       private int screenWidth, screenHeight;
       private Button btnRecorderControl;

       @Override
       public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);
           setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

           setContentView(R.layout.main);

           PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
           mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
                   CLASS_LABEL);
           mWakeLock.acquire();

           initLayout();
           initRecorder();
       }

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

           if (mWakeLock == null) {
               PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
               mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
                       CLASS_LABEL);
               mWakeLock.acquire();
           }
       }

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

           if (mWakeLock != null) {
               mWakeLock.release();
               mWakeLock = null;
           }
       }

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

           recording = false;

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

           if (mWakeLock != null) {
               mWakeLock.release();
               mWakeLock = 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.main, null);
           layoutParam = new RelativeLayout.LayoutParams(screenWidth, screenHeight);
           topLayout.addView(preViewLayout, layoutParam);

           /* 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);
           Log.i(LOG_TAG, "cameara preview start: OK");
       }

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

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

           if (yuvIplimage == null) {
               yuvIplimage = IplImage.create(imageWidth, imageHeight,
                       IPL_DEPTH_8U, 2);
               Log.i(LOG_TAG, "create yuvIplimage");
           }

           ffmpeg_link = CameraHelpers.getOutputMediaFile(
                   CameraHelpers.MEDIA_TYPE_VIDEO).toString();

           Log.i(LOG_TAG, "ffmpeg_url: " + ffmpeg_link);
           recorder = new FFmpegFrameRecorder(ffmpeg_link, imageWidth,
                   imageHeight, 1);
           recorder.setFormat("mp4");
           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);
       }

       public void startRecording() {

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

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

       public void stopRecording() {

           runAudioThread = false;

           if (recorder != null && recording) {
               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);
       }

       // ---------------------------------------------
       // 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;
               short[] 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);

               audioData = new short[bufferSize];

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

               /* ffmpeg_audio encoding loop */
               while (runAudioThread) {
                   // Log.v(LOG_TAG,"recording? " + recording);
                   bufferReadResult = audioRecord.read(audioData, 0,
                           audioData.length);
                   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) {
                           try {
                               recorder.record(ShortBuffer.wrap(audioData, 0,
                                       bufferReadResult));
                               // 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) {
               Log.v(LOG_TAG, "Setting imageWidth: " + imageWidth
                       + " imageHeight: " + imageHeight + " frameRate: "
                       + frameRate);
               Camera.Parameters camParams = mCamera.getParameters();
               camParams.setPreviewSize(imageWidth, imageHeight);

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

               camParams.setPreviewFrameRate(frameRate);
               mCamera.setParameters(camParams);
               startPreview();
           }

           @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) {
               /* get video data */
               if (yuvIplimage != null && recording) {
                   int cap = yuvIplimage.getByteBuffer().capacity();
                   int dataLen = data.length;
                   Log.d("FFmpeg", "cap: " + cap + " data: " + dataLen);
                   yuvIplimage.getByteBuffer().put(data);

                   Log.v(LOG_TAG, "Writing Frame");
                   try {
                       long t = 1000 * (System.currentTimeMillis() - startTime);
                       if (t > recorder.getTimestamp()) {
                           recorder.setTimestamp(t);
                       }
                       recorder.record(yuvIplimage, AV_PIX_FMT_NONE);
                   } catch (FFmpegFrameRecorder.Exception e) {
                       Log.v(LOG_TAG, e.getMessage());
                       e.printStackTrace();
                   }
               }
           }
       }

       @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");
           }
       }
    }

    Thanks in advance !

  • keepalive type and frequency in ffmpeg [on hold]

    19 novembre 2013, par Jack Simth

    My company has a bunch of IP cameras that we distribute - specifically Grandstream - and the manufacturer has changed their firmware. The normal keepalive that ffmpeg uses for the rtsp streams ( either ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL) ; or ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL) ; both in in libavformat/rtspdec.c) is no longer working, for two reasons :

    1) The new Grandstream firmware is now checking for a receiver report to determine whether or not the program reading the stream is live, not just anything.

    2) The new Grandstream firmware requires that the receiver report to keep the connection alive happen at least once every 25 seconds, and on the audio stream it is currently only happening about every 30 seconds or so (video is getting it every 7 seconds or so).

    So after about a minute with ffmpeg connected, the camera stops sending the audio stream, the audio stream on ffmpeg reads end-of-file, and then ffmpeg shuts everything down.

    As I can't change the firmware, I'm trying to dig through the ffmpeg code to make it send the appropriate receiver report for the keep alive... but I am getting nowhere. I've added a little snippet of code into the receiver reports so I know when they're running when I call ffmpeg on debug, but... well, it's not going well.

    Test command :
    ffmpeg -loglevel debug -i rtsp ://admin:admin@192.168.4.3:554/0 -acodec libmp3lame -ar 22050 -vcodec copy -y -f flv /dev/null &> test.txt

    Test output :

    `[root@localhost ffmpeg]# cat test.txt
    ffmpeg version 2.0 Copyright (c) 2000-2013 the FFmpeg developers
     built on Aug 21 2013 14:24:28 with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-3)
     configuration: --datadir=/usr/share/ffmpeg --bindir=/usr/local/bin --libdir=/usr/local/lib --incdir=/usr/local/include --shlibdir=/usr/lib --mandir=/usr/share/man --disable-avisynth --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables' --enable-avfilter --enable-libx264 --enable-gpl --enable-version3 --enable-postproc --enable-pthreads --enable-shared --enable-swscale --enable-vdpau --enable-x11grab --enable-librtmp --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-static --enable-libgsm --enable-libxvid --enable-libvpx --enable-libvorbis --enable-libvo-aacenc --enable-libmp3lame
     libavutil      52. 38.100 / 52. 38.100
     libavcodec     55. 18.102 / 55. 18.102
     libavformat    55. 12.100 / 55. 12.100
     libavdevice    55.  3.100 / 55.  3.100
     libavfilter     3. 79.101 /  3. 79.101
     libswscale      2.  3.100 /  2.  3.100
     libswresample   0. 17.102 /  0. 17.102
     libpostproc    52.  3.100 / 52.  3.100
    Splitting the commandline.
    Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument 'debug'.
    Reading option '-i' ... matched as input file with argument 'rtsp://admin:admin@192.168.4.3:554/0'.
    Reading option '-acodec' ... matched as option 'acodec' (force audio codec ('copy' to copy stream)) with argument 'libmp3lame'.
    Reading option '-ar' ... matched as option 'ar' (set audio sampling rate (in Hz)) with argument '22050'.
    Reading option '-vcodec' ... matched as option 'vcodec' (force video codec ('copy' to copy stream)) with argument 'copy'.
    Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.
    Reading option '-f' ... matched as option 'f' (force format) with argument 'flv'.
    Reading option '/dev/null' ... matched as output file.
    Finished splitting the commandline.
    Parsing a group of options: global .
    Applying option loglevel (set logging level) with argument debug.
    Applying option y (overwrite output files) with argument 1.
    Successfully parsed a group of options.
    Parsing a group of options: input file rtsp://admin:admin@192.168.4.3:554/0.
    Successfully parsed a group of options.
    Opening an input file: rtsp://admin:admin@192.168.4.3:554/0.
    [rtsp @ 0x9d9ccc0] SDP:
    v=0
    o=StreamingServer 3331435948 1116907222000 IN IP4 192.168.4.3
    s=h264.mp4
    c=IN IP4 0.0.0.0
    t=0 0
    a=control:*
    m=video 0 RTP/AVP 96
    a=control:trackID=0
    a=rtpmap:96 H264/90000
    a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z0LgHtoCgPRA,aM4wpIA=
    m=audio 0 RTP/AVP 0
    a=control:trackID=1
    a=rtpmap:0 PCMU/8000
    a=ptime:20
    m=application 0 RTP/AVP 107
    a=control:trackID=2
    a=rtpmap:107 vnd.onvif.metadata/90000


    [rtsp @ 0x9d9ccc0] video codec set to: h264
    [NULL @ 0x9d9f400] RTP Packetization Mode: 1
    [NULL @ 0x9d9f400] Extradata set to 0x9d9f900 (size: 22)!
    [rtsp @ 0x9d9ccc0] audio codec set to: pcm_mulaw
    [rtsp @ 0x9d9ccc0] audio samplerate set to: 8000
    [rtsp @ 0x9d9ccc0] audio channels set to: 1
    [rtsp @ 0x9d9ccc0] hello state=0
    [h264 @ 0x9d9f400] Current profile doesn't provide more RBSP data in PPS, skipping
       Last message repeated 1 times
    [rtsp @ 0x9d9ccc0] All info found
    Guessed Channel Layout for  Input Stream #0.1 : mono
    Input #0, rtsp, from 'rtsp://admin:admin@192.168.4.3:554/0':
     Metadata:
       title           : h264.mp4
     Duration: N/A, start: 0.000000, bitrate: 64 kb/s
       Stream #0:0, 28, 1/90000: Video: h264 (Constrained Baseline), yuv420p, 640x480, 1/180000, 10 tbr, 90k tbn, 180k tbc
       Stream #0:1, 156, 1/8000: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s
    Successfully opened the file.
    Parsing a group of options: output file /dev/null.
    Applying option acodec (force audio codec ('copy' to copy stream)) with argument libmp3lame.
    Applying option ar (set audio sampling rate (in Hz)) with argument 22050.
    Applying option vcodec (force video codec ('copy' to copy stream)) with argument copy.
    Applying option f (force format) with argument flv.
    Successfully parsed a group of options.
    Opening an output file: /dev/null.
    Successfully opened the file.
    detected 2 logical cores
    [graph 0 input from stream 0:1 @ 0x9f15380] Setting 'time_base' to value '1/8000'
    [graph 0 input from stream 0:1 @ 0x9f15380] Setting 'sample_rate' to value '8000'
    [graph 0 input from stream 0:1 @ 0x9f15380] Setting 'sample_fmt' to value 's16'
    [graph 0 input from stream 0:1 @ 0x9f15380] Setting 'channel_layout' to value '0x4'
    [graph 0 input from stream 0:1 @ 0x9f15380] tb:1/8000 samplefmt:s16 samplerate:8000 chlayout:0x4
    [audio format for output stream 0:1 @ 0x9efa7c0] Setting 'sample_fmts' to value 's32p|fltp|s16p'
    [audio format for output stream 0:1 @ 0x9efa7c0] Setting 'sample_rates' to value '22050'
    [audio format for output stream 0:1 @ 0x9efa7c0] Setting 'channel_layouts' to value '0x4|0x3'
    [audio format for output stream 0:1 @ 0x9efa7c0] auto-inserting filter 'auto-inserted resampler 0' between the filter 'Parsed_anull_0' and the filter 'audio format for output stream 0:1'
    [AVFilterGraph @ 0x9f15980] query_formats: 4 queried, 9 merged, 3 already done, 0 delayed
    [auto-inserted resampler 0 @ 0x9dfada0] ch:1 chl:mono fmt:s16 r:8000Hz -> ch:1 chl:mono fmt:s16p r:22050Hz
    Output #0, flv, to '/dev/null':
     Metadata:
       title           : h264.mp4
       encoder         : Lavf55.12.100
       Stream #0:0, 0, 1/1000: Video: h264 ([7][0][0][0] / 0x0007), yuv420p, 640x480, 1/90000, q=2-31, 1k tbn, 90k tbc
       Stream #0:1, 0, 1/1000: Audio: mp3 (libmp3lame) ([2][0][0][0] / 0x0002), 22050 Hz, mono, s16p
    Stream mapping:
     Stream #0:0 -> #0:0 (copy)
     Stream #0:1 -> #0:1 (pcm_mulaw -> libmp3lame)
    Press [q] to stop, [?] for help
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 135.4kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 134.4kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 135.0kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 135.5kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 136.9kbits/s
    Queue input is backward in time=     233kB time=00:00:13.69 bitrate= 139.4kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 136.3kbits/s
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 13926; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 13952; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 13979; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14005; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14031; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14057; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14083; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14109; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14135; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14161; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14188; changing to 14239. This may result in incorrect timestamps in the output file.
    [flv @ 0x9de1200] Non-monotonous DTS in output stream 0:1; previous: 14239, current: 14214; changing to 14239. This may result in incorrect timestamps in the output file.
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 141.5kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 142.0kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 142.5kbits/s
    Receiver Report delay: 469789, gettime: -1527669086, last_recep: 322446, timebase: -1534837492
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 141.5kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 141.7kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 141.1kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 140.6kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 140.7kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.9kbits/s
    Receiver Report delay: 132993, gettime: -1516538925, last_recep: 322446, timebase: -1518568234
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.6kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.6kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.7kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.4kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 140.0kbits/s
    Receiver Report delay: 897727, gettime: -1504870331, last_recep: 322446, timebase: -1518568552
    [NULL @ 0x9d9f400] Current profile doesn't provide more RBSP data in PPS, skipping
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.4kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.1kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.0kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 139.0kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 138.6kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 138.5kbits/s
    Current profile doesn't provide more RBSP data in PPS, skippingrate= 138.2kbits/s
    EOF on sink link output stream 0:1:default.time=00:00:58.40 bitrate= 139.6kbits/s
    No more output streams to write to, finishing.
    [libmp3lame @ 0x9dfa580] Trying to remove 344 more samples than there are in the queue
    frame=  589 fps= 11 q=-1.0 Lsize=    1003kB time=00:00:58.85 bitrate= 139.5kbits/s
    video:724kB audio:231kB subtitle:0 global headers:0kB muxing overhead 4.955356%
    2959 frames successfully decoded, 0 decoding errors
    [AVIOContext @ 0x9e021c0] Statistics: 3 seeks, 2860 writeouts
    [root@localhost ffmpeg]#