LiveSession.cpp revision 5bc087c573c70c84c6a39946457590b42d392a33
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 "LiveSession"
19#include <utils/Log.h>
20
21#include "include/LiveSession.h"
22
23#include "LiveDataSource.h"
24
25#include "include/M3UParser.h"
26#include "include/NuHTTPDataSource.h"
27
28#include <cutils/properties.h>
29#include <media/stagefright/foundation/hexdump.h>
30#include <media/stagefright/foundation/ABuffer.h>
31#include <media/stagefright/foundation/ADebug.h>
32#include <media/stagefright/foundation/AMessage.h>
33#include <media/stagefright/DataSource.h>
34#include <media/stagefright/FileSource.h>
35#include <media/stagefright/MediaErrors.h>
36
37#include <ctype.h>
38#include <openssl/aes.h>
39
40namespace android {
41
42const int64_t LiveSession::kMaxPlaylistAgeUs = 15000000ll;
43
44LiveSession::LiveSession()
45    : mDataSource(new LiveDataSource),
46      mHTTPDataSource(new NuHTTPDataSource),
47      mPrevBandwidthIndex(-1),
48      mLastPlaylistFetchTimeUs(-1),
49      mSeqNumber(-1),
50      mSeekTimeUs(-1),
51      mNumRetries(0),
52      mDurationUs(-1),
53      mSeekDone(false),
54      mMonitorQueueGeneration(0) {
55}
56
57LiveSession::~LiveSession() {
58}
59
60sp<DataSource> LiveSession::getDataSource() {
61    return mDataSource;
62}
63
64void LiveSession::connect(const char *url) {
65    sp<AMessage> msg = new AMessage(kWhatConnect, id());
66    msg->setString("url", url);
67    msg->post();
68}
69
70void LiveSession::disconnect() {
71    (new AMessage(kWhatDisconnect, id()))->post();
72}
73
74void LiveSession::seekTo(int64_t timeUs) {
75    Mutex::Autolock autoLock(mLock);
76    mSeekDone = false;
77
78    sp<AMessage> msg = new AMessage(kWhatSeek, id());
79    msg->setInt64("timeUs", timeUs);
80    msg->post();
81
82    while (!mSeekDone) {
83        mCondition.wait(mLock);
84    }
85}
86
87void LiveSession::onMessageReceived(const sp<AMessage> &msg) {
88    switch (msg->what()) {
89        case kWhatConnect:
90            onConnect(msg);
91            break;
92
93        case kWhatDisconnect:
94            onDisconnect();
95            break;
96
97        case kWhatMonitorQueue:
98        {
99            int32_t generation;
100            CHECK(msg->findInt32("generation", &generation));
101
102            if (generation != mMonitorQueueGeneration) {
103                // Stale event
104                break;
105            }
106
107            onMonitorQueue();
108            break;
109        }
110
111        case kWhatSeek:
112            onSeek(msg);
113            break;
114
115        default:
116            TRESPASS();
117            break;
118    }
119}
120
121// static
122int LiveSession::SortByBandwidth(const BandwidthItem *a, const BandwidthItem *b) {
123    if (a->mBandwidth < b->mBandwidth) {
124        return -1;
125    } else if (a->mBandwidth == b->mBandwidth) {
126        return 0;
127    }
128
129    return 1;
130}
131
132void LiveSession::onConnect(const sp<AMessage> &msg) {
133    AString url;
134    CHECK(msg->findString("url", &url));
135
136    LOGI("onConnect '%s'", url.c_str());
137
138    mMasterURL = url;
139
140    sp<M3UParser> playlist = fetchPlaylist(url.c_str());
141    CHECK(playlist != NULL);
142
143    if (playlist->isVariantPlaylist()) {
144        for (size_t i = 0; i < playlist->size(); ++i) {
145            BandwidthItem item;
146
147            sp<AMessage> meta;
148            playlist->itemAt(i, &item.mURI, &meta);
149
150            unsigned long bandwidth;
151            CHECK(meta->findInt32("bandwidth", (int32_t *)&item.mBandwidth));
152
153            mBandwidthItems.push(item);
154        }
155
156        CHECK_GT(mBandwidthItems.size(), 0u);
157
158        mBandwidthItems.sort(SortByBandwidth);
159
160        char value[PROPERTY_VALUE_MAX];
161        if (!property_get("media.httplive.enable-nuplayer", value, NULL)
162                || (strcasecmp(value, "true") && strcmp(value, "1"))) {
163            // The "legacy" player cannot deal with audio format changes,
164            // some streams use different audio encoding parameters for
165            // their lowest bandwidth stream.
166            if (mBandwidthItems.size() > 1) {
167                // XXX Remove the lowest bitrate stream for now...
168                mBandwidthItems.removeAt(0);
169            }
170        }
171    }
172
173    postMonitorQueue();
174}
175
176void LiveSession::onDisconnect() {
177    LOGI("onDisconnect");
178
179    mDataSource->queueEOS(ERROR_END_OF_STREAM);
180}
181
182status_t LiveSession::fetchFile(const char *url, sp<ABuffer> *out) {
183    *out = NULL;
184
185    sp<DataSource> source;
186
187    if (!strncasecmp(url, "file://", 7)) {
188        source = new FileSource(url + 7);
189    } else if (strncasecmp(url, "http://", 7)) {
190        return ERROR_UNSUPPORTED;
191    } else {
192        status_t err = mHTTPDataSource->connect(url);
193
194        if (err != OK) {
195            return err;
196        }
197
198        source = mHTTPDataSource;
199    }
200
201    off64_t size;
202    status_t err = source->getSize(&size);
203
204    if (err != OK) {
205        size = 65536;
206    }
207
208    sp<ABuffer> buffer = new ABuffer(size);
209    buffer->setRange(0, 0);
210
211    for (;;) {
212        size_t bufferRemaining = buffer->capacity() - buffer->size();
213
214        if (bufferRemaining == 0) {
215            bufferRemaining = 32768;
216
217            LOGV("increasing download buffer to %d bytes",
218                 buffer->size() + bufferRemaining);
219
220            sp<ABuffer> copy = new ABuffer(buffer->size() + bufferRemaining);
221            memcpy(copy->data(), buffer->data(), buffer->size());
222            copy->setRange(0, buffer->size());
223
224            buffer = copy;
225        }
226
227        ssize_t n = source->readAt(
228                buffer->size(), buffer->data() + buffer->size(),
229                bufferRemaining);
230
231        if (n < 0) {
232            return err;
233        }
234
235        if (n == 0) {
236            break;
237        }
238
239        buffer->setRange(0, buffer->size() + (size_t)n);
240    }
241
242    *out = buffer;
243
244    return OK;
245}
246
247sp<M3UParser> LiveSession::fetchPlaylist(const char *url) {
248    sp<ABuffer> buffer;
249    status_t err = fetchFile(url, &buffer);
250
251    if (err != OK) {
252        return NULL;
253    }
254
255    sp<M3UParser> playlist =
256        new M3UParser(url, buffer->data(), buffer->size());
257
258    if (playlist->initCheck() != OK) {
259        return NULL;
260    }
261
262    return playlist;
263}
264
265static double uniformRand() {
266    return (double)rand() / RAND_MAX;
267}
268
269size_t LiveSession::getBandwidthIndex() {
270    if (mBandwidthItems.size() == 0) {
271        return 0;
272    }
273
274#if 1
275    int32_t bandwidthBps;
276    if (mHTTPDataSource != NULL
277            && mHTTPDataSource->estimateBandwidth(&bandwidthBps)) {
278        LOGV("bandwidth estimated at %.2f kbps", bandwidthBps / 1024.0f);
279    } else {
280        LOGV("no bandwidth estimate.");
281        return 0;  // Pick the lowest bandwidth stream by default.
282    }
283
284    char value[PROPERTY_VALUE_MAX];
285    if (property_get("media.httplive.max-bw", value, NULL)) {
286        char *end;
287        long maxBw = strtoul(value, &end, 10);
288        if (end > value && *end == '\0') {
289            if (maxBw > 0 && bandwidthBps > maxBw) {
290                LOGV("bandwidth capped to %ld bps", maxBw);
291                bandwidthBps = maxBw;
292            }
293        }
294    }
295
296    // Consider only 80% of the available bandwidth usable.
297    bandwidthBps = (bandwidthBps * 8) / 10;
298
299    // Pick the highest bandwidth stream below or equal to estimated bandwidth.
300
301    size_t index = mBandwidthItems.size() - 1;
302    while (index > 0 && mBandwidthItems.itemAt(index).mBandwidth
303                            > (size_t)bandwidthBps) {
304        --index;
305    }
306#elif 0
307    // Change bandwidth at random()
308    size_t index = uniformRand() * mBandwidthItems.size();
309#elif 0
310    // There's a 50% chance to stay on the current bandwidth and
311    // a 50% chance to switch to the next higher bandwidth (wrapping around
312    // to lowest)
313    const size_t kMinIndex = 0;
314
315    size_t index;
316    if (mPrevBandwidthIndex < 0) {
317        index = kMinIndex;
318    } else if (uniformRand() < 0.5) {
319        index = (size_t)mPrevBandwidthIndex;
320    } else {
321        index = mPrevBandwidthIndex + 1;
322        if (index == mBandwidthItems.size()) {
323            index = kMinIndex;
324        }
325    }
326#elif 0
327    // Pick the highest bandwidth stream below or equal to 1.2 Mbit/sec
328
329    size_t index = mBandwidthItems.size() - 1;
330    while (index > 0 && mBandwidthItems.itemAt(index).mBandwidth > 1200000) {
331        --index;
332    }
333#else
334    size_t index = mBandwidthItems.size() - 1;  // Highest bandwidth stream
335#endif
336
337    return index;
338}
339
340void LiveSession::onDownloadNext() {
341    size_t bandwidthIndex = getBandwidthIndex();
342
343    int64_t nowUs = ALooper::GetNowUs();
344
345    if (mLastPlaylistFetchTimeUs < 0
346            || (ssize_t)bandwidthIndex != mPrevBandwidthIndex
347            || (!mPlaylist->isComplete()
348                && mLastPlaylistFetchTimeUs + kMaxPlaylistAgeUs <= nowUs)) {
349        AString url;
350        if (mBandwidthItems.size() > 0) {
351            url = mBandwidthItems.editItemAt(bandwidthIndex).mURI;
352        } else {
353            url = mMasterURL;
354        }
355
356        bool firstTime = (mPlaylist == NULL);
357
358        mPlaylist = fetchPlaylist(url.c_str());
359        if (mPlaylist == NULL) {
360            LOGE("failed to load playlist at url '%s'", url.c_str());
361            mDataSource->queueEOS(ERROR_IO);
362            return;
363        }
364
365        if (firstTime) {
366            Mutex::Autolock autoLock(mLock);
367
368            int32_t targetDuration;
369            if (!mPlaylist->isComplete()
370                    || !mPlaylist->meta()->findInt32(
371                    "target-duration", &targetDuration)) {
372                mDurationUs = -1;
373            } else {
374                mDurationUs = 1000000ll * targetDuration * mPlaylist->size();
375            }
376        }
377
378        mLastPlaylistFetchTimeUs = ALooper::GetNowUs();
379    }
380
381    int32_t firstSeqNumberInPlaylist;
382    if (mPlaylist->meta() == NULL || !mPlaylist->meta()->findInt32(
383                "media-sequence", &firstSeqNumberInPlaylist)) {
384        firstSeqNumberInPlaylist = 0;
385    }
386
387    bool explicitDiscontinuity = false;
388
389    if (mSeekTimeUs >= 0) {
390        int32_t targetDuration;
391        if (mPlaylist->isComplete() &&
392                mPlaylist->meta()->findInt32(
393                    "target-duration", &targetDuration)) {
394            int64_t seekTimeSecs = (mSeekTimeUs + 500000ll) / 1000000ll;
395            int64_t index = seekTimeSecs / targetDuration;
396
397            if (index >= 0 && index < mPlaylist->size()) {
398                mSeqNumber = firstSeqNumberInPlaylist + index;
399                mDataSource->reset();
400
401                explicitDiscontinuity = true;
402            }
403        }
404
405        mSeekTimeUs = -1;
406
407        Mutex::Autolock autoLock(mLock);
408        mSeekDone = true;
409        mCondition.broadcast();
410    }
411
412    if (mSeqNumber < 0) {
413        if (mPlaylist->isComplete()) {
414            mSeqNumber = firstSeqNumberInPlaylist;
415        } else {
416            mSeqNumber = firstSeqNumberInPlaylist + mPlaylist->size() / 2;
417        }
418    }
419
420    int32_t lastSeqNumberInPlaylist =
421        firstSeqNumberInPlaylist + (int32_t)mPlaylist->size() - 1;
422
423    if (mSeqNumber < firstSeqNumberInPlaylist
424            || mSeqNumber > lastSeqNumberInPlaylist) {
425        if (!mPlaylist->isComplete()
426                && mSeqNumber > lastSeqNumberInPlaylist
427                && mNumRetries < kMaxNumRetries) {
428            ++mNumRetries;
429
430            mLastPlaylistFetchTimeUs = -1;
431            postMonitorQueue(3000000ll);
432            return;
433        }
434
435        LOGE("Cannot find sequence number %d in playlist "
436             "(contains %d - %d)",
437             mSeqNumber, firstSeqNumberInPlaylist,
438             firstSeqNumberInPlaylist + mPlaylist->size() - 1);
439
440        mDataSource->queueEOS(ERROR_END_OF_STREAM);
441        return;
442    }
443
444    mNumRetries = 0;
445
446    AString uri;
447    sp<AMessage> itemMeta;
448    CHECK(mPlaylist->itemAt(
449                mSeqNumber - firstSeqNumberInPlaylist,
450                &uri,
451                &itemMeta));
452
453    int32_t val;
454    if (itemMeta->findInt32("discontinuity", &val) && val != 0) {
455        explicitDiscontinuity = true;
456    }
457
458    sp<ABuffer> buffer;
459    status_t err = fetchFile(uri.c_str(), &buffer);
460    if (err != OK) {
461        LOGE("failed to fetch .ts segment at url '%s'", uri.c_str());
462        mDataSource->queueEOS(err);
463        return;
464    }
465
466    CHECK_EQ((status_t)OK,
467             decryptBuffer(mSeqNumber - firstSeqNumberInPlaylist, buffer));
468
469    if (buffer->size() == 0 || buffer->data()[0] != 0x47) {
470        // Not a transport stream???
471
472        LOGE("This doesn't look like a transport stream...");
473
474        mDataSource->queueEOS(ERROR_UNSUPPORTED);
475        return;
476    }
477
478    bool bandwidthChanged =
479        mPrevBandwidthIndex >= 0
480            && (size_t)mPrevBandwidthIndex != bandwidthIndex;
481
482    if (explicitDiscontinuity || bandwidthChanged) {
483        // Signal discontinuity.
484
485        sp<ABuffer> tmp = new ABuffer(188);
486        memset(tmp->data(), 0, tmp->size());
487        tmp->data()[1] = bandwidthChanged;
488
489        mDataSource->queueBuffer(tmp);
490    }
491
492    mDataSource->queueBuffer(buffer);
493
494    mPrevBandwidthIndex = bandwidthIndex;
495    ++mSeqNumber;
496
497    postMonitorQueue();
498}
499
500void LiveSession::onMonitorQueue() {
501    if (mSeekTimeUs >= 0
502            || mDataSource->countQueuedBuffers() < kMaxNumQueuedFragments) {
503        onDownloadNext();
504    } else {
505        postMonitorQueue(1000000ll);
506    }
507}
508
509status_t LiveSession::decryptBuffer(
510        size_t playlistIndex, const sp<ABuffer> &buffer) {
511    sp<AMessage> itemMeta;
512    bool found = false;
513    AString method;
514
515    for (ssize_t i = playlistIndex; i >= 0; --i) {
516        AString uri;
517        CHECK(mPlaylist->itemAt(i, &uri, &itemMeta));
518
519        if (itemMeta->findString("cipher-method", &method)) {
520            found = true;
521            break;
522        }
523    }
524
525    if (!found) {
526        method = "NONE";
527    }
528
529    if (method == "NONE") {
530        return OK;
531    } else if (!(method == "AES-128")) {
532        LOGE("Unsupported cipher method '%s'", method.c_str());
533        return ERROR_UNSUPPORTED;
534    }
535
536    AString keyURI;
537    if (!itemMeta->findString("cipher-uri", &keyURI)) {
538        LOGE("Missing key uri");
539        return ERROR_MALFORMED;
540    }
541
542    ssize_t index = mAESKeyForURI.indexOfKey(keyURI);
543
544    sp<ABuffer> key;
545    if (index >= 0) {
546        key = mAESKeyForURI.valueAt(index);
547    } else {
548        key = new ABuffer(16);
549
550        sp<NuHTTPDataSource> keySource = new NuHTTPDataSource;
551        status_t err = keySource->connect(keyURI.c_str());
552
553        if (err == OK) {
554            size_t offset = 0;
555            while (offset < 16) {
556                ssize_t n = keySource->readAt(
557                        offset, key->data() + offset, 16 - offset);
558                if (n <= 0) {
559                    err = ERROR_IO;
560                    break;
561                }
562
563                offset += n;
564            }
565        }
566
567        if (err != OK) {
568            LOGE("failed to fetch cipher key from '%s'.", keyURI.c_str());
569            return ERROR_IO;
570        }
571
572        mAESKeyForURI.add(keyURI, key);
573    }
574
575    AES_KEY aes_key;
576    if (AES_set_decrypt_key(key->data(), 128, &aes_key) != 0) {
577        LOGE("failed to set AES decryption key.");
578        return UNKNOWN_ERROR;
579    }
580
581    unsigned char aes_ivec[16];
582
583    AString iv;
584    if (itemMeta->findString("cipher-iv", &iv)) {
585        if ((!iv.startsWith("0x") && !iv.startsWith("0X"))
586                || iv.size() != 16 * 2 + 2) {
587            LOGE("malformed cipher IV '%s'.", iv.c_str());
588            return ERROR_MALFORMED;
589        }
590
591        memset(aes_ivec, 0, sizeof(aes_ivec));
592        for (size_t i = 0; i < 16; ++i) {
593            char c1 = tolower(iv.c_str()[2 + 2 * i]);
594            char c2 = tolower(iv.c_str()[3 + 2 * i]);
595            if (!isxdigit(c1) || !isxdigit(c2)) {
596                LOGE("malformed cipher IV '%s'.", iv.c_str());
597                return ERROR_MALFORMED;
598            }
599            uint8_t nibble1 = isdigit(c1) ? c1 - '0' : c1 - 'a' + 10;
600            uint8_t nibble2 = isdigit(c2) ? c2 - '0' : c2 - 'a' + 10;
601
602            aes_ivec[i] = nibble1 << 4 | nibble2;
603        }
604    } else {
605        memset(aes_ivec, 0, sizeof(aes_ivec));
606        aes_ivec[15] = mSeqNumber & 0xff;
607        aes_ivec[14] = (mSeqNumber >> 8) & 0xff;
608        aes_ivec[13] = (mSeqNumber >> 16) & 0xff;
609        aes_ivec[12] = (mSeqNumber >> 24) & 0xff;
610    }
611
612    AES_cbc_encrypt(
613            buffer->data(), buffer->data(), buffer->size(),
614            &aes_key, aes_ivec, AES_DECRYPT);
615
616    // hexdump(buffer->data(), buffer->size());
617
618    size_t n = buffer->size();
619    CHECK_GT(n, 0u);
620
621    size_t pad = buffer->data()[n - 1];
622
623    CHECK_GT(pad, 0u);
624    CHECK_LE(pad, 16u);
625    CHECK_GE((size_t)n, pad);
626    for (size_t i = 0; i < pad; ++i) {
627        CHECK_EQ((unsigned)buffer->data()[n - 1 - i], pad);
628    }
629
630    n -= pad;
631
632    buffer->setRange(buffer->offset(), n);
633
634    return OK;
635}
636
637void LiveSession::postMonitorQueue(int64_t delayUs) {
638    sp<AMessage> msg = new AMessage(kWhatMonitorQueue, id());
639    msg->setInt32("generation", ++mMonitorQueueGeneration);
640    msg->post(delayUs);
641}
642
643void LiveSession::onSeek(const sp<AMessage> &msg) {
644    int64_t timeUs;
645    CHECK(msg->findInt64("timeUs", &timeUs));
646
647    mSeekTimeUs = timeUs;
648    postMonitorQueue();
649}
650
651status_t LiveSession::getDuration(int64_t *durationUs) {
652    Mutex::Autolock autoLock(mLock);
653    *durationUs = mDurationUs;
654
655    return OK;
656}
657
658bool LiveSession::isSeekable() {
659    int64_t durationUs;
660    return getDuration(&durationUs) == OK && durationUs >= 0;
661}
662
663}  // namespace android
664
665