AweSomePlayer http流读取过程


在AwesomePlayer::finishSetDataSource_l中,会先调用HTTPBase的创建方法创建一个HTTPBase对象


mConnectingDataSource = HTTPBase::Create(
                (mFlags & INCOGNITO)
                    ? HTTPBase::kFlagIncognito
                    : 0);


HTTPBase的Create方法实际上只会创建一种类型的对象,ChromiumHTTPDataSource


sp<HTTPBase> HTTPBase::Create(uint32_t flags) {
#if CHROMIUM_AVAILABLE
        HTTPBase *dataSource = createChromiumHTTPDataSource(flags);
        if (dataSource) {
           return dataSource;
        }
#endif
    {
        TRESPASS();


        return NULL;
    }
}


HTTPBase *createChromiumHTTPDataSource(uint32_t flags) {
    return new ChromiumHTTPDataSource(flags);
}


实际的连接和读取也不是在ChromiumHTTPDataSource这个类中完成的,而是通过其中的一个mDelegate成员来完成的。


ChromiumHTTPDataSource::ChromiumHTTPDataSource(uint32_t flags)
    : mFlags(flags),
      mState(DISCONNECTED),
      mDelegate(new SfDelegate),
      mCurrentOffset(0),
      mIOResult(OK),
      mContentSize(-1),
      mDecryptHandle(NULL),
      mDrmManagerClient(NULL) {
    mDelegate->setOwner(this);
}


比如,connect操作:
mDelegate->initiateConnection(mURI.c_str(), &mHeaders, offset);
read操作:
mDelegate->initiateRead(data, size);


因此,我们需要来看一下SfDelegate类。它的定义在libstagefright/chromium_http/support.cpp中,
先看一下建立连接的过程:


void SfDelegate::initiateConnection(
        const char *uri,
        const KeyedVector<String8, String8> *headers,
        off64_t offset) {
    GURL url(uri);


    MessageLoop *loop = gNetworkThread->message_loop();
    loop->PostTask(
            FROM_HERE,
            NewRunnableFunction(
                &SfDelegate::OnInitiateConnectionWrapper,
                this,
                url,
                headers,
                offset));


}


这是一个异步的操作,发送一个消息,其中gNetworkThread是一个单例,在第一次初始化时创建。实际的处理函数是静态函数SfDelegate::OnInitiateConnectionWrapper


// static
void SfDelegate::OnInitiateConnectionWrapper(
        SfDelegate *me, GURL url,
        const KeyedVector<String8, String8> *headers,
        off64_t offset) {
    me->onInitiateConnection(url, headers, offset);
}


我们看到这里实际上调用的是SfDelegate的onInitiateConnection方法:


void SfDelegate::onInitiateConnection(
        const GURL &url,
        const KeyedVector<String8, String8> *extra,
        off64_t offset) {
        
    mURLRequest = new net::URLRequest(url, this);
    
    ......
    
    mURLRequest->Start();
}


其中创建了一个net::URLRequest任务,并开始执行。


net::URLRequest定义在external/chromium/net/url_request/url_request.cc中


URLRequest::URLRequest(const GURL& url, Delegate* delegate)
    : url_chain_(1, url),
      method_("GET"),
      load_flags_(LOAD_NORMAL),
      delegate_(delegate),
      is_pending_(false),
      redirect_limit_(kMaxRedirects),
      final_upload_progress_(0),
      priority_(LOWEST),
      identifier_(GenerateURLRequestIdentifier()),
      ALLOW_THIS_IN_INITIALIZER_LIST(
          before_request_callback_(this, &URLRequest::BeforeRequestComplete)) {
  SIMPLE_STATS_COUNTER("URLRequestCount");


  // Sanity check out environment.
  DCHECK(MessageLoop::current()) <<
      "The current MessageLoop must exist";
  DCHECK_EQ(MessageLoop::TYPE_IO, MessageLoop::current()->type()) <<
      "The current MessageLoop must be TYPE_IO";
}


其中最重要的一点是将Delegate* delegate参数赋值给delegate_成员。
看一下其中的Start方法


void URLRequest::Start() {
  response_info_.request_time = Time::Now();


  // Only notify the delegate for the initial request.
  if (context_ && context_->network_delegate()) {
    if (context_->network_delegate()->NotifyBeforeURLRequest(
            this, &before_request_callback_, &delegate_redirect_url_) ==
            net::ERR_IO_PENDING) {
      net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_BLOCKED_ON_EXTENSION, NULL);
      return;  // paused
    }
  }


  StartInternal();
}


void URLRequest::StartInternal() {
  StartJob(URLRequestJobManager::GetInstance()->CreateJob(this));
}


这个方法的过程是先通过单例的工厂类URLRequestJobManager创建一个job,然后再开始这个job


void URLRequest::StartJob(URLRequestJob* job) {
    ......


  job_ = job;
  job_->SetExtraRequestHeaders(extra_request_headers_);


  if (upload_.get())
    job_->SetUpload(upload_.get());


  is_pending_ = true;


  response_info_.was_cached = false;


  // Don't allow errors to be sent from within Start().
  // TODO(brettw) this may cause NotifyDone to be sent synchronously,
  // we probably don't want this: they should be sent asynchronously so
  // the caller does not get reentered.
  job_->Start();
}
Job的类型是根据url的scheme来决定的,对于http来说,其工厂类为URLRequestHttpJob,因此,对应的job为URLRequestHttpJob


URLRequestJob* URLRequestHttpJob::Factory(URLRequest* request,
                                          const std::string& scheme) {
    ......
    
    return new URLRequestHttpJob(request);                                          
}


后面的事情是通过一套复杂的状态机来处理的,以后再分析



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值