Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
rakib062
Posts: 3
Registered: ‎05-12-2013
My Device: BB10 Dev Alpha

Can not write encoded video

 Hi,

 

I am trying to get the encoded data from camera API and then write this data to a file. But after writing I can not play it using any H264 player. Here is the code to start encoding:

 if (mCameraHandle != CAMERA_HANDLE_INVALID) {
        qDebug() << "camera already running";
        return EBUSY;
    }
    mCameraUnit = cameraUnit;
    camera_error_t error;
    if (camera_open(mCameraUnit,
                CAMERA_MODE_RW | CAMERA_MODE_ROLL,
                    &mCameraHandle) != CAMERA_EOK) {
        qDebug() << "could not open camera";
        return EIO;
    }
    if(camera_can_feature(mCameraHandle,CAMERA_FEATURE_VIDEO))
    {
        fprintf(stderr,"Camera can feature video\n");
    }
    else
    {
        fprintf(stderr,"Camera can not feature video\n");
    }
    if(camera_can_feature(mCameraHandle,CAMERA_FEATURE_PREVIEWISVIDEO ))
    {
        fprintf(stderr,"Camera can feature CAMERA_FEATURE_PREVIEWISVIDEO \n");
    }
    else
    {
        fprintf(stderr,"Camera can not feature CAMERA_FEATURE_PREVIEWISVIDEO \n");
    }


    if ((error=camera_set_videovf_property(mCameraHandle,
                                    CAMERA_IMGPROP_WIN_GROUPID, group.toStdString().c_str(),
                                    CAMERA_IMGPROP_WIN_ID, id.toStdString().c_str(),
                                    CAMERA_IMGPROP_FRAMERATE, (double)30.0  )) == CAMERA_EOK)
    {
        fprintf(stderr,"viewfinder configured\n");

        error=camera_start_video_viewfinder(mCameraHandle,NULL,NULL,NULL);
        if(error==CAMERA_EOK)
        {
            fprintf(stderr,"Camera viewfinder open success\n");
        }
        else
        {
            fprintf(stderr,"Camera viewfinder open failed:%s\n",ErrorToString(error).toAscii().constData());
        }

        if(mCameraHandle==CAMERA_HANDLE_INVALID)
            fprintf(stderr,"Invalid camera handle\n");
        else
            fprintf(stderr,"Valid camera handle\n");

        QString f("data/myvideo.mp4");
    
        this->audioFile.setFileName("data/audio.mp3");
        this->videoFile.setFileName(f);

        audioFile.remove();
        videoFile.remove();

        if(!videoFile.open(QIODevice::WriteOnly))
        {
            fprintf(stderr,"Can not open file to write video:%s\n",f.toAscii().constData());
            exit(-1);
        }
        if(!audioFile.open(QIODevice::WriteOnly))
        {
            fprintf(stderr,"Can not open file to write audio:%s\n",f.toAscii().constData());
            exit(-1);
        }

        error=camera_start_encode(mCameraHandle,&VideoCallBack,&EncVideoCallBack,&EncAudioCallBack,&VideoStatusCallBack,this);
        if(error==CAMERA_EOK)
        {
            fprintf(stdout,"Encoding started\n");
             mStartFrontButton->setVisible(false);
                        mStartRearButton->setVisible(false);
                        mStopButton->setVisible(true);
                        mTakePictureButton->setVisible(true);
                        mTakePictureButton->setEnabled(true);
                        return EOK;
        }
        else
        {
            fprintf(stdout,"Encoding failed: %d, %s\n",error,ErrorToString(error).toAscii().constData());
        }

 

And here is the call back function:

 

static void VideoCallBack(camera_handle_t handle, camera_buffer_t* buffer, void* ob)
    {
        fprintf(stderr,"Video callback function called. buffer  size:%ld\n",buffer->framesize);
        ((HelloCameraApp*)ob)->SaveVideoData((const char*)buffer->framebuf,buffer->framesize);
    }

 

 static void EncAudioCallBack(camera_handle_t handle, camera_buffer_t* buffer, void* ob)
    {
        fprintf(stderr,"EncAudioCallBack function called\n");
        ((HelloCameraApp*)ob)->SaveAudioData((const char*)buffer->framebuf,buffer->framesize);
    }

    void SaveVideoData(const char* data,uint64_t size)
    {
        qint64 len=this->videoFile.write(data,size);
        this->bufCount++;
        fprintf(stderr,"End of data saving:%lld. bufCount:%d\n",len,bufCount);
    }
    
    void SaveAudioData(const char* data,uint64_t size)
    {
        qint64 len=this->audioFile.write(data,size);
        this->bufCount++;
        fprintf(stderr,"End of data saving:%lld. bufCount:%d\n",len,bufCount);
    }

 

Any help will be highly appreciated.

 

Thanks all.

 

 

 

 

Developer
nemoryoliver
Posts: 97
Registered: ‎11-13-2012
My Device: BB10 Simulator

Re: Can not write encoded video

Any solutions to this please? Any H264 Camera Video Recorder sample we can test please?
Retired
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30

Re: Can not write encoded video

camera_start_encode is really to let you do things like streaming video, and you need to do some work to be able to play it. If you are just saving the video to the file system, use the regular camera_start_video, it will automatically encode the video into a playable file and write it out to the file system.


The only reason camera_start_encode exists is because camera_start_video was too good at creating video files :smileyhappy:

 

There are a bunch of camera samples here: http://blackberry.github.io/Community/Camera.html

 

Including a few video camera ones.

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer

Re: Can not write encoded video

couple of quick observations...

you are trying to write data to disk using buffer->framesize.  This just defines the size of the camera_buffer_t struct, and not the size of the data.

To figure out the data size, you'll need to look at the frametype and specific framedesc union members.

For encoded video, frametype will be CAMERA_FRAMETYPE_COMPRESSEDVIDEO (or AUDIO), and the framedesc will be a camera_frame_compressedvideo_t (which has a "bufsize" field).

 

Beware that not many apps can play back raw H264 or AVC1 streams.  You typically would be buiding your own MPEG4 headers if you are using camera_start_encode(), or you are streaming video somewhere, or (an interesting application), maybe you are just watching the bitrate to determine if there is motion in the image :smileyhappy: