Pushing HTML5 Video Content over ColdFusion WebSockets(转)

I’ve been playing with the WebSocket feature introduced in ColdFusion 10 for some time now. I was trying out pushing images over a ColdFusion WebSocket channel and it worked just fine. But this time I wanted to put WebSockets to test and wanted to push large data at regular intervals. I thought maybe I can push video data over WebSockets and it turned out that there is no direct way to stream video data to many clients. I came across the function - drawImage that can be used to draw an Image or Video on a HTML5 Canvas. Once an image is drawn on the Canvas, it’s base64 encoded data can be obtained by calling the toDataURL function on the Canvas object.  This data can then be transferred over a ColdFusion WebSocket to all subscribers who can then use this  data to draw the image(video frame) on a Canvas.

Please note, I’m not transferring the audio track present in the Video and I’m still trying to figure how that can be achieved.

Here’s the Publisher code:

 1 <!DOCTYPE html> 
 2 <html>
 3     <body> 
 4     <cfwebsocket name="socket" onmessage="messageHandler"> 
 5     <video id="videoElement" controls muted> 
 6         <source src="windowsill.webm" type="video/webm"> </video>
 7     
 8  
 9     <canvas id="canvasElement" style="border: solid 1px;"> 
10     </canvas> 
11     
12     <script type="text/javascript"> 
13       var context,canvasElement,videoElement, previous, current; 
14 
15       //message handler for CF WebSocket 
16       messageHandler = function(msg){ 
17       } 
18 
19       //function to call once the DOM content has been loaded 
20       document.addEventListener('DOMContentLoaded', function(){ 
21         videoElement = document.getElementById('videoElement'); 
22         canvasElement = document.getElementById('canvasElement'); 
23         context = canvasElement.getContext('2d'); 
24       }); 
25         
26       //function to call once the videos meta data is available 
27       document.getElementById('videoElement').addEventListener('loadedmetadata', function(){ 
28          
29          //set the canvas width and height to videos width and height 
30           canvasElement.width = videoElement.videoWidth; 
31           canvasElement.height = videoElement.videoHeight; 
32           
33           //event listener when the video is played 
34           videoElement.addEventListener('play', function(){
35             //call the draw function 
36             draw(this, videoElement.videoWidth, videoElement.videoHeight); 
37 
38           }); 
39         }); 
40 
41         //function to draw the video frame on a temporary canvas at 20fps 
42         function draw(video, width, height){ 
43           
44           //if the video has been paused or ended return false 
45           if (video.paused || video.ended) return false; 
46           
47           //draw the current video frame onto a canvas 
48           context.drawImage(video, 0, 0, width, height); 
49           
50           //get base64 encoded data from Canvas 
51           current = canvasElement.toDataURL("image/png"); 
52 
53           //just in case if the previous frame is same as current 
54           if (previous != current) {
55            
56            //transfer the base64 encode image over a WebSocket 
57            socket.publish("myChannel", current); 
58 
59          } 
60 
61           previous = current; //draw the video frame on the canvas at 20fps by calling the draw function every 50ms setTimeout(draw, 50, video, width, height); 
62         } 
63       </script> 
64     </body>
65 </html>

As you can see from the above code, once you start playing the video the draw function is called. Here I've drawn the video on a Canvas using the drawImage function and then used the function toDataURL to get the base64 encoded data of the image. This is then transferred over a ColdFusion WebSocket channel (‘myChannel’). I’m calling this function (‘draw’) every 50ms to draw the current video frame on the canvas (to achieve 20fps) and transfer the image over a WebSocket.

The client\subscriber on receiving the data, draws  the image (video frame) on a canvas. Here’s the subscriber code:

 1 <!DOCTYPE HTML> 
 2 <html> 
 3   <body> 
 4       <cfwebsocket name="socket" onmessage="messageHandler" onopen="openHandler"> 
 5       <canvas id="canvasElement" style="border: solid 1px;" width="426" height="240"> 
 6       </canvas> 
 7   </body> 
 8   <script type="text/javascript"> 
 9     var canvas, context, count = 0, flag = false; 
10     var newImage = new Image(); 
11     document.addEventListener('DOMContentLoaded', function(){ 
12       canvas = document.getElementById('canvasElement'); 
13       context = canvas.getContext('2d'); 
14     }); 
15 
16     function openHandler(){ 
17       
18       //subscribe to the CF WebSocket channel 
19       socket.subscribe("myChannel", {}, dataHandler); 
20     } 
21     
22     function messageHandler(msg){ 
23     } 
24 
25     //function that receives the data from the WebSocket channel 
26     function dataHandler(msg){ 
27       if (msg.type == 'data') { 
28         
29         //function to call when the image is loaded with base64 data 
30         newImage.onload = function(){ 
31           
32           //draw the image on the canvas 
33           context.drawImage(newImage, 0, 0); 
34           
35           //set the flags when the above function is complete 
36           flag = true; count = 1; 
37 
38         } 
39 
40         //if ready to be drawn on the canvas 
41         if (count == 0 || flag == true) { 
42           flag = false; 
43 
44           //assign base64 data to the source of the image 
45           newImage.src = msg.data; 
46 
47         } 
48       } 
49     } 
50   </script> 
51 </html>

On the client side, once the data is received over the WebSocket it is assigned to the source of an Image object. The reason why I do this is because the drawImage function takes either an Image or a Video as it's first argument and doesn't allow base64 data. Once the Image is loaded, it is ready to be drawn on the canvas. This process continues until the video ends or the user pauses the video.

posted @ 2012-12-06 23:27  独孤皇帝  阅读(146)  评论(0)    收藏  举报