H263plusVideoSource.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) Massimo Cora' 2006 <maxcvs@email.it>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #include "H263plusVideoSource.hh"
00021 
00022 H263plusVideoSource*
00023 H263plusVideoSource::createNew( UsageEnvironment& env, BaseInputVideo* input_video, int stream_id /* = 0 */,
00024                                                             int frame_rate /* = 25*/, int encoder_internal_buf_size /*= 1000000*/ ) 
00025 {
00026         return new H263plusVideoSource( env, input_video, stream_id, frame_rate, encoder_internal_buf_size );
00027 }
00028 
00029 H263plusVideoSource::H263plusVideoSource ( UsageEnvironment& env, BaseInputVideo* input_video, int stream_id,
00030                                                                                   int frame_rate, int encoder_internal_buf_size ) 
00031                                                                                   : FramedSource( env ), 
00032                                                                                 _input_video( input_video ),
00033                                                                                 _fps( frame_rate ), 
00034                                                                                 _encoder_internal_buf_size( encoder_internal_buf_size ),
00035                                                                                 _stream_id( stream_id )
00036 {
00037         _initialized = false;
00038         _video_encoder = NULL;  
00039 
00040         // set the max output packet buffer size
00041         OutPacketBuffer::maxSize = OUTPUT_PACKET_BUFFER_MAXSIZE;
00042         
00043         _frame_rate_millis = 1000/frame_rate;
00044 
00045         // initialize device
00046         if ( _input_video->init_device () == true )
00047                 _initialized = true;
00048 }
00049 
00050 H263plusVideoSource::~H263plusVideoSource()
00051 {
00052         if ( _video_encoder )
00053                 delete _video_encoder;
00054         _video_encoder = NULL;
00055 
00056         _initialized = false;
00057         // DO *NOT* delete _input_video; otherwise in multi stream mode you'll have a crash
00058 }
00059 
00060 void H263plusVideoSource::startCapture() 
00061 {
00062         IplImage* current_frame;
00063         fPreferredFrameSize = 0;
00064 
00065         if ( _input_video->is_multi_stream () ) {
00066                 if ( (current_frame = _input_video->get_next_frame_by_stream_id ( _stream_id )) == NULL ) {
00067 //                      DEBUG_PRINT ("id #%d :_input_video->get_next_frame_by_stream_id = NULL\n", _stream_id );
00068                 }
00069         }
00070         else if ( (current_frame = _input_video->get_next_frame ()) == NULL ) {
00071                 DEBUG_PRINT ("_input_video->get_next_frame = NULL\n");
00072         }
00073 
00074         // we cannot proceed with encoding or something else. Just exit here
00075         if ( current_frame == NULL ) {
00076                 fFrameSize = fPreferredFrameSize = 0;
00077                 _encoded_frame = NULL;
00078                 return;
00079         }
00080         gettimeofday( &fLastCaptureTime, &Idunno );
00081 
00082         // proceed with encoding
00083         if ( _video_encoder == NULL ) {
00084                 // encoder is NULL, create it.
00085                 _video_encoder = new OStreamVideoEncoder( 1000000, (ostream_encoding_type)OSTREAM_ENCODING_H263P,
00086                                                                 current_frame->width, current_frame->height, _fps );
00087         }
00088 
00089         int h263p_frame_size;
00090         _encoded_frame = _video_encoder->encode_video_frame( current_frame, &h263p_frame_size );
00091         
00092         if ( h263p_frame_size > _encoder_internal_buf_size || _encoded_frame == NULL ) {
00093                 DEBUG_PRINT ("Error: the h263+ frame to stream is too big or NULL.\n");
00094                 fPreferredFrameSize = 0;
00095                 _encoded_frame = NULL;
00096                 return;
00097         }
00098                 
00099     if ( h263p_frame_size <= 0 || (unsigned int)h263p_frame_size  > fMaxSize ) {
00100                 DEBUG_PRINT ("Error: the h263+ frame to stream is too big.\n");
00101                 fPreferredFrameSize = 0;
00102                 _encoded_frame = NULL;
00103                 return;
00104         }
00105 
00106         fPreferredFrameSize = h263p_frame_size;
00107 
00108         cvReleaseImage( &current_frame );
00109 }
00110 
00111 //--------------------------------------------------------------------------
00112 // 
00113 
00114 void H263plusVideoSource::doGetNextFrame() {
00115         // let the cpu breath.
00116         CV_WAIT_KEY( _frame_rate_millis );
00117 
00118         deliverFrame();
00119 }
00120 
00121 //--------------------------------------------------------------------------
00122 //
00123 
00124 void H263plusVideoSource::deliverFrame() {
00125         //
00126         // This would be called when new frame data is available from the device.
00127         // This function should deliver the next frame of data from the device,
00128         // using the following parameters (class members):
00129         // 'in' parameters (these should *not* be modified by this function):
00130         //     fTo: The frame data is copied to this address.
00131         //         (Note that the variable "fTo" is *not* modified.  Instead,
00132         //          the frame data is copied to the address pointed to by "fTo".)
00133         //     fMaxSize: This is the maximum number of bytes that can be copied
00134         //         (If the actual frame is larger than this, then it should
00135         //          be truncated, and "fNumTruncatedBytes" set accordingly.)
00136         // 'out' parameters (these are modified by this function):
00137         //     fFrameSize: Should be set to the delivered frame size (<= fMaxSize).
00138         //     fNumTruncatedBytes: Should be set iff the delivered frame would have been
00139         //         bigger than "fMaxSize", in which case it's set to the number of bytes
00140         //         that have been omitted.
00141         //     fPresentationTime: Should be set to the frame's presentation time
00142         //         (seconds, microseconds).
00143         //     fDurationInMicroseconds: Should be set to the frame's duration, if known.
00144 
00145         // Start capturing the next frame:
00146         startCapture();
00147 
00148         // if our _video_input source is a multi-stream one there is often the possibility that 
00149         // no IplImages are available for streaming.
00150         // If that's the case ignore the _encoded_frame to NULL 
00151 
00152         
00153         
00154         if ( fPreferredFrameSize <= 0 || _encoded_frame == NULL ) {
00155                 if ( _input_video->is_multi_stream() ) {
00156                         nextTask()  = envir().taskScheduler().scheduleDelayedTask( 0, (TaskFunc*)afterGetting,
00157                                                                                                                                         this );
00158                         return;
00159                 }
00160                 else {
00161                         DEBUG_PRINT ("closing from void H263plusVideoSource::deliverFrame() {\n");
00162                         handleClosure(this);
00163                         return;
00164                 }
00165         }
00166 
00167         // Set the 'presentation time': the time that this frame was captured
00168         fPresentationTime = fLastCaptureTime;
00169 
00170         fFrameSize = fPreferredFrameSize;
00171         memcpy( fTo, _encoded_frame, fFrameSize );
00172 
00173         if ( fFrameSize == fMaxSize ) {
00174                 DEBUG_PRINT( "H263plusVideoSource::doGetNextFrame(): "
00175                         "read maximum buffer size: %d bytes.  Frame may be truncated\n", fMaxSize);
00176         }
00177 
00178         // After delivering the data, switch to another task, and inform
00179         // the reader that he has data:
00180         nextTask()  = envir().taskScheduler().scheduleDelayedTask( 0, (TaskFunc*)afterGetting,
00181                                                                                                                                 this);
00182 }
00183 
00184 

Generated on Tue Dec 26 10:32:38 2006 for Omnimeeting by  doxygen 1.4.7