Index: /pjproject/trunk/pjmedia/include/pjmedia/errno.h
===================================================================
--- /pjproject/trunk/pjmedia/include/pjmedia/errno.h	(revision 3900)
+++ /pjproject/trunk/pjmedia/include/pjmedia/errno.h	(revision 3901)
@@ -347,4 +347,9 @@
  */
 #define PJMEDIA_CODEC_EINMODE	    (PJMEDIA_ERRNO_START+86)    /* 220086 */
+/**
+ * @hideinitializer
+ * Bad or corrupted bitstream.
+ */
+#define PJMEDIA_CODEC_EBADBITSTREAM (PJMEDIA_ERRNO_START+87)    /* 220087 */
 
 
Index: /pjproject/trunk/pjmedia/include/pjmedia/event.h
===================================================================
--- /pjproject/trunk/pjmedia/include/pjmedia/event.h	(revision 3900)
+++ /pjproject/trunk/pjmedia/include/pjmedia/event.h	(revision 3901)
@@ -71,12 +71,12 @@
 
     /**
-     * Video key frame has just been decoded event.
-     */
-    PJMEDIA_EVENT_KEY_FRAME_FOUND = PJMEDIA_FOURCC('I', 'F', 'R', 'F'),
-
-    /**
-     * Video decoding error due to missing key frame event.
-     */
-    PJMEDIA_EVENT_KEY_FRAME_MISSING = PJMEDIA_FOURCC('I', 'F', 'R', 'M'),
+     * Video keyframe has just been decoded event.
+     */
+    PJMEDIA_EVENT_KEYFRAME_FOUND = PJMEDIA_FOURCC('I', 'F', 'R', 'F'),
+
+    /**
+     * Video decoding error due to missing keyframe event.
+     */
+    PJMEDIA_EVENT_KEYFRAME_MISSING = PJMEDIA_FOURCC('I', 'F', 'R', 'M'),
 
     /**
@@ -136,9 +136,9 @@
 typedef pjmedia_event_dummy_data pjmedia_event_mouse_btn_down_data;
 
-/** Additional parameters for key frame found event */
-typedef pjmedia_event_dummy_data pjmedia_event_key_frame_found_data;
-
-/** Additional parameters for key frame missing event */
-typedef pjmedia_event_dummy_data pjmedia_event_key_frame_missing_data;
+/** Additional parameters for keyframe found event */
+typedef pjmedia_event_dummy_data pjmedia_event_keyframe_found_data;
+
+/** Additional parameters for keyframe missing event */
+typedef pjmedia_event_dummy_data pjmedia_event_keyframe_missing_data;
 
 /**
@@ -206,9 +206,9 @@
 	pjmedia_event_mouse_btn_down_data	mouse_btn_down;
 
-	/** Key frame found event data */
-	pjmedia_event_key_frame_found_data	key_frm_found;
-
-	/** Key frame missing event data */
-	pjmedia_event_key_frame_missing_data	key_frm_missing;
+	/** Keyframe found event data */
+	pjmedia_event_keyframe_found_data	keyframe_found;
+
+	/** Keyframe missing event data */
+	pjmedia_event_keyframe_missing_data	keyframe_missing;
 
 	/** Storage for user event data */
Index: /pjproject/trunk/pjmedia/include/pjmedia/vid_codec.h
===================================================================
--- /pjproject/trunk/pjmedia/include/pjmedia/vid_codec.h	(revision 3900)
+++ /pjproject/trunk/pjmedia/include/pjmedia/vid_codec.h	(revision 3901)
@@ -76,4 +76,35 @@
 
 } pjmedia_vid_packing;
+
+
+/**
+ * Enumeration of video frame info flag for the bit_info field in the
+ * pjmedia_frame.
+ */
+typedef enum pjmedia_vid_frm_bit_info
+{
+    /**
+     * The video frame is keyframe.
+     */
+    PJMEDIA_VID_FRM_KEYFRAME	= 1
+
+} pjmedia_vid_frm_bit_info;
+
+
+/**
+ * Encoding option.
+ */
+typedef struct pjmedia_vid_encode_opt
+{
+    /**
+     * Flag to force the encoder to generate keyframe for the specified input
+     * frame. When this flag is set, application can verify the result by
+     * examining PJMEDIA_VID_FRM_KEYFRAME flag in the bit_info field of the
+     * output frame.
+     */
+    pj_bool_t force_keyframe;
+
+} pjmedia_vid_encode_opt;
+
 
 /** 
@@ -179,5 +210,5 @@
      * See #pjmedia_vid_codec_modify().
      */
-    pj_status_t	(*modify)(pjmedia_vid_codec *codec, 
+    pj_status_t	(*modify)(pjmedia_vid_codec *codec,
 			  const pjmedia_vid_codec_param *param);
 
@@ -192,4 +223,5 @@
      */
     pj_status_t (*encode_begin)(pjmedia_vid_codec *codec,
+				const pjmedia_vid_encode_opt *opt,
 				const pjmedia_frame *input,
 				unsigned out_size,
@@ -364,13 +396,4 @@
 
 /**
- * Initialize pjmedia_vid_codec structure with default values.
- *
- * @param codec	    The codec to be initialized.
- * @param sig	    Codec's object signature (see signatures.h)
- */
-PJ_DECL(void) pjmedia_vid_codec_reset(pjmedia_vid_codec *codec,
-                                      pjmedia_obj_sig sig);
-
-/**
  * Initialize codec manager. If there is no the default video codec manager,
  * this function will automatically set the default video codec manager to
@@ -729,4 +752,5 @@
  *
  * @param codec		The codec instance.
+ * @param opt		Optional encoding options.
  * @param input		The input frame.
  * @param out_size	The length of buffer in the output frame. This
@@ -742,4 +766,5 @@
 PJ_INLINE(pj_status_t)
 pjmedia_vid_codec_encode_begin( pjmedia_vid_codec *codec,
+				const pjmedia_vid_encode_opt *opt,
 				const pjmedia_frame *input,
 				unsigned out_size,
@@ -747,5 +772,5 @@
 				pj_bool_t *has_more)
 {
-    return (*codec->op->encode_begin)(codec, input, out_size, output,
+    return (*codec->op->encode_begin)(codec, opt, input, out_size, output,
 				      has_more);
 }
Index: /pjproject/trunk/pjmedia/include/pjmedia/vid_stream.h
===================================================================
--- /pjproject/trunk/pjmedia/include/pjmedia/vid_stream.h	(revision 3900)
+++ /pjproject/trunk/pjmedia/include/pjmedia/vid_stream.h	(revision 3901)
@@ -299,8 +299,8 @@
 
 /**
- * Pause the individual channel in the stream.
- *
- * @param stream	The video channel.
- * @param dir		Which direction to pause.
+ * Pause stream channels.
+ *
+ * @param stream	The video stream.
+ * @param dir		Which channel direction to pause.
  *
  * @return		PJ_SUCCESS on success.
@@ -310,8 +310,8 @@
 
 /**
- * Resume the individual channel in the stream.
- *
- * @param stream	The video channel.
- * @param dir		Which direction to resume.
+ * Resume stream channels.
+ *
+ * @param stream	The video stream.
+ * @param dir		Which channel direction to resume.
  *
  * @return		PJ_SUCCESS on success;
@@ -322,4 +322,15 @@
 
 /**
+ * Force stream to send video keyframe on the next transmission.
+ *
+ * @param stream	The video stream.
+ *
+ * @return		PJ_SUCCESS on success;
+ */
+PJ_DECL(pj_status_t) pjmedia_vid_stream_send_keyframe(
+						pjmedia_vid_stream *stream);
+
+
+/**
  * @}
  */
Index: /pjproject/trunk/pjmedia/src/pjmedia-codec/ffmpeg_codecs.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-codec/ffmpeg_codecs.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia-codec/ffmpeg_codecs.c	(revision 3901)
@@ -78,9 +78,10 @@
 static pj_status_t  ffmpeg_codec_get_param(pjmedia_vid_codec *codec,
 					   pjmedia_vid_codec_param *param);
-static pj_status_t ffmpeg_codec_encode_begin( pjmedia_vid_codec *codec,
-                                              const pjmedia_frame *input,
-					      unsigned out_size,
-					      pjmedia_frame *output,
-					      pj_bool_t *has_more);
+static pj_status_t ffmpeg_codec_encode_begin(pjmedia_vid_codec *codec,
+					     const pjmedia_vid_encode_opt *opt,
+                                             const pjmedia_frame *input,
+					     unsigned out_size,
+					     pjmedia_frame *output,
+					     pj_bool_t *has_more);
 static pj_status_t ffmpeg_codec_encode_more(pjmedia_vid_codec *codec,
 					    unsigned out_size,
@@ -137,6 +138,4 @@
     pjmedia_vid_codec_param	     param;	/**< Codec param	    */
     pj_pool_t			    *pool;	/**< Pool for each instance */
-    pj_timestamp		     last_tx;   /**< Timestamp of last 
-						     transmit		    */
 
     /* Format info and apply format param */
@@ -150,8 +149,10 @@
     void			    *enc_buf;
     unsigned			     enc_buf_size;
+    pj_bool_t			     enc_buf_is_keyframe;
     unsigned			     enc_frame_len;
     unsigned     		     enc_processed;
     void			    *dec_buf;
     unsigned			     dec_buf_size;
+    pj_timestamp		     last_dec_keyframe_ts; 
 
     /* The ffmpeg codec states. */
@@ -982,10 +983,9 @@
     /* Create pool for codec instance */
     pool = pj_pool_create(ffmpeg_factory.pf, "ffmpeg codec", 512, 512, NULL);
-    codec = PJ_POOL_ALLOC_T(pool, pjmedia_vid_codec);
+    codec = PJ_POOL_ZALLOC_T(pool, pjmedia_vid_codec);
     if (!codec) {
         status = PJ_ENOMEM;
         goto on_error;
     }
-    pjmedia_vid_codec_reset(codec, PJMEDIA_SIG_VID_CODEC_FFMPEG);
     codec->op = &ffmpeg_op;
     codec->factory = factory;
@@ -1362,4 +1362,5 @@
  */
 static pj_status_t ffmpeg_codec_encode_whole(pjmedia_vid_codec *codec,
+					     const pjmedia_vid_encode_opt *opt,
 					     const pjmedia_frame *input,
 					     unsigned output_buf_len,
@@ -1402,4 +1403,8 @@
     }
 
+    /* Force keyframe */
+    if (opt && opt->force_keyframe)
+	avframe.pict_type = AV_PICTURE_TYPE_I;
+
     err = avcodec_encode_video(ff->enc_ctx, out_buf, out_buf_len, &avframe);
     if (err < 0) {
@@ -1408,4 +1413,7 @@
     } else {
         output->size = err;
+	output->bit_info = 0;
+	if (ff->enc_ctx->coded_frame->key_frame)
+	    output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME;
     }
 
@@ -1413,9 +1421,10 @@
 }
 
-static pj_status_t ffmpeg_codec_encode_begin( pjmedia_vid_codec *codec,
-                                              const pjmedia_frame *input,
-					      unsigned out_size,
-					      pjmedia_frame *output,
-					      pj_bool_t *has_more)
+static pj_status_t ffmpeg_codec_encode_begin(pjmedia_vid_codec *codec,
+					     const pjmedia_vid_encode_opt *opt,
+					     const pjmedia_frame *input,
+					     unsigned out_size,
+					     pjmedia_frame *output,
+					     pj_bool_t *has_more)
 {
     ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data;
@@ -1425,5 +1434,6 @@
 
     if (ff->whole) {
-	status = ffmpeg_codec_encode_whole(codec, input, out_size, output);
+	status = ffmpeg_codec_encode_whole(codec, opt, input, out_size,
+					   output);
     } else {
 	pjmedia_frame whole_frm;
@@ -1434,9 +1444,11 @@
 	whole_frm.buf = ff->enc_buf;
 	whole_frm.size = ff->enc_buf_size;
-	status = ffmpeg_codec_encode_whole(codec, input,
+	status = ffmpeg_codec_encode_whole(codec, opt, input,
 	                                   whole_frm.size, &whole_frm);
 	if (status != PJ_SUCCESS)
 	    return status;
 
+	ff->enc_buf_is_keyframe = (whole_frm.bit_info & 
+				   PJMEDIA_VID_FRM_KEYFRAME);
 	ff->enc_frame_len = (unsigned)whole_frm.size;
 	ff->enc_processed = 0;
@@ -1454,4 +1466,7 @@
         output->size = payload_len;
 
+	if (ff->enc_buf_is_keyframe)
+	    output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME;
+
         *has_more = (ff->enc_processed < ff->enc_frame_len);
     }
@@ -1490,4 +1505,7 @@
     output->size = payload_len;
 
+    if (ff->enc_buf_is_keyframe)
+	output->bit_info |= PJMEDIA_VID_FRM_KEYFRAME;
+
     *has_more = (ff->enc_processed < ff->enc_frame_len);
 
@@ -1495,4 +1513,79 @@
 }
 
+
+static pj_status_t check_decode_result(pjmedia_vid_codec *codec,
+				       const pj_timestamp *ts,
+				       pj_bool_t got_keyframe)
+{
+    ffmpeg_private *ff = (ffmpeg_private*)codec->codec_data;
+    pjmedia_video_apply_fmt_param *vafp = &ff->dec_vafp;
+    pjmedia_event event;
+
+    /* Check for format change.
+     * Decoder output format is set by libavcodec, in case it is different
+     * to the configured param.
+     */
+    if (ff->dec_ctx->pix_fmt != ff->expected_dec_fmt ||
+	ff->dec_ctx->width != (int)vafp->size.w ||
+	ff->dec_ctx->height != (int)vafp->size.h)
+    {
+	pjmedia_format_id new_fmt_id;
+	pj_status_t status;
+
+	/* Get current raw format id from ffmpeg decoder context */
+	status = PixelFormat_to_pjmedia_format_id(ff->dec_ctx->pix_fmt, 
+						  &new_fmt_id);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	/* Update decoder format in param */
+		ff->param.dec_fmt.id = new_fmt_id;
+	ff->param.dec_fmt.det.vid.size.w = ff->dec_ctx->width;
+	ff->param.dec_fmt.det.vid.size.h = ff->dec_ctx->height;
+	ff->expected_dec_fmt = ff->dec_ctx->pix_fmt;
+
+	/* Re-init format info and apply-param of decoder */
+	ff->dec_vfi = pjmedia_get_video_format_info(NULL, ff->param.dec_fmt.id);
+	if (!ff->dec_vfi)
+	    return PJ_ENOTSUP;
+	pj_bzero(&ff->dec_vafp, sizeof(ff->dec_vafp));
+	ff->dec_vafp.size = ff->param.dec_fmt.det.vid.size;
+	ff->dec_vafp.buffer = NULL;
+	status = (*ff->dec_vfi->apply_fmt)(ff->dec_vfi, &ff->dec_vafp);
+	if (status != PJ_SUCCESS)
+	    return status;
+
+	/* Realloc buffer if necessary */
+	if (ff->dec_vafp.framebytes > ff->dec_buf_size) {
+	    PJ_LOG(5,(THIS_FILE, "Reallocating decoding buffer %u --> %u",
+		       (unsigned)ff->dec_buf_size,
+		       (unsigned)ff->dec_vafp.framebytes));
+	    ff->dec_buf_size = ff->dec_vafp.framebytes;
+	    ff->dec_buf = pj_pool_alloc(ff->pool, ff->dec_buf_size);
+	}
+
+	/* Broadcast format changed event */
+	pjmedia_event_init(&event, PJMEDIA_EVENT_FMT_CHANGED, ts, codec);
+	event.data.fmt_changed.dir = PJMEDIA_DIR_DECODING;
+	pj_memcpy(&event.data.fmt_changed.new_fmt, &ff->param.dec_fmt,
+		  sizeof(ff->param.dec_fmt));
+	pjmedia_event_publish(NULL, codec, &event, 0);
+    }
+
+    /* Check for missing/found keyframe */
+    if (got_keyframe) {
+	pj_get_timestamp(&ff->last_dec_keyframe_ts);
+
+	/* Broadcast keyframe event */
+        pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_FOUND, ts, codec);
+        pjmedia_event_publish(NULL, codec, &event, 0);
+    } else if (ff->last_dec_keyframe_ts.u64 == 0) {
+	/* Broadcast missing keyframe event */
+	pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_MISSING, ts, codec);
+	pjmedia_event_publish(NULL, codec, &event, 0);
+    }
+
+    return PJ_SUCCESS;
+}
 
 /*
@@ -1558,67 +1651,29 @@
 #endif
     if (err < 0) {
+	pjmedia_event event;
+
 	output->type = PJMEDIA_FRAME_TYPE_NONE;
 	output->size = 0;
         print_ffmpeg_err(err);
-        return PJMEDIA_CODEC_EFAILED;
+
+	/* Broadcast missing keyframe event */
+	pjmedia_event_init(&event, PJMEDIA_EVENT_KEYFRAME_MISSING,
+			   &input->timestamp, codec);
+	pjmedia_event_publish(NULL, codec, &event, 0);
+
+	return PJMEDIA_CODEC_EBADBITSTREAM;
     } else if (got_picture) {
         pjmedia_video_apply_fmt_param *vafp = &ff->dec_vafp;
         pj_uint8_t *q = (pj_uint8_t*)output->buf;
 	unsigned i;
-
-	/* Decoder output format is set by libavcodec, in case it is different
-	 * to the configured param.
+	pj_status_t status;
+
+	/* Check decoding result, e.g: see if the format got changed,
+	 * keyframe found/missing.
 	 */
-	if (ff->dec_ctx->pix_fmt != ff->expected_dec_fmt ||
-	    ff->dec_ctx->width != (int)vafp->size.w ||
-	    ff->dec_ctx->height != (int)vafp->size.h)
-	{
-	    pjmedia_format_id new_fmt_id;
-	    pj_status_t status;
-
-	    /* Get current raw format id from ffmpeg decoder context */
-	    status = PixelFormat_to_pjmedia_format_id(ff->dec_ctx->pix_fmt, 
-						      &new_fmt_id);
-	    if (status != PJ_SUCCESS)
-		return status;
-
-	    /* Update decoder format in param */
-    	    ff->param.dec_fmt.id = new_fmt_id;
-	    ff->param.dec_fmt.det.vid.size.w = ff->dec_ctx->width;
-	    ff->param.dec_fmt.det.vid.size.h = ff->dec_ctx->height;
-	    ff->expected_dec_fmt = ff->dec_ctx->pix_fmt;
-
-	    /* Re-init format info and apply-param of decoder */
-	    ff->dec_vfi = pjmedia_get_video_format_info(NULL, ff->param.dec_fmt.id);
-	    if (!ff->dec_vfi)
-		return PJ_ENOTSUP;
-	    pj_bzero(&ff->dec_vafp, sizeof(ff->dec_vafp));
-	    ff->dec_vafp.size = ff->param.dec_fmt.det.vid.size;
-	    ff->dec_vafp.buffer = NULL;
-	    status = (*ff->dec_vfi->apply_fmt)(ff->dec_vfi, &ff->dec_vafp);
-	    if (status != PJ_SUCCESS)
-		return status;
-
-	    /* Realloc buffer if necessary */
-	    if (ff->dec_vafp.framebytes > ff->dec_buf_size) {
-		PJ_LOG(5,(THIS_FILE, "Reallocating decoding buffer %u --> %u",
-			   (unsigned)ff->dec_buf_size,
-			   (unsigned)ff->dec_vafp.framebytes));
-		ff->dec_buf_size = ff->dec_vafp.framebytes;
-		ff->dec_buf = pj_pool_alloc(ff->pool, ff->dec_buf_size);
-	    }
-
-	    /* Broadcast event */
-	    {
-		pjmedia_event event;
-
-		pjmedia_event_init(&event, PJMEDIA_EVENT_FMT_CHANGED,
-				   &input->timestamp, codec);
-		event.data.fmt_changed.dir = PJMEDIA_DIR_DECODING;
-		pj_memcpy(&event.data.fmt_changed.new_fmt, &ff->param.dec_fmt,
-			  sizeof(ff->param.dec_fmt));
-		pjmedia_event_publish(NULL, codec, &event, 0);
-	    }
-	}
+	status = check_decode_result(codec, &input->timestamp,
+				     avframe.key_frame);
+	if (status != PJ_SUCCESS)
+	    return status;
 
 	/* Check provided buffer size */
@@ -1650,14 +1705,4 @@
 	output->type = PJMEDIA_FRAME_TYPE_VIDEO;
         output->size = vafp->framebytes;
-
-        /* Check if we got key frame */
-        if (avframe.key_frame)
-        {
-            pjmedia_event event;
-
-            pjmedia_event_init(&event, PJMEDIA_EVENT_KEY_FRAME_FOUND,
-                               &output->timestamp, codec);
-            pjmedia_event_publish(NULL, codec, &event, 0);
-        }
     } else {
 	output->type = PJMEDIA_FRAME_TYPE_NONE;
Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/colorbar_dev.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/colorbar_dev.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/colorbar_dev.c	(revision 3901)
@@ -578,4 +578,6 @@
     struct cbar_stream *stream = (struct cbar_stream*)strm;
 
+    frame->type = PJMEDIA_FRAME_TYPE_VIDEO;
+    frame->bit_info = 0;
     frame->timestamp = stream->ts;
     stream->ts.u64 += stream->ts_inc;
Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/dshow_dev.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/dshow_dev.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/dshow_dev.c	(revision 3901)
@@ -578,5 +578,5 @@
     }
 
-    frame.type = PJMEDIA_TYPE_VIDEO;
+    frame.type = PJMEDIA_FRAME_TYPE_VIDEO;
     IMediaSample_GetPointer(pMediaSample, (BYTE **)&frame.buf);
     frame.size = IMediaSample_GetActualDataLength(pMediaSample);
Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_dev.m	(revision 3901)
@@ -361,5 +361,5 @@
     CVPixelBufferLockBaseAddress(imageBuffer, 0); 
     
-    frame.type = PJMEDIA_TYPE_VIDEO;
+    frame.type = PJMEDIA_FRAME_TYPE_VIDEO;
     frame.buf = CVPixelBufferGetBaseAddress(imageBuffer);
     frame.size = stream->frame_size;
Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/qt_dev.m
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/qt_dev.m	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/qt_dev.m	(revision 3901)
@@ -372,5 +372,5 @@
 	return;
     
-    frame.type = PJMEDIA_TYPE_VIDEO;
+    frame.type = PJMEDIA_FRAME_TYPE_VIDEO;
     frame.buf = [sampleBuffer bytesForAllSamples];
     frame.size = size;
Index: /pjproject/trunk/pjmedia/src/pjmedia-videodev/v4l2_dev.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia-videodev/v4l2_dev.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia-videodev/v4l2_dev.c	(revision 3901)
@@ -691,4 +691,5 @@
 
     frame->type = PJMEDIA_FRAME_TYPE_VIDEO;
+    frame->bit_info = 0;
     frame->size = buf.bytesused;
     frame->timestamp.u64 = PJ_UINT64(1) * PJ_TIME_VAL_MSEC(time) *
Index: /pjproject/trunk/pjmedia/src/pjmedia/errno.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/errno.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia/errno.c	(revision 3901)
@@ -105,4 +105,5 @@
     PJ_BUILD_ERR( PJMEDIA_CODEC_EPCMFRMINLEN,   "Invalid PCM frame length" ),
     PJ_BUILD_ERR( PJMEDIA_CODEC_EINMODE,	"Invalid codec mode (no fmtp?)" ),
+    PJ_BUILD_ERR( PJMEDIA_CODEC_EBADBITSTREAM,	"Bad or corrupted bitstream" ),
 
     /* Media errors. */
Index: /pjproject/trunk/pjmedia/src/pjmedia/vid_codec.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/vid_codec.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia/vid_codec.c	(revision 3901)
@@ -71,12 +71,4 @@
 static void sort_codecs(pjmedia_vid_codec_mgr *mgr);
 
-/*
- * Initialize pjmedia_vid_codec structure with default values.
- */
-PJ_DEF(void) pjmedia_vid_codec_reset(pjmedia_vid_codec *codec,
-                                    pjmedia_obj_sig sig)
-{
-    pj_bzero(codec, sizeof(*codec));
-}
 
 /*
Index: /pjproject/trunk/pjmedia/src/pjmedia/vid_stream.c
===================================================================
--- /pjproject/trunk/pjmedia/src/pjmedia/vid_stream.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/pjmedia/vid_stream.c	(revision 3901)
@@ -69,4 +69,5 @@
 #endif
 
+
 /**
  * Media channel.
@@ -124,4 +125,7 @@
     pjmedia_event            fmt_event;	    /**< Buffered fmt_changed event
                                                  to avoid deadlock	    */
+    pjmedia_event            miss_keyframe_event; 
+					    /**< Buffered missing keyframe
+                                                 event for delayed republish*/
 
     unsigned		     frame_size;    /**< Size of encoded base frame.*/
@@ -131,4 +135,6 @@
     pjmedia_frame	    *rx_frames;	    /**< Temp. buffer for incoming
 					         frame assembly.	    */
+
+    pj_bool_t		     force_keyframe;/**< Forced to encode keyframe? */
 
 #if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
@@ -352,4 +358,10 @@
 	    pj_memcpy(&stream->fmt_event, event, sizeof(*event));
 	    return PJ_SUCCESS;
+
+	case PJMEDIA_EVENT_KEYFRAME_MISSING:
+	    /* Republish this event later from get_frame(). */
+	    pj_memcpy(&stream->miss_keyframe_event, event, sizeof(*event));
+	    return PJ_SUCCESS;
+
 	default:
 	    break;
@@ -764,5 +776,5 @@
     pj_bool_t has_more_data = PJ_FALSE;
     pj_size_t total_sent = 0;
-
+    pjmedia_vid_encode_opt enc_opt;
 
 #if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA != 0
@@ -797,6 +809,16 @@
     frame_out.size = 0;
 
+    /* Init encoding option */
+    pj_bzero(&enc_opt, sizeof(enc_opt));
+    if (stream->force_keyframe) {
+	/* Force encoder to generate keyframe */
+	enc_opt.force_keyframe = PJ_TRUE;
+	stream->force_keyframe = PJ_FALSE;
+	TRC_((channel->port.info.name.ptr,
+	      "Forcing encoder to generate keyframe"));
+    }
+
     /* Encode! */
-    status = pjmedia_vid_codec_encode_begin(stream->codec, frame,
+    status = pjmedia_vid_codec_encode_begin(stream->codec, &enc_opt, frame,
                                             channel->buf_size -
                                                sizeof(pjmedia_rtp_hdr),
@@ -1080,4 +1102,10 @@
     }
 
+    if (stream->miss_keyframe_event.type != PJMEDIA_EVENT_NONE) {
+	pjmedia_event_publish(NULL, port, &stream->miss_keyframe_event,
+			      PJMEDIA_EVENT_PUBLISH_POST_EVENT);
+	stream->miss_keyframe_event.type = PJMEDIA_EVENT_NONE;
+    }
+
     pj_mutex_lock( stream->jb_mutex );
 
@@ -1425,6 +1453,6 @@
 
     /* Set up jitter buffer */
-    pjmedia_jbuf_set_adaptive( stream->jb, jb_init, jb_min_pre, jb_max_pre);
-    //pjmedia_jbuf_enable_discard(stream->jb, PJ_FALSE);
+    pjmedia_jbuf_set_adaptive(stream->jb, jb_init, jb_min_pre, jb_max_pre);
+    pjmedia_jbuf_set_discard(stream->jb, PJMEDIA_JB_DISCARD_NONE);
 
     /* Init RTCP session: */
@@ -2091,3 +2119,21 @@
 }
 
+
+/*
+ * Force stream to send video keyframe.
+ */
+PJ_DEF(pj_status_t) pjmedia_vid_stream_send_keyframe(
+						pjmedia_vid_stream *stream)
+{
+    PJ_ASSERT_RETURN(stream, PJ_EINVAL);
+
+    if (!pjmedia_vid_stream_is_running(stream, PJMEDIA_DIR_ENCODING))
+	return PJ_EINVALIDOP;
+
+    stream->force_keyframe = PJ_TRUE;
+
+    return PJ_SUCCESS;
+}
+
+
 #endif /* PJMEDIA_HAS_VIDEO */
Index: /pjproject/trunk/pjmedia/src/test/vid_codec_test.c
===================================================================
--- /pjproject/trunk/pjmedia/src/test/vid_codec_test.c	(revision 3900)
+++ /pjproject/trunk/pjmedia/src/test/vid_codec_test.c	(revision 3901)
@@ -95,5 +95,5 @@
     enc_frames[enc_cnt].size = enc_size_left;
 
-    status = pjmedia_vid_codec_encode_begin(codec, frame, enc_size_left,
+    status = pjmedia_vid_codec_encode_begin(codec, NULL, frame, enc_size_left,
                                             &enc_frames[enc_cnt], &has_more);
     if (status != PJ_SUCCESS) goto on_error;
Index: /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
===================================================================
--- /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 3900)
+++ /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 3901)
@@ -2634,5 +2634,28 @@
 	 * Handle INFO method.
 	 */
-	if (tsx->role == PJSIP_ROLE_UAC && 
+	const pj_str_t STR_APPLICATION = { "application", 11};
+	const pj_str_t STR_DTMF_RELAY  = { "dtmf-relay", 10 };
+	pjsip_msg_body *body = NULL;
+	pj_bool_t dtmf_info = PJ_FALSE;
+	
+	if (tsx->role == PJSIP_ROLE_UAC) {
+	    if (e->body.tsx_state.type == PJSIP_EVENT_TX_MSG)
+		body = e->body.tsx_state.src.tdata->msg->body;
+	    else
+		body = e->body.tsx_state.tsx->last_tx->msg->body;
+	} else {
+	    if (e->body.tsx_state.type == PJSIP_EVENT_RX_MSG)
+		body = e->body.tsx_state.src.rdata->msg_info.msg->body;
+	}
+	
+	/* Check DTMF content in the INFO message */
+	if (body && body->len &&
+	    pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
+	    pj_stricmp(&body->content_type.subtype, &STR_DTMF_RELAY)==0)
+	{
+	    dtmf_info = PJ_TRUE;
+	}
+
+	if (dtmf_info && tsx->role == PJSIP_ROLE_UAC && 
 	    (tsx->state == PJSIP_TSX_STATE_COMPLETED ||
 	       (tsx->state == PJSIP_TSX_STATE_TERMINATED &&
@@ -2652,5 +2675,5 @@
 			  tsx->status_text.ptr));
 	    }
-	} else if (tsx->role == PJSIP_ROLE_UAS &&
+	} else if (dtmf_info && tsx->role == PJSIP_ROLE_UAS &&
 		   tsx->state == PJSIP_TSX_STATE_TRYING)
 	{
@@ -2883,7 +2906,7 @@
 	if (vid_idx == -1 || call_info.media[vid_idx].dir == PJMEDIA_DIR_NONE) {
 	    PJ_LOG(3,(THIS_FILE,
-		      "Just rejected incoming video offer on call %d"
-		      "use \"vid call add\" to enable video!",
-		      call_id));
+		      "Just rejected incoming video offer on call %d, "
+		      "use \"vid call enable %d\" or \"vid call add\" to enable video!",
+		      call_id, vid_idx));
 	}
     }
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 3900)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 3901)
@@ -331,4 +331,15 @@
 #   define PJSUA_HAS_VIDEO		PJMEDIA_HAS_VIDEO
 #endif
+
+
+/**
+ * Interval between two keyframe requests, in milliseconds.
+ *
+ * Default: 500 ms
+ */
+#ifndef PJSUA_VID_REQ_KEYFRAME_INTERVAL
+#   define PJSUA_VID_REQ_KEYFRAME_INTERVAL	500
+#endif
+
 
 /**
@@ -3372,4 +3383,26 @@
 
 /**
+ * Enumeration of video keyframe request methods. Keyframe request is
+ * triggered by decoder, usually when the incoming video stream cannot
+ * be decoded properly due to missing video keyframe.
+ */
+typedef enum pjsua_vid_req_keyframe_method
+{
+    /**
+     * Requesting keyframe via SIP INFO message. Note that incoming keyframe
+     * request via SIP INFO will always be handled even if this flag is unset.
+     */
+    PJSUA_VID_REQ_KEYFRAME_SIP_INFO	= 1,
+
+    /**
+     * Requesting keyframe via Picture Loss Indication of RTCP feedback.
+     * This is currently not supported.
+     */
+    PJSUA_VID_REQ_KEYFRAME_RTCP_PLI	= 2
+
+} pjsua_vid_req_keyframe_method;
+
+
+/**
  * Call settings.
  */
@@ -3377,9 +3410,15 @@
 {
     /**
-     * Bitmask of pjsua_call_flag constants.
+     * Bitmask of #pjsua_call_flag constants.
      *
      * Default: 0
      */
     unsigned	     flag;
+
+    /**
+     * This flag controls what methods to request keyframe are allowed on
+     * the call. Value is bitmask of #pjsua_vid_req_keyframe_method.
+     */
+    unsigned	     req_keyframe_method;
 
     /**
@@ -3649,4 +3688,11 @@
      */
     PJSUA_CALL_VID_STRM_STOP_TRANSMIT,
+
+    /**
+     * Send keyframe in the video stream. This will force the stream to
+     * generate and send video keyframe as soon as possible. No
+     * re-INVITE/UPDATE is to be transmitted to remote with this operation.
+     */
+    PJSUA_CALL_VID_STRM_SEND_KEYFRAME
 
 } pjsua_call_vid_strm_op;
@@ -4664,4 +4710,9 @@
 extern const pjsip_method pjsip_message_method;
 
+
+/**
+ * The INFO method (defined in pjsua_call.c)
+ */
+extern const pjsip_method pjsip_info_method;
 
 
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 3900)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 3901)
@@ -85,4 +85,5 @@
 					    address)			    */
     pjmedia_srtp_use	 rem_srtp_use; /**< Remote's SRTP usage policy.	    */
+    pj_timestamp	 last_req_keyframe;/**< Last TX keyframe request.   */
 
     pjsua_med_tp_state_cb      med_init_cb;/**< Media transport
@@ -478,4 +479,7 @@
 } pjsua_im_data;
 
+pj_status_t pjsua_media_apply_xml_control(pjsua_call_id call_id,
+					  const pj_str_t *xml_st);
+
 
 /**
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 3900)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 3901)
@@ -35,4 +35,15 @@
 #define LOCK_CODEC_MAX_RETRY	     5
 
+
+/*
+ * The INFO method.
+ */
+const pjsip_method pjsip_info_method = 
+{
+    PJSIP_OTHER_METHOD,
+    { "INFO", 4 }
+};
+
+
 /* This callback receives notification from invite session when the
  * session state has changed.
@@ -501,9 +512,6 @@
 #if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0)
     opt->video_cnt = 1;
-    //{
-    //	unsigned i;
-    //	for (i = 0; i < PJ_ARRAY_SIZE(opt->vid_cap_dev); ++i)
-    //	    opt->vid_cap_dev[i] = PJMEDIA_VID_DEFAULT_CAPTURE_DEV;
-    //}
+    opt->req_keyframe_method = PJSUA_VID_REQ_KEYFRAME_SIP_INFO |
+			     PJSUA_VID_REQ_KEYFRAME_RTCP_PLI;
 #endif
 }
@@ -4191,4 +4199,39 @@
 		      call->index, tsx->status_code));
 	}
+    } else if (tsx->role==PJSIP_ROLE_UAS &&
+	tsx->state==PJSIP_TSX_STATE_TRYING &&
+	pjsip_method_cmp(&tsx->method, &pjsip_info_method)==0)
+    {
+	/*
+	 * Incoming INFO request for media control.
+	 */
+	const pj_str_t STR_APPLICATION	     = { "application", 11};
+	const pj_str_t STR_MEDIA_CONTROL_XML = { "media_control+xml", 17 };
+	pjsip_rx_data *rdata = e->body.tsx_state.src.rdata;
+	pjsip_msg_body *body = rdata->msg_info.msg->body;
+
+	if (body && body->len &&
+	    pj_stricmp(&body->content_type.type, &STR_APPLICATION)==0 &&
+	    pj_stricmp(&body->content_type.subtype, &STR_MEDIA_CONTROL_XML)==0)
+	{
+	    pjsip_tx_data *tdata;
+	    pj_str_t control_st;
+	    pj_status_t status;
+
+	    /* Apply and answer the INFO request */
+	    pj_strset(&control_st, (char*)body->data, body->len);
+	    status = pjsua_media_apply_xml_control(call->index, &control_st);
+	    if (status == PJ_SUCCESS) {
+		status = pjsip_endpt_create_response(tsx->endpt, rdata,
+						     200, NULL, &tdata);
+		if (status == PJ_SUCCESS)
+		    status = pjsip_tsx_send_msg(tsx, tdata);
+	    } else {
+		status = pjsip_endpt_create_response(tsx->endpt, rdata,
+						     400, NULL, &tdata);
+		if (status == PJ_SUCCESS)
+		    status = pjsip_tsx_send_msg(tsx, tdata);
+	    }
+	}
     }
 
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 3900)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 3901)
@@ -1259,4 +1259,46 @@
     pjsua_call_media *call_med = (pjsua_call_media*)user_data;
     pjsua_call *call = call_med->call;
+    pj_status_t status = PJ_SUCCESS;
+  
+    switch(event->type) {
+	case PJMEDIA_EVENT_KEYFRAME_MISSING:
+	    if (call->opt.req_keyframe_method & PJSUA_VID_REQ_KEYFRAME_SIP_INFO)
+	    {
+		pj_timestamp now;
+
+		pj_get_timestamp(&now);
+		if (pj_elapsed_msec(&call_med->last_req_keyframe, &now) >=
+		    PJSUA_VID_REQ_KEYFRAME_INTERVAL)
+		{
+		    pjsua_msg_data msg_data;
+		    const pj_str_t SIP_INFO = {"INFO", 4};
+		    const char *BODY_TYPE = "application/media_control+xml";
+		    const char *BODY =
+			"<?xml version=\"1.0\" encoding=\"utf-8\" ?>"
+			"<media_control><vc_primitive><to_encoder>"
+			"<picture_fast_update/>"
+			"</to_encoder></vc_primitive></media_control>";
+
+		    PJ_LOG(4,(THIS_FILE, 
+			      "Sending video keyframe request via SIP INFO"));
+
+		    pjsua_msg_data_init(&msg_data);
+		    pj_cstr(&msg_data.content_type, BODY_TYPE);
+		    pj_cstr(&msg_data.msg_body, BODY);
+		    status = pjsua_call_send_request(call->index, &SIP_INFO, 
+						     &msg_data);
+		    if (status != PJ_SUCCESS) {
+			pj_perror(3, THIS_FILE, status,
+				  "Failed requesting keyframe via SIP INFO");
+		    } else {
+			call_med->last_req_keyframe = now;
+		    }
+		}
+	    }
+	    break;
+
+	default:
+	    break;
+    }
 
     if (pjsua_var.ua_cfg.cb.on_call_media_event && call) {
@@ -1265,5 +1307,5 @@
     }
 
-    return PJ_SUCCESS;
+    return status;
 }
 
@@ -4188,2 +4230,32 @@
 
 
+pj_status_t pjsua_media_apply_xml_control(pjsua_call_id call_id,
+					  const pj_str_t *xml_st)
+{
+    pjsua_call *call = &pjsua_var.calls[call_id];
+    const pj_str_t PICT_FAST_UPDATE = {"picture_fast_update", 19};
+
+#if PJMEDIA_HAS_VIDEO
+    if (pj_strstr(xml_st, &PICT_FAST_UPDATE)) {
+	unsigned i;
+
+	PJ_LOG(4,(THIS_FILE, "Received keyframe request via SIP INFO"));
+
+	for (i = 0; i < call->med_cnt; ++i) {
+	    pjsua_call_media *cm = &call->media[i];
+	    if (cm->type != PJMEDIA_TYPE_VIDEO || !cm->strm.v.stream)
+		continue;
+
+	    pjmedia_vid_stream_send_keyframe(cm->strm.v.stream);
+	}
+
+	return PJ_SUCCESS;
+    }
+#endif
+
+    /* Just to avoid compiler warning of unused var */
+    PJ_UNUSED_ARG(xml_st);
+
+    return PJ_ENOTSUP;
+}
+
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_vid.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_vid.c	(revision 3900)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_vid.c	(revision 3901)
@@ -2001,4 +2001,30 @@
 
 
+static pj_status_t call_send_vid_keyframe(pjsua_call *call,
+					  int med_idx)
+{
+    pjsua_call_media *call_med;
+
+    /* Verify and normalize media index */
+    if (med_idx == -1) {
+	int first_active;
+	
+	call_get_vid_strm_info(call, &first_active, NULL, NULL, NULL);
+	if (first_active == -1)
+	    return PJ_ENOTFOUND;
+
+	med_idx = first_active;
+    }
+
+    call_med = &call->media[med_idx];
+
+    /* Verify media type and stream instance. */
+    if (call_med->type != PJMEDIA_TYPE_VIDEO || !call_med->strm.v.stream)
+	return PJ_EINVAL;
+
+    return pjmedia_vid_stream_send_keyframe(call_med->strm.v.stream);
+}
+
+
 /*
  * Start, stop, and/or manipulate video transmission for the specified call.
@@ -2070,4 +2096,7 @@
 	status = call_set_tx_video(call, param_.med_idx, PJ_FALSE);
 	break;
+    case PJSUA_CALL_VID_STRM_SEND_KEYFRAME:
+	status = call_send_vid_keyframe(call, param_.med_idx);
+	break;
     default:
 	status = PJ_EINVALIDOP;
