[Win] Extend AVFoundationCF Media Implementation
[WebKit-https.git] / Source / WebCore / platform / graphics / avfoundation / cf / WebCoreAVCFResourceLoader.cpp
1 /*
2  * Copyright (C) 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebCoreAVCFResourceLoader.h"
28
29 #if ENABLE(VIDEO) && USE(AVFOUNDATION) && HAVE(AVFOUNDATION_LOADER_DELEGATE)
30
31 #include "CachedRawResource.h"
32 #include "CachedResourceLoader.h"
33 #include "CachedResourceRequest.h"
34 #include "MediaPlayerPrivateAVFoundationCF.h"
35 #include "NotImplemented.h"
36 #include "ResourceBuffer.h"
37 #include "ResourceLoaderOptions.h"
38 #include "SharedBuffer.h"
39 #include "SoftLinking.h"
40 #include <AVFoundationCF/AVFoundationCF.h>
41 #include <AVFoundationCF/AVCFAssetResourceLoader.h>
42 #include <wtf/text/CString.h>
43
44 // The softlink header files must be included after the AVCF and CoreMedia header files.
45 #include "AVFoundationCFSoftLinking.h"
46
47 namespace WebCore {
48
49 PassRefPtr<WebCoreAVCFResourceLoader> WebCoreAVCFResourceLoader::create(MediaPlayerPrivateAVFoundationCF* parent, AVCFAssetResourceLoadingRequestRef avRequest)
50 {
51     ASSERT(avRequest);
52     ASSERT(parent);
53     return adoptRef(new WebCoreAVCFResourceLoader(parent, avRequest));
54 }
55
56 WebCoreAVCFResourceLoader::WebCoreAVCFResourceLoader(MediaPlayerPrivateAVFoundationCF* parent, AVCFAssetResourceLoadingRequestRef avRequest)
57     : m_parent(parent)
58     , m_avRequest(avRequest)
59 {
60 }
61
62 WebCoreAVCFResourceLoader::~WebCoreAVCFResourceLoader()
63 {
64     stopLoading();
65 }
66
67 void WebCoreAVCFResourceLoader::startLoading()
68 {
69     if (m_resource || !m_parent)
70         return;
71
72     RetainPtr<CFURLRequestRef> urlRequest = AVCFAssetResourceLoadingRequestGetURLRequest(m_avRequest.get());
73     URL requestURL = CFURLRequestGetURL(urlRequest.get());
74
75     CachedResourceRequest request(ResourceRequest(requestURL), ResourceLoaderOptions(SendCallbacks, DoNotSniffContent, BufferData, DoNotAllowStoredCredentials, DoNotAskClientForCrossOriginCredentials, DoSecurityCheck, UseDefaultOriginRestrictionsForType, DoNotIncludeCertificateInfo));
76
77     request.mutableResourceRequest().setPriority(ResourceLoadPriorityLow);
78     CachedResourceLoader* loader = m_parent->player()->cachedResourceLoader();
79     m_resource = loader ? loader->requestRawResource(request) : 0;
80     if (m_resource)
81         m_resource->addClient(this);
82     else {
83         LOG_ERROR("Failed to start load for media at url %s", requestURL.string().ascii().data());
84         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
85         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
86     }
87 }
88
89 void WebCoreAVCFResourceLoader::stopLoading()
90 {
91     if (!m_resource)
92         return;
93
94     m_resource->removeClient(this);
95     m_resource = 0;
96
97     if (m_parent)
98         m_parent->didStopLoadingRequest(m_avRequest.get());
99 }
100
101 void WebCoreAVCFResourceLoader::invalidate()
102 {
103     m_parent = nullptr;
104     stopLoading();
105 }
106
107 void WebCoreAVCFResourceLoader::responseReceived(CachedResource* resource, const ResourceResponse& response)
108 {
109     ASSERT(resource == m_resource);
110     UNUSED_PARAM(resource);
111
112     int status = response.httpStatusCode();
113     if (status && (status < 200 || status > 299)) {
114         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, status, nullptr));
115         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
116         return;
117     }
118
119     notImplemented();
120 }
121
122 void WebCoreAVCFResourceLoader::dataReceived(CachedResource* resource, const char*, int)
123 {
124     fulfillRequestWithResource(resource);
125 }
126
127 void WebCoreAVCFResourceLoader::notifyFinished(CachedResource* resource)
128 {
129     if (resource->loadFailedOrCanceled()) {
130         // <rdar://problem/13987417> Set the contentType of the contentInformationRequest to an empty
131         // string to trigger AVAsset's playable value to complete loading.
132         // FIXME: if ([m_avRequest.get() contentInformationRequest] && ![[m_avRequest.get() contentInformationRequest] contentType])
133         // FIXME:    [[m_avRequest.get() contentInformationRequest] setContentType:@""];
134         notImplemented();
135
136         RetainPtr<CFErrorRef> error = adoptCF(CFErrorCreate(kCFAllocatorDefault, kCFErrorDomainCFNetwork, kCFURLErrorUnknown, nullptr));
137         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), error.get());
138     } else {
139         fulfillRequestWithResource(resource);
140         // FIXME: [m_avRequest.get() finishLoading];
141         notImplemented();
142     }
143     stopLoading();
144 }
145
146 void WebCoreAVCFResourceLoader::fulfillRequestWithResource(CachedResource* resource)
147 {
148     ASSERT(resource == m_resource);
149     notImplemented();
150 }
151
152 }
153
154 #endif // ENABLE(VIDEO) && USE(AVFOUNDATION)