forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TVTuner.cpp
222 lines (183 loc) · 5.66 KB
/
TVTuner.cpp
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "DOMMediaStream.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/TVCurrentSourceChangedEvent.h"
#include "mozilla/dom/TVServiceCallbacks.h"
#include "mozilla/dom/TVServiceFactory.h"
#include "mozilla/dom/TVSource.h"
#include "mozilla/dom/TVUtils.h"
#include "nsISupportsPrimitives.h"
#include "nsITVService.h"
#include "nsServiceManagerUtils.h"
#include "TVTuner.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(TVTuner, DOMEventTargetHelper,
mTVService, mStream, mCurrentSource, mSources)
NS_IMPL_ADDREF_INHERITED(TVTuner, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(TVTuner, DOMEventTargetHelper)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TVTuner)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
TVTuner::TVTuner(nsPIDOMWindow* aWindow)
: DOMEventTargetHelper(aWindow)
{
}
TVTuner::~TVTuner()
{
}
/* static */ already_AddRefed<TVTuner>
TVTuner::Create(nsPIDOMWindow* aWindow,
nsITVTunerData* aData)
{
nsRefPtr<TVTuner> tuner = new TVTuner(aWindow);
return (tuner->Init(aData)) ? tuner.forget() : nullptr;
}
bool
TVTuner::Init(nsITVTunerData* aData)
{
NS_ENSURE_TRUE(aData, false);
nsresult rv = aData->GetId(mId);
NS_ENSURE_SUCCESS(rv, false);
if (NS_WARN_IF(mId.IsEmpty())) {
return false;
}
uint32_t count;
char** supportedSourceTypes;
rv = aData->GetSupportedSourceTypes(&count, &supportedSourceTypes);
NS_ENSURE_SUCCESS(rv, false);
for (uint32_t i = 0; i < count; i++) {
TVSourceType sourceType = ToTVSourceType(supportedSourceTypes[i]);
if (NS_WARN_IF(sourceType == TVSourceType::EndGuard_)) {
continue;
}
// Generate the source instance based on the supported source type.
nsRefPtr<TVSource> source = TVSource::Create(GetOwner(), sourceType, this);
if (NS_WARN_IF(!source)) {
continue;
}
mSupportedSourceTypes.AppendElement(sourceType);
mSources.AppendElement(source);
}
NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(count, supportedSourceTypes);
mTVService = TVServiceFactory::AutoCreateTVService();
NS_ENSURE_TRUE(mTVService, false);
return true;
}
/* virtual */ JSObject*
TVTuner::WrapObject(JSContext* aCx)
{
return TVTunerBinding::Wrap(aCx, this);
}
nsresult
TVTuner::SetCurrentSource(TVSourceType aSourceType)
{
ErrorResult error;
if (mCurrentSource) {
if (aSourceType == mCurrentSource->Type()) {
// No actual change.
return NS_OK;
}
// No need to stay tuned for non-current sources.
nsresult rv = mCurrentSource->UnsetCurrentChannel();
if (NS_FAILED(rv)) {
return rv;
}
}
for (uint32_t i = 0; i < mSources.Length(); i++) {
if (aSourceType == mSources[i]->Type()) {
mCurrentSource = mSources[i];
break;
}
}
nsresult rv = InitMediaStream();
if (NS_FAILED(rv)) {
return rv;
}
return DispatchCurrentSourceChangedEvent(mCurrentSource);
}
nsresult
TVTuner::DispatchTVEvent(nsIDOMEvent* aEvent)
{
return DispatchTrustedEvent(aEvent);
}
void
TVTuner::GetSupportedSourceTypes(nsTArray<TVSourceType>& aSourceTypes,
ErrorResult& aRv) const
{
aSourceTypes = mSupportedSourceTypes;
}
already_AddRefed<Promise>
TVTuner::GetSources(ErrorResult& aRv)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
MOZ_ASSERT(global);
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
promise->MaybeResolve(mSources);
return promise.forget();
}
already_AddRefed<Promise>
TVTuner::SetCurrentSource(const TVSourceType aSourceType, ErrorResult& aRv)
{
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
MOZ_ASSERT(global);
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
// |SetCurrentSource(const TVSourceType)| will be called once |notifySuccess|
// of the callback is invoked.
nsCOMPtr<nsITVServiceCallback> callback =
new TVServiceSourceSetterCallback(this, promise, aSourceType);
nsresult rv = mTVService->SetSource(mId, ToTVSourceTypeStr(aSourceType), callback);
if (NS_WARN_IF(NS_FAILED(rv))) {
promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
}
return promise.forget();
}
void
TVTuner::GetId(nsAString& aId) const
{
aId = mId;
}
already_AddRefed<TVSource>
TVTuner::GetCurrentSource() const
{
nsRefPtr<TVSource> currentSource = mCurrentSource;
return currentSource.forget();
}
already_AddRefed<DOMMediaStream>
TVTuner::GetStream() const
{
nsRefPtr<DOMMediaStream> stream = mStream;
return stream.forget();
}
nsresult
TVTuner::InitMediaStream()
{
// TODO Instantiate |mStream| when bug 987498 is done.
return NS_OK;
}
nsresult
TVTuner::DispatchCurrentSourceChangedEvent(TVSource* aSource)
{
TVCurrentSourceChangedEventInit init;
init.mSource = aSource;
nsCOMPtr<nsIDOMEvent> event =
TVCurrentSourceChangedEvent::Constructor(this,
NS_LITERAL_STRING("currentsourcechanged"),
init);
nsCOMPtr<nsIRunnable> runnable =
NS_NewRunnableMethodWithArg<nsCOMPtr<nsIDOMEvent>>(this,
&TVTuner::DispatchTVEvent,
event);
return NS_DispatchToCurrentThread(runnable);
}
} // namespace dom
} // namespace mozilla