blob: 576a850af2247d9946f125e60cf59fae9e0903c6 [file] [log] [blame]
Andreas Huber54e66492010-12-23 10:27:40 -08001/*
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 "ATSParser.h"
24#include "AnotherPacketSource.h"
25#include "LiveDataSource.h"
26#include "LiveSession.h"
27
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
Andreas Huber50874942011-04-19 11:50:27 -070036NuPlayer::HTTPLiveSource::HTTPLiveSource(
37 const char *url,
38 const KeyedVector<String8, String8> *headers)
Andreas Huber54e66492010-12-23 10:27:40 -080039 : mURL(url),
Andreas Huber50874942011-04-19 11:50:27 -070040 mFlags(0),
Andreas Huber54e66492010-12-23 10:27:40 -080041 mEOS(false),
42 mOffset(0) {
Andreas Huber50874942011-04-19 11:50:27 -070043 if (headers) {
44 mExtraHeaders = *headers;
45
46 ssize_t index =
47 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
48
49 if (index >= 0) {
50 mFlags |= kFlagIncognito;
51
52 mExtraHeaders.removeItemsAt(index);
53 }
54 }
Andreas Huber54e66492010-12-23 10:27:40 -080055}
56
57NuPlayer::HTTPLiveSource::~HTTPLiveSource() {
58 mLiveSession->disconnect();
59 mLiveLooper->stop();
60}
61
62void NuPlayer::HTTPLiveSource::start() {
63 mLiveLooper = new ALooper;
64 mLiveLooper->setName("http live");
65 mLiveLooper->start();
66
Andreas Huber53182c42011-02-24 14:42:48 -080067 mLiveSession = new LiveSession(
68 (mFlags & kFlagIncognito) ? LiveSession::kFlagIncognito : 0);
69
Andreas Huber54e66492010-12-23 10:27:40 -080070 mLiveLooper->registerHandler(mLiveSession);
71
Andreas Huber50874942011-04-19 11:50:27 -070072 mLiveSession->connect(
73 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
Andreas Huber54e66492010-12-23 10:27:40 -080074
75 mTSParser = new ATSParser;
76}
77
78sp<MetaData> NuPlayer::HTTPLiveSource::getFormat(bool audio) {
79 ATSParser::SourceType type =
80 audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
81
82 sp<AnotherPacketSource> source =
83 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
84
85 if (source == NULL) {
86 return NULL;
87 }
88
89 return source->getFormat();
90}
91
92bool NuPlayer::HTTPLiveSource::feedMoreTSData() {
93 if (mEOS) {
94 return false;
95 }
96
97 sp<LiveDataSource> source =
98 static_cast<LiveDataSource *>(mLiveSession->getDataSource().get());
99
Andreas Huber847551c2011-01-05 16:24:27 -0800100 for (int32_t i = 0; i < 50; ++i) {
Andreas Huber54e66492010-12-23 10:27:40 -0800101 char buffer[188];
102 ssize_t n = source->readAtNonBlocking(mOffset, buffer, sizeof(buffer));
103
104 if (n == -EWOULDBLOCK) {
105 break;
106 } else if (n < 0) {
107 LOGI("input data EOS reached.");
Andreas Hubercbeaca72011-01-04 14:01:29 -0800108 mTSParser->signalEOS(n);
Andreas Huber54e66492010-12-23 10:27:40 -0800109 mEOS = true;
110 break;
111 } else {
112 if (buffer[0] == 0x00) {
113 // XXX legacy
Andreas Huber669ad132011-03-02 15:34:46 -0800114 sp<AMessage> extra;
Andreas Huber54e66492010-12-23 10:27:40 -0800115 mTSParser->signalDiscontinuity(
116 buffer[1] == 0x00
117 ? ATSParser::DISCONTINUITY_SEEK
Andreas Huber669ad132011-03-02 15:34:46 -0800118 : ATSParser::DISCONTINUITY_FORMATCHANGE,
119 extra);
Andreas Huber54e66492010-12-23 10:27:40 -0800120 } else {
121 mTSParser->feedTSPacket(buffer, sizeof(buffer));
122 }
123
124 mOffset += n;
125 }
126 }
127
128 return true;
129}
130
131status_t NuPlayer::HTTPLiveSource::dequeueAccessUnit(
132 bool audio, sp<ABuffer> *accessUnit) {
133 ATSParser::SourceType type =
134 audio ? ATSParser::MPEG2ADTS_AUDIO : ATSParser::AVC_VIDEO;
135
136 sp<AnotherPacketSource> source =
137 static_cast<AnotherPacketSource *>(mTSParser->getSource(type).get());
138
139 if (source == NULL) {
140 return -EWOULDBLOCK;
141 }
142
143 status_t finalResult;
144 if (!source->hasBufferAvailable(&finalResult)) {
145 return finalResult == OK ? -EWOULDBLOCK : finalResult;
146 }
147
148 return source->dequeueAccessUnit(accessUnit);
149}
150
Andreas Huber08e10cb2011-01-05 12:17:08 -0800151status_t NuPlayer::HTTPLiveSource::getDuration(int64_t *durationUs) {
152 return mLiveSession->getDuration(durationUs);
153}
154
155status_t NuPlayer::HTTPLiveSource::seekTo(int64_t seekTimeUs) {
156 // We need to make sure we're not seeking until we have seen the very first
157 // PTS timestamp in the whole stream (from the beginning of the stream).
158 while (!mTSParser->PTSTimeDeltaEstablished() && feedMoreTSData()) {
159 usleep(100000);
160 }
161
162 mLiveSession->seekTo(seekTimeUs);
163
164 return OK;
165}
166
167bool NuPlayer::HTTPLiveSource::isSeekable() {
168 return mLiveSession->isSeekable();
169}
170
Andreas Huber54e66492010-12-23 10:27:40 -0800171} // namespace android
172