media/webrtc/signaling/test/FakeMediaStreamsImpl.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/webrtc/signaling/test/FakeMediaStreamsImpl.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,196 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef FAKE_MEDIA_STREAMIMPL_H_
     1.9 +#define FAKE_MEDIA_STREAMIMPL_H_
    1.10 +
    1.11 +#include "FakeMediaStreams.h"
    1.12 +
    1.13 +#include "nspr.h"
    1.14 +#include "nsError.h"
    1.15 +
    1.16 +void LogTime(AsyncLatencyLogger::LatencyLogIndex index, uint64_t b, int64_t c) {}
    1.17 +void LogLatency(AsyncLatencyLogger::LatencyLogIndex index, uint64_t b, int64_t c) {}
    1.18 +
    1.19 +static const int AUDIO_BUFFER_SIZE = 1600;
    1.20 +static const int NUM_CHANNELS      = 2;
    1.21 +
    1.22 +NS_IMPL_ISUPPORTS(Fake_DOMMediaStream, nsIDOMMediaStream)
    1.23 +
    1.24 +// Fake_SourceMediaStream
    1.25 +nsresult Fake_SourceMediaStream::Start() {
    1.26 +  mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
    1.27 +  if (!mTimer) {
    1.28 +    return NS_ERROR_FAILURE;
    1.29 +  }
    1.30 +
    1.31 +  mTimer->InitWithCallback(mPeriodic, 100, nsITimer::TYPE_REPEATING_SLACK);
    1.32 +
    1.33 +  return NS_OK;
    1.34 +}
    1.35 +
    1.36 +nsresult Fake_SourceMediaStream::Stop() {
    1.37 +  mozilla::MutexAutoLock lock(mMutex);
    1.38 +  if (mTimer)
    1.39 +    mTimer->Cancel();
    1.40 +  mPeriodic->Detach();
    1.41 +  return NS_OK;
    1.42 +}
    1.43 +
    1.44 +void Fake_SourceMediaStream::Periodic() {
    1.45 +  mozilla::MutexAutoLock lock(mMutex);
    1.46 +  // Pull more audio-samples iff pulling is enabled
    1.47 +  // and we are not asked by the signaling agent to stop
    1.48 +  //pulling data.
    1.49 +  if (mPullEnabled && !mStop) {
    1.50 +    for (std::set<Fake_MediaStreamListener *>::iterator it =
    1.51 +             mListeners.begin(); it != mListeners.end(); ++it) {
    1.52 +      mDesiredTime += 10;
    1.53 +      (*it)->NotifyPull(nullptr, mozilla::MillisecondsToMediaTime(mDesiredTime));
    1.54 +    }
    1.55 +  }
    1.56 +}
    1.57 +
    1.58 +// Fake_MediaStreamBase
    1.59 +nsresult Fake_MediaStreamBase::Start() {
    1.60 +  mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
    1.61 +  if (!mTimer) {
    1.62 +    return NS_ERROR_FAILURE;
    1.63 +  }
    1.64 +
    1.65 +  mTimer->InitWithCallback(mPeriodic, 100, nsITimer::TYPE_REPEATING_SLACK);
    1.66 +
    1.67 +  return NS_OK;
    1.68 +}
    1.69 +
    1.70 +nsresult Fake_MediaStreamBase::Stop() {
    1.71 +  // Lock the mutex so that we know that after this
    1.72 +  // has returned, periodic will not be firing again
    1.73 +  // and so it's safe to destruct.
    1.74 +  mozilla::MutexAutoLock lock(mMutex);
    1.75 +  mTimer->Cancel();
    1.76 +
    1.77 +  return NS_OK;
    1.78 +}
    1.79 +
    1.80 +// Fake_AudioStreamSource
    1.81 +void Fake_AudioStreamSource::Periodic() {
    1.82 +  mozilla::MutexAutoLock lock(mMutex);
    1.83 +  //Are we asked to stop pumping audio samples ?
    1.84 +  if(mStop) {
    1.85 +    return;
    1.86 +  }
    1.87 +  //Generate Signed 16 Bit Audio samples
    1.88 +  nsRefPtr<mozilla::SharedBuffer> samples =
    1.89 +    mozilla::SharedBuffer::Create(AUDIO_BUFFER_SIZE * NUM_CHANNELS * sizeof(int16_t));
    1.90 +  int16_t* data = reinterpret_cast<int16_t *>(samples->Data());
    1.91 +  for(int i=0; i<(1600*2); i++) {
    1.92 +    //saw tooth audio sample
    1.93 +    data[i] = ((mCount % 8) * 4000) - (7*4000)/2;
    1.94 +    mCount++;
    1.95 +  }
    1.96 +
    1.97 +  mozilla::AudioSegment segment;
    1.98 +  nsAutoTArray<const int16_t *,1> channels;
    1.99 +  channels.AppendElement(data);
   1.100 +  segment.AppendFrames(samples.forget(), channels, AUDIO_BUFFER_SIZE);
   1.101 +
   1.102 +  for(std::set<Fake_MediaStreamListener *>::iterator it = mListeners.begin();
   1.103 +       it != mListeners.end(); ++it) {
   1.104 +    (*it)->NotifyQueuedTrackChanges(nullptr, // Graph
   1.105 +                                    0, // TrackID
   1.106 +                                    16000, // Rate (hz)
   1.107 +                                    0, // Offset TODO(ekr@rtfm.com) fix
   1.108 +                                    0, // ???
   1.109 +                                    segment);
   1.110 +  }
   1.111 +}
   1.112 +
   1.113 +
   1.114 +// Fake_MediaPeriodic
   1.115 +NS_IMPL_ISUPPORTS(Fake_MediaPeriodic, nsITimerCallback)
   1.116 +
   1.117 +NS_IMETHODIMP
   1.118 +Fake_MediaPeriodic::Notify(nsITimer *timer) {
   1.119 +  if (mStream)
   1.120 +    mStream->Periodic();
   1.121 +  ++mCount;
   1.122 +  return NS_OK;
   1.123 +}
   1.124 +
   1.125 +
   1.126 +#if 0
   1.127 +#define WIDTH 320
   1.128 +#define HEIGHT 240
   1.129 +#define RATE USECS_PER_S
   1.130 +#define USECS_PER_S 1000000
   1.131 +#define FPS 10
   1.132 +
   1.133 +NS_IMETHODIMP
   1.134 +Fake_VideoStreamSource::Notify(nsITimer* aTimer)
   1.135 +{
   1.136 +#if 0
   1.137 +  mozilla::layers::BufferRecycleBin bin;
   1.138 +
   1.139 +  nsRefPtr<mozilla::layers::PlanarYCbCrImage> image = new
   1.140 +    mozilla::layers::PlanarYCbCrImage(&bin);
   1.141 +
   1.142 +  const uint8_t lumaBpp = 8;
   1.143 +  const uint8_t chromaBpp = 4;
   1.144 +
   1.145 +  int len = ((WIDTH * HEIGHT) * 3 / 2);
   1.146 +  uint8_t* frame = (uint8_t*) PR_Malloc(len);
   1.147 +  memset(frame, 0x80, len); // Gray
   1.148 +
   1.149 +  mozilla::layers::PlanarYCbCrData data;
   1.150 +  data.mYChannel = frame;
   1.151 +  data.mYSize = gfxIntSize(WIDTH, HEIGHT);
   1.152 +  data.mYStride = WIDTH * lumaBpp / 8.0;
   1.153 +  data.mCbCrStride = WIDTH * chromaBpp / 8.0;
   1.154 +  data.mCbChannel = frame + HEIGHT * data.mYStride;
   1.155 +  data.mCrChannel = data.mCbChannel + HEIGHT * data.mCbCrStride / 2;
   1.156 +  data.mCbCrSize = gfxIntSize(WIDTH / 2, HEIGHT / 2);
   1.157 +  data.mPicX = 0;
   1.158 +  data.mPicY = 0;
   1.159 +  data.mPicSize = gfxIntSize(WIDTH, HEIGHT);
   1.160 +  data.mStereoMode = mozilla::layers::StereoMode::MONO;
   1.161 +
   1.162 +  mozilla::VideoSegment segment;
   1.163 +  segment.AppendFrame(image.forget(), USECS_PER_S / FPS, gfxIntSize(WIDTH, HEIGHT));
   1.164 +
   1.165 +  // TODO(ekr@rtfm.com): are we leaking?
   1.166 +#endif
   1.167 +
   1.168 +  return NS_OK;
   1.169 +}
   1.170 +
   1.171 +
   1.172 +#if 0
   1.173 +// Fake up buffer recycle bin
   1.174 +mozilla::layers::BufferRecycleBin::BufferRecycleBin() :
   1.175 + mLock("mozilla.layers.BufferRecycleBin.mLock") {
   1.176 +}
   1.177 +
   1.178 +void mozilla::layers::BufferRecycleBin::RecycleBuffer(uint8_t* buffer, uint32_t size) {
   1.179 +  PR_Free(buffer);
   1.180 +}
   1.181 +
   1.182 +uint8_t *mozilla::layers::BufferRecycleBin::GetBuffer(uint32_t size) {
   1.183 +  return (uint8_t *)PR_MALLOC(size);
   1.184 +}
   1.185 +
   1.186 +// YCbCrImage constructor (from ImageLayers.cpp)
   1.187 +mozilla::layers::PlanarYCbCrImage::PlanarYCbCrImage(BufferRecycleBin *aRecycleBin)
   1.188 +  : Image(nsnull, ImageFormat::PLANAR_YCBCR)
   1.189 +  , mBufferSize(0)
   1.190 +  , mRecycleBin(aRecycleBin)
   1.191 +{
   1.192 +}
   1.193 +
   1.194 +
   1.195 +#endif
   1.196 +#endif
   1.197 +
   1.198 +
   1.199 +#endif

mercurial