tizen 2.3.1 release
[framework/multimedia/gst-plugins-ext0.10.git] / drmsrc / src / drm_util.c
1 /*
2  * drm-util.c
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Seungbae Shin <seungbae.shin@samsung.com>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at your option)
11  * any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc., 51
20  * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 #include <string.h>
24 #include <drm_trusted_client.h>
25 #include "drm_util.h"
26
27 gboolean drm_util_open (DRM_DECRYPT_HANDLE *phandle, char* file_path, int file_type)
28 {
29         drm_trusted_result_e drm_trusted_result;
30         drm_trusted_open_decrypt_info_s open_decrypt_info;
31         drm_trusted_open_decrypt_resp_data_s open_decrypt_resp;
32         drm_trusted_set_consumption_state_info_s decrypt_state_data = { DRM_CONSUMPTION_STARTED };
33
34         if (phandle == NULL || file_path == NULL) {
35                 GST_ERROR ("Invalid parameter, phandle=%p, file_path=%p", phandle, file_path);
36                 return FALSE;
37         }
38
39         /* Fill parameter structure for opening decrypt session */
40         memset (&open_decrypt_info, 0, sizeof (drm_trusted_open_decrypt_info_s));
41         memset (&open_decrypt_resp, 0, sizeof (drm_trusted_open_decrypt_resp_data_s));
42
43         strncpy (open_decrypt_info.filePath, file_path, sizeof (open_decrypt_info.filePath)-1);
44         open_decrypt_info.file_type = file_type;
45         open_decrypt_info.permission = DRM_TRUSTED_PERMISSION_TYPE_PLAY;
46
47         /* Open decrypt session */
48         drm_trusted_result = drm_trusted_open_decrypt_session(&open_decrypt_info, &open_decrypt_resp, phandle);
49         if (drm_trusted_result != DRM_TRUSTED_RETURN_SUCCESS || *phandle == NULL) {
50                 GST_ERROR ("Error in drm_trusted_open_decrypt_session(), error=%x, *phandle=%p,", drm_trusted_result, *phandle);
51                 return FALSE;
52         }
53         GST_DEBUG ("drm_trusted_open_decrypt_session () success!!, ret=%x, *phandle=%p", drm_trusted_result, *phandle);
54
55         /* Set decryption state to STARTED */
56         drm_trusted_result = drm_trusted_set_decrypt_state(*phandle, &decrypt_state_data);
57         if (drm_trusted_result != DRM_TRUSTED_RETURN_SUCCESS) {
58                 GST_ERROR ("Error in drm_trusted_set_decrypt_state(), error=%x", drm_trusted_result);
59                 drm_trusted_close_decrypt_session (phandle);
60                 *phandle = NULL;
61                 return FALSE;
62         }
63         GST_DEBUG ("drm_trusted_set_decrypt_state () success!!, ret=%x, *phandle=%p", drm_trusted_result, *phandle);
64
65         return TRUE;
66 }
67
68 gboolean drm_util_read (DRM_DECRYPT_HANDLE handle, unsigned char* buf, unsigned int buf_length, unsigned int *read_size)
69 {
70         drm_trusted_result_e drm_trusted_result;
71         drm_trusted_payload_info_s payload_info;
72         drm_trusted_read_decrypt_resp_data_s decrypt_resp;
73
74         if (handle == NULL || buf == NULL) {
75                 GST_ERROR ("Invalid parameter, handle=%p, buf=%p", handle, buf);
76                 return FALSE;
77         }
78
79         GST_DEBUG ("Enter [%s] buf=%p, length=%d, read_size=%p", __func__, buf, buf_length, read_size);
80
81         /* fill input/output data */
82         memset (&payload_info, 0, sizeof (drm_trusted_payload_info_s));
83         memset (&decrypt_resp, 0, sizeof (drm_trusted_read_decrypt_resp_data_s));
84
85         payload_info.payload_data = buf;
86         payload_info.payload_data_len = buf_length;
87         payload_info.payload_data_output = buf;
88
89         drm_trusted_result = drm_trusted_read_decrypt_session (handle, &payload_info, &decrypt_resp);
90         if(drm_trusted_result != DRM_TRUSTED_RETURN_SUCCESS) {
91                 GST_ERROR ("Error in drm_trusted_read_decrypt_session() [%x]", drm_trusted_result);
92                 return FALSE;
93         }
94
95         GST_DEBUG ("Leave [%s], drm_trusted_read_decrypt_session() success, requested=%d, read_size=%d", __func__, buf_length, decrypt_resp.read_size);
96         if (read_size)
97                 *read_size = decrypt_resp.read_size;
98
99         return TRUE;
100 }
101
102 gboolean drm_util_seek (DRM_DECRYPT_HANDLE handle, int offset, int mode)
103 {
104         drm_trusted_result_e drm_trusted_result;
105         drm_trusted_seek_decrypt_info_s seek_decrypt_info;
106         memset (&seek_decrypt_info, 0, sizeof(drm_trusted_seek_decrypt_info_s));
107
108         GST_DEBUG ("Enter [%s] offset=%d, mode=%d", __func__, offset, mode);
109
110         seek_decrypt_info.offset = offset;
111         seek_decrypt_info.seek_mode = mode;
112
113         drm_trusted_result = drm_trusted_seek_decrypt_session(handle, &seek_decrypt_info);
114         if (drm_trusted_result != DRM_TRUSTED_RETURN_SUCCESS) {
115                 GST_ERROR ("Error in drm_trusted_seek_decrypt_session(), error=%x", drm_trusted_result);
116                 return FALSE;
117         }
118
119         GST_DEBUG ("Leave [%s], drm_trusted_seek_decrypt_session () success!!", __func__);
120         return TRUE;
121 }
122
123 gboolean drm_util_tell (DRM_DECRYPT_HANDLE handle, unsigned int *offset)
124 {
125         drm_trusted_result_e drm_trusted_result;
126         drm_trusted_tell_decrypt_resp_data_s tell_decrypt_resp_data;
127
128         if (handle == NULL || offset == NULL) {
129                 GST_ERROR ("Invalid parameter, handle=%p, offset=%p", handle, offset);
130                 return FALSE;
131         }
132
133         GST_DEBUG ("Enter [%s] offset=%p", __func__, offset);
134
135         memset (&tell_decrypt_resp_data, 0, sizeof(drm_trusted_tell_decrypt_resp_data_s));
136
137         drm_trusted_result = drm_trusted_tell_decrypt_session(handle, &tell_decrypt_resp_data);
138         if (drm_trusted_result != DRM_TRUSTED_RETURN_SUCCESS) {
139                 GST_ERROR ("Error in drm_trusted_tell_decrypt_session(), error=%x", drm_trusted_result);
140                 return FALSE;
141         }
142
143         if (offset)
144                 *offset = tell_decrypt_resp_data.offset;
145
146         GST_DEBUG ("Leave [%s], drm_trusted_tell_decrypt_session () success!!", __func__);
147
148         return TRUE;
149 }
150
151 gboolean drm_util_close (DRM_DECRYPT_HANDLE *phandle)
152 {
153         drm_trusted_result_e drm_trusted_result;
154         drm_trusted_set_consumption_state_info_s decrypt_state_data = { DRM_CONSUMPTION_STOPPED };
155
156         if (phandle == NULL) {
157                 GST_ERROR ("Invalid parameter, phandle=%p", phandle);
158                 return FALSE;
159         }
160
161         /* Set decryption state to STOPPED */
162         drm_trusted_result = drm_trusted_set_decrypt_state(*phandle, &decrypt_state_data);
163         if (drm_trusted_result != DRM_TRUSTED_RETURN_SUCCESS) {
164                 GST_ERROR ("Error in drm_trusted_set_decrypt_state(), error=%x", drm_trusted_result);
165         } else {
166                 GST_DEBUG ("drm_trusted_set_decrypt_state () success!!");
167         }
168
169         /* Close decrypt session */
170         drm_trusted_result = drm_trusted_close_decrypt_session(phandle);
171         if(drm_trusted_result != DRM_TRUSTED_RETURN_SUCCESS) {
172                 GST_ERROR ("Error in drm_trusted_close_decrypt_session() error=%x", drm_trusted_result);
173                 return FALSE;
174         }
175         GST_DEBUG ("drm_trusted_close_decrypt_session() success!!!");
176
177         return TRUE;
178 }