Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef FAKE_MEDIA_STREAMIMPL_H_
6 #define FAKE_MEDIA_STREAMIMPL_H_
8 #include "FakeMediaStreams.h"
10 #include "nspr.h"
11 #include "nsError.h"
13 void LogTime(AsyncLatencyLogger::LatencyLogIndex index, uint64_t b, int64_t c) {}
14 void LogLatency(AsyncLatencyLogger::LatencyLogIndex index, uint64_t b, int64_t c) {}
16 static const int AUDIO_BUFFER_SIZE = 1600;
17 static const int NUM_CHANNELS = 2;
19 NS_IMPL_ISUPPORTS(Fake_DOMMediaStream, nsIDOMMediaStream)
21 // Fake_SourceMediaStream
22 nsresult Fake_SourceMediaStream::Start() {
23 mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
24 if (!mTimer) {
25 return NS_ERROR_FAILURE;
26 }
28 mTimer->InitWithCallback(mPeriodic, 100, nsITimer::TYPE_REPEATING_SLACK);
30 return NS_OK;
31 }
33 nsresult Fake_SourceMediaStream::Stop() {
34 mozilla::MutexAutoLock lock(mMutex);
35 if (mTimer)
36 mTimer->Cancel();
37 mPeriodic->Detach();
38 return NS_OK;
39 }
41 void Fake_SourceMediaStream::Periodic() {
42 mozilla::MutexAutoLock lock(mMutex);
43 // Pull more audio-samples iff pulling is enabled
44 // and we are not asked by the signaling agent to stop
45 //pulling data.
46 if (mPullEnabled && !mStop) {
47 for (std::set<Fake_MediaStreamListener *>::iterator it =
48 mListeners.begin(); it != mListeners.end(); ++it) {
49 mDesiredTime += 10;
50 (*it)->NotifyPull(nullptr, mozilla::MillisecondsToMediaTime(mDesiredTime));
51 }
52 }
53 }
55 // Fake_MediaStreamBase
56 nsresult Fake_MediaStreamBase::Start() {
57 mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
58 if (!mTimer) {
59 return NS_ERROR_FAILURE;
60 }
62 mTimer->InitWithCallback(mPeriodic, 100, nsITimer::TYPE_REPEATING_SLACK);
64 return NS_OK;
65 }
67 nsresult Fake_MediaStreamBase::Stop() {
68 // Lock the mutex so that we know that after this
69 // has returned, periodic will not be firing again
70 // and so it's safe to destruct.
71 mozilla::MutexAutoLock lock(mMutex);
72 mTimer->Cancel();
74 return NS_OK;
75 }
77 // Fake_AudioStreamSource
78 void Fake_AudioStreamSource::Periodic() {
79 mozilla::MutexAutoLock lock(mMutex);
80 //Are we asked to stop pumping audio samples ?
81 if(mStop) {
82 return;
83 }
84 //Generate Signed 16 Bit Audio samples
85 nsRefPtr<mozilla::SharedBuffer> samples =
86 mozilla::SharedBuffer::Create(AUDIO_BUFFER_SIZE * NUM_CHANNELS * sizeof(int16_t));
87 int16_t* data = reinterpret_cast<int16_t *>(samples->Data());
88 for(int i=0; i<(1600*2); i++) {
89 //saw tooth audio sample
90 data[i] = ((mCount % 8) * 4000) - (7*4000)/2;
91 mCount++;
92 }
94 mozilla::AudioSegment segment;
95 nsAutoTArray<const int16_t *,1> channels;
96 channels.AppendElement(data);
97 segment.AppendFrames(samples.forget(), channels, AUDIO_BUFFER_SIZE);
99 for(std::set<Fake_MediaStreamListener *>::iterator it = mListeners.begin();
100 it != mListeners.end(); ++it) {
101 (*it)->NotifyQueuedTrackChanges(nullptr, // Graph
102 0, // TrackID
103 16000, // Rate (hz)
104 0, // Offset TODO(ekr@rtfm.com) fix
105 0, // ???
106 segment);
107 }
108 }
111 // Fake_MediaPeriodic
112 NS_IMPL_ISUPPORTS(Fake_MediaPeriodic, nsITimerCallback)
114 NS_IMETHODIMP
115 Fake_MediaPeriodic::Notify(nsITimer *timer) {
116 if (mStream)
117 mStream->Periodic();
118 ++mCount;
119 return NS_OK;
120 }
123 #if 0
124 #define WIDTH 320
125 #define HEIGHT 240
126 #define RATE USECS_PER_S
127 #define USECS_PER_S 1000000
128 #define FPS 10
130 NS_IMETHODIMP
131 Fake_VideoStreamSource::Notify(nsITimer* aTimer)
132 {
133 #if 0
134 mozilla::layers::BufferRecycleBin bin;
136 nsRefPtr<mozilla::layers::PlanarYCbCrImage> image = new
137 mozilla::layers::PlanarYCbCrImage(&bin);
139 const uint8_t lumaBpp = 8;
140 const uint8_t chromaBpp = 4;
142 int len = ((WIDTH * HEIGHT) * 3 / 2);
143 uint8_t* frame = (uint8_t*) PR_Malloc(len);
144 memset(frame, 0x80, len); // Gray
146 mozilla::layers::PlanarYCbCrData data;
147 data.mYChannel = frame;
148 data.mYSize = gfxIntSize(WIDTH, HEIGHT);
149 data.mYStride = WIDTH * lumaBpp / 8.0;
150 data.mCbCrStride = WIDTH * chromaBpp / 8.0;
151 data.mCbChannel = frame + HEIGHT * data.mYStride;
152 data.mCrChannel = data.mCbChannel + HEIGHT * data.mCbCrStride / 2;
153 data.mCbCrSize = gfxIntSize(WIDTH / 2, HEIGHT / 2);
154 data.mPicX = 0;
155 data.mPicY = 0;
156 data.mPicSize = gfxIntSize(WIDTH, HEIGHT);
157 data.mStereoMode = mozilla::layers::StereoMode::MONO;
159 mozilla::VideoSegment segment;
160 segment.AppendFrame(image.forget(), USECS_PER_S / FPS, gfxIntSize(WIDTH, HEIGHT));
162 // TODO(ekr@rtfm.com): are we leaking?
163 #endif
165 return NS_OK;
166 }
169 #if 0
170 // Fake up buffer recycle bin
171 mozilla::layers::BufferRecycleBin::BufferRecycleBin() :
172 mLock("mozilla.layers.BufferRecycleBin.mLock") {
173 }
175 void mozilla::layers::BufferRecycleBin::RecycleBuffer(uint8_t* buffer, uint32_t size) {
176 PR_Free(buffer);
177 }
179 uint8_t *mozilla::layers::BufferRecycleBin::GetBuffer(uint32_t size) {
180 return (uint8_t *)PR_MALLOC(size);
181 }
183 // YCbCrImage constructor (from ImageLayers.cpp)
184 mozilla::layers::PlanarYCbCrImage::PlanarYCbCrImage(BufferRecycleBin *aRecycleBin)
185 : Image(nsnull, ImageFormat::PLANAR_YCBCR)
186 , mBufferSize(0)
187 , mRecycleBin(aRecycleBin)
188 {
189 }
192 #endif
193 #endif
196 #endif