1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "DeviceInputTrack.h"
#include "Tracing.h"
namespace mozilla {
#ifdef LOG_INTERNAL
# undef LOG_INTERNAL
#endif // LOG_INTERNAL
#define LOG_INTERNAL(level, msg, ...) \
MOZ_LOG(gMediaTrackGraphLog, LogLevel::level, (msg, ##__VA_ARGS__))
#ifdef LOG
# undef LOG
#endif // LOG
#define LOG(msg, ...) LOG_INTERNAL(Debug, msg, ##__VA_ARGS__)
#ifdef LOGE
# undef LOGE
#endif // LOGE
#define LOGE(msg, ...) LOG_INTERNAL(Error, msg, ##__VA_ARGS__)
// This can only be called in graph thread since mGraph->CurrentDriver() is
// graph thread only
#ifdef TRACK_GRAPH_LOG_INTERNAL
# undef TRACK_GRAPH_LOG_INTERNAL
#endif // TRACK_GRAPH_LOG_INTERNAL
#define TRACK_GRAPH_LOG_INTERNAL(level, msg, ...) \
LOG_INTERNAL(level, "(Graph %p, Driver %p) DeviceInputTrack %p, " msg, \
this->mGraph, this->mGraph->CurrentDriver(), this, \
##__VA_ARGS__)
#ifdef TRACK_GRAPH_LOG
# undef TRACK_GRAPH_LOG
#endif // TRACK_GRAPH_LOG
#define TRACK_GRAPH_LOG(msg, ...) \
TRACK_GRAPH_LOG_INTERNAL(Debug, msg, ##__VA_ARGS__)
#ifdef TRACK_GRAPH_LOGV
# undef TRACK_GRAPH_LOGV
#endif // TRACK_GRAPH_LOGV
#define TRACK_GRAPH_LOGV(msg, ...) \
TRACK_GRAPH_LOG_INTERNAL(Verbose, msg, ##__VA_ARGS__)
#ifdef TRACK_GRAPH_LOGE
# undef TRACK_GRAPH_LOGE
#endif // TRACK_GRAPH_LOGE
#define TRACK_GRAPH_LOGE(msg, ...) \
TRACK_GRAPH_LOG_INTERNAL(Error, msg, ##__VA_ARGS__)
#ifdef CONSUMER_GRAPH_LOG_INTERNAL
# undef CONSUMER_GRAPH_LOG_INTERNAL
#endif // CONSUMER_GRAPH_LOG_INTERNAL
#define CONSUMER_GRAPH_LOG_INTERNAL(level, msg, ...) \
LOG_INTERNAL( \
level, "(Graph %p, Driver %p) DeviceInputConsumerTrack %p, " msg, \
this->mGraph, this->mGraph->CurrentDriver(), this, ##__VA_ARGS__)
#ifdef CONSUMER_GRAPH_LOGV
# undef CONSUMER_GRAPH_LOGV
#endif // CONSUMER_GRAPH_LOGV
#define CONSUMER_GRAPH_LOGV(msg, ...) \
CONSUMER_GRAPH_LOG_INTERNAL(Verbose, msg, ##__VA_ARGS__)
DeviceInputConsumerTrack::DeviceInputConsumerTrack(TrackRate aSampleRate)
: ProcessedMediaTrack(aSampleRate, MediaSegment::AUDIO,
new AudioSegment()) {}
void DeviceInputConsumerTrack::ConnectDeviceInput(
CubebUtils::AudioDeviceID aId, AudioDataListener* aListener,
const PrincipalHandle& aPrincipal) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(Graph());
MOZ_ASSERT(aListener);
MOZ_ASSERT(!mListener);
MOZ_ASSERT(!mDeviceInputTrack);
MOZ_ASSERT(mDeviceId.isNothing());
MOZ_ASSERT(!mDeviceInputTrack,
"Must disconnect a device input before connecting a new one");
mListener = aListener;
mDeviceId.emplace(aId);
mDeviceInputTrack =
DeviceInputTrack::OpenAudio(Graph(), aId, aPrincipal, this);
LOG("Open device %p (DeviceInputTrack %p) for consumer %p", aId,
mDeviceInputTrack.get(), this);
mPort = AllocateInputPort(mDeviceInputTrack.get());
}
void DeviceInputConsumerTrack::DisconnectDeviceInput() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(Graph());
if (!mListener) {
MOZ_ASSERT(mDeviceId.isNothing());
MOZ_ASSERT(!mDeviceInputTrack);
return;
}
MOZ_ASSERT(mPort);
MOZ_ASSERT(mDeviceInputTrack);
MOZ_ASSERT(mDeviceId.isSome());
LOG("Close device %p (DeviceInputTrack %p) for consumer %p ", *mDeviceId,
mDeviceInputTrack.get(), this);
mPort->Destroy();
DeviceInputTrack::CloseAudio(mDeviceInputTrack.forget(), this);
mListener = nullptr;
mDeviceId = Nothing();
}
Maybe<CubebUtils::AudioDeviceID> DeviceInputConsumerTrack::DeviceId() const {
MOZ_ASSERT(NS_IsMainThread());
return mDeviceId;
}
NotNull<AudioDataListener*> DeviceInputConsumerTrack::GetAudioDataListener()
const {
MOZ_ASSERT(NS_IsMainThread());
return WrapNotNull(mListener.get());
}
bool DeviceInputConsumerTrack::ConnectedToNativeDevice() const {
MOZ_ASSERT(NS_IsMainThread());
return mDeviceInputTrack && mDeviceInputTrack->AsNativeInputTrack();
}
bool DeviceInputConsumerTrack::ConnectedToNonNativeDevice() const {
MOZ_ASSERT(NS_IsMainThread());
return mDeviceInputTrack && mDeviceInputTrack->AsNonNativeInputTrack();
}
DeviceInputTrack* DeviceInputConsumerTrack::GetDeviceInputTrackGraphThread()
const {
AssertOnGraphThread();
if (mInputs.IsEmpty()) {
return nullptr;
}
MOZ_ASSERT(mInputs.Length() == 1);
MediaTrack* track = mInputs[0]->GetSource();
MOZ_ASSERT(track->AsDeviceInputTrack());
return static_cast<DeviceInputTrack*>(track);
}
void DeviceInputConsumerTrack::GetInputSourceData(AudioSegment& aOutput,
GraphTime aFrom,
GraphTime aTo) const {
AssertOnGraphThread();
MOZ_ASSERT(aOutput.IsEmpty());
MOZ_ASSERT(mInputs.Length() == 1);
MediaInputPort* port = mInputs[0];
MediaTrack* source = port->GetSource();
GraphTime next;
for (GraphTime t = aFrom; t < aTo; t = next) {
MediaInputPort::InputInterval interval =
MediaInputPort::GetNextInputInterval(port, t);
interval.mEnd = std::min(interval.mEnd, aTo);
const bool inputEnded =
source->Ended() &&
source->GetEnd() <=
source->GraphTimeToTrackTimeWithBlocking(interval.mStart);
TrackTime ticks = interval.mEnd - interval.mStart;
next = interval.mEnd;
if (interval.mStart >= interval.mEnd) {
break;
}
if (inputEnded) {
aOutput.AppendNullData(ticks);
CONSUMER_GRAPH_LOGV(
"Getting %" PRId64
" ticks of null data from input port source (ended input)",
ticks);
} else if (interval.mInputIsBlocked) {
aOutput.AppendNullData(ticks);
CONSUMER_GRAPH_LOGV(
"Getting %" PRId64
" ticks of null data from input port source (blocked input)",
ticks);
} else if (source->IsSuspended()) {
aOutput.AppendNullData(ticks);
CONSUMER_GRAPH_LOGV(
"Getting %" PRId64
" ticks of null data from input port source (source is suspended)",
ticks);
} else {
TrackTime start =
source->GraphTimeToTrackTimeWithBlocking(interval.mStart);
TrackTime end = source->GraphTimeToTrackTimeWithBlocking(interval.mEnd);
MOZ_ASSERT(source->GetData<AudioSegment>()->GetDuration() >= end);
aOutput.AppendSlice(*source->GetData<AudioSegment>(), start, end);
CONSUMER_GRAPH_LOGV("Getting %" PRId64
" ticks of real data from input port source %p",
end - start, source);
}
}
}
/* static */
NotNull<RefPtr<DeviceInputTrack>> DeviceInputTrack::OpenAudio(
MediaTrackGraph* aGraph, CubebUtils::AudioDeviceID aDeviceId,
const PrincipalHandle& aPrincipalHandle,
DeviceInputConsumerTrack* aConsumer) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aConsumer);
MOZ_ASSERT(aGraph == aConsumer->Graph());
RefPtr<DeviceInputTrack> track =
aGraph->GetDeviceInputTrackMainThread(aDeviceId);
if (track) {
MOZ_ASSERT(!track->mConsumerTracks.IsEmpty());
track->AddDataListener(aConsumer->GetAudioDataListener());
} else {
// Create a NativeInputTrack or NonNativeInputTrack, depending on whether
// the given graph already has a native device or not.
if (aGraph->GetNativeInputTrackMainThread()) {
// A native device is already in use. This device will be a non-native
// device.
track = new NonNativeInputTrack(aGraph->GraphRate(), aDeviceId,
aPrincipalHandle);
} else {
// No native device is in use. This device will be the native device.
track = new NativeInputTrack(aGraph->GraphRate(), aDeviceId,
aPrincipalHandle);
}
LOG("Create %sNativeInputTrack %p in MTG %p for device %p",
(track->AsNativeInputTrack() ? "" : "Non"), track.get(), aGraph,
aDeviceId);
aGraph->AddTrack(track);
// Add the listener before opening the device so the device passed to
// OpenAudioInput always has a non-zero input channel count.
track->AddDataListener(aConsumer->GetAudioDataListener());
aGraph->OpenAudioInput(track);
}
MOZ_ASSERT(track->AsNativeInputTrack() || track->AsNonNativeInputTrack());
MOZ_ASSERT(track->mDeviceId == aDeviceId);
MOZ_ASSERT(!track->mConsumerTracks.Contains(aConsumer));
track->mConsumerTracks.AppendElement(aConsumer);
LOG("DeviceInputTrack %p (device %p: %snative) in MTG %p has %zu users now",
track.get(), track->mDeviceId,
(track->AsNativeInputTrack() ? "" : "non-"), aGraph,
track->mConsumerTracks.Length());
if (track->mConsumerTracks.Length() > 1) {
track->ReevaluateInputDevice();
}
return WrapNotNull(track);
}
/* static */
void DeviceInputTrack::CloseAudio(already_AddRefed<DeviceInputTrack> aTrack,
DeviceInputConsumerTrack* aConsumer) {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<DeviceInputTrack> track = aTrack;
MOZ_ASSERT(track);
track->RemoveDataListener(aConsumer->GetAudioDataListener());
DebugOnly<bool> removed = track->mConsumerTracks.RemoveElement(aConsumer);
MOZ_ASSERT(removed);
LOG("DeviceInputTrack %p (device %p) in MTG %p has %zu users now",
track.get(), track->mDeviceId, track->Graph(),
track->mConsumerTracks.Length());
if (track->mConsumerTracks.IsEmpty()) {
track->Graph()->CloseAudioInput(track);
track->Destroy();
} else {
track->ReevaluateInputDevice();
}
}
const nsTArray<RefPtr<DeviceInputConsumerTrack>>&
DeviceInputTrack::GetConsumerTracks() const {
MOZ_ASSERT(NS_IsMainThread());
return mConsumerTracks;
}
DeviceInputTrack::DeviceInputTrack(TrackRate aSampleRate,
CubebUtils::AudioDeviceID aDeviceId,
const PrincipalHandle& aPrincipalHandle)
: ProcessedMediaTrack(aSampleRate, MediaSegment::AUDIO, new AudioSegment()),
mDeviceId(aDeviceId),
mPrincipalHandle(aPrincipalHandle) {}
uint32_t DeviceInputTrack::MaxRequestedInputChannels() const {
AssertOnGraphThreadOrNotRunning();
uint32_t maxInputChannels = 0;
for (const auto& listener : mListeners) {
maxInputChannels = std::max(maxInputChannels,
listener->RequestedInputChannelCount(mGraph));
}
return maxInputChannels;
}
bool DeviceInputTrack::HasVoiceInput() const {
AssertOnGraphThreadOrNotRunning();
for (const auto& listener : mListeners) {
if (listener->IsVoiceInput(mGraph)) {
return true;
}
}
return false;
}
AudioInputProcessingParamsRequest
DeviceInputTrack::UpdateRequestedProcessingParams() {
AssertOnGraphThreadOrNotRunning();
Maybe<cubeb_input_processing_params> params;
for (const auto& listener : mListeners) {
if (params) {
*params &= listener->RequestedInputProcessingParams(mGraph);
} else {
params = Some(listener->RequestedInputProcessingParams(mGraph));
}
}
if (auto p = params.valueOr(CUBEB_INPUT_PROCESSING_PARAM_NONE);
p != mProcessingParamsRequest.mParams) {
mProcessingParamsRequest.mParams = p;
mProcessingParamsRequest.mGeneration =
Graph()->ProcessingParamsGeneration();
TRACK_GRAPH_LOG(
"%sNativeInputTrack notifying of setting requested processing params "
"%s (Gen %d)",
(AsNonNativeInputTrack() ? "Non" : ""),
CubebUtils::ProcessingParamsToString(mProcessingParamsRequest.mParams)
.get(),
mProcessingParamsRequest.mGeneration);
NotifySetRequestedProcessingParams(Graph(),
mProcessingParamsRequest.mGeneration,
mProcessingParamsRequest.mParams);
}
return mProcessingParamsRequest;
}
void DeviceInputTrack::DeviceChanged(MediaTrackGraph* aGraph) const {
AssertOnGraphThreadOrNotRunning();
MOZ_ASSERT(aGraph == mGraph,
"Receive device changed signal from another graph");
TRACK_GRAPH_LOG("DeviceChanged");
for (const auto& listener : mListeners) {
listener->DeviceChanged(aGraph);
}
}
void DeviceInputTrack::NotifySetRequestedProcessingParams(
MediaTrackGraph* aGraph, int aGeneration,
cubeb_input_processing_params aRequestedParams) {
AssertOnGraphThread();
for (const auto& listener : mListeners) {
listener->NotifySetRequestedInputProcessingParams(mGraph, aGeneration,
aRequestedParams);
}
}
void DeviceInputTrack::NotifySetRequestedProcessingParamsResult(
MediaTrackGraph* aGraph, int aGeneration,
const Result<cubeb_input_processing_params, int>& aResult) {
AssertOnGraphThread();
for (const auto& listener : mListeners) {
listener->NotifySetRequestedInputProcessingParamsResult(mGraph, aGeneration,
aResult);
}
}
void DeviceInputTrack::ReevaluateInputDevice() {
MOZ_ASSERT(NS_IsMainThread());
QueueControlMessageWithNoShutdown([self = RefPtr{this}, this] {
TRACE("DeviceInputTrack::ReevaluateInputDevice ControlMessage");
Graph()->ReevaluateInputDevice(mDeviceId);
});
}
void DeviceInputTrack::AddDataListener(AudioDataListener* aListener) {
MOZ_ASSERT(NS_IsMainThread());
QueueControlMessageWithNoShutdown(
[self = RefPtr{this}, this, listener = RefPtr{aListener}] {
TRACE("DeviceInputTrack::AddDataListener ControlMessage");
MOZ_ASSERT(!mListeners.Contains(listener.get()),
"Don't add a listener twice.");
mListeners.AppendElement(listener.get());
});
}
void DeviceInputTrack::RemoveDataListener(AudioDataListener* aListener) {
MOZ_ASSERT(NS_IsMainThread());
QueueControlMessageWithNoShutdown(
[self = RefPtr{this}, this, listener = RefPtr{aListener}] {
TRACE("DeviceInputTrack::RemoveDataListener ControlMessage");
DebugOnly<bool> wasPresent = mListeners.RemoveElement(listener.get());
MOZ_ASSERT(wasPresent, "Remove an unknown listener");
listener->Disconnect(Graph());
});
}
NativeInputTrack::NativeInputTrack(TrackRate aSampleRate,
CubebUtils::AudioDeviceID aDeviceId,
const PrincipalHandle& aPrincipalHandle)
: DeviceInputTrack(aSampleRate, aDeviceId, aPrincipalHandle),
mIsBufferingAppended(false),
mInputChannels(0) {}
void NativeInputTrack::DestroyImpl() {
AssertOnGraphThreadOrNotRunning();
mPendingData.Clear();
ProcessedMediaTrack::DestroyImpl();
}
void NativeInputTrack::ProcessInput(GraphTime aFrom, GraphTime aTo,
uint32_t aFlags) {
AssertOnGraphThread();
TRACE_COMMENT("NativeInputTrack::ProcessInput", "%p", this);
TRACK_GRAPH_LOGV("(Native) ProcessInput from %" PRId64 " to %" PRId64
", needs %" PRId64 " frames",
aFrom, aTo, aTo - aFrom);
TrackTime from = GraphTimeToTrackTime(aFrom);
TrackTime to = GraphTimeToTrackTime(aTo);
MOZ_ASSERT(from < to);
MOZ_ASSERT_IF(!mIsBufferingAppended, mPendingData.IsEmpty());
TrackTime need = to - from;
TrackTime dataNeed = std::min(mPendingData.GetDuration(), need);
TrackTime silenceNeed = std::max(need - dataNeed, (TrackTime)0);
// TODO (bug 1879353): Reenable assertion.
// MOZ_ASSERT_IF(dataNeed > 0, silenceNeed == 0);
GetData<AudioSegment>()->AppendSlice(mPendingData, 0, dataNeed);
mPendingData.RemoveLeading(dataNeed);
GetData<AudioSegment>()->AppendNullData(silenceNeed);
// TODO (bug 1879353): Remove as assertion above will hold.
if (dataNeed > 0 && silenceNeed > 0) {
NotifyInputStopped(mGraph);
}
}
uint32_t NativeInputTrack::NumberOfChannels() const {
AssertOnGraphThreadOrNotRunning();
return mInputChannels;
}
void NativeInputTrack::NotifyInputStopped(MediaTrackGraph* aGraph) {
AssertOnGraphThreadOrNotRunning();
MOZ_ASSERT(aGraph == mGraph,
"Receive input stopped signal from another graph");
TRACK_GRAPH_LOG("(Native) NotifyInputStopped");
mInputChannels = 0;
mIsBufferingAppended = false;
mPendingData.Clear();
}
void NativeInputTrack::NotifyInputData(MediaTrackGraph* aGraph,
const AudioDataValue* aBuffer,
size_t aFrames, TrackRate aRate,
uint32_t aChannels,
uint32_t aAlreadyBuffered) {
AssertOnGraphThread();
MOZ_ASSERT(aGraph == mGraph, "Receive input data from another graph");
TRACK_GRAPH_LOGV(
"NotifyInputData: frames=%zu, rate=%d, channel=%u, alreadyBuffered=%u",
aFrames, aRate, aChannels, aAlreadyBuffered);
if (!mIsBufferingAppended) {
// First time we see live frames getting added. Use what's already buffered
// in the driver's scratch buffer as a starting point.
MOZ_ASSERT(mPendingData.IsEmpty());
constexpr TrackTime buffering = WEBAUDIO_BLOCK_SIZE;
const TrackTime remaining =
buffering - static_cast<TrackTime>(aAlreadyBuffered);
mPendingData.AppendNullData(remaining);
mIsBufferingAppended = true;
TRACK_GRAPH_LOG("Set mIsBufferingAppended by appending %" PRId64 " frames.",
remaining);
}
MOZ_ASSERT(aChannels);
if (!mInputChannels) {
mInputChannels = aChannels;
}
mPendingData.AppendFromInterleavedBuffer(aBuffer, aFrames, aChannels,
mPrincipalHandle);
}
NonNativeInputTrack::NonNativeInputTrack(
TrackRate aSampleRate, CubebUtils::AudioDeviceID aDeviceId,
const PrincipalHandle& aPrincipalHandle)
: DeviceInputTrack(aSampleRate, aDeviceId, aPrincipalHandle),
mAudioSource(nullptr),
mSourceIdNumber(0) {}
void NonNativeInputTrack::DestroyImpl() {
AssertOnGraphThreadOrNotRunning();
if (mAudioSource) {
mAudioSource->Stop();
mAudioSource = nullptr;
}
ProcessedMediaTrack::DestroyImpl();
}
void NonNativeInputTrack::ProcessInput(GraphTime aFrom, GraphTime aTo,
uint32_t aFlags) {
AssertOnGraphThread();
TRACE_COMMENT("NonNativeInputTrack::ProcessInput", "%p", this);
TRACK_GRAPH_LOGV("(NonNative) ProcessInput from %" PRId64 " to %" PRId64
", needs %" PRId64 " frames",
aFrom, aTo, aTo - aFrom);
TrackTime from = GraphTimeToTrackTime(aFrom);
TrackTime to = GraphTimeToTrackTime(aTo);
MOZ_ASSERT(from < to);
TrackTime delta = to - from;
if (!mAudioSource) {
GetData<AudioSegment>()->AppendNullData(delta);
return;
}
AudioInputSource::Consumer consumer = AudioInputSource::Consumer::Same;
// GraphRunner keeps the same thread.
MOZ_ASSERT(!HasGraphThreadChanged());
ReevaluateProcessingParams();
AudioSegment data = mAudioSource->GetAudioSegment(delta, consumer);
MOZ_ASSERT(data.GetDuration() == delta);
GetData<AudioSegment>()->AppendFrom(&data);
}
uint32_t NonNativeInputTrack::NumberOfChannels() const {
AssertOnGraphThreadOrNotRunning();
return mAudioSource ? mAudioSource->mChannelCount : 0;
}
void NonNativeInputTrack::StartAudio(
RefPtr<AudioInputSource>&& aAudioInputSource) {
AssertOnGraphThread();
MOZ_ASSERT(aAudioInputSource->mPrincipalHandle == mPrincipalHandle);
MOZ_ASSERT(aAudioInputSource->mDeviceId == mDeviceId);
TRACK_GRAPH_LOG("StartAudio with source %p", aAudioInputSource.get());
#ifdef DEBUG
mGraphThreadId = std::this_thread::get_id();
#endif
mAudioSource = std::move(aAudioInputSource);
mAudioSource->Init();
ReevaluateProcessingParams();
mAudioSource->Start();
}
void NonNativeInputTrack::StopAudio() {
AssertOnGraphThread();
TRACK_GRAPH_LOG("StopAudio from source %p", mAudioSource.get());
if (!mAudioSource) {
return;
}
mAudioSource->Stop();
mAudioSource = nullptr;
#ifdef DEBUG
mGraphThreadId = std::thread::id();
#endif
}
AudioInputType NonNativeInputTrack::DevicePreference() const {
AssertOnGraphThreadOrNotRunning();
return mAudioSource && mAudioSource->mIsVoice ? AudioInputType::Voice
: AudioInputType::Unknown;
}
void NonNativeInputTrack::NotifyDeviceChanged(uint32_t aSourceId) {
AssertOnGraphThreadOrNotRunning();
// No need to forward the notification if the audio input has been stopped or
// restarted by it users.
if (!mAudioSource || mAudioSource->mId != aSourceId) {
TRACK_GRAPH_LOG("(NonNative) NotifyDeviceChanged: No need to forward");
return;
}
TRACK_GRAPH_LOG("(NonNative) NotifyDeviceChanged");
// Forward the notification.
DeviceInputTrack::DeviceChanged(mGraph);
}
void NonNativeInputTrack::NotifyInputStopped(uint32_t aSourceId) {
AssertOnGraphThreadOrNotRunning();
// No need to forward the notification if the audio input has been stopped or
// restarted by it users.
if (!mAudioSource || mAudioSource->mId != aSourceId) {
TRACK_GRAPH_LOG("(NonNative) NotifyInputStopped: No need to forward");
return;
}
TRACK_GRAPH_LOGE(
"(NonNative) NotifyInputStopped: audio unexpectedly stopped");
// Destory the underlying audio stream if it's stopped unexpectedly.
mAudioSource->Stop();
}
AudioInputSource::Id NonNativeInputTrack::GenerateSourceId() {
AssertOnGraphThread();
return mSourceIdNumber++;
}
void NonNativeInputTrack::ReevaluateProcessingParams() {
AssertOnGraphThread();
MOZ_ASSERT(mAudioSource);
auto request = UpdateRequestedProcessingParams();
if (mRequestedProcessingParamsGeneration == request.mGeneration) {
return;
}
auto generation = mRequestedProcessingParamsGeneration = request.mGeneration;
auto params = request.mParams;
using Promise = AudioInputSource::SetRequestedProcessingParamsPromise;
mAudioSource->SetRequestedProcessingParams(params)->Then(
GetMainThreadSerialEventTarget(), __func__,
[this, self = RefPtr(this),
generation](Promise::ResolveOrRejectValue&& aValue) {
if (IsDestroyed()) {
return;
}
auto result = ([&]() -> Result<cubeb_input_processing_params, int> {
if (aValue.IsResolve()) {
return aValue.ResolveValue();
}
return Err(aValue.RejectValue());
})();
QueueControlMessageWithNoShutdown([this, self = RefPtr(this),
generation,
result = std::move(result)] {
NotifySetRequestedProcessingParamsResult(Graph(), generation, result);
});
});
}
#ifdef DEBUG
bool NonNativeInputTrack::HasGraphThreadChanged() {
AssertOnGraphThread();
std::thread::id currentId = std::this_thread::get_id();
if (mGraphThreadId == currentId) {
return false;
}
mGraphThreadId = currentId;
return true;
}
#endif // DEBUG
AudioInputSourceListener::AudioInputSourceListener(NonNativeInputTrack* aOwner)
: mOwner(aOwner) {}
void AudioInputSourceListener::AudioDeviceChanged(
AudioInputSource::Id aSourceId) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mOwner);
if (mOwner->IsDestroyed()) {
LOG("NonNativeInputTrack %p has been destroyed. No need to forward the "
"audio device-changed notification",
mOwner.get());
return;
}
MOZ_DIAGNOSTIC_ASSERT(mOwner->Graph());
mOwner->QueueControlMessageWithNoShutdown([inputTrack = mOwner, aSourceId] {
TRACE("NonNativeInputTrack::AudioDeviceChanged ControlMessage");
inputTrack->NotifyDeviceChanged(aSourceId);
});
}
void AudioInputSourceListener::AudioStateCallback(
AudioInputSource::Id aSourceId,
AudioInputSource::EventListener::State aState) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mOwner);
const char* state =
aState == AudioInputSource::EventListener::State::Started ? "started"
: aState == AudioInputSource::EventListener::State::Stopped ? "stopped"
: aState == AudioInputSource::EventListener::State::Drained ? "drained"
: "error";
if (mOwner->IsDestroyed()) {
LOG("NonNativeInputTrack %p has been destroyed. No need to forward the "
"audio state-changed(%s) notification",
mOwner.get(), state);
return;
}
if (aState == AudioInputSource::EventListener::State::Started) {
LOG("We can ignore %s notification for NonNativeInputTrack %p", state,
mOwner.get());
return;
}
LOG("Notify audio stopped due to entering %s state", state);
MOZ_DIAGNOSTIC_ASSERT(mOwner->Graph());
mOwner->QueueControlMessageWithNoShutdown([inputTrack = mOwner, aSourceId] {
TRACE("NonNativeInputTrack::AudioStateCallback ControlMessage");
inputTrack->NotifyInputStopped(aSourceId);
});
}
#undef LOG_INTERNAL
#undef LOG
#undef LOGE
#undef TRACK_GRAPH_LOG_INTERNAL
#undef TRACK_GRAPH_LOG
#undef TRACK_GRAPH_LOGV
#undef TRACK_GRAPH_LOGE
#undef CONSUMER_GRAPH_LOG_INTERNAL
#undef CONSUMER_GRAPH_LOGV
} // namespace mozilla
|