icecast_client.h
Go to the documentation of this file.
1 /**
2  * @file icecast_client.h
3  * @brief Icecast client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 #ifndef _ICECAST_CLIENT_H
32 #define _ICECAST_CLIENT_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/socket.h"
37 
38 //Icecast client support
39 #ifndef ICECAST_CLIENT_SUPPORT
40  #define ICECAST_CLIENT_SUPPORT DISABLED
41 #elif (ICECAST_CLIENT_SUPPORT != ENABLED && ICECAST_CLIENT_SUPPORT != DISABLED)
42  #error ICECAST_CLIENT_SUPPORT parameter is not valid
43 #endif
44 
45 //Stack size required to run the Icecast client
46 #ifndef ICECAST_CLIENT_STACK_SIZE
47  #define ICECAST_CLIENT_STACK_SIZE 650
48 #elif (ICECAST_CLIENT_STACK_SIZE < 1)
49  #error ICECAST_CLIENT_STACK_SIZE parameter is not valid
50 #endif
51 
52 //Priority at which the Icecast client should run
53 #ifndef ICECAST_CLIENT_PRIORITY
54  #define ICECAST_CLIENT_PRIORITY OS_TASK_PRIORITY_NORMAL
55 #endif
56 
57 //Maximum time the Icecast client will wait before closing the connection
58 #ifndef ICECAST_CLIENT_TIMEOUT
59  #define ICECAST_CLIENT_TIMEOUT 10000
60 #elif (ICECAST_CLIENT_TIMEOUT < 1000)
61  #error ICECAST_CLIENT_TIMEOUT parameter is not valid
62 #endif
63 
64 //Recovery delay
65 #ifndef ICECAST_RECOVERY_DELAY
66  #define ICECAST_RECOVERY_DELAY 5000
67 #elif (ICECAST_RECOVERY_DELAY < 1000)
68  #error ICECAST_RECOVERY_DELAY parameter is not valid
69 #endif
70 
71 //Maximum length of the server name
72 #ifndef ICECAST_SERVER_NAME_MAX_LEN
73  #define ICECAST_SERVER_NAME_MAX_LEN 48
74 #elif (ICECAST_SERVER_NAME_MAX_LEN < 1)
75  #error ICECAST_SERVER_NAME_MAX_LEN parameter is not valid
76 #endif
77 
78 //Maxmimum length of the requested resource
79 #ifndef ICECAST_RESOURCE_MAX_LEN
80  #define ICECAST_RESOURCE_MAX_LEN 32
81 #elif (ICECAST_RESOURCE_MAX_LEN < 1)
82  #error ICECAST_RESOURCE_MAX_LEN parameter is not valid
83 #endif
84 
85 //Maximum size of metadata blocks
86 #ifndef ICECAST_CLIENT_METADATA_MAX_SIZE
87  #define ICECAST_CLIENT_METADATA_MAX_SIZE 512
88 #elif (ICECAST_CLIENT_METADATA_MAX_SIZE < 128)
89  #error ICECAST_CLIENT_METADATA_MAX_SIZE parameter is not valid
90 #endif
91 
92 //C++ guard
93 #ifdef __cplusplus
94 extern "C" {
95 #endif
96 
97 
98 /**
99  * @brief Icecast client settings
100  **/
101 
102 typedef struct
103 {
104  OsTaskParameters task; ///<Task parameters
105  NetInterface *interface; ///<Underlying network interface
106  char_t serverName[ICECAST_SERVER_NAME_MAX_LEN]; ///<Icecast server name
107  uint16_t serverPort; ///<Icecast server port
108  char_t resource[ICECAST_RESOURCE_MAX_LEN]; ///<Requested resource
109  size_t bufferSize; ///<Streaming buffer size
111 
112 
113 /**
114  * @brief Icecast client context
115  **/
116 
117 typedef struct
118 {
119  IcecastClientSettings settings; ///<User settings
120  OsMutex mutex; ///<Mutex protecting critical sections
121  OsEvent writeEvent; ///<This event tells whether the buffer is writable
122  OsEvent readEvent; ///<This event tells whether the buffer is readable
123  OsTaskParameters taskParams; ///<Task parameters
124  OsTaskId taskId; ///<Task identifier
125  Socket *socket; ///<Underlying socket
126  size_t blockSize; ///<Number of data bytes between subsequent metadata blocks
127  uint8_t *streamBuffer; ///<Streaming buffer
128  size_t bufferSize; ///<Streaming buffer size
129  size_t bufferLength; ///<Streaming buffer length
130  size_t writeIndex; ///<Current write index within the buffer
131  size_t readIndex; ///<Current read index within the buffer
132  size_t totalLength; ///<Total number of bytes that have been received
133  char_t buffer[ICECAST_CLIENT_METADATA_MAX_SIZE]; ///<Memory buffer for input/output operations
134  char_t metadata[ICECAST_CLIENT_METADATA_MAX_SIZE]; ///<Metadata information
135  size_t metadataLength; ///<Length of the metadata
137 
138 
139 //Icecast related functions
141 
143  const IcecastClientSettings *settings);
144 
146 
148  uint8_t *data, size_t size, size_t *length, systime_t timeout);
149 
151  char_t *metadata, size_t size, size_t *length);
152 
153 void icecastClientTask(void *param);
154 
157 
158 //C++ guard
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif
char char_t
Definition: compiler_port.h:48
error_t
Error codes.
Definition: error.h:43
uint8_t data[]
Definition: ethernet.h:222
error_t icecastClientProcessMetadata(IcecastClientContext *context)
Decode metadata block.
error_t icecastClientReadStream(IcecastClientContext *context, uint8_t *data, size_t size, size_t *length, systime_t timeout)
Copy data from input stream.
error_t icecastClientReadMetadata(IcecastClientContext *context, char_t *metadata, size_t size, size_t *length)
Copy metadata from input stream.
void icecastClientGetDefaultSettings(IcecastClientSettings *settings)
Initialize settings with default values.
#define ICECAST_SERVER_NAME_MAX_LEN
void icecastClientTask(void *param)
Icecast client task.
error_t icecastClientInit(IcecastClientContext *context, const IcecastClientSettings *settings)
Icecast client initialization.
error_t icecastClientStart(IcecastClientContext *context)
Start Icecast client.
#define ICECAST_RESOURCE_MAX_LEN
#define ICECAST_CLIENT_METADATA_MAX_SIZE
error_t icecastClientConnect(IcecastClientContext *context)
Connect to the specified Icecast server.
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
thread_t * OsTaskId
Task identifier.
Socket API.
#define Socket
Definition: socket.h:36
Icecast client context.
uint8_t * streamBuffer
Streaming buffer.
size_t readIndex
Current read index within the buffer.
size_t totalLength
Total number of bytes that have been received.
OsEvent writeEvent
This event tells whether the buffer is writable.
OsTaskId taskId
Task identifier.
size_t metadataLength
Length of the metadata.
size_t bufferSize
Streaming buffer size.
OsTaskParameters taskParams
Task parameters.
size_t blockSize
Number of data bytes between subsequent metadata blocks.
IcecastClientSettings settings
User settings.
OsEvent readEvent
This event tells whether the buffer is readable.
size_t bufferLength
Streaming buffer length.
size_t writeIndex
Current write index within the buffer.
Socket * socket
Underlying socket.
OsMutex mutex
Mutex protecting critical sections.
Icecast client settings.
OsTaskParameters task
Task parameters.
size_t bufferSize
Streaming buffer size.
uint16_t serverPort
Icecast server port.
NetInterface * interface
Underlying network interface.
Event object.
Mutex object.
Task parameters.
uint8_t length
Definition: tcp.h:368