media/webrtc/signaling/test/FakeMediaStreams.h

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

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 file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef FAKE_MEDIA_STREAM_H_
michael@0 6 #define FAKE_MEDIA_STREAM_H_
michael@0 7
michael@0 8 #include <set>
michael@0 9
michael@0 10 #include "nsNetCID.h"
michael@0 11 #include "nsITimer.h"
michael@0 12 #include "nsComponentManagerUtils.h"
michael@0 13 #include "nsIComponentManager.h"
michael@0 14 #include "nsIComponentRegistrar.h"
michael@0 15
michael@0 16 // #includes from MediaStream.h
michael@0 17 #include "mozilla/Mutex.h"
michael@0 18 #include "AudioSegment.h"
michael@0 19 #include "MediaSegment.h"
michael@0 20 #include "StreamBuffer.h"
michael@0 21 #include "nsTArray.h"
michael@0 22 #include "nsIRunnable.h"
michael@0 23 #include "nsISupportsImpl.h"
michael@0 24 #include "nsIDOMMediaStream.h"
michael@0 25
michael@0 26 class nsIDOMWindow;
michael@0 27
michael@0 28 namespace mozilla {
michael@0 29 class MediaStreamGraph;
michael@0 30 class MediaSegment;
michael@0 31 };
michael@0 32
michael@0 33 class Fake_SourceMediaStream;
michael@0 34
michael@0 35 static const int64_t USECS_PER_S = 1000000;
michael@0 36
michael@0 37 class Fake_MediaStreamListener
michael@0 38 {
michael@0 39 public:
michael@0 40 virtual ~Fake_MediaStreamListener() {}
michael@0 41
michael@0 42 virtual void NotifyQueuedTrackChanges(mozilla::MediaStreamGraph* aGraph, mozilla::TrackID aID,
michael@0 43 mozilla::TrackRate aTrackRate,
michael@0 44 mozilla::TrackTicks aTrackOffset,
michael@0 45 uint32_t aTrackEvents,
michael@0 46 const mozilla::MediaSegment& aQueuedMedia) = 0;
michael@0 47 virtual void NotifyPull(mozilla::MediaStreamGraph* aGraph, mozilla::StreamTime aDesiredTime) = 0;
michael@0 48
michael@0 49 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Fake_MediaStreamListener)
michael@0 50 };
michael@0 51
michael@0 52 class Fake_MediaStreamDirectListener : public Fake_MediaStreamListener
michael@0 53 {
michael@0 54 public:
michael@0 55 virtual ~Fake_MediaStreamDirectListener() {}
michael@0 56
michael@0 57 virtual void NotifyRealtimeData(mozilla::MediaStreamGraph* graph, mozilla::TrackID tid,
michael@0 58 mozilla::TrackRate rate,
michael@0 59 mozilla::TrackTicks offset,
michael@0 60 uint32_t events,
michael@0 61 const mozilla::MediaSegment& media) = 0;
michael@0 62 };
michael@0 63
michael@0 64 // Note: only one listener supported
michael@0 65 class Fake_MediaStream {
michael@0 66 public:
michael@0 67 Fake_MediaStream () : mListeners(), mMutex("Fake MediaStream") {}
michael@0 68 virtual ~Fake_MediaStream() { Stop(); }
michael@0 69
michael@0 70 void AddListener(Fake_MediaStreamListener *aListener) {
michael@0 71 mozilla::MutexAutoLock lock(mMutex);
michael@0 72 mListeners.insert(aListener);
michael@0 73 }
michael@0 74
michael@0 75 void RemoveListener(Fake_MediaStreamListener *aListener) {
michael@0 76 mozilla::MutexAutoLock lock(mMutex);
michael@0 77 mListeners.erase(aListener);
michael@0 78 }
michael@0 79
michael@0 80 virtual Fake_SourceMediaStream *AsSourceStream() { return nullptr; }
michael@0 81
michael@0 82 virtual nsresult Start() { return NS_OK; }
michael@0 83 virtual nsresult Stop() { return NS_OK; }
michael@0 84 virtual void StopStream() {}
michael@0 85
michael@0 86 virtual void Periodic() {}
michael@0 87
michael@0 88 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Fake_MediaStream);
michael@0 89
michael@0 90 protected:
michael@0 91 std::set<Fake_MediaStreamListener *> mListeners;
michael@0 92 mozilla::Mutex mMutex; // Lock to prevent the listener list from being modified while
michael@0 93 // executing Periodic().
michael@0 94 };
michael@0 95
michael@0 96 class Fake_MediaPeriodic : public nsITimerCallback {
michael@0 97 public:
michael@0 98 Fake_MediaPeriodic(Fake_MediaStream *aStream) : mStream(aStream),
michael@0 99 mCount(0) {}
michael@0 100 virtual ~Fake_MediaPeriodic() {}
michael@0 101 void Detach() {
michael@0 102 mStream = nullptr;
michael@0 103 }
michael@0 104
michael@0 105 int GetTimesCalled() { return mCount; }
michael@0 106
michael@0 107 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 108 NS_DECL_NSITIMERCALLBACK
michael@0 109
michael@0 110 protected:
michael@0 111 Fake_MediaStream *mStream;
michael@0 112 int mCount;
michael@0 113 };
michael@0 114
michael@0 115
michael@0 116 class Fake_SourceMediaStream : public Fake_MediaStream {
michael@0 117 public:
michael@0 118 Fake_SourceMediaStream() : mSegmentsAdded(0),
michael@0 119 mDesiredTime(0),
michael@0 120 mPullEnabled(false),
michael@0 121 mStop(false),
michael@0 122 mPeriodic(new Fake_MediaPeriodic(this)) {}
michael@0 123
michael@0 124 void AddTrack(mozilla::TrackID aID, mozilla::TrackRate aRate, mozilla::TrackTicks aStart,
michael@0 125 mozilla::MediaSegment* aSegment) {}
michael@0 126 void EndTrack(mozilla::TrackID aID) {}
michael@0 127
michael@0 128 bool AppendToTrack(mozilla::TrackID aID, mozilla::MediaSegment* aSegment,
michael@0 129 mozilla::MediaSegment *aRawSegment) {
michael@0 130 return AppendToTrack(aID, aSegment);
michael@0 131 }
michael@0 132
michael@0 133 bool AppendToTrack(mozilla::TrackID aID, mozilla::MediaSegment* aSegment) {
michael@0 134 bool nonZeroSample = false;
michael@0 135 MOZ_ASSERT(aSegment);
michael@0 136 if(aSegment->GetType() == mozilla::MediaSegment::AUDIO) {
michael@0 137 //On audio segment append, we verify for validity
michael@0 138 //of the audio samples.
michael@0 139 mozilla::AudioSegment* audio =
michael@0 140 static_cast<mozilla::AudioSegment*>(aSegment);
michael@0 141 mozilla::AudioSegment::ChunkIterator iter(*audio);
michael@0 142 while(!iter.IsEnded()) {
michael@0 143 mozilla::AudioChunk& chunk = *(iter);
michael@0 144 MOZ_ASSERT(chunk.mBuffer);
michael@0 145 const int16_t* buf =
michael@0 146 static_cast<const int16_t*>(chunk.mChannelData[0]);
michael@0 147 for(int i=0; i<chunk.mDuration; i++) {
michael@0 148 if(buf[i]) {
michael@0 149 //atleast one non-zero sample found.
michael@0 150 nonZeroSample = true;
michael@0 151 break;
michael@0 152 }
michael@0 153 }
michael@0 154 //process next chunk
michael@0 155 iter.Next();
michael@0 156 }
michael@0 157 if(nonZeroSample) {
michael@0 158 //we increment segments count if
michael@0 159 //atleast one non-zero samples was found.
michael@0 160 ++mSegmentsAdded;
michael@0 161 }
michael@0 162 } else {
michael@0 163 //in the case of video segment appended, we just increase the
michael@0 164 //segment count.
michael@0 165 ++mSegmentsAdded;
michael@0 166 }
michael@0 167 return true;
michael@0 168 }
michael@0 169
michael@0 170 void AdvanceKnownTracksTime(mozilla::StreamTime aKnownTime) {}
michael@0 171
michael@0 172 void SetPullEnabled(bool aEnabled) {
michael@0 173 mPullEnabled = aEnabled;
michael@0 174 }
michael@0 175 void AddDirectListener(Fake_MediaStreamListener* aListener) {}
michael@0 176 void RemoveDirectListener(Fake_MediaStreamListener* aListener) {}
michael@0 177
michael@0 178 //Don't pull anymore data,if mStop is true.
michael@0 179 virtual void StopStream() {
michael@0 180 mStop = true;
michael@0 181 }
michael@0 182
michael@0 183 virtual Fake_SourceMediaStream *AsSourceStream() { return this; }
michael@0 184
michael@0 185 virtual nsresult Start();
michael@0 186 virtual nsresult Stop();
michael@0 187
michael@0 188 virtual void Periodic();
michael@0 189
michael@0 190 virtual int GetSegmentsAdded() {
michael@0 191 return mSegmentsAdded;
michael@0 192 }
michael@0 193
michael@0 194 protected:
michael@0 195 int mSegmentsAdded;
michael@0 196 uint64_t mDesiredTime;
michael@0 197 bool mPullEnabled;
michael@0 198 bool mStop;
michael@0 199 nsRefPtr<Fake_MediaPeriodic> mPeriodic;
michael@0 200 nsCOMPtr<nsITimer> mTimer;
michael@0 201 };
michael@0 202
michael@0 203
michael@0 204 class Fake_DOMMediaStream : public nsIDOMMediaStream
michael@0 205 {
michael@0 206 public:
michael@0 207 Fake_DOMMediaStream() : mMediaStream(new Fake_MediaStream()) {}
michael@0 208 Fake_DOMMediaStream(Fake_MediaStream *stream) :
michael@0 209 mMediaStream(stream) {}
michael@0 210
michael@0 211 virtual ~Fake_DOMMediaStream() {
michael@0 212 // Note: memory leak
michael@0 213 mMediaStream->Stop();
michael@0 214 }
michael@0 215
michael@0 216 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 217
michael@0 218 static already_AddRefed<Fake_DOMMediaStream>
michael@0 219 CreateSourceStream(nsIDOMWindow* aWindow, uint32_t aHintContents) {
michael@0 220 Fake_SourceMediaStream *source = new Fake_SourceMediaStream();
michael@0 221
michael@0 222 nsRefPtr<Fake_DOMMediaStream> ds = new Fake_DOMMediaStream(source);
michael@0 223 ds->SetHintContents(aHintContents);
michael@0 224
michael@0 225 return ds.forget();
michael@0 226 }
michael@0 227
michael@0 228 virtual void Stop() {} // Really DOMLocalMediaStream
michael@0 229
michael@0 230 virtual bool AddDirectListener(Fake_MediaStreamListener *aListener) { return false; }
michael@0 231 virtual void RemoveDirectListener(Fake_MediaStreamListener *aListener) {}
michael@0 232
michael@0 233 Fake_MediaStream *GetStream() { return mMediaStream; }
michael@0 234
michael@0 235 // Hints to tell the SDP generator about whether this
michael@0 236 // MediaStream probably has audio and/or video
michael@0 237 typedef uint8_t TrackTypeHints;
michael@0 238 enum {
michael@0 239 HINT_CONTENTS_AUDIO = 0x01,
michael@0 240 HINT_CONTENTS_VIDEO = 0x02
michael@0 241 };
michael@0 242 uint32_t GetHintContents() const { return mHintContents; }
michael@0 243 void SetHintContents(uint32_t aHintContents) { mHintContents = aHintContents; }
michael@0 244
michael@0 245 private:
michael@0 246 nsRefPtr<Fake_MediaStream> mMediaStream;
michael@0 247
michael@0 248 // tells the SDP generator about whether this
michael@0 249 // MediaStream probably has audio and/or video
michael@0 250 uint32_t mHintContents;
michael@0 251 };
michael@0 252
michael@0 253 class Fake_MediaStreamGraph
michael@0 254 {
michael@0 255 public:
michael@0 256 virtual ~Fake_MediaStreamGraph() {}
michael@0 257 };
michael@0 258
michael@0 259
michael@0 260
michael@0 261 class Fake_MediaStreamBase : public Fake_MediaStream {
michael@0 262 public:
michael@0 263 Fake_MediaStreamBase() : mPeriodic(new Fake_MediaPeriodic(this)) {}
michael@0 264
michael@0 265 virtual nsresult Start();
michael@0 266 virtual nsresult Stop();
michael@0 267
michael@0 268 virtual int GetSegmentsAdded() {
michael@0 269 return mPeriodic->GetTimesCalled();
michael@0 270 }
michael@0 271
michael@0 272 private:
michael@0 273 nsCOMPtr<nsITimer> mTimer;
michael@0 274 nsRefPtr<Fake_MediaPeriodic> mPeriodic;
michael@0 275 };
michael@0 276
michael@0 277
michael@0 278 class Fake_AudioStreamSource : public Fake_MediaStreamBase {
michael@0 279 public:
michael@0 280 Fake_AudioStreamSource() : Fake_MediaStreamBase(),
michael@0 281 mCount(0),
michael@0 282 mStop(false) {}
michael@0 283 //Signaling Agent indicates us to stop generating
michael@0 284 //further audio.
michael@0 285 void StopStream() {
michael@0 286 mStop = true;
michael@0 287 }
michael@0 288 virtual void Periodic();
michael@0 289 int mCount;
michael@0 290 bool mStop;
michael@0 291 };
michael@0 292
michael@0 293 class Fake_VideoStreamSource : public Fake_MediaStreamBase {
michael@0 294 public:
michael@0 295 Fake_VideoStreamSource() : Fake_MediaStreamBase() {}
michael@0 296 };
michael@0 297
michael@0 298
michael@0 299 namespace mozilla {
michael@0 300 typedef Fake_MediaStream MediaStream;
michael@0 301 typedef Fake_SourceMediaStream SourceMediaStream;
michael@0 302 typedef Fake_MediaStreamListener MediaStreamListener;
michael@0 303 typedef Fake_MediaStreamDirectListener MediaStreamDirectListener;
michael@0 304 typedef Fake_DOMMediaStream DOMMediaStream;
michael@0 305 typedef Fake_DOMMediaStream DOMLocalMediaStream;
michael@0 306 }
michael@0 307
michael@0 308 #endif

mercurial