File: CloudProjectMixdownUtils.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (135 lines) | stat: -rw-r--r-- 4,101 bytes parent folder | download
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
/*  SPDX-License-Identifier: GPL-2.0-or-later */
/*!********************************************************************

  Audacity: A Digital Audio Editor

  CloudProjectMixdownUtils.cpp

  Dmitry Vedenko

**********************************************************************/
#include "CloudProjectMixdownUtils.h"

#include "BasicUI.h"
#include "CloudProjectFileIOExtensions.h"
#include "CloudProjectOpenUtils.h"
#include "Project.h"
#include "ProjectWindow.h"
#include "ServiceConfig.h"
#include "UriParser.h"

#include "sync/CloudSyncDTO.h"
#include "sync/MixdownUploader.h"
#include "sync/ProjectCloudExtension.h"

namespace audacity::cloud::audiocom::sync
{

bool HandleMixdownLink(std::string_view uri)
{
   ASSERT_MAIN_THREAD();

   const auto parsedUri = ParseUri(uri);

   if (parsedUri.Scheme != "audacity" || parsedUri.Host != "generate-audio")
      return false;

   const auto queryParameters = ParseUriQuery(parsedUri.Query);

   if (queryParameters.empty())
      return false;

   const auto projectId = queryParameters.find("projectId");

   if (projectId == queryParameters.end())
      return false;

   auto openedProject = GetOpenedProject(projectId->second);

   const auto hasOpenProject = openedProject != nullptr;

   const auto project = hasOpenProject ?
                           openedProject :
                           OpenProjectFromCloud(
                              GetPotentialTarget(), projectId->second,
                              std::string_view {}, false);

   if (project == nullptr)
      return false;

   UploadMixdown(
      *project,
      [hasOpenProject](AudacityProject& project, MixdownState state)
      {
         if (!hasOpenProject)
            ProjectWindow::Get(project).Close(true);
      });

   return true;
}

void UploadMixdown(
   AudacityProject& project,
   std::function<void(AudacityProject&, MixdownState)> onComplete)
{
   SaveToCloud(
      project, UploadMode::Normal,
      [&project, onComplete = std::move(onComplete)](const auto& response)
      {
         auto cancellationContext = concurrency::CancellationContext::Create();

         auto progressDialog = BasicUI::MakeProgress(
            XO("Save to audio.com"), XO("Generating audio preview..."),
            BasicUI::ProgressShowCancel);

         auto mixdownUploader = MixdownUploader::Upload(
            cancellationContext, GetServiceConfig(), project,
            [progressDialog = progressDialog.get(),
             cancellationContext](auto progress)
            {
               if (
                  progressDialog->Poll(
                     static_cast<unsigned>(progress * 10000), 10000) !=
                  BasicUI::ProgressResult::Success)
                  cancellationContext->Cancel();
            });

         mixdownUploader->SetUrls(response.SyncState.MixdownUrls);

         BasicUI::CallAfter(
            [&project,
             progressDialog = std::shared_ptr { std::move(progressDialog) },
             mixdownUploader, cancellationContext, onComplete]() mutable
            {
               auto& projectCloudExtension =
                  ProjectCloudExtension::Get(project);

               auto subscription = projectCloudExtension.SubscribeStatusChanged(
                  [progressDialog = progressDialog.get(), mixdownUploader,
                   cancellationContext](
                     const CloudStatusChangedMessage& message)
                  {
                     if (message.Status != ProjectSyncStatus::Failed)
                        return;

                     cancellationContext->Cancel();
                  },
                  true);

               auto future = mixdownUploader->GetResultFuture();

               while (future.wait_for(std::chrono::milliseconds(50)) !=
                      std::future_status::ready)
                  BasicUI::Yield();

               auto result = future.get();

               progressDialog.reset();

               if (onComplete)
                  onComplete(project, result.State);
            });
      });
}

} // namespace audacity::cloud::audiocom::sync