Index: pjproject/trunk/pjmedia/src/pjmedia-videodev/sdl_dev.c
===================================================================
--- pjproject/trunk/pjmedia/src/pjmedia-videodev/sdl_dev.c	(revision 4722)
+++ pjproject/trunk/pjmedia/src/pjmedia-videodev/sdl_dev.c	(revision 5392)
@@ -635,5 +635,5 @@
 {
     struct sdl_stream *strm = (struct sdl_stream *)data;
-
+     
 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
     if (strm->texture) {
@@ -653,6 +653,5 @@
         SDL_DestroyRenderer(strm->renderer);
         strm->renderer = NULL;
-    }
-
+    }    
     return PJ_SUCCESS;
 }
@@ -660,5 +659,5 @@
 static pj_status_t sdl_destroy_all(void *data)
 {
-    struct sdl_stream *strm = (struct sdl_stream *)data;
+    struct sdl_stream *strm = (struct sdl_stream *)data;  
 
     sdl_destroy(data);
@@ -671,46 +670,12 @@
     strm->window = NULL;
 #endif /* TARGET_OS_IPHONE */
-
-    return PJ_SUCCESS;
-}
-
-static pj_status_t sdl_create_rend(struct sdl_stream * strm,
-                                   pjmedia_format *fmt)
-{
-    sdl_fmt_info *sdl_info;
-    const pjmedia_video_format_info *vfi;
-    pjmedia_video_format_detail *vfd;
-
-    sdl_info = get_sdl_format_info(fmt->id);
-    vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(),
-                                        fmt->id);
-    if (!vfi || !sdl_info)
-        return PJMEDIA_EVID_BADFORMAT;
-
-    strm->vafp.size = fmt->det.vid.size;
-    strm->vafp.buffer = NULL;
-    if (vfi->apply_fmt(vfi, &strm->vafp) != PJ_SUCCESS)
-        return PJMEDIA_EVID_BADFORMAT;
-
-    vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE);
-    strm->rect.x = strm->rect.y = 0;
-    strm->rect.w = (Uint16)vfd->size.w;
-    strm->rect.h = (Uint16)vfd->size.h;
-    if (strm->param.disp_size.w == 0)
-        strm->param.disp_size.w = strm->rect.w;
-    if (strm->param.disp_size.h == 0)
-        strm->param.disp_size.h = strm->rect.h;
-    strm->dstrect.x = strm->dstrect.y = 0;
-    strm->dstrect.w = (Uint16)strm->param.disp_size.w;
-    strm->dstrect.h = (Uint16)strm->param.disp_size.h;
-
-    sdl_destroy(strm);
-
-#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
-    if (strm->param.rend_id == OPENGL_DEV_IDX) {
-	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
-    }
-#endif /* PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL */
-
+    return PJ_SUCCESS;
+}
+
+static pj_status_t sdl_create_window(struct sdl_stream *strm, 
+				     pj_bool_t use_app_win,
+				     Uint32 sdl_format,
+				     pjmedia_vid_dev_hwnd *hwnd)
+{
     if (!strm->window) {
         Uint32 flags = 0;
@@ -737,10 +702,8 @@
         if (strm->param.rend_id == OPENGL_DEV_IDX)
             flags |= SDL_WINDOW_OPENGL;
-#endif /* PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL */
-
-        if (strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) {
-            /* Use the window supplied by the application. */
-	    strm->window = SDL_CreateWindowFrom(
-                               strm->param.window.info.window);
+#endif /* PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL */	
+	if (use_app_win) {
+            /* Use the window supplied by the application. */	    
+	    strm->window = SDL_CreateWindowFrom(hwnd->info.window);
 	    if (!strm->window) {
 		sdl_log_err("SDL_CreateWindowFrom()");
@@ -813,5 +776,5 @@
 #endif /* PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL */
     {    
-        strm->scr_tex = SDL_CreateTexture(strm->renderer, sdl_info->sdl_format,
+        strm->scr_tex = SDL_CreateTexture(strm->renderer, sdl_format,
                                           SDL_TEXTUREACCESS_STREAMING,
                                           strm->rect.w, strm->rect.h);
@@ -821,8 +784,51 @@
         }
     
-        strm->pitch = strm->rect.w * SDL_BYTESPERPIXEL(sdl_info->sdl_format);
-    }
-
-    return PJ_SUCCESS;
+        strm->pitch = strm->rect.w * SDL_BYTESPERPIXEL(sdl_format);
+    }
+
+    return PJ_SUCCESS;
+}
+
+static pj_status_t sdl_create_rend(struct sdl_stream * strm,
+                                   pjmedia_format *fmt)
+{
+    sdl_fmt_info *sdl_info;
+    const pjmedia_video_format_info *vfi;
+    pjmedia_video_format_detail *vfd;
+
+    sdl_info = get_sdl_format_info(fmt->id);
+    vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(),
+                                        fmt->id);
+    if (!vfi || !sdl_info)
+        return PJMEDIA_EVID_BADFORMAT;
+
+    strm->vafp.size = fmt->det.vid.size;
+    strm->vafp.buffer = NULL;
+    if (vfi->apply_fmt(vfi, &strm->vafp) != PJ_SUCCESS)
+        return PJMEDIA_EVID_BADFORMAT;
+
+    vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE);
+    strm->rect.x = strm->rect.y = 0;
+    strm->rect.w = (Uint16)vfd->size.w;
+    strm->rect.h = (Uint16)vfd->size.h;
+    if (strm->param.disp_size.w == 0)
+        strm->param.disp_size.w = strm->rect.w;
+    if (strm->param.disp_size.h == 0)
+        strm->param.disp_size.h = strm->rect.h;
+    strm->dstrect.x = strm->dstrect.y = 0;
+    strm->dstrect.w = (Uint16)strm->param.disp_size.w;
+    strm->dstrect.h = (Uint16)strm->param.disp_size.h;
+
+    sdl_destroy(strm);
+
+#if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL
+    if (strm->param.rend_id == OPENGL_DEV_IDX) {
+	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
+    }
+#endif /* PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL */
+    return sdl_create_window(strm, 
+			 (strm->param.flags & PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW),
+			 sdl_info->sdl_format,
+			 &strm->param.window);
 }
 
@@ -1166,4 +1172,18 @@
 	SDL_SetWindowSize(strm->window, new_size->w, new_size->h);
         return resize_disp(strm, new_size);
+    } else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_WINDOW) {
+	pjmedia_vid_dev_hwnd *hwnd = (pjmedia_vid_dev_hwnd*)pval;
+	pj_status_t status = PJ_SUCCESS;
+	sdl_fmt_info *sdl_info = get_sdl_format_info(strm->param.fmt.id);
+	/* Re-init SDL */
+	status = sdl_destroy_all(strm);
+	if (status != PJ_SUCCESS)
+	    return status;	
+
+	status = sdl_create_window(strm, PJ_TRUE, sdl_info->sdl_format, hwnd);
+        PJ_LOG(4, (THIS_FILE, "Re-initializing SDL with native window"
+        		      " %d: %s", hwnd->info.window,
+                              (status == PJ_SUCCESS? "success": "failed")));
+	return status;	
     }
 
