Ppvm.vala 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using Invercargill;
  2. namespace Ppub {
  3. public class VideoManifest {
  4. public const string TITLE = "title";
  5. public const string AUTHOR = "author";
  6. public const string COPYRIGHT = "copyright";
  7. public const string LICENCE = "licence";
  8. public const string MASTER = "master";
  9. public const string POSTER = "poster";
  10. public const string RATIO = "ratio";
  11. public const string DURATION = "duration";
  12. public const string DESCRIPTION_ASSET = "description";
  13. public const string VIDEO = "video";
  14. private Gee.HashMap<string, string> entries { get; set; }
  15. public Invercargill.Sequence<VideoDescription> streams { get; set; }
  16. public VideoManifest() {
  17. entries = new Gee.HashMap<string, string>();
  18. streams = Invercargill.empty<VideoDescription>().to_sequence();
  19. }
  20. public string? get_value(string name) {
  21. if (entries.has_key(name)) {
  22. return entries.get(name);
  23. }
  24. return null;
  25. }
  26. public void set_value(string name, string value) {
  27. entries.set(name, value);
  28. }
  29. public string? title {
  30. owned get {
  31. return get_value(TITLE);
  32. }
  33. set {
  34. set_value(TITLE, value);
  35. }
  36. }
  37. public string? author {
  38. owned get {
  39. return get_value(AUTHOR);
  40. }
  41. set {
  42. set_value(AUTHOR, value);
  43. }
  44. }
  45. public string? copyright {
  46. owned get {
  47. return get_value(COPYRIGHT);
  48. }
  49. set {
  50. set_value(COPYRIGHT, value);
  51. }
  52. }
  53. public string? licence {
  54. owned get {
  55. return get_value(LICENCE);
  56. }
  57. set {
  58. set_value(LICENCE, value);
  59. }
  60. }
  61. public string? master {
  62. owned get {
  63. return get_value(MASTER);
  64. }
  65. set {
  66. set_value(MASTER, value);
  67. }
  68. }
  69. public string? poster {
  70. owned get {
  71. return get_value(POSTER);
  72. }
  73. set {
  74. set_value(POSTER, value);
  75. }
  76. }
  77. public double[]? ratio {
  78. owned get {
  79. var ratio = get_value(RATIO);
  80. if(ratio == null) {
  81. return null;
  82. }
  83. var ratio_data = ratio.split(":", 2);
  84. return new double[] {
  85. double.parse(ratio_data[0]),
  86. double.parse(ratio_data[1])
  87. };
  88. }
  89. set {
  90. set_value(RATIO, @"$(value[0]):$(value[1])");
  91. }
  92. }
  93. public double? duration {
  94. owned get {
  95. var duration = get_value(DURATION);
  96. if(duration == null) {
  97. return null;
  98. }
  99. return double.parse(duration);
  100. }
  101. set {
  102. set_value(DURATION, value.to_string());
  103. }
  104. }
  105. public string? description_file_name {
  106. owned get {
  107. return get_value(DESCRIPTION_ASSET);
  108. }
  109. set {
  110. set_value(DESCRIPTION_ASSET, value);
  111. }
  112. }
  113. public VideoManifest.from_stream(InputStream stream) throws IOError {
  114. var dis = new DataInputStream(stream);
  115. entries = new Gee.HashMap<string, string>();
  116. streams = new Invercargill.Sequence<VideoDescription>();
  117. var metadata_complete = false;
  118. string line = dis.read_line();
  119. if(line != "PPVM") {
  120. throw new IOError.INVALID_DATA("Stream does not begin with PPVM magic number");
  121. }
  122. while((line = dis.read_line()) != null) {
  123. if(line.chomp().chug() == "") {
  124. metadata_complete = true;
  125. continue;
  126. }
  127. var kv = line.split(": ", 2);
  128. if(!metadata_complete) {
  129. print(@"[Stream reader] Property $(kv[0]) is $(kv[1])\n");
  130. entries.set(kv[0], kv[1]);
  131. }
  132. else {
  133. if(kv[0] == VIDEO) {
  134. streams.add(new VideoDescription.from_string(kv[1]));
  135. }
  136. }
  137. }
  138. }
  139. public string to_string() {
  140. var data = "PPVM\n";
  141. foreach (var key in entries.keys) {
  142. data += @"$key: $(entries[key])\n";
  143. }
  144. data += "\n";
  145. foreach (var stream in streams) {
  146. data += @"$VIDEO: $(stream.to_string())\n";
  147. }
  148. data += "\n";
  149. return data;
  150. }
  151. }
  152. public class VideoDescription {
  153. public string version_label {get; set;}
  154. public string file_name {get; set;}
  155. public const string SIZE = "size";
  156. public const string CODECS = "codecs";
  157. private Gee.HashMap<string, string> properties { get; set; }
  158. public VideoDescription() {
  159. properties = new Gee.HashMap<string, string>();
  160. }
  161. public VideoDescription.from_string(string line) {
  162. var fields = line.split(", ", 3);
  163. version_label = fields[0];
  164. file_name = fields[1];
  165. properties = new Gee.HashMap<string, string>();
  166. var property_items = fields[2].split(";");
  167. foreach (var item in property_items) {
  168. if(item.chomp().chug() == ""){
  169. continue;
  170. }
  171. var item_parts = item.split("=\"", 2);
  172. var name = item_parts[0].chomp().chug();
  173. var value = item_parts[1].split("\"")[0];
  174. print(@"prop: $(name) = $(value)\n");
  175. properties.set(name, value);
  176. }
  177. }
  178. public string to_string() {
  179. var meta = "";
  180. foreach(var key in properties.keys) {
  181. meta += @" $key=\"$(properties[key])\";";
  182. }
  183. return @"$version_label, $file_name,$meta";
  184. }
  185. public string? get_property(string name) {
  186. if (properties.has_key(name)) {
  187. return properties.get(name);
  188. }
  189. return null;
  190. }
  191. public void set_property(string name, string value) {
  192. properties.set(name, value);
  193. }
  194. public string? codecs {
  195. owned get {
  196. return get_property(CODECS);
  197. }
  198. set {
  199. set_property(CODECS, value);
  200. }
  201. }
  202. public string? size {
  203. owned get {
  204. return get_property(SIZE);
  205. }
  206. set {
  207. set_property(SIZE, value);
  208. }
  209. }
  210. }
  211. }