HTTPLiveSource.cpp revision 1228d6b175de8b21787cbe0c6c4bb5642f4d555e
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/IMediaHTTPService.h>
28#include <media/stagefright/foundation/ABuffer.h>
29#include <media/stagefright/foundation/ADebug.h>
30#include <media/stagefright/foundation/AMessage.h>
31#include <media/stagefright/MediaErrors.h>
32#include <media/stagefright/MetaData.h>
33
34namespace android {
35
36NuPlayer::HTTPLiveSource::HTTPLiveSource(
37        const sp<AMessage> &notify,
38        const sp<IMediaHTTPService> &httpService,
39        const char *url,
40        const KeyedVector<String8, String8> *headers)
41    : Source(notify),
42      mHTTPService(httpService),
43      mURL(url),
44      mFlags(0),
45      mFinalResult(OK),
46      mOffset(0),
47      mFetchSubtitleDataGeneration(0) {
48    if (headers) {
49        mExtraHeaders = *headers;
50
51        ssize_t index =
52            mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
53
54        if (index >= 0) {
55            mFlags |= kFlagIncognito;
56
57            mExtraHeaders.removeItemsAt(index);
58        }
59    }
60}
61
62NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
63    if (mLiveSession != NULL) {
64        mLiveSession->disconnect();
65
66        mLiveLooper->unregisterHandler(mLiveSession->id());
67        mLiveLooper->unregisterHandler(id());
68        mLiveLooper->stop();
69
70        mLiveSession.clear();
71        mLiveLooper.clear();
72    }
73}
74
75void NuPlayer::HTTPLiveSource::prepareAsync() {
76    if (mLiveLooper == NULL) {
77        mLiveLooper = new ALooper;
78        mLiveLooper->setName("http live");
79        mLiveLooper->start();
80
81        mLiveLooper->registerHandler(this);
82    }
83
84    sp<AMessage> notify = new AMessage(kWhatSessionNotify, id());
85
86    mLiveSession = new LiveSession(
87            notify,
88            (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0,
89            mHTTPService);
90
91    mLiveLooper->registerHandler(mLiveSession);
92
93    mLiveSession->connectAsync(
94            mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
95}
96
97void NuPlayer::HTTPLiveSource::start() {
98}
99
100sp<AMessage> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
101    sp<AMessage> format;
102    status_t err = mLiveSession->getStreamFormat(
103            audio ? LiveSession::STREAMTYPE_AUDIO
104                  : LiveSession::STREAMTYPE_VIDEO,
105            &format);
106
107    if (err != OK) {
108        return NULL;
109    }
110
111    return format;
112}
113
114status_t NuPlayer::HTTPLiveSource::feedMoreTSData() {
115    return OK;
116}
117
118status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
119        bool audio, sp<ABuffer> *accessUnit) {
120    return mLiveSession->dequeueAccessUnit(
121            audio ? LiveSession::STREAMTYPE_AUDIO
122                  : LiveSession::STREAMTYPE_VIDEO,
123            accessUnit);
124}
125
126status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
127    return mLiveSession->getDuration(durationUs);
128}
129
130size_t NuPlayer::HTTPLiveSource::getTrackCount() const {
131    return mLiveSession->getTrackCount();
132}
133
134sp<AMessage> NuPlayer::HTTPLiveSource::getTrackInfo(size_t trackIndex) const {
135    return mLiveSession->getTrackInfo(trackIndex);
136}
137
138status_t NuPlayer::HTTPLiveSource::selectTrack(size_t trackIndex, bool select) {
139    status_t err = mLiveSession->selectTrack(trackIndex, select);
140
141    if (err == OK) {
142        mFetchSubtitleDataGeneration++;
143        if (select) {
144            sp<AMessage> msg = new AMessage(kWhatFetchSubtitleData, id());
145            msg->setInt32("generation", mFetchSubtitleDataGeneration);
146            msg->post();
147        }
148    }
149
150    // LiveSession::selectTrack returns BAD_VALUE when selecting the currently
151    // selected track, or unselecting a non-selected track. In this case it's an
152    // no-op so we return OK.
153    return (err == OK || err == BAD_VALUE) ? (status_t)OK : err;
154}
155
156status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
157    return mLiveSession->seekTo(seekTimeUs);
158}
159
160void NuPlayer::HTTPLiveSource::onMessageReceived(const sp<AMessage> &msg) {
161    switch (msg->what()) {
162        case kWhatSessionNotify:
163        {
164            onSessionNotify(msg);
165            break;
166        }
167
168        case kWhatFetchSubtitleData:
169        {
170            int32_t generation;
171            CHECK(msg->findInt32("generation", &generation));
172
173            if (generation != mFetchSubtitleDataGeneration) {
174                // stale
175                break;
176            }
177
178            sp<ABuffer> buffer;
179            if (mLiveSession->dequeueAccessUnit(
180                    LiveSession::STREAMTYPE_SUBTITLES, &buffer) == OK) {
181                sp<AMessage> notify = dupNotify();
182                notify->setInt32("what", kWhatSubtitleData);
183                notify->setBuffer("buffer", buffer);
184                notify->post();
185
186                int64_t timeUs, baseUs, durationUs, delayUs;
187                CHECK(buffer->meta()->findInt64("baseUs", &baseUs));
188                CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
189                CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
190                delayUs = baseUs + timeUs - ALooper::GetNowUs();
191
192                msg->post(delayUs > 0ll ? delayUs : 0ll);
193            } else {
194                // try again in 1 second
195                msg->post(1000000ll);
196            }
197
198            break;
199        }
200
201        default:
202            Source::onMessageReceived(msg);
203            break;
204    }
205}
206
207void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
208    int32_t what;
209    CHECK(msg->findInt32("what", &what));
210
211    switch (what) {
212        case LiveSession::kWhatPrepared:
213        {
214            // notify the current size here if we have it, otherwise report an initial size of (0,0)
215            sp<AMessage> format = getFormat(false /* audio */);
216            int32_t width;
217            int32_t height;
218            if (format != NULL &&
219                    format->findInt32("width", &width) && format->findInt32("height", &height)) {
220                notifyVideoSizeChanged(format);
221            } else {
222                notifyVideoSizeChanged();
223            }
224
225            uint32_t flags = FLAG_CAN_PAUSE;
226            if (mLiveSession->isSeekable()) {
227                flags |= FLAG_CAN_SEEK;
228                flags |= FLAG_CAN_SEEK_BACKWARD;
229                flags |= FLAG_CAN_SEEK_FORWARD;
230            }
231
232            if (mLiveSession->hasDynamicDuration()) {
233                flags |= FLAG_DYNAMIC_DURATION;
234            }
235
236            notifyFlagsChanged(flags);
237
238            notifyPrepared();
239            break;
240        }
241
242        case LiveSession::kWhatPreparationFailed:
243        {
244            status_t err;
245            CHECK(msg->findInt32("err", &err));
246
247            notifyPrepared(err);
248            break;
249        }
250
251        case LiveSession::kWhatStreamsChanged:
252        {
253            uint32_t changedMask;
254            CHECK(msg->findInt32(
255                        "changedMask", (int32_t *)&changedMask));
256
257            bool audio = changedMask & LiveSession::STREAMTYPE_AUDIO;
258            bool video = changedMask & LiveSession::STREAMTYPE_VIDEO;
259
260            sp<AMessage> reply;
261            CHECK(msg->findMessage("reply", &reply));
262
263            sp<AMessage> notify = dupNotify();
264            notify->setInt32("what", kWhatQueueDecoderShutdown);
265            notify->setInt32("audio", audio);
266            notify->setInt32("video", video);
267            notify->setMessage("reply", reply);
268            notify->post();
269            break;
270        }
271
272        case LiveSession::kWhatError:
273        {
274            break;
275        }
276
277        default:
278            TRESPASS();
279    }
280}
281
282}  // namespace android
283
284