Chromium實現供JavaScript調用方法

Chromium是支持Video對象的,這個是HTML5的新特性,如今爲了在JavaScript中能夠調用到底層的一些特性,需要給其添加一個新的方法,即: 如何使JavaScript能夠調用到Chromium底層的一些特性。

首先看一下Video Play方法的調用堆棧,



通過調用堆棧我們可以看到,Play方法最初是應該屬於HTMLMediaElement,這個也是HTML5的一個元素,Video繼承至它。

我們再來看看相關一些類的關係,

這樣就更清晰了,通過調查可以得知,我們只有在HTMLMediaElement中定義相應的接口,然後一路調用下去,最後就可以調用到WebMediaPlayerImpl,這個類具體實現Player的相應功能,這個類中可以調用到PipeLine,這樣我們就可以調用我們任意想調用的東西了,例如: demux,decoder,render等。要想把相應的接口Export到JavaScript層,在HTMLMediaElement.idl中定義是關鍵,其他的事情框架就給我做了。下面講一個小例子。


----------------------------------------------------Sample--------------------------------------------

We implement a specialcall interface on HTMLMediaElementand it call WebMediaPlayerImpl’s function finally.

First we prepare a sample.html to triggerthe call as below:

 

<!DOCTYPE HTML>

<html>

<head>

<script>

function playOrPause() {

  var video = document.getElementById('video');

  video.specialcall(); //this is my custom call

  //video.play();

}

</script>

</head>

<body>

<!-- <video src="bear.ogv" width="320" height="240" controls="controls" autoplay="autoplay"> -->

<video id="video" src="car.ts" width="320" height="240">

   your browser dose not support the video tag.

</video>

<input type="button" value="Play" id="playpause" οnclick="playOrPause()" />

</body>

</html>

 
 

Second, we add implemented code in Chromiumproject as below:

1, Add specialcall function inHTMLMediaElement class

voidHTMLMediaElement::specialcall() {

 printf("Good , called my function in HTMLMediaElement! \n");

 if(m_player)

   m_player->myspecialcall();

 else

   printf("can't get m_player in HTMLMediaElement \n");

}

 

2, Export specialcall funciton inHTMLMediaElement.idl file

   void specialcall();

3, Add specialcall in MediaPlayer class

void MediaPlayer::specialcall(){

 printf("myspecialcall is called in MediaPlayer class \n");

 m_private->myspecialcall();

}

4, Add a virtual function in WebMediaPlayerclass in WebMediaPlayer.h

   virtual void myspecialcall() { }

5, Add a virtual function in MediaPlayerPrivateclass in WebMediaPlayer.h

   virtual void specialcall() { }

6, Add a virtual function in WebMediaPlayerClientImplclass

void WebMediaPlayerClientImpl::specialcall(){

 printf("specialcall is called in WebMediaPlayerClientImpl class\n");

 if (m_webMediaPlayer.get())

    m_webMediaPlayer->specialcall();

 else

    printf("can't get web media player at this moment \n");

}

7, Add a virtual function in WebMediaPlayerImpl class

voidWebMediaPlayerImpl::specialcall(){

 printf("myspecialcall is called in WebMediaPlayerImpl class\n");

 printf("Here,i think it's enough,you can get what you want becauseyou can get pipeline here \n");

}

 Third, build theChromium project and run Chrome and load sample.html, click pay button afterload. The result is as below:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章