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

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: Added UseCounter Created 5 years, 6 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
851 void AudioContext::handlePreRenderTasks() 845 void AudioContext::handlePreRenderTasks()
852 { 846 {
853 ASSERT(isAudioThread()); 847 ASSERT(isAudioThread());
854 848
849 // If this is Offline Audio Context, simply lock the graph and do the tasks.
Raymond Toy 2015/06/15 20:42:13 You should probably mention why it's ok to wait fo
850 // For the precise offline audio rendering, we need these tasks to be
851 // performed every render quantum without deferring them. The trylock()
852 // below is not suitable for this purpose.
853 if (isOfflineContext()) {
854 deferredTaskHandler().forceLock();
855 deferredTaskHandler().handleDeferredTasks();
856 handleStoppableSourceNodes();
857 unlock();
858 return;
859 }
Raymond Toy 2015/06/15 20:42:13 Previous version had this as part of the trylock b
860
855 // At the beginning of every render quantum, try to update the internal rend ering graph state (from main thread changes). 861 // 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. 862 // It's OK if the tryLock() fails, we'll just take slightly longer to pick u p the changes.
857 if (tryLock()) { 863 if (tryLock()) {
858 deferredTaskHandler().handleDeferredTasks(); 864 deferredTaskHandler().handleDeferredTasks();
859 865
860 resolvePromisesForResume(); 866 resolvePromisesForResume();
861 867
862 // Check to see if source nodes can be stopped because the end time has passed. 868 // Check to see if source nodes can be stopped because the end time has passed.
863 handleStoppableSourceNodes(); 869 handleStoppableSourceNodes();
864 870
865 unlock(); 871 unlock();
866 } 872 }
867 } 873 }
868 874
869 void AudioContext::handlePostRenderTasks() 875 void AudioContext::handlePostRenderTasks()
870 { 876 {
871 ASSERT(isAudioThread()); 877 ASSERT(isAudioThread());
872 878
879 // Same as |handlePreRenderTasks()|. We perform these for Offline Audio
880 // Context.
881 if (isOfflineContext()) {
882 deferredTaskHandler().forceLock();
883 deferredTaskHandler().breakConnections();
884 releaseFinishedSourceNodes();
885 deferredTaskHandler().handleDeferredTasks();
886 deferredTaskHandler().requestToDeleteHandlersOnMainThread();
887 unlock();
888 return;
889 }
890
873 // Must use a tryLock() here too. Don't worry, the lock will very rarely be contended and this method is called frequently. 891 // 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 892 // 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). 893 // from the render graph (in which case they'll render silence).
876 if (tryLock()) { 894 if (tryLock()) {
877 // Take care of AudioNode tasks where the tryLock() failed previously. 895 // Take care of AudioNode tasks where the tryLock() failed previously.
878 deferredTaskHandler().breakConnections(); 896 deferredTaskHandler().breakConnections();
879 897
880 // Dynamically clean up nodes which are no longer needed. 898 // Dynamically clean up nodes which are no longer needed.
881 releaseFinishedSourceNodes(); 899 releaseFinishedSourceNodes();
882 900
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 return EventTargetNames::AudioContext; 956 return EventTargetNames::AudioContext;
939 } 957 }
940 958
941 ExecutionContext* AudioContext::executionContext() const 959 ExecutionContext* AudioContext::executionContext() const
942 { 960 {
943 return m_isStopScheduled ? 0 : ActiveDOMObject::executionContext(); 961 return m_isStopScheduled ? 0 : ActiveDOMObject::executionContext();
944 } 962 }
945 963
946 void AudioContext::startRendering() 964 void AudioContext::startRendering()
947 { 965 {
948 // This is called for both online and offline contexts. 966 // This is only for the real-time context.
949 ASSERT(isMainThread()); 967 ASSERT(isMainThread());
950 ASSERT(m_destinationNode); 968 ASSERT(m_destinationNode);
969 ASSERT(!isOfflineContext());
951 970
952 if (m_contextState == Suspended) { 971 if (m_contextState == Suspended) {
953 destination()->audioDestinationHandler().startRendering(); 972 destination()->audioDestinationHandler().startRendering();
954 setContextState(Running); 973 setContextState(Running);
955 } 974 }
956 } 975 }
957 976
958 void AudioContext::stopRendering() 977 void AudioContext::stopRendering()
959 { 978 {
960 ASSERT(isMainThread()); 979 ASSERT(isMainThread());
961 ASSERT(m_destinationNode); 980 ASSERT(m_destinationNode);
962 ASSERT(!isOfflineContext()); 981 ASSERT(!isOfflineContext());
963 982
964 if (m_contextState == Running) { 983 if (m_contextState == Running) {
965 destination()->audioDestinationHandler().stopRendering(); 984 destination()->audioDestinationHandler().stopRendering();
966 setContextState(Suspended); 985 setContextState(Suspended);
967 deferredTaskHandler().clearHandlersToBeDeleted(); 986 deferredTaskHandler().clearHandlersToBeDeleted();
968 } 987 }
969 } 988 }
970 989
971 void AudioContext::fireCompletionEvent() 990 void AudioContext::fireCompletionEvent()
972 { 991 {
973 ASSERT(isMainThread()); 992 ASSERT_WITH_MESSAGE(1, "fireCompletionEvent() only valid for offline audio c ontext");
974 if (!isMainThread()) 993 }
975 return;
976 994
977 AudioBuffer* renderedBuffer = m_renderTarget.get(); 995 bool AudioContext::shouldSuspendNow()
996 {
997 ASSERT_WITH_MESSAGE(1, "shouldSuspendNow() only valid for offline audio cont ext");
998 return false;
999 }
978 1000
979 // For an offline context, we set the state to closed here so that the oncom plete handler sees 1001 void AudioContext::resolvePendingSuspendPromises()
980 // that the context has been closed. 1002 {
981 setContextState(Closed); 1003 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 } 1004 }
994 1005
995 DEFINE_TRACE(AudioContext) 1006 DEFINE_TRACE(AudioContext)
996 { 1007 {
997 visitor->trace(m_closeResolver); 1008 visitor->trace(m_closeResolver);
998 visitor->trace(m_offlineResolver);
999 visitor->trace(m_renderTarget); 1009 visitor->trace(m_renderTarget);
1000 visitor->trace(m_destinationNode); 1010 visitor->trace(m_destinationNode);
1001 visitor->trace(m_listener); 1011 visitor->trace(m_listener);
1002 // trace() can be called in AudioContext constructor, and 1012 // trace() can be called in AudioContext constructor, and
1003 // m_contextGraphMutex might be unavailable. 1013 // m_contextGraphMutex might be unavailable.
1004 if (m_didInitializeContextGraphMutex) { 1014 if (m_didInitializeContextGraphMutex) {
1005 AutoLocker lock(this); 1015 AutoLocker lock(this);
1006 visitor->trace(m_activeSourceNodes); 1016 visitor->trace(m_activeSourceNodes);
1007 } else { 1017 } else {
1008 visitor->trace(m_activeSourceNodes); 1018 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 1055 // the destination node can GCed if JS has no references. stop() will also r esolve the Promise
1046 // created here. 1056 // created here.
1047 stop(); 1057 stop();
1048 1058
1049 return promise; 1059 return promise;
1050 } 1060 }
1051 1061
1052 } // namespace blink 1062 } // namespace blink
1053 1063
1054 #endif // ENABLE(WEB_AUDIO) 1064 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698