SDPLoader.cpp revision b4a7a2df4c28c3f32b5d877b54831d2cc5d78f81
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 "include/SDPLoader.h" 22 23#include "ASessionDescription.h" 24 25#include <media/IMediaHTTPConnection.h> 26#include <media/IMediaHTTPService.h> 27#include <media/stagefright/MediaHTTP.h> 28#include <media/stagefright/foundation/ABuffer.h> 29#include <media/stagefright/foundation/ADebug.h> 30 31#define DEFAULT_SDP_SIZE 100000 32 33namespace android { 34 35SDPLoader::SDPLoader( 36 const sp<AMessage> ¬ify, 37 uint32_t flags, 38 const sp<IMediaHTTPService> &httpService) 39 : mNotify(notify), 40 mFlags(flags), 41 mNetLooper(new ALooper), 42 mCancelled(false), 43 mHTTPDataSource(new MediaHTTP(httpService->makeHTTPConnection())) { 44 mNetLooper->setName("sdp net"); 45 mNetLooper->start(false /* runOnCallingThread */, 46 false /* canCallJava */, 47 PRIORITY_HIGHEST); 48} 49 50void SDPLoader::load(const char *url, const KeyedVector<String8, String8> *headers) { 51 mNetLooper->registerHandler(this); 52 53 sp<AMessage> msg = new AMessage(kWhatLoad, id()); 54 msg->setString("url", url); 55 56 if (headers != NULL) { 57 msg->setPointer( 58 "headers", 59 new KeyedVector<String8, String8>(*headers)); 60 } 61 62 msg->post(); 63} 64 65void SDPLoader::cancel() { 66 mCancelled = true; 67 sp<HTTPBase> HTTPDataSource = mHTTPDataSource; 68 HTTPDataSource->disconnect(); 69} 70 71void SDPLoader::onMessageReceived(const sp<AMessage> &msg) { 72 switch (msg->what()) { 73 case kWhatLoad: 74 onLoad(msg); 75 break; 76 77 default: 78 TRESPASS(); 79 break; 80 } 81} 82 83void SDPLoader::onLoad(const sp<AMessage> &msg) { 84 status_t err = OK; 85 sp<ASessionDescription> desc = NULL; 86 AString url; 87 CHECK(msg->findString("url", &url)); 88 89 KeyedVector<String8, String8> *headers = NULL; 90 msg->findPointer("headers", (void **)&headers); 91 92 if (!(mFlags & kFlagIncognito)) { 93 ALOGV("onLoad '%s'", url.c_str()); 94 } else { 95 ALOGI("onLoad <URL suppressed>"); 96 } 97 98 if (!mCancelled) { 99 err = mHTTPDataSource->connect(url.c_str(), headers); 100 101 if (err != OK) { 102 ALOGE("connect() returned %d", err); 103 } 104 } 105 106 if (headers != NULL) { 107 delete headers; 108 headers = NULL; 109 } 110 111 off64_t sdpSize; 112 if (err == OK && !mCancelled) { 113 err = mHTTPDataSource->getSize(&sdpSize); 114 115 if (err != OK) { 116 //We did not get the size of the sdp file, default to a large value 117 sdpSize = DEFAULT_SDP_SIZE; 118 err = OK; 119 } 120 } 121 122 sp<ABuffer> buffer = new ABuffer(sdpSize); 123 124 if (err == OK && !mCancelled) { 125 ssize_t readSize = mHTTPDataSource->readAt(0, buffer->data(), sdpSize); 126 127 if (readSize < 0) { 128 ALOGE("Failed to read SDP, error code = %zu", readSize); 129 err = UNKNOWN_ERROR; 130 } else { 131 desc = new ASessionDescription; 132 133 if (desc == NULL || !desc->setTo(buffer->data(), (size_t)readSize)) { 134 err = UNKNOWN_ERROR; 135 ALOGE("Failed to parse SDP"); 136 } 137 } 138 } 139 140 mHTTPDataSource.clear(); 141 142 sp<AMessage> notify = mNotify->dup(); 143 notify->setInt32("what", kWhatSDPLoaded); 144 notify->setInt32("result", err); 145 notify->setObject("description", desc); 146 notify->post(); 147} 148 149} // namespace android 150