Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: media/audio/win/audio_low_latency_output_win.cc

Issue 12220076: Ensures that WASAPI audio output does not go silent in rare cases (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added DLOG_IF Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/audio/win/audio_low_latency_output_win.h" 5 #include "media/audio/win/audio_low_latency_output_win.h"
6 6
7 #include <Functiondiscoverykeys_devpkey.h> 7 #include <Functiondiscoverykeys_devpkey.h>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h" 10 #include "base/debug/trace_event.h"
(...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 // the risk of overwriting previously written data that the audio 461 // the risk of overwriting previously written data that the audio
462 // engine has not yet read from the buffer. 462 // engine has not yet read from the buffer.
463 size_t num_available_frames = 0; 463 size_t num_available_frames = 0;
464 464
465 if (share_mode_ == AUDCLNT_SHAREMODE_SHARED) { 465 if (share_mode_ == AUDCLNT_SHAREMODE_SHARED) {
466 // Get the padding value which represents the amount of rendering 466 // Get the padding value which represents the amount of rendering
467 // data that is queued up to play in the endpoint buffer. 467 // data that is queued up to play in the endpoint buffer.
468 hr = audio_client_->GetCurrentPadding(&num_queued_frames); 468 hr = audio_client_->GetCurrentPadding(&num_queued_frames);
469 num_available_frames = 469 num_available_frames =
470 endpoint_buffer_size_frames_ - num_queued_frames; 470 endpoint_buffer_size_frames_ - num_queued_frames;
471 if (FAILED(hr)) {
472 DLOG(ERROR) << "Failed to retrieve amount of available space: "
473 << std::hex << hr;
474 continue;
475 }
471 } else { 476 } else {
472 // While the stream is running, the system alternately sends one 477 // While the stream is running, the system alternately sends one
473 // buffer or the other to the client. This form of double buffering 478 // buffer or the other to the client. This form of double buffering
474 // is referred to as "ping-ponging". Each time the client receives 479 // is referred to as "ping-ponging". Each time the client receives
475 // a buffer from the system (triggers this event) the client must 480 // a buffer from the system (triggers this event) the client must
476 // process the entire buffer. Calls to the GetCurrentPadding method 481 // process the entire buffer. Calls to the GetCurrentPadding method
477 // are unnecessary because the packet size must always equal the 482 // are unnecessary because the packet size must always equal the
478 // buffer size. In contrast to the shared mode buffering scheme, 483 // buffer size. In contrast to the shared mode buffering scheme,
479 // the latency for an event-driven, exclusive-mode stream depends 484 // the latency for an event-driven, exclusive-mode stream depends
480 // directly on the buffer size. 485 // directly on the buffer size.
481 num_available_frames = endpoint_buffer_size_frames_; 486 num_available_frames = endpoint_buffer_size_frames_;
482 } 487 }
483 if (FAILED(hr)) { 488
484 DLOG(ERROR) << "Failed to retrieve amount of available space: " 489 // Check if there is enough available space to fit the packet size
485 << std::hex << hr; 490 // specified by the client.
491 if (num_available_frames < packet_size_frames_)
486 continue; 492 continue;
493
494 DLOG_IF(ERROR, num_available_frames % packet_size_frames_ == 0)
tommi (sloooow) - chröme 2013/02/08 15:11:43 !=
495 << "Non-perfect timing detected (num_available_frames="
496 << num_available_frames << ", packet_size_frames="
497 << packet_size_frames_ << ")";
498
499 // Derive the number of packets we need to get from the client to
500 // fill up the available area in the endpoint buffer.
501 // |num_packets| will always be one for exclusive-mode streams and
502 // will be one in most cases for shared mode streams as well.
503 // However, we have found that two packets can sometimes be
504 // required.
505 size_t num_packets = (num_available_frames / packet_size_frames_);
506
507 for (size_t n = 0; n < num_packets; ++n) {
508 // Grab all available space in the rendering endpoint buffer
509 // into which the client can write a data packet.
510 hr = audio_render_client_->GetBuffer(packet_size_frames_,
511 &audio_data);
512 if (FAILED(hr)) {
513 DLOG(ERROR) << "Failed to use rendering audio buffer: "
514 << std::hex << hr;
515 continue;
516 }
517
518 // Derive the audio delay which corresponds to the delay between
519 // a render event and the time when the first audio sample in a
520 // packet is played out through the speaker. This delay value
521 // can typically be utilized by an acoustic echo-control (AEC)
522 // unit at the render side.
523 UINT64 position = 0;
524 int audio_delay_bytes = 0;
525 hr = audio_clock->GetPosition(&position, NULL);
526 if (SUCCEEDED(hr)) {
527 // Stream position of the sample that is currently playing
528 // through the speaker.
529 double pos_sample_playing_frames = format_.Format.nSamplesPerSec *
530 (static_cast<double>(position) / device_frequency);
531
532 // Stream position of the last sample written to the endpoint
533 // buffer. Note that, the packet we are about to receive in
534 // the upcoming callback is also included.
535 size_t pos_last_sample_written_frames =
536 num_written_frames_ + packet_size_frames_;
537
538 // Derive the actual delay value which will be fed to the
539 // render client using the OnMoreData() callback.
540 audio_delay_bytes = (pos_last_sample_written_frames -
541 pos_sample_playing_frames) * format_.Format.nBlockAlign;
542 }
543
544 // Read a data packet from the registered client source and
545 // deliver a delay estimate in the same callback to the client.
546 // A time stamp is also stored in the AudioBuffersState. This
547 // time stamp can be used at the client side to compensate for
548 // the delay between the usage of the delay value and the time
549 // of generation.
550
551 uint32 num_filled_bytes = 0;
552 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3;
553
554 int frames_filled = source_->OnMoreData(
555 audio_bus_.get(), AudioBuffersState(0, audio_delay_bytes));
556 num_filled_bytes = frames_filled * format_.Format.nBlockAlign;
557 DCHECK_LE(num_filled_bytes, packet_size_bytes_);
558
559 // Note: If this ever changes to output raw float the data must be
560 // clipped and sanitized since it may come from an untrusted
561 // source such as NaCl.
562 audio_bus_->ToInterleaved(
563 frames_filled, bytes_per_sample, audio_data);
564
565 // Perform in-place, software-volume adjustments.
566 media::AdjustVolume(audio_data,
567 num_filled_bytes,
568 audio_bus_->channels(),
569 bytes_per_sample,
570 volume_);
571
572 // Release the buffer space acquired in the GetBuffer() call.
573 // Render silence if we were not able to fill up the buffer totally.
574 DWORD flags = (num_filled_bytes < packet_size_bytes_) ?
575 AUDCLNT_BUFFERFLAGS_SILENT : 0;
576 audio_render_client_->ReleaseBuffer(packet_size_frames_,
577 flags);
578
579 num_written_frames_ += packet_size_frames_;
487 } 580 }
488
489 // It can happen that we were not able to find a a perfect match
490 // between the native device rate and the endpoint buffer size.
491 // In this case, we are using a packet size which equals the enpoint
492 // buffer size (does not lead to lowest possible delay and is rare
493 // case) and must therefore wait for yet another callback until we
494 // are able to provide data.
495 if ((num_available_frames > 0) &&
496 (num_available_frames != packet_size_frames_)) {
497 continue;
498 }
499
500 // Grab all available space in the rendering endpoint buffer
501 // into which the client can write a data packet.
502 hr = audio_render_client_->GetBuffer(packet_size_frames_,
503 &audio_data);
504 if (FAILED(hr)) {
505 DLOG(ERROR) << "Failed to use rendering audio buffer: "
506 << std::hex << hr;
507 continue;
508 }
509
510 // Derive the audio delay which corresponds to the delay between
511 // a render event and the time when the first audio sample in a
512 // packet is played out through the speaker. This delay value
513 // can typically be utilized by an acoustic echo-control (AEC)
514 // unit at the render side.
515 UINT64 position = 0;
516 int audio_delay_bytes = 0;
517 hr = audio_clock->GetPosition(&position, NULL);
518 if (SUCCEEDED(hr)) {
519 // Stream position of the sample that is currently playing
520 // through the speaker.
521 double pos_sample_playing_frames = format_.Format.nSamplesPerSec *
522 (static_cast<double>(position) / device_frequency);
523
524 // Stream position of the last sample written to the endpoint
525 // buffer. Note that, the packet we are about to receive in
526 // the upcoming callback is also included.
527 size_t pos_last_sample_written_frames =
528 num_written_frames_ + packet_size_frames_;
529
530 // Derive the actual delay value which will be fed to the
531 // render client using the OnMoreData() callback.
532 audio_delay_bytes = (pos_last_sample_written_frames -
533 pos_sample_playing_frames) * format_.Format.nBlockAlign;
534 }
535
536 // Read a data packet from the registered client source and
537 // deliver a delay estimate in the same callback to the client.
538 // A time stamp is also stored in the AudioBuffersState. This
539 // time stamp can be used at the client side to compensate for
540 // the delay between the usage of the delay value and the time
541 // of generation.
542
543 uint32 num_filled_bytes = 0;
544 const int bytes_per_sample = format_.Format.wBitsPerSample >> 3;
545
546 int frames_filled = source_->OnMoreData(
547 audio_bus_.get(), AudioBuffersState(0, audio_delay_bytes));
548 num_filled_bytes = frames_filled * format_.Format.nBlockAlign;
549 DCHECK_LE(num_filled_bytes, packet_size_bytes_);
550
551 // Note: If this ever changes to output raw float the data must be
552 // clipped and sanitized since it may come from an untrusted
553 // source such as NaCl.
554 audio_bus_->ToInterleaved(
555 frames_filled, bytes_per_sample, audio_data);
556
557 // Perform in-place, software-volume adjustments.
558 media::AdjustVolume(audio_data,
559 num_filled_bytes,
560 audio_bus_->channels(),
561 bytes_per_sample,
562 volume_);
563
564 // Zero out the part of the packet which has not been filled by
565 // the client. Using silence is the least bad option in this
566 // situation.
567 if (num_filled_bytes < packet_size_bytes_) {
568 memset(&audio_data[num_filled_bytes], 0,
569 (packet_size_bytes_ - num_filled_bytes));
570 }
571
572 // Release the buffer space acquired in the GetBuffer() call.
573 DWORD flags = 0;
574 audio_render_client_->ReleaseBuffer(packet_size_frames_,
575 flags);
576
577 num_written_frames_ += packet_size_frames_;
578 } 581 }
579 break; 582 break;
580 default: 583 default:
581 error = true; 584 error = true;
582 break; 585 break;
583 } 586 }
584 } 587 }
585 588
586 if (playing && error) { 589 if (playing && error) {
587 // Stop audio rendering since something has gone wrong in our main thread 590 // Stop audio rendering since something has gone wrong in our main thread
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr; 681 DVLOG(1) << "IAudioClient::GetBufferSize: " << std::hex << hr;
679 return hr; 682 return hr;
680 } 683 }
681 684
682 *endpoint_buffer_size = buffer_size_in_frames; 685 *endpoint_buffer_size = buffer_size_in_frames;
683 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames; 686 DVLOG(2) << "endpoint buffer size: " << buffer_size_in_frames;
684 return hr; 687 return hr;
685 } 688 }
686 689
687 } // namespace media 690 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698