
Recherche avancée
Médias (1)
-
The Slip - Artworks
26 septembre 2011, par
Mis à jour : Septembre 2011
Langue : English
Type : Texte
Autres articles (20)
-
Multilang : améliorer l’interface pour les blocs multilingues
18 février 2011, parMultilang est un plugin supplémentaire qui n’est pas activé par défaut lors de l’initialisation de MediaSPIP.
Après son activation, une préconfiguration est mise en place automatiquement par MediaSPIP init permettant à la nouvelle fonctionnalité d’être automatiquement opérationnelle. Il n’est donc pas obligatoire de passer par une étape de configuration pour cela. -
List of compatible distributions
26 avril 2011, parThe table below is the list of Linux distributions compatible with the automated installation script of MediaSPIP. Distribution nameVersion nameVersion number Debian Squeeze 6.x.x Debian Weezy 7.x.x Debian Jessie 8.x.x Ubuntu The Precise Pangolin 12.04 LTS Ubuntu The Trusty Tahr 14.04
If you want to help us improve this list, you can provide us access to a machine whose distribution is not mentioned above or send the necessary fixes to add (...) -
Selection of projects using MediaSPIP
2 mai 2011, parThe examples below are representative elements of MediaSPIP specific uses for specific projects.
MediaSPIP farm @ Infini
The non profit organizationInfini develops hospitality activities, internet access point, training, realizing innovative projects in the field of information and communication technologies and Communication, and hosting of websites. It plays a unique and prominent role in the Brest (France) area, at the national level, among the half-dozen such association. Its members (...)
Sur d’autres sites (7206)
-
using libav instead of ffmpeg
21 janvier 2015, par n00bieI want to streaming video over http, i am using ogg(theora + vorbis), now i have sender and receiver, and i can run them using command line :
Sender :
ffmpeg -f video4linux2 -s 320x240 -i /dev/mycam -codec:v libtheora -qscale:v 5 -f ogg http://127.0.0.1:8080
Receiver :
sudo gst-launch-0.10 tcpserversrc port = 8080 ! oggdemux ! theoradec ! autovideosink
Now, sender sends both audio and video, but receiver plays only video.
It works perfect, but now i want not to use ffmpeg and use only libav* instead.
Here’s my class for streaming :
class VCORE_LIBRARY_EXPORT VVideoWriter : private boost::noncopyable
{
public:
VVideoWriter( );
~VVideoWriter( );
bool openFile( const std::string& name,
int fps, int videoBitrate, int width, int height,
int audioSampleRate, bool stereo, int audioBitrate );
void close( );
bool writeVideoFrame( const uint8_t* image, int64_t timestamp );
bool writeAudioFrame( const int16_t* data, int64_t timestamp );
int audioFrameSize( ) const;
private:
AVFrame *m_videoFrame;
AVFrame *m_audioFrame;
AVFormatContext *m_context;
AVStream *m_videoStream;
AVStream *m_audioStream;
int64_t m_startTime;
};Initialization :
bool VVideoWriter::openFile( const std::string& name,
int fps, int videoBitrate, int width, int height,
int audioSampleRate, bool stereo, int audioBitrate )
{
if( ! m_context )
{
// initalize the AV context
m_context = avformat_alloc_context( );
assert( m_context );
// get the output format
m_context->oformat = av_guess_format( "ogg", name.c_str( ), nullptr );
if( m_context->oformat )
{
strcpy( m_context->filename, name.c_str( ) );
auto codecID = AV_CODEC_ID_THEORA;
auto codec = avcodec_find_encoder( codecID );
if( codec )
{
m_videoStream = avformat_new_stream( m_context, codec );
assert( m_videoStream );
// initalize codec
auto codecContext = m_videoStream->codec;
bool globalHeader = m_context->oformat->flags & AVFMT_GLOBALHEADER;
if( globalHeader )
codecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
codecContext->codec_id = codecID;
codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
codecContext->width = width;
codecContext->height = height;
codecContext->time_base.den = fps;
codecContext->time_base.num = 1;
codecContext->bit_rate = videoBitrate;
codecContext->pix_fmt = PIX_FMT_YUV420P;
codecContext->flags |= CODEC_FLAG_QSCALE;
codecContext->global_quality = FF_QP2LAMBDA * 5;
int res = avcodec_open2( codecContext, codec, nullptr );
if( res >= 0 )
{
auto codecID = AV_CODEC_ID_VORBIS;
auto codec = avcodec_find_encoder( codecID );
if( codec )
{
m_audioStream = avformat_new_stream( m_context, codec );
assert( m_audioStream );
// initalize codec
auto codecContext = m_audioStream->codec;
bool globalHeader = m_context->oformat->flags & AVFMT_GLOBALHEADER;
if( globalHeader )
codecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
codecContext->codec_id = codecID;
codecContext->codec_type = AVMEDIA_TYPE_AUDIO;
codecContext->sample_fmt = AV_SAMPLE_FMT_FLTP;
codecContext->bit_rate = audioBitrate;
codecContext->sample_rate = audioSampleRate;
codecContext->channels = stereo ? 2 : 1;
codecContext->channel_layout = stereo ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
res = avcodec_open2( codecContext, codec, nullptr );
if( res >= 0 )
{
// try to open the file
if( avio_open( &m_context->pb, m_context->filename, AVIO_FLAG_WRITE ) >= 0 )
{
m_audioFrame->nb_samples = codecContext->frame_size;
m_audioFrame->format = codecContext->sample_fmt;
m_audioFrame->channel_layout = codecContext->channel_layout;
boost::posix_time::ptime time_t_epoch( boost::gregorian::date( 1970, 1, 1 ) );
m_context->start_time_realtime = ( boost::posix_time::microsec_clock::universal_time( ) - time_t_epoch ).total_microseconds( );
m_startTime = -1;
// write the header
if( avformat_write_header( m_context, nullptr ) >= 0 )
{
return true;
}
else std::cerr << "VVideoWriter: failed to write video header" << std::endl;
}
else std::cerr << "VVideoWriter: failed to open video file " << name << std::endl;
}
else std::cerr << "VVideoWriter: failed to initialize audio codec" << std::endl;
}
else std::cerr << "VVideoWriter: requested audio codec is not supported" << std::endl;
}
else std::cerr << "VVideoWriter: failed to initialize video codec" << std::endl;
}
else std::cerr << "VVideoWriter: requested video codec is not supported" << std::endl;
}
else std::cerr << "VVideoWriter: requested video format is not supported" << std::endl;
avformat_free_context( m_context );
m_context = nullptr;
m_videoStream = nullptr;
m_audioStream = nullptr;
}
return false;
}Writing video :
bool VVideoWriter::writeVideoFrame( const uint8_t* image, int64_t timestamp )
{
if( m_context ) {
auto codecContext = m_videoStream->codec;
avpicture_fill( reinterpret_cast( m_videoFrame ),
const_cast( image ),
codecContext->pix_fmt, codecContext->width, codecContext->height );
AVPacket pkt;
av_init_packet( & pkt );
pkt.data = nullptr;
pkt.size = 0;
int gotPacket = 0;
if( ! avcodec_encode_video2( codecContext, &pkt, m_videoFrame, & gotPacket ) ) {
if( gotPacket == 1 ) {
pkt.stream_index = m_videoStream->index;
int res;
{
pkt.pts = AV_NOPTS_VALUE;
pkt.dts = AV_NOPTS_VALUE;
pkt.stream_index = m_videoStream->index;
res = av_write_frame( m_context, &pkt );
}
av_free_packet( & pkt );
return res >= 0;
}
assert( ! pkt.size );
return true;
}
}
return false;
}Writing audio (now i write test dummy audio) :
bool VVideoWriter::writeAudioFrame( const int16_t* data, int64_t timestamp )
{
if( m_context ) {
auto codecContext = m_audioStream->codec;
int buffer_size = av_samples_get_buffer_size(nullptr, codecContext->channels, codecContext->frame_size, codecContext->sample_fmt, 0);
float *samples = (float*)av_malloc(buffer_size);
for (int i = 0; i < buffer_size / sizeof(float); i++)
samples[i] = 1000. * sin((double)i/2.);
int ret = avcodec_fill_audio_frame( m_audioFrame, codecContext->channels, codecContext->sample_fmt, (const uint8_t*)samples, buffer_size, 0);
assert( ret >= 0 );
(void)(ret);
AVPacket pkt;
av_init_packet( & pkt );
pkt.data = nullptr;
pkt.size = 0;
int gotPacket = 0;
if( ! avcodec_encode_audio2( codecContext, &pkt, m_audioFrame, & gotPacket ) ) {
if( gotPacket == 1 ) {
pkt.stream_index = m_audioStream->index;
int res;
{
pkt.pts = AV_NOPTS_VALUE;
pkt.dts = AV_NOPTS_VALUE;
pkt.stream_index = m_audioStream->index;
res = av_write_frame( m_context, &pkt );
}
av_free_packet( & pkt );
return res >= 0;
}
assert( ! pkt.size );
return true;
}
return false;
}
return false;
}Here’s test example (i send video from webcam and dummy audio) :
class TestVVideoWriter : public sigslot::has_slots<>
{
public:
TestVVideoWriter( ) :
m_fileOpened( false )
{
}
void onCapturedFrame( cricket::VideoCapturer*, const cricket::CapturedFrame* capturedFrame )
{
if( m_fileOpened ) {
m_writer.writeVideoFrame( reinterpret_cast<const>( capturedFrame->data ),
capturedFrame->time_stamp / 1000 );
m_writer.writeAudioFrame( nullptr , 0 );
} else {
m_fileOpened = m_writer.openFile( "http://127.0.0.1:8080",
15, 40000, capturedFrame->width, capturedFrame->height,
16000, false, 64000 );
}
}
public:
vcore::VVideoWriter m_writer;
bool m_fileOpened;
};
TestVVideoWriter testWriter;
BOOST_AUTO_TEST_SUITE(TEST_VIDEO_WRITER)
BOOST_AUTO_TEST_CASE(testWritingVideo)
{
cricket::LinuxDeviceManager deviceManager;
std::vector devs;
if( deviceManager.GetVideoCaptureDevices( &devs ) ) {
if( devs.size( ) ) {
boost::shared_ptr camera( deviceManager.CreateVideoCapturer( devs[ 0 ] ) );
if( camera ) {
cricket::VideoFormat format( 320, 240, cricket::VideoFormat::FpsToInterval( 30 ),
camera->GetSupportedFormats( )->front( ).fourcc );
cricket::VideoFormat best;
if( camera->GetBestCaptureFormat( format, &best ) ) {
camera->SignalFrameCaptured.connect( &testWriter, &TestVVideoWriter::onCapturedFrame );
if( camera->Start( best ) != cricket::CS_FAILED ) {
boost::this_thread::sleep( boost::posix_time::seconds( 10 ) );
return;
}
}
}
}
}
std::cerr << "Problem has occured with camera" << std::endl;
}
BOOST_AUTO_TEST_SUITE_END() // TEST_VIDEO_WRITER
</const>But, in this case, gstreamer start playing video only when my test program stop executing (after 10 seconds in this case). It does not suit me, i want gstreamer start playing immediately after starting my test program.
Could someone help me ?
P.S. Sorry for my English.
-
Video Recording on Android from Frames using javaCV
23 juin 2016, par jawad bin zafarI am using this library javaCV to record video on android. They have provided a sample VideoRecording Activity But There is some bug which I could not figure out what I am doing it wrong or what is missing which causing this bug.
package org.bytedeco.javacv.recordactivity;
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 org.bytedeco.javacv.FFmpegFrameRecorder;
import static org.bytedeco.javacpp.opencv_core.*;
public class RecordActivity 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 = 320;
private int imageHeight = 240;
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 = 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 = 10;
IplImage[] images;
long[] timestamps;
ShortBuffer[] samples;
int imagesIndex, samplesIndex;
@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();
}
@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();
}
if(cameraDevice != null) {
cameraDevice.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 (RECORD_LENGTH > 0) {
imagesIndex = 0;
images = new IplImage[RECORD_LENGTH * frameRate];
timestamps = new long[images.length];
for (int i = 0; i < images.length; i++) {
images[i] = IplImage.create(imageWidth, imageHeight, IPL_DEPTH_8U, 2);
timestamps[i] = -1;
}
} else 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);
runAudioThread = true;
}
public void startRecording() {
initRecorder();
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;
}
for (int i = firstIndex; i <= lastIndex; i++) {
long t = timestamps[i % timestamps.length] - startTime;
if (t >= 0) {
if (t > recorder.getTimestamp()) {
recorder.setTimestamp(t);
}
recorder.record(images[i % images.length]);
}
}
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++) {
recorder.record(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);
}
//---------------------------------------------
// 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) {
if (RECORD_LENGTH <= 0) try {
recorder.record(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) {
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) {
if (audioRecord == null || audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {
startTime = System.currentTimeMillis();
return;
}
if (RECORD_LENGTH > 0) {
int i = imagesIndex++ % images.length;
yuvIplimage = images[i];
timestamps[i] = 1000 * (System.currentTimeMillis() - startTime);
}
/* get video data */
if (yuvIplimage != null && recording) {
yuvIplimage.getByteBuffer().put(data);
if (RECORD_LENGTH <= 0) try {
Log.v(LOG_TAG,"Writing Frame");
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");
}
}
}I am getting this error
The type org.bytedeco.javacpp.avutil$AVFrame cannot be resolved. It is indirectly referenced from required .class files
at following line in the above code
if (RECORD_LENGTH <= 0) try {
recorder.record(audioData);
//Log.v(LOG_TAG,"recording " + 1024*i + " to " + 1024*i+1024);
}at recorder.record(audioData). I don’t know what I am doing wrong here. New to JavaCV. Any help will be appreciated.
-
Muxing Android MediaCodec encoded H264 packets into RTMP
31 décembre 2015, par VadymI am coming from a thread Encoding H.264 from camera with Android MediaCodec. My setup is very similar. However, I attempt to write mux the encoded frames and with javacv and broadcast them via rtmp.
RtmpClient.java
...
private volatile BlockingQueue mFrameQueue = new LinkedBlockingQueue(MAXIMUM_VIDEO_FRAME_BACKLOG);
...
private void startStream() throws FrameRecorder.Exception, IOException {
if (TextUtils.isEmpty(mDestination)) {
throw new IllegalStateException("Cannot start RtmpClient without destination");
}
if (mCamera == null) {
throw new IllegalStateException("Cannot start RtmpClient without camera.");
}
Camera.Parameters cameraParams = mCamera.getParameters();
mRecorder = new FFmpegFrameRecorder(
mDestination,
mVideoQuality.resX,
mVideoQuality.resY,
(mAudioQuality.channelType.equals(AudioQuality.CHANNEL_TYPE_STEREO) ? 2 : 1));
mRecorder.setFormat("flv");
mRecorder.setFrameRate(mVideoQuality.frameRate);
mRecorder.setVideoBitrate(mVideoQuality.bitRate);
mRecorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
mRecorder.setSampleRate(mAudioQuality.samplingRate);
mRecorder.setAudioBitrate(mAudioQuality.bitRate);
mRecorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
mVideoStream = new VideoStream(mRecorder, mVideoQuality, mFrameQueue, mCamera);
mAudioStream = new AudioStream(mRecorder, mAudioQuality);
mRecorder.start();
// Setup a bufferred preview callback
setupCameraCallback(mCamera, mRtmpClient, DEFAULT_PREVIEW_CALLBACK_BUFFERS,
mVideoQuality.resX * mVideoQuality.resY * ImageFormat.getBitsPerPixel(
cameraParams.getPreviewFormat())/8);
try {
mVideoStream.start();
mAudioStream.start();
}
catch(Exception e) {
e.printStackTrace();
stopStream();
}
}
...
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
boolean frameQueued = false;
if (mRecorder == null || data == null) {
return;
}
frameQueued = mFrameQueue.offer(data);
// return the buffer to be reused - done in videostream
//camera.addCallbackBuffer(data);
}
...VideoStream.java
...
@Override
public void run() {
try {
mMediaCodec = MediaCodec.createEncoderByType("video/avc");
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", mVideoQuality.resX, mVideoQuality.resY);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, mVideoQuality.bitRate);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, mVideoQuality.frameRate);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
mMediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mMediaCodec.start();
}
catch(IOException e) {
e.printStackTrace();
}
long startTimestamp = System.currentTimeMillis();
long frameTimestamp = 0;
byte[] rawFrame = null;
try {
while (!Thread.interrupted()) {
rawFrame = mFrameQueue.take();
frameTimestamp = 1000 * (System.currentTimeMillis() - startTimestamp);
encodeFrame(rawFrame, frameTimestamp);
// return the buffer to be reused
mCamera.addCallbackBuffer(rawFrame);
}
}
catch (InterruptedException ignore) {
// ignore interrup while waiting
}
// Clean up video stream allocations
try {
mMediaCodec.stop();
mMediaCodec.release();
mOutputStream.flush();
mOutputStream.close();
} catch (Exception e){
e.printStackTrace();
}
}
...
private void encodeFrame(byte[] input, long timestamp) {
try {
ByteBuffer[] inputBuffers = mMediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mMediaCodec.getOutputBuffers();
int inputBufferIndex = mMediaCodec.dequeueInputBuffer(0);
if (inputBufferIndex >= 0) {
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(input);
mMediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, timestamp, 0);
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
if (outputBufferIndex >= 0) {
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
// Should this be a direct byte buffer?
byte[] outData = new byte[bufferInfo.size - bufferInfo.offset];
outputBuffer.get(outData);
mFrameRecorder.record(outData, bufferInfo.offset, outData.length, timestamp);
mMediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
}
else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
outputBuffers = mMediaCodec.getOutputBuffers();
} else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
// ignore for now
}
} catch (Throwable t) {
t.printStackTrace();
}
}
...FFmpegFrameRecorder.java
...
// Hackish codec copy frame recording function
public boolean record(byte[] encodedData, int offset, int length, long frameCount) throws Exception {
int ret;
if (encodedData == null) {
return false;
}
av_init_packet(video_pkt);
// this is why i wondered whether I should get outputbuffer data into direct byte buffer
video_outbuf.put(encodedData, 0, encodedData.length);
video_pkt.data(video_outbuf);
video_pkt.size(video_outbuf_size);
video_pkt.pts(frameCount);
video_pkt.dts(frameCount);
video_pkt.stream_index(video_st.index());
synchronized (oc) {
/* write the compressed frame in the media file */
if (interleaved && audio_st != null) {
if ((ret = av_interleaved_write_frame(oc, video_pkt)) < 0) {
throw new Exception("av_interleaved_write_frame() error " + ret + " while writing interleaved video frame.");
}
} else {
if ((ret = av_write_frame(oc, video_pkt)) < 0) {
throw new Exception("av_write_frame() error " + ret + " while writing video frame.");
}
}
}
return (video_pkt.flags() & AV_PKT_FLAG_KEY) == 1;
}
...When I try to stream the video and run ffprobe on it, I get the following output :
ffprobe version 2.5.3 Copyright (c) 2007-2015 the FFmpeg developers
built on Jan 19 2015 12:56:57 with gcc 4.1.2 (GCC) 20080704 (Red Hat 4.1.2-55)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib64 --mandir=/usr/share/man --arch=x86_64 --optflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --enable-bzlib --disable-crystalhd --enable-libass --enable-libdc1394 --enable-libfaac --enable-nonfree --disable-indev=jack --enable-libfreetype --enable-libgsm --enable-libmp3lame --enable-openal --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libxvid --enable-x11grab --enable-avfilter --enable-avresample --enable-postproc --enable-pthreads --disable-static --enable-shared --enable-gpl --disable-debug --disable-stripping --enable-libcaca --shlibdir=/usr/lib64 --enable-runtime-cpudetect
libavutil 54. 15.100 / 54. 15.100
libavcodec 56. 13.100 / 56. 13.100
libavformat 56. 15.102 / 56. 15.102
libavdevice 56. 3.100 / 56. 3.100
libavfilter 5. 2.103 / 5. 2.103
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 1.100 / 1. 1.100
libpostproc 53. 3.100 / 53. 3.100
Metadata:
Server NGINX RTMP (github.com/arut/nginx-rtmp-module)
width 320.00
height 240.00
displayWidth 320.00
displayHeight 240.00
duration 0.00
framerate 0.00
fps 0.00
videodatarate 261.00
videocodecid 7.00
audiodatarate 62.00
audiocodecid 10.00
profile
level
[live_flv @ 0x1edb0820] Could not find codec parameters for stream 0 (Video: none, none, 267 kb/s): unknown codec
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, live_flv, from 'rtmp://<server>/input/<stream>':
Metadata:
Server : NGINX RTMP (github.com/arut/nginx-rtmp-module)
displayWidth : 320
displayHeight : 240
fps : 0
profile :
level :
Duration: 00:00:00.00, start: 16.768000, bitrate: N/A
Stream #0:0: Video: none, none, 267 kb/s, 1k tbr, 1k tbn, 1k tbc
Stream #0:1: Audio: aac (LC), 16000 Hz, mono, fltp, 63 kb/s
Unsupported codec with id 0 for input stream 0
</stream></server>I am not, by any means, an expert in H264 or video encoding. I know that the encoded frames that come out from MediaCodec contain SPS NAL, PPS NAL, and frame NAL units. I’ve also written the MediaCodec output into a file and was able to play it back (I did have to specify the format and framerate as otherwise it would play too fast).
My assumption is that things should work (see how little I know :)). Knowing that SPS and PPS are written out, decoder should know enough. Yet, ffprobe fails to recognize codec, fps, and other video information. Do I need to pass packet flag information to FFmpegFrameRecorder.java:record() function ? Or should I use direct buffer ? Any suggestion will be appreciated ! I should figure things out with a hint.
PS : I know that some codecs use Planar and other SemiPlanar color formats. That distinction will come later if I get past this. Also, I didn’t go the Surface to MediaCodec way because I need to support API 17 and it requires more changes than this route, which I think helps me understand the more basic flow. Agan, I appreciate any suggestions. Please let me know if something needs to be clarified.
Update #1
So having done more testing, I see that my encoder outputs the following frames :
000000016742800DDA0507E806D0A1350000000168CE06E2
0000000165B840A6F1E7F0EA24000AE73BEB5F51CC7000233A84240...
0000000141E2031364E387FD4F9BB3D67F51CC7000279B9F9CFE811...
0000000141E40304423FFFFF0B7867F89FAFFFFFFFFFFCBE8EF25E6...
0000000141E602899A3512EF8AEAD1379F0650CC3F905131504F839...
...The very first frame contains SPS and PPS. From what I was able to see, these are transmitted only once. The rest are NAL types 1 and 5. So, my assumption is that, for ffprobe to see stream info not only when the stream starts, I should capture SPS and PPS frames and re-transmit them myself periodically, after a certain number of frames, or perhaps before every I-frame. What do you think ?
Update #2
Unable to validate that I’m writing frames successfully. After having tried to read back the written packet, I cannot validate written bytes. As strange, on successful write of IPL image and streaming, I also cannot print out bytes of encoded packet after
avcodec_encode_video2
. Hit the official dead end.