84ea3e9833a2ad07653128029d1dfaf9e2c5491e
[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));
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         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), nullptr);
85     }
86 }
87
88 void WebCoreAVCFResourceLoader::stopLoading()
89 {
90     if (!m_resource)
91         return;
92
93     m_resource->removeClient(this);
94     m_resource = 0;
95
96     if (m_parent)
97         m_parent->didStopLoadingRequest(m_avRequest.get());
98 }
99
100 void WebCoreAVCFResourceLoader::invalidate()
101 {
102     m_parent = nullptr;
103     stopLoading();
104 }
105
106 void WebCoreAVCFResourceLoader::responseReceived(CachedResource* resource, const ResourceResponse& response)
107 {
108     ASSERT(resource == m_resource);
109     UNUSED_PARAM(resource);
110
111     int status = response.httpStatusCode();
112     if (status && (status < 200 || status > 299)) {
113         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), nullptr);
114         return;
115     }
116
117     notImplemented();
118 }
119
120 void WebCoreAVCFResourceLoader::dataReceived(CachedResource* resource, const char*, int)
121 {
122     fulfillRequestWithResource(resource);
123 }
124
125 void WebCoreAVCFResourceLoader::notifyFinished(CachedResource* resource)
126 {
127     if (resource->loadFailedOrCanceled()) {
128         // <rdar://problem/13987417> Set the contentType of the contentInformationRequest to an empty
129         // string to trigger AVAsset's playable value to complete loading.
130         // FIXME: if ([m_avRequest.get() contentInformationRequest] && ![[m_avRequest.get() contentInformationRequest] contentType])
131         // FIXME:    [[m_avRequest.get() contentInformationRequest] setContentType:@""];
132         notImplemented();
133
134         AVCFAssetResourceLoadingRequestFinishLoadingWithError(m_avRequest.get(), nullptr);
135     } else {
136         fulfillRequestWithResource(resource);
137         // FIXME: [m_avRequest.get() finishLoading];
138         notImplemented();
139     }
140     stopLoading();
141 }
142
143 void WebCoreAVCFResourceLoader::fulfillRequestWithResource(CachedResource* resource)
144 {
145     ASSERT(resource == m_resource);
146     notImplemented();
147 }
148
149 }
150
151 #endif // ENABLE(VIDEO) && USE(AVFOUNDATION)