FLVMetadata.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "FLVMetadata.h"
  2. #include "FLVUtil.h"
  3. #include <windows.h>
  4. /*
  5. (c) 2006 Nullsoft, Inc.
  6. Author: Ben Allison [email protected]
  7. */
  8. /*
  9. type - uint8 -
  10. length - uint16
  11. */
  12. AMFType *MakeObject(uint8_t type)
  13. {
  14. switch(type)
  15. {
  16. case AMFType::TYPE_DOUBLE: // double
  17. return new AMFDouble;
  18. case AMFType::TYPE_BOOL: // bool
  19. return new AMFBoolean;
  20. case AMFType::TYPE_STRING: // string
  21. return new AMFString;
  22. case AMFType::TYPE_OBJECT: // object
  23. return new AMFObj;
  24. case AMFType::TYPE_MOVIE: // movie (basically just a URL)
  25. return new AMFString;
  26. case AMFType::TYPE_NULL: // null
  27. return 0;
  28. case AMFType::TYPE_REFERENCE: // reference
  29. return 0;
  30. case AMFType::TYPE_MIXEDARRAY:
  31. return new AMFMixedArray;
  32. case AMFType::TYPE_TERMINATOR:
  33. return new AMFTerminator;
  34. case AMFType::TYPE_ARRAY:
  35. return new AMFArray;
  36. case AMFType::TYPE_DATE: // date
  37. return new AMFTime;
  38. case AMFType::TYPE_LONG_STRING: // long string
  39. return new AMFLongString;
  40. case AMFType::TYPE_XML: // XML
  41. return 0;
  42. default:
  43. return 0;
  44. }
  45. }
  46. FLVMetadata::FLVMetadata()
  47. {
  48. }
  49. FLVMetadata::~FLVMetadata()
  50. {
  51. for ( FLVMetadata::Tag *tag : tags )
  52. delete tag;
  53. }
  54. bool FLVMetadata::Read(uint8_t *data, size_t size)
  55. {
  56. // TODO: there can be multiple name/value pairs so we could read them all
  57. while(size)
  58. {
  59. uint8_t type=*data;
  60. data++;
  61. size--;
  62. if (type == 0 && size >= 2 && data[0] == 0 && data[1] == AMFType::TYPE_TERMINATOR) // check for terminator
  63. return true; // array is done
  64. if (type != AMFType::TYPE_STRING) // first entry is a string, verify this
  65. return false; // malformed, lets bail
  66. FLVMetadata::Tag *tag = new FLVMetadata::Tag;
  67. // read name
  68. size_t skip = tag->name.Read(data, size);
  69. data+=skip;
  70. size-=skip;
  71. type=*data;
  72. data++;
  73. size--;
  74. if (type != AMFType::TYPE_MIXEDARRAY) // second entry is an associative array, verify this
  75. {
  76. delete tag;
  77. return false; // malformed, lets bail
  78. }
  79. tag->parameters = new AMFMixedArray; // we're new'ing this because we need to reference count
  80. skip = tag->parameters->Read(data, size);
  81. data+=skip;
  82. size-=skip;
  83. tags.push_back(tag);
  84. }
  85. return true;
  86. }
  87. FLVMetadata::Tag::Tag() : parameters(0)
  88. {
  89. }
  90. FLVMetadata::Tag::~Tag()
  91. {
  92. if (parameters)
  93. parameters->Release();
  94. }