Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | #include <stdlib.h> |
| 18 | |
| 19 | #include <media/stagefright/HTTPStream.h> |
| 20 | #include <media/stagefright/MediaBuffer.h> |
| 21 | #include <media/stagefright/MediaBufferGroup.h> |
| 22 | #include <media/stagefright/MetaData.h> |
| 23 | #include <media/stagefright/ShoutcastSource.h> |
| 24 | #include <media/stagefright/string.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | ShoutcastSource::ShoutcastSource(HTTPStream *http) |
| 29 | : mHttp(http), |
| 30 | mMetaDataOffset(0), |
| 31 | mBytesUntilMetaData(0), |
| 32 | mGroup(NULL), |
| 33 | mStarted(false) { |
| 34 | string metaint; |
| 35 | if (mHttp->find_header_value("icy-metaint", &metaint)) { |
| 36 | char *end; |
| 37 | const char *start = metaint.c_str(); |
| 38 | mMetaDataOffset = strtol(start, &end, 10); |
| 39 | assert(end > start && *end == '\0'); |
| 40 | assert(mMetaDataOffset > 0); |
| 41 | |
| 42 | mBytesUntilMetaData = mMetaDataOffset; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | ShoutcastSource::~ShoutcastSource() { |
| 47 | if (mStarted) { |
| 48 | stop(); |
| 49 | } |
| 50 | |
| 51 | delete mHttp; |
| 52 | mHttp = NULL; |
| 53 | } |
| 54 | |
| 55 | status_t ShoutcastSource::start(MetaData *) { |
| 56 | assert(!mStarted); |
| 57 | |
| 58 | mGroup = new MediaBufferGroup; |
| 59 | mGroup->add_buffer(new MediaBuffer(4096)); // XXX |
| 60 | |
| 61 | mStarted = true; |
| 62 | |
| 63 | return OK; |
| 64 | } |
| 65 | |
| 66 | status_t ShoutcastSource::stop() { |
| 67 | assert(mStarted); |
| 68 | |
| 69 | delete mGroup; |
| 70 | mGroup = NULL; |
| 71 | |
| 72 | mStarted = false; |
| 73 | |
| 74 | return OK; |
| 75 | } |
| 76 | |
| 77 | sp<MetaData> ShoutcastSource::getFormat() { |
| 78 | sp<MetaData> meta = new MetaData; |
| 79 | meta->setCString(kKeyMIMEType, "audio/mpeg"); |
| 80 | meta->setInt32(kKeySampleRate, 44100); |
| 81 | meta->setInt32(kKeyChannelCount, 2); // XXX |
| 82 | |
| 83 | return meta; |
| 84 | } |
| 85 | |
| 86 | status_t ShoutcastSource::read( |
| 87 | MediaBuffer **out, const ReadOptions *options) { |
| 88 | assert(mStarted); |
| 89 | |
| 90 | *out = NULL; |
| 91 | |
| 92 | int64_t seekTimeUs; |
| 93 | if (options && options->getSeekTo(&seekTimeUs)) { |
| 94 | return ERROR_UNSUPPORTED; |
| 95 | } |
| 96 | |
| 97 | MediaBuffer *buffer; |
| 98 | status_t err = mGroup->acquire_buffer(&buffer); |
| 99 | if (err != OK) { |
| 100 | return err; |
| 101 | } |
| 102 | |
| 103 | *out = buffer; |
| 104 | |
| 105 | size_t num_bytes = buffer->size(); |
| 106 | if (mMetaDataOffset > 0 && num_bytes > mBytesUntilMetaData) { |
| 107 | num_bytes = mBytesUntilMetaData; |
| 108 | } |
| 109 | |
| 110 | ssize_t n = mHttp->receive(buffer->data(), num_bytes); |
| 111 | |
| 112 | if (n <= 0) { |
| 113 | return (status_t)n; |
| 114 | } |
| 115 | |
| 116 | buffer->set_range(0, n); |
| 117 | |
| 118 | mBytesUntilMetaData -= (size_t)n; |
| 119 | |
| 120 | if (mBytesUntilMetaData == 0) { |
| 121 | unsigned char num_16_byte_blocks = 0; |
| 122 | n = mHttp->receive((char *)&num_16_byte_blocks, 1); |
| 123 | assert(n == 1); |
| 124 | |
| 125 | char meta[255 * 16]; |
| 126 | size_t meta_size = num_16_byte_blocks * 16; |
| 127 | size_t meta_length = 0; |
| 128 | while (meta_length < meta_size) { |
| 129 | n = mHttp->receive(&meta[meta_length], meta_size - meta_length); |
| 130 | if (n <= 0) { |
| 131 | return (status_t)n; |
| 132 | } |
| 133 | |
| 134 | meta_length += (size_t) n; |
| 135 | } |
| 136 | |
| 137 | while (meta_length > 0 && meta[meta_length - 1] == '\0') { |
| 138 | --meta_length; |
| 139 | } |
| 140 | |
| 141 | if (meta_length > 0) { |
| 142 | // Technically we should probably attach this meta data to the |
| 143 | // next buffer. XXX |
| 144 | buffer->meta_data()->setData('shou', 'shou', meta, meta_length); |
| 145 | } |
| 146 | |
| 147 | mBytesUntilMetaData = mMetaDataOffset; |
| 148 | } |
| 149 | |
| 150 | return OK; |
| 151 | } |
| 152 | |
| 153 | } // namespace android |
| 154 | |