• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "SDPLoader"
19 #include <utils/Log.h>
20 
21 #include "SDPLoader.h"
22 
23 #include "ASessionDescription.h"
24 #include "HTTPBase.h"
25 
26 #include <media/stagefright/foundation/ABuffer.h>
27 #include <media/stagefright/foundation/ADebug.h>
28 
29 #define DEFAULT_SDP_SIZE 100000
30 
31 namespace android {
32 
SDPLoader(const sp<AMessage> & notify,uint32_t flags,bool uidValid,uid_t uid)33 SDPLoader::SDPLoader(const sp<AMessage> &notify, uint32_t flags, bool uidValid, uid_t uid)
34     : mNotify(notify),
35       mFlags(flags),
36       mUIDValid(uidValid),
37       mUID(uid),
38       mNetLooper(new ALooper),
39       mCancelled(false),
40       mHTTPDataSource(
41               HTTPBase::Create(
42                   (mFlags & kFlagIncognito)
43                     ? HTTPBase::kFlagIncognito
44                     : 0)) {
45     if (mUIDValid) {
46         mHTTPDataSource->setUID(mUID);
47     }
48 
49     mNetLooper->setName("sdp net");
50     mNetLooper->start(false /* runOnCallingThread */,
51                       false /* canCallJava */,
52                       PRIORITY_HIGHEST);
53 }
54 
load(const char * url,const KeyedVector<String8,String8> * headers)55 void SDPLoader::load(const char *url, const KeyedVector<String8, String8> *headers) {
56     mNetLooper->registerHandler(this);
57 
58     sp<AMessage> msg = new AMessage(kWhatLoad, id());
59     msg->setString("url", url);
60 
61     if (headers != NULL) {
62         msg->setPointer(
63                 "headers",
64                 new KeyedVector<String8, String8>(*headers));
65     }
66 
67     msg->post();
68 }
69 
cancel()70 void SDPLoader::cancel() {
71     mCancelled = true;
72     sp<HTTPBase> HTTPDataSource = mHTTPDataSource;
73     HTTPDataSource->disconnect();
74 }
75 
onMessageReceived(const sp<AMessage> & msg)76 void SDPLoader::onMessageReceived(const sp<AMessage> &msg) {
77     switch (msg->what()) {
78         case kWhatLoad:
79             onLoad(msg);
80             break;
81 
82         default:
83             TRESPASS();
84             break;
85     }
86 }
87 
onLoad(const sp<AMessage> & msg)88 void SDPLoader::onLoad(const sp<AMessage> &msg) {
89     status_t err = OK;
90     sp<ASessionDescription> desc = NULL;
91     AString url;
92     CHECK(msg->findString("url", &url));
93 
94     KeyedVector<String8, String8> *headers = NULL;
95     msg->findPointer("headers", (void **)&headers);
96 
97     if (!(mFlags & kFlagIncognito)) {
98         ALOGI("onLoad '%s'", url.c_str());
99     } else {
100         ALOGI("onLoad <URL suppressed>");
101     }
102 
103     if (!mCancelled) {
104         err = mHTTPDataSource->connect(url.c_str(), headers);
105 
106         if (err != OK) {
107             ALOGE("connect() returned %d", err);
108         }
109     }
110 
111     if (headers != NULL) {
112         delete headers;
113         headers = NULL;
114     }
115 
116     off64_t sdpSize;
117     if (err == OK && !mCancelled) {
118         err = mHTTPDataSource->getSize(&sdpSize);
119 
120         if (err != OK) {
121             //We did not get the size of the sdp file, default to a large value
122             sdpSize = DEFAULT_SDP_SIZE;
123             err = OK;
124         }
125     }
126 
127     sp<ABuffer> buffer = new ABuffer(sdpSize);
128 
129     if (err == OK && !mCancelled) {
130         ssize_t readSize = mHTTPDataSource->readAt(0, buffer->data(), sdpSize);
131 
132         if (readSize < 0) {
133             ALOGE("Failed to read SDP, error code = %ld", readSize);
134             err = UNKNOWN_ERROR;
135         } else {
136             desc = new ASessionDescription;
137 
138             if (desc == NULL || !desc->setTo(buffer->data(), (size_t)readSize)) {
139                 err = UNKNOWN_ERROR;
140                 ALOGE("Failed to parse SDP");
141             }
142         }
143     }
144 
145     mHTTPDataSource.clear();
146 
147     sp<AMessage> notify = mNotify->dup();
148     notify->setInt32("what", kWhatSDPLoaded);
149     notify->setInt32("result", err);
150     notify->setObject("description", desc);
151     notify->post();
152 }
153 
154 }  // namespace android
155