HTTPLiveSource.cpp revision 14f7672b5d450ed26a06fd3bb3ce045ea78b11b2
1/*
2 * Copyright (C) 2010 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 "HTTPLiveSource"
19#include <utils/Log.h>
20
21#include "HTTPLiveSource.h"
22
23#include "AnotherPacketSource.h"
24#include "LiveDataSource.h"
25#include "LiveSession.h"
26
27#include <media/stagefright/foundation/ABuffer.h>
28#include <media/stagefright/foundation/ADebug.h>
29#include <media/stagefright/foundation/AMessage.h>
30#include <media/stagefright/MediaErrors.h>
31#include <media/stagefright/MetaData.h>
32
33namespace android {
34
35NuPlayer::HTTPLiveSource::HTTPLiveSource(
36        const sp<AMessage> &notify,
37        const char *url,
38        const KeyedVector<String8, String8> *headers,
39        bool uidValid, uid_t uid)
40    : Source(notify),
41      mURL(url),
42      mUIDValid(uidValid),
43      mUID(uid),
44      mFlags(0),
45      mFinalResult(OK),
46      mOffset(0) {
47    if (headers) {
48        mExtraHeaders = *headers;
49
50        ssize_t index =
51            mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
52
53        if (index >= 0) {
54            mFlags |= kFlagIncognito;
55
56            mExtraHeaders.removeItemsAt(index);
57        }
58    }
59}
60
61NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
62    if (mLiveSession != NULL) {
63        mLiveSession->disconnect();
64        mLiveSession.clear();
65
66        mLiveLooper->stop();
67        mLiveLooper.clear();
68    }
69}
70
71void NuPlayer::HTTPLiveSource::prepareAsync() {
72    mLiveLooper = new ALooper;
73    mLiveLooper->setName("http live");
74    mLiveLooper->start();
75
76    sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
77
78    mLiveSession = new LiveSession(
79            notify,
80            (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
81            mUIDValid,
82            mUID);
83
84    mLiveLooper->registerHandler(mLiveSession);
85
86    mLiveSession->connectAsync(
87            mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
88}
89
90void NuPlayer::HTTPLiveSource::start() {
91}
92
93sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
94    sp<AMessage> format;
95    status_t err = mLiveSession->getStreamFormat(
96            audio ? LiveSession::STREAMTYPE_AUDIO
97                  : LiveSession::STREAMTYPE_VIDEO,
98            &format);
99
100    if (err != OK) {
101        return NULL;
102    }
103
104    return format;
105}
106
107status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
108    return OK;
109}
110
111status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
112        bool audio, sp<ABuffer> *accessUnit) {
113    return mLiveSession->dequeueAccessUnit(
114            audio ? LiveSession::STREAMTYPE_AUDIO
115                  : LiveSession::STREAMTYPE_VIDEO,
116            accessUnit);
117}
118
119status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
120    return mLiveSession->getDuration(durationUs);
121}
122
123status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
124    return mLiveSession->seekTo(seekTimeUs);
125}
126
127void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
128    switch (msg->what()) {
129        case kWhatSessionNotify:
130        {
131            onSessionNotify(msg);
132            break;
133        }
134
135        default:
136            Source::onMessageReceived(msg);
137            break;
138    }
139}
140
141void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
142    int32_t what;
143    CHECK(msg->findInt32("what", &what));
144
145    switch (what) {
146        case LiveSession::kWhatPrepared:
147        {
148            notifyVideoSizeChanged(0, 0);
149
150            uint32_t flags = FLAG_CAN_PAUSE;
151            if (mLiveSession->isSeekable()) {
152                flags |= FLAG_CAN_SEEK;
153                flags |= FLAG_CAN_SEEK_BACKWARD;
154                flags |= FLAG_CAN_SEEK_FORWARD;
155            }
156
157            if (mLiveSession->hasDynamicDuration()) {
158                flags |= FLAG_DYNAMIC_DURATION;
159            }
160
161            notifyFlagsChanged(flags);
162
163            notifyPrepared();
164            break;
165        }
166
167        case LiveSession::kWhatPreparationFailed:
168        {
169            status_t err;
170            CHECK(msg->findInt32("err", &err));
171
172            notifyPrepared(err);
173            break;
174        }
175
176        case LiveSession::kWhatStreamsChanged:
177        {
178            uint32_t changedMask;
179            CHECK(msg->findInt32(
180                        "changedMask", (int32_t *)&changedMask));
181
182            bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
183            bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
184
185            sp<AMessage> reply;
186            CHECK(msg->findMessage("reply", &reply));
187
188            sp<AMessage> notify = dupNotify();
189            notify->setInt32("what", kWhatQueueDecoderShutdown);
190            notify->setInt32("audio", audio);
191            notify->setInt32("video", video);
192            notify->setMessage("reply", reply);
193            notify->post();
194            break;
195        }
196
197        case LiveSession::kWhatError:
198        {
199            break;
200        }
201
202        default:
203            TRESPASS();
204    }
205}
206
207}  // namespace android
208
209