dom/camera/CameraPreviewMediaStream.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/camera/CameraPreviewMediaStream.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "CameraPreviewMediaStream.h"
    1.10 +
    1.11 +using namespace mozilla::layers;
    1.12 +using namespace mozilla::dom;
    1.13 +
    1.14 +namespace mozilla {
    1.15 +
    1.16 +CameraPreviewMediaStream::CameraPreviewMediaStream(DOMMediaStream* aWrapper)
    1.17 +  : MediaStream(aWrapper)
    1.18 +  , mMutex("mozilla::camera::CameraPreviewMediaStream")
    1.19 +  , mFrameCallback(nullptr)
    1.20 +{
    1.21 +  SetGraphImpl(MediaStreamGraph::GetInstance());
    1.22 +  mIsConsumed = false;
    1.23 +}
    1.24 +
    1.25 +void
    1.26 +CameraPreviewMediaStream::AddAudioOutput(void* aKey)
    1.27 +{
    1.28 +}
    1.29 +
    1.30 +void
    1.31 +CameraPreviewMediaStream::SetAudioOutputVolume(void* aKey, float aVolume)
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +void
    1.36 +CameraPreviewMediaStream::RemoveAudioOutput(void* aKey)
    1.37 +{
    1.38 +}
    1.39 +
    1.40 +void
    1.41 +CameraPreviewMediaStream::AddVideoOutput(VideoFrameContainer* aContainer)
    1.42 +{
    1.43 +  MutexAutoLock lock(mMutex);
    1.44 +  nsRefPtr<VideoFrameContainer> container = aContainer;
    1.45 +  AddVideoOutputImpl(container.forget());
    1.46 +
    1.47 +  if (mVideoOutputs.Length() > 1) {
    1.48 +    return;
    1.49 +  }
    1.50 +  MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
    1.51 +  mIsConsumed = true;
    1.52 +  for (uint32_t j = 0; j < mListeners.Length(); ++j) {
    1.53 +    MediaStreamListener* l = mListeners[j];
    1.54 +    l->NotifyConsumptionChanged(gm, MediaStreamListener::CONSUMED);
    1.55 +  }
    1.56 +}
    1.57 +
    1.58 +void
    1.59 +CameraPreviewMediaStream::RemoveVideoOutput(VideoFrameContainer* aContainer)
    1.60 +{
    1.61 +  MutexAutoLock lock(mMutex);
    1.62 +  RemoveVideoOutputImpl(aContainer);
    1.63 +
    1.64 +  if (!mVideoOutputs.IsEmpty()) {
    1.65 +    return;
    1.66 +  }
    1.67 +  MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
    1.68 +  mIsConsumed = false;
    1.69 +  for (uint32_t j = 0; j < mListeners.Length(); ++j) {
    1.70 +    MediaStreamListener* l = mListeners[j];
    1.71 +    l->NotifyConsumptionChanged(gm, MediaStreamListener::NOT_CONSUMED);
    1.72 +  }
    1.73 +}
    1.74 +
    1.75 +void
    1.76 +CameraPreviewMediaStream::ChangeExplicitBlockerCount(int32_t aDelta)
    1.77 +{
    1.78 +}
    1.79 +
    1.80 +void
    1.81 +CameraPreviewMediaStream::AddListener(MediaStreamListener* aListener)
    1.82 +{
    1.83 +  MutexAutoLock lock(mMutex);
    1.84 +
    1.85 +  MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
    1.86 +  MediaStreamListener* listener = *mListeners.AppendElement() = aListener;
    1.87 +  listener->NotifyBlockingChanged(gm, MediaStreamListener::UNBLOCKED);
    1.88 +}
    1.89 +
    1.90 +void
    1.91 +CameraPreviewMediaStream::RemoveListener(MediaStreamListener* aListener)
    1.92 +{
    1.93 +  MutexAutoLock lock(mMutex);
    1.94 +
    1.95 +  MediaStreamGraph* gm = MediaStreamGraph::GetInstance();
    1.96 +  nsRefPtr<MediaStreamListener> listener(aListener);
    1.97 +  mListeners.RemoveElement(aListener);
    1.98 +  listener->NotifyRemoved(gm);
    1.99 +}
   1.100 +
   1.101 +void
   1.102 +CameraPreviewMediaStream::Destroy()
   1.103 +{
   1.104 +  MutexAutoLock lock(mMutex);
   1.105 +  DestroyImpl();
   1.106 +}
   1.107 +
   1.108 +void
   1.109 +CameraPreviewMediaStream::SetCurrentFrame(const gfxIntSize& aIntrinsicSize, Image* aImage)
   1.110 +{
   1.111 +  MutexAutoLock lock(mMutex);
   1.112 +
   1.113 +  TimeStamp now = TimeStamp::Now();
   1.114 +  for (uint32_t i = 0; i < mVideoOutputs.Length(); ++i) {
   1.115 +    VideoFrameContainer* output = mVideoOutputs[i];
   1.116 +    output->SetCurrentFrame(aIntrinsicSize, aImage, now);
   1.117 +    nsCOMPtr<nsIRunnable> event =
   1.118 +      NS_NewRunnableMethod(output, &VideoFrameContainer::Invalidate);
   1.119 +    NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
   1.120 +  }
   1.121 +
   1.122 +  if (mFrameCallback) {
   1.123 +    mFrameCallback->OnNewFrame(aIntrinsicSize, aImage);
   1.124 +  }
   1.125 +}
   1.126 +
   1.127 +void
   1.128 +CameraPreviewMediaStream::ClearCurrentFrame()
   1.129 +{
   1.130 +  MutexAutoLock lock(mMutex);
   1.131 +
   1.132 +  for (uint32_t i = 0; i < mVideoOutputs.Length(); ++i) {
   1.133 +    VideoFrameContainer* output = mVideoOutputs[i];
   1.134 +    output->ClearCurrentFrame();
   1.135 +    nsCOMPtr<nsIRunnable> event =
   1.136 +      NS_NewRunnableMethod(output, &VideoFrameContainer::Invalidate);
   1.137 +    NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
   1.138 +  }
   1.139 +}
   1.140 +
   1.141 +}

mercurial