|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "CameraPreviewMediaStream.h" |
|
7 |
|
8 using namespace mozilla::layers; |
|
9 using namespace mozilla::dom; |
|
10 |
|
11 namespace mozilla { |
|
12 |
|
13 CameraPreviewMediaStream::CameraPreviewMediaStream(DOMMediaStream* aWrapper) |
|
14 : MediaStream(aWrapper) |
|
15 , mMutex("mozilla::camera::CameraPreviewMediaStream") |
|
16 , mFrameCallback(nullptr) |
|
17 { |
|
18 SetGraphImpl(MediaStreamGraph::GetInstance()); |
|
19 mIsConsumed = false; |
|
20 } |
|
21 |
|
22 void |
|
23 CameraPreviewMediaStream::AddAudioOutput(void* aKey) |
|
24 { |
|
25 } |
|
26 |
|
27 void |
|
28 CameraPreviewMediaStream::SetAudioOutputVolume(void* aKey, float aVolume) |
|
29 { |
|
30 } |
|
31 |
|
32 void |
|
33 CameraPreviewMediaStream::RemoveAudioOutput(void* aKey) |
|
34 { |
|
35 } |
|
36 |
|
37 void |
|
38 CameraPreviewMediaStream::AddVideoOutput(VideoFrameContainer* aContainer) |
|
39 { |
|
40 MutexAutoLock lock(mMutex); |
|
41 nsRefPtr<VideoFrameContainer> container = aContainer; |
|
42 AddVideoOutputImpl(container.forget()); |
|
43 |
|
44 if (mVideoOutputs.Length() > 1) { |
|
45 return; |
|
46 } |
|
47 MediaStreamGraph* gm = MediaStreamGraph::GetInstance(); |
|
48 mIsConsumed = true; |
|
49 for (uint32_t j = 0; j < mListeners.Length(); ++j) { |
|
50 MediaStreamListener* l = mListeners[j]; |
|
51 l->NotifyConsumptionChanged(gm, MediaStreamListener::CONSUMED); |
|
52 } |
|
53 } |
|
54 |
|
55 void |
|
56 CameraPreviewMediaStream::RemoveVideoOutput(VideoFrameContainer* aContainer) |
|
57 { |
|
58 MutexAutoLock lock(mMutex); |
|
59 RemoveVideoOutputImpl(aContainer); |
|
60 |
|
61 if (!mVideoOutputs.IsEmpty()) { |
|
62 return; |
|
63 } |
|
64 MediaStreamGraph* gm = MediaStreamGraph::GetInstance(); |
|
65 mIsConsumed = false; |
|
66 for (uint32_t j = 0; j < mListeners.Length(); ++j) { |
|
67 MediaStreamListener* l = mListeners[j]; |
|
68 l->NotifyConsumptionChanged(gm, MediaStreamListener::NOT_CONSUMED); |
|
69 } |
|
70 } |
|
71 |
|
72 void |
|
73 CameraPreviewMediaStream::ChangeExplicitBlockerCount(int32_t aDelta) |
|
74 { |
|
75 } |
|
76 |
|
77 void |
|
78 CameraPreviewMediaStream::AddListener(MediaStreamListener* aListener) |
|
79 { |
|
80 MutexAutoLock lock(mMutex); |
|
81 |
|
82 MediaStreamGraph* gm = MediaStreamGraph::GetInstance(); |
|
83 MediaStreamListener* listener = *mListeners.AppendElement() = aListener; |
|
84 listener->NotifyBlockingChanged(gm, MediaStreamListener::UNBLOCKED); |
|
85 } |
|
86 |
|
87 void |
|
88 CameraPreviewMediaStream::RemoveListener(MediaStreamListener* aListener) |
|
89 { |
|
90 MutexAutoLock lock(mMutex); |
|
91 |
|
92 MediaStreamGraph* gm = MediaStreamGraph::GetInstance(); |
|
93 nsRefPtr<MediaStreamListener> listener(aListener); |
|
94 mListeners.RemoveElement(aListener); |
|
95 listener->NotifyRemoved(gm); |
|
96 } |
|
97 |
|
98 void |
|
99 CameraPreviewMediaStream::Destroy() |
|
100 { |
|
101 MutexAutoLock lock(mMutex); |
|
102 DestroyImpl(); |
|
103 } |
|
104 |
|
105 void |
|
106 CameraPreviewMediaStream::SetCurrentFrame(const gfxIntSize& aIntrinsicSize, Image* aImage) |
|
107 { |
|
108 MutexAutoLock lock(mMutex); |
|
109 |
|
110 TimeStamp now = TimeStamp::Now(); |
|
111 for (uint32_t i = 0; i < mVideoOutputs.Length(); ++i) { |
|
112 VideoFrameContainer* output = mVideoOutputs[i]; |
|
113 output->SetCurrentFrame(aIntrinsicSize, aImage, now); |
|
114 nsCOMPtr<nsIRunnable> event = |
|
115 NS_NewRunnableMethod(output, &VideoFrameContainer::Invalidate); |
|
116 NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); |
|
117 } |
|
118 |
|
119 if (mFrameCallback) { |
|
120 mFrameCallback->OnNewFrame(aIntrinsicSize, aImage); |
|
121 } |
|
122 } |
|
123 |
|
124 void |
|
125 CameraPreviewMediaStream::ClearCurrentFrame() |
|
126 { |
|
127 MutexAutoLock lock(mMutex); |
|
128 |
|
129 for (uint32_t i = 0; i < mVideoOutputs.Length(); ++i) { |
|
130 VideoFrameContainer* output = mVideoOutputs[i]; |
|
131 output->ClearCurrentFrame(); |
|
132 nsCOMPtr<nsIRunnable> event = |
|
133 NS_NewRunnableMethod(output, &VideoFrameContainer::Invalidate); |
|
134 NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); |
|
135 } |
|
136 } |
|
137 |
|
138 } |