Fix for bug 711628 (Implement PeerConnection.localStreams/remoteStreams). r=bz.

--HG--
extra : rebase_source : e21a429d85fc60a972752a2a7deb88a7cb648f3c
This commit is contained in:
Peter Van der Beken 2012-09-13 18:04:31 +02:00
parent 9baaa8cb1b
commit 767c08f204
11 changed files with 231 additions and 5 deletions

View File

@ -247,6 +247,14 @@ DOMInterfaces = {
'workers': True,
}],
'MediaStreamList': {
'headerFile': 'MediaStreamList.h',
'wrapperCache': False,
'nativeOwnership': 'owned',
'resultNotAddRefed': [ '__indexedGetter' ],
'binaryNames': { '__indexedGetter': 'IndexedGetter' }
},
'MozChannel': [
{
'nativeType': 'nsIChannel',
@ -608,6 +616,7 @@ addExternalIface('File')
addExternalIface('HitRegionOptions', nativeType='nsISupports')
addExternalIface('HTMLElement')
addExternalIface('ImageData', nativeType='mozilla::dom::ImageData')
addExternalIface('MediaStream')
addExternalIface('Node', nativeType='nsINode')
addExternalIface('PaintRequest')
addExternalIface('SVGLength')

View File

@ -72,7 +72,8 @@ EXPORTS_$(binding_include_path) = \
LOCAL_INCLUDES += -I$(topsrcdir)/js/xpconnect/src \
-I$(topsrcdir)/js/xpconnect/wrappers \
-I$(topsrcdir)/content/canvas/src \
-I$(topsrcdir)/content/html/content/src
-I$(topsrcdir)/content/html/content/src \
-I$(topsrcdir)/media/webrtc/signaling/src/peerconnection
include $(topsrcdir)/config/rules.mk

View File

@ -404,6 +404,13 @@ PeerConnection.prototype = {
});
},
get localStreams() {
return this._pc.localStreams;
},
get remoteStreams() {
return this._pc.remoteStreams;
},
createDataChannel: function(label, dict) {
if (dict &&
dict.maxRetransmitTime != undefined &&

View File

@ -83,6 +83,9 @@ interface IPeerConnection : nsISupports
void removeStream(in nsIDOMMediaStream stream);
void closeStreams();
[implicit_jscontext] readonly attribute jsval localStreams; // MediaStream[]
[implicit_jscontext] readonly attribute jsval remoteStreams; // MediaStream[]
/* As the ICE candidates roll in this one should be called each time
* in order to keep the candidate list up-to-date for the next SDP-related
* call PeerConnectionImpl does not parse ICE candidates, just sticks them

View File

@ -0,0 +1,13 @@
/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
interface MediaStream;
[NoInterfaceObject]
interface MediaStreamList {
getter MediaStream? (unsigned long index);
readonly attribute unsigned long length;
};

View File

@ -54,6 +54,12 @@ webidl_files += \
$(NULL)
endif
ifdef MOZ_WEBRTC
webidl_files += \
MediaStreamList.webidl \
$(NULL)
endif
ifdef ENABLE_TESTS
test_webidl_files := \
TestCodeGen.webidl \

View File

@ -128,6 +128,8 @@
'./src/softphonewrapper/CC_SIPCCLineInfo.h',
'./src/softphonewrapper/CC_SIPCCService.h',
# PeerConnection
'./src/peerconnection/MediaStreamList.cpp',
'./src/peerconnection/MediaStreamList.h',
'./src/peerconnection/PeerConnectionCtx.cpp',
'./src/peerconnection/PeerConnectionCtx.h',
'./src/peerconnection/PeerConnectionImpl.cpp',

View File

@ -0,0 +1,83 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "base/basictypes.h"
#include "MediaStreamList.h"
#ifdef MOZILLA_INTERNAL_API
#include "mozilla/dom/MediaStreamListBinding.h"
#endif
#include "nsIScriptGlobalObject.h"
#include "PeerConnectionImpl.h"
namespace mozilla {
namespace dom {
MediaStreamList::MediaStreamList(sipcc::PeerConnectionImpl* peerConnection,
StreamType type)
: mPeerConnection(peerConnection),
mType(type)
{
MOZ_COUNT_CTOR(mozilla::dom::MediaStreamList);
}
MediaStreamList::~MediaStreamList()
{
MOZ_COUNT_DTOR(mozilla::dom::MediaStreamList);
}
JSObject*
MediaStreamList::WrapObject(JSContext* cx, ErrorResult& error)
{
#ifdef MOZILLA_INTERNAL_API
nsCOMPtr<nsIScriptGlobalObject> global =
do_QueryInterface(mPeerConnection->GetWindow());
JSObject* scope = global->GetGlobalJSObject();
if (!scope) {
error.Throw(NS_ERROR_FAILURE);
return nullptr;
}
JSAutoCompartment ac(cx, scope);
JSObject* obj = MediaStreamListBinding::Wrap(cx, scope, this);
if (!obj) {
error.Throw(NS_ERROR_FAILURE);
}
return obj;
#else
return nullptr;
#endif
}
template<class T>
static nsIDOMMediaStream*
GetStreamFromInfo(T* info, bool& found)
{
if (!info) {
found = false;
return nullptr;
}
found = true;
return info->GetMediaStream();
}
nsIDOMMediaStream*
MediaStreamList::IndexedGetter(uint32_t index, bool& found)
{
if (mType == Local) {
return GetStreamFromInfo(mPeerConnection->GetLocalStream(index), found);
}
return GetStreamFromInfo(mPeerConnection->GetRemoteStream(index), found);
}
uint32_t
MediaStreamList::Length()
{
return mType == Local ? mPeerConnection->LocalStreamsLength() :
mPeerConnection->RemoteStreamsLength();
}
} // namespace dom
} // namespace mozilla

View File

@ -0,0 +1,46 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef MediaStreamList_h__
#define MediaStreamList_h__
#include "mozilla/ErrorResult.h"
#include "nsISupportsImpl.h"
#include "nsAutoPtr.h"
#include "jspubtd.h"
#include "mozilla/dom/NonRefcountedDOMObject.h"
class nsIDOMMediaStream;
namespace sipcc {
class PeerConnectionImpl;
} // namespace sipcc
namespace mozilla {
namespace dom {
class MediaStreamList : public NonRefcountedDOMObject
{
public:
enum StreamType {
Local,
Remote
};
MediaStreamList(sipcc::PeerConnectionImpl* peerConnection, StreamType type);
~MediaStreamList();
JSObject* WrapObject(JSContext* cx, ErrorResult& error);
nsIDOMMediaStream* IndexedGetter(uint32_t index, bool& found);
uint32_t Length();
private:
nsRefPtr<sipcc::PeerConnectionImpl> mPeerConnection;
StreamType mType;
};
} // namespace dom
} // namespace mozilla
#endif // MediaStreamList_h__

View File

@ -31,6 +31,11 @@
#include "nsPIDOMWindow.h"
#include "nsDOMDataChannel.h"
#ifdef MOZILLA_INTERNAL_API
#include "MediaStreamList.h"
#include "nsIScriptGlobalObject.h"
#include "jsapi.h"
#endif
#ifndef USE_FAKE_MEDIA_STREAMS
#include "MediaSegment.h"
@ -1130,7 +1135,7 @@ PeerConnectionImpl::IceStreamReady(NrIceMediaStream *aStream)
CSFLogDebugS(logTag, __FUNCTION__ << ": " << aStream->name().c_str());
}
nsRefPtr<LocalSourceStreamInfo>
LocalSourceStreamInfo*
PeerConnectionImpl::GetLocalStream(int aIndex)
{
if(aIndex < 0 || aIndex >= (int) mLocalSourceStreams.Length()) {
@ -1141,7 +1146,7 @@ PeerConnectionImpl::GetLocalStream(int aIndex)
return mLocalSourceStreams[aIndex];
}
nsRefPtr<RemoteSourceStreamInfo>
RemoteSourceStreamInfo*
PeerConnectionImpl::GetRemoteStream(int aIndex)
{
if(aIndex < 0 || aIndex >= (int) mRemoteSourceStreams.Length()) {
@ -1165,6 +1170,47 @@ PeerConnectionImpl::AddRemoteStream(nsRefPtr<RemoteSourceStreamInfo> aInfo,
return NS_OK;
}
#ifdef MOZILLA_INTERNAL_API
static nsresult
GetStreams(JSContext* cx, PeerConnectionImpl* peerConnection,
MediaStreamList::StreamType type, JS::Value* streams)
{
nsAutoPtr<MediaStreamList> list(new MediaStreamList(peerConnection, type));
ErrorResult rv;
JSObject* obj = list->WrapObject(cx, rv);
if (rv.Failed()) {
streams->setNull();
return rv.ErrorCode();
}
// Transfer ownership to the binding.
streams->setObject(*obj);
list.forget();
return NS_OK;
}
#endif
NS_IMETHODIMP
PeerConnectionImpl::GetLocalStreams(JSContext* cx, JS::Value* streams)
{
#ifdef MOZILLA_INTERNAL_API
return GetStreams(cx, this, MediaStreamList::Local, streams);
#else
return NS_ERROR_FAILURE;
#endif
}
NS_IMETHODIMP
PeerConnectionImpl::GetRemoteStreams(JSContext* cx, JS::Value* streams)
{
#ifdef MOZILLA_INTERNAL_API
return GetStreams(cx, this, MediaStreamList::Remote, streams);
#else
return NS_ERROR_FAILURE;
#endif
}
void
LocalSourceStreamInfo::StorePipeline(int aTrack,
mozilla::RefPtr<mozilla::MediaPipeline> aPipeline)

View File

@ -329,10 +329,18 @@ public:
}
// Get a specific local stream
nsRefPtr<LocalSourceStreamInfo> GetLocalStream(int aIndex);
uint32_t LocalStreamsLength()
{
return mLocalSourceStreams.Length();
}
LocalSourceStreamInfo* GetLocalStream(int index);
// Get a specific remote stream
nsRefPtr<RemoteSourceStreamInfo> GetRemoteStream(int aIndex);
uint32_t RemoteStreamsLength()
{
return mRemoteSourceStreams.Length();
}
RemoteSourceStreamInfo* GetRemoteStream(int index);
// Add a remote stream. Returns the index in index
nsresult AddRemoteStream(nsRefPtr<RemoteSourceStreamInfo> aInfo, int *aIndex);
@ -370,6 +378,8 @@ public:
// Create a fake media stream
nsresult CreateFakeMediaStream(uint32_t hint, nsIDOMMediaStream** retval);
nsPIDOMWindow* GetWindow() const { return mWindow; }
private:
PeerConnectionImpl(const PeerConnectionImpl&rhs);
PeerConnectionImpl& operator=(PeerConnectionImpl);