• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GstCurlHttpSrc
3  * Copyright 2017 British Broadcasting Corporation - Research and Development
4  *
5  * Author: Sam Hurst <samuelh@rd.bbc.co.uk>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Alternatively, the contents of this file may be used under the
26  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27  * which case the following provisions apply instead of the ones
28  * mentioned above:
29  *
30  * This library is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Library General Public
32  * License as published by the Free Software Foundation; either
33  * version 2 of the License, or (at your option) any later version.
34  *
35  * This library is distributed in the hope that it will be useful,
36  * but WITHOUT ANY WARRANTY; without even the implied warranty of
37  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38  * Library General Public License for more details.
39  *
40  * You should have received a copy of the GNU Library General Public
41  * License along with this library; if not, write to the
42  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
43  * Boston, MA 02111-1307, USA.
44  */
45 
46 /*
47  * This header file contains definitions only for
48  */
49 
50 #ifndef GSTCURLHTTPSRC_H_
51 #define GSTCURLHTTPSRC_H_
52 
53 #include <gst/gst.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <gst/base/gstpushsrc.h>
59 
60 #include "curltask.h"
61 
62 G_BEGIN_DECLS
63 /* #defines don't like whitespacey bits */
64 #define GST_TYPE_CURLHTTPSRC \
65   (gst_curl_http_src_get_type())
66 #define GST_CURLHTTPSRC(obj) \
67   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CURLHTTPSRC,GstCurlHttpSrc))
68 #define GST_CURLHTTPSRC_CLASS(klass) \
69   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CURLHTTPSRC,GstCurlHttpSrcClass))
70 #define GST_IS_CURLHTTPSRC(obj) \
71   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CURLHTTPSRC))
72 #define GST_IS_CURLHTTPSRC_CLASS(klass) \
73   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_CURLHTTPSRC))
74 
75 #if GSTCURL_FUNCTIONTRACE
76 #define GSTCURL_FUNCTION_ENTRY(x) GST_DEBUG_OBJECT(x, "Entering function");
77 #define GSTCURL_FUNCTION_EXIT(x) GST_DEBUG_OBJECT(x, "Leaving function");
78 #else
79 #define GSTCURL_FUNCTION_ENTRY(x)
80 #define GSTCURL_FUNCTION_EXIT(x)
81 #endif
82 typedef struct _GstCurlHttpSrc GstCurlHttpSrc;
83 typedef struct _GstCurlHttpSrcClass GstCurlHttpSrcClass;
84 typedef struct _GstCurlHttpSrcMultiTaskContext GstCurlHttpSrcMultiTaskContext;
85 typedef struct _GstCurlHttpSrcQueueElement GstCurlHttpSrcQueueElement;
86 
87 #define HTTP_HEADERS_NAME       "http-headers"
88 #define HTTP_STATUS_CODE        "http-status-code"
89 #define URI_NAME                "uri"
90 #define REQUEST_HEADERS_NAME    "request-headers"
91 #define RESPONSE_HEADERS_NAME   "response-headers"
92 #define REDIRECT_URI_NAME       "redirection-uri"
93 
94 typedef enum
95   {
96     GSTCURL_HTTP_VERSION_1_0,
97     GSTCURL_HTTP_VERSION_1_1,
98 #ifdef CURL_VERSION_HTTP2
99     GSTCURL_HTTP_VERSION_2_0,
100 #endif
101     GSTCURL_HTTP_NOT,           /* For future use if HTTP protocol not used! */
102     GSTCURL_HTTP_VERSION_MAX
103   } GstCurlHttpVersion;
104 
105 typedef enum _GstCGstCurlHttpSrcSeekable
106 {
107     GSTCURL_SEEKABLE_UNKNOWN,
108     GSTCURL_SEEKABLE_TRUE,
109     GSTCURL_SEEKABLE_FALSE
110 } GstCGstCurlHttpSrcSeekable;
111 
112 #ifdef OHOS_EXT_FUNC
113 // ohos.ext.func.0033
114 typedef enum
115 {
116     GST_PLAYER_STATUS_IDLE,
117     GST_PLAYER_STATUS_BUFFERING,
118     GST_PLAYER_STATUS_READY,
119     GST_PLAYER_STATUS_PAUSED,
120     GST_PLAYER_STATUS_PLAYING,
121 } GstCurlHttpSrcPlayerStatus;
122 #endif
123 
124 struct _GstCurlHttpSrcMultiTaskContext
125 {
126   GstTask     *task;
127   GRecMutex   task_rec_mutex;
128   GMutex      mutex;
129 #ifdef OHOS_OPT_STABLE
130   /* ohos.opt.stable.0003 for multiple instances ref/unref mutex */
131   GMutex      multiple_mutex;
132 #endif
133   guint       refcount;
134   GCond       signal;
135 
136   GstCurlHttpSrcQueueElement  *queue;
137 
138   enum
139   {
140     GSTCURL_MULTI_LOOP_STATE_RUNNING,
141     GSTCURL_MULTI_LOOP_STATE_STOP
142   } state;
143 
144   /* < private > */
145   CURLM *multi_handle;
146 };
147 
148 struct _GstCurlHttpSrcClass
149 {
150   GstPushSrcClass parent_class;
151 
152   GstCurlHttpSrcMultiTaskContext multi_task_context;
153 };
154 
155 /*
156  * Our instance class.
157  */
158 struct _GstCurlHttpSrc
159 {
160   GstPushSrc element;
161 
162   /* < private > */
163   GMutex uri_mutex; /* Make the URIHandler get/set thread safe */
164   /*
165    * Things to tell libcURL about to build up the request message.
166    */
167   /* Type         Name                                      Curl Option */
168   gchar *uri;                   /* CURLOPT_URL */
169   gchar *redirect_uri;		/* CURLINFO_REDIRECT_URL */
170   gchar *username;              /* CURLOPT_USERNAME */
171   gchar *password;              /* CURLOPT_PASSWORD */
172   gchar *proxy_uri;             /* CURLOPT_PROXY */
173   gchar *no_proxy_list;         /* CURLOPT_NOPROXY */
174   gchar *proxy_user;            /* CURLOPT_PROXYUSERNAME */
175   gchar *proxy_pass;            /* CURLOPT_PROXYPASSWORD */
176 
177   /* Header options */
178   gchar **cookies;              /* CURLOPT_COOKIELIST */
179   gint number_cookies;
180   gchar *user_agent;            /* CURLOPT_USERAGENT */
181   GstStructure *request_headers;  /* CURLOPT_HTTPHEADER */
182   struct curl_slist *slist;
183   gboolean accept_compressed_encodings; /* CURLOPT_ACCEPT_ENCODING */
184 #ifdef OHOS_EXT_FUNC
185   /* ohos.ext.func.0025 support https seek: */
186   guint64 request_position;     /* Seek to this position. */
187   guint64 orig_request_pos;     /* Original request position. */
188   guint64 read_position;        /* Current position. */
189 #else
190   gint64 request_position;     /* Seek to this position. */
191 #endif
192   gint64 stop_position;        /* Stop at this position. */
193 
194 #ifdef OHOS_EXT_FUNC
195   // ohos.ext.func.0033
196   gint64      start_usecs;
197   gint64      end_usecs;
198   guint       reconnection_timeout;
199   gint        player_state;
200 #endif
201 
202   /* Connection options */
203   glong allow_3xx_redirect;     /* CURLOPT_FOLLOWLOCATION */
204   glong max_3xx_redirects;      /* CURLOPT_MAXREDIRS */
205   gboolean keep_alive;          /* CURLOPT_TCP_KEEPALIVE */
206   gint timeout_secs;            /* CURLOPT_TIMEOUT */
207   gboolean strict_ssl;		/* CURLOPT_SSL_VERIFYPEER */
208   gchar* custom_ca_file;	/* CURLOPT_CAINFO */
209 
210   gint total_retries;
211   gint retries_remaining;
212 
213   /*TODO As the following are all multi options, move these to curl task */
214   guint max_connection_time;    /* */
215   guint max_conns_per_server;   /* CURLMOPT_MAX_HOST_CONNECTIONS */
216   guint max_conns_per_proxy;    /* ?!? */
217   guint max_conns_global;       /* CURLMOPT_MAXCONNECTS */
218   /* END multi options */
219 
220   /* Some stuff for HTTP/2 */
221   GstCurlHttpVersion preferred_http_version;
222 
223   enum
224   {
225     GSTCURL_NONE,
226     GSTCURL_OK,
227     GSTCURL_DONE,
228     GSTCURL_UNLOCK,
229     GSTCURL_REMOVED,
230     GSTCURL_BAD_QUEUE_REQUEST,
231     GSTCURL_TOTAL_ERROR,
232     GSTCURL_PIPELINE_NULL,
233     GSTCURL_MAX
234   } state, pending_state;
235   CURL *curl_handle;
236   GMutex buffer_mutex;
237   GCond buffer_cond;
238   gchar *buffer;
239   guint buffer_len;
240   gboolean transfer_begun;
241   gboolean data_received;
242   enum {
243     GSTCURL_NOT_CONNECTED,
244     GSTCURL_CONNECTED,
245     GSTCURL_WANT_REMOVAL
246   } connection_status;
247 
248   /*
249    * Response Headers
250    */
251   GstStructure *http_headers;
252   gchar *content_type;
253   guint status_code;
254   gchar *reason_phrase;
255   gboolean hdrs_updated;
256   guint64 content_size;
257   GstCGstCurlHttpSrcSeekable seekable;
258 
259   CURLcode curl_result;
260   char curl_errbuf[CURL_ERROR_SIZE];
261 
262   GstCaps *caps;
263 };
264 
265 GType gst_curl_http_src_get_type (void);
266 
267 G_END_DECLS
268 #endif /* GSTCURLHTTPSRC_H_ */
269