-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebrtc-backend
1124 lines (1115 loc) · 28.1 KB
/
webrtc-backend
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 GUM Webrt Backend v1
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,18 @@ XPIDL_MODULE = content_media
XPIDLSRCS = \
nsIDOMMediaStream.idl \
$(NULL)
EXPORTS = \
AudioSegment.h \
FileBlockCache.h \
+ MediaEngine.h \
+ MediaEngineWebrtc.h \
MediaResource.h \
MediaSegment.h \
MediaStreamGraph.h \
nsAudioAvailableEventManager.h \
nsBuiltinDecoder.h \
nsBuiltinDecoderStateMachine.h \
nsBuiltinDecoderReader.h \
nsDOMMediaStream.h \
@@ -36,16 +38,17 @@ EXPORTS = \
TimeVarying.h \
VideoFrameContainer.h \
VideoUtils.h \
VideoSegment.h \
$(NULL)
CPPSRCS = \
AudioSegment.cpp \
+ MediaEngineWebrtc.cpp \
FileBlockCache.cpp \
MediaResource.cpp \
MediaStreamGraph.cpp \
nsAudioAvailableEventManager.cpp \
nsBuiltinDecoder.cpp \
nsBuiltinDecoderStateMachine.cpp \
nsBuiltinDecoderReader.cpp \
nsDOMMediaStream.cpp \
@@ -87,16 +90,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/MediaEngineWebrtc.cpp b/content/media/MediaEngineWebrtc.cpp
new file mode 100644
--- /dev/null
+++ b/content/media/MediaEngineWebrtc.cpp
@@ -0,0 +1,795 @@
+/* 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);
+ }
+
+
+ //temporary logging
+ mVideoEngine->SetTraceFilter(webrtc::kTraceAll);
+ mVideoEngine->SetTraceFile( "Vievideotrace.out" );
+
+
+ // 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;
+
+ //dumping the capabilities - temp solution
+ webrtc::CaptureCapability cap;
+ int numCaps = mViECapture->NumberOfCapabilities(uniqueId, KMaxUniqueIdLength);
+ for ( int j = 0; j<numCaps; j++ )
+ {
+ if ( mViECapture->GetCaptureCapability(uniqueId,KMaxUniqueIdLength, j, cap ) != 0 ) break;
+ printf( "type=%d width=%d height=%d maxFPS=%d capturedDelay=%d ", cap.rawType, cap.width, cap.height, cap.maxFPS, cap.expectedCaptureDelay );
+ }
+
+ 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;
+
+
+
+
+// Performs very basic & common initialization
+void
+MediaEngineWebrtcAudioSource::Init()
+{
+
+ mVoEBase = webrtc::VoEBase::GetInterface(mVoiceEngine);
+ if (NULL == mVoEBase )
+ {
+ printf( "ERROR in AudioEngine::VoEBase ");
+ return;
+ }
+
+ //get interfaces for capture, render for now
+ mVoEHw = webrtc::VoEHardware::GetInterface(mVoiceEngine);
+
+ // check if all the interfaces were ok till now
+ if(NULL == mVoEHw)
+ return ;
+
+ mChannel = mVoEBase->CreateChannel();
+ if(-1 == mChannel)
+ {
+ printf("\n Couldn't create the channel in AudioEngine ");
+ return;
+ }
+
+ mVoEXmedia = webrtc::VoEExternalMedia::GetInterface(mVoiceEngine);
+ mAudioSegment.Init(CHANNELS);
+ // we should be good by now
+ mInitDone = true;
+
+}
+
+void
+MediaEngineWebrtcAudioSource::GetName(nsAString& aName)
+{
+ char deviceName[KMaxDeviceNameLength];
+ memset(deviceName, 0, KMaxDeviceNameLength);
+ char uniqueId[KMaxUniqueIdLength];
+ memset(uniqueId, 0, KMaxUniqueIdLength);
+ if(true == mInitDone)
+ {
+ mVoEHw->GetRecordingDeviceName(
+ mCapIndex, deviceName, uniqueId );
+ aName.Assign(NS_ConvertASCIItoUTF16(deviceName));
+ }
+
+ return;
+}
+
+void
+MediaEngineWebrtcAudioSource::GetUUID(nsAString& aUUID)
+{
+ char deviceName[KMaxDeviceNameLength];
+ memset(deviceName, 0, KMaxDeviceNameLength);
+ char uniqueId[KMaxUniqueIdLength];
+ memset(uniqueId, 0, KMaxUniqueIdLength);
+ if(true == mInitDone )
+ {
+ mVoEHw->GetRecordingDeviceName(
+ mCapIndex, deviceName, uniqueId );
+ aUUID.Assign(NS_ConvertASCIItoUTF16(uniqueId));
+ }
+
+ return;
+}
+
+already_AddRefed<nsDOMMediaStream>
+MediaEngineWebrtcAudioSource::Allocate()
+{
+ if (mState != kReleased) {
+ return NULL;
+ }
+ // no special webrtc code to be done , i hope
+ mState = kAllocated;
+ return nsDOMMediaStream::CreateInputStream();
+}
+
+nsresult
+MediaEngineWebrtcAudioSource::Deallocate()
+{
+ if (mState != kStopped && mState != kAllocated) {
+ return NS_ERROR_FAILURE;
+ }
+ mState = kReleased;
+ return NS_OK;
+}
+
+//Loop back audio through media-stream
+nsresult
+MediaEngineWebrtcAudioSource::Start(SourceMediaStream* aStream, TrackID aID)
+{
+ const int DEFAULT_PORT = 55555;
+
+ if (false == mInitDone || mState != kAllocated) {
+ return NULL;
+ }
+
+ if(!aStream)
+ return NULL;
+
+ mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
+ if (!mTimer) {
+ return NULL;
+ }
+
+ mSource = aStream;
+
+ AudioSegment* segment = new AudioSegment();
+ segment->Init(CHANNELS);
+ segment->InsertNullDataAtStart(1);
+ mSource->AddTrack(aID, PLAYOUT_SAMPLE_FREQUENCY, 0, segment);
+ mSource->AdvanceKnownTracksTime(STREAM_TIME_MAX);
+ mTrackID = aID;
+
+ printf("\n Starting the audio engine ");
+ mVoEBase->SetLocalReceiver(mChannel,DEFAULT_PORT);
+ mVoEBase->SetSendDestination(mChannel,DEFAULT_PORT,"127.0.0.1");
+
+ if(-1 == mVoEXmedia->SetExternalPlayoutStatus(true))
+ return NULL;
+
+ mVoEBase->StartPlayout(mChannel);
+ mVoEBase->StartSend(mChannel);
+ mVoEBase->StartReceive(mChannel);
+
+ mState = kStarted;
+
+ // call every 10 milliseconds
+ mTimer->InitWithCallback(this, 10, nsITimer::TYPE_REPEATING_SLACK);
+
+ return NS_OK;
+}
+
+nsresult
+MediaEngineWebrtcAudioSource::Stop()
+{
+ if (mState != kStarted) {
+ return NS_ERROR_FAILURE;
+ }
+ if (!mTimer) {
+ return NS_ERROR_FAILURE;
+ }
+
+ if(!mVoEBase) {
+ return NS_ERROR_FAILURE;
+ }
+
+ mVoEBase->StopReceive(mChannel);
+ mVoEBase->StopSend(mChannel);
+ mVoEBase->StopPlayout(mChannel);
+
+ mTimer->Cancel();
+ mTimer = NULL;
+ mState = kStopped;
+ return NS_OK;
+}
+
+void
+MediaEngineWebrtcAudioSource::Shutdown()
+{
+ // Don't clean the mVoiceEngine here ...
+ // it is done in MediaEngine class
+
+ if(false == mInitDone)
+ return;
+
+ if(kStarted == mState)
+ {
+ //Stop External playout
+ mVoEBase->StopReceive(mChannel);
+ mVoEBase->StopSend(mChannel);
+ mVoEBase->StopPlayout(mChannel);
+ mTimer->Cancel();
+ mTimer = NULL;
+ }
+
+ mVoEBase->Terminate();
+ mVoEXmedia->Release();
+ mVoEHw->Release();
+ mVoEBase->Release();
+ mState = kReleased;
+ mInitDone = false;
+
+}
+
+//XXX: Array size - make it more smarter than just 160
+//XXX: Improve this function
+
+NS_IMETHODIMP
+MediaEngineWebrtcAudioSource::Notify(nsITimer* aTimer)
+{
+ // just one audio sample
+ const int AUDIO_SAMPLE_LENGTH = 160;
+ static int16_t audio10ms[AUDIO_SAMPLE_LENGTH];
+ int sample_length =0;
+
+ memset(audio10ms, 0, AUDIO_SAMPLE_LENGTH * sizeof(short));
+
+ mVoEXmedia->ExternalPlayoutGetData(audio10ms, PLAYOUT_SAMPLE_FREQUENCY, 100, sample_length);
+ if(sample_length == 0)
+ {
+ printf("\n Audio Sample lenth is 0 ");
+ return NS_OK;
+ }
+
+ // allocate shared buffer of lenght bytes
+ int buff_size = AUDIO_SAMPLE_LENGTH * sizeof(short);
+ nsRefPtr<SharedBuffer> buffer = SharedBuffer::Create(buff_size);
+ buffer->AddRef();
+ int16_t* dest = static_cast<int16_t*>(buffer->Data());
+ for(int i=0; i < sample_length; i++)
+ {
+ dest[i] = audio10ms[i];
+ }
+
+ mAudioSegment.AppendFrames(buffer.forget(), sample_length, 0, sample_length, nsAudioStream::FORMAT_S16_LE);
+ mSource->AppendToTrack(mTrackID, &mAudioSegment);
+ return NS_OK;
+}
+
+//XXX: Not multi-threded and non-renterant -???
+void
+MediaEngineWebrtc::EnumerateVideoDevices(nsTArray<nsRefPtr<MediaEngineVideoSource> >* aVSources)
+{
+ int error = 0;
+ printf("\n EnumerateVideo Devices ");
+
+ //we can use it function locals .. no need to save it in the class
+ webrtc::ViEBase* ptrViEBase;
+ webrtc::ViECapture* ptrViECapture;
+
+ if(NULL == mVideoEngine)
+ {
+ mVideoEngine = webrtc::VideoEngine::Create();
+ if (NULL == mVideoEngine )
+ {
+ printf( "ERROR in VideoEngine::Create\n");
+ return;
+ }
+ }
+
+ // te should have VideoEngine Created here
+ ptrViEBase = webrtc::ViEBase::GetInterface(mVideoEngine);
+ if (NULL == ptrViEBase) {
+ return;
+ }
+
+
+ if( false == mVideoEngineInit)
+ {
+ error = ptrViEBase->Init();
+ if (-1 == error ) {
+ printf( "ERROR in VideoEngine::Init\n");
+ return;
+ }
+ mVideoEngineInit = true;
+ }
+
+ ptrViECapture = webrtc::ViECapture::GetInterface(mVideoEngine);
+ if (NULL == ptrViECapture ) {
+ printf("ERROR in ViECapture::GetInterface\n");
+ return;
+ }
+
+ int num = ptrViECapture->NumberOfCaptureDevices();
+ if (num <= 0) {
+ printf( "ERROR no video devices found\n");
+ return;
+ } else {
+ printf("GetUserMedia:: Found %d devices!\n", num);
+ }
+
+ for(int i=0; i < num; i++)
+ {
+ //let's create VideoSouce objects
+ aVSources->AppendElement( new MediaEngineWebrtcVideoSource(mVideoEngine , i));
+ }
+
+ // safe to release local interfaces on the engine
+ ptrViEBase->Release();
+ ptrViECapture->Release();
+
+ return;
+}
+
+void
+MediaEngineWebrtc::EnumerateAudioDevices(nsTArray<nsRefPtr<MediaEngineAudioSource> >* aASources)
+{
+
+ // Be sure to release these at the end of the function
+ // safety measure though
+ webrtc::VoEBase* ptrVoEBase = NULL;
+ webrtc::VoEHardware* ptrVoEHw = NULL;
+ int error = 0;
+ printf(" Enumerate Audio Devices ");
+
+ if(NULL == mVoiceEngine)
+ {
+ mVoiceEngine = webrtc::VoiceEngine::Create();
+ if(NULL == mVoiceEngine)
+ {
+ printf(" Unable to create voice engine ");
+ return;
+ }
+ }
+
+ // all interfaces pointers are ref-counted
+ // we should be good on mutiple calls to this function ..
+ ptrVoEBase = webrtc::VoEBase::GetInterface( mVoiceEngine );
+ if(NULL == ptrVoEBase)
+ {
+ printf(" VoEBase creation failed ");
+ return;
+ }
+
+ //Init the voice library for the common parts
+ if(false == mAudioEngineInit)
+ {
+ error = ptrVoEBase->Init();
+ if(-1 == error)
+ {
+ printf("\n Audio Engine Init Failed ");
+ }
+ mAudioEngineInit = true;
+ }
+
+ ptrVoEHw = webrtc::VoEHardware::GetInterface(mVoiceEngine);
+ if(NULL == ptrVoEHw)
+ {
+ printf("\n Unable to get Audio Hardware Interface ");
+ return;
+ }
+
+ //let's enumerate audio input devices
+ int nDevices = 0;
+ error = ptrVoEHw->GetNumOfRecordingDevices(nDevices);
+ for(int i = 0; i < nDevices; i++)
+ {
+ char deviceName[128];
+ memset(deviceName, 0, 128);
+ char uniqueId[128];
+ memset(uniqueId, 0, 128);
+ ptrVoEHw->GetRecordingDeviceName(i, deviceName, uniqueId );
+ aASources->AppendElement(new MediaEngineWebrtcAudioSource(mVoiceEngine,i));
+ }
+
+ // releasing them to re-set the reference count ...
+ ptrVoEHw->Release();
+ ptrVoEBase->Release();
+
+}
+
+
+void
+MediaEngineWebrtc::Shutdown()
+{
+
+ //delete video engine
+ if(mVideoEngine)
+ if(false == webrtc::VideoEngine::Delete(mVideoEngine))
+ printf("\n Video Engine Delete Failed ");
+
+ //delete audio engine
+ if(mVoiceEngine)
+ if(false == webrtc::VoiceEngine::Delete(mVoiceEngine))
+ printf("\n Audio Engine Delete Failed ");
+}
+
+}
diff --git a/content/media/MediaEngineWebrtc.h b/content/media/MediaEngineWebrtc.h
new file mode 100644
--- /dev/null
+++ b/content/media/MediaEngineWebrtc.h
@@ -0,0 +1,257 @@
+/* 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 MEDIAENGINEWEBRTC_H_
+#define MEDIAENGINEWEBRTC_H_
+
+#include "prmem.h"
+#include "nsITimer.h"
+#include "nsIThread.h"
+#include "nsIRunnable.h"
+
+
+#include "nsCOMPtr.h"
+#include "nsDOMMediaStream.h"
+#include "nsComponentManagerUtils.h"
+
+#include "prthread.h"
+#include "nsThreadUtils.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"
+
+//Webrtc Inlcudes - GUM Specific
+// Audio Engine Includes
+#include "voice_engine/main/interface/voe_base.h"
+#include "voice_engine/main/interface/voe_hardware.h"
+#include "voice_engine/main/interface/voe_audio_processing.h"
+#include "voice_engine/main/interface/voe_volume_control.h"
+#include "voice_engine/main/interface/voe_external_media.h"
+// Video Engine Includes
+#include "video_engine/include/vie_base.h"
+#include "video_engine/include/vie_codec.h"
+#include "video_engine/include/vie_render.h"
+#include "video_engine/include/vie_capture.h"
+
+
+
+namespace mozilla {
+
+/**
+ * The webrtc implementation of the MediaEngine interface.
+ */
+
+enum WebrtcEngineState {
+ kAllocated,
+ kStarted,
+ kStopped,
+ kReleased,
+};
+
+//Foreward declarations
+class VideoStartCaptureEvent;
+class VideoRenderToStreamEvent;
+
+//Webrtc Video Subsystem abstraction
+
+class MediaEngineWebrtcVideoSource : public nsITimerCallback,
+ public MediaEngineVideoSource,
+ public webrtc::ExternalRenderer
+
+{
+public:
+
+ // ViEExternalRenderer implementation
+ virtual int FrameSizeChange(
+ unsigned int, unsigned int, unsigned int
+ );
+
+ virtual int DeliverFrame(
+ unsigned char*,int, uint32_t , int64_t
+ );
+
+ explicit MediaEngineWebrtcVideoSource(webrtc::VideoEngine* videoEnginePtr,
+ int index, int aFps = 30)
+ : mTimer(nsnull),
+ mVideoCaptureThread(nsnull),
+ mVideoRenderingThread(nsnull),
+ mVideoEngine(videoEnginePtr),
+ mCapIndex(index),
+ mWidth(352),
+ mHeight(288),
+ mState(kReleased),
+ mMonitor("WebrtcCamera.Monitor"),
+ mFps(aFps),
+ mInitDone(false) {
+ //XXX: eventually move this out of constructor
+ Init();
+ }
+
+ ~MediaEngineWebrtcVideoSource()
+ {
+ printf("\n Video Source Destructor Invoked ");
+ 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:
+
+
+ nsCOMPtr<nsITimer> mTimer; // not used
+ nsCOMPtr<nsIThread> mVideoCaptureThread; // QT Run loop for capture needs a thread
+ nsCOMPtr<nsIThread> mVideoRenderingThread; // in video-frames to media-stream
+
+private:
+ friend class VideoStartCaptureEvent;
+ friend class VideoRenderToStreamEvent;
+
+ //statics
+ static const unsigned int KMaxDeviceNameLength;
+ static const unsigned int KMaxUniqueIdLength;
+
+ // Initialize the needed Video engine interfaces
+ void Init();
+ void Shutdown();
+
+ //GUM needs