|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set sw=2 ts=8 et tw=80 : */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "RtspControllerParent.h" |
|
8 #include "RtspController.h" |
|
9 #include "nsIAuthPromptProvider.h" |
|
10 #include "nsThreadUtils.h" |
|
11 #include "nsProxyRelease.h" |
|
12 #include "mozilla/ipc/InputStreamUtils.h" |
|
13 #include "mozilla/ipc/URIUtils.h" |
|
14 #include "mozilla/unused.h" |
|
15 #include "nsNetUtil.h" |
|
16 #include "prlog.h" |
|
17 |
|
18 #include <sys/types.h> |
|
19 #include <sys/socket.h> |
|
20 |
|
21 PRLogModuleInfo* gRtspLog; |
|
22 #undef LOG |
|
23 #define LOG(args) PR_LOG(gRtspLog, PR_LOG_DEBUG, args) |
|
24 |
|
25 #define SEND_DISCONNECT_IF_ERROR(rv) \ |
|
26 if (NS_FAILED(rv) && mIPCOpen && mTotalTracks > 0ul) { \ |
|
27 for (uint32_t i = 0; i < mTotalTracks; i++) { \ |
|
28 unused << SendOnDisconnected(i, rv); \ |
|
29 } \ |
|
30 } |
|
31 |
|
32 using namespace mozilla::ipc; |
|
33 |
|
34 namespace mozilla { |
|
35 namespace net { |
|
36 |
|
37 void |
|
38 RtspControllerParent::Destroy() |
|
39 { |
|
40 // If we're being destroyed on a non-main thread, we AddRef again and use a |
|
41 // proxy to release the RtspControllerParent on the main thread, where the |
|
42 // RtspControllerParent is deleted. This ensures we only delete the |
|
43 // RtspControllerParent on the main thread. |
|
44 if (!NS_IsMainThread()) { |
|
45 nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
|
46 NS_ENSURE_TRUE_VOID(mainThread); |
|
47 nsRefPtr<RtspControllerParent> doomed(this); |
|
48 if (NS_FAILED(NS_ProxyRelease(mainThread, |
|
49 static_cast<nsIStreamingProtocolListener*>(doomed), true))) { |
|
50 NS_WARNING("Failed to proxy release to main thread!"); |
|
51 } |
|
52 } else { |
|
53 delete this; |
|
54 } |
|
55 } |
|
56 |
|
57 NS_IMPL_ADDREF(RtspControllerParent) |
|
58 NS_IMPL_RELEASE_WITH_DESTROY(RtspControllerParent, Destroy()) |
|
59 NS_IMPL_QUERY_INTERFACE(RtspControllerParent, |
|
60 nsIInterfaceRequestor, |
|
61 nsIStreamingProtocolListener) |
|
62 |
|
63 RtspControllerParent::RtspControllerParent() |
|
64 : mIPCOpen(true) |
|
65 , mTotalTracks(0) |
|
66 { |
|
67 #if defined(PR_LOGGING) |
|
68 if (!gRtspLog) |
|
69 gRtspLog = PR_NewLogModule("nsRtsp"); |
|
70 #endif |
|
71 } |
|
72 |
|
73 RtspControllerParent::~RtspControllerParent() |
|
74 { |
|
75 } |
|
76 |
|
77 void |
|
78 RtspControllerParent::ActorDestroy(ActorDestroyReason why) |
|
79 { |
|
80 LOG(("RtspControllerParent::ActorDestroy()")); |
|
81 mIPCOpen = false; |
|
82 |
|
83 NS_ENSURE_TRUE_VOID(mController); |
|
84 if (mController) { |
|
85 mController->Stop(); |
|
86 mController = nullptr; |
|
87 } |
|
88 } |
|
89 |
|
90 bool |
|
91 RtspControllerParent::RecvAsyncOpen(const URIParams& aURI) |
|
92 { |
|
93 LOG(("RtspControllerParent::RecvAsyncOpen()")); |
|
94 |
|
95 mURI = DeserializeURI(aURI); |
|
96 |
|
97 mController = new RtspController(nullptr); |
|
98 mController->Init(mURI); |
|
99 nsresult rv = mController->AsyncOpen(this); |
|
100 if (NS_SUCCEEDED(rv)) return true; |
|
101 |
|
102 mController = nullptr; |
|
103 return SendAsyncOpenFailed(rv); |
|
104 } |
|
105 |
|
106 bool |
|
107 RtspControllerParent::RecvPlay() |
|
108 { |
|
109 LOG(("RtspControllerParent::RecvPlay()")); |
|
110 NS_ENSURE_TRUE(mController, true); |
|
111 |
|
112 nsresult rv = mController->Play(); |
|
113 SEND_DISCONNECT_IF_ERROR(rv) |
|
114 return true; |
|
115 } |
|
116 |
|
117 bool |
|
118 RtspControllerParent::RecvPause() |
|
119 { |
|
120 LOG(("RtspControllerParent::RecvPause()")); |
|
121 NS_ENSURE_TRUE(mController, true); |
|
122 |
|
123 nsresult rv = mController->Pause(); |
|
124 SEND_DISCONNECT_IF_ERROR(rv) |
|
125 return true; |
|
126 } |
|
127 |
|
128 bool |
|
129 RtspControllerParent::RecvResume() |
|
130 { |
|
131 LOG(("RtspControllerParent::RecvResume()")); |
|
132 NS_ENSURE_TRUE(mController, true); |
|
133 |
|
134 nsresult rv = mController->Resume(); |
|
135 SEND_DISCONNECT_IF_ERROR(rv) |
|
136 return true; |
|
137 } |
|
138 |
|
139 bool |
|
140 RtspControllerParent::RecvSuspend() |
|
141 { |
|
142 LOG(("RtspControllerParent::RecvSuspend()")); |
|
143 NS_ENSURE_TRUE(mController, true); |
|
144 |
|
145 nsresult rv = mController->Suspend(); |
|
146 SEND_DISCONNECT_IF_ERROR(rv) |
|
147 return true; |
|
148 } |
|
149 |
|
150 bool |
|
151 RtspControllerParent::RecvSeek(const uint64_t& offset) |
|
152 { |
|
153 LOG(("RtspControllerParent::RecvSeek()")); |
|
154 NS_ENSURE_TRUE(mController, true); |
|
155 |
|
156 nsresult rv = mController->Seek(offset); |
|
157 SEND_DISCONNECT_IF_ERROR(rv) |
|
158 return true; |
|
159 } |
|
160 |
|
161 bool |
|
162 RtspControllerParent::RecvStop() |
|
163 { |
|
164 LOG(("RtspControllerParent::RecvStop()")); |
|
165 NS_ENSURE_TRUE(mController, true); |
|
166 |
|
167 nsresult rv = mController->Stop(); |
|
168 NS_ENSURE_SUCCESS(rv, true); |
|
169 return true; |
|
170 } |
|
171 |
|
172 NS_IMETHODIMP |
|
173 RtspControllerParent::OnMediaDataAvailable(uint8_t index, |
|
174 const nsACString & data, |
|
175 uint32_t length, |
|
176 uint32_t offset, |
|
177 nsIStreamingProtocolMetaData *meta) |
|
178 { |
|
179 NS_ENSURE_ARG_POINTER(meta); |
|
180 uint32_t int32Value; |
|
181 uint64_t int64Value; |
|
182 |
|
183 nsresult rv = meta->GetTimeStamp(&int64Value); |
|
184 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
185 |
|
186 LOG(("RtspControllerParent:: OnMediaDataAvailable %d:%d time %lld", |
|
187 index, length, int64Value)); |
|
188 |
|
189 // Serialize meta data. |
|
190 nsCString name; |
|
191 name.AssignLiteral("TIMESTAMP"); |
|
192 InfallibleTArray<RtspMetadataParam> metaData; |
|
193 metaData.AppendElement(RtspMetadataParam(name, int64Value)); |
|
194 |
|
195 name.AssignLiteral("FRAMETYPE"); |
|
196 rv = meta->GetFrameType(&int32Value); |
|
197 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
198 metaData.AppendElement(RtspMetadataParam(name, int32Value)); |
|
199 |
|
200 nsCString stream; |
|
201 stream.Assign(data); |
|
202 if (!mIPCOpen || |
|
203 !SendOnMediaDataAvailable(index, stream, length, offset, metaData)) { |
|
204 return NS_ERROR_FAILURE; |
|
205 } |
|
206 return NS_OK; |
|
207 } |
|
208 |
|
209 NS_IMETHODIMP |
|
210 RtspControllerParent::OnConnected(uint8_t index, |
|
211 nsIStreamingProtocolMetaData *meta) |
|
212 { |
|
213 NS_ENSURE_ARG_POINTER(meta); |
|
214 uint32_t int32Value; |
|
215 uint64_t int64Value; |
|
216 |
|
217 LOG(("RtspControllerParent:: OnConnected")); |
|
218 // Serialize meta data. |
|
219 InfallibleTArray<RtspMetadataParam> metaData; |
|
220 nsCString name; |
|
221 name.AssignLiteral("TRACKS"); |
|
222 nsresult rv = meta->GetTotalTracks(&mTotalTracks); |
|
223 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
224 metaData.AppendElement(RtspMetadataParam(name, mTotalTracks)); |
|
225 |
|
226 name.AssignLiteral("MIMETYPE"); |
|
227 nsCString mimeType; |
|
228 rv = meta->GetMimeType(mimeType); |
|
229 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
230 metaData.AppendElement(RtspMetadataParam(name, mimeType)); |
|
231 |
|
232 name.AssignLiteral("WIDTH"); |
|
233 rv = meta->GetWidth(&int32Value); |
|
234 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
235 metaData.AppendElement(RtspMetadataParam(name, int32Value)); |
|
236 |
|
237 name.AssignLiteral("HEIGHT"); |
|
238 rv = meta->GetHeight(&int32Value); |
|
239 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
240 metaData.AppendElement(RtspMetadataParam(name, int32Value)); |
|
241 |
|
242 name.AssignLiteral("DURATION"); |
|
243 rv = meta->GetDuration(&int64Value); |
|
244 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
245 metaData.AppendElement(RtspMetadataParam(name, int64Value)); |
|
246 |
|
247 name.AssignLiteral("SAMPLERATE"); |
|
248 rv = meta->GetSampleRate(&int32Value); |
|
249 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
250 metaData.AppendElement(RtspMetadataParam(name, int32Value)); |
|
251 |
|
252 name.AssignLiteral("TIMESTAMP"); |
|
253 rv = meta->GetTimeStamp(&int64Value); |
|
254 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
255 metaData.AppendElement(RtspMetadataParam(name, int64Value)); |
|
256 |
|
257 name.AssignLiteral("CHANNELCOUNT"); |
|
258 rv = meta->GetChannelCount(&int32Value); |
|
259 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
260 metaData.AppendElement(RtspMetadataParam(name, int32Value)); |
|
261 |
|
262 nsCString esds; |
|
263 rv = meta->GetEsdsData(esds); |
|
264 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
265 name.AssignLiteral("ESDS"); |
|
266 metaData.AppendElement(RtspMetadataParam(name, esds)); |
|
267 |
|
268 nsCString avcc; |
|
269 rv = meta->GetAvccData(avcc); |
|
270 NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
|
271 name.AssignLiteral("AVCC"); |
|
272 metaData.AppendElement(RtspMetadataParam(name, avcc)); |
|
273 |
|
274 if (!mIPCOpen || !SendOnConnected(index, metaData)) { |
|
275 return NS_ERROR_FAILURE; |
|
276 } |
|
277 |
|
278 return NS_OK; |
|
279 } |
|
280 |
|
281 NS_IMETHODIMP |
|
282 RtspControllerParent::OnDisconnected(uint8_t index, |
|
283 nsresult reason) |
|
284 { |
|
285 LOG(("RtspControllerParent::OnDisconnected() for track %d reason = 0x%x", index, reason)); |
|
286 if (!mIPCOpen || !SendOnDisconnected(index, reason)) { |
|
287 return NS_ERROR_FAILURE; |
|
288 } |
|
289 if (mController) { |
|
290 mController = nullptr; |
|
291 } |
|
292 return NS_OK; |
|
293 } |
|
294 |
|
295 NS_IMETHODIMP |
|
296 RtspControllerParent::GetInterface(const nsIID & iid, void **result) |
|
297 { |
|
298 LOG(("RtspControllerParent::GetInterface()")); |
|
299 return QueryInterface(iid, result); |
|
300 } |
|
301 |
|
302 } // namespace net |
|
303 } // namespace mozilla |