├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── client.c ├── client.py ├── example.gif ├── gsttimeoverlayparse.c ├── gsttimeoverlayparse.h ├── gsttimestampoverlay.c ├── gsttimestampoverlay.h ├── plugin.c └── server.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | client 4 | server 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | (This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.) 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | {description} 474 | Copyright (C) {year} {fullname} 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | {signature of Ty Coon}, 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: client server libgsttimeoverlayparse.so 2 | 3 | CFLAGS?=-Wall -Werror -O2 4 | 5 | libgsttimeoverlayparse.so : \ 6 | gsttimestampoverlay.c \ 7 | gsttimestampoverlay.h \ 8 | gsttimeoverlayparse.c \ 9 | gsttimeoverlayparse.h \ 10 | plugin.c 11 | $(CC) -o$@ --shared -fPIC $^ $(CFLAGS) \ 12 | $$(pkg-config --cflags --libs gstreamer-1.0 gstreamer-video-1.0) 13 | 14 | server : server.c 15 | $(CC) -o$@ $^ $(CFLAGS) $$(pkg-config --cflags --libs gstreamer-1.0) -lm 16 | 17 | client : client.c 18 | $(CC) -o$@ $^ $(CFLAGS) $$(pkg-config --cflags --libs gstreamer-1.0) 19 | 20 | dist: 21 | git archive -o latency-clock-0.0.1.tar HEAD --prefix=latency-clock-0.0.1/ 22 | 23 | clean: 24 | rm -f client server gsttimestampoverlay.so 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | latency-clock 2 | ============= 3 | 4 | Measure the latency of a video-output/video-capture combination. 5 | 6 | `server` generates a video, drawing timestamps onto the video itself as 64-bit 7 | nanoseconds binary in 8x8px black and white boxes. It is intended to be run on 8 | a Raspberry Pi, but should work anywhere. 9 | 10 | There are six timestamps recorded. The relevant one is the sixth. This is the 11 | time the frame will be displayed as 64-bit nanoseconds since the unix epoch 12 | (realtime). 13 | 14 | The output looks like: 15 | 16 | ![server video output](example.gif) 17 | 18 | `client` reads the timestamps back from the video and logs the latency to stderr 19 | when run with `GST_DEBUG=timeoverlayparse:4`. It is intended to be run on a 20 | system that is capturing the video generated by the Raspberry Pi. 21 | 22 | `client.py` is a separate implementation of the client in Python, using 23 | [stb-tester](https://stb-tester.com). 24 | 25 | For an example use-case see 26 | . 27 | 28 | LICENCE 29 | ------- 30 | 31 | LGPLv2.1, the same as GStreamer that this is based on. 32 | -------------------------------------------------------------------------------- /client.c: -------------------------------------------------------------------------------- 1 | /* GStreamer 2 | * 3 | * Copyright (C) 2016 William Manley 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 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data); 27 | 28 | int main(int argc, char* argv[]) 29 | { 30 | GMainLoop *loop; 31 | GstBus *bus; 32 | GstElement * epipeline; 33 | GstPipeline * pipeline; 34 | GstClock* clock; 35 | GError * err = NULL; 36 | gchar * source_pipeline; 37 | struct timespec ts; 38 | int res; 39 | 40 | gst_init(&argc, &argv); 41 | 42 | loop = g_main_loop_new (NULL, FALSE); 43 | 44 | if (argc > 1) 45 | source_pipeline = argv[1]; 46 | else 47 | source_pipeline = "v4l2src"; 48 | 49 | epipeline = gst_parse_launch (g_strdup_printf ( 50 | "%s " 51 | "! video/x-raw,width=1280,height=720 " 52 | "! timeoverlayparse " 53 | "! fakesink", source_pipeline), &err); 54 | 55 | if (err) { 56 | fprintf(stderr, "Error creating pipeline: %s\n", err->message); 57 | return 1; 58 | } 59 | g_return_val_if_fail (epipeline != NULL, 1); 60 | pipeline = GST_PIPELINE(epipeline); 61 | 62 | /* we add a message handler */ 63 | bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); 64 | gst_bus_add_watch (bus, bus_call, loop); 65 | gst_object_unref (bus); 66 | 67 | /* gst_element_set_state(epipeline, GST_STATE_READY); */ 68 | 69 | clock = g_object_new (GST_TYPE_SYSTEM_CLOCK, "clock-type", 70 | GST_CLOCK_TYPE_REALTIME, NULL); 71 | gst_pipeline_use_clock(pipeline, clock); 72 | 73 | res = clock_gettime(CLOCK_REALTIME, &ts); 74 | g_return_val_if_fail (res == 0, 1); 75 | 76 | gst_element_set_state(epipeline, GST_STATE_PLAYING); 77 | 78 | g_main_loop_run (loop); 79 | 80 | return 0; 81 | } 82 | 83 | static gboolean 84 | bus_call (GstBus *bus, GstMessage *msg, gpointer data) 85 | { 86 | GMainLoop *loop = (GMainLoop *) data; 87 | 88 | switch (GST_MESSAGE_TYPE (msg)) { 89 | 90 | case GST_MESSAGE_EOS: 91 | g_print ("End of stream\n"); 92 | g_main_loop_quit (loop); 93 | break; 94 | 95 | case GST_MESSAGE_ERROR: { 96 | gchar *debug; 97 | GError *error; 98 | 99 | gst_message_parse_error (msg, &error, &debug); 100 | g_free (debug); 101 | 102 | g_printerr ("Error: %s\n", error->message); 103 | g_error_free (error); 104 | 105 | exit (1); 106 | break; 107 | } 108 | default: 109 | break; 110 | } 111 | 112 | return TRUE; 113 | } 114 | -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | """ 2 | This script is to be run using Stb-tester. 3 | 4 | Copyright (C) 2016 William Manley 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Library General Public 8 | License as published by the Free Software Foundation; either 9 | version 2 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Library General Public License for more details. 15 | 16 | You should have received a copy of the GNU Library General Public 17 | License along with this library; if not, write to the 18 | Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 19 | Boston, MA 02110-1301, USA. 20 | """ 21 | 22 | from __future__ import division 23 | 24 | import time 25 | 26 | import numpy 27 | import stbt 28 | from numpy.lib.recfunctions import merge_arrays 29 | 30 | 31 | NUM_TIMESTAMPS = 6 32 | SQUARE_SIZE = 8 33 | NUM_SQUARES = 64 34 | NUM_SAMPLES = 50*25 35 | 36 | 37 | VIDEO_TIMESTAMPS_FIELDS = [ 38 | ("buffer_time", float), 39 | ("stream_time", float), 40 | ("running_time", float), 41 | ("clock_time", float), 42 | ("render_time", float), 43 | ("render_realtime", float), 44 | ] 45 | VIDEO_TIMESTAMPS = numpy.dtype(VIDEO_TIMESTAMPS_FIELDS) 46 | 47 | OWN_TIMESTAMPS_FIELDS = [ 48 | ("hw_receive_time", float), 49 | ("stbt_receive_time", float), 50 | ] 51 | OWN_TIMESTAMPS = numpy.dtype(OWN_TIMESTAMPS_FIELDS) 52 | 53 | RECORD = numpy.dtype(VIDEO_TIMESTAMPS_FIELDS + OWN_TIMESTAMPS_FIELDS) 54 | 55 | def test_measure_latency(): 56 | data = numpy.ndarray(shape=(NUM_SAMPLES), dtype=RECORD) 57 | 58 | for n, (frame, _) in enumerate(stbt.frames(50)): 59 | if n >= NUM_SAMPLES: 60 | break 61 | stbt_receive_time = numpy.array( 62 | [(frame.time, time.time())], dtype=OWN_TIMESTAMPS) 63 | timestamps = read_timestamps(frame) 64 | data[n] = merge_arrays([timestamps, stbt_receive_time], flatten=True, 65 | usemask=False) 66 | 67 | # Sometimes we'll lose a row but no biggie 68 | numpy.savetxt("latency-test.txt", data[:n]) 69 | 70 | 71 | def read_timestamps(frame): 72 | startline = (frame.shape[0] - NUM_TIMESTAMPS * SQUARE_SIZE) // 2 73 | endline = startline + NUM_TIMESTAMPS * SQUARE_SIZE 74 | startcol = (frame.shape[1] - NUM_SQUARES * SQUARE_SIZE) // 2 75 | endcol = startcol + NUM_SQUARES * SQUARE_SIZE 76 | 77 | bits = numpy.packbits( 78 | (frame[startline:endline:8, startcol:endcol:8, 0] & 0x80), 79 | axis=1) 80 | timestamps = numpy.fromstring(bits.data, dtype=">u8").astype(float) / 1e9 81 | return timestamps.view(dtype=VIDEO_TIMESTAMPS) 82 | -------------------------------------------------------------------------------- /example.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stb-tester/latency-clock/c2d166139e401b274e96445f5020c4ebb8c82bdd/example.gif -------------------------------------------------------------------------------- /gsttimeoverlayparse.c: -------------------------------------------------------------------------------- 1 | /* GStreamer 2 | * Copyright (C) 2016 William Manley 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Library General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Library General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Library General Public 15 | * License along with this library; if not, write to the 16 | * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, 17 | * Boston, MA 02110-1335, USA. 18 | */ 19 | /** 20 | * SECTION:element-gsttimeoverlayparse 21 | * 22 | * The timeoverlayparse element records various timestamps onto the video. 23 | * 24 | * 25 | * Example launch line 26 | * |[ 27 | * gst-launch -v fakesrc ! timeoverlayparse ! timeoverlayparse ! fakesink 28 | * ]| 29 | * FIXME Describe what the pipeline does. 30 | * 31 | */ 32 | 33 | #ifdef HAVE_CONFIG_H 34 | #include "config.h" 35 | #endif 36 | 37 | #include 38 | #include 39 | #include 40 | #include "gsttimeoverlayparse.h" 41 | 42 | #include 43 | 44 | GST_DEBUG_CATEGORY_STATIC (gst_timeoverlayparse_debug_category); 45 | #define GST_CAT_DEFAULT gst_timeoverlayparse_debug_category 46 | 47 | /* prototypes */ 48 | static GstFlowReturn gst_timeoverlayparse_transform_frame_ip (GstVideoFilter * filter, 49 | GstVideoFrame * frame); 50 | 51 | enum 52 | { 53 | PROP_0 54 | }; 55 | 56 | /* pad templates */ 57 | 58 | /* FIXME: add/remove formats you can handle */ 59 | #define VIDEO_SRC_CAPS \ 60 | GST_VIDEO_CAPS_MAKE("{RGB, xRGB, BGR, BGRx}") 61 | 62 | /* FIXME: add/remove formats you can handle */ 63 | #define VIDEO_SINK_CAPS \ 64 | GST_VIDEO_CAPS_MAKE("{RGB, xRGB, BGR, BGRx}") 65 | 66 | 67 | /* class initialization */ 68 | 69 | G_DEFINE_TYPE_WITH_CODE (GstTimeOverlayParse, gst_timeoverlayparse, GST_TYPE_VIDEO_FILTER, 70 | GST_DEBUG_CATEGORY_INIT (gst_timeoverlayparse_debug_category, "timeoverlayparse", 0, 71 | "debug category for timeoverlayparse element")); 72 | 73 | static void 74 | gst_timeoverlayparse_class_init (GstTimeOverlayParseClass * klass) 75 | { 76 | GstVideoFilterClass *video_filter_class = GST_VIDEO_FILTER_CLASS (klass); 77 | 78 | /* Setting up pads and setting metadata should be moved to 79 | base_class_init if you intend to subclass this class. */ 80 | gst_element_class_add_pad_template (GST_ELEMENT_CLASS(klass), 81 | gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, 82 | gst_caps_from_string (VIDEO_SRC_CAPS))); 83 | gst_element_class_add_pad_template (GST_ELEMENT_CLASS(klass), 84 | gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, 85 | gst_caps_from_string (VIDEO_SINK_CAPS))); 86 | 87 | gst_element_class_set_static_metadata (GST_ELEMENT_CLASS(klass), 88 | "TimeOverlayParse", "Generic", "Reads the various timestamps from the " 89 | "video written by timestampoverlay", 90 | "William Manley "); 91 | 92 | video_filter_class->transform_frame_ip = GST_DEBUG_FUNCPTR (gst_timeoverlayparse_transform_frame_ip); 93 | } 94 | 95 | static void 96 | gst_timeoverlayparse_init (GstTimeOverlayParse *timeoverlayparse) 97 | { 98 | } 99 | 100 | typedef struct { 101 | GstClockTime buffer_time; 102 | GstClockTime stream_time; 103 | GstClockTime running_time; 104 | GstClockTime clock_time; 105 | GstClockTime render_time; 106 | GstClockTime render_realtime; 107 | } Timestamps; 108 | 109 | static GstClockTime 110 | read_timestamp(int lineoffset, unsigned char* buf, size_t stride, int pxsize) 111 | { 112 | int bit; 113 | GstClockTime timestamp = 0; 114 | 115 | buf += (lineoffset * 8 + 4) * stride; 116 | 117 | for (bit = 0; bit < 64; bit++) { 118 | char color = buf[bit * pxsize * 8 + 4]; 119 | timestamp |= (color & 0x80) ? (guint64) 1 << (63 - bit) : 0; 120 | } 121 | 122 | return timestamp; 123 | } 124 | 125 | static GstFlowReturn 126 | gst_timeoverlayparse_transform_frame_ip (GstVideoFilter * filter, GstVideoFrame * frame) 127 | { 128 | GstTimeOverlayParse *overlay = GST_TIMEOVERLAYPARSE (filter); 129 | Timestamps timestamps; 130 | unsigned char * imgdata; 131 | 132 | GST_DEBUG_OBJECT (overlay, "transform_frame_ip"); 133 | 134 | GstClockTime buffer_time, running_time, clock_time; 135 | GstClockTimeDiff latency; 136 | GstSegment *segment = &GST_BASE_TRANSFORM (overlay)->segment; 137 | 138 | buffer_time = GST_BUFFER_TIMESTAMP (frame->buffer); 139 | 140 | if (!GST_CLOCK_TIME_IS_VALID (buffer_time)) { 141 | GST_DEBUG_OBJECT (filter, "Can't measure latency: buffer timestamp is " 142 | "invalid"); 143 | return GST_FLOW_OK; 144 | } 145 | 146 | if (frame->info.stride[0] < (8 * frame->info.finfo->pixel_stride[0] * 64)) { 147 | GST_WARNING_OBJECT (filter, "Can't read timestamps: video-frame is to narrow"); 148 | return GST_FLOW_OK; 149 | } 150 | 151 | GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT, 152 | GST_TIME_ARGS (buffer_time)); 153 | 154 | running_time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, 155 | buffer_time); 156 | clock_time = running_time + gst_element_get_base_time (GST_ELEMENT (overlay)); 157 | 158 | GST_DEBUG_OBJECT (filter, "Buffer timestamps" 159 | ": buffer_time = %" GST_TIME_FORMAT 160 | ", running_time = %" GST_TIME_FORMAT 161 | ", clock_time = %" GST_TIME_FORMAT, 162 | GST_TIME_ARGS(buffer_time), 163 | GST_TIME_ARGS(running_time), 164 | GST_TIME_ARGS(clock_time)); 165 | 166 | imgdata = frame->data[0]; 167 | 168 | /* Centre Vertically: */ 169 | imgdata += (frame->info.height - 6 * 8) * frame->info.stride[0] / 2; 170 | 171 | /* Centre Horizontally: */ 172 | imgdata += (frame->info.width - 64 * 8) * frame->info.finfo->pixel_stride[0] 173 | / 2; 174 | 175 | timestamps.buffer_time = read_timestamp (0, imgdata, 176 | frame->info.stride[0], frame->info.finfo->pixel_stride[0]); 177 | timestamps.stream_time = read_timestamp (1, imgdata, 178 | frame->info.stride[0], frame->info.finfo->pixel_stride[0]); 179 | timestamps.running_time = read_timestamp (2, imgdata, 180 | frame->info.stride[0], frame->info.finfo->pixel_stride[0]); 181 | timestamps.clock_time = read_timestamp (3, imgdata, 182 | frame->info.stride[0], frame->info.finfo->pixel_stride[0]); 183 | timestamps.render_time = read_timestamp (4, imgdata, 184 | frame->info.stride[0], frame->info.finfo->pixel_stride[0]); 185 | timestamps.render_realtime = read_timestamp (5, imgdata, 186 | frame->info.stride[0], frame->info.finfo->pixel_stride[0]); 187 | 188 | GST_DEBUG_OBJECT (filter, "Read timestamps: buffer_time = %" GST_TIME_FORMAT 189 | ", stream_time = %" GST_TIME_FORMAT ", running_time = %" GST_TIME_FORMAT 190 | ", clock_time = %" GST_TIME_FORMAT ", render_time = %" GST_TIME_FORMAT 191 | ", render_realtime = %" GST_TIME_FORMAT, 192 | GST_TIME_ARGS(timestamps.buffer_time), 193 | GST_TIME_ARGS(timestamps.stream_time), 194 | GST_TIME_ARGS(timestamps.running_time), 195 | GST_TIME_ARGS(timestamps.clock_time), 196 | GST_TIME_ARGS(timestamps.render_time), 197 | GST_TIME_ARGS(timestamps.render_realtime)); 198 | 199 | latency = clock_time - timestamps.render_realtime; 200 | 201 | GST_INFO_OBJECT (filter, "Latency: %" GST_TIME_FORMAT, 202 | GST_TIME_ARGS(latency)); 203 | 204 | return GST_FLOW_OK; 205 | } 206 | -------------------------------------------------------------------------------- /gsttimeoverlayparse.h: -------------------------------------------------------------------------------- 1 | /* GStreamer 2 | * Copyright (C) 2016 William Manley 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Library General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Library General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Library General Public 15 | * License along with this library; if not, write to the 16 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 17 | * Boston, MA 02110-1301, USA. 18 | */ 19 | 20 | #ifndef _GST_TIMEOVERLAYPARSE_H_ 21 | #define _GST_TIMEOVERLAYPARSE_H_ 22 | 23 | #include 24 | #include 25 | 26 | G_BEGIN_DECLS 27 | 28 | #define GST_TYPE_TIMEOVERLAYPARSE (gst_timeoverlayparse_get_type()) 29 | #define GST_TIMEOVERLAYPARSE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_TIMEOVERLAYPARSE,GstTimeOverlayParse)) 30 | #define GST_TIMEOVERLAYPARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_TIMEOVERLAYPARSE,GstTimeOverlayParseClass)) 31 | #define GST_IS_TIMEOVERLAYPARSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_TIMEOVERLAYPARSE)) 32 | #define GST_IS_TIMEOVERLAYPARSE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_TIMEOVERLAYPARSE)) 33 | 34 | typedef struct _GstTimeOverlayParse GstTimeOverlayParse; 35 | typedef struct _GstTimeOverlayParseClass GstTimeOverlayParseClass; 36 | 37 | struct _GstTimeOverlayParse 38 | { 39 | GstVideoFilter base_timeoverlayparse; 40 | }; 41 | 42 | struct _GstTimeOverlayParseClass 43 | { 44 | GstVideoFilterClass base_timeoverlayparse_class; 45 | }; 46 | 47 | GType gst_timeoverlayparse_get_type (void); 48 | 49 | G_END_DECLS 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /gsttimestampoverlay.c: -------------------------------------------------------------------------------- 1 | /* GStreamer 2 | * Copyright (C) 2016 William Manley 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Library General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Library General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Library General Public 15 | * License along with this library; if not, write to the 16 | * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, 17 | * Boston, MA 02110-1335, USA. 18 | */ 19 | /** 20 | * SECTION:element-gsttimestampoverlay 21 | * 22 | * The timestampoverlay element records various timestamps onto the video. 23 | * 24 | * 25 | * Example launch line 26 | * |[ 27 | * gst-launch -v fakesrc ! timestampoverlay ! FIXME ! fakesink 28 | * ]| 29 | * FIXME Describe what the pipeline does. 30 | * 31 | */ 32 | 33 | #ifdef HAVE_CONFIG_H 34 | #include "config.h" 35 | #endif 36 | 37 | #include 38 | #include 39 | #include 40 | #include "gsttimestampoverlay.h" 41 | 42 | #include 43 | 44 | GST_DEBUG_CATEGORY_STATIC (gst_timestampoverlay_debug_category); 45 | #define GST_CAT_DEFAULT gst_timestampoverlay_debug_category 46 | 47 | /* prototypes */ 48 | static void gst_timestampoverlay_dispose (GObject *object); 49 | static gboolean gst_timestampoverlay_src_event (GstBaseTransform * 50 | basetransform, GstEvent * event); 51 | static GstFlowReturn gst_timestampoverlay_transform_frame_ip (GstVideoFilter * filter, 52 | GstVideoFrame * frame); 53 | static gboolean gst_timestampoverlay_set_clock (GstElement * element, 54 | GstClock * clock); 55 | 56 | enum 57 | { 58 | PROP_0 59 | }; 60 | 61 | /* pad templates */ 62 | 63 | /* FIXME: add/remove formats you can handle */ 64 | #define VIDEO_SRC_CAPS \ 65 | GST_VIDEO_CAPS_MAKE("{RGB, BGR, BGRx, xBGR, RGB, RGBx, xRGB, RGB15, RGB16}") 66 | 67 | /* FIXME: add/remove formats you can handle */ 68 | #define VIDEO_SINK_CAPS \ 69 | GST_VIDEO_CAPS_MAKE("{RGB, BGR, BGRx, xBGR, RGB, RGBx, xRGB, RGB15, RGB16}") 70 | 71 | 72 | /* class initialization */ 73 | 74 | G_DEFINE_TYPE_WITH_CODE (GstTimeStampOverlay, gst_timestampoverlay, GST_TYPE_VIDEO_FILTER, 75 | GST_DEBUG_CATEGORY_INIT (gst_timestampoverlay_debug_category, "timestampoverlay", 0, 76 | "debug category for timestampoverlay element")); 77 | 78 | static void 79 | gst_timestampoverlay_class_init (GstTimeStampOverlayClass * klass) 80 | { 81 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); 82 | GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass); 83 | GstBaseTransformClass *base_transform_class = GST_BASE_TRANSFORM_CLASS (klass); 84 | GstVideoFilterClass *video_filter_class = GST_VIDEO_FILTER_CLASS (klass); 85 | 86 | /* Setting up pads and setting metadata should be moved to 87 | base_class_init if you intend to subclass this class. */ 88 | gst_element_class_add_pad_template (gstelement_class, 89 | gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, 90 | gst_caps_from_string (VIDEO_SRC_CAPS))); 91 | gst_element_class_add_pad_template (gstelement_class, 92 | gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, 93 | gst_caps_from_string (VIDEO_SINK_CAPS))); 94 | 95 | gst_element_class_set_static_metadata (gstelement_class, 96 | "Timestampoverlay", "Generic", "Draws the various timestamps on the " 97 | "video so they can be read off the video afterwards", 98 | "William Manley "); 99 | 100 | gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_timestampoverlay_dispose); 101 | gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_timestampoverlay_set_clock); 102 | base_transform_class->src_event = GST_DEBUG_FUNCPTR (gst_timestampoverlay_src_event); 103 | video_filter_class->transform_frame_ip = GST_DEBUG_FUNCPTR (gst_timestampoverlay_transform_frame_ip); 104 | } 105 | 106 | static void 107 | gst_timestampoverlay_init (GstTimeStampOverlay *timestampoverlay) 108 | { 109 | GST_OBJECT_FLAG_SET (timestampoverlay, GST_ELEMENT_FLAG_REQUIRE_CLOCK); 110 | 111 | timestampoverlay->latency = GST_CLOCK_TIME_NONE; 112 | timestampoverlay->realtime_clock = g_object_new (GST_TYPE_SYSTEM_CLOCK, 113 | "clock-type", GST_CLOCK_TYPE_REALTIME, NULL); 114 | GST_OBJECT_FLAG_SET (timestampoverlay->realtime_clock, 115 | GST_CLOCK_FLAG_CAN_SET_MASTER); 116 | } 117 | 118 | static void 119 | gst_timestampoverlay_dispose (GObject *object) 120 | { 121 | GstTimeStampOverlay *timeoverlay = GST_TIMESTAMPOVERLAY (object); 122 | g_clear_object (&timeoverlay->realtime_clock); 123 | } 124 | 125 | static gboolean 126 | gst_timestampoverlay_src_event (GstBaseTransform * basetransform, GstEvent * event) 127 | { 128 | GstTimeStampOverlay *timeoverlay = GST_TIMESTAMPOVERLAY (basetransform); 129 | 130 | if (GST_EVENT_TYPE (event) == GST_EVENT_LATENCY) { 131 | GstClockTime latency = GST_CLOCK_TIME_NONE; 132 | gst_event_parse_latency (event, &latency); 133 | GST_OBJECT_LOCK (timeoverlay); 134 | timeoverlay->latency = latency; 135 | GST_OBJECT_UNLOCK (timeoverlay); 136 | } 137 | 138 | /* Chain up */ 139 | return 140 | GST_BASE_TRANSFORM_CLASS (gst_timestampoverlay_parent_class)->src_event 141 | (basetransform, event); 142 | } 143 | 144 | static gboolean 145 | gst_timestampoverlay_set_clock (GstElement * element, GstClock * clock) 146 | { 147 | GstTimeStampOverlay *timestampoverlay = GST_TIMESTAMPOVERLAY (element); 148 | 149 | GST_DEBUG_OBJECT (timestampoverlay, "set_clock (%" GST_PTR_FORMAT ")", clock); 150 | 151 | if (gst_clock_set_master (timestampoverlay->realtime_clock, clock)) { 152 | if (clock) { 153 | /* gst_clock_set_master is asynchronous and may take some time to sync. 154 | * To give it a helping hand we'll initialise it here so we don't send 155 | * through spurious timings with the first buffer. */ 156 | gst_clock_set_calibration (timestampoverlay->realtime_clock, 157 | gst_clock_get_internal_time (timestampoverlay->realtime_clock), 158 | gst_clock_get_time (clock), 1, 1); 159 | } 160 | } else { 161 | GST_WARNING_OBJECT (element, "Failed to slave internal REALTIME clock %" 162 | GST_PTR_FORMAT " to master clock %" GST_PTR_FORMAT, 163 | timestampoverlay->realtime_clock, clock); 164 | } 165 | 166 | return GST_ELEMENT_CLASS (gst_timestampoverlay_parent_class)->set_clock (element, 167 | clock); 168 | } 169 | 170 | static void 171 | draw_timestamp(int lineoffset, GstClockTime timestamp, unsigned char* buf, 172 | size_t stride, int pxsize) 173 | { 174 | int bit, line; 175 | buf += lineoffset * 8 * stride; 176 | 177 | for (line = 0; line < 8; line++) { 178 | for (bit = 0; bit < 64; bit++) { 179 | char color = ((timestamp >> (63 - bit)) & 1) * 255; 180 | memset(buf + bit * pxsize * 8, color, pxsize * 8); 181 | } 182 | buf += stride; 183 | } 184 | } 185 | 186 | static GstFlowReturn 187 | gst_timestampoverlay_transform_frame_ip (GstVideoFilter * filter, GstVideoFrame * frame) 188 | { 189 | GstTimeStampOverlay *overlay = GST_TIMESTAMPOVERLAY (filter); 190 | 191 | GST_DEBUG_OBJECT (overlay, "transform_frame_ip"); 192 | 193 | GstClockTime buffer_time, stream_time, running_time, clock_time, latency, 194 | render_time, render_realtime; 195 | GstSegment *segment = &GST_BASE_TRANSFORM (overlay)->segment; 196 | unsigned char * imgdata; 197 | 198 | buffer_time = GST_BUFFER_TIMESTAMP (frame->buffer); 199 | 200 | if (!GST_CLOCK_TIME_IS_VALID (buffer_time)) { 201 | GST_DEBUG_OBJECT (filter, "Can't draw timestamps: buffer timestamp is " 202 | "invalid"); 203 | return GST_FLOW_OK; 204 | } 205 | 206 | if (frame->info.stride[0] < (8 * frame->info.finfo->pixel_stride[0] * 64)) { 207 | GST_WARNING_OBJECT (filter, "Can't draw timestamps: video-frame is to narrow"); 208 | return GST_FLOW_OK; 209 | } 210 | 211 | GST_DEBUG ("buffer with timestamp %" GST_TIME_FORMAT, 212 | GST_TIME_ARGS (buffer_time)); 213 | 214 | stream_time = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, 215 | buffer_time); 216 | running_time = gst_segment_to_running_time (segment, GST_FORMAT_TIME, 217 | buffer_time); 218 | clock_time = running_time + gst_element_get_base_time (GST_ELEMENT (overlay)); 219 | 220 | latency = overlay->latency; 221 | if (GST_CLOCK_TIME_IS_VALID (latency)) 222 | render_time = clock_time + latency; 223 | else 224 | render_time = clock_time; 225 | 226 | GST_OBJECT_LOCK (overlay->realtime_clock); 227 | render_realtime = gst_clock_unadjust_unlocked ( 228 | overlay->realtime_clock, render_time); 229 | GST_OBJECT_UNLOCK (overlay->realtime_clock); 230 | 231 | imgdata = frame->data[0]; 232 | 233 | /* Centre Vertically: */ 234 | imgdata += (frame->info.height - 6 * 8) * frame->info.stride[0] / 2; 235 | 236 | /* Centre Horizontally: */ 237 | imgdata += (frame->info.width - 64 * 8) * frame->info.finfo->pixel_stride[0] 238 | / 2; 239 | 240 | draw_timestamp (0, buffer_time, imgdata, frame->info.stride[0], 241 | frame->info.finfo->pixel_stride[0]); 242 | draw_timestamp (1, stream_time, imgdata, frame->info.stride[0], 243 | frame->info.finfo->pixel_stride[0]); 244 | draw_timestamp (2, running_time, imgdata, frame->info.stride[0], 245 | frame->info.finfo->pixel_stride[0]); 246 | draw_timestamp (3, clock_time, imgdata, frame->info.stride[0], 247 | frame->info.finfo->pixel_stride[0]); 248 | draw_timestamp (4, render_time, imgdata, frame->info.stride[0], 249 | frame->info.finfo->pixel_stride[0]); 250 | draw_timestamp (5, render_realtime, imgdata, frame->info.stride[0], 251 | frame->info.finfo->pixel_stride[0]); 252 | 253 | return GST_FLOW_OK; 254 | } 255 | -------------------------------------------------------------------------------- /gsttimestampoverlay.h: -------------------------------------------------------------------------------- 1 | /* GStreamer 2 | * Copyright (C) 2016 William Manley 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Library General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Library General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Library General Public 15 | * License along with this library; if not, write to the 16 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 17 | * Boston, MA 02110-1301, USA. 18 | */ 19 | 20 | #ifndef _GST_TIMESTAMPOVERLAY_H_ 21 | #define _GST_TIMESTAMPOVERLAY_H_ 22 | 23 | #include 24 | #include 25 | 26 | G_BEGIN_DECLS 27 | 28 | #define GST_TYPE_TIMESTAMPOVERLAY (gst_timestampoverlay_get_type()) 29 | #define GST_TIMESTAMPOVERLAY(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_TIMESTAMPOVERLAY,GstTimeStampOverlay)) 30 | #define GST_TIMESTAMPOVERLAY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_TIMESTAMPOVERLAY,GstTimeStampOverlayClass)) 31 | #define GST_IS_TIMESTAMPOVERLAY(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_TIMESTAMPOVERLAY)) 32 | #define GST_IS_TIMESTAMPOVERLAY_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_TIMESTAMPOVERLAY)) 33 | 34 | typedef struct _GstTimeStampOverlay GstTimeStampOverlay; 35 | typedef struct _GstTimeStampOverlayClass GstTimeStampOverlayClass; 36 | 37 | struct _GstTimeStampOverlay 38 | { 39 | GstVideoFilter base_timestampoverlay; 40 | 41 | GstClockTime latency; 42 | GstClock *realtime_clock; 43 | }; 44 | 45 | struct _GstTimeStampOverlayClass 46 | { 47 | GstVideoFilterClass base_timestampoverlay_class; 48 | }; 49 | 50 | GType gst_timestampoverlay_get_type (void); 51 | 52 | G_END_DECLS 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /plugin.c: -------------------------------------------------------------------------------- 1 | /* GStreamer 2 | * Copyright (C) 2016 William Manley 3 | * 4 | * This library is free software; you can redistribute it and/or 5 | * modify it under the terms of the GNU Library General Public 6 | * License as published by the Free Software Foundation; either 7 | * version 2 of the License, or (at your option) any later version. 8 | * 9 | * This library is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | * Library General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU Library General Public 15 | * License along with this library; if not, write to the 16 | * Free Software Foundation, Inc., 51 Franklin Street, Suite 500, 17 | * Boston, MA 02110-1335, USA. 18 | */ 19 | 20 | #ifdef HAVE_CONFIG_H 21 | #include "config.h" 22 | #endif 23 | 24 | #include 25 | 26 | #include "gsttimeoverlayparse.h" 27 | #include "gsttimestampoverlay.h" 28 | 29 | static gboolean 30 | plugin_init (GstPlugin * plugin) 31 | { 32 | return gst_element_register (plugin, "timestampoverlay", GST_RANK_NONE, 33 | GST_TYPE_TIMESTAMPOVERLAY) && 34 | gst_element_register (plugin, "timeoverlayparse", GST_RANK_NONE, 35 | GST_TYPE_TIMEOVERLAYPARSE); 36 | } 37 | 38 | #ifndef VERSION 39 | #define VERSION "0.0.1" 40 | #endif 41 | #ifndef PACKAGE 42 | #define PACKAGE "latency_clock" 43 | #endif 44 | #ifndef PACKAGE_NAME 45 | #define PACKAGE_NAME "latency_clock" 46 | #endif 47 | #ifndef GST_PACKAGE_ORIGIN 48 | #define GST_PACKAGE_ORIGIN "http://stb-tester.com/" 49 | #endif 50 | 51 | GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, 52 | GST_VERSION_MINOR, 53 | timeoverlayparse, 54 | "Elements for measuring video capture latency", 55 | plugin_init, VERSION, "LGPL", PACKAGE_NAME, GST_PACKAGE_ORIGIN) 56 | 57 | -------------------------------------------------------------------------------- /server.c: -------------------------------------------------------------------------------- 1 | /* GStreamer 2 | * 3 | * Copyright (C) 2016 William Manley 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 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | static gboolean bus_call (GstBus *bus, GstMessage *msg, gpointer data); 28 | static gchar* get_current_mode (void); 29 | 30 | int main(int argc, char* argv[]) 31 | { 32 | GMainLoop *loop; 33 | GstBus *bus; 34 | GstElement * epipeline; 35 | GstElement * mmalvideosink; 36 | GstPipeline * pipeline; 37 | GError * err = NULL; 38 | gchar * sink_pipeline, *pipeline_description; 39 | struct timespec ts; 40 | int res; 41 | GstClock *clock; 42 | 43 | gst_init(&argc, &argv); 44 | 45 | loop = g_main_loop_new (NULL, FALSE); 46 | 47 | if (argc > 1) 48 | sink_pipeline = argv[1]; 49 | else 50 | sink_pipeline = "videoconvert ! mmalvideosink name=mmalsink"; 51 | 52 | pipeline_description = g_strdup_printf ( 53 | "videotestsrc is-live=true pattern=white " 54 | "! %s " 55 | "! timestampoverlay " 56 | "! queue " 57 | "! %s", get_current_mode(), sink_pipeline); 58 | g_printerr ("Using pipeline %s\n", pipeline_description); 59 | epipeline = gst_parse_launch (pipeline_description, &err); 60 | 61 | if (err) { 62 | fprintf(stderr, "Error creating pipeline: %s\n", err->message); 63 | return 1; 64 | } 65 | g_return_val_if_fail (epipeline != NULL, 1); 66 | pipeline = GST_PIPELINE(epipeline); 67 | 68 | mmalvideosink = gst_bin_get_by_name (GST_BIN(pipeline), "mmalsink"); 69 | if (mmalvideosink) { 70 | /* By default mmalvideosink scales the video to fullscreen. We want it 71 | * original resolution but centered */ 72 | GstStructure * destinationwindow = gst_structure_new ("destinationwindow", 73 | "x", G_TYPE_DOUBLE, (double) (1280. - 640.) / 2. / 1280., 74 | "y", G_TYPE_DOUBLE, (double) (720. - 240.) / 2. / 720., 75 | "width", G_TYPE_DOUBLE, (double) 640./1280., 76 | "height", G_TYPE_DOUBLE, (double) 240./720., 77 | NULL); 78 | g_object_set (mmalvideosink, "destinationwindow", destinationwindow, NULL); 79 | gst_object_unref (mmalvideosink); 80 | mmalvideosink = NULL; 81 | } 82 | 83 | /* we add a message handler */ 84 | bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); 85 | gst_bus_add_watch (bus, bus_call, loop); 86 | gst_object_unref (bus); 87 | 88 | gst_pipeline_set_latency (pipeline, 100 * GST_MSECOND); 89 | 90 | gst_element_set_state(epipeline, GST_STATE_READY); 91 | 92 | res = clock_gettime(CLOCK_REALTIME, &ts); 93 | g_return_val_if_fail (res == 0, 1); 94 | 95 | gst_element_set_state(epipeline, GST_STATE_PLAYING); 96 | 97 | clock = gst_pipeline_get_clock (pipeline); 98 | GST_INFO("Pipeline clock is %" GST_PTR_FORMAT, clock); 99 | g_clear_object (&clock); 100 | 101 | g_main_loop_run (loop); 102 | 103 | return 0; 104 | } 105 | 106 | 107 | static gboolean 108 | bus_call (GstBus *bus, GstMessage *msg, gpointer data) 109 | { 110 | GMainLoop *loop = (GMainLoop *) data; 111 | 112 | switch (GST_MESSAGE_TYPE (msg)) { 113 | 114 | case GST_MESSAGE_EOS: 115 | g_print ("End of stream\n"); 116 | g_main_loop_quit (loop); 117 | break; 118 | 119 | case GST_MESSAGE_ERROR: { 120 | gchar *debug; 121 | GError *error; 122 | 123 | gst_message_parse_error (msg, &error, &debug); 124 | g_free (debug); 125 | 126 | g_printerr ("Error: %s\n", error->message); 127 | g_error_free (error); 128 | 129 | exit (1); 130 | break; 131 | } 132 | default: 133 | break; 134 | } 135 | 136 | return TRUE; 137 | } 138 | 139 | struct frac { 140 | int n, d; 141 | }; 142 | 143 | struct frac fps_to_frac(double fps) { 144 | struct frac out; 145 | if (fabs(fps * 1001. / 1000. - round(fps)) > fabs(round(fps) - fps)) 146 | out.d = 1; 147 | else 148 | out.d = 1001; 149 | 150 | out.n = round(fps * out.d); 151 | return out; 152 | } 153 | 154 | static gchar* 155 | get_current_mode (void) 156 | { 157 | gchar *tv_stderr = NULL, *tv_stdout = NULL; 158 | gint tv_exit_status = -1; 159 | GError * err = NULL; 160 | GRegex * regex; 161 | GMatchInfo * match_info = NULL; 162 | struct frac fps; 163 | gchar* argv[] = {"tvservice", "-s", NULL}; 164 | 165 | /* On a Raspberry Pi we can get the current mode from tvservice so we can set 166 | * the caps appropriately */ 167 | g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &tv_stdout, 168 | &tv_stderr, &tv_exit_status, &err); 169 | 170 | if (err) { 171 | g_printerr ("Failed to run tvservice, falling back to defaults: %s\n", 172 | err->message); 173 | goto error; 174 | } 175 | if (tv_exit_status != 0) { 176 | g_printerr ("tvservice failed, falling back to defaults: %s\n", tv_stderr); 177 | goto error; 178 | } 179 | 180 | regex = g_regex_new (" (\\d+)x(\\d+) @ (\\d+\\.\\d+)Hz", 0, 0, NULL); 181 | if (!g_regex_match (regex, tv_stdout, 0, &match_info)) { 182 | g_printerr("Failed to parse tvservice output, falling back to defaults. " 183 | "Output was %s\n", tv_stdout); 184 | goto error; 185 | }; 186 | fps = fps_to_frac (g_strtod (g_match_info_fetch (match_info, 3), NULL)); 187 | return g_strdup_printf ("video/x-raw,width=640,height=240,framerate=%i/%i", 188 | fps.n, fps.d); 189 | error: 190 | return "video/x-raw,width=640,height=240,framerate=50/1"; 191 | } 192 | --------------------------------------------------------------------------------