| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2021 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include "video.h" |
| 14 | #include "videosite.h" |
| 15 | #include "converter.h" |
| 16 | #include "config.h" |
| 17 | |
| 18 | using namespace smooth::Net; |
| 19 | using namespace smooth::Threads; |
| 20 | using namespace smooth::IO; |
| 21 | |
| 22 | Int BoCA::Video::activeDownloads = 0; |
| 23 | |
| 24 | BoCA::Video::Video(VideoSite *iVideoSite) |
| 25 | { |
| 26 | pageDownloaded = False; |
| 27 | metadataQueried = False; |
| 28 | |
| 29 | videoDownloadStarted = False; |
| 30 | videoDownloadFinished = False; |
| 31 | |
| 32 | doCancelDownload = False; |
| 33 | |
| 34 | videoSite = iVideoSite; |
| 35 | } |
| 36 | |
| 37 | BoCA::Video::~Video() |
| 38 | { |
| 39 | CancelDownload(); |
| 40 | } |
| 41 | |
| 42 | Int BoCA::Video::DownloaderThread(String targetFileName) |
| 43 | { |
| 44 | Config *config = Config::Get(); |
| 45 | |
| 46 | /* Wait until number of active downloads drops below maximum. |
| 47 | */ |
| 48 | Int &maxActiveDownloads = config->GetPersistentIntValue(ConfigureYouTube::ConfigID, "MaxDownloads", 8); |
| 49 | |
| 50 | while (activeDownloads >= maxActiveDownloads) S::System::System::Sleep(100); |
| 51 | |
| 52 | activeDownloads++; |
| 53 | |
| 54 | /* Start download. |
| 55 | */ |
| 56 | startDownload.Emit(this); |
| 57 | |
| 58 | videoDownloadStarted = True; |
| 59 | |
| 60 | DownloadPage(); |
| 61 | |
| 62 | /* Create target directory if it does not exist yet. |
| 63 | */ |
| 64 | Utilities::CreateDirectoryForFile(targetFileName); |
| 65 | |
| 66 | /* Get video URL and start download. |
| 67 | */ |
| 68 | Bool error = False; |
| 69 | String cacheURL = videoSite->GetVideoURL(videoPageHTML); |
| 70 | |
| 71 | if (!(cacheURL.StartsWith("http://") || cacheURL.StartsWith("https://")) || cacheURL.Length() < 11) error = True; |
| 72 | |
| 73 | if (!error) |
| 74 | { |
| 75 | Protocols::Protocol *protocol = Protocols::Protocol::CreateForURL(cacheURL); |
| 76 | |
| 77 | protocol->downloadProgress.Connect(&downloadProgress); |
| 78 | protocol->downloadSpeed.Connect(&downloadSpeed); |
| 79 | protocol->doCancelDownload.DisconnectAll(); |
| 80 | protocol->doCancelDownload.Connect(&Video::DoCancelDownload, this); |
| 81 | protocol->DownloadToFile(targetFileName); |
| 82 | |
| 83 | delete protocol; |
| 84 | } |
| 85 | |
| 86 | /* Check whether download was canceled. |
| 87 | */ |
| 88 | if (doCancelDownload) |
| 89 | { |
| 90 | /* Remove partially downloaded file. |
| 91 | */ |
| 92 | File(targetFileName).Delete(); |
| 93 | } |
| 94 | else if (config->GetIntValue(ConfigureYouTube::ConfigID, "OutputFormat", -1) >= 0) |
| 95 | { |
| 96 | /* Convert video file if requested. |
| 97 | */ |
| 98 | Array<Converter *> &converters = Converter::Get(); |
| 99 | Converter *converter = converters.GetNth(config->GetIntValue(ConfigureYouTube::ConfigID, "OutputFormat", -1)); |
| 100 | |
| 101 | String convertedFileName = targetFileName.Head(targetFileName.FindLast(".") + 1).Append(converter->GetFormat().GetExtensions().GetFirst()); |
| 102 | |
| 103 | if (targetFileName != convertedFileName) |
| 104 | { |
| 105 | if (converter->Run(targetFileName, convertedFileName) == Success()) |
| 106 | { |
| 107 | SetVideoFile(convertedFileName); |
| 108 | |
| 109 | File(targetFileName).Delete(); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | File(convertedFileName).Delete(); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /* Finish and clean up. |
| 119 | */ |
| 120 | finishDownload.Emit(this); |
| 121 | |
| 122 | videoDownloadFinished = True; |
| 123 | |
| 124 | activeDownloads--; |
| 125 | |
| 126 | if (error) return Error(); |
| 127 | else return Success(); |
| 128 | } |
| 129 | |
| 130 | Bool BoCA::Video::DownloadPage() |
| 131 | { |
| 132 | downloadMutex.Lock(); |
| 133 | |
| 134 | if (pageDownloaded) { downloadMutex.Release(); return True; } |
| 135 | |
| 136 | Buffer<UnsignedByte> buffer; |
| 137 | Protocols::Protocol *protocol = Protocols::Protocol::CreateForURL(videoURL); |
| 138 | |
| 139 | protocol->DownloadToBuffer(buffer); |
| 140 | |
| 141 | delete protocol; |
| 142 | |
| 143 | videoPageHTML.ImportFrom("ISO-8859-1", (char *) (UnsignedByte *) buffer); |
| 144 | |
| 145 | pageDownloaded = True; |
| 146 | |
| 147 | downloadMutex.Release(); |
| 148 | |
| 149 | return True; |
| 150 | } |
| 151 | |
| 152 | const String &BoCA::Video::GetDecoderID() |
| 153 | { |
| 154 | if (videoDecoder != NIL) return videoDecoder; |
| 155 | |
| 156 | DownloadPage(); |
| 157 | |
| 158 | videoDecoder = videoSite->GetDecoderID(videoPageHTML); |
| 159 | |
| 160 | return videoDecoder; |
| 161 | } |
| 162 | |
| 163 | Bool BoCA::Video::QueryMetadata() |
| 164 | { |
| 165 | if (metadataQueried) return True; |
| 166 | |
| 167 | DownloadPage(); |
| 168 | |
| 169 | Metadata metadata = videoSite->QueryMetadata(videoPageHTML); |
| 170 | |
| 171 | videoTitle = metadata.title; |
| 172 | videoDescription = metadata.description; |
| 173 | videoDate = metadata.date; |
| 174 | videoUploader = metadata.uploader; |
| 175 | videoThumbnailURL = metadata.thumbnail; |
| 176 | |
| 177 | /* Download video thumbnail. |
| 178 | */ |
| 179 | if (videoThumbnailURL.StartsWith("http://") || videoThumbnailURL.StartsWith("https://")) |
| 180 | { |
| 181 | Buffer<UnsignedByte> buffer; |
| 182 | Protocols::Protocol *protocol = Protocols::Protocol::CreateForURL(videoThumbnailURL); |
| 183 | |
| 184 | protocol->DownloadToBuffer(buffer); |
| 185 | |
| 186 | delete protocol; |
| 187 | |
| 188 | videoThumbnail.mime = "image/jpeg"; |
| 189 | videoThumbnail.type = 16; |
| 190 | videoThumbnail.data = buffer; |
| 191 | } |
| 192 | |
| 193 | metadataQueried = True; |
| 194 | |
| 195 | return True; |
| 196 | } |
| 197 | |
| 198 | Bool BoCA::Video::Download() |
| 199 | { |
| 200 | if (videoFile == NIL) return False; |
| 201 | |
| 202 | Thread *thread = NonBlocking1<String>(&Video::DownloaderThread, this).Call(videoFile); |
| 203 | |
| 204 | thread->SetFlags(THREAD_KILLFLAG_WAIT); |
| 205 | |
| 206 | return True; |
| 207 | } |
| 208 | |
| 209 | Bool BoCA::Video::CancelDownload() |
| 210 | { |
| 211 | if (videoDownloadStarted && !videoDownloadFinished) |
| 212 | { |
| 213 | /* Cancel download. |
| 214 | */ |
| 215 | doCancelDownload = True; |
| 216 | } |
| 217 | |
| 218 | return True; |
| 219 | } |