-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmediaenginebackend_webrtc
1627 lines (1618 loc) · 40.5 KB
/
mediaenginebackend_webrtc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# HG changeset patch
# Parent 1bc4542bb06ddcb905239a9e8de3841243a77435
Bug 691234 - WebRTC media engine backend patch v1.0
diff --git a/content/media/Makefile.in b/content/media/Makefile.in
--- a/content/media/Makefile.in
+++ b/content/media/Makefile.in
@@ -16,16 +16,19 @@ XPIDL_MODULE = content_media
XPIDLSRCS = \
nsIDOMMediaStream.idl \
$(NULL)
EXPORTS = \
AudioSegment.h \
FileBlockCache.h \
+ MediaEngine.h \
+ MediaEngineDefault.h \
+ MediaEngineWebrtc.h \
MediaResource.h \
MediaSegment.h \
MediaStreamGraph.h \
nsAudioAvailableEventManager.h \
nsBuiltinDecoder.h \
nsBuiltinDecoderStateMachine.h \
nsBuiltinDecoderReader.h \
nsDOMMediaStream.h \
@@ -36,16 +39,18 @@ EXPORTS = \
TimeVarying.h \
VideoFrameContainer.h \
VideoUtils.h \
VideoSegment.h \
$(NULL)
CPPSRCS = \
AudioSegment.cpp \
+ MediaEngineDefault.cpp \
+ MediaEngineWebrtc.cpp \
FileBlockCache.cpp \
MediaResource.cpp \
MediaStreamGraph.cpp \
nsAudioAvailableEventManager.cpp \
nsBuiltinDecoder.cpp \
nsBuiltinDecoderStateMachine.cpp \
nsBuiltinDecoderReader.cpp \
nsDOMMediaStream.cpp \
@@ -87,16 +92,17 @@ endif
ifdef ENABLE_TESTS
PARALLEL_DIRS += test
endif
FORCE_STATIC_LIB = 1
include $(topsrcdir)/config/config.mk
+include $(topsrcdir)/media/webrtc/webrtc-config.mk
include $(topsrcdir)/ipc/chromium/chromium-config.mk
include $(topsrcdir)/config/rules.mk
INCLUDES += \
-I$(srcdir)/../base/src \
-I$(srcdir)/../html/content/src \
$(NULL)
diff --git a/content/media/MediaEngine.h b/content/media/MediaEngine.h
new file mode 100644
--- /dev/null
+++ b/content/media/MediaEngine.h
@@ -0,0 +1,110 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef MEDIAENGINE_H_
+#define MEDIAENGINE_H_
+
+#include "nsDOMMediaStream.h"
+#include "MediaStreamGraph.h"
+
+namespace mozilla {
+
+/**
+ * Abstract interface for managing audio and video devices. Each platform
+ * must implement a concrete class that will map these classes and methods
+ * to the appropriate backend. For example, on Desktop platforms, these will
+ * correspond to equivalent webrtc (GIPS) calls, and on B2G they will map to
+ * a Gonk interface.
+ */
+class MediaEngineVideoSource;
+class MediaEngineAudioSource;
+
+class MediaEngine
+{
+public:
+ virtual ~MediaEngine() {};
+
+ /* Populate an array of video sources in the nsTArray. Also include devices
+ * that are currently unavailable. */
+ virtual void EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >*) = 0;
+
+ /* Populate an array of audio sources in the nsTArray. Also include devices
+ * that are currently unavailable. */
+ virtual void EnumerateAudioDevices(nsTArray<nsRefPtr<MediaEngineAudioSource> >*) = 0;
+};
+
+/**
+ * Common abstract base class for audio and video sources.
+ */
+class MediaEngineSource : public nsISupports
+{
+public:
+ virtual ~MediaEngineSource() {};
+
+ /* Populate the human readable name of this device in the nsAString */
+ virtual void GetName(nsAString&) = 0;
+
+ /* Populate the UUID of this device in the nsAString */
+ virtual void GetUUID(nsAString&) = 0;
+
+ /* This call reserves but does not start the device. */
+ virtual already_AddRefed<nsDOMMediaStream> Allocate() = 0;
+
+ /* Release the device back to the system. */
+ virtual nsresult Deallocate() = 0;
+
+ /* Start the device and add the track to the provided SourceMediaStream, with
+ * the provided TrackID. You may start appending data to the track
+ * immediately after. */
+ virtual nsresult Start(SourceMediaStream*, TrackID) = 0;
+
+ /* Stop the device and release the corresponding MediaStream */
+ virtual nsresult Stop() = 0;
+
+ /* It is an error to call Start() before an Allocate(), and Stop() before
+ * a Start(). Only Allocate() may be called after a Deallocate(). */
+};
+
+/**
+ * Video source and friends.
+ */
+enum MediaEngineVideoCodecType {
+ kVideoCodecH263,
+ kVideoCodecVP8,
+ kVideoCodecI420
+};
+
+struct MediaEngineVideoOptions {
+ PRUint32 mWidth;
+ PRUint32 mHeight;
+ PRUint32 mMaxFPS;
+ MediaEngineVideoCodecType codecType;
+};
+
+class MediaEngineVideoSource : public MediaEngineSource
+{
+public:
+ virtual ~MediaEngineVideoSource() {};
+
+ /* Return a MediaEngineVideoOptions struct with appropriate values for all
+ * fields. */
+ virtual MediaEngineVideoOptions GetOptions() = 0;
+
+ virtual void Shutdown() = 0;
+};
+
+/**
+ * Audio source and friends.
+ */
+class MediaEngineAudioSource : public MediaEngineSource
+{
+public:
+ virtual ~MediaEngineAudioSource() {};
+
+ virtual void Shutdown() = 0;
+};
+
+}
+
+#endif /* MEDIAENGINE_H_ */
diff --git a/content/media/MediaEngineDefault.cpp b/content/media/MediaEngineDefault.cpp
new file mode 100644
--- /dev/null
+++ b/content/media/MediaEngineDefault.cpp
@@ -0,0 +1,283 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "MediaEngineDefault.h"
+
+#define WIDTH 320
+#define HEIGHT 240
+#define FPS 10
+#define CHANNELS 1
+
+#define TRACK_VIDEO 1
+#define TRACK_AUDIO 2
+#define RATE USECS_PER_S
+
+namespace mozilla {
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(MediaEngineDefaultVideoSource, nsITimerCallback)
+/**
+ * Default video source.
+ */
+void
+MediaEngineDefaultVideoSource::GetName(nsAString& aName)
+{
+ aName.Assign(NS_LITERAL_STRING("Default Video Device"));
+ return;
+}
+
+void
+MediaEngineDefaultVideoSource::GetUUID(nsAString& aUUID)
+{
+ aUUID.Assign(NS_LITERAL_STRING("1041FCBD-3F12-4F7B-9E9B-1EC556DD5676"));
+ return;
+}
+
+already_AddRefed<nsDOMMediaStream>
+MediaEngineDefaultVideoSource::Allocate()
+{
+ if (mState != kReleased) {
+ return NULL;
+ }
+
+ mState = kAllocated;
+ return nsDOMMediaStream::CreateInputStream();
+}
+
+nsresult
+MediaEngineDefaultVideoSource::Deallocate()
+{
+ if (mState != kStopped && mState != kAllocated) {
+ return NS_ERROR_FAILURE;
+ }
+ mState = kReleased;
+ return NS_OK;
+}
+
+MediaEngineVideoOptions
+MediaEngineDefaultVideoSource::GetOptions()
+{
+ MediaEngineVideoOptions aOpts;
+ aOpts.mWidth = WIDTH;
+ aOpts.mHeight = HEIGHT;
+ aOpts.mMaxFPS = FPS;
+ aOpts.codecType = kVideoCodecI420;
+ return aOpts;
+}
+
+nsresult
+MediaEngineDefaultVideoSource::Start(SourceMediaStream* aStream, TrackID aID)
+{
+ if (mState != kAllocated) {
+ return NULL;
+ }
+
+ mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
+ if (!mTimer) {
+ return NULL;
+ }
+
+ mSource = aStream;
+
+ // Allocate a single blank Image
+ layers::Image::Format format = layers::Image::PLANAR_YCBCR;
+ mImageContainer = layers::LayerManager::CreateImageContainer();
+
+ nsRefPtr<layers::Image> image = mImageContainer->CreateImage(&format, 1);
+
+ int len = ((WIDTH * HEIGHT) * 3 / 2);
+ mImage = static_cast<layers::PlanarYCbCrImage*>(image.get());
+ PRUint8* frame = (PRUint8*) PR_Malloc(len);
+ memset(frame, 0x80, len); // Gray
+
+ const PRUint8 lumaBpp = 8;
+ const PRUint8 chromaBpp = 4;
+
+ layers::PlanarYCbCrImage::Data data;
+ data.mYChannel = frame;
+ data.mYSize = gfxIntSize(WIDTH, HEIGHT);
+ data.mYStride = WIDTH * lumaBpp / 8.0;
+ data.mCbCrStride = WIDTH * chromaBpp / 8.0;
+ data.mCbChannel = frame + HEIGHT * data.mYStride;
+ data.mCrChannel = data.mCbChannel + HEIGHT * data.mCbCrStride / 2;
+ data.mCbCrSize = gfxIntSize(WIDTH / 2, HEIGHT / 2);
+ data.mPicX = 0;
+ data.mPicY = 0;
+ data.mPicSize = gfxIntSize(WIDTH, HEIGHT);
+ data.mStereoMode = layers::STEREO_MODE_MONO;
+
+ // SetData copies data, so we can free the frame
+ mImage->SetData(data);
+ PR_Free(frame);
+
+ // Set start time to 0
+ mSource->AdvanceKnownTracksTime(0);
+
+ // AddTrack takes ownership of segment
+ VideoSegment *segment = new VideoSegment();
+ segment->AppendFrame(image.forget(), USECS_PER_S / FPS, gfxIntSize(WIDTH, HEIGHT));
+ mSource->AddTrack(aID, RATE, 0, segment);
+
+ // Remember TrackID so we can end it later
+ mTrackID = aID;
+
+ // Start timer for subsequent frames
+ mTimer->InitWithCallback(this, 1000 / FPS, nsITimer::TYPE_REPEATING_SLACK);
+ mState = kStarted;
+
+ return NS_OK;
+}
+
+nsresult
+MediaEngineDefaultVideoSource::Stop()
+{
+ if (mState != kStarted) {
+ return NS_ERROR_FAILURE;
+ }
+ if (!mTimer) {
+ return NS_ERROR_FAILURE;
+ }
+
+ mTimer->Cancel();
+ mTimer = NULL;
+
+ mSource->EndTrack(mTrackID);
+ mSource->Finish();
+
+ mState = kStopped;
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+MediaEngineDefaultVideoSource::Notify(nsITimer* aTimer)
+{
+ VideoSegment segment;
+
+ // We never add another track to the stream, so we can always advance?
+ // mSource->AdvanceKnownTracksTime(ticksSoFar * RATE);
+
+ nsRefPtr<layers::PlanarYCbCrImage> image = mImage;
+ segment.AppendFrame(image.forget(), USECS_PER_S / FPS, gfxIntSize(WIDTH, HEIGHT));
+ mSource->AppendToTrack(TRACK_VIDEO, &segment);
+
+ return NS_OK;
+}
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(MediaEngineDefaultAudioSource, nsITimerCallback)
+/**
+ * Default audio source.
+ */
+void
+MediaEngineDefaultAudioSource::GetName(nsAString& aName)
+{
+ aName.Assign(NS_LITERAL_STRING("Default Audio Device"));
+ return;
+}
+
+void
+MediaEngineDefaultAudioSource::GetUUID(nsAString& aUUID)
+{
+ aUUID.Assign(NS_LITERAL_STRING("B7CBD7C1-53EF-42F9-8353-73F61C70C092"));
+ return;
+}
+
+already_AddRefed<nsDOMMediaStream>
+MediaEngineDefaultAudioSource::Allocate()
+{
+ if (mState != kReleased) {
+ return NULL;
+ }
+ mState = kAllocated;
+ return nsDOMMediaStream::CreateInputStream();
+}
+
+nsresult
+MediaEngineDefaultAudioSource::Deallocate()
+{
+ if (mState != kStopped && mState != kAllocated) {
+ return NS_ERROR_FAILURE;
+ }
+ mState = kReleased;
+ return NS_OK;
+}
+
+nsresult
+MediaEngineDefaultAudioSource::Start(SourceMediaStream* aStream, TrackID aID)
+{
+ if (mState != kAllocated) {
+ return NULL;
+ }
+
+ mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
+ if (!mTimer) {
+ return NULL;
+ }
+
+ mSource = aStream;
+
+ // Set start time to 0
+ mSource->AdvanceKnownTracksTime(0);
+
+ // AddTrack will take ownership of segment
+ AudioSegment* segment = new AudioSegment();
+ segment->Init(CHANNELS);
+ mSource->AddTrack(aID, RATE, 0, segment);
+
+ mSource->AdvanceKnownTracksTime(STREAM_TIME_MAX);
+ mTrackID = aID;
+
+ // 1 Audio frame per Video frame.
+ mTimer->InitWithCallback(this, 1000 / FPS, nsITimer::TYPE_REPEATING_SLACK);
+ mState = kStarted;
+
+ return NS_OK;
+}
+
+nsresult
+MediaEngineDefaultAudioSource::Stop()
+{
+ if (mState != kStarted) {
+ return NS_ERROR_FAILURE;
+ }
+ if (!mTimer) {
+ return NS_ERROR_FAILURE;
+ }
+
+ mTimer->Cancel();
+ mTimer = NULL;
+ mState = kStopped;
+ return NS_OK;
+}
+
+NS_IMETHODIMP
+MediaEngineDefaultAudioSource::Notify(nsITimer* aTimer)
+{
+ AudioSegment segment;
+ segment.Init(CHANNELS);
+ segment.InsertNullDataAtStart(1);
+
+ // We never add another track to the stream, so we can always advance?
+ // mSource->AdvanceKnownTracksTime(ticksSoFar * RATE);
+
+ mSource->AppendToTrack(mTrackID, &segment);
+
+ return NS_OK;
+}
+
+void
+MediaEngineDefault::EnumerateVideoDevices(
+ nsTArray<nsRefPtr<MediaEngineVideoSource> >* aVSources) {
+
+ aVSources->AppendElement(mVSource);
+ return;
+}
+
+void
+MediaEngineDefault::EnumerateAudioDevices(
+ nsTArray<nsRefPtr<MediaEngineAudioSource> >* aASources) {
+
+ aASources->AppendElement(mASource);
+ return;
+}
+
+}
diff --git a/content/media/MediaEngineDefault.h b/content/media/MediaEngineDefault.h
new file mode 100644
--- /dev/null
+++ b/content/media/MediaEngineDefault.h
@@ -0,0 +1,117 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef MEDIAENGINEDEFAULT_H_
+#define MEDIAENGINEDEFAULT_H_
+
+#include "prmem.h"
+#include "nsITimer.h"
+
+#include "nsCOMPtr.h"
+#include "nsDOMMediaStream.h"
+#include "nsComponentManagerUtils.h"
+
+#include "Layers.h"
+#include "VideoUtils.h"
+#include "MediaEngine.h"
+#include "ImageLayers.h"
+#include "VideoSegment.h"
+#include "AudioSegment.h"
+#include "StreamBuffer.h"
+#include "MediaStreamGraph.h"
+
+namespace mozilla {
+
+/**
+ * The default implementation of the MediaEngine interface.
+ */
+
+enum DefaultEngineState {
+ kAllocated,
+ kStarted,
+ kStopped,
+ kReleased
+};
+
+class MediaEngineDefaultVideoSource : public nsITimerCallback,
+ public MediaEngineVideoSource
+
+{
+public:
+ MediaEngineDefaultVideoSource() : mTimer(nsnull), mState(kReleased) {}
+ ~MediaEngineDefaultVideoSource(){};
+
+ virtual void Shutdown() {};
+ virtual void GetName(nsAString&);
+ virtual void GetUUID(nsAString&);
+
+ virtual MediaEngineVideoOptions GetOptions();
+ virtual already_AddRefed<nsDOMMediaStream> Allocate();
+
+ virtual nsresult Deallocate();
+ virtual nsresult Start(SourceMediaStream*, TrackID);
+ virtual nsresult Stop();
+
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSITIMERCALLBACK
+
+protected:
+ TrackID mTrackID;
+ nsCOMPtr<nsITimer> mTimer;
+ nsRefPtr<layers::ImageContainer> mImageContainer;
+
+ DefaultEngineState mState;
+ SourceMediaStream* mSource;
+ layers::PlanarYCbCrImage* mImage;
+};
+
+class MediaEngineDefaultAudioSource : public nsITimerCallback,
+ public MediaEngineAudioSource
+
+{
+public:
+ MediaEngineDefaultAudioSource() : mTimer(nsnull), mState(kReleased) {}
+ ~MediaEngineDefaultAudioSource(){};
+ virtual void Shutdown() {};
+
+ virtual void GetName(nsAString&);
+ virtual void GetUUID(nsAString&);
+
+ virtual already_AddRefed<nsDOMMediaStream> Allocate();
+
+ virtual nsresult Deallocate();
+ virtual nsresult Start(SourceMediaStream*, TrackID);
+ virtual nsresult Stop();
+
+ NS_DECL_ISUPPORTS
+ NS_DECL_NSITIMERCALLBACK
+
+protected:
+ nsCOMPtr<nsITimer> mTimer;
+ TrackID mTrackID;
+
+ DefaultEngineState mState;
+ SourceMediaStream* mSource;
+};
+
+class MediaEngineDefault : public MediaEngine
+{
+public:
+ MediaEngineDefault() {
+ mVSource = new MediaEngineDefaultVideoSource();
+ mASource = new MediaEngineDefaultAudioSource();
+ }
+ ~MediaEngineDefault() {}
+
+ virtual void EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >*);
+ virtual void EnumerateAudioDevices(nsTArray<nsRefPtr<MediaEngineAudioSource> >*);
+
+private:
+ nsRefPtr<MediaEngineVideoSource> mVSource;
+ nsRefPtr<MediaEngineAudioSource> mASource;
+};
+
+}
+
+#endif /* NSMEDIAENGINEDEFAULT_H_ */
diff --git a/content/media/MediaEngineWebrtc.cpp b/content/media/MediaEngineWebrtc.cpp
new file mode 100644
--- /dev/null
+++ b/content/media/MediaEngineWebrtc.cpp
@@ -0,0 +1,779 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "MediaEngineWebrtc.h"
+
+#define CHANNELS 1
+
+namespace mozilla {
+
+/**
+ * Webrtc video source.
+ */
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(MediaEngineWebrtcVideoSource, nsITimerCallback)
+
+/*
+ * Shutdown video renderer and capture threads Async
+ */
+class AsyncShutdownThread : public nsRunnable
+{
+public:
+ AsyncShutdownThread(nsIThread* aThread) : mThread(aThread) {}
+ NS_IMETHODIMP Run()
+ {
+ return mThread->Shutdown();
+ }
+private:
+ nsCOMPtr<nsIThread> mThread;
+};
+
+
+
+/**
+ * Runnable to start the capture device
+ */
+class VideoStartCaptureEvent: public nsRunnable
+{
+public:
+ VideoStartCaptureEvent(MediaEngineWebrtcVideoSource* aOwner)
+ {
+ mOwner = aOwner;
+ }
+
+ NS_IMETHOD Run()
+ {
+ int error = mOwner->mViERender->AddRenderer(mOwner->mCapIndex, webrtc::kVideoI420, (webrtc::ExternalRenderer*) mOwner);
+ error = mOwner->mViECapture->StartCapture(mOwner->mCapIndex);
+ if (-1 == error )
+ {
+ printf( " ERROR in ViECapture::StartCapture %d " , mOwner->mViEBase->LastError());
+ return NS_ERROR_FAILURE;
+ }
+
+ mOwner->mState = kStarted;
+ error = mOwner->mViERender->StartRender(mOwner->mCapIndex);
+ if (-1 == error)
+ {
+ printf( "ERROR in ViERender::StartRender %d ", mOwner->mViEBase->LastError());
+ return NS_ERROR_FAILURE;
+ }
+
+ return NS_OK;
+ }
+
+ MediaEngineWebrtcVideoSource* mOwner;
+
+};
+
+/**
+ * Runnable to write the video frames to MediaStream
+ */
+class VideoRenderToStreamEvent : public nsRunnable
+{
+
+public:
+ VideoRenderToStreamEvent(MediaEngineWebrtcVideoSource* aOwner,
+ unsigned char* aBuffer,
+ int aSize,
+ uint32_t aTimestamp,
+ int64_t aRenderTime)
+ {
+ mOwner = aOwner;
+ mBuffer = aBuffer;
+ mSize = aSize;
+ mTimestamp = aTimestamp;
+ mRenderTime = aRenderTime;
+ }
+
+ NS_IMETHOD Run()
+ {
+ ReentrantMonitorAutoEnter enter(mOwner->mMonitor);
+ // create a VideoFrame and push it to the input stream;
+ layers::Image::Format format = layers::Image::PLANAR_YCBCR;
+ nsRefPtr<layers::Image> image = mOwner->mImageContainer->CreateImage(&format, 1);
+ image->AddRef();
+ layers::PlanarYCbCrImage* videoImage = static_cast<mozilla::layers::PlanarYCbCrImage*> (image.get());
+ PRUint8* frame = static_cast<PRUint8*> (mBuffer);
+ const PRUint8 lumaBpp = 8;
+ const PRUint8 chromaBpp = 4;
+
+ layers::PlanarYCbCrImage::Data data;
+ data.mYChannel = frame;
+ data.mYSize = gfxIntSize(mOwner->mWidth, mOwner->mHeight);
+ data.mYStride = mOwner->mWidth * lumaBpp/ 8.0;
+ data.mCbCrStride = mOwner->mWidth * chromaBpp / 8.0;
+ data.mCbChannel = frame + mOwner->mHeight * data.mYStride;
+ data.mCrChannel = data.mCbChannel + mOwner->mHeight * data.mCbCrStride / 2;
+ data.mCbCrSize = gfxIntSize(mOwner->mWidth/ 2, mOwner->mHeight/ 2);
+ data.mPicX = 0;
+ data.mPicY = 0;
+ data.mPicSize = gfxIntSize(mOwner->mWidth, mOwner->mHeight);
+ data.mStereoMode = layers::STEREO_MODE_MONO;
+
+ videoImage->SetData(data);
+
+ mOwner->mVideoSegment.AppendFrame(videoImage,1, gfxIntSize(mOwner->mWidth, mOwner->mHeight));
+ mOwner->mSource->AppendToTrack(mOwner->mTrackID, &(mOwner->mVideoSegment));
+ return NS_OK;
+ }
+
+
+ MediaEngineWebrtcVideoSource* mOwner;
+ unsigned char* mBuffer;
+ int mSize;
+ uint32_t mTimestamp;
+ int64_t mRenderTime;
+};
+
+//static initialization
+const unsigned int MediaEngineWebrtcVideoSource::KMaxDeviceNameLength = 128;
+const unsigned int MediaEngineWebrtcVideoSource::KMaxUniqueIdLength = 256;
+
+// Webrtc_External Renderer Implementation
+int
+MediaEngineWebrtcVideoSource::FrameSizeChange(
+ unsigned int w, unsigned int h, unsigned int streams)
+{
+ mWidth = w;
+ mHeight = h;
+ return 0;
+}
+
+// Webrtc_External Renderer - video frame callback
+int
+MediaEngineWebrtcVideoSource::DeliverFrame(
+ unsigned char* buffer, int size, uint32_t time_stamp, int64_t render_time)
+{
+ // let our rendering thread deal with the media-streams.
+ nsCOMPtr<nsIRunnable> event = new VideoRenderToStreamEvent(this, buffer, size, time_stamp, render_time);
+ mVideoRenderingThread->Dispatch(event,0);
+ return 0;
+}
+
+
+/**
+ * Acquires the basic interfaces for the Video Engine
+ */
+
+void
+MediaEngineWebrtcVideoSource::Init()
+{
+
+ if(NULL == mVideoEngine)
+ return;
+
+ mViEBase = webrtc::ViEBase::GetInterface(mVideoEngine);
+ if (NULL == mViEBase )
+ {
+ printf( "ERROR in VideoEngine::ViEBase ");
+ return;
+ }
+
+ //get interfaces for capture, render for now
+ mViECapture = webrtc::ViECapture::GetInterface(mVideoEngine);
+ mViERender = webrtc::ViERender::GetInterface(mVideoEngine);
+
+ if(NULL == mViECapture || NULL == mViERender)
+ return ;
+
+ if (!mVideoCaptureThread)
+ {
+ NS_NewThread(getter_AddRefs(mVideoCaptureThread),
+ nsnull,
+ MEDIA_THREAD_STACK_SIZE);
+ }
+
+ if (!mVideoRenderingThread)
+ {
+ NS_NewThread(getter_AddRefs(mVideoRenderingThread),
+ nsnull,
+ MEDIA_THREAD_STACK_SIZE);
+ }
+
+ // we should be good here
+ mInitDone = true;
+}
+
+//XXX: Improve this function ??
+void
+MediaEngineWebrtcVideoSource::GetName(nsAString& aName)
+{
+
+ char deviceName[KMaxDeviceNameLength];
+ memset(deviceName, 0, KMaxDeviceNameLength);
+ char uniqueId[KMaxUniqueIdLength];
+ memset(uniqueId, 0, KMaxUniqueIdLength);
+
+ if(true == mInitDone )
+ {
+ mViECapture->GetCaptureDevice(
+ mCapIndex, deviceName, KMaxDeviceNameLength, uniqueId, KMaxUniqueIdLength
+ );
+
+ aName.Assign(NS_ConvertASCIItoUTF16(deviceName));
+
+ }
+}
+
+//XXX: Improve this function ???
+void
+MediaEngineWebrtcVideoSource::GetUUID(nsAString& aUUID)
+{
+ char deviceName[KMaxDeviceNameLength];
+ memset(deviceName, 0, KMaxDeviceNameLength);
+ char uniqueId[KMaxUniqueIdLength];
+ memset(uniqueId, 0, KMaxUniqueIdLength);
+ if(true == mInitDone)
+ {
+ mViECapture->GetCaptureDevice(
+ mCapIndex, deviceName, KMaxDeviceNameLength, uniqueId, KMaxUniqueIdLength
+ );
+
+ aUUID.Assign(NS_ConvertASCIItoUTF16(uniqueId));
+ }
+}
+
+already_AddRefed<nsDOMMediaStream>
+MediaEngineWebrtcVideoSource::Allocate()
+{
+ if (mState != kReleased) {
+ return NULL;
+ }
+
+ //let's allocate the device
+ char deviceName[KMaxDeviceNameLength];
+ char uniqueId[KMaxUniqueIdLength];
+ memset(deviceName, 0, KMaxDeviceNameLength);
+ memset(uniqueId, 0, KMaxUniqueIdLength);
+ mViECapture->GetCaptureDevice(
+ mCapIndex, deviceName, KMaxDeviceNameLength, uniqueId, KMaxUniqueIdLength
+ );
+
+ if(0 == mViECapture->AllocateCaptureDevice(
+ uniqueId, KMaxUniqueIdLength, mCapIndex)) {
+ mState = kAllocated;
+ return nsDOMMediaStream::CreateInputStream();
+ }
+
+ return NULL;
+}
+
+nsresult
+MediaEngineWebrtcVideoSource::Deallocate()
+{
+ if (mState != kStopped && mState != kAllocated) {
+ return NS_ERROR_FAILURE;
+ }
+ mViECapture->ReleaseCaptureDevice(mCapIndex);
+
+ mState = kReleased;
+ return NS_OK;
+}
+
+//XXX: Get rid of the constants
+MediaEngineVideoOptions
+MediaEngineWebrtcVideoSource::GetOptions()
+{
+ MediaEngineVideoOptions aOpts;
+ aOpts.mWidth = mWidth;
+ aOpts.mHeight = mHeight;
+ aOpts.mMaxFPS = mFps;
+ aOpts.codecType = kVideoCodecI420;
+ return aOpts;
+}
+
+// XXX: implement nsIThread here
+nsresult
+MediaEngineWebrtcVideoSource::Start(SourceMediaStream* aStream, TrackID aID)
+{
+ if (false == mInitDone || mState != kAllocated) {
+ return NULL;
+ }
+
+ if(!aStream)
+ return NULL;
+
+ if(mState == kStarted)
+ return NS_OK;
+
+ mSource = aStream;
+ mTrackID = aID;
+
+ //setup a blank track
+ mImageContainer = layers::LayerManager::CreateImageContainer();
+ mSource->AddTrack(aID, mFps, 0, new VideoSegment());
+ mSource->AdvanceKnownTracksTime(STREAM_TIME_MAX);
+
+ nsCOMPtr<nsIRunnable> event = new VideoStartCaptureEvent(this);
+ mVideoCaptureThread->Dispatch(event,0);
+
+ return NS_OK;
+}
+
+
+nsresult
+MediaEngineWebrtcVideoSource::Stop()
+{
+ if (mState != kStarted) {
+ return NS_ERROR_FAILURE;
+ }
+
+
+ mSource->EndTrack(mTrackID);
+ mSource->Finish();
+
+ mViERender->StopRender(mCapIndex);
+ mViERender->RemoveRenderer(mCapIndex);
+ mViECapture->StopCapture(mCapIndex);
+
+ //Stop capture and rendering threads
+ if(mVideoCaptureThread)
+ {
+ nsCOMPtr<nsIRunnable> event = new AsyncShutdownThread(mVideoCaptureThread);
+ NS_DispatchToMainThread(event);
+ }
+
+ if(mVideoRenderingThread)
+ {
+ nsCOMPtr<nsIRunnable> event = new AsyncShutdownThread(mVideoRenderingThread);
+ NS_DispatchToMainThread(event);
+ }
+
+ mState = kStopped;
+ return NS_OK;
+}
+
+void
+MediaEngineWebrtcVideoSource::Shutdown()
+{
+ // Don't clean the mVideoEngine here ...
+ // it is done in MediaEngine class
+ if(false == mInitDone)
+ return;
+
+ if(kStarted == mState)
+ {
+ //there is capture in progress ..
+ mViERender->StopRender(mCapIndex);
+ mViECapture->StopCapture(mCapIndex);
+ //Stop capture and rendering threads
+ if(mVideoCaptureThread)
+ {
+ nsCOMPtr<nsIRunnable> event = new AsyncShutdownThread(mVideoCaptureThread);
+ NS_DispatchToMainThread(event);
+ }
+ if(mVideoRenderingThread)
+ {
+ nsCOMPtr<nsIRunnable> event = new AsyncShutdownThread(mVideoRenderingThread);
+ NS_DispatchToMainThread(event);
+ }
+ //mVideoCaptureThread->Shutdown();
+ //mVideoRenderingThread->Shutdown();
+ mViERender->RemoveRenderer(mCapIndex);
+ }
+
+ mViECapture->ReleaseCaptureDevice(mCapIndex);
+
+ //let's clean up the interfaces now
+ mViECapture->Release();
+ mViERender->Release();
+ mViEBase->Release();
+ mState = kReleased;
+ mInitDone = false;
+
+}
+//XXX: How to get rid of this ??
+NS_IMETHODIMP
+MediaEngineWebrtcVideoSource::Notify(nsITimer* aTimer)
+{
+ return NS_OK;
+}
+
+
+
+/**
+ * Webrtc audio source.
+ */
+
+NS_IMPL_THREADSAFE_ISUPPORTS1(MediaEngineWebrtcAudioSource, nsITimerCallback)
+
+//static initialization
+const unsigned int MediaEngineWebrtcAudioSource::PLAYOUT_SAMPLE_FREQUENCY = 16000;
+const unsigned int MediaEngineWebrtcAudioSource::KMaxDeviceNameLength = 128;
+const unsigned int MediaEngineWebrtcAudioSource::KMaxUniqueIdLength = 128;
+