您的位置:首页 > Web前端 > JavaScript

Chromium实现供JavaScript调用方法

2012-01-14 20:41 239 查看
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" onclick="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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: