From 317b5386b4dd309ee6abb1aa95d217fd67353b53 Mon Sep 17 00:00:00 2001 From: Daosheng Mu Date: Thu, 18 Jun 2015 00:21:00 +0200 Subject: [PATCH] Bug 987498 - Part 3 - Implement DOMHwMediaStream. r=roc --- dom/media/DOMMediaStream.cpp | 77 ++++++++++++++++++++++++++++++++++++ dom/media/DOMMediaStream.h | 42 ++++++++++++++++++++ dom/tv/TVTuner.cpp | 4 +- 3 files changed, 122 insertions(+), 1 deletion(-) diff --git a/dom/media/DOMMediaStream.cpp b/dom/media/DOMMediaStream.cpp index c6face3d9e5b..275f3106d1ff 100644 --- a/dom/media/DOMMediaStream.cpp +++ b/dom/media/DOMMediaStream.cpp @@ -18,9 +18,13 @@ #include "MediaStreamGraph.h" #include "AudioStreamTrack.h" #include "VideoStreamTrack.h" +#include "Layers.h" using namespace mozilla; using namespace mozilla::dom; +using namespace mozilla::layers; + +const TrackID TRACK_VIDEO_PRIMARY = 1; class DOMMediaStream::StreamListener : public MediaStreamListener { public: @@ -668,3 +672,76 @@ DOMAudioNodeMediaStream::CreateTrackUnionStream(nsIDOMWindow* aWindow, return stream.forget(); } +DOMHwMediaStream::DOMHwMediaStream() +{ +#ifdef MOZ_WIDGET_GONK + mImageContainer = LayerManager::CreateImageContainer(ImageContainer::ASYNCHRONOUS_OVERLAY); + nsRefPtr img = mImageContainer->CreateImage(ImageFormat::OVERLAY_IMAGE); + mOverlayImage = static_cast(img.get()); + mImageContainer->SetCurrentImage(mOverlayImage); +#endif +} + +DOMHwMediaStream::~DOMHwMediaStream() +{ +} + +already_AddRefed +DOMHwMediaStream::CreateHwStream(nsIDOMWindow* aWindow) +{ + nsRefPtr stream = new DOMHwMediaStream(); + stream->InitSourceStream(aWindow); + stream->Init(stream->GetStream()); + + return stream.forget(); +} + +void +DOMHwMediaStream::Init(MediaStream* stream) +{ + SourceMediaStream* srcStream = stream->AsSourceStream(); + + if (srcStream) { + VideoSegment segment; +#ifdef MOZ_WIDGET_GONK + const StreamTime delta = STREAM_TIME_MAX; // Because MediaStreamGraph will run out frames in non-autoplay mode, + // we must give it bigger frame length to cover this situation. + mImageData.mOverlayId = DEFAULT_IMAGE_ID; + mImageData.mSize.width = DEFAULT_IMAGE_WIDTH; + mImageData.mSize.height = DEFAULT_IMAGE_HEIGHT; + mOverlayImage->SetData(mImageData); + + nsRefPtr image = static_cast(mOverlayImage.get()); + mozilla::gfx::IntSize size = image->GetSize(); + + segment.AppendFrame(image.forget(), delta, size); +#endif + srcStream->AddTrack(TRACK_VIDEO_PRIMARY, 0, new VideoSegment()); + srcStream->AppendToTrack(TRACK_VIDEO_PRIMARY, &segment); + srcStream->FinishAddTracks(); + srcStream->AdvanceKnownTracksTime(STREAM_TIME_MAX); + } +} + +int32_t +DOMHwMediaStream::RequestOverlayId() +{ +#ifdef MOZ_WIDGET_GONK + return mOverlayImage->GetOverlayId(); +#else + return -1; +#endif +} + +void +DOMHwMediaStream::SetOverlayId(int32_t aOverlayId) +{ +#ifdef MOZ_WIDGET_GONK + OverlayImage::Data imgData; + + imgData.mOverlayId = aOverlayId; + imgData.mSize = mOverlayImage->GetSize(); + + mOverlayImage->SetData(imgData); +#endif +} diff --git a/dom/media/DOMMediaStream.h b/dom/media/DOMMediaStream.h index b8941d84c30f..c0a883688e3a 100644 --- a/dom/media/DOMMediaStream.h +++ b/dom/media/DOMMediaStream.h @@ -6,6 +6,8 @@ #ifndef NSDOMMEDIASTREAM_H_ #define NSDOMMEDIASTREAM_H_ +#include "ImageContainer.h" + #include "nsCycleCollectionParticipant.h" #include "nsWrapperCache.h" #include "StreamBuffer.h" @@ -29,6 +31,7 @@ namespace mozilla { +class DOMHwMediaStream; class DOMLocalMediaStream; class MediaStream; class MediaEngineSource; @@ -47,6 +50,11 @@ class VideoTrackList; class MediaTrackListListener; } +namespace layers { +class ImageContainer; +class OverlayImage; +} + class MediaStreamDirectListener; #define NS_DOMMEDIASTREAM_IID \ @@ -114,6 +122,7 @@ public: virtual void StopTrack(TrackID aTrackID); virtual DOMLocalMediaStream* AsDOMLocalMediaStream() { return nullptr; } + virtual DOMHwMediaStream* AsDOMHwMediaStream() { return nullptr; } bool IsFinished(); /** @@ -375,6 +384,39 @@ private: nsRefPtr mStreamNode; }; +class DOMHwMediaStream : public DOMLocalMediaStream +{ + typedef mozilla::gfx::IntSize IntSize; + typedef layers::ImageContainer ImageContainer; +#ifdef MOZ_WIDGET_GONK + typedef layers::OverlayImage OverlayImage; + typedef layers::OverlayImage::Data Data; +#endif + +public: + DOMHwMediaStream(); + + static already_AddRefed CreateHwStream(nsIDOMWindow* aWindow); + virtual DOMHwMediaStream* AsDOMHwMediaStream() override { return this; } + int32_t RequestOverlayId(); + void SetOverlayId(int32_t aOverlayId); + +protected: + ~DOMHwMediaStream(); + +private: + void Init(MediaStream* aStream); + +#ifdef MOZ_WIDGET_GONK + nsRefPtr mImageContainer; + const int DEFAULT_IMAGE_ID = 0x01; + const int DEFAULT_IMAGE_WIDTH = 400; + const int DEFAULT_IMAGE_HEIGHT = 300; + nsRefPtr mOverlayImage; + Data mImageData; +#endif +}; + } #endif /* NSDOMMEDIASTREAM_H_ */ diff --git a/dom/tv/TVTuner.cpp b/dom/tv/TVTuner.cpp index 8af7d30282f1..3a609b7e22e6 100644 --- a/dom/tv/TVTuner.cpp +++ b/dom/tv/TVTuner.cpp @@ -197,8 +197,10 @@ TVTuner::GetStream() const nsresult TVTuner::InitMediaStream() { - // TODO Instantiate |mStream| when bug 987498 is done. + nsCOMPtr window = do_QueryInterface(GetOwner()); + nsRefPtr stream = DOMHwMediaStream::CreateHwStream(window); + mStream = stream.forget(); return NS_OK; }