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