• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer unit tests for the RTSP support library
2  * Copyright (C) 2010 Andy Wingo <wingo@oblong.com>
3  * Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <glib/gstdio.h>
26 #include <gst/check/gstcheck.h>
27 
28 #include <fcntl.h>
29 #include <gst/allocators/gstdmabuf.h>
30 #include <gst/allocators/gstfdmemory.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 
36 #define FILE_SIZE 4096
37 
38 
GST_START_TEST(test_dmabuf)39 GST_START_TEST (test_dmabuf)
40 {
41   char tmpfilename[] = "/tmp/dmabuf-test.XXXXXX";
42   int fd;
43   GstMemory *mem;
44   GstAllocator *alloc;
45   GstMapInfo info;
46 
47   fd = mkstemp (tmpfilename);
48   fail_unless (fd > 0);
49   fail_unless (g_unlink (tmpfilename) == 0);
50 
51   alloc = gst_dmabuf_allocator_new ();
52 
53   mem = gst_dmabuf_allocator_alloc (alloc, fd, FILE_SIZE);
54 
55   fail_unless (gst_memory_map (mem, &info, GST_MAP_READWRITE));
56   fail_unless (info.flags == GST_MAP_READWRITE);
57   fail_unless (info.data != NULL);
58   fail_unless (info.size == FILE_SIZE);
59   fail_unless (info.maxsize == FILE_SIZE);
60   gst_memory_unmap (mem, &info);
61 
62   gst_memory_unref (mem);
63   g_object_unref (alloc);
64 }
65 
66 GST_END_TEST;
67 
GST_START_TEST(test_fdmem)68 GST_START_TEST (test_fdmem)
69 {
70   GstAllocator *alloc;
71   GstMemory *mem;
72   GstMapInfo info;
73   GError *error = NULL;
74   int fd;
75   const char *data = "0123456789";
76 
77   fd = g_file_open_tmp (NULL, NULL, &error);
78   fail_if (error);
79   fail_unless (write (fd, data, 10) == 10);
80 
81   alloc = gst_fd_allocator_new ();
82   fail_unless (alloc);
83   mem = gst_fd_allocator_alloc (alloc, fd, 10, GST_FD_MEMORY_FLAG_KEEP_MAPPED);
84 
85   fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
86   fail_unless (info.data[5] == '5');
87   gst_memory_unmap (mem, &info);
88 
89   fail_unless (gst_memory_map (mem, &info, GST_MAP_WRITE));
90   info.data[5] = 'X';
91   gst_memory_unmap (mem, &info);
92 
93   fail_unless (gst_memory_map (mem, &info, GST_MAP_READ));
94   fail_unless (info.data[5] == 'X');
95   gst_memory_unmap (mem, &info);
96 
97   gst_memory_unref (mem);
98   fail_unless (g_close (fd, NULL) == 0);
99   gst_object_unref (alloc);
100 }
101 
102 GST_END_TEST;
103 
104 static Suite *
allocators_suite(void)105 allocators_suite (void)
106 {
107   Suite *s = suite_create ("allocators");
108   TCase *tc_chain = tcase_create ("general");
109 
110   suite_add_tcase (s, tc_chain);
111   tcase_add_test (tc_chain, test_dmabuf);
112   tcase_add_test (tc_chain, test_fdmem);
113 
114   return s;
115 }
116 
117 GST_CHECK_MAIN (allocators);
118