00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "H263plusVideoSource.hh"
00021
00022 H263plusVideoSource*
00023 H263plusVideoSource::createNew( UsageEnvironment& env, BaseInputVideo* input_video, int stream_id ,
00024 int frame_rate , int encoder_internal_buf_size )
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
00041 OutPacketBuffer::maxSize = OUTPUT_PACKET_BUFFER_MAXSIZE;
00042
00043 _frame_rate_millis = 1000/frame_rate;
00044
00045
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
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
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
00075 if ( current_frame == NULL ) {
00076 fFrameSize = fPreferredFrameSize = 0;
00077 _encoded_frame = NULL;
00078 return;
00079 }
00080 gettimeofday( &fLastCaptureTime, &Idunno );
00081
00082
00083 if ( _video_encoder == NULL ) {
00084
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( ¤t_frame );
00109 }
00110
00111
00112
00113
00114 void H263plusVideoSource::doGetNextFrame() {
00115
00116 CV_WAIT_KEY( _frame_rate_millis );
00117
00118 deliverFrame();
00119 }
00120
00121
00122
00123
00124 void H263plusVideoSource::deliverFrame() {
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 startCapture();
00147
00148
00149
00150
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
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
00179
00180 nextTask() = envir().taskScheduler().scheduleDelayedTask( 0, (TaskFunc*)afterGetting,
00181 this);
00182 }
00183
00184