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