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