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

Side by Side Diff: Source/modules/webaudio/AudioContext.cpp

Issue 1140723003: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Removing ThreadSafeRefCounted from ScheduledSuspendContainer Created 5 years, 5 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 } 723 }
724 724
725 void AudioContext::notifyStateChange() 725 void AudioContext::notifyStateChange()
726 { 726 {
727 dispatchEvent(Event::create(EventTypeNames::statechange)); 727 dispatchEvent(Event::create(EventTypeNames::statechange));
728 } 728 }
729 729
730 ScriptPromise AudioContext::suspendContext(ScriptState* scriptState) 730 ScriptPromise AudioContext::suspendContext(ScriptState* scriptState)
731 { 731 {
732 ASSERT(isMainThread()); 732 ASSERT(isMainThread());
733 ASSERT(!isOfflineContext());
734
733 AutoLocker locker(this); 735 AutoLocker locker(this);
734 736
735 if (isOfflineContext()) {
736 return ScriptPromise::rejectWithDOMException(
737 scriptState,
738 DOMException::create(
739 InvalidAccessError,
740 "cannot suspend an OfflineAudioContext"));
741 }
742
743 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 737 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
744 ScriptPromise promise = resolver->promise(); 738 ScriptPromise promise = resolver->promise();
745 739
746 if (m_contextState == Closed) { 740 if (m_contextState == Closed) {
747 resolver->reject( 741 resolver->reject(
748 DOMException::create(InvalidStateError, "Cannot suspend a context th at has been closed")); 742 DOMException::create(InvalidStateError, "Cannot suspend a context th at has been closed"));
749 } else { 743 } else {
750 // Stop rendering now. 744 // Stop rendering now.
751 if (m_destinationNode) 745 if (m_destinationNode)
752 stopRendering(); 746 stopRendering();
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 835
842 // Find AudioBufferSourceNodes to see if we can stop playing them. 836 // Find AudioBufferSourceNodes to see if we can stop playing them.
843 for (AudioNode* node : m_activeSourceNodes) { 837 for (AudioNode* node : m_activeSourceNodes) {
844 if (node->handler().nodeType() == AudioHandler::NodeTypeAudioBufferSourc e) { 838 if (node->handler().nodeType() == AudioHandler::NodeTypeAudioBufferSourc e) {
845 AudioBufferSourceNode* sourceNode = static_cast<AudioBufferSourceNod e*>(node); 839 AudioBufferSourceNode* sourceNode = static_cast<AudioBufferSourceNod e*>(node);
846 sourceNode->audioBufferSourceHandler().handleStoppableSourceNode(); 840 sourceNode->audioBufferSourceHandler().handleStoppableSourceNode();
847 } 841 }
848 } 842 }
849 } 843 }
850 844
845 void AudioContext::performPostRenderTasks()
846 {
847 // Take care of AudioNode tasks where the tryLock() failed previously.
848 deferredTaskHandler().breakConnections();
849
850 // Dynamically clean up nodes which are no longer needed.
851 releaseFinishedSourceNodes();
852
853 deferredTaskHandler().handleDeferredTasks();
854 deferredTaskHandler().requestToDeleteHandlersOnMainThread();
855 }
856
851 void AudioContext::handlePreRenderTasks() 857 void AudioContext::handlePreRenderTasks()
852 { 858 {
853 ASSERT(isAudioThread()); 859 ASSERT(isAudioThread());
854 860
861 // For the precise offline audio rendering, we need these tasks to be
862 // performed every render quantum. Thus, we wait for the graph lock (rather
863 // than tryLock()) so we can run the pre-render tasks without deferring
864 // them.
865 //
866 // Also note that we do not need to run resolvePromiseForResume() here
867 // because the offline audio context keeps its own suspend/resume promise
868 // resolvers separately.
869 if (isOfflineContext()) {
870 deferredTaskHandler().forceLock();
871 deferredTaskHandler().handleDeferredTasks();
872 handleStoppableSourceNodes();
873 unlock();
874 return;
875 }
876
855 // At the beginning of every render quantum, try to update the internal rend ering graph state (from main thread changes). 877 // At the beginning of every render quantum, try to update the internal rend ering graph state (from main thread changes).
856 // It's OK if the tryLock() fails, we'll just take slightly longer to pick u p the changes. 878 // It's OK if the tryLock() fails, we'll just take slightly longer to pick u p the changes.
857 if (tryLock()) { 879 if (tryLock()) {
858 deferredTaskHandler().handleDeferredTasks(); 880 deferredTaskHandler().handleDeferredTasks();
859 881
860 resolvePromisesForResume(); 882 resolvePromisesForResume();
861 883
862 // Check to see if source nodes can be stopped because the end time has passed. 884 // Check to see if source nodes can be stopped because the end time has passed.
863 handleStoppableSourceNodes(); 885 handleStoppableSourceNodes();
864 886
865 unlock(); 887 unlock();
866 } 888 }
867 } 889 }
868 890
869 void AudioContext::handlePostRenderTasks() 891 void AudioContext::handlePostRenderTasks()
870 { 892 {
871 ASSERT(isAudioThread()); 893 ASSERT(isAudioThread());
872 894
895 // Same as |handlePreRenderTasks()|. We force to perform the post-render
896 // tasks for the offline rendering.
897 if (isOfflineContext()) {
898 deferredTaskHandler().forceLock();
899 performPostRenderTasks();
900 unlock();
901 return;
902 }
903
873 // Must use a tryLock() here too. Don't worry, the lock will very rarely be contended and this method is called frequently. 904 // Must use a tryLock() here too. Don't worry, the lock will very rarely be contended and this method is called frequently.
874 // The worst that can happen is that there will be some nodes which will tak e slightly longer than usual to be deleted or removed 905 // The worst that can happen is that there will be some nodes which will tak e slightly longer than usual to be deleted or removed
875 // from the render graph (in which case they'll render silence). 906 // from the render graph (in which case they'll render silence).
876 if (tryLock()) { 907 if (tryLock()) {
877 // Take care of AudioNode tasks where the tryLock() failed previously. 908 performPostRenderTasks();
878 deferredTaskHandler().breakConnections();
879
880 // Dynamically clean up nodes which are no longer needed.
881 releaseFinishedSourceNodes();
882
883 deferredTaskHandler().handleDeferredTasks();
884 deferredTaskHandler().requestToDeleteHandlersOnMainThread();
885
886 unlock(); 909 unlock();
887 } 910 }
888 } 911 }
889 912
890 void AudioContext::resolvePromisesForResumeOnMainThread() 913 void AudioContext::resolvePromisesForResumeOnMainThread()
891 { 914 {
892 ASSERT(isMainThread()); 915 ASSERT(isMainThread());
893 AutoLocker locker(this); 916 AutoLocker locker(this);
894 917
895 for (auto& resolver : m_resumeResolvers) { 918 for (auto& resolver : m_resumeResolvers) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 return EventTargetNames::AudioContext; 961 return EventTargetNames::AudioContext;
939 } 962 }
940 963
941 ExecutionContext* AudioContext::executionContext() const 964 ExecutionContext* AudioContext::executionContext() const
942 { 965 {
943 return m_isStopScheduled ? 0 : ActiveDOMObject::executionContext(); 966 return m_isStopScheduled ? 0 : ActiveDOMObject::executionContext();
944 } 967 }
945 968
946 void AudioContext::startRendering() 969 void AudioContext::startRendering()
947 { 970 {
948 // This is called for both online and offline contexts. 971 // This is only for the real-time context.
949 ASSERT(isMainThread()); 972 ASSERT(isMainThread());
950 ASSERT(m_destinationNode); 973 ASSERT(m_destinationNode);
974 ASSERT(!isOfflineContext());
951 975
952 if (m_contextState == Suspended) { 976 if (m_contextState == Suspended) {
953 destination()->audioDestinationHandler().startRendering(); 977 destination()->audioDestinationHandler().startRendering();
954 setContextState(Running); 978 setContextState(Running);
955 } 979 }
956 } 980 }
957 981
958 void AudioContext::stopRendering() 982 void AudioContext::stopRendering()
959 { 983 {
960 ASSERT(isMainThread()); 984 ASSERT(isMainThread());
961 ASSERT(m_destinationNode); 985 ASSERT(m_destinationNode);
962 ASSERT(!isOfflineContext()); 986 ASSERT(!isOfflineContext());
963 987
964 if (m_contextState == Running) { 988 if (m_contextState == Running) {
965 destination()->audioDestinationHandler().stopRendering(); 989 destination()->audioDestinationHandler().stopRendering();
966 setContextState(Suspended); 990 setContextState(Suspended);
967 deferredTaskHandler().clearHandlersToBeDeleted(); 991 deferredTaskHandler().clearHandlersToBeDeleted();
968 } 992 }
969 } 993 }
970 994
971 void AudioContext::fireCompletionEvent() 995 void AudioContext::fireCompletionEvent()
972 { 996 {
973 ASSERT(isMainThread()); 997 ASSERT_WITH_MESSAGE(1, "fireCompletionEvent() only valid for offline audio c ontext");
974 if (!isMainThread()) 998 }
975 return;
976 999
977 AudioBuffer* renderedBuffer = m_renderTarget.get(); 1000 bool AudioContext::shouldSuspendNow()
1001 {
1002 ASSERT_WITH_MESSAGE(1, "shouldSuspendNow() only valid for offline audio cont ext");
1003 return false;
1004 }
978 1005
979 // For an offline context, we set the state to closed here so that the oncom plete handler sees 1006 void AudioContext::resolvePendingSuspendPromises()
980 // that the context has been closed. 1007 {
981 setContextState(Closed); 1008 ASSERT_WITH_MESSAGE(1, "clearResolvedSuspend() only valid for offline audio context");
982
983 ASSERT(renderedBuffer);
984 if (!renderedBuffer)
985 return;
986
987 // Avoid firing the event if the document has already gone away.
988 if (executionContext()) {
989 // Call the offline rendering completion event listener and resolve the promise too.
990 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
991 m_offlineResolver->resolve(renderedBuffer);
992 }
993 } 1009 }
994 1010
995 DEFINE_TRACE(AudioContext) 1011 DEFINE_TRACE(AudioContext)
996 { 1012 {
997 visitor->trace(m_closeResolver); 1013 visitor->trace(m_closeResolver);
998 visitor->trace(m_offlineResolver);
999 visitor->trace(m_renderTarget); 1014 visitor->trace(m_renderTarget);
1000 visitor->trace(m_destinationNode); 1015 visitor->trace(m_destinationNode);
1001 visitor->trace(m_listener); 1016 visitor->trace(m_listener);
1002 // trace() can be called in AudioContext constructor, and 1017 // trace() can be called in AudioContext constructor, and
1003 // m_contextGraphMutex might be unavailable. 1018 // m_contextGraphMutex might be unavailable.
1004 if (m_didInitializeContextGraphMutex) { 1019 if (m_didInitializeContextGraphMutex) {
1005 AutoLocker lock(this); 1020 AutoLocker lock(this);
1006 visitor->trace(m_activeSourceNodes); 1021 visitor->trace(m_activeSourceNodes);
1007 } else { 1022 } else {
1008 visitor->trace(m_activeSourceNodes); 1023 visitor->trace(m_activeSourceNodes);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 // the destination node can GCed if JS has no references. stop() will also r esolve the Promise 1060 // the destination node can GCed if JS has no references. stop() will also r esolve the Promise
1046 // created here. 1061 // created here.
1047 stop(); 1062 stop();
1048 1063
1049 return promise; 1064 return promise;
1050 } 1065 }
1051 1066
1052 } // namespace blink 1067 } // namespace blink
1053 1068
1054 #endif // ENABLE(WEB_AUDIO) 1069 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698