static int media_get_socket(void* handle)
{
MediaIOPriv* priv = handle;
if (!priv || priv->socket <= 0)
return -EINVAL;
return priv->socket;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int media_process_command(const char* target, const char* cmd,
const char* arg, char* res, int res_len)
{
return media_proxy(MEDIA_ID_GRAPH, NULL, target, cmd, arg, 0, res, res_len);
}
void media_graph_dump(const char* options)
{
media_proxy(MEDIA_ID_GRAPH, NULL, NULL, "dump", options, 0, NULL, 0);
}
void* media_player_open(const char* params)
{
return media_open(MEDIA_ID_PLAYER, params);
}
int media_player_close(void* handle, int pending_stop)
{
return media_close(handle, pending_stop);
}
int media_player_set_event_callback(void* handle, void* cookie,
media_event_callback event_cb)
{
return media_set_event_cb(handle, cookie, event_cb);
}
int media_player_prepare(void* handle, const char* url, const char* options)
{
return media_prepare(handle, url, options);
}
int media_player_reset(void* handle)
{
if (!handle)
return -EINVAL;
media_close_socket(handle);
return media_proxy_once(handle, NULL, "reset", NULL, 0, NULL, 0);
}
ssize_t media_player_write_data(void* handle, const void* data, size_t len)
{
return media_process_data(handle, true, (void*)data, len);
}
int media_player_get_sockaddr(void* handle, struct sockaddr_storage* addr)
{
return media_get_sockaddr(handle, addr);
}
void media_player_close_socket(void* handle)
{
media_close_socket(handle);
}
int media_player_get_socket(void* handle)
{
return media_get_socket(handle);
}
int media_player_start(void* handle)
{
if (!handle)
return -EINVAL;
return media_proxy_once(handle, NULL, "start", NULL, 0, NULL, 0);
}
int media_player_stop(void* handle)
{
if (!handle)
return -EINVAL;
media_close_socket(handle);
return media_proxy_once(handle, NULL, "stop", NULL, 0, NULL, 0);
}
int media_player_pause(void* handle)
{
return media_proxy_once(handle, NULL, "pause", NULL, 0, NULL, 0);
}
int media_player_seek(void* handle, unsigned int msec)
{
char tmp[32];
snprintf(tmp, sizeof(tmp), "%u", msec);
return media_proxy_once(handle, NULL, "seek", tmp, 0, NULL, 0);
}
int media_player_set_looping(void* handle, int loop)
{
char tmp[32];
snprintf(tmp, sizeof(tmp), "%d", loop);
return media_proxy_once(handle, NULL, "set_loop", tmp, 0, NULL, 0);
}
int media_player_is_playing(void* handle)
{
char tmp[32];
int ret;
ret = media_proxy_once(handle, NULL, "get_playing", NULL, 0, tmp, sizeof(tmp));
return ret < 0 ? ret : !!atoi(tmp);
}
int media_player_get_position(void* handle, unsigned int* msec)
{
char tmp[32];
int ret;
if (!msec)
return -EINVAL;
ret = media_proxy_once(handle, NULL, "get_position", NULL, 0, tmp, sizeof(tmp));
if (ret >= 0)
*msec = strtoul(tmp, NULL, 0);
return ret;
}
int media_player_get_duration(void* handle, unsigned int* msec)
{
char tmp[32];
int ret;
if (!msec)
return -EINVAL;
ret = media_proxy_once(handle, NULL, "get_duration", NULL, 0, tmp, sizeof(tmp));
if (ret >= 0)
*msec = strtoul(tmp, NULL, 0);
return ret;
}
int media_player_get_latency(void* handle, unsigned int* latency)
{
char tmp[32];
int ret;
if (!latency)
return -EINVAL;
ret = media_proxy_once(handle, NULL, "get_latency", NULL, 0, tmp, sizeof(tmp));
if (ret >= 0)
*latency = strtoul(tmp, NULL, 0);
return ret;
}
int media_player_set_volume(void* handle, float volume)
{
char tmp[32];
snprintf(tmp, sizeof(tmp), "%f", volume);
return media_proxy_once(handle, NULL, "volume", tmp, 0, NULL, 0);
}
int media_player_get_volume(void* handle, float* volume)
{
char tmp[32];
int ret;
if (!volume)
return -EINVAL;
ret = media_proxy_once(handle, NULL, "get_volume", NULL, 0, tmp, sizeof(tmp));
if (ret >= 0) {
sscanf(tmp, "vol:%f", volume);
ret = 0;
}
return ret;
}
int media_player_set_property(void* handle, const char* target, const char* key, const char* value)
{
return media_proxy_once(handle, target, key, value, 0, NULL, 0);
}
int media_player_get_property(void* handle, const char* target, const char* key, char* value, int value_len)
{
return media_proxy_once(handle, target, key, NULL, 0, value, value_len);
}
void* media_recorder_open(const char* params)
{
return media_open(MEDIA_ID_RECORDER, params);
}
int media_recorder_close(void* handle)
{
return media_close(handle, 0);
}
int media_recorder_set_event_callback(void* handle, void* cookie,
media_event_callback event_cb)
{
return media_set_event_cb(handle, cookie, event_cb);
}
int media_recorder_prepare(void* handle, const char* url, const char* options)
{
return media_prepare(handle, url, options);
}
int media_recorder_reset(void* handle)
{
if (!handle)
return -EINVAL;
media_close_socket(handle);
return media_proxy_once(handle, NULL, "reset", NULL, 0, NULL, 0);
}
ssize_t media_recorder_read_data(void* handle, void* data, size_t len)
{
return media_process_data(handle, false, data, len);
}
int media_recorder_get_sockaddr(void* handle, struct sockaddr_storage* addr)
{
return media_get_sockaddr(handle, addr);
}
int media_recorder_get_socket(void* handle)
{
return media_get_socket(handle);
}
void media_recorder_close_socket(void* handle)
{
media_close_socket(handle);
}
int media_recorder_start(void* handle)
{
return media_proxy_once(handle, NULL, "start", NULL, 0, NULL, 0);
}
int media_recorder_pause(void* handle)
{
return media_proxy_once(handle, NULL, "pause", NULL, 0, NULL, 0);
}
int media_recorder_stop(void* handle)
{
if (!handle)
return -EINVAL;
media_close_socket(handle);
return media_proxy_once(handle, NULL, "stop", NULL, 0, NULL, 0);
}
int media_recorder_set_property(void* handle, const char* target, const char* key, const char* value)
{
return media_proxy_once(handle, target, key, value, 0, NULL, 0);
}
int media_recorder_get_property(void* handle, const char* target, const char* key, char* value, int value_len)
{
return media_proxy_once(handle, target, key, NULL, 0, value, value_len);
}
int media_recorder_take_picture(char* params, char* filename, size_t number)
{
int ret = 0;
MediaIOPriv* priv;
if (!number || number > INT_MAX)
return -EINVAL;
priv = calloc(1, sizeof(MediaIOPriv));
if (!priv)
return -ENOMEM;
sem_init(&priv->sem, 0, 0);
priv->cookie = media_recorder_start_picture(params, filename, number, media_recorder_take_picture_cb, priv);
if (!priv->cookie) {
free(priv);
return -EINVAL;
}
sem_wait(&priv->sem);
sem_destroy(&priv->sem);
ret = priv->result;
free(priv);
return ret;
}
void* media_recorder_start_picture(char* params, char* filename, size_t number,
media_event_callback event_cb, void* cookie)
{
char option[32];
void* handle = NULL;
int ret;
if (!number || number > INT_MAX)
return NULL;
handle = media_recorder_open(params);
if (!handle)
return NULL;
ret = media_recorder_set_event_callback(handle, cookie, event_cb);
if (ret < 0)
goto error;
snprintf(option, sizeof(option), "total_number=%zu", number);
ret = media_recorder_prepare(handle, filename, option);
if (ret < 0)
goto error;
ret = media_recorder_start(handle);
if (ret < 0)
goto error;
return handle;
error:
media_recorder_close(handle);
return NULL;
}
int media_recorder_finish_picture(void* handle)
{
return media_recorder_close(handle);
}
最新发布