1 /* 2 * Copyright (C) 2011 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_TAG "LibAAH_RTP" 18 //#define LOG_NDEBUG 0 19 #include <utils/Log.h> 20 21 #include <poll.h> 22 #include <pthread.h> 23 24 #include <common_time/cc_helper.h> 25 #include <media/AudioSystem.h> 26 #include <media/AudioTrack.h> 27 #include <media/stagefright/foundation/ADebug.h> 28 #include <media/stagefright/MetaData.h> 29 #include <media/stagefright/OMXClient.h> 30 #include <media/stagefright/OMXCodec.h> 31 #include <media/stagefright/Utils.h> 32 #include <utils/Timers.h> 33 #include <utils/threads.h> 34 35 #include "aah_decoder_pump.h" 36 37 namespace android { 38 39 static const long long kLongDecodeErrorThreshold = 1000000ll; 40 static const uint32_t kMaxLongErrorsBeforeFatal = 3; 41 static const uint32_t kMaxErrorsBeforeFatal = 60; 42 43 AAH_DecoderPump::AAH_DecoderPump(OMXClient& omx) 44 : omx_(omx) 45 , thread_status_(OK) 46 , renderer_(NULL) 47 , last_queued_pts_valid_(false) 48 , last_queued_pts_(0) 49 , last_ts_transform_valid_(false) 50 , last_volume_(0xFF) { 51 thread_ = new ThreadWrapper(this); 52 } 53 54 AAH_DecoderPump::~AAH_DecoderPump() { 55 shutdown(); 56 } 57 58 status_t AAH_DecoderPump::initCheck() { 59 if (thread_ == NULL) { 60 ALOGE("Failed to allocate thread"); 61 return NO_MEMORY; 62 } 63 64 return OK; 65 } 66 67 status_t AAH_DecoderPump::queueForDecode(MediaBuffer* buf) { 68 if (NULL == buf) { 69 return BAD_VALUE; 70 } 71 72 if (OK != thread_status_) { 73 return thread_status_; 74 } 75 76 { // Explicit scope for AutoMutex pattern. 77 AutoMutex lock(&thread_lock_); 78 in_queue_.push_back(buf); 79 } 80 81 thread_cond_.signal(); 82 83 return OK; 84 } 85 86 void AAH_DecoderPump::queueToRenderer(MediaBuffer* decoded_sample) { 87 Mutex::Autolock lock(&render_lock_); 88 sp<MetaData> meta; 89 int64_t ts; 90 status_t res; 91 92 // Fetch the metadata and make sure the sample has a timestamp. We 93 // cannot render samples which are missing PTSs. 94 meta = decoded_sample->meta_data(); 95 if ((meta == NULL) || (!meta->findInt64(kKeyTime, &ts))) { 96 ALOGV("Decoded sample missing timestamp, cannot render."); 97 CHECK(false); 98 } else { 99 // If we currently are not holding on to a renderer, go ahead and 100 // make one now. 101 if (NULL == renderer_) { 102 renderer_ = new TimedAudioTrack(); 103 if (NULL != renderer_) { 104 int frameCount; 105 AudioTrack::getMinFrameCount(&frameCount, 106 AUDIO_STREAM_DEFAULT, 107 static_cast<int>(format_sample_rate_)); 108 audio_channel_mask_t ch_format = 109 audio_channel_out_mask_from_count(format_channels_); 110 111 res = renderer_->set(AUDIO_STREAM_DEFAULT, 112 format_sample_rate_, 113 AUDIO_FORMAT_PCM_16_BIT, 114 ch_format, 115 frameCount); 116 if (res != OK) { 117 ALOGE("Failed to setup audio renderer. (res = %d)", res); 118 delete renderer_; 119 renderer_ = NULL; 120 } else { 121 CHECK(last_ts_transform_valid_); 122 123 res = renderer_->setMediaTimeTransform( 124 last_ts_transform_, TimedAudioTrack::COMMON_TIME); 125 if (res != NO_ERROR) { 126 ALOGE("Failed to set media time transform on AudioTrack" 127 " (res = %d)", res); 128 delete renderer_; 129 renderer_ = NULL; 130 } else { 131 float volume = static_cast<float>(last_volume_) 132 / 255.0f; 133 if (renderer_->setVolume(volume, volume) != OK) { 134 ALOGW("%s: setVolume failed", __FUNCTION__); 135 } 136 137 renderer_->start(); 138 } 139 } 140 } else { 141 ALOGE("Failed to allocate AudioTrack to use as a renderer."); 142 } 143 } 144 145 if (NULL != renderer_) { 146 uint8_t* decoded_data = 147 reinterpret_cast<uint8_t*>(decoded_sample->data()); 148 uint32_t decoded_amt = decoded_sample->range_length(); 149 decoded_data += decoded_sample->range_offset(); 150 151 sp<IMemory> pcm_payload; 152 res = renderer_->allocateTimedBuffer(decoded_amt, &pcm_payload); 153 if (res != OK) { 154 ALOGE("Failed to allocate %d byte audio track buffer." 155 " (res = %d)", decoded_amt, res); 156 } else { 157 memcpy(pcm_payload->pointer(), decoded_data, decoded_amt); 158 159 res = renderer_->queueTimedBuffer(pcm_payload, ts); 160 if (res != OK) { 161 ALOGE("Failed to queue %d byte audio track buffer with" 162 " media PTS %lld. (res = %d)", decoded_amt, ts, res); 163 } else { 164 last_queued_pts_valid_ = true; 165 last_queued_pts_ = ts; 166 } 167 } 168 169 } else { 170 ALOGE("No renderer, dropping audio payload."); 171 } 172 } 173 } 174 175 void AAH_DecoderPump::stopAndCleanupRenderer() { 176 if (NULL == renderer_) { 177 return; 178 } 179 180 renderer_->stop(); 181 delete renderer_; 182 renderer_ = NULL; 183 } 184 185 void AAH_DecoderPump::setRenderTSTransform(const LinearTransform& trans) { 186 Mutex::Autolock lock(&render_lock_); 187 188 if (last_ts_transform_valid_ && !memcmp(&trans, 189 &last_ts_transform_, 190 sizeof(trans))) { 191 return; 192 } 193 194 last_ts_transform_ = trans; 195 last_ts_transform_valid_ = true; 196 197 if (NULL != renderer_) { 198 status_t res = renderer_->setMediaTimeTransform( 199 last_ts_transform_, TimedAudioTrack::COMMON_TIME); 200 if (res != NO_ERROR) { 201 ALOGE("Failed to set media time transform on AudioTrack" 202 " (res = %d)", res); 203 } 204 } 205 } 206 207 void AAH_DecoderPump::setRenderVolume(uint8_t volume) { 208 Mutex::Autolock lock(&render_lock_); 209 210 if (volume == last_volume_) { 211 return; 212 } 213 214 last_volume_ = volume; 215 if (renderer_ != NULL) { 216 float volume = static_cast<float>(last_volume_) / 255.0f; 217 if (renderer_->setVolume(volume, volume) != OK) { 218 ALOGW("%s: setVolume failed", __FUNCTION__); 219 } 220 } 221 } 222 223 // isAboutToUnderflow is something of a hack used to figure out when it might be 224 // time to give up on trying to fill in a gap in the RTP sequence and simply 225 // move on with a discontinuity. If we had perfect knowledge of when we were 226 // going to underflow, it would not be a hack, but unfortunately we do not. 227 // Right now, we just take the PTS of the last sample queued, and check to see 228 // if its presentation time is within kAboutToUnderflowThreshold from now. If 229 // it is, then we say that we are about to underflow. This decision is based on 230 // two (possibly invalid) assumptions. 231 // 232 // 1) The transmitter is leading the clock by more than 233 // kAboutToUnderflowThreshold. 234 // 2) The delta between the PTS of the last sample queued and the next sample 235 // is less than the transmitter's clock lead amount. 236 // 237 // Right now, the default transmitter lead time is 1 second, which is a pretty 238 // large number and greater than the 50mSec that kAboutToUnderflowThreshold is 239 // currently set to. This should satisfy assumption #1 for now, but changes to 240 // the transmitter clock lead time could effect this. 241 // 242 // For non-sparse streams with a homogeneous sample rate (the vast majority of 243 // streams in the world), the delta between any two adjacent PTSs will always be 244 // the homogeneous sample period. It is very uncommon to see a sample period 245 // greater than the 1 second clock lead we are currently using, and you 246 // certainly will not see it in an MP3 file which should satisfy assumption #2. 247 // Sparse audio streams (where no audio is transmitted for long periods of 248 // silence) and extremely low framerate video stream (like an MPEG-2 slideshow 249 // or the video stream for a pay TV audio channel) are examples of streams which 250 // might violate assumption #2. 251 bool AAH_DecoderPump::isAboutToUnderflow(int64_t threshold) { 252 Mutex::Autolock lock(&render_lock_); 253 254 // If we have never queued anything to the decoder, we really don't know if 255 // we are going to underflow or not. 256 if (!last_queued_pts_valid_ || !last_ts_transform_valid_) { 257 return false; 258 } 259 260 // Don't have access to Common Time? If so, then things are Very Bad 261 // elsewhere in the system; it pretty much does not matter what we do here. 262 // Since we cannot really tell if we are about to underflow or not, its 263 // probably best to assume that we are not and proceed accordingly. 264 int64_t tt_now; 265 if (OK != cc_helper_.getCommonTime(&tt_now)) { 266 return false; 267 } 268 269 // Transform from media time to common time. 270 int64_t last_queued_pts_tt; 271 if (!last_ts_transform_.doForwardTransform(last_queued_pts_, 272 &last_queued_pts_tt)) { 273 return false; 274 } 275 276 // Check to see if we are underflowing. 277 return ((tt_now + threshold - last_queued_pts_tt) > 0); 278 } 279 280 void* AAH_DecoderPump::workThread() { 281 // No need to lock when accessing decoder_ from the thread. The 282 // implementation of init and shutdown ensure that other threads never touch 283 // decoder_ while the work thread is running. 284 CHECK(decoder_ != NULL); 285 CHECK(format_ != NULL); 286 287 // Start the decoder and note its result code. If something goes horribly 288 // wrong, callers of queueForDecode and getOutput will be able to detect 289 // that the thread encountered a fatal error and shut down by examining 290 // thread_status_. 291 thread_status_ = decoder_->start(format_.get()); 292 if (OK != thread_status_) { 293 ALOGE("AAH_DecoderPump's work thread failed to start decoder" 294 " (res = %d)", thread_status_); 295 return NULL; 296 } 297 298 DurationTimer decode_timer; 299 uint32_t consecutive_long_errors = 0; 300 uint32_t consecutive_errors = 0; 301 302 while (!thread_->exitPending()) { 303 status_t res; 304 MediaBuffer* bufOut = NULL; 305 306 decode_timer.start(); 307 res = decoder_->read(&bufOut); 308 decode_timer.stop(); 309 310 if (res == INFO_FORMAT_CHANGED) { 311 // Format has changed. Destroy our current renderer so that a new 312 // one can be created during queueToRenderer with the proper format. 313 // 314 // TODO : In order to transition seamlessly, we should change this 315 // to put the old renderer in a queue to play out completely before 316 // we destroy it. We can still create a new renderer, the timed 317 // nature of the renderer should ensure a seamless splice. 318 stopAndCleanupRenderer(); 319 res = OK; 320 } 321 322 // Try to be a little nuanced in our handling of actual decode errors. 323 // Errors could happen because of minor stream corruption or because of 324 // transient resource limitations. In these cases, we would rather drop 325 // a little bit of output and ride out the unpleasantness then throw up 326 // our hands and abort everything. 327 // 328 // OTOH - When things are really bad (like we have a non-transient 329 // resource or bookkeeping issue, or the stream being fed to us is just 330 // complete and total garbage) we really want to terminate playback and 331 // raise an error condition all the way up to the application level so 332 // they can deal with it. 333 // 334 // Unfortunately, the error codes returned by the decoder can be a 335 // little non-specific. For example, if an OMXCodec times out 336 // attempting to obtain an output buffer, the error we get back is a 337 // generic -1. Try to distinguish between this resource timeout error 338 // and ES corruption error by timing how long the decode operation 339 // takes. Maintain accounting for both errors and "long errors". If we 340 // get more than a certain number consecutive errors of either type, 341 // consider it fatal and shutdown (which will cause the error to 342 // propagate all of the way up to the application level). The threshold 343 // for "long errors" is deliberately much lower than that of normal 344 // decode errors, both because of how long they take to happen and 345 // because they generally indicate resource limitation errors which are 346 // unlikely to go away in pathologically bad cases (in contrast to 347 // stream corruption errors which might happen 20 times in a row and 348 // then be suddenly OK again) 349 if (res != OK) { 350 consecutive_errors++; 351 if (decode_timer.durationUsecs() >= kLongDecodeErrorThreshold) 352 consecutive_long_errors++; 353 354 CHECK(NULL == bufOut); 355 356 ALOGW("%s: Failed to decode data (res = %d)", 357 __PRETTY_FUNCTION__, res); 358 359 if ((consecutive_errors >= kMaxErrorsBeforeFatal) || 360 (consecutive_long_errors >= kMaxLongErrorsBeforeFatal)) { 361 ALOGE("%s: Maximum decode error threshold has been reached." 362 " There have been %d consecutive decode errors, and %d" 363 " consecutive decode operations which resulted in errors" 364 " and took more than %lld uSec to process. The last" 365 " decode operation took %lld uSec.", 366 __PRETTY_FUNCTION__, 367 consecutive_errors, consecutive_long_errors, 368 kLongDecodeErrorThreshold, decode_timer.durationUsecs()); 369 thread_status_ = res; 370 break; 371 } 372 373 continue; 374 } 375 376 if (NULL == bufOut) { 377 ALOGW("%s: Successful decode, but no buffer produced", 378 __PRETTY_FUNCTION__); 379 continue; 380 } 381 382 // Successful decode (with actual output produced). Clear the error 383 // counters. 384 consecutive_errors = 0; 385 consecutive_long_errors = 0; 386 387 queueToRenderer(bufOut); 388 bufOut->release(); 389 } 390 391 decoder_->stop(); 392 stopAndCleanupRenderer(); 393 394 return NULL; 395 } 396 397 status_t AAH_DecoderPump::init(const sp<MetaData>& params) { 398 Mutex::Autolock lock(&init_lock_); 399 400 if (decoder_ != NULL) { 401 // already inited 402 return OK; 403 } 404 405 if (params == NULL) { 406 return BAD_VALUE; 407 } 408 409 if (!params->findInt32(kKeyChannelCount, &format_channels_)) { 410 return BAD_VALUE; 411 } 412 413 if (!params->findInt32(kKeySampleRate, &format_sample_rate_)) { 414 return BAD_VALUE; 415 } 416 417 CHECK(OK == thread_status_); 418 CHECK(decoder_ == NULL); 419 420 status_t ret_val = UNKNOWN_ERROR; 421 422 // Cache the format and attempt to create the decoder. 423 format_ = params; 424 decoder_ = OMXCodec::Create( 425 omx_.interface(), // IOMX Handle 426 format_, // Metadata for substream (indicates codec) 427 false, // Make a decoder, not an encoder 428 sp<MediaSource>(this)); // We will be the source for this codec. 429 430 if (decoder_ == NULL) { 431 ALOGE("Failed to allocate decoder in %s", __PRETTY_FUNCTION__); 432 goto bailout; 433 } 434 435 // Fire up the pump thread. It will take care of starting and stopping the 436 // decoder. 437 ret_val = thread_->run("aah_decode_pump", ANDROID_PRIORITY_AUDIO); 438 if (OK != ret_val) { 439 ALOGE("Failed to start work thread in %s (res = %d)", 440 __PRETTY_FUNCTION__, ret_val); 441 goto bailout; 442 } 443 444 bailout: 445 if (OK != ret_val) { 446 decoder_ = NULL; 447 format_ = NULL; 448 } 449 450 return OK; 451 } 452 453 status_t AAH_DecoderPump::shutdown() { 454 Mutex::Autolock lock(&init_lock_); 455 return shutdown_l(); 456 } 457 458 status_t AAH_DecoderPump::shutdown_l() { 459 thread_->requestExit(); 460 thread_cond_.signal(); 461 thread_->requestExitAndWait(); 462 463 for (MBQueue::iterator iter = in_queue_.begin(); 464 iter != in_queue_.end(); 465 ++iter) { 466 (*iter)->release(); 467 } 468 in_queue_.clear(); 469 470 last_queued_pts_valid_ = false; 471 last_ts_transform_valid_ = false; 472 last_volume_ = 0xFF; 473 thread_status_ = OK; 474 475 decoder_ = NULL; 476 format_ = NULL; 477 478 return OK; 479 } 480 481 status_t AAH_DecoderPump::read(MediaBuffer **buffer, 482 const ReadOptions *options) { 483 if (!buffer) { 484 return BAD_VALUE; 485 } 486 487 *buffer = NULL; 488 489 // While its not time to shut down, and we have no data to process, wait. 490 AutoMutex lock(&thread_lock_); 491 while (!thread_->exitPending() && in_queue_.empty()) 492 thread_cond_.wait(thread_lock_); 493 494 // At this point, if its not time to shutdown then we must have something to 495 // process. Go ahead and pop the front of the queue for processing. 496 if (!thread_->exitPending()) { 497 CHECK(!in_queue_.empty()); 498 499 *buffer = *(in_queue_.begin()); 500 in_queue_.erase(in_queue_.begin()); 501 } 502 503 // If we managed to get a buffer, then everything must be OK. If not, then 504 // we must be shutting down. 505 return (NULL == *buffer) ? INVALID_OPERATION : OK; 506 } 507 508 AAH_DecoderPump::ThreadWrapper::ThreadWrapper(AAH_DecoderPump* owner) 509 : Thread(false /* canCallJava*/ ) 510 , owner_(owner) { 511 } 512 513 bool AAH_DecoderPump::ThreadWrapper::threadLoop() { 514 CHECK(NULL != owner_); 515 owner_->workThread(); 516 return false; 517 } 518 519 } // namespace android 520