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

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: Restructured the resolution of suspend promise 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 return EventTargetNames::AudioContext; 932 return EventTargetNames::AudioContext;
939 } 933 }
940 934
941 ExecutionContext* AudioContext::executionContext() const 935 ExecutionContext* AudioContext::executionContext() const
942 { 936 {
943 return m_isStopScheduled ? 0 : ActiveDOMObject::executionContext(); 937 return m_isStopScheduled ? 0 : ActiveDOMObject::executionContext();
944 } 938 }
945 939
946 void AudioContext::startRendering() 940 void AudioContext::startRendering()
947 { 941 {
948 // This is called for both online and offline contexts. 942 // This is only for the real-time context.
949 ASSERT(isMainThread()); 943 ASSERT(isMainThread());
950 ASSERT(m_destinationNode); 944 ASSERT(m_destinationNode);
945 ASSERT(!isOfflineContext());
951 946
952 if (m_contextState == Suspended) { 947 if (m_contextState == Suspended) {
953 destination()->audioDestinationHandler().startRendering(); 948 destination()->audioDestinationHandler().startRendering();
954 setContextState(Running); 949 setContextState(Running);
955 } 950 }
956 } 951 }
957 952
958 void AudioContext::stopRendering() 953 void AudioContext::stopRendering()
959 { 954 {
960 ASSERT(isMainThread()); 955 ASSERT(isMainThread());
961 ASSERT(m_destinationNode); 956 ASSERT(m_destinationNode);
962 ASSERT(!isOfflineContext()); 957 ASSERT(!isOfflineContext());
963 958
964 if (m_contextState == Running) { 959 if (m_contextState == Running) {
965 destination()->audioDestinationHandler().stopRendering(); 960 destination()->audioDestinationHandler().stopRendering();
966 setContextState(Suspended); 961 setContextState(Suspended);
967 deferredTaskHandler().clearHandlersToBeDeleted(); 962 deferredTaskHandler().clearHandlersToBeDeleted();
968 } 963 }
969 } 964 }
970 965
971 void AudioContext::fireCompletionEvent() 966 void AudioContext::fireCompletionEvent()
972 { 967 {
973 ASSERT(isMainThread()); 968 ASSERT_WITH_MESSAGE(1, "fireCompletionEvent() only valid for offline audio c ontext");
974 if (!isMainThread()) 969 }
975 return;
976 970
977 AudioBuffer* renderedBuffer = m_renderTarget.get(); 971 bool AudioContext::shouldSuspendNow()
972 {
973 ASSERT_WITH_MESSAGE(1, "shouldSuspendNow() only valid for offline audio cont ext");
974 return false;
975 }
978 976
979 // For an offline context, we set the state to closed here so that the oncom plete handler sees 977 void AudioContext::resolvePendingSuspendPromises()
980 // that the context has been closed. 978 {
981 setContextState(Closed); 979 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 } 980 }
994 981
995 DEFINE_TRACE(AudioContext) 982 DEFINE_TRACE(AudioContext)
996 { 983 {
997 visitor->trace(m_closeResolver); 984 visitor->trace(m_closeResolver);
998 visitor->trace(m_offlineResolver);
999 visitor->trace(m_renderTarget); 985 visitor->trace(m_renderTarget);
1000 visitor->trace(m_destinationNode); 986 visitor->trace(m_destinationNode);
1001 visitor->trace(m_listener); 987 visitor->trace(m_listener);
1002 // trace() can be called in AudioContext constructor, and 988 // trace() can be called in AudioContext constructor, and
1003 // m_contextGraphMutex might be unavailable. 989 // m_contextGraphMutex might be unavailable.
1004 if (m_didInitializeContextGraphMutex) { 990 if (m_didInitializeContextGraphMutex) {
1005 AutoLocker lock(this); 991 AutoLocker lock(this);
1006 visitor->trace(m_activeSourceNodes); 992 visitor->trace(m_activeSourceNodes);
1007 } else { 993 } else {
1008 visitor->trace(m_activeSourceNodes); 994 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 1031 // the destination node can GCed if JS has no references. stop() will also r esolve the Promise
1046 // created here. 1032 // created here.
1047 stop(); 1033 stop();
1048 1034
1049 return promise; 1035 return promise;
1050 } 1036 }
1051 1037
1052 } // namespace blink 1038 } // namespace blink
1053 1039
1054 #endif // ENABLE(WEB_AUDIO) 1040 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698