mod_format.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Header for providers of file and format handling routines.
  20. * Clients of these routines should include "asterisk/file.h" instead.
  21. */
  22. #ifndef _ASTERISK_MOD_FORMAT_H
  23. #define _ASTERISK_MOD_FORMAT_H
  24. #include "asterisk/file.h"
  25. #include "asterisk/frame.h"
  26. #if defined(__cplusplus) || defined(c_plusplus)
  27. extern "C" {
  28. #endif
  29. /*! \brief
  30. * Each supported file format is described by the following structure.
  31. *
  32. * Not all are necessary, the support routine implement default
  33. * values for some of them.
  34. * A handler typically fills a structure initializing the desired
  35. * fields, and then calls ast_format_def_register() with the (readonly)
  36. * structure as an argument.
  37. */
  38. struct ast_format_def {
  39. char name[80]; /*!< Name of format */
  40. char exts[80]; /*!< Extensions (separated by | if more than one)
  41. * this format can read. First is assumed for writing (e.g. .mp3) */
  42. struct ast_format *format; /*!< Format of frames it uses/provides (one only) */
  43. /*!
  44. * \brief Prepare an input stream for playback.
  45. * \return 0 on success, -1 on error.
  46. * The FILE is already open (in s->f) so this function only needs to perform
  47. * any applicable validity checks on the file. If none is required, the
  48. * function can be omitted.
  49. */
  50. int (*open)(struct ast_filestream *s);
  51. /*!
  52. * \brief Prepare a stream for output, and comment it appropriately if applicable.
  53. * \return 0 on success, -1 on error.
  54. * Same as the open, the FILE is already open so the function just needs to
  55. * prepare any header and other fields, if any.
  56. * The function can be omitted if nothing is needed.
  57. */
  58. int (*rewrite)(struct ast_filestream *s, const char *comment);
  59. /*! Write a frame to a channel */
  60. int (*write)(struct ast_filestream *, struct ast_frame *);
  61. /*! seek num samples into file, whence - like a normal seek but with offset in samples */
  62. int (*seek)(struct ast_filestream *, off_t, int);
  63. int (*trunc)(struct ast_filestream *fs); /*!< trunc file to current position */
  64. off_t (*tell)(struct ast_filestream *fs); /*!< tell current position */
  65. /*! Read the next frame from the filestream (if available) and report
  66. * when to get next frame (in samples)
  67. */
  68. struct ast_frame * (*read)(struct ast_filestream *, int *whennext);
  69. /*! Do any closing actions, if any. The descriptor and structure are closed
  70. * and destroyed by the generic routines, so they must not be done here. */
  71. void (*close)(struct ast_filestream *);
  72. char * (*getcomment)(struct ast_filestream *); /*!< Retrieve file comment */
  73. AST_LIST_ENTRY(ast_format_def) list; /*!< Link */
  74. /*!
  75. * If the handler needs a buffer (for read, typically)
  76. * and/or a private descriptor, put here the
  77. * required size (in bytes) and the support routine will allocate them
  78. * for you, pointed by s->buf and s->private, respectively.
  79. * When allocating a buffer, remember to leave AST_FRIENDLY_OFFSET
  80. * spare bytes at the bginning.
  81. */
  82. int buf_size; /*!< size of frame buffer, if any, aligned to 8 bytes. */
  83. int desc_size; /*!< size of private descriptor, if any */
  84. struct ast_module *module;
  85. };
  86. /*! \brief
  87. * This structure is allocated by file.c in one chunk,
  88. * together with buf_size and desc_size bytes of memory
  89. * to be used for private purposes (e.g. buffers etc.)
  90. */
  91. struct ast_filestream {
  92. /*! Everybody reserves a block of AST_RESERVED_POINTERS pointers for us */
  93. struct ast_format_def *fmt; /* need to write to the lock and usecnt */
  94. int flags;
  95. mode_t mode;
  96. char *open_filename;
  97. char *filename;
  98. char *realfilename;
  99. /*! Video file stream */
  100. struct ast_filestream *vfs;
  101. /*! Transparently translate from another format -- just once */
  102. struct ast_trans_pvt *trans;
  103. struct ast_tranlator_pvt *tr;
  104. struct ast_format *lastwriteformat;
  105. int lasttimeout;
  106. struct ast_channel *owner;
  107. FILE *f;
  108. /*!
  109. * \brief frame produced by read, typically
  110. * \note This frame holds a fr.subclass.format ref.
  111. */
  112. struct ast_frame fr;
  113. char *buf; /*!< buffer pointed to by ast_frame; */
  114. void *_private; /*!< pointer to private buffer */
  115. const char *orig_chan_name;
  116. char *write_buffer;
  117. };
  118. /*!
  119. * \brief Register a new file format capability.
  120. * Adds a format to Asterisk's format abilities.
  121. * \retval 0 on success
  122. * \retval -1 on failure
  123. */
  124. int __ast_format_def_register(const struct ast_format_def *f, struct ast_module *mod);
  125. #define ast_format_def_register(f) __ast_format_def_register(f, ast_module_info->self)
  126. /*!
  127. * \brief Unregisters a file format
  128. * \param name the name of the format you wish to unregister
  129. * Unregisters a format based on the name of the format.
  130. * \retval 0 on success
  131. * \retval -1 on failure to unregister
  132. */
  133. int ast_format_def_unregister(const char *name);
  134. #if defined(__cplusplus) || defined(c_plusplus)
  135. }
  136. #endif
  137. #endif /* _ASTERISK_MOD_FORMAT_H */